@lynx-js/devtool-connector 0.19.1 → 0.20.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/types.d.ts CHANGED
@@ -61,6 +61,7 @@ export type CDPRequest = CustomizedEvent<"CDP", {
61
61
  };
62
62
  }>;
63
63
  export type CDPResponse = CustomizedEvent<"CDP", {
64
+ session_id?: number;
64
65
  /** JSON string. See {@link CDPResponseMessage} for parsed result. */
65
66
  message: string;
66
67
  }>;
@@ -74,6 +75,11 @@ export type CDPResponseMessage = {
74
75
  message: string;
75
76
  };
76
77
  });
78
+ /** JSON message envelope for a host-defined Customized protocol. */
79
+ export type CustomizedResponse<T extends string = string> = CustomizedEvent<T, {
80
+ session_id?: number;
81
+ message: string;
82
+ }>;
77
83
  export type Session = {
78
84
  session_id: number;
79
85
  type: "" | "lynx" | "web";
@@ -110,10 +116,6 @@ export type SetGlobalSwitchResponse = CustomizedEvent<"SetGlobalSwitch", {
110
116
  /** JSON string */
111
117
  message: string;
112
118
  }>;
113
- /** JSON message envelope for a host-defined Customized protocol. */
114
- export type CustomizedResponse<T extends string = string> = CustomizedEvent<T, {
115
- message: string;
116
- }>;
117
119
  export type CustomizedResponseMap = {
118
120
  App: AppResponse;
119
121
  CDP: CDPResponse;
package/dist/ws-stream.js CHANGED
@@ -1,7 +1,7 @@
1
- import { __webpack_require__ } from "./rslib-runtime.js";
1
+ import { rspackRequire } from "./rslib-runtime.js";
2
2
  import { ReadableStream, WritableStream } from "node:stream/web";
3
- import "./0.js";
4
- const websocket = __webpack_require__("../../../node_modules/.pnpm/ws@8.22.2/node_modules/ws/lib/websocket.js");
3
+ import "./t.js";
4
+ const websocket = rspackRequire("../../../node_modules/.pnpm/ws@8.23.0/node_modules/ws/lib/websocket.js");
5
5
  function stringifyWebSocketData(data) {
6
6
  return "string" == typeof data ? data : data.toString();
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/devtool-connector",
3
- "version": "0.19.1",
3
+ "version": "0.20.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/lynx-community/skills.git",
@@ -32,7 +32,7 @@
32
32
  "import": "./dist/streams/index.js",
33
33
  "default": "./dist/streams/index.js"
34
34
  },
35
- "./command": {
35
+ "./_internal/command": {
36
36
  "types": "./dist/command/index.d.ts",
37
37
  "import": "./dist/command/index.js",
38
38
  "default": "./dist/command/index.js"
@@ -51,20 +51,21 @@
51
51
  "@yume-chan/adb": "3.0.0-beta.5"
52
52
  },
53
53
  "devDependencies": {
54
- "@rslib/core": "^1.0.6",
54
+ "@rslib/core": "^1.1.0",
55
55
  "@types/node": "^24.19.2",
56
56
  "@types/semver": "^7.8.0",
57
57
  "@types/ws": "^8.18.2",
58
58
  "@yume-chan/adb-server-node-tcp": "3.0.0-beta.5",
59
59
  "@yume-chan/stream-extra": "3.0.0-beta.5",
60
- "dprint": "^0.64.3",
60
+ "@zigc/cli": "0.17.0",
61
+ "dprint": "^0.65.0",
61
62
  "jpeg-js": "^0.4.4",
62
63
  "obug": "^2.2.1",
63
- "plist": "^5.1.1",
64
+ "plist": "^5.1.2",
64
65
  "rsbuild-plugin-publint": "^1.0.0",
65
66
  "semver": "^7.8.6",
66
67
  "typescript": "^7.0.2",
67
- "ws": "^8.22.2"
68
+ "ws": "^8.23.0"
68
69
  },
69
70
  "engines": {
70
71
  "node": ">=18.19"
@@ -73,7 +74,8 @@
73
74
  "access": "public"
74
75
  },
75
76
  "scripts": {
76
- "build": "rslib build",
77
+ "build": "pnpm run build:simulator && rslib build",
78
+ "build:simulator": "node scripts/simulator/build.mjs",
77
79
  "test": "node --test --test-concurrency=1 \"test/**/*.test.ts\"",
78
80
  "test:e2e": "node --test --test-concurrency=1 'e2e/**/*.test.ts'",
79
81
  "test:e2e:public": "node --test --test-concurrency=1 'e2e/public/**/*.test.ts'",
@@ -0,0 +1,68 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { writeFileSync } from "node:fs";
5
+ import { createRequire } from "node:module";
6
+ import path from "node:path";
7
+ import { pathToFileURL } from "node:url";
8
+ import { createExtensions } from "./extensions.mjs";
9
+
10
+ const directory = process.env.AGENT_LYNX_SIMULATOR_RUN;
11
+ const require = createRequire(path.join(process.env.AGENT_LYNX_SIMULATOR_ROOT, "package.json"));
12
+ const { createSimulatorRuntime, createPageEnvironmentManager } = await import(
13
+ pathToFileURL(require.resolve(process.env.AGENT_LYNX_SIMULATOR_CORE)),
14
+ );
15
+ const { app } = require("lynxtron");
16
+ app.setPath("userData", directory);
17
+ app.on("window-all-closed", () => {});
18
+
19
+ const runtime = createSimulatorRuntime();
20
+ let environments;
21
+ let stopping = false;
22
+ const stop = () => {
23
+ if (stopping) return;
24
+ stopping = true;
25
+ environments?.dispose();
26
+ void runtime.dispose().finally(() => app.quit());
27
+ };
28
+ process.stdin.on("end", stop).resume(); // EOF also handles a daemon killed without cleanup.
29
+ process.on("SIGTERM", stop).on("SIGINT", stop);
30
+ void app.whenReady().then(async () => {
31
+ const packages = JSON.parse(process.env.AGENT_LYNX_SIMULATOR_PACKAGES ?? '{"jsb":[],"nativeExtensions":[]}');
32
+ const { loadPackage } = await import(pathToFileURL(path.join(directory, "packages.mjs")).href);
33
+ environments = await createExtensions({ loadPackage, runtime, createPageEnvironmentManager, packages });
34
+ if (stopping) {
35
+ environments.dispose();
36
+ return;
37
+ }
38
+ await runtime.devTool.setDevToolEnabled(true);
39
+ await runtime.devTool.setLogBoxEnabled(false);
40
+ await runtime.devTool.setOpenCardCallback(url => {
41
+ void (async () => {
42
+ const query = url.startsWith("lynx-simulator:") ? new URL(url).searchParams : new URLSearchParams();
43
+ const target = query.size ? query.get("url") : url;
44
+ if (!target || target.startsWith("lynx-simulator:")) throw new Error("Lynx Simulator URL requires a page url.");
45
+ const flag = query.get("windowless");
46
+ if (flag !== null && flag !== "true" && flag !== "false") throw new Error("windowless must be true or false.");
47
+ const windowless = flag === null ? process.platform === "linux" : flag === "true";
48
+ if (process.platform === "linux" && !windowless) throw new Error("Linux requires windowless rendering.");
49
+ const dpr = query.get("dpr");
50
+ const backgroundColor = query.get("background-color");
51
+ await environments.open({
52
+ target,
53
+ cwd: query.get("cwd") ?? process.cwd(),
54
+ pageEnvironment: query.get("page-env") ?? undefined,
55
+ window: {
56
+ windowless,
57
+ size: { width: Number(query.get("width") ?? 390), height: Number(query.get("height") ?? 844) },
58
+ ...(windowless && dpr !== null ? { deviceScaleFactor: Number(dpr) } : {}),
59
+ ...(backgroundColor !== null ? { backgroundColor } : {}),
60
+ },
61
+ });
62
+ })().catch(console.error);
63
+ });
64
+ writeFileSync(path.join(directory, "ready"), "");
65
+ }).catch(error => {
66
+ console.error(error);
67
+ stop();
68
+ });
@@ -0,0 +1,230 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { readFile } from "node:fs/promises";
5
+ import path from "node:path";
6
+ import { pathToFileURL } from "node:url";
7
+
8
+ // Packages execute in the native host, never in the connector daemon.
9
+ export async function createExtensions({ loadPackage, runtime, createPageEnvironmentManager, packages }) {
10
+ const extensions = [];
11
+ const handlers = new Map();
12
+ const subscriptions = [];
13
+ const sessions = new Map();
14
+ let opening;
15
+ let queue = Promise.resolve();
16
+ let disposed = false;
17
+ let environments;
18
+ const deactivate = session => {
19
+ let failure;
20
+ for (const context of session.activated.splice(0).reverse()) {
21
+ try {
22
+ context.deactivate?.();
23
+ } catch (error) {
24
+ failure ??= error;
25
+ }
26
+ }
27
+ if (failure) throw failure;
28
+ };
29
+ const cleanup = session => {
30
+ if (session.disposed) return;
31
+ session.disposed = true;
32
+ if (session.id) sessions.delete(session.id);
33
+ try {
34
+ deactivate(session);
35
+ } catch (error) {
36
+ console.error("Failed to deactivate native extension context", error);
37
+ }
38
+ for (const context of [...session.contexts].reverse()) {
39
+ try {
40
+ context.dispose();
41
+ } catch (error) {
42
+ console.error("Failed to dispose native extension context", error);
43
+ }
44
+ }
45
+ };
46
+ const unsubscribe = runtime.addWindowEventListener(event => {
47
+ if (event.type === "created" && opening) {
48
+ opening.id = event.window.id;
49
+ sessions.set(event.window.id, opening);
50
+ }
51
+ if (event.type === "failed" || event.type === "closed") {
52
+ const session = sessions.get(event.window.id);
53
+ if (session === opening && session) session.releaseRequested = true;
54
+ else if (session) cleanup(session);
55
+ }
56
+ });
57
+
58
+ const dispose = () => {
59
+ if (disposed) return;
60
+ disposed = true;
61
+ unsubscribe();
62
+ for (const unsubscribe of subscriptions.splice(0)) unsubscribe();
63
+ environments?.dispose();
64
+ if (opening) cleanup(opening);
65
+ for (const session of sessions.values()) cleanup(session);
66
+ };
67
+ // These methods provide a small host contract for JSB factories, including
68
+ // createApplicationJSBHandlers exported by the Search Simulator JSB package.
69
+ const host = new Proxy(runtime, {
70
+ get(target, key) {
71
+ if (key === "open") return open;
72
+ const value = Reflect.get(target, key);
73
+ return typeof value === "function" ? value.bind(target) : value;
74
+ },
75
+ });
76
+ try {
77
+ for (const [i, spec] of packages.nativeExtensions.entries()) {
78
+ const module = await loadPackage(`agent-lynx-native-${i}`);
79
+ const extension = module.default ?? module;
80
+ if (typeof extension.setUp !== "function") throw new Error(`${spec} must export setUp().`);
81
+ if (extension.createContext !== undefined && typeof extension.createContext !== "function") {
82
+ throw new Error(`${spec}.createContext must be a function.`);
83
+ }
84
+ await runtime.registerNativeExtension(extension);
85
+ extensions.push(extension);
86
+ }
87
+ for (const [i, spec] of packages.jsb.entries()) {
88
+ const module = await loadPackage(`agent-lynx-jsb-${i}`);
89
+ const factory = module.createJSBHandlers ?? module.createApplicationJSBHandlers ?? module.default;
90
+ if (typeof factory !== "function") throw new Error(`${spec} must export a JSB handler factory.`);
91
+ const registrations = await factory({
92
+ runtime: host,
93
+ cwd: process.cwd(),
94
+ fetch: globalThis.fetch,
95
+ xbridge: {
96
+ event: {
97
+ host: {
98
+ sendGlobalEvent: (id, name, params) => runtime.getWindow(id)?.getLynxWindow()?.sendGlobalEvent(name, params),
99
+ addWindowCloseListener: listener => {
100
+ const unsubscribe = runtime.addWindowEventListener(event => {
101
+ if (event.type === "closed") listener(event.window.id);
102
+ });
103
+ subscriptions.push(unsubscribe);
104
+ return unsubscribe;
105
+ },
106
+ },
107
+ },
108
+ },
109
+ getBackgroundColor: () => "#ffffff",
110
+ resolveSize: input => {
111
+ const device = input?.device === undefined ? undefined : runtime.getDevice(input.device);
112
+ if (input?.device !== undefined && !device) return { error: `Unknown device: ${input.device}` };
113
+ const [width, height] = typeof input?.size === "string" ? input.size.split("x").map(Number) : [];
114
+ const size = device?.size ?? {
115
+ width: input?.width ?? width ?? 390,
116
+ height: input?.height ?? height ?? 844,
117
+ };
118
+ return Object.values(size).every(value => Number.isFinite(value) && value > 0)
119
+ ? { size }
120
+ : { error: "Window dimensions must be positive numbers." };
121
+ },
122
+ });
123
+ if (!Array.isArray(registrations)) throw new Error(`${spec} must return an array of JSB handlers.`);
124
+ for (const registration of registrations) {
125
+ if (!registration || typeof registration.name !== "string" || typeof registration.handler !== "function") {
126
+ throw new Error(`${spec} returned an invalid JSB handler.`);
127
+ }
128
+ handlers.set(registration.name, registration);
129
+ }
130
+ }
131
+ environments = createPageEnvironmentManager({ runtime, realHandlers: [...handlers.values()] });
132
+ } catch (error) {
133
+ dispose();
134
+ throw error;
135
+ }
136
+
137
+ async function open(options) {
138
+ // The extension API binds the active native context in OnLynxViewCreate.
139
+ // Serialize opens so overlapping asynchronous loads cannot cross-bind data.
140
+ const task = queue.then(async () => {
141
+ if (disposed) throw new Error("Simulator extensions have been disposed.");
142
+ const session = { contexts: [], activated: [], id: undefined, disposed: false, releaseRequested: false };
143
+ let ref;
144
+ try {
145
+ try {
146
+ const cwd = options.cwd ?? process.cwd();
147
+ const environment = await readEnvironment(options.pageEnvironment, {
148
+ cwd,
149
+ target: options.target,
150
+ windowOptions: options.window,
151
+ context: options.context,
152
+ });
153
+ if (disposed) throw new Error("Simulator extensions have been disposed.");
154
+ const { nativeData, context, ...pageEnvironment } = environment;
155
+ for (const extension of extensions) {
156
+ if (!extension.createContext) continue;
157
+ const nativeContext = extension.createContext(structuredClone(nativeData ?? {}));
158
+ if (!nativeContext || typeof nativeContext.dispose !== "function") {
159
+ throw new Error("Native extension createContext() must return a disposable context.");
160
+ }
161
+ session.contexts.push(nativeContext);
162
+ session.activated.push(nativeContext);
163
+ nativeContext.activate?.();
164
+ }
165
+ opening = session;
166
+ ref = await environments.open({
167
+ ...options,
168
+ cwd,
169
+ window: { windowless: process.platform === "linux", ...options.window },
170
+ context: { ...options.context, ...context },
171
+ pageEnvironment,
172
+ });
173
+ session.id ??= ref.id;
174
+ if (!session.disposed) sessions.set(ref.id, session);
175
+ if (session.releaseRequested || ref.getInfo()?.error || disposed) cleanup(session);
176
+ return ref;
177
+ } finally {
178
+ opening = undefined;
179
+ deactivate(session);
180
+ }
181
+ } catch (error) {
182
+ cleanup(session);
183
+ const window = ref ?? (session.id ? runtime.getWindow(session.id) : undefined);
184
+ await window?.close().catch(console.error);
185
+ throw error;
186
+ }
187
+ });
188
+ queue = task.catch(() => {});
189
+ return task;
190
+ }
191
+ return { open, dispose };
192
+ }
193
+
194
+ async function readEnvironment(source, context) {
195
+ let definition = source ?? {};
196
+ if (typeof source === "string") {
197
+ const url = path.isAbsolute(source) || !URL.canParse(source)
198
+ ? pathToFileURL(path.resolve(context.cwd, source))
199
+ : new URL(source);
200
+ context = { ...context, source: url.href };
201
+ if (/\.(mjs|js)$/i.test(url.pathname)) {
202
+ if (url.protocol !== "file:") throw new Error("JavaScript page environments must be local files.");
203
+ definition = (await import(url.href)).default;
204
+ if (typeof definition !== "function") throw new Error("Page environment must default-export a factory.");
205
+ } else if (url.protocol === "file:") definition = JSON.parse(await readFile(url, "utf8"));
206
+ else if (url.protocol === "http:" || url.protocol === "https:") {
207
+ const response = await fetch(url);
208
+ if (!response.ok) throw new Error(`Page environment fetch failed: ${response.status}`);
209
+ definition = await response.json();
210
+ } else throw new Error(`Unsupported page environment protocol: ${url.protocol}`);
211
+ }
212
+ if (typeof definition === "function") definition = await definition(context);
213
+ if (!definition || typeof definition !== "object" || Array.isArray(definition)) {
214
+ throw new Error("Page environment must be an object.");
215
+ }
216
+ // Evaluate once, before native view creation, retaining the source context
217
+ // for factories that resolve fixtures relative to the page-env module.
218
+ const result = { ...definition };
219
+ for (const key of ["templateData", "globalProps", "nativeData", "context"]) {
220
+ if (typeof result[key] === "function") result[key] = await result[key](context);
221
+ if (result[key] !== undefined && (!result[key] || typeof result[key] !== "object" || Array.isArray(result[key]))) {
222
+ throw new Error(`pageEnvironment.${key} must be an object.`);
223
+ }
224
+ }
225
+ if (typeof result.jsb === "function") {
226
+ const invoke = result.jsb;
227
+ result.jsb = (name, params, callContext) => invoke(name, params, { ...context, ...callContext, source: context.source });
228
+ }
229
+ return result;
230
+ }