@hybrid-compute/core 0.0.14 → 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/CHANGELOG.md +26 -0
- package/README.md +278 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.0](https://github.com/phun-ky/hybrid-compute/compare/@hybrid-compute/core@0.1.0...@hybrid-compute/core@0.2.0) (2025-07-07)
|
|
4
|
+
|
|
5
|
+
### Tasks
|
|
6
|
+
|
|
7
|
+
* 🤖 @hybrid-compute/local@0.1.0 ([14599e9](https://github.com/phun-ky/hybrid-compute/commit/14599e91ec059f0f611e8ccbd444bf0b64b1f503))
|
|
8
|
+
* 🤖 @hybrid-compute/remote@0.1.0 ([59292bd](https://github.com/phun-ky/hybrid-compute/commit/59292bd169fb0dbeff24c44183b2ae8d3fe37f3f))
|
|
9
|
+
* 🤖 @hybrid-compute/worker@0.1.0 ([45b7f8c](https://github.com/phun-ky/hybrid-compute/commit/45b7f8ce9453aca1e3b3a3fc3b16cec36305a2dc))
|
|
10
|
+
* 🤖 Add more keywords ([c5646a2](https://github.com/phun-ky/hybrid-compute/commit/c5646a20df3df7f005b08ab1216b987f1ec0430c))
|
|
11
|
+
* 🤖 bump the minor-and-patch group across 1 directory with 7 updates ([2f5d9d6](https://github.com/phun-ky/hybrid-compute/commit/2f5d9d63cb7714c95ac0f69f37446f8497c891cc))
|
|
12
|
+
|
|
13
|
+
## [0.1.0](https://github.com/phun-ky/hybrid-compute/compare/@hybrid-compute/core@0.0.14...@hybrid-compute/core@0.1.0) (2025-06-23)
|
|
14
|
+
|
|
15
|
+
### Tasks
|
|
16
|
+
|
|
17
|
+
* 🤖 @hybrid-compute/local@0.0.13 ([b6bf061](https://github.com/phun-ky/hybrid-compute/commit/b6bf0617bb3b5a8dfc22fddf634e07698f3df13e))
|
|
18
|
+
* 🤖 @hybrid-compute/remote@0.0.13 ([c349cb9](https://github.com/phun-ky/hybrid-compute/commit/c349cb9b1ecf1ec2a1cf6565f84601f28f6143d6))
|
|
19
|
+
* 🤖 @hybrid-compute/worker@0.0.13 ([290e15a](https://github.com/phun-ky/hybrid-compute/commit/290e15a7472062ec44a7b55d185dc94d38bb317d))
|
|
20
|
+
|
|
21
|
+
### Documentation
|
|
22
|
+
|
|
23
|
+
* ✏️ Add more examples ([d874e1d](https://github.com/phun-ky/hybrid-compute/commit/d874e1d044eadf94c6641e2e01b134fd04ec0799))
|
|
24
|
+
|
|
25
|
+
### Feature
|
|
26
|
+
|
|
27
|
+
* 🎸 Add svg logo ([ea5f9d2](https://github.com/phun-ky/hybrid-compute/commit/ea5f9d23fedda902ca3344e0b207bb7b2c647020))
|
|
28
|
+
|
|
3
29
|
## [0.0.14](https://github.com/phun-ky/hybrid-compute/compare/@hybrid-compute/core@0.0.13...@hybrid-compute/core@0.0.14) (2025-06-23)
|
|
4
30
|
|
|
5
31
|
### Tasks
|
package/README.md
CHANGED
|
@@ -41,6 +41,284 @@ npm install @hybrid-compute/core
|
|
|
41
41
|
import { HybridCompute } from '@hybrid-compute/core';
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
### Usage with local
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install @hybrid-compute/core
|
|
48
|
+
npm install @hybrid-compute/local
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { createHybridCompute } from '@hybrid-compute/core';
|
|
53
|
+
import { createLocalCompute } from '@hybrid-compute/local';
|
|
54
|
+
|
|
55
|
+
// Define a simple echo task
|
|
56
|
+
const echoTask = {
|
|
57
|
+
name: 'echo',
|
|
58
|
+
async run(input: string): Promise<string> {
|
|
59
|
+
return `Echo: ${input}`;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Set up the local backend and register the task
|
|
64
|
+
const local = createLocalCompute();
|
|
65
|
+
local.registerTask(echoTask);
|
|
66
|
+
|
|
67
|
+
// Set up the HybridCompute instance with only the local backend
|
|
68
|
+
const compute = createHybridCompute({
|
|
69
|
+
local
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Run the task
|
|
73
|
+
async function main() {
|
|
74
|
+
try {
|
|
75
|
+
const result = await compute.runTask('echo', 'Hello from HybridCompute!');
|
|
76
|
+
console.log(result); // Output: Echo: Hello from HybridCompute!
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error('Task failed:', error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Usage with remote
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install @hybrid-compute/core
|
|
89
|
+
npm install @hybrid-compute/remote
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Fetch
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { createHybridCompute } from '@hybrid-compute/core';
|
|
96
|
+
import { createRemoteCompute } from '@hybrid-compute/remote';
|
|
97
|
+
|
|
98
|
+
const remote = createRemoteCompute({
|
|
99
|
+
transport: 'fetch',
|
|
100
|
+
endpoint: 'https://your-remote-service.com/compute',
|
|
101
|
+
canRunTasks: ['echo']
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const compute = createHybridCompute({
|
|
105
|
+
remote
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
async function main() {
|
|
109
|
+
try {
|
|
110
|
+
const result = await compute.runTask('echo', 'Remote via fetch!');
|
|
111
|
+
console.log(result); // Output from remote server
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error('Task failed:', error);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
main();
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
##### Example server for the remote fetch example
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
import express from 'express';
|
|
124
|
+
|
|
125
|
+
const app = express();
|
|
126
|
+
const port = 3000;
|
|
127
|
+
|
|
128
|
+
// Middleware to parse JSON
|
|
129
|
+
app.use(express.json());
|
|
130
|
+
|
|
131
|
+
// Dummy task implementations
|
|
132
|
+
const tasks = {
|
|
133
|
+
echo: async (input) => {
|
|
134
|
+
return `Echo from remote server: ${input}`;
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
// Example additional task
|
|
138
|
+
reverse: async (input) => {
|
|
139
|
+
if (typeof input !== 'string') throw new Error('Input must be a string');
|
|
140
|
+
return input.split('').reverse().join('');
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Route to handle compute requests
|
|
145
|
+
app.post('/compute', async (req, res) => {
|
|
146
|
+
const { task, input } = req.body;
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
if (!task || !tasks[task]) {
|
|
150
|
+
throw new Error(`Unknown task '${task}'`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const result = await tasks[task](input);
|
|
154
|
+
res.json({ result });
|
|
155
|
+
} catch (error) {
|
|
156
|
+
res.status(400).json({ error: error.message });
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
app.listen(port, () => {
|
|
161
|
+
console.log(
|
|
162
|
+
`🧠 RemoteCompute server listening at http://localhost:${port}/compute`
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
This can be tested locally:
|
|
168
|
+
|
|
169
|
+
```curl
|
|
170
|
+
curl -X POST http://localhost:3000/compute \
|
|
171
|
+
-H "Content-Type: application/json" \
|
|
172
|
+
-d '{"task": "echo", "input": "hello"}'
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### WebSocket
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import { createHybridCompute } from '@hybrid-compute/core';
|
|
179
|
+
import { createRemoteCompute } from '@hybrid-compute/remote';
|
|
180
|
+
|
|
181
|
+
const remote = createRemoteCompute({
|
|
182
|
+
transport: 'websocket',
|
|
183
|
+
endpoint: 'wss://your-remote-service.com/socket',
|
|
184
|
+
canRunTasks: ['echo']
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const compute = createHybridCompute({
|
|
188
|
+
remote
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
async function main() {
|
|
192
|
+
try {
|
|
193
|
+
const result = await compute.runTask('echo', 'Remote via WebSocket!');
|
|
194
|
+
console.log(result); // Output from remote service
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('Task failed:', error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
main();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
##### Example server for the remote WebSocket example
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
import { WebSocketServer } from 'ws';
|
|
207
|
+
import http from 'http';
|
|
208
|
+
|
|
209
|
+
// Simple task definitions
|
|
210
|
+
const tasks = {
|
|
211
|
+
echo: async (input) => {
|
|
212
|
+
return `Echo from WebSocket server: ${input}`;
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
reverse: async (input) => {
|
|
216
|
+
if (typeof input !== 'string') throw new Error('Input must be a string');
|
|
217
|
+
return input.split('').reverse().join('');
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Create an HTTP server to attach WebSocket
|
|
222
|
+
const server = http.createServer();
|
|
223
|
+
const wss = new WebSocketServer({ server });
|
|
224
|
+
|
|
225
|
+
wss.on('connection', (ws) => {
|
|
226
|
+
ws.on('message', async (message) => {
|
|
227
|
+
let request;
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
request = JSON.parse(message.toString());
|
|
231
|
+
} catch (err) {
|
|
232
|
+
ws.send(JSON.stringify({ id: null, error: 'Invalid JSON' }));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const { id, task, input } = request;
|
|
237
|
+
|
|
238
|
+
if (!id || typeof task !== 'string') {
|
|
239
|
+
ws.send(JSON.stringify({ id, error: 'Missing or invalid task name' }));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
if (!tasks[task]) throw new Error(`Unknown task '${task}'`);
|
|
245
|
+
|
|
246
|
+
const result = await tasks[task](input);
|
|
247
|
+
ws.send(JSON.stringify({ id, result }));
|
|
248
|
+
} catch (error) {
|
|
249
|
+
ws.send(JSON.stringify({ id, error: error.message }));
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
server.listen(3001, () => {
|
|
255
|
+
console.log(
|
|
256
|
+
'🔌 WebSocket RemoteCompute server listening at ws://localhost:3001'
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Usage with worker
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
npm install @hybrid-compute/core
|
|
265
|
+
npm install @hybrid-compute/worker
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Create a file named `worker.ts`:
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
self.onmessage = async (event) => {
|
|
272
|
+
const { task, input, id } = event.data;
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
let result;
|
|
276
|
+
|
|
277
|
+
switch (task) {
|
|
278
|
+
case 'echo':
|
|
279
|
+
result = `Echo from Worker: ${input}`;
|
|
280
|
+
break;
|
|
281
|
+
|
|
282
|
+
// Add more cases for additional tasks
|
|
283
|
+
default:
|
|
284
|
+
throw new Error(`Unknown task: ${task}`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
self.postMessage({ id, result });
|
|
288
|
+
} catch (error) {
|
|
289
|
+
self.postMessage({ id, error: (error as Error).message });
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
And your `main.ts`:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import { createHybridCompute } from '@hybrid-compute/core';
|
|
298
|
+
import { createThreadedCompute } from '@hybrid-compute/worker';
|
|
299
|
+
|
|
300
|
+
// Worker must be served as a module
|
|
301
|
+
const worker = createThreadedCompute(
|
|
302
|
+
new URL('./worker.ts', import.meta.url).href,
|
|
303
|
+
['echo']
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
const compute = createHybridCompute({
|
|
307
|
+
worker
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
const main = async () => {
|
|
311
|
+
try {
|
|
312
|
+
const result = await compute.runTask('echo', 'Hello from Worker!');
|
|
313
|
+
console.log(result); // Echo from Worker: Hello from Worker!
|
|
314
|
+
} catch (error) {
|
|
315
|
+
console.error('Task failed:', error);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
main();
|
|
320
|
+
```
|
|
321
|
+
|
|
44
322
|
---
|
|
45
323
|
|
|
46
324
|
## Contributing
|