@ricsam/quickjs-test-utils 1.0.17 → 1.0.18
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 +4 -2
- package/dist/cjs/integration-server.cjs +5 -1
- package/dist/cjs/integration-server.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/mjs/integration-server.mjs +5 -1
- package/dist/mjs/integration-server.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/types/integration-server.d.ts +4 -0
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -73,7 +73,8 @@ import {
|
|
|
73
73
|
CORE_TYPES, // ReadableStream, Blob, File, URL, etc.
|
|
74
74
|
FETCH_TYPES, // fetch, Request, Response, serve, etc.
|
|
75
75
|
FS_TYPES, // fs.getDirectory, FileSystemHandle, etc.
|
|
76
|
-
|
|
76
|
+
CRYPTO_TYPES, // crypto.subtle, CryptoKey, etc.
|
|
77
|
+
TYPE_DEFINITIONS // All types as { core, fetch, fs, crypto, ... }
|
|
77
78
|
} from "@ricsam/quickjs-test-utils";
|
|
78
79
|
|
|
79
80
|
// Use with your own ts-morph project
|
|
@@ -93,7 +94,8 @@ Each package also exports `.d.ts` files for use with `tsconfig.json`:
|
|
|
93
94
|
"references": [
|
|
94
95
|
{ "path": "./node_modules/@ricsam/quickjs-core/src/quickjs.d.ts" },
|
|
95
96
|
{ "path": "./node_modules/@ricsam/quickjs-fetch/src/quickjs.d.ts" },
|
|
96
|
-
{ "path": "./node_modules/@ricsam/quickjs-fs/src/quickjs.d.ts" }
|
|
97
|
+
{ "path": "./node_modules/@ricsam/quickjs-fs/src/quickjs.d.ts" },
|
|
98
|
+
{ "path": "./node_modules/@ricsam/quickjs-crypto/src/quickjs.d.ts" }
|
|
97
99
|
]
|
|
98
100
|
}
|
|
99
101
|
```
|
|
@@ -36,6 +36,7 @@ module.exports = __toCommonJS(exports_integration_server);
|
|
|
36
36
|
var import_quickjs_emscripten = require("quickjs-emscripten");
|
|
37
37
|
var import_quickjs_fetch = require("@ricsam/quickjs-fetch");
|
|
38
38
|
var import_quickjs_core = require("@ricsam/quickjs-core");
|
|
39
|
+
var import_quickjs_console = require("@ricsam/quickjs-console");
|
|
39
40
|
async function startIntegrationServer(quickJSCode, options) {
|
|
40
41
|
const QuickJS = await import_quickjs_emscripten.getQuickJS();
|
|
41
42
|
const runtime = QuickJS.newRuntime();
|
|
@@ -45,6 +46,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
45
46
|
coreHandle,
|
|
46
47
|
onFetch: options?.onFetch
|
|
47
48
|
});
|
|
49
|
+
const consoleHandle = options?.consoleHandlers ? import_quickjs_console.setupConsole(context, { handlers: options.consoleHandlers, coreHandle }) : undefined;
|
|
48
50
|
const wsConnections = new Map;
|
|
49
51
|
fetchHandle.onWebSocketCommand((cmd) => {
|
|
50
52
|
const ws = wsConnections.get(cmd.connectionId);
|
|
@@ -118,6 +120,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
118
120
|
context,
|
|
119
121
|
runtime,
|
|
120
122
|
coreHandle,
|
|
123
|
+
consoleHandle,
|
|
121
124
|
async stop() {
|
|
122
125
|
for (const ws of wsConnections.values()) {
|
|
123
126
|
ws.close(1000, "Server stopping");
|
|
@@ -125,6 +128,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
125
128
|
wsConnections.clear();
|
|
126
129
|
server.stop(true);
|
|
127
130
|
fetchHandle.dispose();
|
|
131
|
+
consoleHandle?.dispose();
|
|
128
132
|
coreHandle.dispose();
|
|
129
133
|
context.dispose();
|
|
130
134
|
runtime.dispose();
|
|
@@ -133,4 +137,4 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
133
137
|
}
|
|
134
138
|
})
|
|
135
139
|
|
|
136
|
-
//# debugId=
|
|
140
|
+
//# debugId=815B5672C4B716AE64756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/integration-server.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { getQuickJS, type QuickJSContext, type QuickJSRuntime } from \"quickjs-emscripten\";\nimport { setupFetch, type FetchHandle, type WebSocketCommand, type SetupFetchOptions } from \"@ricsam/quickjs-fetch\";\nimport { setupCore, type CoreHandle } from \"@ricsam/quickjs-core\";\nimport type { Server, ServerWebSocket } from \"bun\";\n\ninterface WsData {\n connectionId: string;\n}\n\nexport interface IntegrationTestServer {\n baseURL: string;\n wsURL: string;\n port: number;\n fetchHandle: FetchHandle;\n context: QuickJSContext;\n runtime: QuickJSRuntime;\n coreHandle: CoreHandle;\n stop(): Promise<void>;\n}\n\nexport interface StartIntegrationServerOptions {\n /** Handler for outbound fetch() calls from QuickJS */\n onFetch?: SetupFetchOptions[\"onFetch\"];\n}\n\n/**\n * Start a real HTTP server that dispatches requests to a QuickJS serve() handler.\n *\n * @param quickJSCode - JavaScript code to run in QuickJS, should call serve()\n * @param options - Optional configuration\n * @returns Server info including baseURL, wsURL, and stop function\n *\n * @example\n * const server = await startIntegrationServer(`\n * serve({\n * fetch(request) {\n * return new Response(\"Hello!\");\n * }\n * });\n * `);\n *\n * const response = await fetch(\\`\\${server.baseURL}/test\\`);\n * expect(await response.text()).toBe(\"Hello!\");\n *\n * await server.stop();\n */\nexport async function startIntegrationServer(\n quickJSCode: string,\n options?: StartIntegrationServerOptions\n): Promise<IntegrationTestServer> {\n const QuickJS = await getQuickJS();\n const runtime = QuickJS.newRuntime();\n const context = runtime.newContext();\n\n // Setup core and fetch APIs\n const coreHandle = setupCore(context);\n const fetchHandle = setupFetch(context, {\n coreHandle,\n onFetch: options?.onFetch,\n });\n\n // Track WebSocket connections\n const wsConnections = new Map<string, ServerWebSocket<WsData>>();\n\n // Handle outgoing WebSocket commands from QuickJS\n fetchHandle.onWebSocketCommand((cmd: WebSocketCommand) => {\n const ws = wsConnections.get(cmd.connectionId);\n if (!ws) return;\n\n if (cmd.type === \"message\") {\n ws.send(cmd.data);\n } else if (cmd.type === \"close\") {\n ws.close(cmd.code, cmd.reason);\n }\n });\n\n // Run the user's QuickJS code\n const result = context.evalCode(quickJSCode);\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n throw new Error(`Failed to evaluate QuickJS code: ${JSON.stringify(error)}`);\n }\n result.value.dispose();\n\n // Start Bun server with port 0 for dynamic assignment\n const server: Server<WsData> = Bun.serve<WsData>({\n port: 0,\n async fetch(req, server) {\n try {\n // Dispatch to QuickJS handler\n const response = await fetchHandle.dispatchRequest(req);\n\n // Check if QuickJS requested a WebSocket upgrade\n const upgrade = fetchHandle.getUpgradeRequest();\n if (upgrade?.requested) {\n // The connectionId is generated by QuickJS in server.upgrade()\n // User data is stored in QuickJS registry - we just pass the connectionId\n const success = server.upgrade(req, {\n data: { connectionId: upgrade.connectionId },\n });\n if (success) {\n return undefined;\n }\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\n }\n\n return response;\n } catch (error) {\n console.error(\"Integration test request failed:\", error);\n return new Response(\"Internal Server Error\", { status: 500 });\n }\n },\n websocket: {\n open(ws) {\n const { connectionId } = ws.data;\n wsConnections.set(connectionId, ws);\n // User data is looked up from QuickJS registry using connectionId\n fetchHandle.dispatchWebSocketOpen(connectionId);\n },\n message(ws, message) {\n const msg = typeof message === \"string\" ? message : message.buffer;\n fetchHandle.dispatchWebSocketMessage(ws.data.connectionId, msg as string | ArrayBuffer);\n },\n close(ws, code, reason) {\n fetchHandle.dispatchWebSocketClose(ws.data.connectionId, code, reason);\n wsConnections.delete(ws.data.connectionId);\n },\n },\n });\n\n const port = server.port;\n if (port === undefined) {\n throw new Error(\"Server failed to get port\");\n }\n const baseURL = `http://localhost:${port}`;\n const wsURL = `ws://localhost:${port}`;\n\n return {\n baseURL,\n wsURL,\n port,\n fetchHandle,\n context,\n runtime,\n coreHandle,\n async stop() {\n // Close all WebSocket connections\n for (const ws of wsConnections.values()) {\n ws.close(1000, \"Server stopping\");\n }\n wsConnections.clear();\n\n // Stop the server\n server.stop(true);\n\n // Dispose QuickJS resources\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n },\n };\n}\n"
|
|
5
|
+
"import { getQuickJS, type QuickJSContext, type QuickJSRuntime } from \"quickjs-emscripten\";\nimport { setupFetch, type FetchHandle, type WebSocketCommand, type SetupFetchOptions } from \"@ricsam/quickjs-fetch\";\nimport { setupCore, type CoreHandle } from \"@ricsam/quickjs-core\";\nimport type { Server, ServerWebSocket } from \"bun\";\nimport { setupConsole, type ConsoleOutputHandler, type ConsoleHandle } from \"@ricsam/quickjs-console\";\n\ninterface WsData {\n connectionId: string;\n}\n\nexport interface IntegrationTestServer {\n baseURL: string;\n wsURL: string;\n port: number;\n fetchHandle: FetchHandle;\n context: QuickJSContext;\n runtime: QuickJSRuntime;\n coreHandle: CoreHandle;\n consoleHandle?: ConsoleHandle;\n stop(): Promise<void>;\n}\n\nexport interface StartIntegrationServerOptions {\n /** Handler for outbound fetch() calls from QuickJS */\n onFetch?: SetupFetchOptions[\"onFetch\"];\n /** Handlers for console output from QuickJS */\n consoleHandlers?: ConsoleOutputHandler;\n}\n\n/**\n * Start a real HTTP server that dispatches requests to a QuickJS serve() handler.\n *\n * @param quickJSCode - JavaScript code to run in QuickJS, should call serve()\n * @param options - Optional configuration\n * @returns Server info including baseURL, wsURL, and stop function\n *\n * @example\n * const server = await startIntegrationServer(`\n * serve({\n * fetch(request) {\n * return new Response(\"Hello!\");\n * }\n * });\n * `);\n *\n * const response = await fetch(\\`\\${server.baseURL}/test\\`);\n * expect(await response.text()).toBe(\"Hello!\");\n *\n * await server.stop();\n */\nexport async function startIntegrationServer(\n quickJSCode: string,\n options?: StartIntegrationServerOptions\n): Promise<IntegrationTestServer> {\n const QuickJS = await getQuickJS();\n const runtime = QuickJS.newRuntime();\n const context = runtime.newContext();\n\n // Setup core and fetch APIs\n const coreHandle = setupCore(context);\n const fetchHandle = setupFetch(context, {\n coreHandle,\n onFetch: options?.onFetch,\n });\n\n // Setup console if handlers provided\n const consoleHandle = options?.consoleHandlers\n ? setupConsole(context, { handlers: options.consoleHandlers, coreHandle })\n : undefined;\n\n // Track WebSocket connections\n const wsConnections = new Map<string, ServerWebSocket<WsData>>();\n\n // Handle outgoing WebSocket commands from QuickJS\n fetchHandle.onWebSocketCommand((cmd: WebSocketCommand) => {\n const ws = wsConnections.get(cmd.connectionId);\n if (!ws) return;\n\n if (cmd.type === \"message\") {\n ws.send(cmd.data);\n } else if (cmd.type === \"close\") {\n ws.close(cmd.code, cmd.reason);\n }\n });\n\n // Run the user's QuickJS code\n const result = context.evalCode(quickJSCode);\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n throw new Error(`Failed to evaluate QuickJS code: ${JSON.stringify(error)}`);\n }\n result.value.dispose();\n\n // Start Bun server with port 0 for dynamic assignment\n const server: Server<WsData> = Bun.serve<WsData>({\n port: 0,\n async fetch(req, server) {\n try {\n // Dispatch to QuickJS handler\n const response = await fetchHandle.dispatchRequest(req);\n\n // Check if QuickJS requested a WebSocket upgrade\n const upgrade = fetchHandle.getUpgradeRequest();\n if (upgrade?.requested) {\n // The connectionId is generated by QuickJS in server.upgrade()\n // User data is stored in QuickJS registry - we just pass the connectionId\n const success = server.upgrade(req, {\n data: { connectionId: upgrade.connectionId },\n });\n if (success) {\n return undefined;\n }\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\n }\n\n return response;\n } catch (error) {\n console.error(\"Integration test request failed:\", error);\n return new Response(\"Internal Server Error\", { status: 500 });\n }\n },\n websocket: {\n open(ws) {\n const { connectionId } = ws.data;\n wsConnections.set(connectionId, ws);\n // User data is looked up from QuickJS registry using connectionId\n fetchHandle.dispatchWebSocketOpen(connectionId);\n },\n message(ws, message) {\n const msg = typeof message === \"string\" ? message : message.buffer;\n fetchHandle.dispatchWebSocketMessage(ws.data.connectionId, msg as string | ArrayBuffer);\n },\n close(ws, code, reason) {\n fetchHandle.dispatchWebSocketClose(ws.data.connectionId, code, reason);\n wsConnections.delete(ws.data.connectionId);\n },\n },\n });\n\n const port = server.port;\n if (port === undefined) {\n throw new Error(\"Server failed to get port\");\n }\n const baseURL = `http://localhost:${port}`;\n const wsURL = `ws://localhost:${port}`;\n\n return {\n baseURL,\n wsURL,\n port,\n fetchHandle,\n context,\n runtime,\n coreHandle,\n consoleHandle,\n async stop() {\n // Close all WebSocket connections\n for (const ws of wsConnections.values()) {\n ws.close(1000, \"Server stopping\");\n }\n wsConnections.clear();\n\n // Stop the server\n server.stop(true);\n\n // Dispose QuickJS resources\n fetchHandle.dispose();\n consoleHandle?.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n },\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAqE,IAArE;AAC4F,IAA5F;AAC2C,IAA3C;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAqE,IAArE;AAC4F,IAA5F;AAC2C,IAA3C;AAE4E,IAA5E;AA8CA,eAAsB,sBAAsB,CAC1C,aACA,SACgC;AAAA,EAChC,MAAM,UAAU,MAAM,qCAAW;AAAA,EACjC,MAAM,UAAU,QAAQ,WAAW;AAAA,EACnC,MAAM,UAAU,QAAQ,WAAW;AAAA,EAGnC,MAAM,aAAa,8BAAU,OAAO;AAAA,EACpC,MAAM,cAAc,gCAAW,SAAS;AAAA,IACtC;AAAA,IACA,SAAS,SAAS;AAAA,EACpB,CAAC;AAAA,EAGD,MAAM,gBAAgB,SAAS,kBAC3B,oCAAa,SAAS,EAAE,UAAU,QAAQ,iBAAiB,WAAW,CAAC,IACvE;AAAA,EAGJ,MAAM,gBAAgB,IAAI;AAAA,EAG1B,YAAY,mBAAmB,CAAC,QAA0B;AA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 @@
|
|
|
3
3
|
import { getQuickJS } from "quickjs-emscripten";
|
|
4
4
|
import { setupFetch } from "@ricsam/quickjs-fetch";
|
|
5
5
|
import { setupCore } from "@ricsam/quickjs-core";
|
|
6
|
+
import { setupConsole } from "@ricsam/quickjs-console";
|
|
6
7
|
async function startIntegrationServer(quickJSCode, options) {
|
|
7
8
|
const QuickJS = await getQuickJS();
|
|
8
9
|
const runtime = QuickJS.newRuntime();
|
|
@@ -12,6 +13,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
12
13
|
coreHandle,
|
|
13
14
|
onFetch: options?.onFetch
|
|
14
15
|
});
|
|
16
|
+
const consoleHandle = options?.consoleHandlers ? setupConsole(context, { handlers: options.consoleHandlers, coreHandle }) : undefined;
|
|
15
17
|
const wsConnections = new Map;
|
|
16
18
|
fetchHandle.onWebSocketCommand((cmd) => {
|
|
17
19
|
const ws = wsConnections.get(cmd.connectionId);
|
|
@@ -85,6 +87,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
85
87
|
context,
|
|
86
88
|
runtime,
|
|
87
89
|
coreHandle,
|
|
90
|
+
consoleHandle,
|
|
88
91
|
async stop() {
|
|
89
92
|
for (const ws of wsConnections.values()) {
|
|
90
93
|
ws.close(1000, "Server stopping");
|
|
@@ -92,6 +95,7 @@ async function startIntegrationServer(quickJSCode, options) {
|
|
|
92
95
|
wsConnections.clear();
|
|
93
96
|
server.stop(true);
|
|
94
97
|
fetchHandle.dispose();
|
|
98
|
+
consoleHandle?.dispose();
|
|
95
99
|
coreHandle.dispose();
|
|
96
100
|
context.dispose();
|
|
97
101
|
runtime.dispose();
|
|
@@ -102,4 +106,4 @@ export {
|
|
|
102
106
|
startIntegrationServer
|
|
103
107
|
};
|
|
104
108
|
|
|
105
|
-
//# debugId=
|
|
109
|
+
//# debugId=97F49DC31D81D1FD64756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/integration-server.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { getQuickJS, type QuickJSContext, type QuickJSRuntime } from \"quickjs-emscripten\";\nimport { setupFetch, type FetchHandle, type WebSocketCommand, type SetupFetchOptions } from \"@ricsam/quickjs-fetch\";\nimport { setupCore, type CoreHandle } from \"@ricsam/quickjs-core\";\nimport type { Server, ServerWebSocket } from \"bun\";\n\ninterface WsData {\n connectionId: string;\n}\n\nexport interface IntegrationTestServer {\n baseURL: string;\n wsURL: string;\n port: number;\n fetchHandle: FetchHandle;\n context: QuickJSContext;\n runtime: QuickJSRuntime;\n coreHandle: CoreHandle;\n stop(): Promise<void>;\n}\n\nexport interface StartIntegrationServerOptions {\n /** Handler for outbound fetch() calls from QuickJS */\n onFetch?: SetupFetchOptions[\"onFetch\"];\n}\n\n/**\n * Start a real HTTP server that dispatches requests to a QuickJS serve() handler.\n *\n * @param quickJSCode - JavaScript code to run in QuickJS, should call serve()\n * @param options - Optional configuration\n * @returns Server info including baseURL, wsURL, and stop function\n *\n * @example\n * const server = await startIntegrationServer(`\n * serve({\n * fetch(request) {\n * return new Response(\"Hello!\");\n * }\n * });\n * `);\n *\n * const response = await fetch(\\`\\${server.baseURL}/test\\`);\n * expect(await response.text()).toBe(\"Hello!\");\n *\n * await server.stop();\n */\nexport async function startIntegrationServer(\n quickJSCode: string,\n options?: StartIntegrationServerOptions\n): Promise<IntegrationTestServer> {\n const QuickJS = await getQuickJS();\n const runtime = QuickJS.newRuntime();\n const context = runtime.newContext();\n\n // Setup core and fetch APIs\n const coreHandle = setupCore(context);\n const fetchHandle = setupFetch(context, {\n coreHandle,\n onFetch: options?.onFetch,\n });\n\n // Track WebSocket connections\n const wsConnections = new Map<string, ServerWebSocket<WsData>>();\n\n // Handle outgoing WebSocket commands from QuickJS\n fetchHandle.onWebSocketCommand((cmd: WebSocketCommand) => {\n const ws = wsConnections.get(cmd.connectionId);\n if (!ws) return;\n\n if (cmd.type === \"message\") {\n ws.send(cmd.data);\n } else if (cmd.type === \"close\") {\n ws.close(cmd.code, cmd.reason);\n }\n });\n\n // Run the user's QuickJS code\n const result = context.evalCode(quickJSCode);\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n throw new Error(`Failed to evaluate QuickJS code: ${JSON.stringify(error)}`);\n }\n result.value.dispose();\n\n // Start Bun server with port 0 for dynamic assignment\n const server: Server<WsData> = Bun.serve<WsData>({\n port: 0,\n async fetch(req, server) {\n try {\n // Dispatch to QuickJS handler\n const response = await fetchHandle.dispatchRequest(req);\n\n // Check if QuickJS requested a WebSocket upgrade\n const upgrade = fetchHandle.getUpgradeRequest();\n if (upgrade?.requested) {\n // The connectionId is generated by QuickJS in server.upgrade()\n // User data is stored in QuickJS registry - we just pass the connectionId\n const success = server.upgrade(req, {\n data: { connectionId: upgrade.connectionId },\n });\n if (success) {\n return undefined;\n }\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\n }\n\n return response;\n } catch (error) {\n console.error(\"Integration test request failed:\", error);\n return new Response(\"Internal Server Error\", { status: 500 });\n }\n },\n websocket: {\n open(ws) {\n const { connectionId } = ws.data;\n wsConnections.set(connectionId, ws);\n // User data is looked up from QuickJS registry using connectionId\n fetchHandle.dispatchWebSocketOpen(connectionId);\n },\n message(ws, message) {\n const msg = typeof message === \"string\" ? message : message.buffer;\n fetchHandle.dispatchWebSocketMessage(ws.data.connectionId, msg as string | ArrayBuffer);\n },\n close(ws, code, reason) {\n fetchHandle.dispatchWebSocketClose(ws.data.connectionId, code, reason);\n wsConnections.delete(ws.data.connectionId);\n },\n },\n });\n\n const port = server.port;\n if (port === undefined) {\n throw new Error(\"Server failed to get port\");\n }\n const baseURL = `http://localhost:${port}`;\n const wsURL = `ws://localhost:${port}`;\n\n return {\n baseURL,\n wsURL,\n port,\n fetchHandle,\n context,\n runtime,\n coreHandle,\n async stop() {\n // Close all WebSocket connections\n for (const ws of wsConnections.values()) {\n ws.close(1000, \"Server stopping\");\n }\n wsConnections.clear();\n\n // Stop the server\n server.stop(true);\n\n // Dispose QuickJS resources\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n },\n };\n}\n"
|
|
5
|
+
"import { getQuickJS, type QuickJSContext, type QuickJSRuntime } from \"quickjs-emscripten\";\nimport { setupFetch, type FetchHandle, type WebSocketCommand, type SetupFetchOptions } from \"@ricsam/quickjs-fetch\";\nimport { setupCore, type CoreHandle } from \"@ricsam/quickjs-core\";\nimport type { Server, ServerWebSocket } from \"bun\";\nimport { setupConsole, type ConsoleOutputHandler, type ConsoleHandle } from \"@ricsam/quickjs-console\";\n\ninterface WsData {\n connectionId: string;\n}\n\nexport interface IntegrationTestServer {\n baseURL: string;\n wsURL: string;\n port: number;\n fetchHandle: FetchHandle;\n context: QuickJSContext;\n runtime: QuickJSRuntime;\n coreHandle: CoreHandle;\n consoleHandle?: ConsoleHandle;\n stop(): Promise<void>;\n}\n\nexport interface StartIntegrationServerOptions {\n /** Handler for outbound fetch() calls from QuickJS */\n onFetch?: SetupFetchOptions[\"onFetch\"];\n /** Handlers for console output from QuickJS */\n consoleHandlers?: ConsoleOutputHandler;\n}\n\n/**\n * Start a real HTTP server that dispatches requests to a QuickJS serve() handler.\n *\n * @param quickJSCode - JavaScript code to run in QuickJS, should call serve()\n * @param options - Optional configuration\n * @returns Server info including baseURL, wsURL, and stop function\n *\n * @example\n * const server = await startIntegrationServer(`\n * serve({\n * fetch(request) {\n * return new Response(\"Hello!\");\n * }\n * });\n * `);\n *\n * const response = await fetch(\\`\\${server.baseURL}/test\\`);\n * expect(await response.text()).toBe(\"Hello!\");\n *\n * await server.stop();\n */\nexport async function startIntegrationServer(\n quickJSCode: string,\n options?: StartIntegrationServerOptions\n): Promise<IntegrationTestServer> {\n const QuickJS = await getQuickJS();\n const runtime = QuickJS.newRuntime();\n const context = runtime.newContext();\n\n // Setup core and fetch APIs\n const coreHandle = setupCore(context);\n const fetchHandle = setupFetch(context, {\n coreHandle,\n onFetch: options?.onFetch,\n });\n\n // Setup console if handlers provided\n const consoleHandle = options?.consoleHandlers\n ? setupConsole(context, { handlers: options.consoleHandlers, coreHandle })\n : undefined;\n\n // Track WebSocket connections\n const wsConnections = new Map<string, ServerWebSocket<WsData>>();\n\n // Handle outgoing WebSocket commands from QuickJS\n fetchHandle.onWebSocketCommand((cmd: WebSocketCommand) => {\n const ws = wsConnections.get(cmd.connectionId);\n if (!ws) return;\n\n if (cmd.type === \"message\") {\n ws.send(cmd.data);\n } else if (cmd.type === \"close\") {\n ws.close(cmd.code, cmd.reason);\n }\n });\n\n // Run the user's QuickJS code\n const result = context.evalCode(quickJSCode);\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n fetchHandle.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n throw new Error(`Failed to evaluate QuickJS code: ${JSON.stringify(error)}`);\n }\n result.value.dispose();\n\n // Start Bun server with port 0 for dynamic assignment\n const server: Server<WsData> = Bun.serve<WsData>({\n port: 0,\n async fetch(req, server) {\n try {\n // Dispatch to QuickJS handler\n const response = await fetchHandle.dispatchRequest(req);\n\n // Check if QuickJS requested a WebSocket upgrade\n const upgrade = fetchHandle.getUpgradeRequest();\n if (upgrade?.requested) {\n // The connectionId is generated by QuickJS in server.upgrade()\n // User data is stored in QuickJS registry - we just pass the connectionId\n const success = server.upgrade(req, {\n data: { connectionId: upgrade.connectionId },\n });\n if (success) {\n return undefined;\n }\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\n }\n\n return response;\n } catch (error) {\n console.error(\"Integration test request failed:\", error);\n return new Response(\"Internal Server Error\", { status: 500 });\n }\n },\n websocket: {\n open(ws) {\n const { connectionId } = ws.data;\n wsConnections.set(connectionId, ws);\n // User data is looked up from QuickJS registry using connectionId\n fetchHandle.dispatchWebSocketOpen(connectionId);\n },\n message(ws, message) {\n const msg = typeof message === \"string\" ? message : message.buffer;\n fetchHandle.dispatchWebSocketMessage(ws.data.connectionId, msg as string | ArrayBuffer);\n },\n close(ws, code, reason) {\n fetchHandle.dispatchWebSocketClose(ws.data.connectionId, code, reason);\n wsConnections.delete(ws.data.connectionId);\n },\n },\n });\n\n const port = server.port;\n if (port === undefined) {\n throw new Error(\"Server failed to get port\");\n }\n const baseURL = `http://localhost:${port}`;\n const wsURL = `ws://localhost:${port}`;\n\n return {\n baseURL,\n wsURL,\n port,\n fetchHandle,\n context,\n runtime,\n coreHandle,\n consoleHandle,\n async stop() {\n // Close all WebSocket connections\n for (const ws of wsConnections.values()) {\n ws.close(1000, \"Server stopping\");\n }\n wsConnections.clear();\n\n // Stop the server\n server.stop(true);\n\n // Dispose QuickJS resources\n fetchHandle.dispose();\n consoleHandle?.dispose();\n coreHandle.dispose();\n context.dispose();\n runtime.dispose();\n },\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;AAAA;AACA;AACA;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;AAAA;AACA;AACA;AAEA;AA8CA,eAAsB,sBAAsB,CAC1C,aACA,SACgC;AAAA,EAChC,MAAM,UAAU,MAAM,WAAW;AAAA,EACjC,MAAM,UAAU,QAAQ,WAAW;AAAA,EACnC,MAAM,UAAU,QAAQ,WAAW;AAAA,EAGnC,MAAM,aAAa,UAAU,OAAO;AAAA,EACpC,MAAM,cAAc,WAAW,SAAS;AAAA,IACtC;AAAA,IACA,SAAS,SAAS;AAAA,EACpB,CAAC;AAAA,EAGD,MAAM,gBAAgB,SAAS,kBAC3B,aAAa,SAAS,EAAE,UAAU,QAAQ,iBAAiB,WAAW,CAAC,IACvE;AAAA,EAGJ,MAAM,gBAAgB,IAAI;AAAA,EAG1B,YAAY,mBAAmB,CAAC,QAA0B;AAAA,IACxD,MAAM,KAAK,cAAc,IAAI,IAAI,YAAY;AAAA,IAC7C,IAAI,CAAC;AAAA,MAAI;AAAA,IAET,IAAI,IAAI,SAAS,WAAW;AAAA,MAC1B,GAAG,KAAK,IAAI,IAAI;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sBAAsB,YAAY;AAAA;AAAA,MAEhD,OAAO,CAAC,IAAI,SAAS;AAAA,QACnB,MAAM,MAAM,OAAO,YAAY,WAAW,UAAU,QAAQ;AAAA,QAC5D,YAAY,yBAAyB,GAAG,KAAK,cAAc,GAA2B;AAAA;AAAA,MAExF,KAAK,CAAC,IAAI,MAAM,QAAQ;AAAA,QACtB,YAAY,uBAAuB,GAAG,KAAK,cAAc,MAAM,MAAM;AAAA,QACrE,cAAc,OAAO,GAAG,KAAK,YAAY;AAAA;AAAA,IAE7C;AAAA,EACF,CAAC;AAAA,EAED,MAAM,OAAO,OAAO;AAAA,EACpB,IAAI,SAAS,WAAW;AAAA,IACtB,MAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AAAA,EACA,MAAM,UAAU,oBAAoB;AAAA,EACpC,MAAM,QAAQ,kBAAkB;AAAA,EAEhC,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,SACM,KAAI,GAAG;AAAA,MAEX,WAAW,MAAM,cAAc,OAAO,GAAG;AAAA,QACvC,GAAG,MAAM,MAAM,iBAAiB;AAAA,MAClC;AAAA,MACA,cAAc,MAAM;AAAA,MAGpB,OAAO,KAAK,IAAI;AAAA,MAGhB,YAAY,QAAQ;AAAA,MACpB,eAAe,QAAQ;AAAA,MACvB,WAAW,QAAQ;AAAA,MACnB,QAAQ,QAAQ;AAAA,MAChB,QAAQ,QAAQ;AAAA;AAAA,EAEpB;AAAA;",
|
|
8
|
+
"debugId": "97F49DC31D81D1FD64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/dist/mjs/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type QuickJSContext, type QuickJSRuntime } from "quickjs-emscripten";
|
|
2
2
|
import { type FetchHandle, type SetupFetchOptions } from "@ricsam/quickjs-fetch";
|
|
3
3
|
import { type CoreHandle } from "@ricsam/quickjs-core";
|
|
4
|
+
import { type ConsoleOutputHandler, type ConsoleHandle } from "@ricsam/quickjs-console";
|
|
4
5
|
export interface IntegrationTestServer {
|
|
5
6
|
baseURL: string;
|
|
6
7
|
wsURL: string;
|
|
@@ -9,11 +10,14 @@ export interface IntegrationTestServer {
|
|
|
9
10
|
context: QuickJSContext;
|
|
10
11
|
runtime: QuickJSRuntime;
|
|
11
12
|
coreHandle: CoreHandle;
|
|
13
|
+
consoleHandle?: ConsoleHandle;
|
|
12
14
|
stop(): Promise<void>;
|
|
13
15
|
}
|
|
14
16
|
export interface StartIntegrationServerOptions {
|
|
15
17
|
/** Handler for outbound fetch() calls from QuickJS */
|
|
16
18
|
onFetch?: SetupFetchOptions["onFetch"];
|
|
19
|
+
/** Handlers for console output from QuickJS */
|
|
20
|
+
consoleHandlers?: ConsoleOutputHandler;
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
23
|
* Start a real HTTP server that dispatches requests to a QuickJS serve() handler.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ricsam/quickjs-test-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"main": "./dist/cjs/index.cjs",
|
|
5
5
|
"types": "./dist/types/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -20,12 +20,16 @@
|
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"quickjs-emscripten": "^0.31.0",
|
|
23
|
-
"@ricsam/quickjs-
|
|
24
|
-
"@ricsam/quickjs-
|
|
25
|
-
"@ricsam/quickjs-
|
|
26
|
-
"@ricsam/quickjs-
|
|
23
|
+
"@ricsam/quickjs-console": "^0.2.14",
|
|
24
|
+
"@ricsam/quickjs-core": "^0.2.14",
|
|
25
|
+
"@ricsam/quickjs-fetch": "^0.2.15",
|
|
26
|
+
"@ricsam/quickjs-fs": "^0.2.14",
|
|
27
|
+
"@ricsam/quickjs-runtime": "^0.2.17"
|
|
27
28
|
},
|
|
28
29
|
"peerDependenciesMeta": {
|
|
30
|
+
"@ricsam/quickjs-console": {
|
|
31
|
+
"optional": true
|
|
32
|
+
},
|
|
29
33
|
"@ricsam/quickjs-fs": {
|
|
30
34
|
"optional": true
|
|
31
35
|
},
|