This repository has been archived by the owner on Nov 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 573
/
local.ts
141 lines (118 loc) · 4.25 KB
/
local.ts
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
import { Chrome, Command, ChromelessOptions, Client } from '../types'
import * as CDP from 'chrome-remote-interface'
import { LaunchedChrome, launch } from 'chrome-launcher'
import LocalRuntime from './local-runtime'
import { evaluate, setViewport } from '../util'
import { DeviceMetrics } from '../types'
interface RuntimeClient {
client: Client
runtime: LocalRuntime
}
export default class LocalChrome implements Chrome {
private options: ChromelessOptions
private runtimeClientPromise: Promise<RuntimeClient>
private chromeInstance?: LaunchedChrome
constructor(options: ChromelessOptions = {}) {
this.options = options
this.runtimeClientPromise = this.initRuntimeClient()
}
private async initRuntimeClient(): Promise<RuntimeClient> {
const client = this.options.launchChrome
? await this.startChrome()
: await this.connectToChrome()
const { viewport = {} as DeviceMetrics } = this.options
await setViewport(client, viewport as DeviceMetrics)
const runtime = new LocalRuntime(client, this.options)
return { client, runtime }
}
private async startChrome(): Promise<Client> {
const { port } = this.options.cdp
this.chromeInstance = await launch({
logLevel: this.options.debug ? 'info' : 'silent',
chromeFlags: [
// Do not render scroll bars
'--hide-scrollbars',
// The following options copied verbatim from https://github.com/GoogleChrome/chrome-launcher/blob/master/src/flags.ts
// Disable built-in Google Translate service
'--disable-translate',
// Disable all chrome extensions entirely
'--disable-extensions',
// Disable various background network services, including extension updating,
// safe browsing service, upgrade detector, translate, UMA
'--disable-background-networking',
// Disable fetching safebrowsing lists, likely redundant due to disable-background-networking
'--safebrowsing-disable-auto-update',
// Disable syncing to a Google account
'--disable-sync',
// Disable reporting to UMA, but allows for collection
'--metrics-recording-only',
// Mute any audio
'--mute-audio',
// Skip first run wizards
'--no-first-run',
],
port,
})
const target = await CDP.New({
port,
})
return await CDP({ target, port })
}
private async connectToChrome(): Promise<Client> {
const { host, port } = this.options.cdp
const target = await CDP.New({
port,
host,
})
return await CDP({ target, host, port })
}
private async setViewport(client: Client) {
const { viewport = {} } = this.options
const config: any = {
deviceScaleFactor: 1,
mobile: false,
scale: viewport.scale || 1,
fitWindow: false, // as we cannot resize the window, `fitWindow: false` is needed in order for the viewport to be resizable
}
const { host, port } = this.options.cdp
const versionResult = await CDP.Version({ host, port })
const isHeadless = versionResult['User-Agent'].includes('Headless')
if (viewport.height && viewport.width) {
config.height = viewport.height
config.width = viewport.width
} else if (isHeadless) {
// just apply default value in headless mode to maintain original browser viewport
config.height = 900
config.width = 1440
} else {
config.height = await evaluate(
client,
(() => window.innerHeight).toString(),
)
config.width = await evaluate(
client,
(() => window.innerWidth).toString(),
)
}
await client.Emulation.setDeviceMetricsOverride(config)
await client.Emulation.setVisibleSize({
width: config.width,
height: config.height,
})
}
async process<T extends any>(command: Command): Promise<T> {
const { runtime } = await this.runtimeClientPromise
return (await runtime.run(command)) as T
}
async close(): Promise<void> {
const { client } = await this.runtimeClientPromise
if (this.options.cdp.closeTab) {
const { host, port } = this.options.cdp
await CDP.Close({ host, port, id: client.target.id })
}
if (this.chromeInstance) {
this.chromeInstance.kill()
}
await client.close()
}
}