@ricsam/isolate 0.1.10 → 0.1.12
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/README.md +257 -18
- package/dist/cjs/bridge/diagnostics.cjs +37 -2
- package/dist/cjs/bridge/diagnostics.cjs.map +3 -3
- package/dist/cjs/bridge/runtime-bindings.cjs +230 -51
- package/dist/cjs/bridge/runtime-bindings.cjs.map +3 -3
- package/dist/cjs/bridge/sandbox-isolate.cjs +464 -0
- package/dist/cjs/bridge/sandbox-isolate.cjs.map +10 -0
- package/dist/cjs/host/create-isolate-host.cjs +130 -25
- package/dist/cjs/host/create-isolate-host.cjs.map +3 -3
- package/dist/cjs/host/nested-host-controller.cjs +369 -0
- package/dist/cjs/host/nested-host-controller.cjs.map +10 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/internal/browser-source.cjs +102 -0
- package/dist/cjs/internal/browser-source.cjs.map +10 -0
- package/dist/cjs/internal/client/connection.cjs +163 -172
- package/dist/cjs/internal/client/connection.cjs.map +3 -3
- package/dist/cjs/internal/daemon/callback-fs-handler.cjs +3 -3
- package/dist/cjs/internal/daemon/callback-fs-handler.cjs.map +3 -3
- package/dist/cjs/internal/daemon/connection.cjs +156 -13
- package/dist/cjs/internal/daemon/connection.cjs.map +3 -3
- package/dist/cjs/internal/playwright/client.cjs +4 -2
- package/dist/cjs/internal/playwright/client.cjs.map +3 -3
- package/dist/cjs/internal/playwright/handler.cjs +298 -25
- package/dist/cjs/internal/playwright/handler.cjs.map +3 -3
- package/dist/cjs/internal/playwright/index.cjs +54 -8
- package/dist/cjs/internal/playwright/index.cjs.map +3 -3
- package/dist/cjs/internal/playwright/types.cjs +3 -1
- package/dist/cjs/internal/playwright/types.cjs.map +3 -3
- package/dist/cjs/internal/protocol/codec.cjs +16 -5
- package/dist/cjs/internal/protocol/codec.cjs.map +3 -3
- package/dist/cjs/internal/protocol/marshalValue.cjs +37 -6
- package/dist/cjs/internal/protocol/marshalValue.cjs.map +3 -3
- package/dist/cjs/internal/protocol/types.cjs +2 -1
- package/dist/cjs/internal/protocol/types.cjs.map +3 -3
- package/dist/cjs/internal/runtime/index.cjs +377 -22
- package/dist/cjs/internal/runtime/index.cjs.map +3 -3
- package/dist/cjs/internal/typecheck/index.cjs +2 -1
- package/dist/cjs/internal/typecheck/index.cjs.map +3 -3
- package/dist/cjs/internal/typecheck/isolate-types.cjs +218 -13
- package/dist/cjs/internal/typecheck/isolate-types.cjs.map +3 -3
- package/dist/cjs/internal/typecheck/typecheck.cjs +2 -3
- package/dist/cjs/internal/typecheck/typecheck.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/playwright.cjs +76 -0
- package/dist/cjs/playwright.cjs.map +10 -0
- package/dist/cjs/runtime/namespaced-runtime.cjs +181 -0
- package/dist/cjs/runtime/namespaced-runtime.cjs.map +10 -0
- package/dist/cjs/runtime/script-runtime.cjs +14 -12
- package/dist/cjs/runtime/script-runtime.cjs.map +3 -3
- package/dist/cjs/runtime/test-runtime.cjs +113 -0
- package/dist/cjs/runtime/test-runtime.cjs.map +10 -0
- package/dist/cjs/server/app-server.cjs +16 -9
- package/dist/cjs/server/app-server.cjs.map +3 -3
- package/dist/cjs/typecheck/index.cjs +2 -1
- package/dist/cjs/typecheck/index.cjs.map +3 -3
- package/dist/mjs/bridge/diagnostics.mjs +37 -2
- package/dist/mjs/bridge/diagnostics.mjs.map +3 -3
- package/dist/mjs/bridge/runtime-bindings.mjs +233 -51
- package/dist/mjs/bridge/runtime-bindings.mjs.map +3 -3
- package/dist/mjs/bridge/sandbox-isolate.mjs +424 -0
- package/dist/mjs/bridge/sandbox-isolate.mjs.map +10 -0
- package/dist/mjs/host/create-isolate-host.mjs +132 -25
- package/dist/mjs/host/create-isolate-host.mjs.map +3 -3
- package/dist/mjs/host/nested-host-controller.mjs +333 -0
- package/dist/mjs/host/nested-host-controller.mjs.map +10 -0
- package/dist/mjs/index.mjs.map +1 -1
- package/dist/mjs/internal/browser-source.mjs +62 -0
- package/dist/mjs/internal/browser-source.mjs.map +10 -0
- package/dist/mjs/internal/client/connection.mjs +165 -173
- package/dist/mjs/internal/client/connection.mjs.map +3 -3
- package/dist/mjs/internal/daemon/callback-fs-handler.mjs +3 -3
- package/dist/mjs/internal/daemon/callback-fs-handler.mjs.map +3 -3
- package/dist/mjs/internal/daemon/connection.mjs +156 -13
- package/dist/mjs/internal/daemon/connection.mjs.map +3 -3
- package/dist/mjs/internal/playwright/client.mjs +7 -3
- package/dist/mjs/internal/playwright/client.mjs.map +3 -3
- package/dist/mjs/internal/playwright/handler.mjs +300 -26
- package/dist/mjs/internal/playwright/handler.mjs.map +3 -3
- package/dist/mjs/internal/playwright/index.mjs +59 -9
- package/dist/mjs/internal/playwright/index.mjs.map +3 -3
- package/dist/mjs/internal/playwright/types.mjs +3 -1
- package/dist/mjs/internal/playwright/types.mjs.map +3 -3
- package/dist/mjs/internal/protocol/codec.mjs +16 -5
- package/dist/mjs/internal/protocol/codec.mjs.map +3 -3
- package/dist/mjs/internal/protocol/marshalValue.mjs +38 -6
- package/dist/mjs/internal/protocol/marshalValue.mjs.map +3 -3
- package/dist/mjs/internal/protocol/types.mjs +2 -1
- package/dist/mjs/internal/protocol/types.mjs.map +3 -3
- package/dist/mjs/internal/runtime/index.mjs +377 -22
- package/dist/mjs/internal/runtime/index.mjs.map +3 -3
- package/dist/mjs/internal/typecheck/index.mjs +3 -1
- package/dist/mjs/internal/typecheck/index.mjs.map +3 -3
- package/dist/mjs/internal/typecheck/isolate-types.mjs +218 -13
- package/dist/mjs/internal/typecheck/isolate-types.mjs.map +3 -3
- package/dist/mjs/internal/typecheck/typecheck.mjs +2 -3
- package/dist/mjs/internal/typecheck/typecheck.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/playwright.mjs +47 -0
- package/dist/mjs/playwright.mjs.map +10 -0
- package/dist/mjs/runtime/namespaced-runtime.mjs +143 -0
- package/dist/mjs/runtime/namespaced-runtime.mjs.map +10 -0
- package/dist/mjs/runtime/script-runtime.mjs +16 -12
- package/dist/mjs/runtime/script-runtime.mjs.map +3 -3
- package/dist/mjs/runtime/test-runtime.mjs +78 -0
- package/dist/mjs/runtime/test-runtime.mjs.map +10 -0
- package/dist/mjs/server/app-server.mjs +23 -11
- package/dist/mjs/server/app-server.mjs.map +3 -3
- package/dist/mjs/typecheck/index.mjs +2 -1
- package/dist/mjs/typecheck/index.mjs.map +3 -3
- package/dist/types/bridge/diagnostics.d.ts +6 -1
- package/dist/types/bridge/runtime-bindings.d.ts +5 -1
- package/dist/types/bridge/sandbox-isolate.d.ts +21 -0
- package/dist/types/host/nested-host-controller.d.ts +15 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/internal/browser-source.d.ts +10 -0
- package/dist/types/internal/client/types.d.ts +9 -0
- package/dist/types/internal/daemon/types.d.ts +0 -2
- package/dist/types/internal/playwright/client.d.ts +2 -2
- package/dist/types/internal/playwright/handler.d.ts +27 -4
- package/dist/types/internal/playwright/index.d.ts +2 -2
- package/dist/types/internal/playwright/types.d.ts +33 -1
- package/dist/types/internal/protocol/codec.d.ts +12 -2
- package/dist/types/internal/protocol/marshalValue.d.ts +3 -2
- package/dist/types/internal/protocol/types.d.ts +33 -2
- package/dist/types/internal/runtime/index.d.ts +5 -0
- package/dist/types/internal/typecheck/index.d.ts +1 -1
- package/dist/types/internal/typecheck/isolate-types.d.ts +6 -4
- package/dist/types/internal/typecheck/typecheck.d.ts +1 -1
- package/dist/types/playwright.d.ts +26 -0
- package/dist/types/runtime/namespaced-runtime.d.ts +11 -0
- package/dist/types/runtime/script-runtime.d.ts +2 -1
- package/dist/types/runtime/test-runtime.d.ts +4 -0
- package/dist/types/server/app-server.d.ts +2 -1
- package/dist/types/types.d.ts +75 -33
- package/package.json +8 -3
- package/dist/cjs/browser/browser-runtime.cjs +0 -157
- package/dist/cjs/browser/browser-runtime.cjs.map +0 -10
- package/dist/mjs/browser/browser-runtime.mjs +0 -93
- package/dist/mjs/browser/browser-runtime.mjs.map +0 -10
- package/dist/types/browser/browser-runtime.d.ts +0 -3
|
@@ -68,10 +68,10 @@ function createCallbackFileSystemHandler(options) {
|
|
|
68
68
|
const resolvePath = (path) => {
|
|
69
69
|
const cleanPath = path.startsWith("/") ? path.slice(1) : path;
|
|
70
70
|
if (!basePath || basePath === "/") {
|
|
71
|
-
return `/${cleanPath}
|
|
71
|
+
return cleanPath ? `/${cleanPath}` : "/";
|
|
72
72
|
}
|
|
73
73
|
const cleanBase = basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
74
|
-
return `${cleanBase}/${cleanPath}
|
|
74
|
+
return cleanPath ? `${cleanBase}/${cleanPath}` : cleanBase;
|
|
75
75
|
};
|
|
76
76
|
const getCallbackId = (name) => {
|
|
77
77
|
return callbackContext.fs[name];
|
|
@@ -352,4 +352,4 @@ function createCallbackFileSystemHandler(options) {
|
|
|
352
352
|
};
|
|
353
353
|
}
|
|
354
354
|
|
|
355
|
-
//# debugId=
|
|
355
|
+
//# debugId=CB9F765E52A1FA1B64756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/internal/daemon/callback-fs-handler.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Callback-based FileSystemHandler adapter.\n *\n * Adapts simple client callbacks (readFile, writeFile, etc.) to the\n * FileSystemHandler interface used by ../fs/index.ts.\n */\n\nimport type { FileSystemHandler } from \"../fs/index.cjs\";\nimport type { ConnectionState, CallbackContext } from \"./types.cjs\";\n\n/** Common MIME type mappings by file extension. */\nconst MIME_TYPES: Record<string, string> = {\n txt: \"text/plain\",\n html: \"text/html\",\n htm: \"text/html\",\n css: \"text/css\",\n js: \"text/javascript\",\n json: \"application/json\",\n xml: \"application/xml\",\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n svg: \"image/svg+xml\",\n pdf: \"application/pdf\",\n};\n\ninterface InvokeClientCallback {\n (\n connection: ConnectionState,\n callbackId: number,\n args: unknown[],\n options?: { signal?: AbortSignal },\n ): Promise<unknown>;\n}\n\ninterface CallbackFsHandlerOptions {\n connection: ConnectionState;\n callbackContext: CallbackContext;\n invokeClientCallback: InvokeClientCallback;\n basePath?: string;\n getSignal?: () => AbortSignal | undefined;\n}\n\n/**\n * Create a FileSystemHandler that invokes client callbacks.\n *\n * Maps WHATWG FileSystem API operations to simple POSIX-like callbacks.\n * Uses callbackContext for dynamic callback ID lookup to support runtime reuse.\n */\nexport function createCallbackFileSystemHandler(\n options: CallbackFsHandlerOptions\n): FileSystemHandler {\n const {\n connection,\n callbackContext,\n invokeClientCallback,\n basePath = \"\",\n getSignal,\n } = options;\n\n const resolvePath = (path: string): string => {\n // Remove leading slash from the path\n const cleanPath = path.startsWith(\"/\") ? path.slice(1) : path;\n // Handle root case\n if (!basePath || basePath === \"/\") {\n return `/${cleanPath}`;\n }\n // Remove trailing slash from basePath\n const cleanBase = basePath.endsWith(\"/\") ? basePath.slice(0, -1) : basePath;\n return `${cleanBase}/${cleanPath}`;\n };\n\n // Helper to get current callback ID (supports runtime reuse)\n const getCallbackId = (name: keyof CallbackContext[\"fs\"]): number | undefined => {\n return callbackContext.fs[name];\n };\n\n // Helper to get current connection (supports runtime reuse and reconnection)\n const getConnection = async (): Promise<ConnectionState> => {\n if (callbackContext.connection) {\n return callbackContext.connection;\n }\n if (callbackContext.reconnectionPromise) {\n return callbackContext.reconnectionPromise.promise;\n }\n // Fall back to the originally captured connection\n return connection;\n };\n\n const invokeFsCallback = async (\n conn: ConnectionState,\n callbackId: number,\n args: unknown[],\n ): Promise<unknown> => {\n return invokeClientCallback(conn, callbackId, args, {\n signal: getSignal?.(),\n });\n };\n\n return {\n async getFileHandle(path: string, opts?: { create?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n if (opts?.create) {\n // Ensure file exists by writing empty content if it doesn't exist\n const writeFileId = getCallbackId(\"writeFile\");\n if (writeFileId !== undefined) {\n try {\n // Check if file exists first\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n await invokeFsCallback(conn, statId, [fullPath]);\n // File exists, nothing to do\n return;\n } catch {\n // File doesn't exist, create it\n }\n }\n // Create empty file\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n new Uint8Array(0),\n ]);\n } catch (err) {\n const error = err as Error;\n throw new Error(`[NotFoundError]${error.message}`);\n }\n }\n return;\n }\n\n // Check file exists\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isFile: boolean };\n if (!result.isFile) {\n throw new Error(`[TypeMismatchError]Not a file: ${fullPath}`);\n }\n } catch (err) {\n const error = err as Error;\n if (error.message.includes(\"TypeMismatchError\")) throw error;\n throw new Error(`[NotFoundError]File not found: ${fullPath}`);\n }\n }\n },\n\n async getDirectoryHandle(path: string, opts?: { create?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n if (opts?.create) {\n const mkdirId = getCallbackId(\"mkdir\");\n if (mkdirId !== undefined) {\n try {\n await invokeFsCallback(conn, mkdirId, [\n fullPath,\n { recursive: true },\n ]);\n } catch {\n // Ignore error if directory already exists\n }\n }\n return;\n }\n\n // Check directory exists\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isDirectory: boolean };\n if (!result.isDirectory) {\n throw new Error(`[TypeMismatchError]Not a directory: ${fullPath}`);\n }\n } catch (err) {\n const error = err as Error;\n if (error.message.includes(\"TypeMismatchError\")) throw error;\n throw new Error(`[NotFoundError]Directory not found: ${fullPath}`);\n }\n }\n },\n\n async removeEntry(path: string, opts?: { recursive?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n // Check if it's a file or directory\n let isFile = true;\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isFile: boolean; isDirectory: boolean };\n isFile = result.isFile;\n } catch {\n throw new Error(`[NotFoundError]Entry not found: ${fullPath}`);\n }\n }\n\n if (isFile) {\n const unlinkId = getCallbackId(\"unlink\");\n if (unlinkId === undefined) {\n throw new Error(`[NotAllowedError]File deletion not supported`);\n }\n await invokeFsCallback(conn, unlinkId, [fullPath]);\n } else {\n const rmdirId = getCallbackId(\"rmdir\");\n if (rmdirId === undefined) {\n throw new Error(`[NotAllowedError]Directory deletion not supported`);\n }\n // Note: recursive option may need special handling\n await invokeFsCallback(conn, rmdirId, [fullPath]);\n }\n },\n\n async readDirectory(\n path: string\n ): Promise<Array<{ name: string; kind: \"file\" | \"directory\" }>> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readdirId = getCallbackId(\"readdir\");\n if (readdirId === undefined) {\n throw new Error(`[NotAllowedError]Directory reading not supported`);\n }\n\n const entries = (await invokeFsCallback(conn, readdirId, [\n fullPath,\n ])) as string[];\n\n // We need to stat each entry to determine if it's a file or directory\n const result: Array<{ name: string; kind: \"file\" | \"directory\" }> = [];\n\n const statId = getCallbackId(\"stat\");\n for (const name of entries) {\n const entryPath = fullPath ? `${fullPath}/${name}` : name;\n let kind: \"file\" | \"directory\" = \"file\";\n\n if (statId !== undefined) {\n try {\n const stat = (await invokeFsCallback(conn, statId, [\n entryPath,\n ])) as { isFile: boolean; isDirectory: boolean };\n kind = stat.isDirectory ? \"directory\" : \"file\";\n } catch {\n // Default to file if stat fails\n }\n }\n\n result.push({ name, kind });\n }\n\n return result;\n },\n\n async readFile(\n path: string\n ): Promise<{ data: Uint8Array; size: number; lastModified: number; type: string }> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readFileId = getCallbackId(\"readFile\");\n if (readFileId === undefined) {\n throw new Error(`[NotAllowedError]File reading not supported`);\n }\n\n const data = (await invokeFsCallback(conn, readFileId, [\n fullPath,\n ])) as Uint8Array | ArrayBuffer | number[];\n\n // Convert to Uint8Array if needed\n let bytes: Uint8Array;\n if (data instanceof Uint8Array) {\n bytes = data;\n } else if (Array.isArray(data)) {\n bytes = new Uint8Array(data);\n } else if (data instanceof ArrayBuffer) {\n bytes = new Uint8Array(data);\n } else {\n bytes = new Uint8Array(0);\n }\n\n // Get metadata if stat is available\n let size = bytes.length;\n let lastModified = Date.now();\n\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const stat = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { size: number; lastModified?: number };\n size = stat.size;\n if (stat.lastModified) {\n lastModified = stat.lastModified;\n }\n } catch {\n // Use byte length as fallback\n }\n }\n\n // Determine MIME type from extension\n const ext = path.split(\".\").pop()?.toLowerCase() || \"\";\n const type = MIME_TYPES[ext] || \"application/octet-stream\";\n\n return { data: bytes, size, lastModified, type };\n },\n\n async writeFile(path: string, data: Uint8Array, position?: number): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const writeFileId = getCallbackId(\"writeFile\");\n if (writeFileId === undefined) {\n throw new Error(`[NotAllowedError]File writing not supported`);\n }\n\n // Note: position parameter for partial writes may need special handling\n // Simple implementation overwrites entire file\n if (position !== undefined && position > 0) {\n // For positional writes, we need to read existing content and merge\n const readFileId = getCallbackId(\"readFile\");\n if (readFileId !== undefined) {\n try {\n const existing = (await invokeFsCallback(\n conn,\n readFileId,\n [fullPath]\n )) as Uint8Array | ArrayBuffer | number[];\n\n let existingBytes: Uint8Array;\n if (existing instanceof Uint8Array) {\n existingBytes = existing;\n } else if (Array.isArray(existing)) {\n existingBytes = new Uint8Array(existing);\n } else if (existing instanceof ArrayBuffer) {\n existingBytes = new Uint8Array(existing);\n } else {\n existingBytes = new Uint8Array(0);\n }\n\n // Create merged buffer\n const newSize = Math.max(existingBytes.length, position + data.length);\n const merged = new Uint8Array(newSize);\n merged.set(existingBytes);\n merged.set(data, position);\n\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n merged,\n ]);\n return;\n } catch {\n // File doesn't exist, create new one at position\n const newData = new Uint8Array(position + data.length);\n newData.set(data, position);\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n newData,\n ]);\n return;\n }\n }\n }\n\n await invokeFsCallback(conn, writeFileId, [fullPath, data]);\n },\n\n async truncateFile(path: string, size: number): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readFileId = getCallbackId(\"readFile\");\n const writeFileId = getCallbackId(\"writeFile\");\n if (readFileId === undefined || writeFileId === undefined) {\n throw new Error(`[NotAllowedError]File truncation not supported`);\n }\n\n // Read existing content\n const existing = (await invokeFsCallback(conn, readFileId, [\n fullPath,\n ])) as Uint8Array | ArrayBuffer | number[];\n\n let existingBytes: Uint8Array;\n if (existing instanceof Uint8Array) {\n existingBytes = existing;\n } else if (Array.isArray(existing)) {\n existingBytes = new Uint8Array(existing);\n } else if (existing instanceof ArrayBuffer) {\n existingBytes = new Uint8Array(existing);\n } else {\n existingBytes = new Uint8Array(0);\n }\n\n // Create truncated buffer\n const truncated = new Uint8Array(size);\n truncated.set(existingBytes.slice(0, size));\n\n await invokeFsCallback(conn, writeFileId, [fullPath, truncated]);\n },\n\n async getFileMetadata(\n path: string\n ): Promise<{ size: number; lastModified: number; type: string }> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const statId = getCallbackId(\"stat\");\n if (statId === undefined) {\n throw new Error(`[NotAllowedError]File stat not supported`);\n }\n\n const stat = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { size: number; lastModified?: number; isFile: boolean };\n\n if (!stat.isFile) {\n throw new Error(`[TypeMismatchError]Not a file: ${fullPath}`);\n }\n\n // Determine MIME type from extension\n const ext = path.split(\".\").pop()?.toLowerCase() || \"\";\n const type = MIME_TYPES[ext] || \"application/octet-stream\";\n\n return {\n size: stat.size,\n lastModified: stat.lastModified ?? Date.now(),\n type,\n };\n },\n };\n}\n"
|
|
5
|
+
"/**\n * Callback-based FileSystemHandler adapter.\n *\n * Adapts simple client callbacks (readFile, writeFile, etc.) to the\n * FileSystemHandler interface used by ../fs/index.ts.\n */\n\nimport type { FileSystemHandler } from \"../fs/index.cjs\";\nimport type { ConnectionState, CallbackContext } from \"./types.cjs\";\n\n/** Common MIME type mappings by file extension. */\nconst MIME_TYPES: Record<string, string> = {\n txt: \"text/plain\",\n html: \"text/html\",\n htm: \"text/html\",\n css: \"text/css\",\n js: \"text/javascript\",\n json: \"application/json\",\n xml: \"application/xml\",\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n svg: \"image/svg+xml\",\n pdf: \"application/pdf\",\n};\n\ninterface InvokeClientCallback {\n (\n connection: ConnectionState,\n callbackId: number,\n args: unknown[],\n options?: { signal?: AbortSignal },\n ): Promise<unknown>;\n}\n\ninterface CallbackFsHandlerOptions {\n connection: ConnectionState;\n callbackContext: CallbackContext;\n invokeClientCallback: InvokeClientCallback;\n basePath?: string;\n getSignal?: () => AbortSignal | undefined;\n}\n\n/**\n * Create a FileSystemHandler that invokes client callbacks.\n *\n * Maps WHATWG FileSystem API operations to simple POSIX-like callbacks.\n * Uses callbackContext for dynamic callback ID lookup to support runtime reuse.\n */\nexport function createCallbackFileSystemHandler(\n options: CallbackFsHandlerOptions\n): FileSystemHandler {\n const {\n connection,\n callbackContext,\n invokeClientCallback,\n basePath = \"\",\n getSignal,\n } = options;\n\n const resolvePath = (path: string): string => {\n // Remove leading slash from the path\n const cleanPath = path.startsWith(\"/\") ? path.slice(1) : path;\n // Handle root case\n if (!basePath || basePath === \"/\") {\n return cleanPath ? `/${cleanPath}` : \"/\";\n }\n // Remove trailing slash from basePath\n const cleanBase = basePath.endsWith(\"/\") ? basePath.slice(0, -1) : basePath;\n return cleanPath ? `${cleanBase}/${cleanPath}` : cleanBase;\n };\n\n // Helper to get current callback ID (supports runtime reuse)\n const getCallbackId = (name: keyof CallbackContext[\"fs\"]): number | undefined => {\n return callbackContext.fs[name];\n };\n\n // Helper to get current connection (supports runtime reuse and reconnection)\n const getConnection = async (): Promise<ConnectionState> => {\n if (callbackContext.connection) {\n return callbackContext.connection;\n }\n if (callbackContext.reconnectionPromise) {\n return callbackContext.reconnectionPromise.promise;\n }\n // Fall back to the originally captured connection\n return connection;\n };\n\n const invokeFsCallback = async (\n conn: ConnectionState,\n callbackId: number,\n args: unknown[],\n ): Promise<unknown> => {\n return invokeClientCallback(conn, callbackId, args, {\n signal: getSignal?.(),\n });\n };\n\n return {\n async getFileHandle(path: string, opts?: { create?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n if (opts?.create) {\n // Ensure file exists by writing empty content if it doesn't exist\n const writeFileId = getCallbackId(\"writeFile\");\n if (writeFileId !== undefined) {\n try {\n // Check if file exists first\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n await invokeFsCallback(conn, statId, [fullPath]);\n // File exists, nothing to do\n return;\n } catch {\n // File doesn't exist, create it\n }\n }\n // Create empty file\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n new Uint8Array(0),\n ]);\n } catch (err) {\n const error = err as Error;\n throw new Error(`[NotFoundError]${error.message}`);\n }\n }\n return;\n }\n\n // Check file exists\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isFile: boolean };\n if (!result.isFile) {\n throw new Error(`[TypeMismatchError]Not a file: ${fullPath}`);\n }\n } catch (err) {\n const error = err as Error;\n if (error.message.includes(\"TypeMismatchError\")) throw error;\n throw new Error(`[NotFoundError]File not found: ${fullPath}`);\n }\n }\n },\n\n async getDirectoryHandle(path: string, opts?: { create?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n if (opts?.create) {\n const mkdirId = getCallbackId(\"mkdir\");\n if (mkdirId !== undefined) {\n try {\n await invokeFsCallback(conn, mkdirId, [\n fullPath,\n { recursive: true },\n ]);\n } catch {\n // Ignore error if directory already exists\n }\n }\n return;\n }\n\n // Check directory exists\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isDirectory: boolean };\n if (!result.isDirectory) {\n throw new Error(`[TypeMismatchError]Not a directory: ${fullPath}`);\n }\n } catch (err) {\n const error = err as Error;\n if (error.message.includes(\"TypeMismatchError\")) throw error;\n throw new Error(`[NotFoundError]Directory not found: ${fullPath}`);\n }\n }\n },\n\n async removeEntry(path: string, opts?: { recursive?: boolean }): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n // Check if it's a file or directory\n let isFile = true;\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const result = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { isFile: boolean; isDirectory: boolean };\n isFile = result.isFile;\n } catch {\n throw new Error(`[NotFoundError]Entry not found: ${fullPath}`);\n }\n }\n\n if (isFile) {\n const unlinkId = getCallbackId(\"unlink\");\n if (unlinkId === undefined) {\n throw new Error(`[NotAllowedError]File deletion not supported`);\n }\n await invokeFsCallback(conn, unlinkId, [fullPath]);\n } else {\n const rmdirId = getCallbackId(\"rmdir\");\n if (rmdirId === undefined) {\n throw new Error(`[NotAllowedError]Directory deletion not supported`);\n }\n // Note: recursive option may need special handling\n await invokeFsCallback(conn, rmdirId, [fullPath]);\n }\n },\n\n async readDirectory(\n path: string\n ): Promise<Array<{ name: string; kind: \"file\" | \"directory\" }>> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readdirId = getCallbackId(\"readdir\");\n if (readdirId === undefined) {\n throw new Error(`[NotAllowedError]Directory reading not supported`);\n }\n\n const entries = (await invokeFsCallback(conn, readdirId, [\n fullPath,\n ])) as string[];\n\n // We need to stat each entry to determine if it's a file or directory\n const result: Array<{ name: string; kind: \"file\" | \"directory\" }> = [];\n\n const statId = getCallbackId(\"stat\");\n for (const name of entries) {\n const entryPath = fullPath ? `${fullPath}/${name}` : name;\n let kind: \"file\" | \"directory\" = \"file\";\n\n if (statId !== undefined) {\n try {\n const stat = (await invokeFsCallback(conn, statId, [\n entryPath,\n ])) as { isFile: boolean; isDirectory: boolean };\n kind = stat.isDirectory ? \"directory\" : \"file\";\n } catch {\n // Default to file if stat fails\n }\n }\n\n result.push({ name, kind });\n }\n\n return result;\n },\n\n async readFile(\n path: string\n ): Promise<{ data: Uint8Array; size: number; lastModified: number; type: string }> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readFileId = getCallbackId(\"readFile\");\n if (readFileId === undefined) {\n throw new Error(`[NotAllowedError]File reading not supported`);\n }\n\n const data = (await invokeFsCallback(conn, readFileId, [\n fullPath,\n ])) as Uint8Array | ArrayBuffer | number[];\n\n // Convert to Uint8Array if needed\n let bytes: Uint8Array;\n if (data instanceof Uint8Array) {\n bytes = data;\n } else if (Array.isArray(data)) {\n bytes = new Uint8Array(data);\n } else if (data instanceof ArrayBuffer) {\n bytes = new Uint8Array(data);\n } else {\n bytes = new Uint8Array(0);\n }\n\n // Get metadata if stat is available\n let size = bytes.length;\n let lastModified = Date.now();\n\n const statId = getCallbackId(\"stat\");\n if (statId !== undefined) {\n try {\n const stat = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { size: number; lastModified?: number };\n size = stat.size;\n if (stat.lastModified) {\n lastModified = stat.lastModified;\n }\n } catch {\n // Use byte length as fallback\n }\n }\n\n // Determine MIME type from extension\n const ext = path.split(\".\").pop()?.toLowerCase() || \"\";\n const type = MIME_TYPES[ext] || \"application/octet-stream\";\n\n return { data: bytes, size, lastModified, type };\n },\n\n async writeFile(path: string, data: Uint8Array, position?: number): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const writeFileId = getCallbackId(\"writeFile\");\n if (writeFileId === undefined) {\n throw new Error(`[NotAllowedError]File writing not supported`);\n }\n\n // Note: position parameter for partial writes may need special handling\n // Simple implementation overwrites entire file\n if (position !== undefined && position > 0) {\n // For positional writes, we need to read existing content and merge\n const readFileId = getCallbackId(\"readFile\");\n if (readFileId !== undefined) {\n try {\n const existing = (await invokeFsCallback(\n conn,\n readFileId,\n [fullPath]\n )) as Uint8Array | ArrayBuffer | number[];\n\n let existingBytes: Uint8Array;\n if (existing instanceof Uint8Array) {\n existingBytes = existing;\n } else if (Array.isArray(existing)) {\n existingBytes = new Uint8Array(existing);\n } else if (existing instanceof ArrayBuffer) {\n existingBytes = new Uint8Array(existing);\n } else {\n existingBytes = new Uint8Array(0);\n }\n\n // Create merged buffer\n const newSize = Math.max(existingBytes.length, position + data.length);\n const merged = new Uint8Array(newSize);\n merged.set(existingBytes);\n merged.set(data, position);\n\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n merged,\n ]);\n return;\n } catch {\n // File doesn't exist, create new one at position\n const newData = new Uint8Array(position + data.length);\n newData.set(data, position);\n await invokeFsCallback(conn, writeFileId, [\n fullPath,\n newData,\n ]);\n return;\n }\n }\n }\n\n await invokeFsCallback(conn, writeFileId, [fullPath, data]);\n },\n\n async truncateFile(path: string, size: number): Promise<void> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const readFileId = getCallbackId(\"readFile\");\n const writeFileId = getCallbackId(\"writeFile\");\n if (readFileId === undefined || writeFileId === undefined) {\n throw new Error(`[NotAllowedError]File truncation not supported`);\n }\n\n // Read existing content\n const existing = (await invokeFsCallback(conn, readFileId, [\n fullPath,\n ])) as Uint8Array | ArrayBuffer | number[];\n\n let existingBytes: Uint8Array;\n if (existing instanceof Uint8Array) {\n existingBytes = existing;\n } else if (Array.isArray(existing)) {\n existingBytes = new Uint8Array(existing);\n } else if (existing instanceof ArrayBuffer) {\n existingBytes = new Uint8Array(existing);\n } else {\n existingBytes = new Uint8Array(0);\n }\n\n // Create truncated buffer\n const truncated = new Uint8Array(size);\n truncated.set(existingBytes.slice(0, size));\n\n await invokeFsCallback(conn, writeFileId, [fullPath, truncated]);\n },\n\n async getFileMetadata(\n path: string\n ): Promise<{ size: number; lastModified: number; type: string }> {\n const fullPath = resolvePath(path);\n const conn = await getConnection();\n\n const statId = getCallbackId(\"stat\");\n if (statId === undefined) {\n throw new Error(`[NotAllowedError]File stat not supported`);\n }\n\n const stat = (await invokeFsCallback(conn, statId, [\n fullPath,\n ])) as { size: number; lastModified?: number; isFile: boolean };\n\n if (!stat.isFile) {\n throw new Error(`[TypeMismatchError]Not a file: ${fullPath}`);\n }\n\n // Determine MIME type from extension\n const ext = path.split(\".\").pop()?.toLowerCase() || \"\";\n const type = MIME_TYPES[ext] || \"application/octet-stream\";\n\n return {\n size: stat.size,\n lastModified: stat.lastModified ?? Date.now(),\n type,\n };\n },\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,IAAM,aAAqC;AAAA,EACzC,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACP;AAyBO,SAAS,+BAA+B,CAC7C,SACmB;AAAA,EACnB;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX;AAAA,MACE;AAAA,EAEJ,MAAM,cAAc,CAAC,SAAyB;AAAA,IAE5C,MAAM,YAAY,KAAK,WAAW,GAAG,IAAI,KAAK,MAAM,CAAC,IAAI;AAAA,IAEzD,IAAI,CAAC,YAAY,aAAa,KAAK;AAAA,MACjC,OAAO,IAAI;AAAA,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,IAAM,aAAqC;AAAA,EACzC,KAAK;AAAA,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,oBAAoB,YAAY;AAAA,QAClC,gBAAgB;AAAA,MAClB,EAAO,SAAI,MAAM,QAAQ,QAAQ,GAAG;AAAA,QAClC,gBAAgB,IAAI,WAAW,QAAQ;AAAA,MACzC,EAAO,SAAI,oBAAoB,aAAa;AAAA,QAC1C,gBAAgB,IAAI,WAAW,QAAQ;AAAA,MACzC,EAAO;AAAA,QACL,gBAAgB,IAAI,WAAW,CAAC;AAAA;AAAA,MAIlC,MAAM,YAAY,IAAI,WAAW,IAAI;AAAA,MACrC,UAAU,IAAI,cAAc,MAAM,GAAG,IAAI,CAAC;AAAA,MAE1C,MAAM,iBAAiB,MAAM,aAAa,CAAC,UAAU,SAAS,CAAC;AAAA;AAAA,SAG3D,gBAAe,CACnB,MAC+D;AAAA,MAC/D,MAAM,WAAW,YAAY,IAAI;AAAA,MACjC,MAAM,OAAO,MAAM,cAAc;AAAA,MAEjC,MAAM,SAAS,cAAc,MAAM;AAAA,MACnC,IAAI,WAAW,WAAW;AAAA,QACxB,MAAM,IAAI,MAAM,0CAA0C;AAAA,MAC5D;AAAA,MAEA,MAAM,OAAQ,MAAM,iBAAiB,MAAM,QAAQ;AAAA,QACjD;AAAA,MACF,CAAC;AAAA,MAED,IAAI,CAAC,KAAK,QAAQ;AAAA,QAChB,MAAM,IAAI,MAAM,kCAAkC,UAAU;AAAA,MAC9D;AAAA,MAGA,MAAM,MAAM,KAAK,MAAM,GAAG,EAAE,IAAI,GAAG,YAAY,KAAK;AAAA,MACpD,MAAM,OAAO,WAAW,QAAQ;AAAA,MAEhC,OAAO;AAAA,QACL,MAAM,KAAK;AAAA,QACX,cAAc,KAAK,gBAAgB,KAAK,IAAI;AAAA,QAC5C;AAAA,MACF;AAAA;AAAA,EAEJ;AAAA;",
|
|
8
|
+
"debugId": "CB9F765E52A1FA1B64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -205,6 +205,9 @@ async function handleMessage(message, connection, state) {
|
|
|
205
205
|
case import_protocol.MessageType.DISPOSE_RUNTIME:
|
|
206
206
|
await handleDisposeRuntime(message, connection, state);
|
|
207
207
|
break;
|
|
208
|
+
case import_protocol.MessageType.DISPOSE_NAMESPACE:
|
|
209
|
+
await handleDisposeNamespace(message, connection, state);
|
|
210
|
+
break;
|
|
208
211
|
case import_protocol.MessageType.EVAL:
|
|
209
212
|
await handleEval(message, connection, state);
|
|
210
213
|
break;
|
|
@@ -358,6 +361,7 @@ async function hardDeleteRuntime(instance, state, reason) {
|
|
|
358
361
|
}
|
|
359
362
|
await instance.runtime.dispose();
|
|
360
363
|
} finally {
|
|
364
|
+
detachRuntimeFromOwningConnection(instance, state);
|
|
361
365
|
state.isolates.delete(instance.isolateId);
|
|
362
366
|
if (instance.namespaceId != null) {
|
|
363
367
|
const indexed = state.namespacedRuntimes.get(instance.namespaceId);
|
|
@@ -380,6 +384,12 @@ async function hardDeleteRuntime(instance, state, reason) {
|
|
|
380
384
|
logRuntimeLifecycle(state, wasPooled ? "evicted pooled" : "hard-disposed", instance, reason);
|
|
381
385
|
}
|
|
382
386
|
}
|
|
387
|
+
function detachRuntimeFromOwningConnection(instance, state) {
|
|
388
|
+
if (!instance.ownerConnection) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
state.connections.get(instance.ownerConnection)?.isolates.delete(instance.isolateId);
|
|
392
|
+
}
|
|
383
393
|
var RECONNECTION_TIMEOUT_MS = 30000;
|
|
384
394
|
function softDeleteRuntime(instance, state, reason) {
|
|
385
395
|
const runtimeAbortWasAborted = instance.runtimeAbortController?.signal.aborted ?? false;
|
|
@@ -542,7 +552,6 @@ function reuseNamespacedRuntime(instance, connection, message, state) {
|
|
|
542
552
|
instance.returnedCallbacks = new Map;
|
|
543
553
|
instance.returnedPromises = new Map;
|
|
544
554
|
instance.returnedIterators = new Map;
|
|
545
|
-
instance.nextLocalCallbackId = 1e6;
|
|
546
555
|
logRuntimeLifecycle(state, "reused pooled", instance);
|
|
547
556
|
logSuspiciousRuntimeOperation(state, "reuse state", instance, connection, {
|
|
548
557
|
disposedForMs,
|
|
@@ -608,14 +617,7 @@ async function handleCreateRuntime(message, connection, state) {
|
|
|
608
617
|
const existing = state.namespacedRuntimes.get(namespaceId);
|
|
609
618
|
if (existing) {
|
|
610
619
|
if (!existing.isDisposed) {
|
|
611
|
-
|
|
612
|
-
sendOk(connection.socket, message.requestId, {
|
|
613
|
-
isolateId: existing.isolateId,
|
|
614
|
-
reused: true
|
|
615
|
-
});
|
|
616
|
-
return;
|
|
617
|
-
}
|
|
618
|
-
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, `Namespace "${namespaceId}" already has an active runtime`);
|
|
620
|
+
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, `Namespace "${namespaceId}" already has an active runtime`, { name: "NamespaceInUseError" });
|
|
619
621
|
return;
|
|
620
622
|
}
|
|
621
623
|
reuseNamespacedRuntime(existing, connection, message, state);
|
|
@@ -626,7 +628,7 @@ async function handleCreateRuntime(message, connection, state) {
|
|
|
626
628
|
return;
|
|
627
629
|
}
|
|
628
630
|
if (state.namespacedCreatesInFlight.has(namespaceId)) {
|
|
629
|
-
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, `Namespace "${namespaceId}" creation already in progress
|
|
631
|
+
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, `Namespace "${namespaceId}" creation already in progress`, { name: "NamespaceInUseError" });
|
|
630
632
|
return;
|
|
631
633
|
}
|
|
632
634
|
state.namespacedCreatesInFlight.add(namespaceId);
|
|
@@ -681,7 +683,6 @@ async function handleCreateRuntime(message, connection, state) {
|
|
|
681
683
|
returnedCallbacks: new Map,
|
|
682
684
|
returnedPromises: new Map,
|
|
683
685
|
returnedIterators: new Map,
|
|
684
|
-
nextLocalCallbackId: 1e6,
|
|
685
686
|
namespaceId,
|
|
686
687
|
isDisposed: false,
|
|
687
688
|
isPoisoned: false,
|
|
@@ -918,6 +919,7 @@ async function handleCreateRuntime(message, connection, state) {
|
|
|
918
919
|
return { ok: false, error: { name: error.name, message: error.message } };
|
|
919
920
|
}
|
|
920
921
|
},
|
|
922
|
+
hasDefaultPage: message.options.playwright?.hasDefaultPage,
|
|
921
923
|
timeout: message.options.playwright?.timeout,
|
|
922
924
|
console: playwrightCallbacks.console,
|
|
923
925
|
onEvent: (event) => {
|
|
@@ -1190,6 +1192,21 @@ async function handleDisposeRuntime(message, connection, state) {
|
|
|
1190
1192
|
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
|
|
1191
1193
|
}
|
|
1192
1194
|
}
|
|
1195
|
+
async function handleDisposeNamespace(message, connection, state) {
|
|
1196
|
+
const instance = state.namespacedRuntimes.get(message.namespaceId);
|
|
1197
|
+
if (!instance) {
|
|
1198
|
+
sendOk(connection.socket, message.requestId);
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
const requestReason = typeof message.reason === "string" && message.reason.length > 0 ? message.reason : "client requested namespace dispose";
|
|
1202
|
+
try {
|
|
1203
|
+
await hardDeleteRuntime(instance, state, requestReason);
|
|
1204
|
+
sendOk(connection.socket, message.requestId);
|
|
1205
|
+
} catch (err) {
|
|
1206
|
+
const error = err;
|
|
1207
|
+
sendError(connection.socket, message.requestId, import_protocol.ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1193
1210
|
async function handleEval(message, connection, state) {
|
|
1194
1211
|
const instance = state.isolates.get(message.isolateId);
|
|
1195
1212
|
if (!instance) {
|
|
@@ -1599,10 +1616,136 @@ async function handleCallbackInvoke(message, connection, state) {
|
|
|
1599
1616
|
return;
|
|
1600
1617
|
}
|
|
1601
1618
|
try {
|
|
1602
|
-
|
|
1619
|
+
const allocateLocalCallbackId = () => {
|
|
1620
|
+
if (!instance.ownerConnection) {
|
|
1621
|
+
throw new Error("Cannot register callback without an active client connection");
|
|
1622
|
+
}
|
|
1623
|
+
return connection.nextLocalCallbackId++;
|
|
1624
|
+
};
|
|
1625
|
+
const createMarshalContext = () => ({
|
|
1626
|
+
registerCallback: (fn) => {
|
|
1627
|
+
const callbackId = allocateLocalCallbackId();
|
|
1628
|
+
instance.returnedCallbacks.set(callbackId, fn);
|
|
1629
|
+
return callbackId;
|
|
1630
|
+
},
|
|
1631
|
+
registerPromise: (promise) => {
|
|
1632
|
+
const promiseId = allocateLocalCallbackId();
|
|
1633
|
+
instance.returnedPromises.set(promiseId, promise);
|
|
1634
|
+
return promiseId;
|
|
1635
|
+
},
|
|
1636
|
+
registerIterator: (iterator) => {
|
|
1637
|
+
const iteratorId = allocateLocalCallbackId();
|
|
1638
|
+
instance.returnedIterators.set(iteratorId, iterator);
|
|
1639
|
+
return iteratorId;
|
|
1640
|
+
}
|
|
1641
|
+
});
|
|
1642
|
+
const addCallbackIdsToRefs = (value) => {
|
|
1643
|
+
if (value === null || typeof value !== "object") {
|
|
1644
|
+
return value;
|
|
1645
|
+
}
|
|
1646
|
+
if (import_protocol.isPromiseRef(value)) {
|
|
1647
|
+
if ("__resolveCallbackId" in value) {
|
|
1648
|
+
return value;
|
|
1649
|
+
}
|
|
1650
|
+
const resolveCallbackId = allocateLocalCallbackId();
|
|
1651
|
+
instance.returnedCallbacks.set(resolveCallbackId, async (promiseId) => {
|
|
1652
|
+
const promise = instance.returnedPromises.get(promiseId);
|
|
1653
|
+
if (!promise) {
|
|
1654
|
+
throw new Error(`Promise ${promiseId} not found`);
|
|
1655
|
+
}
|
|
1656
|
+
const result2 = await promise;
|
|
1657
|
+
instance.returnedPromises.delete(promiseId);
|
|
1658
|
+
const ctx = createMarshalContext();
|
|
1659
|
+
const marshalled = await import_protocol.marshalValue(result2, ctx);
|
|
1660
|
+
return addCallbackIdsToRefs(marshalled);
|
|
1661
|
+
});
|
|
1662
|
+
return { ...value, __resolveCallbackId: resolveCallbackId };
|
|
1663
|
+
}
|
|
1664
|
+
if (import_protocol.isAsyncIteratorRef(value)) {
|
|
1665
|
+
if ("__nextCallbackId" in value) {
|
|
1666
|
+
return value;
|
|
1667
|
+
}
|
|
1668
|
+
const nextCallbackId = allocateLocalCallbackId();
|
|
1669
|
+
instance.returnedCallbacks.set(nextCallbackId, async (iteratorId) => {
|
|
1670
|
+
const iterator = instance.returnedIterators.get(iteratorId);
|
|
1671
|
+
if (!iterator) {
|
|
1672
|
+
throw new Error(`Iterator ${iteratorId} not found`);
|
|
1673
|
+
}
|
|
1674
|
+
const result2 = await iterator.next();
|
|
1675
|
+
if (result2.done) {
|
|
1676
|
+
instance.returnedIterators.delete(iteratorId);
|
|
1677
|
+
}
|
|
1678
|
+
const ctx = createMarshalContext();
|
|
1679
|
+
const marshalledValue = await import_protocol.marshalValue(result2.value, ctx);
|
|
1680
|
+
return {
|
|
1681
|
+
done: result2.done,
|
|
1682
|
+
value: addCallbackIdsToRefs(marshalledValue)
|
|
1683
|
+
};
|
|
1684
|
+
});
|
|
1685
|
+
const returnCallbackId = allocateLocalCallbackId();
|
|
1686
|
+
instance.returnedCallbacks.set(returnCallbackId, async (iteratorId, returnValue) => {
|
|
1687
|
+
const iterator = instance.returnedIterators.get(iteratorId);
|
|
1688
|
+
instance.returnedIterators.delete(iteratorId);
|
|
1689
|
+
if (!iterator || !iterator.return) {
|
|
1690
|
+
return { done: true, value: undefined };
|
|
1691
|
+
}
|
|
1692
|
+
const result2 = await iterator.return(returnValue);
|
|
1693
|
+
const ctx = createMarshalContext();
|
|
1694
|
+
const marshalledValue = await import_protocol.marshalValue(result2.value, ctx);
|
|
1695
|
+
return {
|
|
1696
|
+
done: true,
|
|
1697
|
+
value: addCallbackIdsToRefs(marshalledValue)
|
|
1698
|
+
};
|
|
1699
|
+
});
|
|
1700
|
+
const throwCallbackId = allocateLocalCallbackId();
|
|
1701
|
+
instance.returnedCallbacks.set(throwCallbackId, async (iteratorId, errorValue) => {
|
|
1702
|
+
const iterator = instance.returnedIterators.get(iteratorId);
|
|
1703
|
+
if (!iterator) {
|
|
1704
|
+
throw new Error(`Iterator ${iteratorId} not found`);
|
|
1705
|
+
}
|
|
1706
|
+
try {
|
|
1707
|
+
if (!iterator.throw) {
|
|
1708
|
+
throw Object.assign(new Error(errorValue?.message ?? "Iterator does not support throw()"), { name: errorValue?.name ?? "Error", stack: errorValue?.stack });
|
|
1709
|
+
}
|
|
1710
|
+
const thrownError = Object.assign(new Error(errorValue?.message ?? "Iterator throw()"), { name: errorValue?.name ?? "Error", stack: errorValue?.stack });
|
|
1711
|
+
const result2 = await iterator.throw(thrownError);
|
|
1712
|
+
if (result2.done) {
|
|
1713
|
+
instance.returnedIterators.delete(iteratorId);
|
|
1714
|
+
}
|
|
1715
|
+
const ctx = createMarshalContext();
|
|
1716
|
+
const marshalledValue = await import_protocol.marshalValue(result2.value, ctx);
|
|
1717
|
+
return {
|
|
1718
|
+
done: result2.done,
|
|
1719
|
+
value: addCallbackIdsToRefs(marshalledValue)
|
|
1720
|
+
};
|
|
1721
|
+
} catch (error) {
|
|
1722
|
+
instance.returnedIterators.delete(iteratorId);
|
|
1723
|
+
throw error;
|
|
1724
|
+
}
|
|
1725
|
+
});
|
|
1726
|
+
return {
|
|
1727
|
+
...value,
|
|
1728
|
+
__nextCallbackId: nextCallbackId,
|
|
1729
|
+
__returnCallbackId: returnCallbackId,
|
|
1730
|
+
__throwCallbackId: throwCallbackId
|
|
1731
|
+
};
|
|
1732
|
+
}
|
|
1733
|
+
if (Array.isArray(value)) {
|
|
1734
|
+
return value.map((item) => addCallbackIdsToRefs(item));
|
|
1735
|
+
}
|
|
1736
|
+
const result = {};
|
|
1737
|
+
for (const key of Object.keys(value)) {
|
|
1738
|
+
result[key] = addCallbackIdsToRefs(value[key]);
|
|
1739
|
+
}
|
|
1740
|
+
return result;
|
|
1741
|
+
};
|
|
1742
|
+
const rawResult = await requestContextStorage.run({
|
|
1603
1743
|
...message.context ?? {},
|
|
1604
1744
|
signal: getCurrentHostSignal(instance)
|
|
1605
1745
|
}, async () => await callback(...message.args));
|
|
1746
|
+
const marshalContext = createMarshalContext();
|
|
1747
|
+
const marshalledResult = await import_protocol.marshalValue(rawResult, marshalContext);
|
|
1748
|
+
response.result = addCallbackIdsToRefs(marshalledResult);
|
|
1606
1749
|
} catch (err) {
|
|
1607
1750
|
const error = err;
|
|
1608
1751
|
response.error = {
|
|
@@ -2108,4 +2251,4 @@ async function handleClearCollectedData(message, connection, state) {
|
|
|
2108
2251
|
}
|
|
2109
2252
|
}
|
|
2110
2253
|
|
|
2111
|
-
//# debugId=
|
|
2254
|
+
//# debugId=B94406A814BB763964756E2164756E21
|