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