@hazae41/bobine 0.0.23 → 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.
- package/README.md +5 -197
- package/out/libs/packed/mod.d.ts +5 -5
- package/out/libs/packed/mod.js +42 -42
- package/out/libs/sql/mod.d.ts +2 -2
- package/out/libs/sql/mod.js +2 -2
- package/out/mod.d.ts +0 -2
- package/out/mod.js +0 -24
- package/out/mods/bobine/errors/mod.d.ts +8 -0
- package/out/mods/bobine/errors/mod.js +12 -0
- package/out/mods/bobine/mod.d.ts +3 -0
- package/out/mods/bobine/mod.js +3 -0
- package/out/mods/bobine/runner/mod.d.ts +12 -0
- package/out/mods/bobine/runner/mod.js +447 -0
- package/out/mods/bobine/worker/bin.d.ts +12 -0
- package/out/mods/bobine/worker/bin.js +67 -0
- package/out/mods/bobine/worker/mod.d.ts +10 -0
- package/out/mods/bobine/worker/mod.js +105 -0
- package/out/mods/mod.d.ts +1 -1
- package/out/mods/mod.js +1 -1
- package/package.json +11 -17
- package/out/libs/database/mod.d.ts +0 -1
- package/out/libs/database/mod.js +0 -1
- package/out/mods/config/mod.d.ts +0 -12
- package/out/mods/config/mod.js +0 -1
- package/out/mods/helper/bin.d.ts +0 -1
- package/out/mods/helper/bin.js +0 -78
- package/out/mods/server/bin.d.ts +0 -1
- package/out/mods/server/bin.js +0 -72
- package/out/mods/server/mod.d.ts +0 -9
- package/out/mods/server/mod.js +0 -314
- package/out/mods/worker/bin.d.ts +0 -1
- package/out/mods/worker/bin.js +0 -464
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
// deno-lint-ignore-file no-unused-vars no-explicit-any
|
|
2
|
+
import { meter } from "../../../libs/metering/mod.js";
|
|
3
|
+
import { Packed } from "../../../libs/packed/mod.js";
|
|
4
|
+
import { runAsImmediate } from "../../../libs/sql/mod.js";
|
|
5
|
+
import { RpcPaymentError } from "../errors/mod.js";
|
|
6
|
+
import { Readable, Writable } from "@hazae41/binary";
|
|
7
|
+
import * as Wasm from "@hazae41/wasm";
|
|
8
|
+
import { Buffer } from "node:buffer";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import process from "node:process";
|
|
12
|
+
export class Runner {
|
|
13
|
+
uuid;
|
|
14
|
+
path;
|
|
15
|
+
data;
|
|
16
|
+
constructor(uuid, path, data) {
|
|
17
|
+
this.uuid = uuid;
|
|
18
|
+
this.path = path;
|
|
19
|
+
this.data = data;
|
|
20
|
+
}
|
|
21
|
+
async setup() {
|
|
22
|
+
mkdirSync(join(this.path, "."), { recursive: true });
|
|
23
|
+
mkdirSync(join(this.path, "scripts"), { recursive: true });
|
|
24
|
+
await this.data.exec(`CREATE TABLE IF NOT EXISTS events (
|
|
25
|
+
nonce INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
26
|
+
|
|
27
|
+
moment INTEGER NOT NULL,
|
|
28
|
+
|
|
29
|
+
module TEXT NOT NULL,
|
|
30
|
+
|
|
31
|
+
key BLOB NOT NULL,
|
|
32
|
+
|
|
33
|
+
value BLOB NOT NULL
|
|
34
|
+
);`);
|
|
35
|
+
await this.data.exec(`CREATE TABLE IF NOT EXISTS moments (
|
|
36
|
+
nonce INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
37
|
+
|
|
38
|
+
epoch INTEGER NOT NULL,
|
|
39
|
+
|
|
40
|
+
module TEXT NOT NULL,
|
|
41
|
+
|
|
42
|
+
method TEXT NOT NULL,
|
|
43
|
+
|
|
44
|
+
params BLOB NOT NULL
|
|
45
|
+
);`);
|
|
46
|
+
await this.data.close();
|
|
47
|
+
}
|
|
48
|
+
async create(code, salt) {
|
|
49
|
+
const pack = Writable.writeToBytes(new Packed([code, salt]));
|
|
50
|
+
const digestOfCodeAsRaw = new Uint8Array(await crypto.subtle.digest("SHA-256", code));
|
|
51
|
+
const digestOfPackAsRaw = new Uint8Array(await crypto.subtle.digest("SHA-256", pack));
|
|
52
|
+
const digestOfCodeAsHex = digestOfCodeAsRaw.toHex();
|
|
53
|
+
const digestOfPackAsHex = digestOfPackAsRaw.toHex();
|
|
54
|
+
if (!existsSync(join(this.path, "scripts", `${digestOfCodeAsHex}.wasm`)))
|
|
55
|
+
writeFileSync(join(this.path, "scripts", `${digestOfCodeAsHex}.wasm`), code);
|
|
56
|
+
if (!existsSync(join(this.path, "scripts", `${digestOfPackAsHex}.wasm`)))
|
|
57
|
+
symlinkSync(`./${digestOfCodeAsHex}.wasm`, join(this.path, "scripts", `${digestOfPackAsHex}.wasm`), "file");
|
|
58
|
+
return digestOfPackAsHex;
|
|
59
|
+
}
|
|
60
|
+
async execute(module, method, params, target) {
|
|
61
|
+
return await this.#run(module, method, params, target, 1);
|
|
62
|
+
}
|
|
63
|
+
async simulate(module, method, params, target) {
|
|
64
|
+
return await this.#run(module, method, params, target, 2);
|
|
65
|
+
}
|
|
66
|
+
async #run(module, method, params, target, mode) {
|
|
67
|
+
let sparks = 0;
|
|
68
|
+
const exports = {};
|
|
69
|
+
const logs = new Array();
|
|
70
|
+
const caches = new Map();
|
|
71
|
+
const reads = new Array();
|
|
72
|
+
const writes = new Array();
|
|
73
|
+
const encode = (value) => {
|
|
74
|
+
return Writable.writeToBytes(new Packed(value));
|
|
75
|
+
};
|
|
76
|
+
const decode = (bytes) => {
|
|
77
|
+
return Readable.readFromBytes(Packed, bytes);
|
|
78
|
+
};
|
|
79
|
+
const consume = (amount) => {
|
|
80
|
+
sparks += amount;
|
|
81
|
+
if (process.env.NODE_ENV === "development")
|
|
82
|
+
return;
|
|
83
|
+
if (sparks > target)
|
|
84
|
+
throw new RpcPaymentError("Out of sparks");
|
|
85
|
+
return;
|
|
86
|
+
};
|
|
87
|
+
const load = (module) => {
|
|
88
|
+
const current = {};
|
|
89
|
+
caches.set(module, new Map());
|
|
90
|
+
const imports = {};
|
|
91
|
+
imports["env"] = {
|
|
92
|
+
mode: mode,
|
|
93
|
+
abort: (...args) => {
|
|
94
|
+
return;
|
|
95
|
+
},
|
|
96
|
+
uuid: () => {
|
|
97
|
+
return this.uuid;
|
|
98
|
+
},
|
|
99
|
+
panic: (message) => {
|
|
100
|
+
throw new Error(message);
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
imports["sparks"] = {
|
|
104
|
+
remaining: () => {
|
|
105
|
+
return target - BigInt(sparks);
|
|
106
|
+
},
|
|
107
|
+
consume: (amount) => {
|
|
108
|
+
consume(amount >>> 0);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
imports["console"] = {
|
|
112
|
+
log: (text) => {
|
|
113
|
+
logs.push(text);
|
|
114
|
+
console.log(text);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
imports["blobs"] = {
|
|
119
|
+
save: (offset, length) => {
|
|
120
|
+
const { memory } = current.instance.exports;
|
|
121
|
+
const view = new Uint8Array(memory.buffer, offset >>> 0, length >>> 0);
|
|
122
|
+
return view.slice();
|
|
123
|
+
},
|
|
124
|
+
load: (blob, offset) => {
|
|
125
|
+
const { memory } = current.instance.exports;
|
|
126
|
+
const view = new Uint8Array(memory.buffer, offset >>> 0, blob.length);
|
|
127
|
+
view.set(blob);
|
|
128
|
+
},
|
|
129
|
+
length: (blob) => {
|
|
130
|
+
return blob.length;
|
|
131
|
+
},
|
|
132
|
+
concat: (left, right) => {
|
|
133
|
+
const concat = new Uint8Array(left.length + right.length);
|
|
134
|
+
concat.set(left, 0);
|
|
135
|
+
concat.set(right, left.length);
|
|
136
|
+
return concat;
|
|
137
|
+
},
|
|
138
|
+
equals: (left, right) => {
|
|
139
|
+
return !Buffer.compare(left, right);
|
|
140
|
+
},
|
|
141
|
+
includes: (haystack, needle) => {
|
|
142
|
+
return haystack.toHex().includes(needle.toHex());
|
|
143
|
+
},
|
|
144
|
+
slice: (blob, start, end) => {
|
|
145
|
+
return blob.slice(start >>> 0, end >>> 0);
|
|
146
|
+
},
|
|
147
|
+
from_base16: (text) => {
|
|
148
|
+
return Uint8Array.fromHex(text);
|
|
149
|
+
},
|
|
150
|
+
to_base16: (blob) => {
|
|
151
|
+
return blob.toHex();
|
|
152
|
+
},
|
|
153
|
+
from_base64: (text) => {
|
|
154
|
+
return Uint8Array.fromBase64(text);
|
|
155
|
+
},
|
|
156
|
+
to_base64: (blob) => {
|
|
157
|
+
return blob.toBase64();
|
|
158
|
+
},
|
|
159
|
+
encode: (value) => {
|
|
160
|
+
return encode(value);
|
|
161
|
+
},
|
|
162
|
+
decode: (blob) => {
|
|
163
|
+
return decode(blob);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
imports["texts"] = {
|
|
167
|
+
length: (text) => {
|
|
168
|
+
return new TextEncoder().encode(text).length;
|
|
169
|
+
},
|
|
170
|
+
concat: (left, right) => {
|
|
171
|
+
return left + right;
|
|
172
|
+
},
|
|
173
|
+
equals: (left, right) => {
|
|
174
|
+
return left === right;
|
|
175
|
+
},
|
|
176
|
+
includes: (haystack, needle) => {
|
|
177
|
+
return haystack.includes(needle);
|
|
178
|
+
},
|
|
179
|
+
slice: (text, start, end) => {
|
|
180
|
+
return text.slice(start >>> 0, end >>> 0);
|
|
181
|
+
},
|
|
182
|
+
from_utf8: (bytes) => {
|
|
183
|
+
return new TextDecoder().decode(bytes);
|
|
184
|
+
},
|
|
185
|
+
to_utf8: (text) => {
|
|
186
|
+
return new TextEncoder().encode(text);
|
|
187
|
+
},
|
|
188
|
+
to_uppercase: (text) => {
|
|
189
|
+
return text.toUpperCase();
|
|
190
|
+
},
|
|
191
|
+
to_lowercase: (text) => {
|
|
192
|
+
return text.toLowerCase();
|
|
193
|
+
},
|
|
194
|
+
trim: (text) => {
|
|
195
|
+
return text.trim();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
imports["packs"] = {
|
|
199
|
+
create: (...values) => {
|
|
200
|
+
return values;
|
|
201
|
+
},
|
|
202
|
+
concat: (left, right) => {
|
|
203
|
+
return [...left, ...right];
|
|
204
|
+
},
|
|
205
|
+
length: (pack) => {
|
|
206
|
+
return pack.length;
|
|
207
|
+
},
|
|
208
|
+
get(pack, index) {
|
|
209
|
+
const value = pack[index >>> 0];
|
|
210
|
+
if (value === undefined)
|
|
211
|
+
throw new Error("Not found");
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
imports["bigints"] = {
|
|
216
|
+
identity: (value) => {
|
|
217
|
+
return value;
|
|
218
|
+
},
|
|
219
|
+
zero: () => {
|
|
220
|
+
return 0n;
|
|
221
|
+
},
|
|
222
|
+
one: () => {
|
|
223
|
+
return 1n;
|
|
224
|
+
},
|
|
225
|
+
two: () => {
|
|
226
|
+
return 2n;
|
|
227
|
+
},
|
|
228
|
+
three: () => {
|
|
229
|
+
return 3n;
|
|
230
|
+
},
|
|
231
|
+
four: () => {
|
|
232
|
+
return 4n;
|
|
233
|
+
},
|
|
234
|
+
five: () => {
|
|
235
|
+
return 5n;
|
|
236
|
+
},
|
|
237
|
+
six: () => {
|
|
238
|
+
return 6n;
|
|
239
|
+
},
|
|
240
|
+
seven: () => {
|
|
241
|
+
return 7n;
|
|
242
|
+
},
|
|
243
|
+
eight: () => {
|
|
244
|
+
return 8n;
|
|
245
|
+
},
|
|
246
|
+
nine: () => {
|
|
247
|
+
return 9n;
|
|
248
|
+
},
|
|
249
|
+
ten: () => {
|
|
250
|
+
return 10n;
|
|
251
|
+
},
|
|
252
|
+
inc: (value) => {
|
|
253
|
+
return value + 1n;
|
|
254
|
+
},
|
|
255
|
+
dec: (value) => {
|
|
256
|
+
return value - 1n;
|
|
257
|
+
},
|
|
258
|
+
neg: (value) => {
|
|
259
|
+
return -value;
|
|
260
|
+
},
|
|
261
|
+
add: (left, right) => {
|
|
262
|
+
return left + right;
|
|
263
|
+
},
|
|
264
|
+
sub: (left, right) => {
|
|
265
|
+
return left - right;
|
|
266
|
+
},
|
|
267
|
+
mul: (left, right) => {
|
|
268
|
+
return left * right;
|
|
269
|
+
},
|
|
270
|
+
div: (left, right) => {
|
|
271
|
+
return left / right;
|
|
272
|
+
},
|
|
273
|
+
pow: (left, right) => {
|
|
274
|
+
return left ** right;
|
|
275
|
+
},
|
|
276
|
+
mod: (left, right) => {
|
|
277
|
+
return left % right;
|
|
278
|
+
},
|
|
279
|
+
lt: (left, right) => {
|
|
280
|
+
return left < right;
|
|
281
|
+
},
|
|
282
|
+
lte: (left, right) => {
|
|
283
|
+
return left <= right;
|
|
284
|
+
},
|
|
285
|
+
gt: (left, right) => {
|
|
286
|
+
return left > right;
|
|
287
|
+
},
|
|
288
|
+
gte: (left, right) => {
|
|
289
|
+
return left >= right;
|
|
290
|
+
},
|
|
291
|
+
eq: (left, right) => {
|
|
292
|
+
return left === right;
|
|
293
|
+
},
|
|
294
|
+
neq: (left, right) => {
|
|
295
|
+
return left !== right;
|
|
296
|
+
},
|
|
297
|
+
min: (left, right) => {
|
|
298
|
+
return left < right ? left : right;
|
|
299
|
+
},
|
|
300
|
+
max: (left, right) => {
|
|
301
|
+
return left > right ? left : right;
|
|
302
|
+
},
|
|
303
|
+
from_base16: (text) => {
|
|
304
|
+
return BigInt("0x" + text);
|
|
305
|
+
},
|
|
306
|
+
to_base16: (bigint) => {
|
|
307
|
+
return bigint.toString(16);
|
|
308
|
+
},
|
|
309
|
+
from_base10: (text) => {
|
|
310
|
+
return BigInt(text);
|
|
311
|
+
},
|
|
312
|
+
to_base10: (bigint) => {
|
|
313
|
+
return bigint.toString();
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
imports["modules"] = {
|
|
317
|
+
self: () => {
|
|
318
|
+
return module;
|
|
319
|
+
},
|
|
320
|
+
load: (module) => {
|
|
321
|
+
return readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
322
|
+
},
|
|
323
|
+
call: (module, method, params) => {
|
|
324
|
+
if (exports[module] == null)
|
|
325
|
+
load(module);
|
|
326
|
+
if (typeof exports[module][method] !== "function")
|
|
327
|
+
throw new Error("Not found");
|
|
328
|
+
return [exports[module][method](...params)];
|
|
329
|
+
},
|
|
330
|
+
create: async (code, salt) => {
|
|
331
|
+
consume(code.length + salt.length);
|
|
332
|
+
return await this.create(code, salt);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
imports["storage"] = {
|
|
336
|
+
set: (key, fresh) => {
|
|
337
|
+
const cache = caches.get(module);
|
|
338
|
+
cache.set(key, fresh);
|
|
339
|
+
const keyAsRaw = encode(key);
|
|
340
|
+
const valueAsRaw = encode(fresh);
|
|
341
|
+
writes.push([module, keyAsRaw, valueAsRaw]);
|
|
342
|
+
return;
|
|
343
|
+
},
|
|
344
|
+
get: async (key) => {
|
|
345
|
+
const cache = caches.get(module);
|
|
346
|
+
const stale = cache.get(key);
|
|
347
|
+
if (stale != null)
|
|
348
|
+
return [stale];
|
|
349
|
+
const keyAsRaw = encode(key);
|
|
350
|
+
const row = await this.data.get(`SELECT value FROM events event WHERE event.module = ? AND event.key = ? ORDER BY event.nonce DESC LIMIT 1;`, module, keyAsRaw);
|
|
351
|
+
if (row == null)
|
|
352
|
+
return null;
|
|
353
|
+
const valueAsRaw = new Uint8Array(row.value);
|
|
354
|
+
reads.push([module, keyAsRaw, valueAsRaw]);
|
|
355
|
+
const fresh = decode(valueAsRaw);
|
|
356
|
+
cache.set(key, fresh);
|
|
357
|
+
return [fresh];
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
imports["symbols"] = {
|
|
361
|
+
create: () => {
|
|
362
|
+
return Symbol();
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
const refs = new Array();
|
|
366
|
+
const ptrs = new Map();
|
|
367
|
+
imports["refs"] = {
|
|
368
|
+
numerize: (ref) => {
|
|
369
|
+
const stale = ptrs.get(ref);
|
|
370
|
+
if (stale != null)
|
|
371
|
+
return stale;
|
|
372
|
+
const fresh = refs.push(ref) - 1;
|
|
373
|
+
ptrs.set(ref, fresh);
|
|
374
|
+
return fresh;
|
|
375
|
+
},
|
|
376
|
+
denumerize: (ptr) => {
|
|
377
|
+
const ref = refs.at(ptr >>> 0);
|
|
378
|
+
if (ref == null)
|
|
379
|
+
throw new Error("Not found");
|
|
380
|
+
return ref;
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
imports["sha256"] = {
|
|
384
|
+
digest: async (payload) => {
|
|
385
|
+
consume(payload.length);
|
|
386
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-256", payload));
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
imports["ed25519"] = {
|
|
390
|
+
verify: async (pubkey, signature, payload) => {
|
|
391
|
+
consume(payload.length);
|
|
392
|
+
const material = await crypto.subtle.importKey("spki", pubkey, "Ed25519", true, ["verify"]);
|
|
393
|
+
return await crypto.subtle.verify("Ed25519", material, signature, payload);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
let meteredCodeAsRaw;
|
|
397
|
+
if (!existsSync(join(this.path, "scripts", `${module}.metered.wasm`))) {
|
|
398
|
+
const codeAsRaw = readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
399
|
+
const codeAsWasm = Readable.readFromBytes(Wasm.Module, codeAsRaw);
|
|
400
|
+
meter(codeAsWasm, "sparks", "consume");
|
|
401
|
+
meteredCodeAsRaw = Writable.writeToBytes(codeAsWasm);
|
|
402
|
+
writeFileSync(join(this.path, "scripts", `${module}.metered.wasm`), meteredCodeAsRaw);
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.metered.wasm`));
|
|
406
|
+
}
|
|
407
|
+
const meteredCodeAsModule = new WebAssembly.Module(meteredCodeAsRaw);
|
|
408
|
+
for (const descriptor of WebAssembly.Module.imports(meteredCodeAsModule)) {
|
|
409
|
+
if (imports[descriptor.module] != null) {
|
|
410
|
+
// NOOP
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (exports[descriptor.module] != null) {
|
|
414
|
+
imports[descriptor.module] = exports[descriptor.module];
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const { instance } = load(descriptor.module);
|
|
418
|
+
imports[descriptor.module] = instance.exports;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
const meteredCodeAsInstance = new WebAssembly.Instance(meteredCodeAsModule, imports);
|
|
422
|
+
current.instance = meteredCodeAsInstance;
|
|
423
|
+
current.module = meteredCodeAsModule;
|
|
424
|
+
exports[module] = meteredCodeAsInstance.exports;
|
|
425
|
+
return current;
|
|
426
|
+
};
|
|
427
|
+
const { instance } = load(module);
|
|
428
|
+
if (typeof instance.exports[method] !== "function")
|
|
429
|
+
throw new Error("Not found");
|
|
430
|
+
const args = decode(params);
|
|
431
|
+
if (!Array.isArray(args))
|
|
432
|
+
throw new Error("Params is not an array");
|
|
433
|
+
const returned = await instance.exports[method](...args);
|
|
434
|
+
if (mode !== 1)
|
|
435
|
+
return encode([logs, reads, writes, returned, sparks]);
|
|
436
|
+
if (!writes.length)
|
|
437
|
+
return encode([logs, reads, writes, returned, sparks]);
|
|
438
|
+
await runAsImmediate(this.data, async (database) => {
|
|
439
|
+
const moment = await database.run(`INSERT INTO moments (epoch, module, method, params) VALUES (0, ?, ?, ?);`, module, method, args);
|
|
440
|
+
const writer = await database.prepare(`INSERT INTO events (moment, module, key, value) VALUES (?, ?, ?, ?);`);
|
|
441
|
+
for (const [module, key, value] of writes)
|
|
442
|
+
await writer.run(moment.lastInsertRowid, module, key, value);
|
|
443
|
+
return;
|
|
444
|
+
});
|
|
445
|
+
return encode([logs, reads, writes, returned, sparks]);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Runner } from "../runner/mod.ts";
|
|
2
|
+
import { RpcErr, RpcOk, RpcRequestInit } from "@hazae41/jsonrpc";
|
|
3
|
+
import { Database } from "@tursodatabase/database";
|
|
4
|
+
export declare class RpcWorkerServer {
|
|
5
|
+
#private;
|
|
6
|
+
readonly uuid: string;
|
|
7
|
+
readonly path: string;
|
|
8
|
+
readonly data: Database;
|
|
9
|
+
readonly runner: Runner;
|
|
10
|
+
constructor(uuid: string, path: string, data: Database);
|
|
11
|
+
respond(request: RpcRequestInit): Promise<RpcErr | RpcOk<string | Uint8Array<ArrayBuffer> | undefined>>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import { Runner } from "../runner/mod.js";
|
|
3
|
+
import { RpcErr, RpcError, RpcMethodNotFoundError, RpcOk } from "@hazae41/jsonrpc";
|
|
4
|
+
import { connect } from "@tursodatabase/database";
|
|
5
|
+
import { mkdirSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
export class RpcWorkerServer {
|
|
8
|
+
uuid;
|
|
9
|
+
path;
|
|
10
|
+
data;
|
|
11
|
+
runner;
|
|
12
|
+
constructor(uuid, path, data) {
|
|
13
|
+
this.uuid = uuid;
|
|
14
|
+
this.path = path;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.runner = new Runner(uuid, path, data);
|
|
17
|
+
}
|
|
18
|
+
async respond(request) {
|
|
19
|
+
try {
|
|
20
|
+
return new RpcOk(request.id, await this.#route(request));
|
|
21
|
+
}
|
|
22
|
+
catch (cause) {
|
|
23
|
+
return new RpcErr(request.id, RpcError.rewrap(cause));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async #route(request) {
|
|
27
|
+
if (request.method === "setup") {
|
|
28
|
+
const _ = request.params;
|
|
29
|
+
await this.runner.setup();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (request.method === "create") {
|
|
33
|
+
const [code, salt] = request.params;
|
|
34
|
+
const pack = await this.runner.create(code, salt);
|
|
35
|
+
return pack;
|
|
36
|
+
}
|
|
37
|
+
if (request.method === "execute") {
|
|
38
|
+
const [module, method, params, target] = request.params;
|
|
39
|
+
const proof = await this.runner.execute(module, method, params, target);
|
|
40
|
+
return proof;
|
|
41
|
+
}
|
|
42
|
+
if (request.method === "simulate") {
|
|
43
|
+
const [module, method, params, target] = request.params;
|
|
44
|
+
const proof = await this.runner.simulate(module, method, params, target);
|
|
45
|
+
return proof;
|
|
46
|
+
}
|
|
47
|
+
throw new RpcMethodNotFoundError();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function main() {
|
|
51
|
+
const url = new URL(location.href);
|
|
52
|
+
const uuid = url.searchParams.get("uuid");
|
|
53
|
+
if (uuid == null)
|
|
54
|
+
throw new Error();
|
|
55
|
+
const path = url.searchParams.get("path");
|
|
56
|
+
if (path == null)
|
|
57
|
+
throw new Error();
|
|
58
|
+
mkdirSync(join(path, "."), { recursive: true });
|
|
59
|
+
const data = await connect(join(path, "records.db"));
|
|
60
|
+
const server = new RpcWorkerServer(uuid, path, data);
|
|
61
|
+
self.addEventListener("message", async (event) => {
|
|
62
|
+
const refs = await server.respond(event.data);
|
|
63
|
+
const json = refs.toJSON();
|
|
64
|
+
self.postMessage(json);
|
|
65
|
+
}, { passive: true });
|
|
66
|
+
}
|
|
67
|
+
await main();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class RpcWorkerClient {
|
|
2
|
+
#private;
|
|
3
|
+
readonly worker: Worker;
|
|
4
|
+
constructor(worker: Worker);
|
|
5
|
+
static spawn(uuid: string, path: string): RpcWorkerClient;
|
|
6
|
+
setup(): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
+
create(code: Uint8Array<ArrayBuffer>, salt: Uint8Array<ArrayBuffer>): Promise<string>;
|
|
8
|
+
execute(module: string, method: string, params: Uint8Array<ArrayBuffer>, sparks: bigint): Promise<Uint8Array<ArrayBuffer>>;
|
|
9
|
+
simulate(module: string, method: string, params: Uint8Array<ArrayBuffer>, sparks: bigint): Promise<Uint8Array<ArrayBuffer>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { RpcResponse } from "@hazae41/jsonrpc";
|
|
54
|
+
export class RpcWorkerClient {
|
|
55
|
+
worker;
|
|
56
|
+
constructor(worker) {
|
|
57
|
+
this.worker = worker;
|
|
58
|
+
}
|
|
59
|
+
static spawn(uuid, path) {
|
|
60
|
+
const url = new URL(import.meta.resolve("./bin.js"));
|
|
61
|
+
url.searchParams.set("uuid", uuid);
|
|
62
|
+
url.searchParams.set("path", path);
|
|
63
|
+
const worker = new Worker(url, { type: "module" });
|
|
64
|
+
return new RpcWorkerClient(worker);
|
|
65
|
+
}
|
|
66
|
+
async setup() {
|
|
67
|
+
return await this.#request({ method: "setup" });
|
|
68
|
+
}
|
|
69
|
+
async create(code, salt) {
|
|
70
|
+
return await this.#request({ method: "create", params: [code, salt] });
|
|
71
|
+
}
|
|
72
|
+
async execute(module, method, params, sparks) {
|
|
73
|
+
return await this.#request({ method: "execute", params: [module, method, params, sparks] });
|
|
74
|
+
}
|
|
75
|
+
async simulate(module, method, params, sparks) {
|
|
76
|
+
return await this.#request({ method: "simulate", params: [module, method, params, sparks] });
|
|
77
|
+
}
|
|
78
|
+
async #request(request) {
|
|
79
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
80
|
+
try {
|
|
81
|
+
const stack = __addDisposableResource(env_1, new DisposableStack(), false);
|
|
82
|
+
const future = Promise.withResolvers();
|
|
83
|
+
const aborter = new AbortController();
|
|
84
|
+
stack.defer(() => aborter.abort());
|
|
85
|
+
this.worker.addEventListener("message", (event) => {
|
|
86
|
+
RpcResponse.from(event.data).inspectSync(future.resolve).inspectErrSync(future.reject);
|
|
87
|
+
}, { signal: aborter.signal });
|
|
88
|
+
this.worker.addEventListener("error", (event) => {
|
|
89
|
+
future.reject(event.error);
|
|
90
|
+
}, { signal: aborter.signal });
|
|
91
|
+
this.worker.addEventListener("messageerror", (event) => {
|
|
92
|
+
future.reject(event.data);
|
|
93
|
+
}, { signal: aborter.signal });
|
|
94
|
+
this.worker.postMessage(request);
|
|
95
|
+
return await future.promise;
|
|
96
|
+
}
|
|
97
|
+
catch (e_1) {
|
|
98
|
+
env_1.error = e_1;
|
|
99
|
+
env_1.hasError = true;
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
__disposeResources(env_1);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
package/out/mods/mod.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./bobine/mod.ts";
|
package/out/mods/mod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./bobine/mod.js";
|