@lynx-js/devtool-connector 0.1.1 → 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.
Files changed (47) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +1 -1
  3. package/README.md +152 -0
  4. package/dist/187.js +272 -0
  5. package/dist/390.js +102 -0
  6. package/dist/43.js +20 -0
  7. package/dist/556.js +253 -0
  8. package/dist/63.js +93 -0
  9. package/dist/677.js +19 -0
  10. package/dist/712.js +180 -0
  11. package/dist/872.js +771 -0
  12. package/dist/962.js +12151 -0
  13. package/dist/client-id.d.ts +7 -0
  14. package/dist/daemon/device-connection.d.ts +36 -0
  15. package/dist/daemon/entry.d.ts +1 -0
  16. package/dist/daemon/entry.js +46 -0
  17. package/dist/daemon/index.d.ts +1 -0
  18. package/dist/daemon/index.js +1 -0
  19. package/dist/daemon/manager.d.ts +14 -0
  20. package/dist/daemon/protocol.d.ts +73 -0
  21. package/dist/daemon/server.d.ts +14 -0
  22. package/dist/daemon/static-server.d.ts +5 -0
  23. package/dist/daemon/tarball-cache.d.ts +16 -0
  24. package/dist/daemon/version.d.ts +1 -0
  25. package/dist/index.d.ts +47 -19
  26. package/dist/index.js +188 -349
  27. package/dist/rslib-runtime.js +0 -20
  28. package/dist/streams/cdp.d.ts +2 -4
  29. package/dist/streams/customized.d.ts +5 -4
  30. package/dist/streams/index.d.ts +3 -0
  31. package/dist/streams/index.js +1 -0
  32. package/dist/streams/utils.d.ts +11 -0
  33. package/dist/transport/android.d.ts +4 -3
  34. package/dist/transport/base.d.ts +9 -0
  35. package/dist/transport/daemon.d.ts +20 -0
  36. package/dist/transport/desktop.d.ts +4 -3
  37. package/dist/transport/index.d.ts +5 -0
  38. package/dist/transport/index.js +3 -11253
  39. package/dist/transport/ios.d.ts +3 -4
  40. package/dist/transport/transport.d.ts +7 -5
  41. package/dist/transport/usbmux.d.ts +49 -0
  42. package/dist/transport/ws-stream.d.ts +28 -0
  43. package/dist/types.d.ts +73 -19
  44. package/package.json +33 -15
  45. package/public/inspector-wrapper.html +539 -0
  46. package/dist/844.js +0 -632
  47. /package/dist/{streams → transport}/peertalk.d.ts +0 -0
@@ -0,0 +1,539 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Lynx Inspector Wrapper</title>
6
+ <style>
7
+ * {
8
+ margin: 0;
9
+ padding: 0;
10
+ box-sizing: border-box;
11
+ }
12
+ html, body {
13
+ width: 100%;
14
+ height: 100%;
15
+ overflow: hidden;
16
+ font-family: -apple-system, BlinkMacSystemFont, sans-serif;
17
+ }
18
+ #toolbar {
19
+ height: 36px;
20
+ background: #f3f3f3;
21
+ border-bottom: 1px solid #ccc;
22
+ display: flex;
23
+ align-items: center;
24
+ padding: 0 12px;
25
+ gap: 8px;
26
+ font-size: 12px;
27
+ }
28
+ #toolbar select, #toolbar button {
29
+ font-size: 12px;
30
+ padding: 2px 8px;
31
+ border-radius: 4px;
32
+ border: 1px solid #bbb;
33
+ }
34
+ #toolbar button {
35
+ cursor: pointer;
36
+ background: #fff;
37
+ }
38
+ #toolbar button:hover {
39
+ background: #e8e8e8;
40
+ }
41
+ #status {
42
+ margin-left: auto;
43
+ color: #666;
44
+ }
45
+ #inspector-frame {
46
+ width: 100%;
47
+ height: calc(100% - 36px);
48
+ border: none;
49
+ }
50
+ </style>
51
+ </head>
52
+ <body>
53
+ <div id="toolbar">
54
+ <button id="btn-connect">Connect</button>
55
+ <select id="client-select">
56
+ <option value="">-- select client --</option>
57
+ </select>
58
+ <select id="session-select">
59
+ <option value="">-- select session --</option>
60
+ </select>
61
+ <button id="btn-load">Load Inspector</button>
62
+ <span id="status">Disconnected</span>
63
+ </div>
64
+ <iframe id="inspector-frame"></iframe>
65
+
66
+ <script>
67
+ const DAEMON_WS_URL = (() => {
68
+ const url = new URL("/devtool/connector", window.location.href);
69
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
70
+ return url.toString();
71
+ })();
72
+ const INSPECTOR_BASE_URL =
73
+ "/devtool-frontend/inspector.html";
74
+
75
+ let ws = null;
76
+ let registered = false;
77
+ let controlIdCounter = 0;
78
+ const pendingControlCallbacks = new Map();
79
+ let currentClientId = null;
80
+ let currentPort = null;
81
+ let currentSessionId = null;
82
+ let currentDeviceId = null;
83
+ let currentClientInfo = null;
84
+ let subscribedPort = null;
85
+ let inspectorReady = false;
86
+
87
+ const statusEl = document.getElementById("status");
88
+ const clientSelect = document.getElementById("client-select");
89
+ const sessionSelect = document.getElementById("session-select");
90
+ const inspectorFrame = document.getElementById("inspector-frame");
91
+
92
+ function setStatus(text) {
93
+ statusEl.textContent = text;
94
+ }
95
+ function log(...args) {
96
+ console.log("[wrapper]", ...args);
97
+ }
98
+
99
+ // --- Daemon WebSocket ---
100
+
101
+ document.getElementById("btn-connect").addEventListener(
102
+ "click",
103
+ connectDaemon,
104
+ );
105
+
106
+ function connectDaemon() {
107
+ if (ws && ws.readyState === WebSocket.OPEN) {
108
+ ws.close();
109
+ }
110
+ setStatus("Connecting...");
111
+ ws = new WebSocket(DAEMON_WS_URL);
112
+ ws.onopen = () => {
113
+ setStatus("Connected (handshaking...)");
114
+ };
115
+ ws.onclose = () => {
116
+ setStatus("Disconnected");
117
+ registered = false;
118
+ };
119
+ ws.onerror = (e) => {
120
+ setStatus("Error");
121
+ console.error("WS error", e);
122
+ };
123
+ ws.onmessage = (ev) => {
124
+ const msg = JSON.parse(ev.data);
125
+ handleDaemonMessage(msg);
126
+ };
127
+ }
128
+
129
+ function sendToDaemon(msg) {
130
+ if (ws && ws.readyState === WebSocket.OPEN) {
131
+ log("→ daemon:", msg.event, msg.data?.type || "");
132
+ ws.send(JSON.stringify(msg));
133
+ }
134
+ }
135
+
136
+ function sendControl(method, params) {
137
+ return new Promise((resolve, reject) => {
138
+ const id = ++controlIdCounter;
139
+ pendingControlCallbacks.set(id, { resolve, reject });
140
+ sendToDaemon({ event: "Control", data: { id, method, params } });
141
+ setTimeout(() => {
142
+ if (pendingControlCallbacks.has(id)) {
143
+ pendingControlCallbacks.delete(id);
144
+ reject(new Error("Control timeout"));
145
+ }
146
+ }, 10000);
147
+ });
148
+ }
149
+
150
+ function handleDaemonMessage(msg) {
151
+ switch (msg.event) {
152
+ case "Initialize":
153
+ sendToDaemon({
154
+ event: "Register",
155
+ data: { id: msg.data, type: "Driver" },
156
+ });
157
+ registered = true;
158
+ setStatus("Registered");
159
+ sendToDaemon({ event: "ListClients" });
160
+ break;
161
+
162
+ case "ClientList":
163
+ populateClients(msg.data || []);
164
+ break;
165
+
166
+ case "ControlResponse":
167
+ handleControlResponse(msg.data);
168
+ break;
169
+
170
+ case "Customized":
171
+ handleCustomizedFromDevice(msg);
172
+ break;
173
+
174
+ case "Pong":
175
+ break;
176
+
177
+ default:
178
+ log("daemon msg:", msg);
179
+ }
180
+ }
181
+
182
+ function handleControlResponse(data) {
183
+ const cb = pendingControlCallbacks.get(data.id);
184
+ if (cb) {
185
+ pendingControlCallbacks.delete(data.id);
186
+ if (data.error) cb.reject(new Error(data.error));
187
+ else cb.resolve(data.result);
188
+ }
189
+ }
190
+
191
+ // --- Client / Session Selection ---
192
+
193
+ function populateClients(clients) {
194
+ clientSelect.innerHTML =
195
+ "<option value=\"\">-- select client --</option>";
196
+ for (const c of clients) {
197
+ const opt = document.createElement("option");
198
+ opt.value = c.id;
199
+ opt.dataset.info = JSON.stringify(c.info || {});
200
+ opt.textContent = `${c.info.App} (${c.info.sdkVersion}) [${c.id}]`;
201
+ clientSelect.appendChild(opt);
202
+ }
203
+ if (clients.length > 0) {
204
+ setStatus(`Registered - ${clients.length} client(s)`);
205
+ }
206
+ autoSelectClient(clients);
207
+ }
208
+
209
+ clientSelect.addEventListener("change", async () => {
210
+ const selected = clientSelect.value;
211
+ if (!selected) return;
212
+ currentClientId = selected;
213
+
214
+ const selectedOpt = clientSelect.selectedOptions[0];
215
+ currentClientInfo = selectedOpt?.dataset.info
216
+ ? JSON.parse(selectedOpt.dataset.info)
217
+ : {};
218
+
219
+ const lastColon = selected.lastIndexOf(":");
220
+ currentDeviceId = decodeURIComponent(
221
+ selected.substring(0, lastColon),
222
+ );
223
+ currentPort = parseInt(selected.substring(lastColon + 1), 10);
224
+
225
+ try {
226
+ await sendControl("subscribe", {
227
+ deviceId: currentDeviceId,
228
+ port: currentPort,
229
+ });
230
+ subscribedPort = currentPort;
231
+ setStatus(`Subscribed to ${currentDeviceId}:${currentPort}`);
232
+ } catch (e) {
233
+ setStatus(`Subscribe failed: ${e.message}`);
234
+ return;
235
+ }
236
+
237
+ sendToDaemon({
238
+ event: "Customized",
239
+ data: {
240
+ type: "ListSession",
241
+ data: { client_id: currentPort, session_id: 0 },
242
+ },
243
+ to: currentPort,
244
+ });
245
+ });
246
+
247
+ function handleCustomizedFromDevice(msg) {
248
+ const { type, data } = msg.data;
249
+
250
+ if (type === "SessionList") {
251
+ populateSessions(Array.isArray(data) ? data : []);
252
+ return;
253
+ }
254
+
255
+ if (type === "CDP") {
256
+ forwardCDPToInspector(data.message);
257
+ return;
258
+ }
259
+
260
+ forwardCustomizedToInspector(type, data);
261
+ }
262
+
263
+ function populateSessions(sessions) {
264
+ sessionSelect.innerHTML =
265
+ "<option value=\"\">-- select session --</option>";
266
+ for (const s of sessions) {
267
+ const opt = document.createElement("option");
268
+ opt.value = s.session_id;
269
+ opt.textContent = `[${s.session_id}] ${s.type || "unknown"} - ${
270
+ s.url || "(no url)"
271
+ }`;
272
+ sessionSelect.appendChild(opt);
273
+ }
274
+ if (sessions.length > 0) {
275
+ setStatus(`${sessions.length} session(s) found`);
276
+ }
277
+ autoSelectSession(sessions);
278
+ }
279
+
280
+ // --- Inspector iframe loading ---
281
+
282
+ document.getElementById("btn-load").addEventListener(
283
+ "click",
284
+ loadInspector,
285
+ );
286
+
287
+ function loadInspector() {
288
+ const sessionId = sessionSelect.value;
289
+ if (!sessionId || currentPort === null || Number.isNaN(currentPort)) {
290
+ alert("Select a client and session first");
291
+ return;
292
+ }
293
+ currentSessionId = parseInt(sessionId, 10);
294
+ inspectorReady = false;
295
+
296
+ const params = new URLSearchParams({
297
+ sessionId: String(currentSessionId),
298
+ // clientId: String(currentPort),
299
+ hdtVersion: "1.0.0",
300
+ // showPanels: 'elements,console,sources,network',
301
+ });
302
+
303
+ const url = `${INSPECTOR_BASE_URL}?${params.toString()}`;
304
+ inspectorFrame.src = url;
305
+ setStatus(`Loading inspector for session ${currentSessionId}...`);
306
+ }
307
+
308
+ // --- postMessage bridge: Inspector <-> Daemon ---
309
+
310
+ window.addEventListener("message", (ev) => {
311
+ const msg = ev.data;
312
+ if (!msg || !msg.type) return;
313
+
314
+ switch (msg.type) {
315
+ case "iframe_init":
316
+ log("inspector: iframe_init");
317
+ sendInjectData();
318
+ break;
319
+
320
+ case "iframe_loaded":
321
+ log("inspector: iframe_loaded");
322
+ inspectorReady = true;
323
+ sendLynxOpen();
324
+ break;
325
+
326
+ case "send_message":
327
+ handleInspectorOutbound(msg.content);
328
+ break;
329
+
330
+ case "update_loading_progress":
331
+ case "devtool_stats":
332
+ case "slardar":
333
+ break;
334
+
335
+ default:
336
+ log("iframe msg:", msg.type);
337
+ break;
338
+ }
339
+ });
340
+
341
+ const DEFAULT_INFO = {
342
+ App: "LynxPlayground",
343
+ AppVersion: "1.0.0",
344
+ sdkVersion: "0.0.1",
345
+ hdtVersion: "3.0.0",
346
+ osVersion: "16",
347
+ deviceModel: "OnePlus IN2020",
348
+ network: "WIFI",
349
+ };
350
+
351
+ function getClientInfo() {
352
+ const info = currentClientInfo || {};
353
+ return {
354
+ ...DEFAULT_INFO,
355
+ ...info,
356
+ sdkVersion: info.sdkVersion || DEFAULT_INFO.sdkVersion,
357
+ hdtVersion: info.hdtVersion || info.ldtVersion
358
+ || DEFAULT_INFO.hdtVersion,
359
+ };
360
+ }
361
+
362
+ function sendInjectData() {
363
+ if (!inspectorFrame.contentWindow) return;
364
+ inspectorFrame.contentWindow.postMessage({
365
+ type: "inject_data",
366
+ content: {
367
+ info: getClientInfo(),
368
+ plugins: [
369
+ {
370
+ url:
371
+ "https://unpkg.com/@lynx-js/preact-devtools@5.0.1-20260525112551-dfe2d03/dist/index.js",
372
+ _id: "preact-devtools",
373
+ name: "PreactDevtools",
374
+ location: "panel",
375
+ isValid: true,
376
+ type: "lynx",
377
+ },
378
+ ],
379
+ },
380
+ }, "*");
381
+ log("sent inject_data");
382
+ }
383
+
384
+ function sendLynxOpen() {
385
+ if (!inspectorFrame.contentWindow || currentSessionId === null) {
386
+ return;
387
+ }
388
+ inspectorFrame.contentWindow.postMessage({
389
+ type: "lynx_open",
390
+ content: {
391
+ wsUrl: DAEMON_WS_URL,
392
+ roomId: "",
393
+ sessionId: currentSessionId,
394
+ info: getClientInfo(),
395
+ sessionUrl: "",
396
+ },
397
+ }, "*");
398
+ setStatus(`Inspector connected (session ${currentSessionId})`);
399
+ log("sent lynx_open");
400
+ }
401
+
402
+ function handleInspectorOutbound(content) {
403
+ if (!content || !ws || ws.readyState !== WebSocket.OPEN) return;
404
+
405
+ if (content.type === "CDP") {
406
+ const cdpMessage = typeof content.message === "string"
407
+ ? JSON.parse(content.message)
408
+ : content.message;
409
+ log("→ CDP:", cdpMessage.method || `id:${cdpMessage.id}`);
410
+ sendToDaemon({
411
+ event: "Customized",
412
+ data: {
413
+ type: "CDP",
414
+ data: {
415
+ client_id: currentPort,
416
+ session_id: currentSessionId,
417
+ message: cdpMessage,
418
+ },
419
+ },
420
+ to: currentPort,
421
+ });
422
+ } else {
423
+ const message = typeof content.message === "string"
424
+ ? JSON.parse(content.message)
425
+ : (content.message || {});
426
+ sendToDaemon({
427
+ event: "Customized",
428
+ data: {
429
+ type: content.type,
430
+ data: {
431
+ client_id: currentPort,
432
+ session_id: currentSessionId,
433
+ message: message,
434
+ },
435
+ },
436
+ to: currentPort,
437
+ });
438
+ }
439
+ }
440
+
441
+ function forwardCDPToInspector(message) {
442
+ if (!inspectorFrame.contentWindow || !inspectorReady) return;
443
+ const msgStr = typeof message === "string"
444
+ ? message
445
+ : JSON.stringify(message);
446
+ log("← CDP to inspector:", msgStr.substring(0, 100));
447
+ inspectorFrame.contentWindow.postMessage({
448
+ type: "lynx_message",
449
+ content: {
450
+ type: "CDP",
451
+ message: msgStr,
452
+ },
453
+ }, "*");
454
+ }
455
+
456
+ function forwardCustomizedToInspector(type, data) {
457
+ if (!inspectorFrame.contentWindow || !inspectorReady) return;
458
+ const message = data.message != null ? data.message : data;
459
+ const msgStr = typeof message === "string"
460
+ ? message
461
+ : JSON.stringify(message);
462
+ log("← customized to inspector:", type, msgStr.substring(0, 80));
463
+ inspectorFrame.contentWindow.postMessage({
464
+ type: "lynx_message",
465
+ content: {
466
+ type: type,
467
+ message: msgStr,
468
+ },
469
+ }, "*");
470
+ }
471
+
472
+ // --- Auto-select from URL query params ---
473
+
474
+ const urlParams = new URLSearchParams(window.location.search);
475
+ const autoClientId = urlParams.get("clientId");
476
+ const autoSessionId = urlParams.get("sessionId");
477
+ let autoStarted = false;
478
+
479
+ async function autoSelectClient(clients) {
480
+ if (!autoClientId || autoStarted) return;
481
+ const match = clients.find((c) => c.id === autoClientId);
482
+ if (!match) return;
483
+ autoStarted = true;
484
+
485
+ clientSelect.value = autoClientId;
486
+ currentClientId = autoClientId;
487
+ currentClientInfo = match.info || {};
488
+ const lastColon = autoClientId.lastIndexOf(":");
489
+ currentDeviceId = decodeURIComponent(
490
+ autoClientId.substring(0, lastColon),
491
+ );
492
+ currentPort = parseInt(autoClientId.substring(lastColon + 1), 10);
493
+
494
+ try {
495
+ await sendControl("subscribe", {
496
+ deviceId: currentDeviceId,
497
+ port: currentPort,
498
+ });
499
+ subscribedPort = currentPort;
500
+ setStatus(`Subscribed to ${currentDeviceId}:${currentPort}`);
501
+ } catch (e) {
502
+ setStatus(`Subscribe failed: ${e.message}`);
503
+ return;
504
+ }
505
+
506
+ sendToDaemon({
507
+ event: "Customized",
508
+ data: {
509
+ type: "ListSession",
510
+ data: { client_id: currentPort, session_id: 0 },
511
+ },
512
+ to: currentPort,
513
+ });
514
+ }
515
+
516
+ function autoSelectSession(sessions) {
517
+ if (
518
+ !autoSessionId || currentPort === null || Number.isNaN(currentPort)
519
+ ) return;
520
+ const sid = parseInt(autoSessionId, 10);
521
+ const match = sessions.find((s) => s.session_id === sid);
522
+ if (!match) return;
523
+
524
+ sessionSelect.value = String(sid);
525
+ currentSessionId = sid;
526
+ inspectorReady = false;
527
+
528
+ const params = new URLSearchParams({
529
+ sessionId: String(currentSessionId),
530
+ hdtVersion: "1.0.0",
531
+ });
532
+ inspectorFrame.src = `${INSPECTOR_BASE_URL}?${params.toString()}`;
533
+ setStatus(`Loading inspector for session ${currentSessionId}...`);
534
+ }
535
+
536
+ connectDaemon();
537
+ </script>
538
+ </body>
539
+ </html>