@lynx-js/devtool-connector 0.1.1 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +1 -1
- package/README.md +152 -0
- package/dist/187.js +272 -0
- package/dist/390.js +102 -0
- package/dist/43.js +20 -0
- package/dist/556.js +253 -0
- package/dist/63.js +93 -0
- package/dist/677.js +19 -0
- package/dist/712.js +180 -0
- package/dist/872.js +771 -0
- package/dist/962.js +12150 -0
- package/dist/client-id.d.ts +7 -0
- package/dist/daemon/device-connection.d.ts +36 -0
- package/dist/daemon/entry.d.ts +1 -0
- package/dist/daemon/entry.js +46 -0
- package/dist/daemon/index.d.ts +1 -0
- package/dist/daemon/index.js +1 -0
- package/dist/daemon/manager.d.ts +14 -0
- package/dist/daemon/protocol.d.ts +73 -0
- package/dist/daemon/server.d.ts +14 -0
- package/dist/daemon/static-server.d.ts +5 -0
- package/dist/daemon/tarball-cache.d.ts +16 -0
- package/dist/daemon/version.d.ts +1 -0
- package/dist/index.d.ts +47 -19
- package/dist/index.js +188 -349
- package/dist/rslib-runtime.js +0 -20
- package/dist/streams/cdp.d.ts +2 -4
- package/dist/streams/customized.d.ts +5 -4
- package/dist/streams/index.d.ts +3 -0
- package/dist/streams/index.js +1 -0
- package/dist/streams/utils.d.ts +11 -0
- package/dist/transport/android.d.ts +4 -3
- package/dist/transport/base.d.ts +9 -0
- package/dist/transport/daemon.d.ts +20 -0
- package/dist/transport/desktop.d.ts +4 -3
- package/dist/transport/index.d.ts +5 -0
- package/dist/transport/index.js +3 -11253
- package/dist/transport/ios.d.ts +3 -4
- package/dist/transport/transport.d.ts +7 -5
- package/dist/transport/usbmux.d.ts +49 -0
- package/dist/transport/ws-stream.d.ts +28 -0
- package/dist/types.d.ts +73 -19
- package/package.json +33 -15
- package/public/inspector-wrapper.html +539 -0
- package/dist/844.js +0 -632
- /package/dist/{streams → transport}/peertalk.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1,253 +1,10 @@
|
|
|
1
|
-
import { __webpack_require__ } from "./rslib-runtime.js";
|
|
2
1
|
import { randomInt } from "node:crypto";
|
|
3
|
-
import { ReadableStream
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import "./
|
|
8
|
-
|
|
9
|
-
return 'Register' === response.event;
|
|
10
|
-
}
|
|
11
|
-
function isListSessionResponse(response) {
|
|
12
|
-
return 'Customized' === response.event && 'SessionList' === response.data.type;
|
|
13
|
-
}
|
|
14
|
-
function isGetGlobalSwitchResponse(response) {
|
|
15
|
-
return 'Customized' === response.event && 'GetGlobalSwitch' === response.data.type;
|
|
16
|
-
}
|
|
17
|
-
function isSetGlobalSwitchResponse(response) {
|
|
18
|
-
return 'Customized' === response.event && 'SetGlobalSwitch' === response.data.type;
|
|
19
|
-
}
|
|
20
|
-
function isCustomizedResponseWithType(response, type) {
|
|
21
|
-
return 'Customized' === response.event && response.data.type === type;
|
|
22
|
-
}
|
|
23
|
-
class CustomizedRequestTransformStream extends TransformStream {
|
|
24
|
-
port;
|
|
25
|
-
constructor(options){
|
|
26
|
-
const { type, port, sessionId = -1, messageBuilder } = options;
|
|
27
|
-
super({
|
|
28
|
-
transform (chunk, controller) {
|
|
29
|
-
const sid = 'function' == typeof sessionId ? sessionId(chunk) : sessionId;
|
|
30
|
-
controller.enqueue({
|
|
31
|
-
event: 'Customized',
|
|
32
|
-
data: {
|
|
33
|
-
type,
|
|
34
|
-
data: {
|
|
35
|
-
client_id: port,
|
|
36
|
-
session_id: sid,
|
|
37
|
-
message: messageBuilder(chunk)
|
|
38
|
-
},
|
|
39
|
-
sender: port
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
this.port = port;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
class CustomizedResponseTransformStream extends TransformStream {
|
|
48
|
-
constructor(type, id){
|
|
49
|
-
super({
|
|
50
|
-
transform (response, controller) {
|
|
51
|
-
if (!isCustomizedResponseWithType(response, type)) return;
|
|
52
|
-
try {
|
|
53
|
-
const message = JSON.parse(response.data.data.message);
|
|
54
|
-
if (void 0 === id || message?.id === id) controller.enqueue(message);
|
|
55
|
-
} catch (err) {
|
|
56
|
-
controller.error(new Error(`Failed to parse response for type ${type}`, {
|
|
57
|
-
cause: err
|
|
58
|
-
}));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
class ResponseParserTransformStream extends TransformStream {
|
|
65
|
-
constructor(options){
|
|
66
|
-
const { parseResult, checkError } = options;
|
|
67
|
-
super({
|
|
68
|
-
transform (chunk, controller) {
|
|
69
|
-
const error = checkError(chunk);
|
|
70
|
-
if (error) return void controller.error(error);
|
|
71
|
-
try {
|
|
72
|
-
controller.enqueue(parseResult(chunk));
|
|
73
|
-
} catch (err) {
|
|
74
|
-
controller.error(err);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
class AppResponseTransformStream extends ResponseParserTransformStream {
|
|
81
|
-
constructor(method){
|
|
82
|
-
super({
|
|
83
|
-
checkError: (message)=>{
|
|
84
|
-
try {
|
|
85
|
-
const result = JSON.parse(message.result);
|
|
86
|
-
if (0 !== result.code && '0' !== result.code) return new Error(`App request ${method} error: ${result.message}`, {
|
|
87
|
-
cause: message
|
|
88
|
-
});
|
|
89
|
-
return null;
|
|
90
|
-
} catch (err) {
|
|
91
|
-
return new Error('Failed to parse App response message', {
|
|
92
|
-
cause: err
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
parseResult: (message)=>JSON.parse(message.result)
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
class GlobalSwitchRequestTransformStream extends CustomizedRequestTransformStream {
|
|
101
|
-
constructor(type, port){
|
|
102
|
-
super({
|
|
103
|
-
type,
|
|
104
|
-
port,
|
|
105
|
-
sessionId: -1,
|
|
106
|
-
messageBuilder: ({ key, value })=>({
|
|
107
|
-
global_key: key,
|
|
108
|
-
global_value: value
|
|
109
|
-
})
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
class CDPRequestTransformStream extends CustomizedRequestTransformStream {
|
|
114
|
-
constructor(port, fixedId){
|
|
115
|
-
super({
|
|
116
|
-
type: 'CDP',
|
|
117
|
-
port,
|
|
118
|
-
sessionId: (chunk)=>chunk.sessionId,
|
|
119
|
-
messageBuilder: (chunk)=>{
|
|
120
|
-
const id = fixedId ?? randomInt(10000, 50000);
|
|
121
|
-
const { method, params } = chunk;
|
|
122
|
-
return {
|
|
123
|
-
id,
|
|
124
|
-
method,
|
|
125
|
-
params
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
class CDPResponseTransformStream extends CustomizedResponseTransformStream {
|
|
132
|
-
constructor(id){
|
|
133
|
-
super('CDP', id);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
class CDPOutputTransformStream extends ResponseParserTransformStream {
|
|
137
|
-
constructor(){
|
|
138
|
-
super({
|
|
139
|
-
checkError: (message)=>{
|
|
140
|
-
if ('error' in message) return new Error(`CDP request error: ${message.error.message}`, {
|
|
141
|
-
cause: message
|
|
142
|
-
});
|
|
143
|
-
return null;
|
|
144
|
-
},
|
|
145
|
-
parseResult: (message)=>{
|
|
146
|
-
if ('result' in message) return message.result;
|
|
147
|
-
throw new Error('No result in CDP response message', {
|
|
148
|
-
cause: message
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
class PeertalkToMessageTransformStream extends TransformStream {
|
|
155
|
-
constructor(){
|
|
156
|
-
let buffer = new Uint8Array(0);
|
|
157
|
-
const decoder = new TextDecoder();
|
|
158
|
-
super({
|
|
159
|
-
transform: (chunk, c)=>{
|
|
160
|
-
const n = new Uint8Array(buffer.length + chunk.length);
|
|
161
|
-
n.set(buffer);
|
|
162
|
-
n.set(chunk, buffer.length);
|
|
163
|
-
buffer = n;
|
|
164
|
-
while(buffer.length >= 20){
|
|
165
|
-
const v = new DataView(buffer.buffer, buffer.byteOffset);
|
|
166
|
-
const len = v.getUint32(16);
|
|
167
|
-
if (buffer.length < 20 + len) break;
|
|
168
|
-
try {
|
|
169
|
-
c.enqueue(JSON.parse(decoder.decode(buffer.subarray(20, 20 + len))));
|
|
170
|
-
} catch (e) {
|
|
171
|
-
c.error(e);
|
|
172
|
-
}
|
|
173
|
-
buffer = buffer.subarray(20 + len);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
class MessageToPeertalkTransformStream extends TransformStream {
|
|
180
|
-
constructor(){
|
|
181
|
-
const encoder = new TextEncoder();
|
|
182
|
-
super({
|
|
183
|
-
transform (chunk, controller) {
|
|
184
|
-
const body = encoder.encode(JSON.stringify(chunk));
|
|
185
|
-
const len = body.length;
|
|
186
|
-
const data = new Uint8Array(20 + len);
|
|
187
|
-
const view = new DataView(data.buffer);
|
|
188
|
-
view.setUint32(0, 1);
|
|
189
|
-
view.setUint32(4, 101);
|
|
190
|
-
view.setUint32(8, 0);
|
|
191
|
-
view.setUint32(12, len + 4);
|
|
192
|
-
view.setUint32(16, len);
|
|
193
|
-
data.set(body, 20);
|
|
194
|
-
controller.enqueue(data);
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
class FilterTransformStream extends TransformStream {
|
|
200
|
-
constructor(filter){
|
|
201
|
-
super({
|
|
202
|
-
transform (chunk, controller) {
|
|
203
|
-
if (filter(chunk)) controller.enqueue(chunk);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
class InspectStream extends TransformStream {
|
|
209
|
-
constructor(callback){
|
|
210
|
-
super({
|
|
211
|
-
transform (chunk, controller) {
|
|
212
|
-
callback(chunk);
|
|
213
|
-
controller.enqueue(chunk);
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
const src = __webpack_require__("../../../node_modules/.pnpm/debug@4.4.3/node_modules/debug/src/index.js");
|
|
219
|
-
var src_default = /*#__PURE__*/ __webpack_require__.n(src);
|
|
220
|
-
const debug = src_default()('devtool-mcp-server:takeover');
|
|
221
|
-
const DEBUG_ROUTER_DIR = node_path.join(node_os.homedir(), '.DebugRouterConnector');
|
|
222
|
-
const DEBUG_ROUTER_LOCK_DIR = node_path.join(DEBUG_ROUTER_DIR, 'lockfile');
|
|
223
|
-
const DEBUG_ROUTER_LATEST_FILE = node_path.join(DEBUG_ROUTER_DIR, 'LatestDriverProcess');
|
|
224
|
-
async function takeoverDebugRouterLock() {
|
|
225
|
-
try {
|
|
226
|
-
await promises.mkdir(DEBUG_ROUTER_DIR, {
|
|
227
|
-
recursive: true
|
|
228
|
-
});
|
|
229
|
-
await promises.rm(DEBUG_ROUTER_LOCK_DIR, {
|
|
230
|
-
recursive: true,
|
|
231
|
-
force: true
|
|
232
|
-
});
|
|
233
|
-
await promises.mkdir(DEBUG_ROUTER_LOCK_DIR, {
|
|
234
|
-
recursive: true
|
|
235
|
-
});
|
|
236
|
-
await promises.writeFile(DEBUG_ROUTER_LATEST_FILE, `${process.pid}`, 'utf-8');
|
|
237
|
-
debug(`wrote PID=${process.pid}`);
|
|
238
|
-
} catch (err) {
|
|
239
|
-
debug('skipped due to filesystem error %O', err);
|
|
240
|
-
} finally{
|
|
241
|
-
try {
|
|
242
|
-
await promises.rm(DEBUG_ROUTER_LOCK_DIR, {
|
|
243
|
-
recursive: true,
|
|
244
|
-
force: true
|
|
245
|
-
});
|
|
246
|
-
} catch (_cleanupError) {
|
|
247
|
-
debug('failed to remove lock directory %O', _cleanupError);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
2
|
+
import { ReadableStream } from "node:stream/web";
|
|
3
|
+
import { CDPOutputTransformStream, GlobalSwitchRequestTransformStream, CustomizedResponseTransformStream, AppResponseTransformStream, CDPRequestTransformStream, CustomizedRequestTransformStream, SessionGuardTransformStream, FilterTransformStream, InspectStream, CustomizedClientIdTransformStream, CDPResponseTransformStream } from "./712.js";
|
|
4
|
+
import { DaemonTransport } from "./556.js";
|
|
5
|
+
import { isListSessionResponse, isGetGlobalSwitchResponse, isHeadlessPrepareResponse, isInitializeResponse, isSetGlobalSwitchResponse } from "./677.js";
|
|
6
|
+
import { ClientId } from "./43.js";
|
|
7
|
+
import { node_createDebug } from "./187.js";
|
|
251
8
|
function _ts_add_disposable_resource(env, value, async) {
|
|
252
9
|
if (null != value) {
|
|
253
10
|
if ("object" != typeof value && "function" != typeof value) throw new TypeError("Object expected.");
|
|
@@ -309,36 +66,34 @@ function src_ts_dispose_resources(env) {
|
|
|
309
66
|
return next();
|
|
310
67
|
})(env);
|
|
311
68
|
}
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
return `${encodeURIComponent(deviceId)}:${port}`;
|
|
316
|
-
}
|
|
317
|
-
static deserialize(clientId) {
|
|
318
|
-
try {
|
|
319
|
-
const lastColonIndex = clientId.lastIndexOf(':');
|
|
320
|
-
if (-1 === lastColonIndex) return null;
|
|
321
|
-
const port = Number.parseInt(clientId.substring(lastColonIndex + 1), 10);
|
|
322
|
-
if (Number.isNaN(port)) return null;
|
|
323
|
-
return {
|
|
324
|
-
deviceId: decodeURIComponent(clientId.substring(0, lastColonIndex)),
|
|
325
|
-
port
|
|
326
|
-
};
|
|
327
|
-
} catch {
|
|
328
|
-
return null;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
69
|
+
const debug = node_createDebug('devtool-mcp-server:connector');
|
|
70
|
+
function hasClientList(transport) {
|
|
71
|
+
return 'function' == typeof transport.listClients;
|
|
331
72
|
}
|
|
332
73
|
class Connector {
|
|
333
74
|
#transports;
|
|
75
|
+
#daemonTransports;
|
|
334
76
|
constructor(transports){
|
|
335
77
|
this.#transports = transports;
|
|
78
|
+
this.#daemonTransports = transports.filter((t)=>t instanceof DaemonTransport);
|
|
336
79
|
}
|
|
337
80
|
async listClients() {
|
|
338
|
-
const
|
|
81
|
+
const daemonClientResults = await Promise.allSettled(this.#daemonTransports.map(async (transport)=>{
|
|
82
|
+
const clients = await transport.listClients();
|
|
83
|
+
await Promise.allSettled(clients.flatMap(({ id })=>this.#setupClient(transport, id)));
|
|
84
|
+
return clients;
|
|
85
|
+
}));
|
|
86
|
+
const fulfilledDaemonClientResults = daemonClientResults.filter((r)=>'fulfilled' === r.status);
|
|
87
|
+
const daemonClients = fulfilledDaemonClientResults.flatMap((r)=>r.value);
|
|
88
|
+
if (fulfilledDaemonClientResults.length > 0) {
|
|
89
|
+
debug('Using clients from daemon transport: %o', daemonClients);
|
|
90
|
+
return daemonClients;
|
|
91
|
+
}
|
|
92
|
+
const transportDevices = await Promise.allSettled(this.#transports.filter((t)=>!(t instanceof DaemonTransport)).map(async (transport)=>({
|
|
339
93
|
transport,
|
|
340
94
|
devices: await transport.listDevices()
|
|
341
95
|
})));
|
|
96
|
+
for (const result of transportDevices)if ('rejected' === result.status) debug('listClients: listDevices failed on one transport: %O', result.reason);
|
|
342
97
|
const results = await Promise.allSettled(transportDevices.filter((r)=>'fulfilled' === r.status).map((r)=>r.value).flatMap(({ transport, devices })=>devices.flatMap(({ id })=>this.#listClientsForDevice(transport, id))));
|
|
343
98
|
return results.filter((r)=>'fulfilled' === r.status).flatMap((r)=>r.value);
|
|
344
99
|
}
|
|
@@ -346,9 +101,6 @@ class Connector {
|
|
|
346
101
|
const results = await Promise.allSettled(this.#transports.map((t)=>t.listDevices()));
|
|
347
102
|
return results.filter((result)=>'fulfilled' === result.status).flatMap(({ value })=>value);
|
|
348
103
|
}
|
|
349
|
-
async close() {
|
|
350
|
-
await Promise.allSettled(this.#transports.map((t)=>t.close()));
|
|
351
|
-
}
|
|
352
104
|
async listAvailableApps(deviceId) {
|
|
353
105
|
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
354
106
|
return await transport.listAvailableApps(deviceId);
|
|
@@ -363,19 +115,44 @@ class Connector {
|
|
|
363
115
|
const { setTimeout } = await import("node:timers/promises");
|
|
364
116
|
while(!signal.aborted){
|
|
365
117
|
try {
|
|
366
|
-
const clients = await this.#listClientsForDevice(transport, deviceId);
|
|
118
|
+
const clients = hasClientList(transport) ? (await transport.listClients()).filter(({ id })=>ClientId.deserialize(id)?.deviceId === deviceId) : await this.#listClientsForDevice(transport, deviceId);
|
|
367
119
|
if (clients.some(({ info })=>info.AppProcessName === packageName || info.bundleId === packageName || info.bundleName === packageName)) break;
|
|
368
120
|
} catch (err) {
|
|
369
|
-
|
|
121
|
+
debug(`openApp ${deviceId} ${packageName} client not found %o`, err);
|
|
370
122
|
}
|
|
371
123
|
await setTimeout(1000);
|
|
372
124
|
}
|
|
373
125
|
}
|
|
374
|
-
async sendMessage(clientId, message
|
|
375
|
-
|
|
126
|
+
async sendMessage(clientId, message, pipeline = {
|
|
127
|
+
input: [],
|
|
128
|
+
output: []
|
|
129
|
+
}) {
|
|
130
|
+
return this.#sendMessage(clientId, message, pipeline);
|
|
131
|
+
}
|
|
132
|
+
async sendMessageNoReply(clientId, message) {
|
|
133
|
+
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
134
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
135
|
+
const signal = AbortSignal.timeout(5000);
|
|
136
|
+
const conn = await transport.connect({
|
|
137
|
+
deviceId,
|
|
138
|
+
port,
|
|
139
|
+
signal
|
|
140
|
+
});
|
|
141
|
+
try {
|
|
142
|
+
const inputStream = [
|
|
143
|
+
new CustomizedClientIdTransformStream(port),
|
|
144
|
+
new InspectStream((msg)=>debug(`sendMessageNoReply ${deviceId}:${port} send %o`, JSON.stringify(msg)))
|
|
145
|
+
].reduce((stream, transform)=>stream.pipeThrough(transform), ReadableStream.from([
|
|
146
|
+
message
|
|
147
|
+
]));
|
|
148
|
+
await inputStream.pipeTo(conn.writable, {
|
|
149
|
+
preventClose: true
|
|
150
|
+
});
|
|
151
|
+
} finally{
|
|
152
|
+
await conn[Symbol.asyncDispose]();
|
|
153
|
+
}
|
|
376
154
|
}
|
|
377
155
|
async sendAppMessage(clientId, method, params) {
|
|
378
|
-
const { port } = this.#resolveClientId(clientId);
|
|
379
156
|
const id = randomInt(10000, 50000);
|
|
380
157
|
return await this.#sendMessage(clientId, {
|
|
381
158
|
method,
|
|
@@ -386,7 +163,6 @@ class Connector {
|
|
|
386
163
|
input: [
|
|
387
164
|
new CustomizedRequestTransformStream({
|
|
388
165
|
type: 'App',
|
|
389
|
-
port,
|
|
390
166
|
sessionId: -1,
|
|
391
167
|
messageBuilder: (message)=>({
|
|
392
168
|
id,
|
|
@@ -400,16 +176,22 @@ class Connector {
|
|
|
400
176
|
]
|
|
401
177
|
});
|
|
402
178
|
}
|
|
403
|
-
async sendCDPMessage(clientId, sessionId, method, params) {
|
|
404
|
-
const { port } = this.#resolveClientId(clientId);
|
|
179
|
+
async sendCDPMessage(clientId, sessionId, method, params, isMainThread = false) {
|
|
405
180
|
const id = randomInt(10000, 50000);
|
|
181
|
+
const SUPPORTED_DOMAIN = [
|
|
182
|
+
'Debugger',
|
|
183
|
+
'Runtime',
|
|
184
|
+
'HeapProfiler',
|
|
185
|
+
'Profiler'
|
|
186
|
+
];
|
|
187
|
+
if (isMainThread && !SUPPORTED_DOMAIN.some((domain)=>method.startsWith(domain + '.'))) throw new Error(`Method ${method} is not supported for main thread. Supported domains: ${SUPPORTED_DOMAIN.join(', ')}`);
|
|
406
188
|
return await this.#sendMessage(clientId, {
|
|
407
189
|
method,
|
|
408
190
|
params,
|
|
409
|
-
sessionId
|
|
191
|
+
sessionId: isMainThread ? 'Main' : void 0
|
|
410
192
|
}, {
|
|
411
193
|
input: [
|
|
412
|
-
new CDPRequestTransformStream(
|
|
194
|
+
new CDPRequestTransformStream(sessionId, id)
|
|
413
195
|
],
|
|
414
196
|
output: [
|
|
415
197
|
new CDPResponseTransformStream(id),
|
|
@@ -418,12 +200,42 @@ class Connector {
|
|
|
418
200
|
});
|
|
419
201
|
}
|
|
420
202
|
async sendListSessionMessage(clientId) {
|
|
421
|
-
|
|
203
|
+
return await this.#sendListSessionMessage(clientId);
|
|
204
|
+
}
|
|
205
|
+
async prepareHeadless(clientId) {
|
|
206
|
+
const { data: { data: state } } = await this.#sendMessage(clientId, {
|
|
207
|
+
event: 'Customized',
|
|
208
|
+
data: {
|
|
209
|
+
type: 'HeadlessPrepare',
|
|
210
|
+
data: {}
|
|
211
|
+
}
|
|
212
|
+
}, {
|
|
213
|
+
input: [],
|
|
214
|
+
output: [
|
|
215
|
+
new FilterTransformStream(isHeadlessPrepareResponse)
|
|
216
|
+
]
|
|
217
|
+
});
|
|
218
|
+
return state;
|
|
219
|
+
}
|
|
220
|
+
async waitForHeadlessReady(clientId, options = {}) {
|
|
221
|
+
const timeoutMs = options.timeoutMs ?? 300000;
|
|
222
|
+
const pollIntervalMs = options.pollIntervalMs ?? 1000;
|
|
223
|
+
const { setTimeout: delay } = await import("node:timers/promises");
|
|
224
|
+
const deadline = Date.now() + timeoutMs;
|
|
225
|
+
let lastError;
|
|
226
|
+
for(;;){
|
|
227
|
+
const state = await this.prepareHeadless(clientId);
|
|
228
|
+
if ('ready' === state.status) return;
|
|
229
|
+
if ('error' === state.status) lastError = state.message ?? 'unknown error';
|
|
230
|
+
if (Date.now() >= deadline) throw new Error(void 0 !== lastError ? `Failed to prepare headless runtime: ${lastError}` : `Timed out preparing headless runtime after ${timeoutMs}ms`);
|
|
231
|
+
await delay(pollIntervalMs);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async #sendListSessionMessage(clientId) {
|
|
422
235
|
const { data: { data: sessions } } = await this.#sendMessage(clientId, {
|
|
423
236
|
event: 'Customized',
|
|
424
237
|
data: {
|
|
425
238
|
type: 'ListSession',
|
|
426
|
-
sender: options.port,
|
|
427
239
|
data: {}
|
|
428
240
|
}
|
|
429
241
|
}, {
|
|
@@ -432,15 +244,17 @@ class Connector {
|
|
|
432
244
|
new FilterTransformStream(isListSessionResponse)
|
|
433
245
|
]
|
|
434
246
|
});
|
|
435
|
-
return sessions
|
|
247
|
+
return sessions.map((session)=>({
|
|
248
|
+
...session,
|
|
249
|
+
type: '' === session.type ? 'lynx' : session.type
|
|
250
|
+
}));
|
|
436
251
|
}
|
|
437
252
|
async getGlobalSwitch(clientId, key) {
|
|
438
|
-
const options = this.#resolveClientId(clientId);
|
|
439
253
|
const { data: { data: { message } } } = await this.#sendMessage(clientId, {
|
|
440
254
|
key
|
|
441
255
|
}, {
|
|
442
256
|
input: [
|
|
443
|
-
new GlobalSwitchRequestTransformStream('GetGlobalSwitch'
|
|
257
|
+
new GlobalSwitchRequestTransformStream('GetGlobalSwitch')
|
|
444
258
|
],
|
|
445
259
|
output: [
|
|
446
260
|
new FilterTransformStream(isGetGlobalSwitchResponse)
|
|
@@ -450,45 +264,42 @@ class Connector {
|
|
|
450
264
|
return 'true' === message || true === message;
|
|
451
265
|
}
|
|
452
266
|
async setGlobalSwitch(clientId, key, value) {
|
|
453
|
-
const options = this.#resolveClientId(clientId);
|
|
454
267
|
await this.#sendMessage(clientId, {
|
|
455
268
|
key,
|
|
456
269
|
value
|
|
457
270
|
}, {
|
|
458
271
|
input: [
|
|
459
|
-
new GlobalSwitchRequestTransformStream('SetGlobalSwitch'
|
|
272
|
+
new GlobalSwitchRequestTransformStream('SetGlobalSwitch')
|
|
460
273
|
],
|
|
461
274
|
output: [
|
|
462
275
|
new FilterTransformStream(isSetGlobalSwitchResponse)
|
|
463
276
|
]
|
|
464
277
|
});
|
|
465
278
|
}
|
|
466
|
-
async sendStream(clientId, inputStream, { signal }) {
|
|
279
|
+
async sendStream(clientId, inputStream, { signal, pipeline } = {}) {
|
|
467
280
|
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
468
281
|
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
469
282
|
return await this.#connect(transport, {
|
|
470
283
|
deviceId,
|
|
471
284
|
port,
|
|
472
285
|
signal
|
|
473
|
-
}, inputStream, {
|
|
286
|
+
}, inputStream, pipeline ?? {
|
|
474
287
|
input: [],
|
|
475
288
|
output: []
|
|
476
289
|
});
|
|
477
290
|
}
|
|
478
|
-
async sendCDPStream(clientId, inputStream, { signal } = {}) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
new CDPResponseTransformStream()
|
|
491
|
-
]
|
|
291
|
+
async sendCDPStream(clientId, sessionId, inputStream, { signal } = {}) {
|
|
292
|
+
return await this.sendStream(clientId, inputStream, {
|
|
293
|
+
signal,
|
|
294
|
+
pipeline: {
|
|
295
|
+
input: [
|
|
296
|
+
new CDPRequestTransformStream(sessionId)
|
|
297
|
+
],
|
|
298
|
+
output: [
|
|
299
|
+
new SessionGuardTransformStream(sessionId),
|
|
300
|
+
new CDPResponseTransformStream()
|
|
301
|
+
]
|
|
302
|
+
}
|
|
492
303
|
});
|
|
493
304
|
}
|
|
494
305
|
#resolveClientId(clientId) {
|
|
@@ -497,37 +308,46 @@ class Connector {
|
|
|
497
308
|
return parsed;
|
|
498
309
|
}
|
|
499
310
|
async #findTransportWithDeviceId(deviceId) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
311
|
+
const daemonTransport = await this.#findTransportWithDeviceIdInPool(this.#daemonTransports, deviceId);
|
|
312
|
+
if (daemonTransport) return daemonTransport;
|
|
313
|
+
const transport = await this.#findTransportWithDeviceIdInPool(this.#transports.filter((t)=>!(t instanceof DaemonTransport)), deviceId);
|
|
314
|
+
if (transport) return transport;
|
|
315
|
+
throw new Error(`Device with id: ${deviceId} not found`);
|
|
316
|
+
}
|
|
317
|
+
async #findTransportWithDeviceIdInPool(transports, deviceId) {
|
|
318
|
+
return await Promise.any(transports.map(async (transport)=>{
|
|
319
|
+
const devices = await transport.listDevices();
|
|
320
|
+
if (devices.some(({ id })=>id === deviceId)) return transport;
|
|
503
321
|
throw new Error('Not found in this transport');
|
|
504
|
-
})).catch(()=>
|
|
505
|
-
throw new Error(`Device with id: ${deviceId} not found`);
|
|
506
|
-
});
|
|
322
|
+
})).catch(()=>null);
|
|
507
323
|
}
|
|
508
324
|
async #connect(transport, options, inputStream, pipeline) {
|
|
509
325
|
const { deviceId, port } = options;
|
|
510
|
-
await takeoverDebugRouterLock();
|
|
511
326
|
const conn = await transport.connect(options);
|
|
512
|
-
|
|
327
|
+
const inputAbortController = new AbortController();
|
|
328
|
+
const inputClosed = [
|
|
513
329
|
...pipeline.input,
|
|
514
|
-
new
|
|
515
|
-
new
|
|
330
|
+
new CustomizedClientIdTransformStream(port),
|
|
331
|
+
new InspectStream((msg)=>debug(`connect ${deviceId}:${port} input stream send %o`, JSON.stringify(msg)))
|
|
516
332
|
].reduce((stream, transform)=>stream.pipeThrough(transform), inputStream).pipeTo(conn.writable, {
|
|
517
|
-
preventClose: true
|
|
333
|
+
preventClose: true,
|
|
334
|
+
signal: inputAbortController.signal
|
|
518
335
|
}).catch((err)=>{
|
|
519
|
-
|
|
520
|
-
conn[Symbol.asyncDispose]();
|
|
336
|
+
if (err?.name !== 'AbortError') debug(`connect ${deviceId}:${port} input stream err %O`, err);
|
|
521
337
|
});
|
|
522
338
|
const outputStream = [
|
|
523
|
-
new
|
|
524
|
-
new InspectStream((msg)=>src_debug(`connect ${deviceId}:${port} output stream receive %O`, msg)),
|
|
339
|
+
new InspectStream((msg)=>debug(`connect ${deviceId}:${port} output stream receive %O`, msg)),
|
|
525
340
|
...pipeline.output
|
|
526
|
-
].reduce((stream, transform)=>stream.pipeThrough(transform
|
|
341
|
+
].reduce((stream, transform)=>stream.pipeThrough(transform, {
|
|
342
|
+
preventCancel: true
|
|
343
|
+
}), conn.readable);
|
|
527
344
|
return Object.assign(outputStream, {
|
|
345
|
+
inputClosed,
|
|
528
346
|
async [Symbol.asyncDispose] () {
|
|
529
|
-
|
|
530
|
-
|
|
347
|
+
debug(`connect ${deviceId}:${port} close connection`);
|
|
348
|
+
inputAbortController.abort();
|
|
349
|
+
await inputClosed.catch(()=>{});
|
|
350
|
+
return conn[Symbol.asyncDispose]();
|
|
531
351
|
}
|
|
532
352
|
});
|
|
533
353
|
}
|
|
@@ -537,7 +357,7 @@ class Connector {
|
|
|
537
357
|
}) {
|
|
538
358
|
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
539
359
|
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
540
|
-
const signal = AbortSignal.timeout(
|
|
360
|
+
const signal = AbortSignal.timeout(10000);
|
|
541
361
|
return this.#sendMessageWithTransport(transport, {
|
|
542
362
|
deviceId,
|
|
543
363
|
port,
|
|
@@ -554,9 +374,13 @@ class Connector {
|
|
|
554
374
|
const outputStream = _ts_add_disposable_resource(env, await this.#connect(transport, options, ReadableStream.from([
|
|
555
375
|
input
|
|
556
376
|
]), pipeline), true);
|
|
557
|
-
for await (const response of outputStream)
|
|
558
|
-
|
|
559
|
-
|
|
377
|
+
for await (const response of outputStream){
|
|
378
|
+
await outputStream.inputClosed;
|
|
379
|
+
return response;
|
|
380
|
+
}
|
|
381
|
+
await outputStream.inputClosed;
|
|
382
|
+
const clientId = ClientId.serialize(options.deviceId, options.port);
|
|
383
|
+
throw new Error(`No response found for clientId: ${clientId}`);
|
|
560
384
|
} catch (e) {
|
|
561
385
|
env.error = e;
|
|
562
386
|
env.hasError = true;
|
|
@@ -570,11 +394,12 @@ class Connector {
|
|
|
570
394
|
const PORTS = Array.from({
|
|
571
395
|
length: 10
|
|
572
396
|
}, (_, i)=>MIN_PORT + i);
|
|
397
|
+
const signal = AbortSignal.timeout(5000);
|
|
573
398
|
const results = await Promise.allSettled(PORTS.map(async (port)=>{
|
|
574
399
|
const { data: { info } } = await this.#sendMessageWithTransport(transport, {
|
|
575
400
|
deviceId,
|
|
576
401
|
port,
|
|
577
|
-
signal
|
|
402
|
+
signal
|
|
578
403
|
}, {
|
|
579
404
|
event: 'Initialize',
|
|
580
405
|
data: port
|
|
@@ -584,32 +409,46 @@ class Connector {
|
|
|
584
409
|
new FilterTransformStream(isInitializeResponse)
|
|
585
410
|
]
|
|
586
411
|
});
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
deviceId,
|
|
590
|
-
port,
|
|
591
|
-
signal: AbortSignal.timeout(3000)
|
|
592
|
-
}, {
|
|
593
|
-
key: 'enable_devtool',
|
|
594
|
-
value: true
|
|
595
|
-
}, {
|
|
596
|
-
input: [
|
|
597
|
-
new GlobalSwitchRequestTransformStream('SetGlobalSwitch', port)
|
|
598
|
-
],
|
|
599
|
-
output: [
|
|
600
|
-
new FilterTransformStream(isSetGlobalSwitchResponse)
|
|
601
|
-
]
|
|
602
|
-
});
|
|
603
|
-
} catch (err) {
|
|
604
|
-
src_debug(`listClientsForDevice ${deviceId}:${port} enable_devtool failed %O`, err);
|
|
605
|
-
}
|
|
412
|
+
const clientId = ClientId.serialize(deviceId, port);
|
|
413
|
+
await this.#setupClient(transport, clientId);
|
|
606
414
|
return {
|
|
607
|
-
id:
|
|
415
|
+
id: clientId,
|
|
608
416
|
info,
|
|
609
417
|
port
|
|
610
418
|
};
|
|
611
419
|
}));
|
|
612
420
|
return results.filter((result)=>'fulfilled' === result.status).map((result)=>result.value);
|
|
613
421
|
}
|
|
422
|
+
async #setupClient(transport, clientId) {
|
|
423
|
+
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
424
|
+
for (const input of [
|
|
425
|
+
{
|
|
426
|
+
key: 'enable_devtool',
|
|
427
|
+
value: true
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
key: 'enable_quickjs_debug',
|
|
431
|
+
value: true
|
|
432
|
+
}
|
|
433
|
+
])try {
|
|
434
|
+
await this.#sendMessageWithTransport(transport, {
|
|
435
|
+
deviceId,
|
|
436
|
+
port,
|
|
437
|
+
signal: AbortSignal.timeout(3000)
|
|
438
|
+
}, input, {
|
|
439
|
+
input: [
|
|
440
|
+
new GlobalSwitchRequestTransformStream('SetGlobalSwitch')
|
|
441
|
+
],
|
|
442
|
+
output: [
|
|
443
|
+
new FilterTransformStream(isSetGlobalSwitchResponse)
|
|
444
|
+
]
|
|
445
|
+
});
|
|
446
|
+
} catch (err) {
|
|
447
|
+
debug(`setupClient ${deviceId}:${port} ${input.key} failed %O`, err);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
614
450
|
}
|
|
615
|
-
export {
|
|
451
|
+
export { CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream } from "./712.js";
|
|
452
|
+
export { ClientId } from "./43.js";
|
|
453
|
+
export { isCustomizedResponseWithType, isGetGlobalSwitchResponse, isHeadlessPrepareResponse, isInitializeResponse, isListSessionResponse, isSetGlobalSwitchResponse } from "./677.js";
|
|
454
|
+
export { Connector };
|