forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
324 lines (300 loc) · 8.69 KB
/
run.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package playwright
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const (
playwrightCliVersion = "1.25.2"
baseURL = "https://playwright.azureedge.net/builds/driver"
)
type PlaywrightDriver struct {
DriverDirectory, DriverBinaryLocation, Version string
options *RunOptions
}
func NewDriver(options *RunOptions) (*PlaywrightDriver, error) {
baseDriverDirectory := options.DriverDirectory
if baseDriverDirectory == "" {
var err error
baseDriverDirectory, err = getDefaultCacheDirectory()
if err != nil {
return nil, fmt.Errorf("could not get default cache directory: %v", err)
}
}
driverDirectory := filepath.Join(baseDriverDirectory, "ms-playwright-go", playwrightCliVersion)
driverBinaryLocation := filepath.Join(driverDirectory, getDriverName())
return &PlaywrightDriver{
options: options,
DriverBinaryLocation: driverBinaryLocation,
DriverDirectory: driverDirectory,
Version: playwrightCliVersion,
}, nil
}
func getDefaultCacheDirectory() (string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not get user home directory: %v", err)
}
switch runtime.GOOS {
case "windows":
return filepath.Join(userHomeDir, "AppData", "Local"), nil
case "darwin":
return filepath.Join(userHomeDir, "Library", "Caches"), nil
case "linux":
return filepath.Join(userHomeDir, ".cache"), nil
}
return "", errors.New("could not determine cache directory")
}
func (d *PlaywrightDriver) isUpToDateDriver() (bool, error) {
if _, err := os.Stat(d.DriverDirectory); os.IsNotExist(err) {
if err := os.MkdirAll(d.DriverDirectory, 0777); err != nil {
return false, fmt.Errorf("could not create driver directory: %w", err)
}
}
if _, err := os.Stat(d.DriverBinaryLocation); os.IsNotExist(err) {
return false, nil
}
cmd := exec.Command(d.DriverBinaryLocation, "--version")
output, err := cmd.Output()
if err != nil {
return false, fmt.Errorf("could not run driver: %w", err)
}
if bytes.Contains(output, []byte(d.Version)) {
return true, nil
}
return false, nil
}
func (d *PlaywrightDriver) install() error {
if err := d.DownloadDriver(); err != nil {
return fmt.Errorf("could not install driver: %w", err)
}
if d.options.SkipInstallBrowsers {
return nil
}
if d.options.Verbose {
log.Println("Downloading browsers...")
}
if err := d.installBrowsers(d.DriverBinaryLocation); err != nil {
return fmt.Errorf("could not install browsers: %w", err)
}
if d.options.Verbose {
log.Println("Downloaded browsers successfully")
}
return nil
}
func (d *PlaywrightDriver) DownloadDriver() error {
up2Date, err := d.isUpToDateDriver()
if err != nil {
return fmt.Errorf("could not check if driver is up2date: %w", err)
}
if up2Date {
return nil
}
if d.options.Verbose {
log.Printf("Downloading driver to %s", d.DriverDirectory)
}
driverURL := d.getDriverURL()
resp, err := http.Get(driverURL)
if err != nil {
return fmt.Errorf("could not download driver: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error: got non 200 status code: %d (%s)", resp.StatusCode, resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read response body: %w", err)
}
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return fmt.Errorf("could not read zip content: %w", err)
}
for _, zipFile := range zipReader.File {
zipFileDiskPath := filepath.Join(d.DriverDirectory, zipFile.Name)
if zipFile.FileInfo().IsDir() {
if err := os.MkdirAll(zipFileDiskPath, os.ModePerm); err != nil {
return fmt.Errorf("could not create directory: %w", err)
}
continue
}
outFile, err := os.Create(zipFileDiskPath)
if err != nil {
return fmt.Errorf("could not create driver: %w", err)
}
file, err := zipFile.Open()
if err != nil {
return fmt.Errorf("could not open zip file: %w", err)
}
if _, err = io.Copy(outFile, file); err != nil {
return fmt.Errorf("could not copy response body to file: %w", err)
}
if err := outFile.Close(); err != nil {
return fmt.Errorf("could not close file (driver): %w", err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("could not close file (zip file): %w", err)
}
if zipFile.Mode().Perm()&0100 != 0 && runtime.GOOS != "windows" {
if err := makeFileExecutable(zipFileDiskPath); err != nil {
return fmt.Errorf("could not make executable: %w", err)
}
}
}
if d.options.Verbose {
log.Println("Downloaded driver successfully")
}
return nil
}
func (d *PlaywrightDriver) run() (*connection, error) {
cmd := exec.Command(d.DriverBinaryLocation, "run-driver")
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("could not get stdin pipe: %w", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("could not get stdout pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("could not start driver: %w", err)
}
transport := newPipeTransport(stdin, stdout)
go func() {
if err := transport.Start(); err != nil {
log.Fatal(err)
}
}()
connection := newConnection(func() error {
if err := stdin.Close(); err != nil {
return fmt.Errorf("could not close stdin: %v", err)
}
if err := stdout.Close(); err != nil {
return fmt.Errorf("could not close stdout: %v", err)
}
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("could not kill process: %v", err)
}
if _, err := cmd.Process.Wait(); err != nil {
return fmt.Errorf("could not wait for process: %v", err)
}
return nil
})
connection.onmessage = transport.Send
transport.onmessage = connection.Dispatch
return connection, nil
}
func (d *PlaywrightDriver) installBrowsers(driverPath string) error {
additionalArgs := []string{"install"}
if d.options.Browsers != nil {
additionalArgs = append(additionalArgs, d.options.Browsers...)
}
cmd := exec.Command(driverPath, additionalArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("could not install browsers: %w", err)
}
return nil
}
// RunOptions are custom options to run the driver
type RunOptions struct {
DriverDirectory string
SkipInstallBrowsers bool
Browsers []string
Verbose bool
}
// Install does download the driver and the browsers. If not called manually
// before playwright.Run() it will get executed there and might take a few seconds
// to download the Playwright suite.
func Install(options ...*RunOptions) error {
driver, err := NewDriver(transformRunOptions(options))
if err != nil {
return fmt.Errorf("could not get driver instance: %w", err)
}
if err := driver.install(); err != nil {
return fmt.Errorf("could not install driver: %w", err)
}
return nil
}
// Run starts a Playwright instance
func Run(options ...*RunOptions) (*Playwright, error) {
driver, err := NewDriver(transformRunOptions(options))
if err != nil {
return nil, fmt.Errorf("could not get driver instance: %w", err)
}
connection, err := driver.run()
if err != nil {
return nil, err
}
playwright := connection.Start()
return playwright, nil
}
func transformRunOptions(options []*RunOptions) *RunOptions {
if len(options) == 1 {
return options[0]
}
return &RunOptions{
Verbose: true,
}
}
func getDriverName() string {
switch runtime.GOOS {
case "windows":
return "playwright.cmd"
case "darwin":
fallthrough
case "linux":
return "playwright.sh"
}
panic("Not supported OS!")
}
func (d *PlaywrightDriver) getDriverURL() string {
platform := ""
switch runtime.GOOS {
case "windows":
platform = "win32_x64"
case "darwin":
if runtime.GOARCH == "arm64" {
platform = "mac-arm64"
} else {
platform = "mac"
}
case "linux":
if runtime.GOARCH == "arm64" {
platform = "linux-arm64"
} else {
platform = "linux"
}
}
if d.isReleaseVersion() {
return fmt.Sprintf("%s/playwright-%s-%s.zip", baseURL, d.Version, platform)
}
return fmt.Sprintf("%s/next/playwright-%s-%s.zip", baseURL, d.Version, platform)
}
// isReleaseVersion checks if the version is not a beta or alpha release
// this helps to determine the url from where to download the driver
func (d *PlaywrightDriver) isReleaseVersion() bool {
return !strings.Contains(d.Version, "beta") && !strings.Contains(d.Version, "alpha")
}
func makeFileExecutable(path string) error {
stats, err := os.Stat(path)
if err != nil {
return fmt.Errorf("could not stat driver: %w", err)
}
if err := os.Chmod(path, stats.Mode()|0x40); err != nil {
return fmt.Errorf("could not set permissions: %w", err)
}
return nil
}