@componentor/quickjs-emscripten 0.31.26 → 0.31.28

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/index.d.mts CHANGED
@@ -1,11 +1,98 @@
1
1
  import { QuickJSWASMModule, AsyncRuntimeOptions, QuickJSAsyncRuntime, ContextOptions, QuickJSAsyncContext } from '@componentor/quickjs-emscripten-core';
2
2
  export * from '@componentor/quickjs-emscripten-core';
3
3
  export { newQuickJSAsyncWASMModule, newQuickJSWASMModule } from './variants.mjs';
4
+ import { WorkerEnabledContextOptions, WorkerEnabledContext } from '@componentor/quickjs-emscripten-worker-pool';
5
+ export { MultiThreadingNotSupportedError, PoolDisposedError, PoolStats, QueueFullError, QuickJSWorkerPool, SessionEvalOptions, TaskHandle, WorkerCrashError, WorkerEnabledContext, WorkerEnabledContextOptions, WorkerEnabledContextResult, WorkerPoolContext, WorkerPoolContextResult, WorkerPoolEvalOptions, WorkerPoolOptions, WorkerPoolVariant, WorkerSession, WorkerTask, WorkerTaskCancelledError, WorkerTaskError, WorkerTaskResult, WorkerTaskTimeoutError, detectPlatform, getDefaultPoolSize, getWorkerPool, getWorkerPoolSync, isMultiThreadingSupported, newWorkerEnabledContext, newWorkerPool, newWorkerPoolContext } from '@componentor/quickjs-emscripten-worker-pool';
4
6
  export { default as DEBUG_SYNC } from '@componentor/quickjs-wasmfile-debug-sync';
5
7
  export { default as RELEASE_SYNC } from '@componentor/quickjs-wasmfile-release-sync';
6
8
  export { default as DEBUG_ASYNC } from '@componentor/quickjs-wasmfile-debug-asyncify';
7
9
  export { default as RELEASE_ASYNC } from '@componentor/quickjs-wasmfile-release-asyncify';
8
10
 
11
+ /**
12
+ * Global configuration for worker pool contexts.
13
+ * These settings are used as defaults when calling {@link newWorkerAsyncContext}.
14
+ */
15
+ interface WorkerPoolConfig {
16
+ /**
17
+ * Whether worker pool is enabled.
18
+ * @default true
19
+ */
20
+ enabled?: boolean;
21
+ /**
22
+ * Number of workers in the pool.
23
+ * @default navigator.hardwareConcurrency or 4
24
+ */
25
+ poolSize?: number;
26
+ /**
27
+ * Bootstrap code that runs on each worker during initialization.
28
+ * Use this to set up shared state like mocks, utilities, etc.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * configureWorkerPool({
33
+ * bootstrapCode: `
34
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
35
+ * `
36
+ * })
37
+ * ```
38
+ */
39
+ bootstrapCode?: string;
40
+ /**
41
+ * Whether to use a session for state persistence.
42
+ * - false (default): Parallel execution, each eval may hit different worker
43
+ * - true: Sequential execution, all evals go to same worker (state persists)
44
+ * @default false
45
+ */
46
+ useSession?: boolean;
47
+ /**
48
+ * Enable verbose logging for debugging.
49
+ * @default false
50
+ */
51
+ verbose?: boolean;
52
+ /**
53
+ * Default timeout for code execution in milliseconds.
54
+ * @default 0 (no timeout)
55
+ */
56
+ defaultTimeout?: number;
57
+ }
58
+ /**
59
+ * Configure the global worker pool settings.
60
+ *
61
+ * Call this before creating worker contexts to set up default worker pool options.
62
+ * These settings are used as defaults when calling {@link newWorkerAsyncContext}.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { configureWorkerPool, newWorkerAsyncContext } from "@componentor/quickjs-emscripten"
67
+ *
68
+ * // Configure once at startup
69
+ * configureWorkerPool({
70
+ * poolSize: 4,
71
+ * bootstrapCode: `
72
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
73
+ * `
74
+ * })
75
+ *
76
+ * // Create worker context - uses global config!
77
+ * const ctx = await newWorkerAsyncContext()
78
+ *
79
+ * // Parallel execution across workers
80
+ * const results = await Promise.all([
81
+ * ctx.evalCodeAsync('mockFetch("/api/1")'),
82
+ * ctx.evalCodeAsync('mockFetch("/api/2")'),
83
+ * ctx.evalCodeAsync('mockFetch("/api/3")'),
84
+ * ])
85
+ * ```
86
+ */
87
+ declare function configureWorkerPool(config: WorkerPoolConfig): void;
88
+ /**
89
+ * Get the current global worker pool configuration.
90
+ */
91
+ declare function getWorkerPoolConfig(): WorkerPoolConfig;
92
+ /**
93
+ * Reset the global worker pool configuration to defaults.
94
+ */
95
+ declare function resetWorkerPoolConfig(): void;
9
96
  /**
10
97
  * Get a shared singleton {@link QuickJSWASMModule}. Use this to evaluate code
11
98
  * or create Javascript environments.
@@ -41,8 +128,7 @@ declare function getQuickJSSync(): QuickJSWASMModule;
41
128
  */
42
129
  declare function newAsyncRuntime(options?: AsyncRuntimeOptions): Promise<QuickJSAsyncRuntime>;
43
130
  /**
44
- * Create a new {@link QuickJSAsyncContext} (with an associated runtime) in an
45
- * separate WebAssembly module.
131
+ * Create a new {@link QuickJSAsyncContext} in a separate WebAssembly module.
46
132
  *
47
133
  * Each context is isolated in a separate WebAssembly module, so that errors in
48
134
  * one runtime cannot contaminate another runtime, and each runtime can execute
@@ -51,7 +137,72 @@ declare function newAsyncRuntime(options?: AsyncRuntimeOptions): Promise<QuickJS
51
137
  * Note that there is a hard limit on the number of WebAssembly modules in older
52
138
  * versions of v8:
53
139
  * https://bugs.chromium.org/p/v8/issues/detail?id=12076
140
+ *
141
+ * For parallel execution across workers, use {@link newWorkerAsyncContext} instead.
54
142
  */
55
143
  declare function newAsyncContext(options?: ContextOptions): Promise<QuickJSAsyncContext>;
144
+ /**
145
+ * Options for creating a worker-enabled async context.
146
+ */
147
+ interface WorkerAsyncContextOptions extends WorkerEnabledContextOptions {
148
+ /**
149
+ * Context options for the local QuickJS context.
150
+ */
151
+ contextOptions?: ContextOptions;
152
+ }
153
+ /**
154
+ * Create a new {@link WorkerEnabledContext} for parallel execution across workers.
155
+ *
156
+ * This is the recommended way to use QuickJS with parallel execution. Each
157
+ * `evalCodeAsync` call can be distributed to different workers for parallelism.
158
+ *
159
+ * Uses global config from {@link configureWorkerPool} as defaults, which can
160
+ * be overridden by passing options.
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * // Simple usage with defaults
165
+ * const ctx = await newWorkerAsyncContext()
166
+ *
167
+ * // Parallel execution
168
+ * const results = await Promise.all([
169
+ * ctx.evalCodeAsync('1 + 1'),
170
+ * ctx.evalCodeAsync('2 + 2'),
171
+ * ctx.evalCodeAsync('3 + 3'),
172
+ * ])
173
+ * ```
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * // With bootstrap code for mocks
178
+ * const ctx = await newWorkerAsyncContext({
179
+ * poolSize: 8,
180
+ * bootstrapCode: `
181
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
182
+ * `,
183
+ * })
184
+ * ```
185
+ *
186
+ * @param options Worker pool and context options (overrides global config)
187
+ * @returns A new WorkerEnabledContext instance
188
+ */
189
+ declare function newWorkerAsyncContext(options?: WorkerAsyncContextOptions): Promise<WorkerEnabledContext>;
190
+ /**
191
+ * Check if multi-threading is supported in the current environment.
192
+ *
193
+ * Returns true if SharedArrayBuffer is available (required for Web Workers
194
+ * to share memory). In browsers, this requires COOP/COEP headers to be set.
195
+ * In Node.js, this is typically always available.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * if (canUseWorkers()) {
200
+ * console.log('Parallel execution available!')
201
+ * } else {
202
+ * console.log('Falling back to single-threaded execution')
203
+ * }
204
+ * ```
205
+ */
206
+ declare function canUseWorkers(): boolean;
56
207
 
57
- export { getQuickJS, getQuickJSSync, newAsyncContext, newAsyncRuntime };
208
+ export { type WorkerAsyncContextOptions, type WorkerPoolConfig, canUseWorkers, configureWorkerPool, getQuickJS, getQuickJSSync, getWorkerPoolConfig, newAsyncContext, newAsyncRuntime, newWorkerAsyncContext, resetWorkerPoolConfig };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,98 @@
1
1
  import { QuickJSWASMModule, AsyncRuntimeOptions, QuickJSAsyncRuntime, ContextOptions, QuickJSAsyncContext } from '@componentor/quickjs-emscripten-core';
2
2
  export * from '@componentor/quickjs-emscripten-core';
3
3
  export { newQuickJSAsyncWASMModule, newQuickJSWASMModule } from './variants.js';
4
+ import { WorkerEnabledContextOptions, WorkerEnabledContext } from '@componentor/quickjs-emscripten-worker-pool';
5
+ export { MultiThreadingNotSupportedError, PoolDisposedError, PoolStats, QueueFullError, QuickJSWorkerPool, SessionEvalOptions, TaskHandle, WorkerCrashError, WorkerEnabledContext, WorkerEnabledContextOptions, WorkerEnabledContextResult, WorkerPoolContext, WorkerPoolContextResult, WorkerPoolEvalOptions, WorkerPoolOptions, WorkerPoolVariant, WorkerSession, WorkerTask, WorkerTaskCancelledError, WorkerTaskError, WorkerTaskResult, WorkerTaskTimeoutError, detectPlatform, getDefaultPoolSize, getWorkerPool, getWorkerPoolSync, isMultiThreadingSupported, newWorkerEnabledContext, newWorkerPool, newWorkerPoolContext } from '@componentor/quickjs-emscripten-worker-pool';
4
6
  export { default as DEBUG_SYNC } from '@componentor/quickjs-wasmfile-debug-sync';
5
7
  export { default as RELEASE_SYNC } from '@componentor/quickjs-wasmfile-release-sync';
6
8
  export { default as DEBUG_ASYNC } from '@componentor/quickjs-wasmfile-debug-asyncify';
7
9
  export { default as RELEASE_ASYNC } from '@componentor/quickjs-wasmfile-release-asyncify';
8
10
 
11
+ /**
12
+ * Global configuration for worker pool contexts.
13
+ * These settings are used as defaults when calling {@link newWorkerAsyncContext}.
14
+ */
15
+ interface WorkerPoolConfig {
16
+ /**
17
+ * Whether worker pool is enabled.
18
+ * @default true
19
+ */
20
+ enabled?: boolean;
21
+ /**
22
+ * Number of workers in the pool.
23
+ * @default navigator.hardwareConcurrency or 4
24
+ */
25
+ poolSize?: number;
26
+ /**
27
+ * Bootstrap code that runs on each worker during initialization.
28
+ * Use this to set up shared state like mocks, utilities, etc.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * configureWorkerPool({
33
+ * bootstrapCode: `
34
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
35
+ * `
36
+ * })
37
+ * ```
38
+ */
39
+ bootstrapCode?: string;
40
+ /**
41
+ * Whether to use a session for state persistence.
42
+ * - false (default): Parallel execution, each eval may hit different worker
43
+ * - true: Sequential execution, all evals go to same worker (state persists)
44
+ * @default false
45
+ */
46
+ useSession?: boolean;
47
+ /**
48
+ * Enable verbose logging for debugging.
49
+ * @default false
50
+ */
51
+ verbose?: boolean;
52
+ /**
53
+ * Default timeout for code execution in milliseconds.
54
+ * @default 0 (no timeout)
55
+ */
56
+ defaultTimeout?: number;
57
+ }
58
+ /**
59
+ * Configure the global worker pool settings.
60
+ *
61
+ * Call this before creating worker contexts to set up default worker pool options.
62
+ * These settings are used as defaults when calling {@link newWorkerAsyncContext}.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { configureWorkerPool, newWorkerAsyncContext } from "@componentor/quickjs-emscripten"
67
+ *
68
+ * // Configure once at startup
69
+ * configureWorkerPool({
70
+ * poolSize: 4,
71
+ * bootstrapCode: `
72
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
73
+ * `
74
+ * })
75
+ *
76
+ * // Create worker context - uses global config!
77
+ * const ctx = await newWorkerAsyncContext()
78
+ *
79
+ * // Parallel execution across workers
80
+ * const results = await Promise.all([
81
+ * ctx.evalCodeAsync('mockFetch("/api/1")'),
82
+ * ctx.evalCodeAsync('mockFetch("/api/2")'),
83
+ * ctx.evalCodeAsync('mockFetch("/api/3")'),
84
+ * ])
85
+ * ```
86
+ */
87
+ declare function configureWorkerPool(config: WorkerPoolConfig): void;
88
+ /**
89
+ * Get the current global worker pool configuration.
90
+ */
91
+ declare function getWorkerPoolConfig(): WorkerPoolConfig;
92
+ /**
93
+ * Reset the global worker pool configuration to defaults.
94
+ */
95
+ declare function resetWorkerPoolConfig(): void;
9
96
  /**
10
97
  * Get a shared singleton {@link QuickJSWASMModule}. Use this to evaluate code
11
98
  * or create Javascript environments.
@@ -41,8 +128,7 @@ declare function getQuickJSSync(): QuickJSWASMModule;
41
128
  */
42
129
  declare function newAsyncRuntime(options?: AsyncRuntimeOptions): Promise<QuickJSAsyncRuntime>;
43
130
  /**
44
- * Create a new {@link QuickJSAsyncContext} (with an associated runtime) in an
45
- * separate WebAssembly module.
131
+ * Create a new {@link QuickJSAsyncContext} in a separate WebAssembly module.
46
132
  *
47
133
  * Each context is isolated in a separate WebAssembly module, so that errors in
48
134
  * one runtime cannot contaminate another runtime, and each runtime can execute
@@ -51,7 +137,72 @@ declare function newAsyncRuntime(options?: AsyncRuntimeOptions): Promise<QuickJS
51
137
  * Note that there is a hard limit on the number of WebAssembly modules in older
52
138
  * versions of v8:
53
139
  * https://bugs.chromium.org/p/v8/issues/detail?id=12076
140
+ *
141
+ * For parallel execution across workers, use {@link newWorkerAsyncContext} instead.
54
142
  */
55
143
  declare function newAsyncContext(options?: ContextOptions): Promise<QuickJSAsyncContext>;
144
+ /**
145
+ * Options for creating a worker-enabled async context.
146
+ */
147
+ interface WorkerAsyncContextOptions extends WorkerEnabledContextOptions {
148
+ /**
149
+ * Context options for the local QuickJS context.
150
+ */
151
+ contextOptions?: ContextOptions;
152
+ }
153
+ /**
154
+ * Create a new {@link WorkerEnabledContext} for parallel execution across workers.
155
+ *
156
+ * This is the recommended way to use QuickJS with parallel execution. Each
157
+ * `evalCodeAsync` call can be distributed to different workers for parallelism.
158
+ *
159
+ * Uses global config from {@link configureWorkerPool} as defaults, which can
160
+ * be overridden by passing options.
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * // Simple usage with defaults
165
+ * const ctx = await newWorkerAsyncContext()
166
+ *
167
+ * // Parallel execution
168
+ * const results = await Promise.all([
169
+ * ctx.evalCodeAsync('1 + 1'),
170
+ * ctx.evalCodeAsync('2 + 2'),
171
+ * ctx.evalCodeAsync('3 + 3'),
172
+ * ])
173
+ * ```
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * // With bootstrap code for mocks
178
+ * const ctx = await newWorkerAsyncContext({
179
+ * poolSize: 8,
180
+ * bootstrapCode: `
181
+ * globalThis.mockFetch = (url) => ({ status: 200, url })
182
+ * `,
183
+ * })
184
+ * ```
185
+ *
186
+ * @param options Worker pool and context options (overrides global config)
187
+ * @returns A new WorkerEnabledContext instance
188
+ */
189
+ declare function newWorkerAsyncContext(options?: WorkerAsyncContextOptions): Promise<WorkerEnabledContext>;
190
+ /**
191
+ * Check if multi-threading is supported in the current environment.
192
+ *
193
+ * Returns true if SharedArrayBuffer is available (required for Web Workers
194
+ * to share memory). In browsers, this requires COOP/COEP headers to be set.
195
+ * In Node.js, this is typically always available.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * if (canUseWorkers()) {
200
+ * console.log('Parallel execution available!')
201
+ * } else {
202
+ * console.log('Falling back to single-threaded execution')
203
+ * }
204
+ * ```
205
+ */
206
+ declare function canUseWorkers(): boolean;
56
207
 
57
- export { getQuickJS, getQuickJSSync, newAsyncContext, newAsyncRuntime };
208
+ export { type WorkerAsyncContextOptions, type WorkerPoolConfig, canUseWorkers, configureWorkerPool, getQuickJS, getQuickJSSync, getWorkerPoolConfig, newAsyncContext, newAsyncRuntime, newWorkerAsyncContext, resetWorkerPoolConfig };