@lynx-js/devtool-connector 0.19.0 → 0.19.2
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 +32 -0
- package/dist/2.js +1 -1
- package/dist/a.js +9305 -566
- package/dist/az.js +616 -0
- package/dist/daemon/entry.js +5 -3
- package/dist/daemon/index.js +1 -1
- package/dist/f.js +4 -4
- package/dist/f1.js +201 -135
- package/dist/h.js +143 -9426
- package/dist/h6.js +578 -0
- package/dist/index.js +2 -2
- package/dist/main.js +1 -1
- package/dist/streams/index.js +1 -1
- package/dist/t.js +2472 -1274
- package/dist/transport/hdt.d.ts +15 -0
- package/dist/transport/index.d.ts +1 -0
- package/dist/transport/index.js +2 -1
- package/dist/transport/peertalk.d.ts +0 -3
- package/dist/transport/simulator.d.ts +62 -0
- package/dist/tw.js +1397 -0
- package/dist/ws-stream.js +2 -2
- package/package.json +7 -5
- package/public/simulator/bind-darwin-arm64.dylib +0 -0
- package/public/simulator/bind-darwin-x64.dylib +0 -0
- package/public/simulator/bind-linux-x64.so +0 -0
- package/public/simulator/entry.mjs +68 -0
- package/public/simulator/extensions.mjs +230 -0
- package/dist/hb.js +0 -203
- package/dist/u.js +0 -2525
package/dist/h6.js
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import { TransformStream, WritableStream } from "node:stream/web";
|
|
2
|
+
import promises, { mkdir, mkdtemp, open as promises_open, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import node_os from "node:os";
|
|
4
|
+
import node_path from "node:path";
|
|
5
|
+
import node_net from "node:net";
|
|
6
|
+
import { Duplex } from "node:stream";
|
|
7
|
+
import { execFile, spawn } from "node:child_process";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { closeSync, existsSync, openSync } from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import { setTimeout as promises_setTimeout } from "node:timers/promises";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { promisify } from "node:util";
|
|
14
|
+
import { ansi_createDebug } from "./2.js";
|
|
15
|
+
import { ClientId, requireNumericPort } from "./f1.js";
|
|
16
|
+
const debug = ansi_createDebug("devtool-mcp-server:takeover");
|
|
17
|
+
const DEBUG_ROUTER_DIR = node_path.join(node_os.homedir(), ".DebugRouterConnector");
|
|
18
|
+
const DEBUG_ROUTER_LOCK_DIR = node_path.join(DEBUG_ROUTER_DIR, "lockfile");
|
|
19
|
+
const DEBUG_ROUTER_LATEST_FILE = node_path.join(DEBUG_ROUTER_DIR, "LatestDriverProcess");
|
|
20
|
+
async function takeoverDebugRouterLock() {
|
|
21
|
+
try {
|
|
22
|
+
await promises.mkdir(DEBUG_ROUTER_DIR, {
|
|
23
|
+
recursive: true
|
|
24
|
+
});
|
|
25
|
+
await promises.rm(DEBUG_ROUTER_LOCK_DIR, {
|
|
26
|
+
recursive: true,
|
|
27
|
+
force: true
|
|
28
|
+
});
|
|
29
|
+
await promises.mkdir(DEBUG_ROUTER_LOCK_DIR, {
|
|
30
|
+
recursive: true
|
|
31
|
+
});
|
|
32
|
+
await promises.writeFile(DEBUG_ROUTER_LATEST_FILE, `${process.pid}`, "utf-8");
|
|
33
|
+
debug(`wrote PID=${process.pid}`);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
debug("skipped due to filesystem error %O", err);
|
|
36
|
+
} finally{
|
|
37
|
+
try {
|
|
38
|
+
await promises.rm(DEBUG_ROUTER_LOCK_DIR, {
|
|
39
|
+
recursive: true,
|
|
40
|
+
force: true
|
|
41
|
+
});
|
|
42
|
+
} catch (_cleanupError) {
|
|
43
|
+
debug("failed to remove lock directory %O", _cleanupError);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
class PeertalkToMessageTransformStream extends TransformStream {
|
|
48
|
+
constructor(){
|
|
49
|
+
let buffer = new Uint8Array(0);
|
|
50
|
+
let start = 0;
|
|
51
|
+
let end = 0;
|
|
52
|
+
const decoder = new TextDecoder();
|
|
53
|
+
super({
|
|
54
|
+
transform (chunk, controller) {
|
|
55
|
+
const buffered = end - start;
|
|
56
|
+
if (buffer.length - end < chunk.length) if (buffered + chunk.length <= buffer.length) {
|
|
57
|
+
buffer.copyWithin(0, start, end);
|
|
58
|
+
start = 0;
|
|
59
|
+
end = buffered;
|
|
60
|
+
} else {
|
|
61
|
+
const next = new Uint8Array(Math.max(buffered + chunk.length, Math.max(20, 2 * buffer.length)));
|
|
62
|
+
next.set(buffer.subarray(start, end));
|
|
63
|
+
buffer = next;
|
|
64
|
+
start = 0;
|
|
65
|
+
end = buffered;
|
|
66
|
+
}
|
|
67
|
+
buffer.set(chunk, end);
|
|
68
|
+
end += chunk.length;
|
|
69
|
+
while(end - start >= 20){
|
|
70
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset + start, 20);
|
|
71
|
+
const len = view.getUint32(16);
|
|
72
|
+
if (end - start < 20 + len) break;
|
|
73
|
+
try {
|
|
74
|
+
controller.enqueue(JSON.parse(decoder.decode(buffer.subarray(start + 20, start + 20 + len))));
|
|
75
|
+
} catch (error) {
|
|
76
|
+
controller.error(error);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
start += 20 + len;
|
|
80
|
+
}
|
|
81
|
+
if (start === end) {
|
|
82
|
+
start = 0;
|
|
83
|
+
end = 0;
|
|
84
|
+
buffer = new Uint8Array(0);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const encoder = new TextEncoder();
|
|
91
|
+
function encodePeertalkMessage(message) {
|
|
92
|
+
const body = encoder.encode(JSON.stringify(message));
|
|
93
|
+
const len = body.length;
|
|
94
|
+
const data = new Uint8Array(20 + len);
|
|
95
|
+
const view = new DataView(data.buffer);
|
|
96
|
+
view.setUint32(0, 1);
|
|
97
|
+
view.setUint32(4, 101);
|
|
98
|
+
view.setUint32(8, 0);
|
|
99
|
+
view.setUint32(12, len + 4);
|
|
100
|
+
view.setUint32(16, len);
|
|
101
|
+
data.set(body, 20);
|
|
102
|
+
return data;
|
|
103
|
+
}
|
|
104
|
+
async function connectWithPeertalk(connectRaw, options, takeoverHDT = true) {
|
|
105
|
+
if (takeoverHDT) await takeoverDebugRouterLock();
|
|
106
|
+
const conn = await connectRaw(options);
|
|
107
|
+
let rawWriter = conn.writable.getWriter();
|
|
108
|
+
let writing = false;
|
|
109
|
+
let terminationPromise;
|
|
110
|
+
const releaseWriter = ()=>{
|
|
111
|
+
rawWriter?.releaseLock();
|
|
112
|
+
rawWriter = void 0;
|
|
113
|
+
};
|
|
114
|
+
const terminateConnection = (reason, abortIdleWriter = true)=>{
|
|
115
|
+
if (!terminationPromise) {
|
|
116
|
+
const writer = rawWriter;
|
|
117
|
+
const shouldAbort = void 0 !== writer && (abortIdleWriter || writing);
|
|
118
|
+
if (!shouldAbort) releaseWriter();
|
|
119
|
+
const rawAbort = shouldAbort ? writer.abort(reason) : Promise.resolve();
|
|
120
|
+
const dispose = Promise.resolve().then(()=>conn[Symbol.asyncDispose]());
|
|
121
|
+
terminationPromise = rawAbort.then(()=>dispose, async (error)=>{
|
|
122
|
+
await dispose.catch(()=>{});
|
|
123
|
+
throw error;
|
|
124
|
+
}).finally(releaseWriter);
|
|
125
|
+
terminationPromise.catch(()=>{});
|
|
126
|
+
}
|
|
127
|
+
return terminationPromise;
|
|
128
|
+
};
|
|
129
|
+
const writable = new WritableStream({
|
|
130
|
+
async write (message, controller) {
|
|
131
|
+
const signal = controller.signal;
|
|
132
|
+
const abort = ()=>{
|
|
133
|
+
if (writing) terminateConnection(signal.reason).catch(()=>{});
|
|
134
|
+
};
|
|
135
|
+
signal.addEventListener("abort", abort, {
|
|
136
|
+
once: true
|
|
137
|
+
});
|
|
138
|
+
writing = true;
|
|
139
|
+
try {
|
|
140
|
+
await rawWriter.write(encodePeertalkMessage(message));
|
|
141
|
+
} catch (error) {
|
|
142
|
+
terminateConnection(error).catch(()=>{});
|
|
143
|
+
throw error;
|
|
144
|
+
} finally{
|
|
145
|
+
writing = false;
|
|
146
|
+
signal.removeEventListener("abort", abort);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
close: releaseWriter,
|
|
150
|
+
abort (reason) {
|
|
151
|
+
if (!writing) {
|
|
152
|
+
releaseWriter();
|
|
153
|
+
return Promise.resolve();
|
|
154
|
+
}
|
|
155
|
+
return terminateConnection(reason, true);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
const readable = conn.readable.pipeThrough(new PeertalkToMessageTransformStream());
|
|
159
|
+
return {
|
|
160
|
+
readable,
|
|
161
|
+
writable,
|
|
162
|
+
[Symbol.asyncDispose] () {
|
|
163
|
+
return terminateConnection(new Error("Peertalk connection disposed"), false);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const desktop_debug = ansi_createDebug("devtool-mcp-server:connector:desktop");
|
|
168
|
+
class DesktopTransport {
|
|
169
|
+
id = "desktop";
|
|
170
|
+
#takeoverHDT;
|
|
171
|
+
constructor(takeoverHDT = true){
|
|
172
|
+
this.#takeoverHDT = takeoverHDT;
|
|
173
|
+
}
|
|
174
|
+
async connect(options) {
|
|
175
|
+
return connectWithPeertalk((opts)=>this.#connectRaw(opts), options, this.#takeoverHDT);
|
|
176
|
+
}
|
|
177
|
+
async close() {
|
|
178
|
+
desktop_debug("Desktop transport closed");
|
|
179
|
+
}
|
|
180
|
+
async listDevices() {
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
id: "localhost",
|
|
184
|
+
os: "Desktop"
|
|
185
|
+
}
|
|
186
|
+
];
|
|
187
|
+
}
|
|
188
|
+
async listAvailableApps(deviceId) {
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
async openApp(deviceId, packageName) {
|
|
192
|
+
throw new Error("openApp is not supported on DesktopTransport");
|
|
193
|
+
}
|
|
194
|
+
async #connectRaw({ deviceId, port, signal }) {
|
|
195
|
+
const numericPort = requireNumericPort(port);
|
|
196
|
+
if ("localhost" !== deviceId) throw new Error(`DesktopTransport only supports 'localhost' deviceId, got: ${deviceId}`);
|
|
197
|
+
desktop_debug(`connect: connecting to 127.0.0.1:${numericPort}`);
|
|
198
|
+
const socket = node_net.createConnection({
|
|
199
|
+
host: "127.0.0.1",
|
|
200
|
+
port: numericPort,
|
|
201
|
+
signal
|
|
202
|
+
});
|
|
203
|
+
socket.setNoDelay?.(true);
|
|
204
|
+
const socketClosed = socket.closed ? Promise.resolve() : new Promise((resolve)=>socket.once("close", resolve));
|
|
205
|
+
let disposePromise;
|
|
206
|
+
const dispose = ()=>{
|
|
207
|
+
disposePromise ??= (async ()=>{
|
|
208
|
+
socket.destroy();
|
|
209
|
+
await socketClosed;
|
|
210
|
+
})();
|
|
211
|
+
return disposePromise;
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
if (socket.connecting) await new Promise((resolve, reject)=>{
|
|
215
|
+
socket.once("connect", resolve);
|
|
216
|
+
socket.once("error", reject);
|
|
217
|
+
});
|
|
218
|
+
desktop_debug(`connect: connected to 127.0.0.1:${numericPort}`);
|
|
219
|
+
const { readable, writable } = Duplex.toWeb(socket);
|
|
220
|
+
return {
|
|
221
|
+
readable,
|
|
222
|
+
writable,
|
|
223
|
+
async [Symbol.asyncDispose] () {
|
|
224
|
+
desktop_debug(`connect: closing connection to 127.0.0.1:${numericPort}`);
|
|
225
|
+
await dispose();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
} catch (err) {
|
|
229
|
+
desktop_debug(`connect: error connecting to 127.0.0.1:${numericPort} %O`, err);
|
|
230
|
+
await dispose();
|
|
231
|
+
throw err;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const ASSETS = node_path.join(node_path.dirname(createRequire(import.meta.url).resolve("@lynx-js/devtool-connector/package.json")), "public/simulator");
|
|
236
|
+
function duration(ms) {
|
|
237
|
+
if (!Number.isSafeInteger(ms) || ms < 1 || ms > 2147483647) throw new Error("Duration must be an integer between 1 and 2147483647 milliseconds.");
|
|
238
|
+
return ms;
|
|
239
|
+
}
|
|
240
|
+
class LynxSimulatorTransport extends DesktopTransport {
|
|
241
|
+
id = "simulator";
|
|
242
|
+
persistent = true;
|
|
243
|
+
#runs = new Map();
|
|
244
|
+
#prepared = new Map();
|
|
245
|
+
#lifetime = new AbortController();
|
|
246
|
+
#config;
|
|
247
|
+
get active() {
|
|
248
|
+
return [
|
|
249
|
+
...this.#runs.values()
|
|
250
|
+
].some((run)=>!run.info.endedAt);
|
|
251
|
+
}
|
|
252
|
+
constructor(config = {
|
|
253
|
+
registry: "https://registry.npmjs.org",
|
|
254
|
+
core: {
|
|
255
|
+
name: "@lynx-js/lynx-simulator-core",
|
|
256
|
+
version: "0.1.0-alpha.0"
|
|
257
|
+
},
|
|
258
|
+
lynxtron: {
|
|
259
|
+
name: "@lynx-js/lynxtron",
|
|
260
|
+
version: "0.0.31"
|
|
261
|
+
}
|
|
262
|
+
}){
|
|
263
|
+
super(false);
|
|
264
|
+
this.#config = config;
|
|
265
|
+
}
|
|
266
|
+
async listDevices() {
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
id: "localhost",
|
|
270
|
+
os: "Desktop"
|
|
271
|
+
}
|
|
272
|
+
];
|
|
273
|
+
}
|
|
274
|
+
async listClients() {
|
|
275
|
+
return this.list().filter((info)=>"running" === info.status).map((info)=>({
|
|
276
|
+
id: info.clientId,
|
|
277
|
+
info: {
|
|
278
|
+
App: "Lynx Simulator",
|
|
279
|
+
AppVersion: info.lynxtronVersion,
|
|
280
|
+
sdkVersion: info.lynxtronVersion,
|
|
281
|
+
debugRouterId: info.clientId,
|
|
282
|
+
debugRouterVersion: "unknown",
|
|
283
|
+
deviceModel: `${node_os.platform()}-${node_os.arch()}`,
|
|
284
|
+
network: "localhost",
|
|
285
|
+
osType: node_os.platform(),
|
|
286
|
+
osVersion: node_os.release(),
|
|
287
|
+
...info
|
|
288
|
+
}
|
|
289
|
+
}));
|
|
290
|
+
}
|
|
291
|
+
list() {
|
|
292
|
+
return [
|
|
293
|
+
...this.#runs.values()
|
|
294
|
+
].map((run)=>({
|
|
295
|
+
...run.info
|
|
296
|
+
}));
|
|
297
|
+
}
|
|
298
|
+
#get(clientId) {
|
|
299
|
+
const run = this.#runs.get(clientId);
|
|
300
|
+
if (!run) throw new Error(`Unknown Lynx Simulator client: ${clientId}`);
|
|
301
|
+
return run;
|
|
302
|
+
}
|
|
303
|
+
get(clientId) {
|
|
304
|
+
return {
|
|
305
|
+
...this.#get(clientId).info
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
async logs(clientId, offset = 0) {
|
|
309
|
+
const run = this.#get(clientId);
|
|
310
|
+
if (!Number.isSafeInteger(offset) || offset < 0) throw new Error("Invalid log offset.");
|
|
311
|
+
const file = await promises_open(run.info.logPath, "r");
|
|
312
|
+
try {
|
|
313
|
+
const { size } = await file.stat();
|
|
314
|
+
if (offset > size) offset = size;
|
|
315
|
+
const bytes = Buffer.alloc(Math.min(65536, size - offset));
|
|
316
|
+
const { bytesRead } = await file.read(bytes, 0, bytes.length, offset);
|
|
317
|
+
return {
|
|
318
|
+
content: bytes.subarray(0, bytesRead).toString("base64"),
|
|
319
|
+
offset: offset + bytesRead,
|
|
320
|
+
size,
|
|
321
|
+
eof: offset + bytesRead >= size,
|
|
322
|
+
finished: Boolean(run.info.endedAt)
|
|
323
|
+
};
|
|
324
|
+
} finally{
|
|
325
|
+
await file.close();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async wait(clientId, timeoutMs, signal) {
|
|
329
|
+
const run = this.#get(clientId);
|
|
330
|
+
duration(timeoutMs);
|
|
331
|
+
const cancelled = new AbortController();
|
|
332
|
+
try {
|
|
333
|
+
await Promise.race([
|
|
334
|
+
run.finished,
|
|
335
|
+
promises_setTimeout(timeoutMs, void 0, {
|
|
336
|
+
signal: AbortSignal.any([
|
|
337
|
+
cancelled.signal,
|
|
338
|
+
...signal ? [
|
|
339
|
+
signal
|
|
340
|
+
] : []
|
|
341
|
+
])
|
|
342
|
+
})
|
|
343
|
+
]);
|
|
344
|
+
return this.get(clientId);
|
|
345
|
+
} finally{
|
|
346
|
+
cancelled.abort();
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async stop(clientId) {
|
|
350
|
+
await this.#stop(this.#get(clientId));
|
|
351
|
+
return this.get(clientId);
|
|
352
|
+
}
|
|
353
|
+
connect(options) {
|
|
354
|
+
const run = this.#runs.get(ClientId.serialize(this.id, "localhost", Number(options.port)));
|
|
355
|
+
if (!run || run.info.endedAt) throw new Error("Lynx Simulator is not running.");
|
|
356
|
+
return super.connect({
|
|
357
|
+
...options,
|
|
358
|
+
deviceId: "localhost"
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
async #prepare(cache, host, packages) {
|
|
362
|
+
if (existsSync(cache)) return;
|
|
363
|
+
await mkdir(node_path.dirname(cache), {
|
|
364
|
+
recursive: true
|
|
365
|
+
});
|
|
366
|
+
const staging = await mkdtemp(`${cache}-install-`);
|
|
367
|
+
try {
|
|
368
|
+
await promisify(execFile)("npm", [
|
|
369
|
+
"install",
|
|
370
|
+
"--prefix",
|
|
371
|
+
staging,
|
|
372
|
+
`--registry=${this.#config.registry}`,
|
|
373
|
+
"--no-audit",
|
|
374
|
+
"--no-fund",
|
|
375
|
+
"--save-exact",
|
|
376
|
+
"--ignore-scripts=false",
|
|
377
|
+
`${this.#config.core.name}@${this.#config.core.version}`,
|
|
378
|
+
`${this.#config.lynxtron.name}@${host}`,
|
|
379
|
+
...packages
|
|
380
|
+
], {
|
|
381
|
+
timeout: 480000,
|
|
382
|
+
signal: this.#lifetime.signal
|
|
383
|
+
});
|
|
384
|
+
try {
|
|
385
|
+
await rename(staging, cache);
|
|
386
|
+
} catch (error) {
|
|
387
|
+
if (!existsSync(cache)) throw error;
|
|
388
|
+
}
|
|
389
|
+
} finally{
|
|
390
|
+
await rm(staging, {
|
|
391
|
+
recursive: true,
|
|
392
|
+
force: true
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
async run(connector, signal, ttlMs, options = {}) {
|
|
397
|
+
if (void 0 !== ttlMs) duration(ttlMs);
|
|
398
|
+
const { core, lynxtron, registry } = this.#config;
|
|
399
|
+
const host = options.lynxtronVersion ?? lynxtron.version;
|
|
400
|
+
if ("string" != typeof host || !/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(host)) throw new Error("lynxtronVersion must be an exact version.");
|
|
401
|
+
const jsb = withDefaults(this.#config.jsb ?? [], packageList(options.jsb, "jsb"));
|
|
402
|
+
const nativeExtensions = withDefaults(this.#config.nativeExtensions?.(host) ?? [], packageList(options.nativeExtensions, "nativeExtensions"));
|
|
403
|
+
const cache = node_path.join(node_os.homedir(), ".cache/agent-lynx/simulator", createHash("sha256").update(JSON.stringify({
|
|
404
|
+
registry,
|
|
405
|
+
core,
|
|
406
|
+
host,
|
|
407
|
+
name: lynxtron.name,
|
|
408
|
+
jsb,
|
|
409
|
+
nativeExtensions
|
|
410
|
+
})).digest("hex").slice(0, 24));
|
|
411
|
+
const packages = [
|
|
412
|
+
...jsb.map((spec, i)=>`agent-lynx-jsb-${i}@npm:${spec}`),
|
|
413
|
+
...nativeExtensions.map((spec, i)=>`agent-lynx-native-${i}@npm:${spec}`)
|
|
414
|
+
];
|
|
415
|
+
const hook = node_path.join(ASSETS, `bind-${process.platform}-${process.arch}.${"darwin" === process.platform ? "dylib" : "so"}`);
|
|
416
|
+
if (!existsSync(hook)) throw new Error(`Lynx Simulator does not support ${process.platform}/${process.arch}.`);
|
|
417
|
+
let prepared = this.#prepared.get(cache);
|
|
418
|
+
if (!prepared) {
|
|
419
|
+
prepared = this.#prepare(cache, host, packages).catch((error)=>{
|
|
420
|
+
this.#prepared.delete(cache);
|
|
421
|
+
throw error;
|
|
422
|
+
});
|
|
423
|
+
this.#prepared.set(cache, prepared);
|
|
424
|
+
}
|
|
425
|
+
await prepared;
|
|
426
|
+
signal?.throwIfAborted();
|
|
427
|
+
this.#lifetime.signal.throwIfAborted();
|
|
428
|
+
const listeners = [];
|
|
429
|
+
let port, clientId;
|
|
430
|
+
try {
|
|
431
|
+
do {
|
|
432
|
+
signal?.throwIfAborted();
|
|
433
|
+
this.#lifetime.signal.throwIfAborted();
|
|
434
|
+
const listener = node_net.createServer();
|
|
435
|
+
await new Promise((resolve, reject)=>{
|
|
436
|
+
listener.once("error", reject);
|
|
437
|
+
listener.listen(0, "127.0.0.1", resolve);
|
|
438
|
+
});
|
|
439
|
+
listeners.push(listener);
|
|
440
|
+
port = listener.address().port;
|
|
441
|
+
clientId = ClientId.serialize(this.id, "localhost", port);
|
|
442
|
+
}while (this.#runs.has(clientId));
|
|
443
|
+
} finally{
|
|
444
|
+
await Promise.all(listeners.map((listener)=>new Promise((resolve, reject)=>listener.close((error)=>error ? reject(error) : resolve()))));
|
|
445
|
+
}
|
|
446
|
+
const directory = await mkdtemp(node_path.join(cache, "run-"));
|
|
447
|
+
await writeFile(node_path.join(directory, "packages.mjs"), "export const loadPackage = name => import(name);\n");
|
|
448
|
+
const logPath = node_path.join(directory, "simulator.log");
|
|
449
|
+
const { default: executable } = await import(pathToFileURL(node_path.join(cache, "node_modules", lynxtron.name, "lynxtron_bin.js")).href);
|
|
450
|
+
const log = openSync(logPath, "a");
|
|
451
|
+
const child = spawn(executable, [
|
|
452
|
+
node_path.join(ASSETS, "entry.mjs")
|
|
453
|
+
], {
|
|
454
|
+
detached: true,
|
|
455
|
+
stdio: [
|
|
456
|
+
"pipe",
|
|
457
|
+
log,
|
|
458
|
+
log
|
|
459
|
+
],
|
|
460
|
+
env: {
|
|
461
|
+
...process.env,
|
|
462
|
+
AGENT_LYNX_SIMULATOR_ROOT: cache,
|
|
463
|
+
AGENT_LYNX_SIMULATOR_CORE: core.name,
|
|
464
|
+
AGENT_LYNX_SIMULATOR_PACKAGES: JSON.stringify({
|
|
465
|
+
jsb,
|
|
466
|
+
nativeExtensions
|
|
467
|
+
}),
|
|
468
|
+
AGENT_LYNX_SIMULATOR_RUN: directory,
|
|
469
|
+
AGENT_LYNX_SIMULATOR_PORT: String(port),
|
|
470
|
+
AGENT_LYNX_SIMULATOR_BOUND: node_path.join(directory, "bound"),
|
|
471
|
+
["darwin" === process.platform ? "DYLD_INSERT_LIBRARIES" : "LD_PRELOAD"]: hook
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
closeSync(log);
|
|
475
|
+
const info = {
|
|
476
|
+
clientId,
|
|
477
|
+
pid: child.pid ?? null,
|
|
478
|
+
port,
|
|
479
|
+
status: "starting",
|
|
480
|
+
logPath,
|
|
481
|
+
startedAt: new Date().toISOString(),
|
|
482
|
+
exitCode: null,
|
|
483
|
+
signal: null,
|
|
484
|
+
coreVersion: core.version,
|
|
485
|
+
lynxtronVersion: host,
|
|
486
|
+
jsb,
|
|
487
|
+
nativeExtensions
|
|
488
|
+
};
|
|
489
|
+
let finish;
|
|
490
|
+
const run = {
|
|
491
|
+
info,
|
|
492
|
+
child,
|
|
493
|
+
finished: new Promise((resolve)=>{
|
|
494
|
+
finish = resolve;
|
|
495
|
+
})
|
|
496
|
+
};
|
|
497
|
+
this.#runs.set(clientId, run);
|
|
498
|
+
const complete = (code, exitSignal, error)=>{
|
|
499
|
+
if (info.endedAt) return;
|
|
500
|
+
if (error) info.error = error.message;
|
|
501
|
+
info.status = info.error ? "failed" : "stopping" === info.status ? "stopped" : 0 === code ? "exited" : "failed";
|
|
502
|
+
Object.assign(info, {
|
|
503
|
+
exitCode: code,
|
|
504
|
+
signal: exitSignal,
|
|
505
|
+
endedAt: new Date().toISOString()
|
|
506
|
+
});
|
|
507
|
+
clearTimeout(run.ttl);
|
|
508
|
+
finish();
|
|
509
|
+
};
|
|
510
|
+
child.once("error", (error)=>complete(null, null, error));
|
|
511
|
+
child.once("exit", (code, exitSignal)=>complete(code, exitSignal));
|
|
512
|
+
try {
|
|
513
|
+
const deadline = Date.now() + 20000;
|
|
514
|
+
while(!existsSync(node_path.join(directory, "ready")) || !existsSync(node_path.join(directory, "bound"))){
|
|
515
|
+
signal?.throwIfAborted();
|
|
516
|
+
this.#lifetime.signal.throwIfAborted();
|
|
517
|
+
if (info.endedAt || Date.now() > deadline) throw new Error(`${info.error ?? "Lynx Simulator failed to start"}: ${await readFile(logPath, "utf8")} (${logPath})`);
|
|
518
|
+
await promises_setTimeout(50);
|
|
519
|
+
}
|
|
520
|
+
await connector.sendListSessionMessage(clientId);
|
|
521
|
+
signal?.throwIfAborted();
|
|
522
|
+
this.#lifetime.signal.throwIfAborted();
|
|
523
|
+
if ("starting" !== info.status) throw new Error("Lynx Simulator stopped during startup.");
|
|
524
|
+
info.status = "running";
|
|
525
|
+
if (void 0 !== ttlMs) {
|
|
526
|
+
info.expiresAt = new Date(Date.now() + ttlMs).toISOString();
|
|
527
|
+
run.ttl = setTimeout(()=>{
|
|
528
|
+
this.#stop(run, "ttl").catch(console.error);
|
|
529
|
+
}, ttlMs);
|
|
530
|
+
}
|
|
531
|
+
return this.get(clientId);
|
|
532
|
+
} catch (error) {
|
|
533
|
+
if (void 0 === info.stopReason) {
|
|
534
|
+
info.error = error instanceof Error ? error.message : String(error);
|
|
535
|
+
if (info.endedAt) info.status = "failed";
|
|
536
|
+
}
|
|
537
|
+
await this.#stop(run, "startup-failed");
|
|
538
|
+
throw error;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
async #stop(run, reason = "requested") {
|
|
542
|
+
if (run.info.endedAt) return;
|
|
543
|
+
await (run.stopping ??= (async ()=>{
|
|
544
|
+
run.info.status = "stopping";
|
|
545
|
+
run.info.stopReason = reason;
|
|
546
|
+
run.child.stdin?.end();
|
|
547
|
+
const timer = setTimeout(()=>{
|
|
548
|
+
try {
|
|
549
|
+
if (run.child.pid) process.kill(-run.child.pid, "SIGKILL");
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if ("ESRCH" !== error.code) console.error(error);
|
|
552
|
+
}
|
|
553
|
+
}, 3000);
|
|
554
|
+
await run.finished.finally(()=>clearTimeout(timer));
|
|
555
|
+
})());
|
|
556
|
+
}
|
|
557
|
+
async close() {
|
|
558
|
+
this.#lifetime.abort();
|
|
559
|
+
await Promise.all([
|
|
560
|
+
...this.#runs.values()
|
|
561
|
+
].map((run)=>this.#stop(run, "daemon")));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
function packageList(value, name) {
|
|
565
|
+
if (void 0 === value) return [];
|
|
566
|
+
if (!Array.isArray(value) || !value.every((spec)=>"string" == typeof spec && /^(?:@[a-z0-9._-]+\/)?[a-z0-9][a-z0-9._-]*(?:@[a-zA-Z0-9*^~.><=|+ -]+)?$/.test(spec))) throw new Error(`${name} must be an array of npm package names, optionally followed by @version.`);
|
|
567
|
+
return [
|
|
568
|
+
...new Set(value)
|
|
569
|
+
];
|
|
570
|
+
}
|
|
571
|
+
function withDefaults(builtins, packages) {
|
|
572
|
+
const name = (spec)=>spec.replace(/@[^@/]+$/, "");
|
|
573
|
+
return [
|
|
574
|
+
...builtins.filter((builtin)=>!packages.some((spec)=>name(spec) === name(builtin))),
|
|
575
|
+
...packages
|
|
576
|
+
];
|
|
577
|
+
}
|
|
578
|
+
export { DesktopTransport, LynxSimulatorTransport, PeertalkToMessageTransformStream, connectWithPeertalk, encodePeertalkMessage };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream, createCorrelatedFilter, isCustomizedResponseWithType, isGetGlobalSwitchResponse, isInitializeResponse, isListSessionResponse, isSetGlobalSwitchResponse } from "./
|
|
1
|
+
export { CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream, createCorrelatedFilter, isCustomizedResponseWithType, isGetGlobalSwitchResponse, isInitializeResponse, isListSessionResponse, isSetGlobalSwitchResponse } from "./h.js";
|
|
2
2
|
export { ClientId } from "./f1.js";
|
|
3
|
-
export { Connector } from "./
|
|
3
|
+
export { Connector } from "./az.js";
|
|
4
4
|
export { nextRequestId } from "./3.js";
|
package/dist/main.js
CHANGED
package/dist/streams/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { AppResponseTransformStream, CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream, CustomizedClientIdTransformStream, CustomizedRequestTransformStream, CustomizedResponseTransformStream, FilterTransformStream, GlobalSwitchRequestTransformStream, InspectStream, ResponseParserTransformStream, SessionGuardTransformStream } from "../
|
|
1
|
+
export { AppResponseTransformStream, CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream, CustomizedClientIdTransformStream, CustomizedRequestTransformStream, CustomizedResponseTransformStream, FilterTransformStream, GlobalSwitchRequestTransformStream, InspectStream, ResponseParserTransformStream, SessionGuardTransformStream } from "../h.js";
|
|
2
2
|
export { nextRequestId } from "../3.js";
|