@computekit/core 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +80 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +138 -14
- package/dist/index.d.ts +138 -14
- package/dist/index.js +80 -8
- package/dist/index.js.map +1 -1
- package/dist/types-BNUPDwV-.d.cts +363 -0
- package/dist/types-BNUPDwV-.d.ts +363 -0
- package/dist/worker.cjs +54 -2
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.cts +1 -1
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +54 -2
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +154 -28
- package/src/pool.ts +9 -1
- package/src/registry.ts +132 -0
- package/src/types.ts +222 -0
- package/src/utils.ts +74 -0
- package/src/worker/runtime.ts +8 -1
- package/dist/types-2XRPtzH9.d.cts +0 -145
- package/dist/types-2XRPtzH9.d.ts +0 -145
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,95 @@
|
|
|
1
|
-
import { W as WasmModuleConfig, C as ComputeKitOptions, a as ComputeOptions, P as PoolStats, b as ComputeKitEvents, c as ComputeResult } from './types-
|
|
2
|
-
export { e as ComputeFunction, d as ComputeProgress, f as WorkerInfo } from './types-
|
|
1
|
+
import { W as WasmModuleConfig, C as ComputeKitOptions, a as ComputeOptions, P as PoolStats, b as ComputeKitEvents, c as ComputeResult } from './types-BNUPDwV-.cjs';
|
|
2
|
+
export { B as BatchItemResult, e as ComputeFunction, d as ComputeProgress, o as ParallelBatchConfig, p as ParallelBatchResult, n as PipelineEvents, l as PipelineMetrics, i as PipelineMode, m as PipelineOptions, k as PipelineState, j as PipelineStatus, h as StageConfig, g as StageInfo, S as StageStatus, f as WorkerInfo } from './types-BNUPDwV-.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ComputeKit Typed Registry
|
|
6
|
+
*
|
|
7
|
+
* This module provides type-safe function registration and execution.
|
|
8
|
+
* Users can extend the ComputeFunctionRegistry interface to get autocomplete
|
|
9
|
+
* and type safety for their registered functions.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Extend the registry interface (in a .d.ts file or at the top of your file)
|
|
14
|
+
* declare module '@computekit/core' {
|
|
15
|
+
* interface ComputeFunctionRegistry {
|
|
16
|
+
* fibonacci: { input: number; output: number };
|
|
17
|
+
* sum: { input: number[]; output: number };
|
|
18
|
+
* processData: { input: { items: string[] }; output: { count: number } };
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* // Now you get autocomplete and type safety!
|
|
23
|
+
* const kit = new ComputeKit();
|
|
24
|
+
* kit.register('fibonacci', (n) => ...); // n is inferred as number
|
|
25
|
+
* const result = await kit.run('fibonacci', 42); // result is number
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Registry interface for compute functions.
|
|
30
|
+
* Extend this interface using module augmentation to add your own functions.
|
|
31
|
+
*
|
|
32
|
+
* Each entry should be in the format:
|
|
33
|
+
* ```ts
|
|
34
|
+
* functionName: { input: InputType; output: OutputType }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* declare module '@computekit/core' {
|
|
40
|
+
* interface ComputeFunctionRegistry {
|
|
41
|
+
* myFunction: { input: string; output: number };
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
interface ComputeFunctionRegistry {
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Helper type to get all registered function names.
|
|
50
|
+
* If no functions are registered, falls back to string.
|
|
51
|
+
*/
|
|
52
|
+
type RegisteredFunctionName = keyof ComputeFunctionRegistry extends never ? string : keyof ComputeFunctionRegistry;
|
|
53
|
+
/**
|
|
54
|
+
* Helper type to check if the registry has any entries.
|
|
55
|
+
*/
|
|
56
|
+
type HasRegisteredFunctions = keyof ComputeFunctionRegistry extends never ? false : true;
|
|
57
|
+
/**
|
|
58
|
+
* Get the input type for a registered function.
|
|
59
|
+
* Falls back to TFallback if the function is not registered.
|
|
60
|
+
*/
|
|
61
|
+
type FunctionInput<TName extends string, TFallback = unknown> = TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TFallback;
|
|
62
|
+
/**
|
|
63
|
+
* Get the output type for a registered function.
|
|
64
|
+
* Falls back to TFallback if the function is not registered.
|
|
65
|
+
*/
|
|
66
|
+
type FunctionOutput<TName extends string, TFallback = unknown> = TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TFallback;
|
|
67
|
+
/**
|
|
68
|
+
* Type for a compute function based on registry or explicit types.
|
|
69
|
+
*/
|
|
70
|
+
type ComputeFn<TInput, TOutput> = (input: TInput) => TOutput | Promise<TOutput>;
|
|
71
|
+
/**
|
|
72
|
+
* Infer the compute function type for a registered function name.
|
|
73
|
+
*/
|
|
74
|
+
type InferComputeFn<TName extends string> = TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<unknown, unknown>;
|
|
75
|
+
/**
|
|
76
|
+
* Type helper for creating registry entries.
|
|
77
|
+
* Use this to define your function types more easily.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* declare module '@computekit/core' {
|
|
82
|
+
* interface ComputeFunctionRegistry {
|
|
83
|
+
* fibonacci: DefineFunction<number, number>;
|
|
84
|
+
* sum: DefineFunction<number[], number>;
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
type DefineFunction<TInput, TOutput> = {
|
|
90
|
+
input: TInput;
|
|
91
|
+
output: TOutput;
|
|
92
|
+
};
|
|
3
93
|
|
|
4
94
|
/**
|
|
5
95
|
* Check if SharedArrayBuffer is available
|
|
@@ -199,34 +289,46 @@ declare class ComputeKit extends EventEmitter<ComputeKitEvents> {
|
|
|
199
289
|
/**
|
|
200
290
|
* Register a compute function
|
|
201
291
|
*
|
|
202
|
-
* @param name - Unique name for the function
|
|
292
|
+
* @param name - Unique name for the function (autocompletes if registry is extended)
|
|
203
293
|
* @param fn - The function to execute (will run in a Web Worker)
|
|
204
294
|
*
|
|
205
295
|
* @example
|
|
206
296
|
* ```ts
|
|
297
|
+
* // Basic usage
|
|
207
298
|
* kit.register('sum', (arr: number[]) => arr.reduce((a, b) => a + b, 0));
|
|
299
|
+
*
|
|
300
|
+
* // With typed registry (extend ComputeFunctionRegistry for autocomplete)
|
|
301
|
+
* // declare module '@computekit/core' {
|
|
302
|
+
* // interface ComputeFunctionRegistry {
|
|
303
|
+
* // sum: { input: number[]; output: number };
|
|
304
|
+
* // }
|
|
305
|
+
* // }
|
|
306
|
+
* // kit.register('sum', (arr) => arr.reduce((a, b) => a + b, 0));
|
|
208
307
|
* ```
|
|
209
308
|
*/
|
|
210
|
-
register<
|
|
309
|
+
register<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, fn: TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<TInput, TOutput>): this;
|
|
211
310
|
/**
|
|
212
311
|
* Execute a registered compute function
|
|
213
312
|
*
|
|
214
|
-
* @param name - Name of the registered function
|
|
215
|
-
* @param input - Input data for the function
|
|
313
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
314
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
216
315
|
* @param options - Execution options
|
|
217
|
-
* @returns Promise resolving to the function result
|
|
316
|
+
* @returns Promise resolving to the function result (type-safe if registry is extended)
|
|
218
317
|
*
|
|
219
318
|
* @example
|
|
220
319
|
* ```ts
|
|
221
320
|
* const sum = await kit.run('sum', [1, 2, 3, 4, 5]);
|
|
321
|
+
*
|
|
322
|
+
* // With typed registry, input/output types are inferred:
|
|
323
|
+
* // const result = await kit.run('fibonacci', 50); // result: number
|
|
222
324
|
* ```
|
|
223
325
|
*/
|
|
224
|
-
run<
|
|
326
|
+
run<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>;
|
|
225
327
|
/**
|
|
226
328
|
* Execute a registered compute function with full result metadata
|
|
227
329
|
*
|
|
228
|
-
* @param name - Name of the registered function
|
|
229
|
-
* @param input - Input data for the function
|
|
330
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
331
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
230
332
|
* @param options - Execution options
|
|
231
333
|
* @returns Promise resolving to ComputeResult with metadata
|
|
232
334
|
*
|
|
@@ -236,7 +338,7 @@ declare class ComputeKit extends EventEmitter<ComputeKitEvents> {
|
|
|
236
338
|
* console.log(`Took ${result.duration}ms`);
|
|
237
339
|
* ```
|
|
238
340
|
*/
|
|
239
|
-
runWithMetadata<
|
|
341
|
+
runWithMetadata<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<ComputeResult<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>>;
|
|
240
342
|
/**
|
|
241
343
|
* Get pool statistics
|
|
242
344
|
*/
|
|
@@ -260,11 +362,33 @@ declare function createComputeKit(options?: ComputeKitOptions): ComputeKit;
|
|
|
260
362
|
declare function getDefaultInstance(): ComputeKit;
|
|
261
363
|
/**
|
|
262
364
|
* Register a function on the default instance
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* import { register } from '@computekit/core';
|
|
369
|
+
*
|
|
370
|
+
* register('fibonacci', (n: number) => {
|
|
371
|
+
* if (n <= 1) return n;
|
|
372
|
+
* let a = 0, b = 1;
|
|
373
|
+
* for (let i = 2; i <= n; i++) {
|
|
374
|
+
* [a, b] = [b, a + b];
|
|
375
|
+
* }
|
|
376
|
+
* return b;
|
|
377
|
+
* });
|
|
378
|
+
* ```
|
|
263
379
|
*/
|
|
264
|
-
declare function register<
|
|
380
|
+
declare function register<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, fn: TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<TInput, TOutput>): void;
|
|
265
381
|
/**
|
|
266
382
|
* Run a function on the default instance
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* import { run } from '@computekit/core';
|
|
387
|
+
*
|
|
388
|
+
* const result = await run('fibonacci', 50);
|
|
389
|
+
* console.log(result); // Type is inferred if registry is extended
|
|
390
|
+
* ```
|
|
267
391
|
*/
|
|
268
|
-
declare function run<
|
|
392
|
+
declare function run<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>;
|
|
269
393
|
|
|
270
|
-
export { ComputeKit, ComputeKitEvents, ComputeKitOptions, ComputeOptions, ComputeResult, PoolStats, WasmModuleConfig, WorkerPool, clearWasmCache, copyFromWasmMemory, copyToWasmMemory, createComputeKit, findTransferables, getDefaultInstance, getHardwareConcurrency, getMemoryView, getWasmCacheStats, isSharedArrayBufferAvailable, isWasmSupported, loadAndInstantiate, loadAssemblyScript, loadWasmModule, register, run, wrapWasmExports };
|
|
394
|
+
export { type ComputeFn, type ComputeFunctionRegistry, ComputeKit, ComputeKitEvents, ComputeKitOptions, ComputeOptions, ComputeResult, type DefineFunction, type FunctionInput, type FunctionOutput, type HasRegisteredFunctions, type InferComputeFn, PoolStats, type RegisteredFunctionName, WasmModuleConfig, WorkerPool, clearWasmCache, copyFromWasmMemory, copyToWasmMemory, createComputeKit, findTransferables, getDefaultInstance, getHardwareConcurrency, getMemoryView, getWasmCacheStats, isSharedArrayBufferAvailable, isWasmSupported, loadAndInstantiate, loadAssemblyScript, loadWasmModule, register, run, wrapWasmExports };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,95 @@
|
|
|
1
|
-
import { W as WasmModuleConfig, C as ComputeKitOptions, a as ComputeOptions, P as PoolStats, b as ComputeKitEvents, c as ComputeResult } from './types-
|
|
2
|
-
export { e as ComputeFunction, d as ComputeProgress, f as WorkerInfo } from './types-
|
|
1
|
+
import { W as WasmModuleConfig, C as ComputeKitOptions, a as ComputeOptions, P as PoolStats, b as ComputeKitEvents, c as ComputeResult } from './types-BNUPDwV-.js';
|
|
2
|
+
export { B as BatchItemResult, e as ComputeFunction, d as ComputeProgress, o as ParallelBatchConfig, p as ParallelBatchResult, n as PipelineEvents, l as PipelineMetrics, i as PipelineMode, m as PipelineOptions, k as PipelineState, j as PipelineStatus, h as StageConfig, g as StageInfo, S as StageStatus, f as WorkerInfo } from './types-BNUPDwV-.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ComputeKit Typed Registry
|
|
6
|
+
*
|
|
7
|
+
* This module provides type-safe function registration and execution.
|
|
8
|
+
* Users can extend the ComputeFunctionRegistry interface to get autocomplete
|
|
9
|
+
* and type safety for their registered functions.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Extend the registry interface (in a .d.ts file or at the top of your file)
|
|
14
|
+
* declare module '@computekit/core' {
|
|
15
|
+
* interface ComputeFunctionRegistry {
|
|
16
|
+
* fibonacci: { input: number; output: number };
|
|
17
|
+
* sum: { input: number[]; output: number };
|
|
18
|
+
* processData: { input: { items: string[] }; output: { count: number } };
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* // Now you get autocomplete and type safety!
|
|
23
|
+
* const kit = new ComputeKit();
|
|
24
|
+
* kit.register('fibonacci', (n) => ...); // n is inferred as number
|
|
25
|
+
* const result = await kit.run('fibonacci', 42); // result is number
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Registry interface for compute functions.
|
|
30
|
+
* Extend this interface using module augmentation to add your own functions.
|
|
31
|
+
*
|
|
32
|
+
* Each entry should be in the format:
|
|
33
|
+
* ```ts
|
|
34
|
+
* functionName: { input: InputType; output: OutputType }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* declare module '@computekit/core' {
|
|
40
|
+
* interface ComputeFunctionRegistry {
|
|
41
|
+
* myFunction: { input: string; output: number };
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
interface ComputeFunctionRegistry {
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Helper type to get all registered function names.
|
|
50
|
+
* If no functions are registered, falls back to string.
|
|
51
|
+
*/
|
|
52
|
+
type RegisteredFunctionName = keyof ComputeFunctionRegistry extends never ? string : keyof ComputeFunctionRegistry;
|
|
53
|
+
/**
|
|
54
|
+
* Helper type to check if the registry has any entries.
|
|
55
|
+
*/
|
|
56
|
+
type HasRegisteredFunctions = keyof ComputeFunctionRegistry extends never ? false : true;
|
|
57
|
+
/**
|
|
58
|
+
* Get the input type for a registered function.
|
|
59
|
+
* Falls back to TFallback if the function is not registered.
|
|
60
|
+
*/
|
|
61
|
+
type FunctionInput<TName extends string, TFallback = unknown> = TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TFallback;
|
|
62
|
+
/**
|
|
63
|
+
* Get the output type for a registered function.
|
|
64
|
+
* Falls back to TFallback if the function is not registered.
|
|
65
|
+
*/
|
|
66
|
+
type FunctionOutput<TName extends string, TFallback = unknown> = TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TFallback;
|
|
67
|
+
/**
|
|
68
|
+
* Type for a compute function based on registry or explicit types.
|
|
69
|
+
*/
|
|
70
|
+
type ComputeFn<TInput, TOutput> = (input: TInput) => TOutput | Promise<TOutput>;
|
|
71
|
+
/**
|
|
72
|
+
* Infer the compute function type for a registered function name.
|
|
73
|
+
*/
|
|
74
|
+
type InferComputeFn<TName extends string> = TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<unknown, unknown>;
|
|
75
|
+
/**
|
|
76
|
+
* Type helper for creating registry entries.
|
|
77
|
+
* Use this to define your function types more easily.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* declare module '@computekit/core' {
|
|
82
|
+
* interface ComputeFunctionRegistry {
|
|
83
|
+
* fibonacci: DefineFunction<number, number>;
|
|
84
|
+
* sum: DefineFunction<number[], number>;
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
type DefineFunction<TInput, TOutput> = {
|
|
90
|
+
input: TInput;
|
|
91
|
+
output: TOutput;
|
|
92
|
+
};
|
|
3
93
|
|
|
4
94
|
/**
|
|
5
95
|
* Check if SharedArrayBuffer is available
|
|
@@ -199,34 +289,46 @@ declare class ComputeKit extends EventEmitter<ComputeKitEvents> {
|
|
|
199
289
|
/**
|
|
200
290
|
* Register a compute function
|
|
201
291
|
*
|
|
202
|
-
* @param name - Unique name for the function
|
|
292
|
+
* @param name - Unique name for the function (autocompletes if registry is extended)
|
|
203
293
|
* @param fn - The function to execute (will run in a Web Worker)
|
|
204
294
|
*
|
|
205
295
|
* @example
|
|
206
296
|
* ```ts
|
|
297
|
+
* // Basic usage
|
|
207
298
|
* kit.register('sum', (arr: number[]) => arr.reduce((a, b) => a + b, 0));
|
|
299
|
+
*
|
|
300
|
+
* // With typed registry (extend ComputeFunctionRegistry for autocomplete)
|
|
301
|
+
* // declare module '@computekit/core' {
|
|
302
|
+
* // interface ComputeFunctionRegistry {
|
|
303
|
+
* // sum: { input: number[]; output: number };
|
|
304
|
+
* // }
|
|
305
|
+
* // }
|
|
306
|
+
* // kit.register('sum', (arr) => arr.reduce((a, b) => a + b, 0));
|
|
208
307
|
* ```
|
|
209
308
|
*/
|
|
210
|
-
register<
|
|
309
|
+
register<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, fn: TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<TInput, TOutput>): this;
|
|
211
310
|
/**
|
|
212
311
|
* Execute a registered compute function
|
|
213
312
|
*
|
|
214
|
-
* @param name - Name of the registered function
|
|
215
|
-
* @param input - Input data for the function
|
|
313
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
314
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
216
315
|
* @param options - Execution options
|
|
217
|
-
* @returns Promise resolving to the function result
|
|
316
|
+
* @returns Promise resolving to the function result (type-safe if registry is extended)
|
|
218
317
|
*
|
|
219
318
|
* @example
|
|
220
319
|
* ```ts
|
|
221
320
|
* const sum = await kit.run('sum', [1, 2, 3, 4, 5]);
|
|
321
|
+
*
|
|
322
|
+
* // With typed registry, input/output types are inferred:
|
|
323
|
+
* // const result = await kit.run('fibonacci', 50); // result: number
|
|
222
324
|
* ```
|
|
223
325
|
*/
|
|
224
|
-
run<
|
|
326
|
+
run<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>;
|
|
225
327
|
/**
|
|
226
328
|
* Execute a registered compute function with full result metadata
|
|
227
329
|
*
|
|
228
|
-
* @param name - Name of the registered function
|
|
229
|
-
* @param input - Input data for the function
|
|
330
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
331
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
230
332
|
* @param options - Execution options
|
|
231
333
|
* @returns Promise resolving to ComputeResult with metadata
|
|
232
334
|
*
|
|
@@ -236,7 +338,7 @@ declare class ComputeKit extends EventEmitter<ComputeKitEvents> {
|
|
|
236
338
|
* console.log(`Took ${result.duration}ms`);
|
|
237
339
|
* ```
|
|
238
340
|
*/
|
|
239
|
-
runWithMetadata<
|
|
341
|
+
runWithMetadata<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<ComputeResult<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>>;
|
|
240
342
|
/**
|
|
241
343
|
* Get pool statistics
|
|
242
344
|
*/
|
|
@@ -260,11 +362,33 @@ declare function createComputeKit(options?: ComputeKitOptions): ComputeKit;
|
|
|
260
362
|
declare function getDefaultInstance(): ComputeKit;
|
|
261
363
|
/**
|
|
262
364
|
* Register a function on the default instance
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* import { register } from '@computekit/core';
|
|
369
|
+
*
|
|
370
|
+
* register('fibonacci', (n: number) => {
|
|
371
|
+
* if (n <= 1) return n;
|
|
372
|
+
* let a = 0, b = 1;
|
|
373
|
+
* for (let i = 2; i <= n; i++) {
|
|
374
|
+
* [a, b] = [b, a + b];
|
|
375
|
+
* }
|
|
376
|
+
* return b;
|
|
377
|
+
* });
|
|
378
|
+
* ```
|
|
263
379
|
*/
|
|
264
|
-
declare function register<
|
|
380
|
+
declare function register<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, fn: TName extends keyof ComputeFunctionRegistry ? ComputeFn<ComputeFunctionRegistry[TName]['input'], ComputeFunctionRegistry[TName]['output']> : ComputeFn<TInput, TOutput>): void;
|
|
265
381
|
/**
|
|
266
382
|
* Run a function on the default instance
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* import { run } from '@computekit/core';
|
|
387
|
+
*
|
|
388
|
+
* const result = await run('fibonacci', 50);
|
|
389
|
+
* console.log(result); // Type is inferred if registry is extended
|
|
390
|
+
* ```
|
|
267
391
|
*/
|
|
268
|
-
declare function run<
|
|
392
|
+
declare function run<TName extends RegisteredFunctionName, TInput = FunctionInput<TName extends string ? TName : never>, TOutput = FunctionOutput<TName extends string ? TName : never>>(name: TName, input: TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['input'] : TInput, options?: ComputeOptions): Promise<TName extends keyof ComputeFunctionRegistry ? ComputeFunctionRegistry[TName]['output'] : TOutput>;
|
|
269
393
|
|
|
270
|
-
export { ComputeKit, ComputeKitEvents, ComputeKitOptions, ComputeOptions, ComputeResult, PoolStats, WasmModuleConfig, WorkerPool, clearWasmCache, copyFromWasmMemory, copyToWasmMemory, createComputeKit, findTransferables, getDefaultInstance, getHardwareConcurrency, getMemoryView, getWasmCacheStats, isSharedArrayBufferAvailable, isWasmSupported, loadAndInstantiate, loadAssemblyScript, loadWasmModule, register, run, wrapWasmExports };
|
|
394
|
+
export { type ComputeFn, type ComputeFunctionRegistry, ComputeKit, ComputeKitEvents, ComputeKitOptions, ComputeOptions, ComputeResult, type DefineFunction, type FunctionInput, type FunctionOutput, type HasRegisteredFunctions, type InferComputeFn, PoolStats, type RegisteredFunctionName, WasmModuleConfig, WorkerPool, clearWasmCache, copyFromWasmMemory, copyToWasmMemory, createComputeKit, findTransferables, getDefaultInstance, getHardwareConcurrency, getMemoryView, getWasmCacheStats, isSharedArrayBufferAvailable, isWasmSupported, loadAndInstantiate, loadAssemblyScript, loadWasmModule, register, run, wrapWasmExports };
|
package/dist/index.js
CHANGED
|
@@ -160,6 +160,60 @@ var LRUCache = class {
|
|
|
160
160
|
return this.cache.size;
|
|
161
161
|
}
|
|
162
162
|
};
|
|
163
|
+
function estimatePayloadSize(value) {
|
|
164
|
+
if (value === null || value === void 0) return 0;
|
|
165
|
+
if (typeof value === "boolean") return 4;
|
|
166
|
+
if (typeof value === "number") return 8;
|
|
167
|
+
if (typeof value === "string") return value.length * 2;
|
|
168
|
+
if (value instanceof ArrayBuffer) return value.byteLength;
|
|
169
|
+
if (ArrayBuffer.isView(value)) return value.byteLength;
|
|
170
|
+
if (value instanceof Blob) return value.size;
|
|
171
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
172
|
+
function traverse(obj) {
|
|
173
|
+
if (obj === null || typeof obj !== "object") {
|
|
174
|
+
if (typeof obj === "boolean") return 4;
|
|
175
|
+
if (typeof obj === "number") return 8;
|
|
176
|
+
if (typeof obj === "string") return obj.length * 2;
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
if (seen.has(obj)) return 0;
|
|
180
|
+
seen.add(obj);
|
|
181
|
+
if (obj instanceof ArrayBuffer) return obj.byteLength;
|
|
182
|
+
if (ArrayBuffer.isView(obj)) return obj.byteLength;
|
|
183
|
+
if (obj instanceof Blob) return obj.size;
|
|
184
|
+
if (obj instanceof Date) return 8;
|
|
185
|
+
if (obj instanceof RegExp) return obj.source.length * 2;
|
|
186
|
+
if (Array.isArray(obj)) {
|
|
187
|
+
return obj.reduce((sum, item) => sum + traverse(item), 0);
|
|
188
|
+
}
|
|
189
|
+
if (obj instanceof Map) {
|
|
190
|
+
let size = 0;
|
|
191
|
+
obj.forEach((val, key) => {
|
|
192
|
+
size += traverse(key) + traverse(val);
|
|
193
|
+
});
|
|
194
|
+
return size;
|
|
195
|
+
}
|
|
196
|
+
if (obj instanceof Set) {
|
|
197
|
+
let size = 0;
|
|
198
|
+
obj.forEach((val) => {
|
|
199
|
+
size += traverse(val);
|
|
200
|
+
});
|
|
201
|
+
return size;
|
|
202
|
+
}
|
|
203
|
+
return Object.entries(obj).reduce(
|
|
204
|
+
(sum, [key, val]) => sum + key.length * 2 + traverse(val),
|
|
205
|
+
0
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return traverse(value);
|
|
209
|
+
}
|
|
210
|
+
function formatBytes(bytes) {
|
|
211
|
+
if (bytes === 0) return "0 B";
|
|
212
|
+
const k = 1024;
|
|
213
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
214
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
215
|
+
return `${(bytes / Math.pow(k, i)).toFixed(i > 0 ? 1 : 0)} ${sizes[i]}`;
|
|
216
|
+
}
|
|
163
217
|
function createLogger(prefix, enabled = false) {
|
|
164
218
|
const noop = () => {
|
|
165
219
|
};
|
|
@@ -451,8 +505,10 @@ self.postMessage({ type: 'ready' });
|
|
|
451
505
|
poolWorker.lastActiveAt = Date.now();
|
|
452
506
|
this.stats.tasksCompleted++;
|
|
453
507
|
this.stats.totalDuration += resultPayload.duration;
|
|
508
|
+
const inputSize = estimatePayloadSize(task.input);
|
|
509
|
+
const outputSize = resultPayload.outputSize ?? 0;
|
|
454
510
|
this.logger.debug(
|
|
455
|
-
`Task ${id}
|
|
511
|
+
`Task ${id} (${task.functionName}): input=${formatBytes(inputSize)}, output=${formatBytes(outputSize)}, duration=${resultPayload.duration.toFixed(2)}ms`
|
|
456
512
|
);
|
|
457
513
|
task.deferred.resolve(resultPayload.data);
|
|
458
514
|
this.processQueue();
|
|
@@ -709,12 +765,21 @@ var ComputeKit = class extends EventEmitter {
|
|
|
709
765
|
/**
|
|
710
766
|
* Register a compute function
|
|
711
767
|
*
|
|
712
|
-
* @param name - Unique name for the function
|
|
768
|
+
* @param name - Unique name for the function (autocompletes if registry is extended)
|
|
713
769
|
* @param fn - The function to execute (will run in a Web Worker)
|
|
714
770
|
*
|
|
715
771
|
* @example
|
|
716
772
|
* ```ts
|
|
773
|
+
* // Basic usage
|
|
717
774
|
* kit.register('sum', (arr: number[]) => arr.reduce((a, b) => a + b, 0));
|
|
775
|
+
*
|
|
776
|
+
* // With typed registry (extend ComputeFunctionRegistry for autocomplete)
|
|
777
|
+
* // declare module '@computekit/core' {
|
|
778
|
+
* // interface ComputeFunctionRegistry {
|
|
779
|
+
* // sum: { input: number[]; output: number };
|
|
780
|
+
* // }
|
|
781
|
+
* // }
|
|
782
|
+
* // kit.register('sum', (arr) => arr.reduce((a, b) => a + b, 0));
|
|
718
783
|
* ```
|
|
719
784
|
*/
|
|
720
785
|
register(name, fn) {
|
|
@@ -724,14 +789,17 @@ var ComputeKit = class extends EventEmitter {
|
|
|
724
789
|
/**
|
|
725
790
|
* Execute a registered compute function
|
|
726
791
|
*
|
|
727
|
-
* @param name - Name of the registered function
|
|
728
|
-
* @param input - Input data for the function
|
|
792
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
793
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
729
794
|
* @param options - Execution options
|
|
730
|
-
* @returns Promise resolving to the function result
|
|
795
|
+
* @returns Promise resolving to the function result (type-safe if registry is extended)
|
|
731
796
|
*
|
|
732
797
|
* @example
|
|
733
798
|
* ```ts
|
|
734
799
|
* const sum = await kit.run('sum', [1, 2, 3, 4, 5]);
|
|
800
|
+
*
|
|
801
|
+
* // With typed registry, input/output types are inferred:
|
|
802
|
+
* // const result = await kit.run('fibonacci', 50); // result: number
|
|
735
803
|
* ```
|
|
736
804
|
*/
|
|
737
805
|
async run(name, input, options) {
|
|
@@ -740,8 +808,8 @@ var ComputeKit = class extends EventEmitter {
|
|
|
740
808
|
/**
|
|
741
809
|
* Execute a registered compute function with full result metadata
|
|
742
810
|
*
|
|
743
|
-
* @param name - Name of the registered function
|
|
744
|
-
* @param input - Input data for the function
|
|
811
|
+
* @param name - Name of the registered function (autocompletes if registry is extended)
|
|
812
|
+
* @param input - Input data for the function (type-safe if registry is extended)
|
|
745
813
|
* @param options - Execution options
|
|
746
814
|
* @returns Promise resolving to ComputeResult with metadata
|
|
747
815
|
*
|
|
@@ -753,7 +821,11 @@ var ComputeKit = class extends EventEmitter {
|
|
|
753
821
|
*/
|
|
754
822
|
async runWithMetadata(name, input, options) {
|
|
755
823
|
const startTime = performance.now();
|
|
756
|
-
const data = await this.pool.execute(
|
|
824
|
+
const data = await this.pool.execute(
|
|
825
|
+
name,
|
|
826
|
+
input,
|
|
827
|
+
options
|
|
828
|
+
);
|
|
757
829
|
const duration = performance.now() - startTime;
|
|
758
830
|
return {
|
|
759
831
|
data,
|