@computekit/core 0.1.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 +820 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +270 -0
- package/dist/index.d.ts +270 -0
- package/dist/index.js +800 -0
- package/dist/index.js.map +1 -0
- package/dist/types-AaH5nWxG.d.cts +143 -0
- package/dist/types-AaH5nWxG.d.ts +143 -0
- package/dist/worker.cjs +148 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.cts +23 -0
- package/dist/worker.d.ts +23 -0
- package/dist/worker.js +143 -0
- package/dist/worker.js.map +1 -0
- package/package.json +61 -0
- package/src/cli.ts +246 -0
- package/src/index.test.ts +93 -0
- package/src/index.ts +232 -0
- package/src/pool.ts +591 -0
- package/src/types.ts +229 -0
- package/src/utils.ts +305 -0
- package/src/wasm.ts +205 -0
- package/src/worker/index.ts +11 -0
- package/src/worker/runtime.ts +149 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ComputeKit - Main Entry Point
|
|
3
|
+
* WASM + Worker toolkit for React & Web apps
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
ComputeKitOptions,
|
|
8
|
+
ComputeOptions,
|
|
9
|
+
ComputeResult,
|
|
10
|
+
PoolStats,
|
|
11
|
+
ComputeKitEvents,
|
|
12
|
+
} from './types';
|
|
13
|
+
|
|
14
|
+
import { WorkerPool } from './pool';
|
|
15
|
+
import { EventEmitter, isWasmSupported, createLogger } from './utils';
|
|
16
|
+
|
|
17
|
+
const logger = createLogger('ComputeKit');
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* ComputeKit - The main class for managing compute operations
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { ComputeKit } from '@computekit/core';
|
|
25
|
+
*
|
|
26
|
+
* const kit = new ComputeKit();
|
|
27
|
+
*
|
|
28
|
+
* // Register a compute function
|
|
29
|
+
* kit.register('fibonacci', (n: number) => {
|
|
30
|
+
* if (n <= 1) return n;
|
|
31
|
+
* let a = 0, b = 1;
|
|
32
|
+
* for (let i = 2; i <= n; i++) {
|
|
33
|
+
* [a, b] = [b, a + b];
|
|
34
|
+
* }
|
|
35
|
+
* return b;
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Execute the function
|
|
39
|
+
* const result = await kit.run('fibonacci', 50);
|
|
40
|
+
* console.log(result); // 12586269025
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export class ComputeKit extends EventEmitter<ComputeKitEvents> {
|
|
44
|
+
private pool: WorkerPool;
|
|
45
|
+
|
|
46
|
+
constructor(options: ComputeKitOptions = {}) {
|
|
47
|
+
super();
|
|
48
|
+
this.pool = new WorkerPool(options);
|
|
49
|
+
logger.debug('ComputeKit initialized', options);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Initialize ComputeKit
|
|
54
|
+
* Called automatically on first run, but can be called manually for eager initialization
|
|
55
|
+
*/
|
|
56
|
+
async initialize(): Promise<void> {
|
|
57
|
+
await this.pool.initialize();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Register a compute function
|
|
62
|
+
*
|
|
63
|
+
* @param name - Unique name for the function
|
|
64
|
+
* @param fn - The function to execute (will run in a Web Worker)
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* kit.register('sum', (arr: number[]) => arr.reduce((a, b) => a + b, 0));
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
register<TInput, TOutput>(
|
|
72
|
+
name: string,
|
|
73
|
+
fn: (input: TInput) => TOutput | Promise<TOutput>
|
|
74
|
+
): this {
|
|
75
|
+
this.pool.register(name, fn);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Execute a registered compute function
|
|
81
|
+
*
|
|
82
|
+
* @param name - Name of the registered function
|
|
83
|
+
* @param input - Input data for the function
|
|
84
|
+
* @param options - Execution options
|
|
85
|
+
* @returns Promise resolving to the function result
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const sum = await kit.run('sum', [1, 2, 3, 4, 5]);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
async run<TInput, TOutput>(
|
|
93
|
+
name: string,
|
|
94
|
+
input: TInput,
|
|
95
|
+
options?: ComputeOptions
|
|
96
|
+
): Promise<TOutput> {
|
|
97
|
+
return this.pool.execute<TInput, TOutput>(name, input, options);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Execute a registered compute function with full result metadata
|
|
102
|
+
*
|
|
103
|
+
* @param name - Name of the registered function
|
|
104
|
+
* @param input - Input data for the function
|
|
105
|
+
* @param options - Execution options
|
|
106
|
+
* @returns Promise resolving to ComputeResult with metadata
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const result = await kit.runWithMetadata('sum', data);
|
|
111
|
+
* console.log(`Took ${result.duration}ms`);
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
async runWithMetadata<TInput, TOutput>(
|
|
115
|
+
name: string,
|
|
116
|
+
input: TInput,
|
|
117
|
+
options?: ComputeOptions
|
|
118
|
+
): Promise<ComputeResult<TOutput>> {
|
|
119
|
+
const startTime = performance.now();
|
|
120
|
+
const data = await this.pool.execute<TInput, TOutput>(name, input, options);
|
|
121
|
+
const duration = performance.now() - startTime;
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
data,
|
|
125
|
+
duration,
|
|
126
|
+
cached: false,
|
|
127
|
+
workerId: 'unknown', // Would need pool changes to track this
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get pool statistics
|
|
133
|
+
*/
|
|
134
|
+
getStats(): PoolStats {
|
|
135
|
+
return this.pool.getStats();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Check if WebAssembly is supported
|
|
140
|
+
*/
|
|
141
|
+
isWasmSupported(): boolean {
|
|
142
|
+
return isWasmSupported();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Terminate the worker pool and clean up resources
|
|
147
|
+
*/
|
|
148
|
+
async terminate(): Promise<void> {
|
|
149
|
+
await this.pool.terminate();
|
|
150
|
+
this.removeAllListeners();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Create a pre-configured ComputeKit instance
|
|
156
|
+
*/
|
|
157
|
+
export function createComputeKit(options?: ComputeKitOptions): ComputeKit {
|
|
158
|
+
return new ComputeKit(options);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Default shared instance
|
|
163
|
+
*/
|
|
164
|
+
let defaultInstance: ComputeKit | null = null;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get the default shared ComputeKit instance
|
|
168
|
+
*/
|
|
169
|
+
export function getDefaultInstance(): ComputeKit {
|
|
170
|
+
if (!defaultInstance) {
|
|
171
|
+
defaultInstance = new ComputeKit();
|
|
172
|
+
}
|
|
173
|
+
return defaultInstance;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Register a function on the default instance
|
|
178
|
+
*/
|
|
179
|
+
export function register<TInput, TOutput>(
|
|
180
|
+
name: string,
|
|
181
|
+
fn: (input: TInput) => TOutput | Promise<TOutput>
|
|
182
|
+
): void {
|
|
183
|
+
getDefaultInstance().register(name, fn);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Run a function on the default instance
|
|
188
|
+
*/
|
|
189
|
+
export async function run<TInput, TOutput>(
|
|
190
|
+
name: string,
|
|
191
|
+
input: TInput,
|
|
192
|
+
options?: ComputeOptions
|
|
193
|
+
): Promise<TOutput> {
|
|
194
|
+
return getDefaultInstance().run<TInput, TOutput>(name, input, options);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Re-export types
|
|
198
|
+
export type {
|
|
199
|
+
ComputeKitOptions,
|
|
200
|
+
ComputeOptions,
|
|
201
|
+
ComputeProgress,
|
|
202
|
+
ComputeResult,
|
|
203
|
+
ComputeFunction,
|
|
204
|
+
PoolStats,
|
|
205
|
+
WorkerInfo,
|
|
206
|
+
WasmModuleConfig,
|
|
207
|
+
ComputeKitEvents,
|
|
208
|
+
} from './types';
|
|
209
|
+
|
|
210
|
+
// Re-export utilities
|
|
211
|
+
export {
|
|
212
|
+
isWasmSupported,
|
|
213
|
+
isSharedArrayBufferAvailable,
|
|
214
|
+
getHardwareConcurrency,
|
|
215
|
+
findTransferables,
|
|
216
|
+
} from './utils';
|
|
217
|
+
|
|
218
|
+
// Re-export WASM utilities
|
|
219
|
+
export {
|
|
220
|
+
loadWasmModule,
|
|
221
|
+
loadAndInstantiate,
|
|
222
|
+
loadAssemblyScript,
|
|
223
|
+
wrapWasmExports,
|
|
224
|
+
getMemoryView,
|
|
225
|
+
copyToWasmMemory,
|
|
226
|
+
copyFromWasmMemory,
|
|
227
|
+
clearWasmCache,
|
|
228
|
+
getWasmCacheStats,
|
|
229
|
+
} from './wasm';
|
|
230
|
+
|
|
231
|
+
// Re-export pool
|
|
232
|
+
export { WorkerPool } from './pool';
|