-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathremote.js
241 lines (215 loc) · 7.01 KB
/
remote.js
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
import net from 'net';
import { connectToFirefox as defaultFirefoxConnector } from './rdp-client.js';
import { createLogger } from '../util/logger.js';
import {
isErrorWithCode,
RemoteTempInstallNotSupported,
UsageError,
WebExtError,
} from '../errors.js';
const log = createLogger(import.meta.url);
// NOTE: this type aliases Object to catch any other possible response.
// Convert a request rejection to a message string.
function requestErrorToMessage(err) {
if (err instanceof Error) {
return String(err);
}
return `${err.error}: ${err.message}`;
}
export class RemoteFirefox {
client;
checkedForAddonReloading;
constructor(client) {
this.client = client;
this.checkedForAddonReloading = false;
client.on('disconnect', () => {
log.debug('Received "disconnect" from Firefox client');
});
client.on('end', () => {
log.debug('Received "end" from Firefox client');
});
client.on('unsolicited-event', (info) => {
log.debug(`Received message from client: ${JSON.stringify(info)}`);
});
client.on('rdp-error', (rdpError) => {
log.debug(`Received error from client: ${JSON.stringify(rdpError)}`);
});
client.on('error', (error) => {
log.debug(`Received error from client: ${String(error)}`);
});
}
disconnect() {
this.client.disconnect();
}
async addonRequest(addon, request) {
try {
const response = await this.client.request({
to: addon.actor,
type: request,
});
return response;
} catch (err) {
log.debug(`Client responded to '${request}' request with error:`, err);
const message = requestErrorToMessage(err);
throw new WebExtError(`Remote Firefox: addonRequest() error: ${message}`);
}
}
async getAddonsActor() {
try {
// getRoot should work since Firefox 55 (bug 1352157).
const response = await this.client.request('getRoot');
if (response.addonsActor == null) {
return Promise.reject(
new RemoteTempInstallNotSupported(
'This version of Firefox does not provide an add-ons actor for ' +
'remote installation.',
),
);
}
return response.addonsActor;
} catch (err) {
// Fallback to listTabs otherwise, Firefox 49 - 77 (bug 1618691).
log.debug('Falling back to listTabs because getRoot failed', err);
}
try {
const response = await this.client.request('listTabs');
// addonsActor was added to listTabs in Firefox 49 (bug 1273183).
if (response.addonsActor == null) {
log.debug(
'listTabs returned a falsey addonsActor: ' +
`${JSON.stringify(response)}`,
);
return Promise.reject(
new RemoteTempInstallNotSupported(
'This is an older version of Firefox that does not provide an ' +
'add-ons actor for remote installation. Try Firefox 49 or ' +
'higher.',
),
);
}
return response.addonsActor;
} catch (err) {
log.debug('listTabs error', err);
const message = requestErrorToMessage(err);
throw new WebExtError(`Remote Firefox: listTabs() error: ${message}`);
}
}
async installTemporaryAddon(addonPath, openDevTools) {
const addonsActor = await this.getAddonsActor();
try {
const response = await this.client.request({
to: addonsActor,
type: 'installTemporaryAddon',
addonPath,
openDevTools,
});
log.debug(`installTemporaryAddon: ${JSON.stringify(response)}`);
log.info(`Installed ${addonPath} as a temporary add-on`);
return response;
} catch (err) {
const message = requestErrorToMessage(err);
throw new WebExtError(`installTemporaryAddon: Error: ${message}`);
}
}
async getInstalledAddon(addonId) {
try {
const response = await this.client.request('listAddons');
for (const addon of response.addons) {
if (addon.id === addonId) {
return addon;
}
}
log.debug(
`Remote Firefox has these addons: ${response.addons.map((a) => a.id)}`,
);
return Promise.reject(
new WebExtError(
'The remote Firefox does not have your extension installed',
),
);
} catch (err) {
const message = requestErrorToMessage(err);
throw new WebExtError(`Remote Firefox: listAddons() error: ${message}`);
}
}
async checkForAddonReloading(addon) {
if (this.checkedForAddonReloading) {
// We only need to check once if reload() is supported.
return addon;
} else {
const response = await this.addonRequest(addon, 'requestTypes');
if (response.requestTypes.indexOf('reload') === -1) {
const supportedRequestTypes = JSON.stringify(response.requestTypes);
log.debug(`Remote Firefox only supports: ${supportedRequestTypes}`);
throw new UsageError(
'This Firefox version does not support add-on reloading. ' +
'Re-run with --no-reload',
);
} else {
this.checkedForAddonReloading = true;
return addon;
}
}
}
async reloadAddon(addonId) {
const addon = await this.getInstalledAddon(addonId);
await this.checkForAddonReloading(addon);
await this.addonRequest(addon, 'reload');
process.stdout.write(
`\rLast extension reload: ${new Date().toTimeString()}`,
);
log.debug('\n');
}
}
// Connect types and implementation
export async function connect(
port,
{ connectToFirefox = defaultFirefoxConnector } = {},
) {
log.debug(`Connecting to Firefox on port ${port}`);
const client = await connectToFirefox(port);
log.debug(`Connected to the remote Firefox debugger on port ${port}`);
return new RemoteFirefox(client);
}
// ConnectWithMaxRetries types and implementation
export async function connectWithMaxRetries(
// A max of 250 will try connecting for 30 seconds.
{ maxRetries = 250, retryInterval = 120, port },
{ connectToFirefox = connect } = {},
) {
async function establishConnection() {
var lastError;
for (let retries = 0; retries <= maxRetries; retries++) {
try {
return await connectToFirefox(port);
} catch (error) {
if (isErrorWithCode('ECONNREFUSED', error)) {
// Wait for `retryInterval` ms.
await new Promise((resolve) => {
setTimeout(resolve, retryInterval);
});
lastError = error;
log.debug(
`Retrying Firefox (${retries}); connection error: ${error}`,
);
} else {
log.error(error.stack);
throw error;
}
}
}
log.debug('Connect to Firefox debugger: too many retries');
throw lastError;
}
log.debug('Connecting to the remote Firefox debugger');
return establishConnection();
}
export function findFreeTcpPort() {
return new Promise((resolve) => {
const srv = net.createServer();
srv.listen(0, '127.0.0.1', () => {
const freeTcpPort = srv.address().port;
srv.close(() => resolve(freeTcpPort));
});
});
}