Skip to content

Commit d8a4180

Browse files
Puter.js workers api (#1339)
1 parent 858929a commit d8a4180

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/puter-js/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Perms from './modules/Perms.js';
2525
import { pFetch } from './modules/networking/requests.js';
2626
import localStorageMemory from './lib/polyfills/localStorage.js'
2727
import xhrshim from './lib/polyfills/xhrshim.js'
28+
import { WorkersHandler } from './modules/Workers.js';
2829

2930
// TODO: This is for a safe-guard below; we should check if we can
3031
// generalize this behavior rather than hard-coding it.
@@ -378,6 +379,8 @@ export default globalThis.puter = (function() {
378379
},
379380
fetch: pFetch
380381
}
382+
383+
this.workers = new WorkersHandler(this.authToken);
381384
}
382385

383386
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export class WorkersHandler {
2+
3+
constructor(authToken) {
4+
this.authToken = authToken;
5+
}
6+
7+
async create(workerName, filePath) {
8+
const data = await puter.fs.read(filePath).then(r => r.text());
9+
let currentWorkers = await puter.kv.get("user-workers");
10+
if (!currentWorkers) {
11+
currentWorkers = {};
12+
}
13+
14+
const driverCall = await puter.drivers.call("workers", "worker-service", "create", { authorization: puter.authToken, fileData: data, workerName });
15+
const driverResult = JSON.parse(driverCall.result);
16+
if (!driverCall.success || !driverResult.success) {
17+
throw new Error(driverResult?.errors || "Driver failed to execute, do you have the necessary permissions?");
18+
}
19+
currentWorkers[workerName] = { filePath, url: driverResult["url"], deployTime: Date.now() };
20+
await puter.kv.set("user-workers", currentWorkers);
21+
22+
return driverResult;
23+
}
24+
25+
// This is temporary until FS stuff is hooked properly
26+
async update(workerName) {
27+
let filePath = (await puter.kv.get("user-workers"))[workerName]["filePath"];
28+
return this.create(workerName, filePath);
29+
}
30+
31+
async list() {
32+
return await puter.kv.get("user-workers");
33+
}
34+
35+
async get(workerName) {
36+
try {
37+
return (await puter.kv.get("user-workers"))[workerName].url;
38+
} catch (e) {
39+
throw new Error("Failed to get worker");
40+
}
41+
}
42+
43+
async delete(workerName) {
44+
const driverCall = await puter.drivers.call("workers", "worker-service", "destroy", { authorization: puter.authToken, workerName });
45+
46+
if (!driverCall.success || !driverCall.result.result) {
47+
if (!driverCall.result.result) {
48+
throw new Error("Worker doesn't exist");
49+
}
50+
throw new Error(driverResult?.errors || "Driver failed to execute, do you have the necessary permissions?");
51+
} else {
52+
let currentWorkers = await puter.kv.get("user-workers");
53+
54+
if (!currentWorkers) {
55+
currentWorkers = {};
56+
}
57+
delete currentWorkers[workerName];
58+
59+
await puter.kv.set("user-workers", currentWorkers);
60+
return true;
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)