@lynx-js/devtool-connector 0.0.0 → 0.1.0
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/dist/844.js +632 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +600 -0
- package/dist/rslib-runtime.js +38 -0
- package/dist/streams/cdp.d.ts +13 -0
- package/dist/streams/customized.d.ts +32 -0
- package/dist/streams/peertalk.d.ts +8 -0
- package/dist/streams/utils.d.ts +7 -0
- package/dist/takeover.d.ts +1 -0
- package/dist/transport/android.d.ts +12 -0
- package/dist/transport/desktop.d.ts +8 -0
- package/dist/transport/index.d.ts +4 -0
- package/dist/transport/index.js +11223 -0
- package/dist/transport/ios.d.ts +12 -0
- package/dist/transport/transport.d.ts +34 -0
- package/dist/types.d.ts +113 -0
- package/package.json +44 -6
- package/index.js +0 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { __webpack_require__ } from "./rslib-runtime.js";
|
|
2
|
+
import { randomInt } from "node:crypto";
|
|
3
|
+
import { ReadableStream, TransformStream } from "node:stream/web";
|
|
4
|
+
import promises from "node:fs/promises";
|
|
5
|
+
import node_os from "node:os";
|
|
6
|
+
import node_path from "node:path";
|
|
7
|
+
import "./844.js";
|
|
8
|
+
function isInitializeResponse(response) {
|
|
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
|
+
}
|
|
245
|
+
function _ts_add_disposable_resource(env, value, async) {
|
|
246
|
+
if (null != value) {
|
|
247
|
+
if ("object" != typeof value && "function" != typeof value) throw new TypeError("Object expected.");
|
|
248
|
+
var dispose, inner;
|
|
249
|
+
if (async) {
|
|
250
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
251
|
+
dispose = value[Symbol.asyncDispose];
|
|
252
|
+
}
|
|
253
|
+
if (void 0 === dispose) {
|
|
254
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
255
|
+
dispose = value[Symbol.dispose];
|
|
256
|
+
if (async) inner = dispose;
|
|
257
|
+
}
|
|
258
|
+
if ("function" != typeof dispose) throw new TypeError("Object not disposable.");
|
|
259
|
+
if (inner) dispose = function() {
|
|
260
|
+
try {
|
|
261
|
+
inner.call(this);
|
|
262
|
+
} catch (e) {
|
|
263
|
+
return Promise.reject(e);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
env.stack.push({
|
|
267
|
+
value: value,
|
|
268
|
+
dispose: dispose,
|
|
269
|
+
async: async
|
|
270
|
+
});
|
|
271
|
+
} else if (async) env.stack.push({
|
|
272
|
+
async: true
|
|
273
|
+
});
|
|
274
|
+
return value;
|
|
275
|
+
}
|
|
276
|
+
function src_ts_dispose_resources(env) {
|
|
277
|
+
var _SuppressedError = "function" == typeof SuppressedError ? SuppressedError : function(error, suppressed, message) {
|
|
278
|
+
var e = new Error(message);
|
|
279
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
280
|
+
};
|
|
281
|
+
return (src_ts_dispose_resources = function(env) {
|
|
282
|
+
function fail(e) {
|
|
283
|
+
env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
284
|
+
env.hasError = true;
|
|
285
|
+
}
|
|
286
|
+
var r, s = 0;
|
|
287
|
+
function next() {
|
|
288
|
+
while(r = env.stack.pop())try {
|
|
289
|
+
if (!r.async && 1 === s) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
290
|
+
if (r.dispose) {
|
|
291
|
+
var result = r.dispose.call(r.value);
|
|
292
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
293
|
+
fail(e);
|
|
294
|
+
return next();
|
|
295
|
+
});
|
|
296
|
+
} else s |= 1;
|
|
297
|
+
} catch (e) {
|
|
298
|
+
fail(e);
|
|
299
|
+
}
|
|
300
|
+
if (1 === s) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
301
|
+
if (env.hasError) throw env.error;
|
|
302
|
+
}
|
|
303
|
+
return next();
|
|
304
|
+
})(env);
|
|
305
|
+
}
|
|
306
|
+
const src_debug = src_default()('devtool-mcp-server:connector');
|
|
307
|
+
class ClientId {
|
|
308
|
+
static serialize(deviceId, port) {
|
|
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
|
+
}
|
|
325
|
+
}
|
|
326
|
+
class Connector {
|
|
327
|
+
#transports;
|
|
328
|
+
constructor(transports){
|
|
329
|
+
this.#transports = transports;
|
|
330
|
+
}
|
|
331
|
+
async listClients() {
|
|
332
|
+
const transportDevices = await Promise.allSettled(this.#transports.map(async (transport)=>({
|
|
333
|
+
transport,
|
|
334
|
+
devices: await transport.listDevices()
|
|
335
|
+
})));
|
|
336
|
+
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
|
+
return results.filter((r)=>'fulfilled' === r.status).flatMap((r)=>r.value);
|
|
338
|
+
}
|
|
339
|
+
async listDevices() {
|
|
340
|
+
const results = await Promise.allSettled(this.#transports.map((t)=>t.listDevices()));
|
|
341
|
+
return results.filter((result)=>'fulfilled' === result.status).flatMap(({ value })=>value);
|
|
342
|
+
}
|
|
343
|
+
async listAvailableApps(deviceId) {
|
|
344
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
345
|
+
return await transport.listAvailableApps(deviceId);
|
|
346
|
+
}
|
|
347
|
+
async openApp(deviceId, packageName, options) {
|
|
348
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
349
|
+
await transport.openApp(deviceId, packageName, options);
|
|
350
|
+
const signal = AbortSignal.any([
|
|
351
|
+
options?.signal,
|
|
352
|
+
AbortSignal.timeout(60000)
|
|
353
|
+
].filter((i)=>void 0 !== i));
|
|
354
|
+
const { setTimeout } = await import("node:timers/promises");
|
|
355
|
+
while(!signal.aborted){
|
|
356
|
+
try {
|
|
357
|
+
const clients = await this.#listClientsForDevice(transport, deviceId);
|
|
358
|
+
if (clients.some(({ info })=>info.AppProcessName === packageName || info.bundleId === packageName || info.bundleName === packageName)) break;
|
|
359
|
+
} catch (err) {
|
|
360
|
+
src_debug(`openApp ${deviceId} ${packageName} client not found %o`, err);
|
|
361
|
+
}
|
|
362
|
+
await setTimeout(1000);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async sendMessage(clientId, message) {
|
|
366
|
+
return await this.#sendMessage(clientId, message);
|
|
367
|
+
}
|
|
368
|
+
async sendAppMessage(clientId, method, params) {
|
|
369
|
+
const { port } = this.#resolveClientId(clientId);
|
|
370
|
+
const id = randomInt(10000, 50000);
|
|
371
|
+
return await this.#sendMessage(clientId, {
|
|
372
|
+
method,
|
|
373
|
+
params: {
|
|
374
|
+
...params
|
|
375
|
+
}
|
|
376
|
+
}, {
|
|
377
|
+
input: [
|
|
378
|
+
new CustomizedRequestTransformStream({
|
|
379
|
+
type: 'App',
|
|
380
|
+
port,
|
|
381
|
+
sessionId: -1,
|
|
382
|
+
messageBuilder: (message)=>({
|
|
383
|
+
id,
|
|
384
|
+
...message
|
|
385
|
+
})
|
|
386
|
+
})
|
|
387
|
+
],
|
|
388
|
+
output: [
|
|
389
|
+
new CustomizedResponseTransformStream('App', id),
|
|
390
|
+
new AppResponseTransformStream(method)
|
|
391
|
+
]
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
async sendCDPMessage(clientId, sessionId, method, params) {
|
|
395
|
+
const { port } = this.#resolveClientId(clientId);
|
|
396
|
+
const id = randomInt(10000, 50000);
|
|
397
|
+
return await this.#sendMessage(clientId, {
|
|
398
|
+
method,
|
|
399
|
+
params,
|
|
400
|
+
sessionId
|
|
401
|
+
}, {
|
|
402
|
+
input: [
|
|
403
|
+
new CDPRequestTransformStream(port, id)
|
|
404
|
+
],
|
|
405
|
+
output: [
|
|
406
|
+
new CDPResponseTransformStream(id),
|
|
407
|
+
new CDPOutputTransformStream()
|
|
408
|
+
]
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
async sendListSessionMessage(clientId) {
|
|
412
|
+
const options = this.#resolveClientId(clientId);
|
|
413
|
+
const { data: { data: sessions } } = await this.#sendMessage(clientId, {
|
|
414
|
+
event: 'Customized',
|
|
415
|
+
data: {
|
|
416
|
+
type: 'ListSession',
|
|
417
|
+
sender: options.port,
|
|
418
|
+
data: {}
|
|
419
|
+
}
|
|
420
|
+
}, {
|
|
421
|
+
input: [],
|
|
422
|
+
output: [
|
|
423
|
+
new FilterTransformStream(isListSessionResponse)
|
|
424
|
+
]
|
|
425
|
+
});
|
|
426
|
+
return sessions;
|
|
427
|
+
}
|
|
428
|
+
async getGlobalSwitch(clientId, key) {
|
|
429
|
+
const options = this.#resolveClientId(clientId);
|
|
430
|
+
const { data: { data: { message } } } = await this.#sendMessage(clientId, {
|
|
431
|
+
key
|
|
432
|
+
}, {
|
|
433
|
+
input: [
|
|
434
|
+
new GlobalSwitchRequestTransformStream('GetGlobalSwitch', options.port)
|
|
435
|
+
],
|
|
436
|
+
output: []
|
|
437
|
+
});
|
|
438
|
+
if ('object' == typeof message) return message?.global_value === 'true' || message?.global_value === true;
|
|
439
|
+
return 'true' === message || true === message;
|
|
440
|
+
}
|
|
441
|
+
async setGlobalSwitch(clientId, key, value) {
|
|
442
|
+
const options = this.#resolveClientId(clientId);
|
|
443
|
+
await this.#sendMessage(clientId, {
|
|
444
|
+
key,
|
|
445
|
+
value
|
|
446
|
+
}, {
|
|
447
|
+
input: [
|
|
448
|
+
new GlobalSwitchRequestTransformStream('SetGlobalSwitch', options.port)
|
|
449
|
+
],
|
|
450
|
+
output: []
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
async sendStream(clientId, inputStream, { signal }) {
|
|
454
|
+
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
455
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
456
|
+
return await this.#connect(transport, {
|
|
457
|
+
deviceId,
|
|
458
|
+
port,
|
|
459
|
+
signal
|
|
460
|
+
}, inputStream, {
|
|
461
|
+
input: [],
|
|
462
|
+
output: []
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
async sendCDPStream(clientId, inputStream, { signal } = {}) {
|
|
466
|
+
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
467
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
468
|
+
return await this.#connect(transport, {
|
|
469
|
+
deviceId,
|
|
470
|
+
port,
|
|
471
|
+
signal
|
|
472
|
+
}, inputStream, {
|
|
473
|
+
input: [
|
|
474
|
+
new CDPRequestTransformStream(port)
|
|
475
|
+
],
|
|
476
|
+
output: [
|
|
477
|
+
new CDPResponseTransformStream()
|
|
478
|
+
]
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
#resolveClientId(clientId) {
|
|
482
|
+
const parsed = ClientId.deserialize(clientId);
|
|
483
|
+
if (!parsed) throw new Error(`Invalid clientId: ${clientId}`);
|
|
484
|
+
return parsed;
|
|
485
|
+
}
|
|
486
|
+
async #findTransportWithDeviceId(deviceId) {
|
|
487
|
+
return await Promise.any(this.#transports.map(async (t)=>{
|
|
488
|
+
const devices = await t.listDevices();
|
|
489
|
+
if (devices.some(({ id })=>id === deviceId)) return t;
|
|
490
|
+
throw new Error('Not found in this transport');
|
|
491
|
+
})).catch(()=>{
|
|
492
|
+
throw new Error(`Device with id: ${deviceId} not found`);
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
async #connect(transport, options, inputStream, pipeline) {
|
|
496
|
+
const { deviceId, port } = options;
|
|
497
|
+
await takeoverDebugRouterLock();
|
|
498
|
+
const conn = await transport.connect(options);
|
|
499
|
+
[
|
|
500
|
+
...pipeline.input,
|
|
501
|
+
new InspectStream((msg)=>src_debug(`connect ${deviceId}:${port} input stream send %O`, msg)),
|
|
502
|
+
new MessageToPeertalkTransformStream()
|
|
503
|
+
].reduce((stream, transform)=>stream.pipeThrough(transform), inputStream).pipeTo(conn.writable, {
|
|
504
|
+
preventClose: true
|
|
505
|
+
}).catch((err)=>{
|
|
506
|
+
src_debug(`connect ${deviceId}:${port} input stream err %O`, err);
|
|
507
|
+
conn[Symbol.asyncDispose]();
|
|
508
|
+
});
|
|
509
|
+
const outputStream = [
|
|
510
|
+
new PeertalkToMessageTransformStream(),
|
|
511
|
+
new InspectStream((msg)=>src_debug(`connect ${deviceId}:${port} output stream receive %O`, msg)),
|
|
512
|
+
...pipeline.output
|
|
513
|
+
].reduce((stream, transform)=>stream.pipeThrough(transform), conn.readable);
|
|
514
|
+
return Object.assign(outputStream, {
|
|
515
|
+
async [Symbol.asyncDispose] () {
|
|
516
|
+
src_debug(`connect ${deviceId}:${port} close connection`);
|
|
517
|
+
return await conn[Symbol.asyncDispose]();
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
async #sendMessage(clientId, input, pipeline = {
|
|
522
|
+
input: [],
|
|
523
|
+
output: []
|
|
524
|
+
}) {
|
|
525
|
+
const { deviceId, port } = this.#resolveClientId(clientId);
|
|
526
|
+
const transport = await this.#findTransportWithDeviceId(deviceId);
|
|
527
|
+
const signal = AbortSignal.timeout(5000);
|
|
528
|
+
return this.#sendMessageWithTransport(transport, {
|
|
529
|
+
deviceId,
|
|
530
|
+
port,
|
|
531
|
+
signal
|
|
532
|
+
}, input, pipeline);
|
|
533
|
+
}
|
|
534
|
+
async #sendMessageWithTransport(transport, options, input, pipeline) {
|
|
535
|
+
const env = {
|
|
536
|
+
stack: [],
|
|
537
|
+
error: void 0,
|
|
538
|
+
hasError: false
|
|
539
|
+
};
|
|
540
|
+
try {
|
|
541
|
+
const outputStream = _ts_add_disposable_resource(env, await this.#connect(transport, options, ReadableStream.from([
|
|
542
|
+
input
|
|
543
|
+
]), pipeline), true);
|
|
544
|
+
for await (const response of outputStream)return response;
|
|
545
|
+
const { deviceId, port } = options;
|
|
546
|
+
throw new Error(`No response found for deviceId: ${deviceId} port: ${port}`);
|
|
547
|
+
} catch (e) {
|
|
548
|
+
env.error = e;
|
|
549
|
+
env.hasError = true;
|
|
550
|
+
} finally{
|
|
551
|
+
const result = src_ts_dispose_resources(env);
|
|
552
|
+
if (result) await result;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
async #listClientsForDevice(transport, deviceId) {
|
|
556
|
+
const MIN_PORT = 8901;
|
|
557
|
+
const PORTS = Array.from({
|
|
558
|
+
length: 10
|
|
559
|
+
}, (_, i)=>MIN_PORT + i);
|
|
560
|
+
const results = await Promise.allSettled(PORTS.map(async (port)=>{
|
|
561
|
+
const { data: { info } } = await this.#sendMessageWithTransport(transport, {
|
|
562
|
+
deviceId,
|
|
563
|
+
port,
|
|
564
|
+
signal: AbortSignal.timeout(5000)
|
|
565
|
+
}, {
|
|
566
|
+
event: 'Initialize',
|
|
567
|
+
data: port
|
|
568
|
+
}, {
|
|
569
|
+
input: [],
|
|
570
|
+
output: [
|
|
571
|
+
new FilterTransformStream(isInitializeResponse)
|
|
572
|
+
]
|
|
573
|
+
});
|
|
574
|
+
try {
|
|
575
|
+
await this.#sendMessageWithTransport(transport, {
|
|
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
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
id: ClientId.serialize(deviceId, port),
|
|
593
|
+
info,
|
|
594
|
+
port
|
|
595
|
+
};
|
|
596
|
+
}));
|
|
597
|
+
return results.filter((result)=>'fulfilled' === result.status).map((result)=>result.value);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
export { ClientId, Connector, MessageToPeertalkTransformStream, PeertalkToMessageTransformStream };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var __webpack_modules__ = {};
|
|
2
|
+
var __webpack_module_cache__ = {};
|
|
3
|
+
function __webpack_require__(moduleId) {
|
|
4
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
5
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
6
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
7
|
+
exports: {}
|
|
8
|
+
};
|
|
9
|
+
__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
10
|
+
return module.exports;
|
|
11
|
+
}
|
|
12
|
+
__webpack_require__.m = __webpack_modules__;
|
|
13
|
+
(()=>{
|
|
14
|
+
__webpack_require__.add = function(modules) {
|
|
15
|
+
Object.assign(__webpack_require__.m, modules);
|
|
16
|
+
};
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.n = (module)=>{
|
|
20
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
21
|
+
__webpack_require__.d(getter, {
|
|
22
|
+
a: getter
|
|
23
|
+
});
|
|
24
|
+
return getter;
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
27
|
+
(()=>{
|
|
28
|
+
__webpack_require__.d = (exports, definition)=>{
|
|
29
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: definition[key]
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
(()=>{
|
|
36
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
37
|
+
})();
|
|
38
|
+
export { __webpack_require__ };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CDPRequestMessage, CDPResponseMessage } from '../types.ts';
|
|
2
|
+
import { CustomizedRequestTransformStream, CustomizedResponseTransformStream, ResponseParserTransformStream } from './customized.ts';
|
|
3
|
+
export declare class CDPRequestTransformStream extends CustomizedRequestTransformStream<CDPRequestMessage & {
|
|
4
|
+
sessionId: number;
|
|
5
|
+
}> {
|
|
6
|
+
constructor(port: number, fixedId?: number);
|
|
7
|
+
}
|
|
8
|
+
export declare class CDPResponseTransformStream<Output = CDPResponseMessage> extends CustomizedResponseTransformStream<'CDP', Output> {
|
|
9
|
+
constructor(id?: number);
|
|
10
|
+
}
|
|
11
|
+
export declare class CDPOutputTransformStream<Output> extends ResponseParserTransformStream<CDPResponseMessage, Output> {
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { TransformStream } from 'node:stream/web';
|
|
2
|
+
import type { AppResponseMessage, CustomizedResponseMap, CustomizedResponseMessageMap, Response } from '../types.ts';
|
|
3
|
+
export declare class CustomizedRequestTransformStream<T = unknown> extends TransformStream<T, unknown> {
|
|
4
|
+
protected port: number;
|
|
5
|
+
constructor(options: {
|
|
6
|
+
type: string;
|
|
7
|
+
port: number;
|
|
8
|
+
sessionId?: number | ((chunk: T) => number);
|
|
9
|
+
messageBuilder: (chunk: T) => unknown;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export declare class CustomizedResponseTransformStream<T extends keyof CustomizedResponseMap, Output = CustomizedResponseMessageMap[T]> extends TransformStream<Response, Output> {
|
|
13
|
+
constructor(type: T, id?: number);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Common response parser that handles JSON parsing and error checking in the payload.
|
|
17
|
+
*/
|
|
18
|
+
export declare class ResponseParserTransformStream<Input, Output> extends TransformStream<Input, Output> {
|
|
19
|
+
constructor(options: {
|
|
20
|
+
parseResult: (input: Input) => Output;
|
|
21
|
+
checkError: (input: Input) => Error | null;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export declare class AppResponseTransformStream<Output> extends ResponseParserTransformStream<AppResponseMessage, Output> {
|
|
25
|
+
constructor(method: string);
|
|
26
|
+
}
|
|
27
|
+
export declare class GlobalSwitchRequestTransformStream extends CustomizedRequestTransformStream<{
|
|
28
|
+
key: string;
|
|
29
|
+
value?: boolean;
|
|
30
|
+
}> {
|
|
31
|
+
constructor(type: 'SetGlobalSwitch' | 'GetGlobalSwitch', port: number);
|
|
32
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { TransformStream } from 'node:stream/web';
|
|
2
|
+
import type { Response } from '../types.ts';
|
|
3
|
+
export declare class PeertalkToMessageTransformStream extends TransformStream<Uint8Array, Response> {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export declare class MessageToPeertalkTransformStream<TMessage = unknown> extends TransformStream<TMessage, Uint8Array> {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|