@hazae41/bobine 0.1.2 → 0.1.4
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 +17 -1
- package/out/mods/bobine/runner/mod.d.ts +16 -4
- package/out/mods/bobine/runner/mod.js +28 -25
- package/out/mods/bobine/worker/bin.d.ts +2 -6
- package/out/mods/bobine/worker/bin.js +4 -19
- package/out/mods/bobine/worker/mod.d.ts +4 -2
- package/out/mods/bobine/worker/mod.js +4 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -10,4 +10,20 @@ npm install @hazae41/bobine
|
|
|
10
10
|
|
|
11
11
|
## Ecosystem
|
|
12
12
|
|
|
13
|
-
- [Awesome Bobine](https://github.com/hazae41/awesome-bobine) / A curated list on Bobine ecosystem
|
|
13
|
+
- [Awesome Bobine](https://github.com/hazae41/awesome-bobine) / A curated list on Bobine ecosystem
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
const runner = await Runner.setup("c00c3fb2-36d4-42af-aaa7-4b30b22259a0", "./data")
|
|
19
|
+
|
|
20
|
+
const target = await runner.create(readFileSync("./program.wasm"))
|
|
21
|
+
|
|
22
|
+
const { logs, result, sparks } = await runner.execute(target, "main", ["Hello world"], 100n)
|
|
23
|
+
|
|
24
|
+
for (const log of logs)
|
|
25
|
+
console.log(log)
|
|
26
|
+
|
|
27
|
+
console.log(result)
|
|
28
|
+
console.log(sparks)
|
|
29
|
+
```
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
+
import { Nullable } from "../../../libs/nullable/mod.ts";
|
|
2
|
+
import { type Packable } from "../../../libs/packed/mod.ts";
|
|
1
3
|
import { Database } from "@tursodatabase/database";
|
|
4
|
+
export interface Run {
|
|
5
|
+
readonly module: string;
|
|
6
|
+
readonly method: string;
|
|
7
|
+
readonly params: Packable[];
|
|
8
|
+
readonly result: Packable;
|
|
9
|
+
readonly sparks: bigint;
|
|
10
|
+
readonly reads: [string, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>][];
|
|
11
|
+
readonly writes: [string, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>][];
|
|
12
|
+
readonly logs: string[];
|
|
13
|
+
}
|
|
2
14
|
export declare class Runner {
|
|
3
15
|
#private;
|
|
4
16
|
readonly uuid: string;
|
|
5
17
|
readonly path: string;
|
|
6
18
|
readonly data: Database;
|
|
7
|
-
constructor(
|
|
8
|
-
setup(): Promise<
|
|
19
|
+
private constructor();
|
|
20
|
+
static setup(uuid: string, path: string): Promise<Runner>;
|
|
9
21
|
create(code: Uint8Array<ArrayBuffer>, salt: Uint8Array<ArrayBuffer>): Promise<string>;
|
|
10
|
-
execute(module: string, method: string, params:
|
|
11
|
-
simulate(module: string, method: string, params:
|
|
22
|
+
execute(module: string, method: string, params: Packable[], target?: Nullable<bigint>): Promise<Run>;
|
|
23
|
+
simulate(module: string, method: string, params: Packable[], target?: Nullable<bigint>): Promise<Run>;
|
|
12
24
|
}
|
|
@@ -5,6 +5,7 @@ import { runAsImmediate } from "../../../libs/sql/mod.js";
|
|
|
5
5
|
import { RpcPaymentError } from "../errors/mod.js";
|
|
6
6
|
import { Readable, Writable } from "@hazae41/binary";
|
|
7
7
|
import * as Wasm from "@hazae41/wasm";
|
|
8
|
+
import { connect } from "@tursodatabase/database";
|
|
8
9
|
import { Buffer } from "node:buffer";
|
|
9
10
|
import { existsSync, mkdirSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";
|
|
10
11
|
import { join } from "node:path";
|
|
@@ -18,10 +19,11 @@ export class Runner {
|
|
|
18
19
|
this.path = path;
|
|
19
20
|
this.data = data;
|
|
20
21
|
}
|
|
21
|
-
async setup() {
|
|
22
|
-
mkdirSync(join(
|
|
23
|
-
mkdirSync(join(
|
|
24
|
-
|
|
22
|
+
static async setup(uuid, path) {
|
|
23
|
+
mkdirSync(join(path), { recursive: true });
|
|
24
|
+
mkdirSync(join(path, "scripts"), { recursive: true });
|
|
25
|
+
const data = await connect(join(path, "records.db"));
|
|
26
|
+
await data.exec(`CREATE TABLE IF NOT EXISTS events (
|
|
25
27
|
nonce INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
26
28
|
|
|
27
29
|
moment INTEGER NOT NULL,
|
|
@@ -32,7 +34,7 @@ export class Runner {
|
|
|
32
34
|
|
|
33
35
|
value BLOB NOT NULL
|
|
34
36
|
);`);
|
|
35
|
-
await
|
|
37
|
+
await data.exec(`CREATE TABLE IF NOT EXISTS moments (
|
|
36
38
|
nonce INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
37
39
|
|
|
38
40
|
epoch INTEGER NOT NULL,
|
|
@@ -43,7 +45,7 @@ export class Runner {
|
|
|
43
45
|
|
|
44
46
|
params BLOB NOT NULL
|
|
45
47
|
);`);
|
|
46
|
-
|
|
48
|
+
return new Runner(uuid, path, data);
|
|
47
49
|
}
|
|
48
50
|
async create(code, salt) {
|
|
49
51
|
const pack = Writable.writeToBytes(new Packed([code, salt]));
|
|
@@ -58,13 +60,13 @@ export class Runner {
|
|
|
58
60
|
return digestOfPackAsHex;
|
|
59
61
|
}
|
|
60
62
|
async execute(module, method, params, target) {
|
|
61
|
-
return await this.#run(module, method, params, target
|
|
63
|
+
return await this.#run(1, module, method, params, target);
|
|
62
64
|
}
|
|
63
65
|
async simulate(module, method, params, target) {
|
|
64
|
-
return await this.#run(module, method, params, target
|
|
66
|
+
return await this.#run(2, module, method, params, target);
|
|
65
67
|
}
|
|
66
|
-
async #run(module, method, params, target
|
|
67
|
-
let sparks =
|
|
68
|
+
async #run(mode, module, method, params, target) {
|
|
69
|
+
let sparks = 0n;
|
|
68
70
|
const exports = {};
|
|
69
71
|
const logs = new Array();
|
|
70
72
|
const caches = new Map();
|
|
@@ -77,9 +79,11 @@ export class Runner {
|
|
|
77
79
|
return Readable.readFromBytes(Packed, bytes);
|
|
78
80
|
};
|
|
79
81
|
const consume = (amount) => {
|
|
80
|
-
sparks += amount;
|
|
82
|
+
sparks += BigInt(amount);
|
|
81
83
|
if (process.env.NODE_ENV === "development")
|
|
82
84
|
return;
|
|
85
|
+
if (target == null)
|
|
86
|
+
return;
|
|
83
87
|
if (sparks > target)
|
|
84
88
|
throw new RpcPaymentError("Out of sparks");
|
|
85
89
|
return;
|
|
@@ -101,9 +105,6 @@ export class Runner {
|
|
|
101
105
|
},
|
|
102
106
|
};
|
|
103
107
|
imports["sparks"] = {
|
|
104
|
-
remaining: () => {
|
|
105
|
-
return target - BigInt(sparks);
|
|
106
|
-
},
|
|
107
108
|
consume: (amount) => {
|
|
108
109
|
consume(amount >>> 0);
|
|
109
110
|
}
|
|
@@ -394,16 +395,19 @@ export class Runner {
|
|
|
394
395
|
}
|
|
395
396
|
};
|
|
396
397
|
let meteredCodeAsRaw;
|
|
397
|
-
if (
|
|
398
|
+
if (target == null) {
|
|
399
|
+
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
400
|
+
}
|
|
401
|
+
else if (existsSync(join(this.path, "scripts", `${module}.metered.wasm`))) {
|
|
402
|
+
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.metered.wasm`));
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
398
405
|
const codeAsRaw = readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
399
406
|
const codeAsWasm = Readable.readFromBytes(Wasm.Module, codeAsRaw);
|
|
400
407
|
meter(codeAsWasm, "sparks", "consume");
|
|
401
408
|
meteredCodeAsRaw = Writable.writeToBytes(codeAsWasm);
|
|
402
409
|
writeFileSync(join(this.path, "scripts", `${module}.metered.wasm`), meteredCodeAsRaw);
|
|
403
410
|
}
|
|
404
|
-
else {
|
|
405
|
-
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.metered.wasm`));
|
|
406
|
-
}
|
|
407
411
|
const meteredCodeAsModule = new WebAssembly.Module(meteredCodeAsRaw);
|
|
408
412
|
for (const descriptor of WebAssembly.Module.imports(meteredCodeAsModule)) {
|
|
409
413
|
if (imports[descriptor.module] != null) {
|
|
@@ -427,21 +431,20 @@ export class Runner {
|
|
|
427
431
|
const { instance } = load(module);
|
|
428
432
|
if (typeof instance.exports[method] !== "function")
|
|
429
433
|
throw new Error("Not found");
|
|
430
|
-
|
|
431
|
-
if (!Array.isArray(args))
|
|
434
|
+
if (!Array.isArray(params))
|
|
432
435
|
throw new Error("Params is not an array");
|
|
433
|
-
const
|
|
436
|
+
const result = await instance.exports[method](...params);
|
|
434
437
|
if (mode !== 1)
|
|
435
|
-
return
|
|
438
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
436
439
|
if (!writes.length)
|
|
437
|
-
return
|
|
440
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
438
441
|
await runAsImmediate(this.data, async (database) => {
|
|
439
|
-
const moment = await database.run(`INSERT INTO moments (epoch, module, method, params) VALUES (0, ?, ?, ?);`, module, method,
|
|
442
|
+
const moment = await database.run(`INSERT INTO moments (epoch, module, method, params) VALUES (0, ?, ?, ?);`, module, method, params);
|
|
440
443
|
const writer = await database.prepare(`INSERT INTO events (moment, module, key, value) VALUES (?, ?, ?, ?);`);
|
|
441
444
|
for (const [module, key, value] of writes)
|
|
442
445
|
await writer.run(moment.lastInsertRowid, module, key, value);
|
|
443
446
|
return;
|
|
444
447
|
});
|
|
445
|
-
return
|
|
448
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
446
449
|
}
|
|
447
450
|
}
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { Runner } from "../runner/mod.ts";
|
|
2
2
|
import { RpcErr, RpcOk, RpcRequestInit } from "@hazae41/jsonrpc";
|
|
3
|
-
import { Database } from "@tursodatabase/database";
|
|
4
3
|
export declare class RpcWorkerServer {
|
|
5
4
|
#private;
|
|
6
|
-
readonly uuid: string;
|
|
7
|
-
readonly path: string;
|
|
8
|
-
readonly data: Database;
|
|
9
5
|
readonly runner: Runner;
|
|
10
|
-
constructor(
|
|
11
|
-
respond(request: RpcRequestInit): Promise<RpcErr | RpcOk<string |
|
|
6
|
+
constructor(runner: Runner);
|
|
7
|
+
respond(request: RpcRequestInit): Promise<RpcErr | RpcOk<string | import("../runner/mod.ts").Run>>;
|
|
12
8
|
}
|
|
@@ -1,19 +1,10 @@
|
|
|
1
1
|
/// <reference lib="webworker" />
|
|
2
2
|
import { Runner } from "../runner/mod.js";
|
|
3
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
4
|
export class RpcWorkerServer {
|
|
8
|
-
uuid;
|
|
9
|
-
path;
|
|
10
|
-
data;
|
|
11
5
|
runner;
|
|
12
|
-
constructor(
|
|
13
|
-
this.
|
|
14
|
-
this.path = path;
|
|
15
|
-
this.data = data;
|
|
16
|
-
this.runner = new Runner(uuid, path, data);
|
|
6
|
+
constructor(runner) {
|
|
7
|
+
this.runner = runner;
|
|
17
8
|
}
|
|
18
9
|
async respond(request) {
|
|
19
10
|
try {
|
|
@@ -29,11 +20,6 @@ export class RpcWorkerServer {
|
|
|
29
20
|
// NOOP
|
|
30
21
|
return "pong";
|
|
31
22
|
}
|
|
32
|
-
if (request.method === "setup") {
|
|
33
|
-
const _ = request.params;
|
|
34
|
-
await this.runner.setup();
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
23
|
if (request.method === "create") {
|
|
38
24
|
const [code, salt] = request.params;
|
|
39
25
|
const pack = await this.runner.create(code, salt);
|
|
@@ -60,9 +46,8 @@ async function main() {
|
|
|
60
46
|
const path = url.searchParams.get("path");
|
|
61
47
|
if (path == null)
|
|
62
48
|
throw new Error();
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
const server = new RpcWorkerServer(uuid, path, data);
|
|
49
|
+
const runner = await Runner.setup(uuid, path);
|
|
50
|
+
const server = new RpcWorkerServer(runner);
|
|
66
51
|
self.addEventListener("message", async (event) => {
|
|
67
52
|
const refs = await server.respond(event.data);
|
|
68
53
|
const json = refs.toJSON();
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Nullable } from "../../../libs/nullable/mod.ts";
|
|
2
|
+
import { Run } from "../runner/mod.ts";
|
|
1
3
|
export declare class RpcWorkerClient {
|
|
2
4
|
#private;
|
|
3
5
|
readonly worker: Worker;
|
|
@@ -5,6 +7,6 @@ export declare class RpcWorkerClient {
|
|
|
5
7
|
static spawn(uuid: string, path: string): Promise<RpcWorkerClient>;
|
|
6
8
|
setup(): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
9
|
create(code: Uint8Array<ArrayBuffer>, salt: Uint8Array<ArrayBuffer>): Promise<string>;
|
|
8
|
-
execute(module: string, method: string, params: Uint8Array<ArrayBuffer>,
|
|
9
|
-
simulate(module: string, method: string, params: Uint8Array<ArrayBuffer>,
|
|
10
|
+
execute(module: string, method: string, params: Uint8Array<ArrayBuffer>, target?: Nullable<bigint>): Promise<Run>;
|
|
11
|
+
simulate(module: string, method: string, params: Uint8Array<ArrayBuffer>, target?: Nullable<bigint>): Promise<Run>;
|
|
10
12
|
}
|
|
@@ -71,11 +71,11 @@ export class RpcWorkerClient {
|
|
|
71
71
|
async create(code, salt) {
|
|
72
72
|
return await this.#request({ method: "create", params: [code, salt] });
|
|
73
73
|
}
|
|
74
|
-
async execute(module, method, params,
|
|
75
|
-
return await this.#request({ method: "execute", params: [module, method, params,
|
|
74
|
+
async execute(module, method, params, target) {
|
|
75
|
+
return await this.#request({ method: "execute", params: [module, method, params, target] });
|
|
76
76
|
}
|
|
77
|
-
async simulate(module, method, params,
|
|
78
|
-
return await this.#request({ method: "simulate", params: [module, method, params,
|
|
77
|
+
async simulate(module, method, params, target) {
|
|
78
|
+
return await this.#request({ method: "simulate", params: [module, method, params, target] });
|
|
79
79
|
}
|
|
80
80
|
async #request(init) {
|
|
81
81
|
const env_1 = { stack: [], error: void 0, hasError: false };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@hazae41/bobine",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"description": "WebAssembly-based metered chain of compute",
|
|
6
6
|
"repository": "github:hazae41/bobine",
|
|
7
7
|
"author": "hazae41",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
"@hazae41/binary": "^2.1.1",
|
|
24
24
|
"@hazae41/cursor": "^2.2.0",
|
|
25
25
|
"@hazae41/jsonrpc": "^1.2.1",
|
|
26
|
-
"@hazae41/wasm": "^1.0.
|
|
26
|
+
"@hazae41/wasm": "^1.0.2",
|
|
27
27
|
"@tursodatabase/database": "^0.8.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@hazae41/phobos": "^2.0.16",
|
|
31
31
|
"@hazae41/tscousin": "^1.0.28",
|
|
32
|
-
"@types/node": "^26.6.
|
|
32
|
+
"@types/node": "^26.6.11",
|
|
33
33
|
"typescript": "^7.0.2"
|
|
34
34
|
},
|
|
35
35
|
"keywords": [
|