-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
transport.go
141 lines (123 loc) · 3.04 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package playwright
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"os"
"github.com/go-jose/go-jose/v3/json"
)
type transport interface {
Send(msg map[string]interface{}) error
Poll() (*message, error)
Close() error
}
type pipeTransport struct {
writer io.WriteCloser
bufReader *bufio.Reader
closed chan struct{}
onClose func() error
}
func (t *pipeTransport) Poll() (*message, error) {
if t.isClosed() {
return nil, fmt.Errorf("transport closed")
}
var length uint32
err := binary.Read(t.bufReader, binary.LittleEndian, &length)
if err != nil {
return nil, fmt.Errorf("could not read protocol padding: %w", err)
}
data := make([]byte, length)
_, err = io.ReadFull(t.bufReader, data)
if err != nil {
return nil, fmt.Errorf("could not read protocol data: %w", err)
}
msg := &message{}
if err := json.Unmarshal(data, &msg); err != nil {
return nil, fmt.Errorf("could not decode json: %w", err)
}
if os.Getenv("DEBUGP") != "" {
fmt.Fprintf(os.Stdout, "\x1b[33mRECV>\x1b[0m\n%s\n", data)
}
return msg, nil
}
type message struct {
ID int `json:"id"`
GUID string `json:"guid"`
Method string `json:"method,omitempty"`
Params map[string]interface{} `json:"params,omitempty"`
Result map[string]interface{} `json:"result,omitempty"`
Error *struct {
Error Error `json:"error"`
} `json:"error,omitempty"`
}
func (t *pipeTransport) Send(msg map[string]interface{}) error {
if t.isClosed() {
return fmt.Errorf("transport closed")
}
msgBytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("pipeTransport: could not marshal json: %w", err)
}
if os.Getenv("DEBUGP") != "" {
fmt.Fprintf(os.Stdout, "\x1b[32mSEND>\x1b[0m\n%s\n", msgBytes)
}
lengthPadding := make([]byte, 4)
binary.LittleEndian.PutUint32(lengthPadding, uint32(len(msgBytes)))
if _, err = t.writer.Write(append(lengthPadding, msgBytes...)); err != nil {
return err
}
return nil
}
func (t *pipeTransport) Close() error {
select {
case <-t.closed:
return nil
default:
return t.onClose()
}
}
func (t *pipeTransport) isClosed() bool {
select {
case <-t.closed:
return true
default:
return false
}
}
func newPipeTransport(driver *PlaywrightDriver, stderr io.Writer) (transport, error) {
t := &pipeTransport{
closed: make(chan struct{}, 1),
}
cmd := driver.Command("run-driver")
cmd.Stderr = stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("could not create stdin pipe: %w", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("could not create stdout pipe: %w", err)
}
t.writer = stdin
t.bufReader = bufio.NewReader(stdout)
t.onClose = func() error {
select {
case <-t.closed:
default:
close(t.closed)
}
if err := t.writer.Close(); err != nil {
return err
}
// playwright-cli will exit when its stdin is closed
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("could not start driver: %w", err)
}
return t, nil
}