@computekit/core 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/README.md +220 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # @computekit/core
2
+
3
+ The core library for ComputeKit - run heavy computations in Web Workers with WASM support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @computekit/core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { ComputeKit } from '@computekit/core';
15
+
16
+ // Create an instance
17
+ const kit = new ComputeKit();
18
+
19
+ // Register a compute function
20
+ kit.register('fibonacci', (n: number) => {
21
+ let a = 0,
22
+ b = 1;
23
+ for (let i = 0; i < n; i++) {
24
+ [a, b] = [b, a + b];
25
+ }
26
+ return a;
27
+ });
28
+
29
+ // Run it (non-blocking!)
30
+ const result = await kit.run('fibonacci', 50);
31
+ console.log(result); // 12586269025
32
+ ```
33
+
34
+ ## API Reference
35
+
36
+ ### `new ComputeKit(options?)`
37
+
38
+ Create a new ComputeKit instance.
39
+
40
+ ```typescript
41
+ const kit = new ComputeKit({
42
+ maxWorkers: 4, // Max workers (default: CPU cores)
43
+ timeout: 30000, // Default timeout in ms
44
+ debug: false, // Enable debug logging
45
+ useSharedMemory: true, // Use SharedArrayBuffer when available
46
+ });
47
+ ```
48
+
49
+ ### `kit.register(name, fn)`
50
+
51
+ Register a function to run in workers.
52
+
53
+ ```typescript
54
+ kit.register('sum', (numbers: number[]) => {
55
+ return numbers.reduce((a, b) => a + b, 0);
56
+ });
57
+
58
+ // Async functions work too
59
+ kit.register('fetchAndProcess', async (url: string) => {
60
+ const res = await fetch(url);
61
+ const data = await res.json();
62
+ return processData(data);
63
+ });
64
+ ```
65
+
66
+ ### `kit.run(name, input, options?)`
67
+
68
+ Execute a registered function.
69
+
70
+ ```typescript
71
+ const result = await kit.run('sum', [1, 2, 3, 4, 5]);
72
+ console.log(result); // 15
73
+ ```
74
+
75
+ **Options:**
76
+
77
+ ```typescript
78
+ await kit.run('task', data, {
79
+ timeout: 5000, // Timeout in ms
80
+ priority: 10, // Priority (0-10, higher = first)
81
+ signal: abortController.signal, // AbortSignal for cancellation
82
+ onProgress: (p) => {
83
+ // Progress callback
84
+ console.log(`${p.percent}%`);
85
+ },
86
+ });
87
+ ```
88
+
89
+ ### `kit.runWithMetadata(name, input, options?)`
90
+
91
+ Execute and get execution metadata.
92
+
93
+ ```typescript
94
+ const result = await kit.runWithMetadata('task', data);
95
+ console.log(result.data); // The result
96
+ console.log(result.duration); // Execution time in ms
97
+ console.log(result.workerId); // Which worker ran it
98
+ ```
99
+
100
+ ### `kit.getStats()`
101
+
102
+ Get worker pool statistics.
103
+
104
+ ```typescript
105
+ const stats = kit.getStats();
106
+ console.log(stats.activeWorkers); // Currently busy workers
107
+ console.log(stats.queueLength); // Tasks waiting
108
+ console.log(stats.tasksCompleted); // Total completed
109
+ ```
110
+
111
+ ### `kit.terminate()`
112
+
113
+ Terminate all workers and clean up.
114
+
115
+ ```typescript
116
+ await kit.terminate();
117
+ ```
118
+
119
+ ## WASM Support
120
+
121
+ Load and use WebAssembly modules:
122
+
123
+ ```typescript
124
+ import { loadWasmModule, loadAssemblyScript } from '@computekit/core';
125
+
126
+ // Load a WASM module
127
+ const module = await loadWasmModule('/path/to/module.wasm');
128
+
129
+ // Load AssemblyScript with default imports
130
+ const { exports } = await loadAssemblyScript('/as-module.wasm');
131
+ const result = exports.compute(data);
132
+ ```
133
+
134
+ ### WASM Utilities
135
+
136
+ ```typescript
137
+ import {
138
+ loadWasmModule,
139
+ loadAndInstantiate,
140
+ loadAssemblyScript,
141
+ getMemoryView,
142
+ copyToWasmMemory,
143
+ copyFromWasmMemory,
144
+ isWasmSupported,
145
+ } from '@computekit/core';
146
+
147
+ // Check support
148
+ if (isWasmSupported()) {
149
+ // Load and instantiate with custom imports
150
+ const { instance } = await loadAndInstantiate({
151
+ source: '/module.wasm',
152
+ imports: { env: { log: console.log } },
153
+ memory: { initial: 256, maximum: 512 },
154
+ });
155
+ }
156
+ ```
157
+
158
+ ## Events
159
+
160
+ ComputeKit emits events for monitoring:
161
+
162
+ ```typescript
163
+ kit.on('worker:created', (info) => console.log('Worker created:', info.id));
164
+ kit.on('worker:terminated', (info) => console.log('Worker terminated:', info.id));
165
+ kit.on('task:start', (taskId, name) => console.log('Task started:', name));
166
+ kit.on('task:complete', (taskId, duration) => console.log('Done in', duration, 'ms'));
167
+ kit.on('task:error', (taskId, error) => console.error('Task failed:', error));
168
+ kit.on('task:progress', (taskId, progress) => console.log(progress.percent, '%'));
169
+ ```
170
+
171
+ ## Error Handling
172
+
173
+ ```typescript
174
+ try {
175
+ await kit.run('task', data);
176
+ } catch (error) {
177
+ if (error.message.includes('not registered')) {
178
+ // Function not found
179
+ } else if (error.message.includes('timed out')) {
180
+ // Timeout exceeded
181
+ } else if (error.message.includes('aborted')) {
182
+ // Cancelled via AbortSignal
183
+ }
184
+ }
185
+ ```
186
+
187
+ ## Cancellation
188
+
189
+ ```typescript
190
+ const controller = new AbortController();
191
+
192
+ // Start a long task
193
+ const promise = kit.run('heavyTask', data, {
194
+ signal: controller.signal,
195
+ });
196
+
197
+ // Cancel it
198
+ controller.abort();
199
+
200
+ try {
201
+ await promise;
202
+ } catch (error) {
203
+ // error.message contains 'aborted'
204
+ }
205
+ ```
206
+
207
+ ## TypeScript
208
+
209
+ Full type safety:
210
+
211
+ ```typescript
212
+ // Generic types flow through
213
+ kit.register('double', (n: number) => n * 2);
214
+ const result = await kit.run<number, number>('double', 21);
215
+ // result is typed as number
216
+ ```
217
+
218
+ ## License
219
+
220
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@computekit/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Core WASM + Worker toolkit for running heavy computations without blocking the UI",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -20,7 +20,8 @@
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
- "src"
23
+ "src",
24
+ "README.md"
24
25
  ],
25
26
  "scripts": {
26
27
  "build": "tsup",