@hazae41/bobine 0.1.3 → 0.1.5
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 +18 -2
- package/out/mods/bobine/runner/mod.d.ts +16 -4
- package/out/mods/bobine/runner/mod.js +35 -38
- 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 +2 -2
- package/out/libs/packed/mod.d.ts +0 -13
- package/out/libs/packed/mod.js +0 -114
package/README.md
CHANGED
|
@@ -6,8 +6,24 @@ WebAssembly-based metered chain of compute
|
|
|
6
6
|
npm install @hazae41/bobine
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
[**📦 NPM**](https://www.npmjs.com/package/@hazae41/bobine)
|
|
9
|
+
[**📦 NPM**](https://www.npmjs.com/package/@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 "@hazae41/webpack";
|
|
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
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// deno-lint-ignore-file no-unused-vars no-explicit-any
|
|
2
2
|
import { meter } from "../../../libs/metering/mod.js";
|
|
3
|
-
import { Packed } from "../../../libs/packed/mod.js";
|
|
4
3
|
import { runAsImmediate } from "../../../libs/sql/mod.js";
|
|
5
4
|
import { RpcPaymentError } from "../errors/mod.js";
|
|
6
5
|
import { Readable, Writable } from "@hazae41/binary";
|
|
7
6
|
import * as Wasm from "@hazae41/wasm";
|
|
7
|
+
import { Packed } from "@hazae41/webpack";
|
|
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,28 +60,24 @@ 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();
|
|
71
73
|
const reads = new Array();
|
|
72
74
|
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
75
|
const consume = (amount) => {
|
|
80
|
-
sparks += amount;
|
|
76
|
+
sparks += BigInt(amount);
|
|
81
77
|
if (process.env.NODE_ENV === "development")
|
|
82
78
|
return;
|
|
79
|
+
if (target == null)
|
|
80
|
+
return;
|
|
83
81
|
if (sparks > target)
|
|
84
82
|
throw new RpcPaymentError("Out of sparks");
|
|
85
83
|
return;
|
|
@@ -101,9 +99,6 @@ export class Runner {
|
|
|
101
99
|
},
|
|
102
100
|
};
|
|
103
101
|
imports["sparks"] = {
|
|
104
|
-
remaining: () => {
|
|
105
|
-
return target - BigInt(sparks);
|
|
106
|
-
},
|
|
107
102
|
consume: (amount) => {
|
|
108
103
|
consume(amount >>> 0);
|
|
109
104
|
}
|
|
@@ -157,10 +152,10 @@ export class Runner {
|
|
|
157
152
|
return blob.toBase64();
|
|
158
153
|
},
|
|
159
154
|
encode: (value) => {
|
|
160
|
-
return encode(value);
|
|
155
|
+
return Packed.encode(value);
|
|
161
156
|
},
|
|
162
157
|
decode: (blob) => {
|
|
163
|
-
return decode(blob);
|
|
158
|
+
return Packed.decode(blob);
|
|
164
159
|
}
|
|
165
160
|
};
|
|
166
161
|
imports["texts"] = {
|
|
@@ -336,8 +331,8 @@ export class Runner {
|
|
|
336
331
|
set: (key, fresh) => {
|
|
337
332
|
const cache = caches.get(module);
|
|
338
333
|
cache.set(key, fresh);
|
|
339
|
-
const keyAsRaw = encode(key);
|
|
340
|
-
const valueAsRaw = encode(fresh);
|
|
334
|
+
const keyAsRaw = Packed.encode(key);
|
|
335
|
+
const valueAsRaw = Packed.encode(fresh);
|
|
341
336
|
writes.push([module, keyAsRaw, valueAsRaw]);
|
|
342
337
|
return;
|
|
343
338
|
},
|
|
@@ -346,13 +341,13 @@ export class Runner {
|
|
|
346
341
|
const stale = cache.get(key);
|
|
347
342
|
if (stale != null)
|
|
348
343
|
return [stale];
|
|
349
|
-
const keyAsRaw = encode(key);
|
|
344
|
+
const keyAsRaw = Packed.encode(key);
|
|
350
345
|
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
346
|
if (row == null)
|
|
352
347
|
return null;
|
|
353
348
|
const valueAsRaw = new Uint8Array(row.value);
|
|
354
349
|
reads.push([module, keyAsRaw, valueAsRaw]);
|
|
355
|
-
const fresh = decode(valueAsRaw);
|
|
350
|
+
const fresh = Packed.decode(valueAsRaw);
|
|
356
351
|
cache.set(key, fresh);
|
|
357
352
|
return [fresh];
|
|
358
353
|
}
|
|
@@ -394,16 +389,19 @@ export class Runner {
|
|
|
394
389
|
}
|
|
395
390
|
};
|
|
396
391
|
let meteredCodeAsRaw;
|
|
397
|
-
if (
|
|
392
|
+
if (target == null) {
|
|
393
|
+
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
394
|
+
}
|
|
395
|
+
else if (existsSync(join(this.path, "scripts", `${module}.metered.wasm`))) {
|
|
396
|
+
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.metered.wasm`));
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
398
399
|
const codeAsRaw = readFileSync(join(this.path, "scripts", `${module}.wasm`));
|
|
399
400
|
const codeAsWasm = Readable.readFromBytes(Wasm.Module, codeAsRaw);
|
|
400
401
|
meter(codeAsWasm, "sparks", "consume");
|
|
401
402
|
meteredCodeAsRaw = Writable.writeToBytes(codeAsWasm);
|
|
402
403
|
writeFileSync(join(this.path, "scripts", `${module}.metered.wasm`), meteredCodeAsRaw);
|
|
403
404
|
}
|
|
404
|
-
else {
|
|
405
|
-
meteredCodeAsRaw = readFileSync(join(this.path, "scripts", `${module}.metered.wasm`));
|
|
406
|
-
}
|
|
407
405
|
const meteredCodeAsModule = new WebAssembly.Module(meteredCodeAsRaw);
|
|
408
406
|
for (const descriptor of WebAssembly.Module.imports(meteredCodeAsModule)) {
|
|
409
407
|
if (imports[descriptor.module] != null) {
|
|
@@ -427,21 +425,20 @@ export class Runner {
|
|
|
427
425
|
const { instance } = load(module);
|
|
428
426
|
if (typeof instance.exports[method] !== "function")
|
|
429
427
|
throw new Error("Not found");
|
|
430
|
-
|
|
431
|
-
if (!Array.isArray(args))
|
|
428
|
+
if (!Array.isArray(params))
|
|
432
429
|
throw new Error("Params is not an array");
|
|
433
|
-
const
|
|
430
|
+
const result = await instance.exports[method](...params);
|
|
434
431
|
if (mode !== 1)
|
|
435
|
-
return
|
|
432
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
436
433
|
if (!writes.length)
|
|
437
|
-
return
|
|
434
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
438
435
|
await runAsImmediate(this.data, async (database) => {
|
|
439
|
-
const moment = await database.run(`INSERT INTO moments (epoch, module, method, params) VALUES (0, ?, ?, ?);`, module, method,
|
|
436
|
+
const moment = await database.run(`INSERT INTO moments (epoch, module, method, params) VALUES (0, ?, ?, ?);`, module, method, params);
|
|
440
437
|
const writer = await database.prepare(`INSERT INTO events (moment, module, key, value) VALUES (?, ?, ?, ?);`);
|
|
441
438
|
for (const [module, key, value] of writes)
|
|
442
439
|
await writer.run(moment.lastInsertRowid, module, key, value);
|
|
443
440
|
return;
|
|
444
441
|
});
|
|
445
|
-
return
|
|
442
|
+
return { module, method, params, logs, reads, writes, result, sparks };
|
|
446
443
|
}
|
|
447
444
|
}
|
|
@@ -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.5",
|
|
5
5
|
"description": "WebAssembly-based metered chain of compute",
|
|
6
6
|
"repository": "github:hazae41/bobine",
|
|
7
7
|
"author": "hazae41",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@hazae41/binary": "^2.1.1",
|
|
24
|
-
"@hazae41/cursor": "^2.2.0",
|
|
25
24
|
"@hazae41/jsonrpc": "^1.2.1",
|
|
26
25
|
"@hazae41/wasm": "^1.0.2",
|
|
26
|
+
"@hazae41/webpack": "^1.0.0",
|
|
27
27
|
"@tursodatabase/database": "^0.8.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
package/out/libs/packed/mod.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Cursor } from "@hazae41/cursor";
|
|
2
|
-
export type Packable = null | number | Uint8Array | string | bigint | Array<Packable>;
|
|
3
|
-
export declare class Packed {
|
|
4
|
-
readonly value: Packable;
|
|
5
|
-
constructor(value: Packable);
|
|
6
|
-
size(): number;
|
|
7
|
-
write(cursor: Cursor): void;
|
|
8
|
-
}
|
|
9
|
-
export declare namespace Packed {
|
|
10
|
-
function read(cursor: Cursor): Packable;
|
|
11
|
-
function size(value: Packable): number;
|
|
12
|
-
function write(value: Packable, cursor: Cursor): void;
|
|
13
|
-
}
|
package/out/libs/packed/mod.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
// deno-lint-ignore-file no-namespace
|
|
2
|
-
export class Packed {
|
|
3
|
-
value;
|
|
4
|
-
constructor(value) {
|
|
5
|
-
this.value = value;
|
|
6
|
-
}
|
|
7
|
-
size() {
|
|
8
|
-
return Packed.size(this.value);
|
|
9
|
-
}
|
|
10
|
-
write(cursor) {
|
|
11
|
-
Packed.write(this.value, cursor);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
(function (Packed) {
|
|
15
|
-
function read(cursor) {
|
|
16
|
-
const type = cursor.readUint8();
|
|
17
|
-
if (type === 0)
|
|
18
|
-
return null;
|
|
19
|
-
if (type === 1)
|
|
20
|
-
return cursor.readFloat64(true);
|
|
21
|
-
if (type === 2)
|
|
22
|
-
return cursor.read(cursor.readUint32(true));
|
|
23
|
-
if (type === 3) {
|
|
24
|
-
const size = cursor.readUint32(true);
|
|
25
|
-
const data = cursor.read(size);
|
|
26
|
-
return new TextDecoder().decode(data);
|
|
27
|
-
}
|
|
28
|
-
if (type === 4) {
|
|
29
|
-
const negative = cursor.readUint8();
|
|
30
|
-
const size = cursor.readUint32(true);
|
|
31
|
-
const data = cursor.read(size);
|
|
32
|
-
const absolute = BigInt("0x" + data.toHex());
|
|
33
|
-
return negative ? -absolute : absolute;
|
|
34
|
-
}
|
|
35
|
-
if (type === 5) {
|
|
36
|
-
const length = cursor.readUint32(true);
|
|
37
|
-
const values = new Array(length);
|
|
38
|
-
for (let i = 0; i < length; i++)
|
|
39
|
-
values[i] = read(cursor);
|
|
40
|
-
return values;
|
|
41
|
-
}
|
|
42
|
-
throw new Error("Unknown pack type");
|
|
43
|
-
}
|
|
44
|
-
Packed.read = read;
|
|
45
|
-
function size(value) {
|
|
46
|
-
if (value == null)
|
|
47
|
-
return 1;
|
|
48
|
-
if (typeof value === "number")
|
|
49
|
-
return 1 + 8;
|
|
50
|
-
if (value instanceof Uint8Array)
|
|
51
|
-
return 1 + 4 + value.length;
|
|
52
|
-
if (typeof value === "string")
|
|
53
|
-
return 1 + 4 + new TextEncoder().encode(value).length;
|
|
54
|
-
if (typeof value === "bigint") {
|
|
55
|
-
const absolute = value < 0n ? -value : value;
|
|
56
|
-
const text = absolute.toString(16);
|
|
57
|
-
const data = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
|
|
58
|
-
return 1 + 1 + 4 + data.length;
|
|
59
|
-
}
|
|
60
|
-
if (Array.isArray(value)) {
|
|
61
|
-
let total = 0;
|
|
62
|
-
total += 1;
|
|
63
|
-
total += 4;
|
|
64
|
-
for (const subvalue of value)
|
|
65
|
-
total += size(subvalue);
|
|
66
|
-
return total;
|
|
67
|
-
}
|
|
68
|
-
throw new Error("Unknown pack value");
|
|
69
|
-
}
|
|
70
|
-
Packed.size = size;
|
|
71
|
-
function write(value, cursor) {
|
|
72
|
-
if (value == null) {
|
|
73
|
-
cursor.writeUint8(0);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (typeof value === "number") {
|
|
77
|
-
cursor.writeUint8(1);
|
|
78
|
-
cursor.writeFloat64(value, true);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (value instanceof Uint8Array) {
|
|
82
|
-
cursor.writeUint8(2);
|
|
83
|
-
cursor.writeUint32(value.length, true);
|
|
84
|
-
cursor.write(value);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (typeof value === "string") {
|
|
88
|
-
cursor.writeUint8(3);
|
|
89
|
-
const data = new TextEncoder().encode(value);
|
|
90
|
-
cursor.writeUint32(data.length, true);
|
|
91
|
-
cursor.write(data);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
if (typeof value === "bigint") {
|
|
95
|
-
cursor.writeUint8(4);
|
|
96
|
-
const [negative, absolute] = value < 0n ? [1, -value] : [0, value];
|
|
97
|
-
const text = absolute.toString(16);
|
|
98
|
-
const data = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
|
|
99
|
-
cursor.writeUint8(negative);
|
|
100
|
-
cursor.writeUint32(data.length, true);
|
|
101
|
-
cursor.write(data);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (Array.isArray(value)) {
|
|
105
|
-
cursor.writeUint8(5);
|
|
106
|
-
cursor.writeUint32(value.length, true);
|
|
107
|
-
for (const subvalue of value)
|
|
108
|
-
write(subvalue, cursor);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
throw new Error("Unknown pack value");
|
|
112
|
-
}
|
|
113
|
-
Packed.write = write;
|
|
114
|
-
})(Packed || (Packed = {}));
|