@lynx-js/devtool-connector 0.18.7 → 0.19.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/f1.js CHANGED
@@ -11,23 +11,44 @@ __webpack_require__.add({
11
11
  module.exports = require("utf-8-validate");
12
12
  }
13
13
  });
14
+ function requireNumericPort(port) {
15
+ if ("number" == typeof port) return port;
16
+ if (/^\d+$/.test(port)) return Number(port);
17
+ throw new TypeError(`Transport requires a numeric port, received target: ${port}`);
18
+ }
19
+ function withTransports(clients, transports) {
20
+ if (void 0 !== transports) Object.defineProperty(clients, "transports", {
21
+ value: transports,
22
+ configurable: true
23
+ });
24
+ return clients;
25
+ }
14
26
  class ClientId {
15
- static serialize(deviceId, target) {
27
+ transportId;
28
+ deviceId;
29
+ port;
30
+ constructor(transportId, deviceId, port){
31
+ this.transportId = transportId;
32
+ this.deviceId = deviceId;
33
+ this.port = port;
34
+ }
35
+ static serialize(transportId, deviceId, target) {
36
+ const encodedTransportId = encodeURIComponent(transportId);
37
+ const encodedDeviceId = encodeURIComponent(deviceId);
16
38
  const encodedTarget = "string" == typeof target ? encodeURIComponent(target) : target;
17
- return `${encodeURIComponent(deviceId)}:${encodedTarget}`;
39
+ return `${encodedTransportId}:${encodedDeviceId}:${encodedTarget}`;
18
40
  }
19
41
  static deserialize(clientId) {
20
42
  try {
21
- const lastColonIndex = clientId.lastIndexOf(":");
22
- if (-1 === lastColonIndex) return null;
23
- const target = decodeURIComponent(clientId.substring(lastColonIndex + 1));
24
- if (!target) return null;
43
+ const [rawTransportId, rawDeviceId, rawTarget, ...rest] = clientId.split(":");
44
+ if (void 0 === rawTransportId || void 0 === rawDeviceId || void 0 === rawTarget || rest.length > 0) return null;
45
+ const transportId = decodeURIComponent(rawTransportId);
46
+ const deviceId = decodeURIComponent(rawDeviceId);
47
+ const target = decodeURIComponent(rawTarget);
48
+ if (!transportId || !deviceId || !target) return null;
25
49
  const port = /^-?\d+$/.test(target) ? Number(target) : target;
26
50
  if ("number" == typeof port && !Number.isSafeInteger(port)) return null;
27
- return {
28
- deviceId: decodeURIComponent(clientId.substring(0, lastColonIndex)),
29
- port
30
- };
51
+ return new ClientId(transportId, deviceId, port);
31
52
  } catch {
32
53
  return null;
33
54
  }
@@ -170,6 +191,7 @@ function urlToDeviceId(url) {
170
191
  return `HDT-${url}`;
171
192
  }
172
193
  class HDTTransport {
194
+ id;
173
195
  #url;
174
196
  static DEFAULT_PORT = 19783;
175
197
  static createTransports(port = HDTTransport.DEFAULT_PORT, backoff = 10) {
@@ -182,6 +204,7 @@ class HDTTransport {
182
204
  constructor(urlOrPort){
183
205
  const url = "number" == typeof urlOrPort ? `ws://127.0.0.1:${urlOrPort}/mdevices/page/android` : urlOrPort;
184
206
  this.#url = url;
207
+ this.id = urlToDeviceId(url);
185
208
  }
186
209
  close() {
187
210
  return Promise.resolve();
@@ -214,7 +237,7 @@ class HDTTransport {
214
237
  const clients = msg.data;
215
238
  debug("Received client list from HDT transport: %o", clients);
216
239
  return clients.filter(({ info })=>"ws://127.0.0.1:3005/ws" !== this.#url || "unknown" !== info.osType).map(({ info, id })=>({
217
- id: ClientId.serialize(urlToDeviceId(this.#url), info.debugRouterId || Number(id)),
240
+ id: ClientId.serialize(this.id, urlToDeviceId(this.#url), info.debugRouterId || Number(id)),
218
241
  info
219
242
  }));
220
243
  }
@@ -459,6 +482,7 @@ async function withAbortSignal(promise, signal) {
459
482
  }
460
483
  const daemon_debug = ansi_createDebug("devtool-mcp-server:daemon:transport");
461
484
  class DaemonTransport {
485
+ id = "daemon";
462
486
  #endpoint;
463
487
  constructor(endpoint = 21783){
464
488
  if ("string" == typeof endpoint) {
@@ -490,15 +514,24 @@ class DaemonTransport {
490
514
  }, options?.signal);
491
515
  }
492
516
  async listClients() {
493
- const entries = await this.#controlRequest("listClients");
494
- daemon_debug("received ClientList with %d entries", entries.length);
495
- return entries.map(({ id, info })=>({
496
- id,
497
- info
498
- }));
517
+ const conn = await this.#createWebSocketConnection();
518
+ try {
519
+ const response = await this.#controlRequestOnConn(conn, "listClients");
520
+ const entries = response.result ?? [];
521
+ daemon_debug("received ClientList with %d entries", entries.length);
522
+ return withTransports(entries.map(({ id, info })=>({
523
+ id,
524
+ info
525
+ })), [
526
+ this.id,
527
+ ...response.transports ?? []
528
+ ]);
529
+ } finally{
530
+ await conn[Symbol.asyncDispose]();
531
+ }
499
532
  }
500
533
  async connect(options) {
501
- const { deviceId, signal } = options;
534
+ const { transportId, deviceId, signal } = options;
502
535
  const { port } = options;
503
536
  daemon_debug("connect to %s:%o via daemon", deviceId, port);
504
537
  const conn = await this.#createWebSocketConnection(signal);
@@ -508,6 +541,7 @@ class DaemonTransport {
508
541
  AbortSignal.timeout(10000)
509
542
  ]) : void 0;
510
543
  await this.#controlRequestOnConn(conn, "subscribe", {
544
+ transportId,
511
545
  deviceId,
512
546
  port
513
547
  }, subscribeSignal);
@@ -538,7 +572,7 @@ class DaemonTransport {
538
572
  async #controlRequest(method, params, signal) {
539
573
  const conn = await this.#createWebSocketConnection(signal);
540
574
  try {
541
- return await this.#controlRequestOnConn(conn, method, params, signal);
575
+ return (await this.#controlRequestOnConn(conn, method, params, signal)).result;
542
576
  } finally{
543
577
  await conn[Symbol.asyncDispose]();
544
578
  }
@@ -568,7 +602,7 @@ class DaemonTransport {
568
602
  const resp = msg.data;
569
603
  if (resp.id === id) {
570
604
  if (resp.error) throw new Error(resp.error);
571
- return resp.result;
605
+ return resp;
572
606
  }
573
607
  }
574
608
  }
@@ -723,4 +757,4 @@ class daemon_JSONStringToObjectStream extends TransformStream {
723
757
  });
724
758
  }
725
759
  }
726
- export { ClientId, DaemonTransport, HDTTransport };
760
+ export { ClientId, DaemonTransport, HDTTransport, requireNumericPort, withTransports };