@syntropika/tembra-sdk 0.2.2 → 0.3.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/README.md +43 -17
- package/dist/authoring/types.d.ts +64 -3
- package/dist/authoring/wit.d.ts +6 -0
- package/dist/bridge/commands.d.ts +1190 -0
- package/dist/bridge/identifiers.d.ts +2 -0
- package/dist/bridge/kernel.d.ts +18 -107
- package/dist/bridge/protocol.d.ts +3 -138
- package/dist/browser.d.ts +14 -1
- package/dist/browser.js +141 -3332
- package/dist/chunks/browser-b8jamxw2.js +3399 -0
- package/dist/chunks/execution-nsqc6nrt.js +1419 -0
- package/dist/chunks/index-01zk0xqx.js +160 -0
- package/dist/chunks/index-3fjdfdfd.js +152 -0
- package/dist/chunks/index-aaymjn7v.js +787 -0
- package/dist/chunks/index-c1w7z9hx.js +356 -0
- package/dist/chunks/index-h8t0n42q.js +738 -0
- package/dist/chunks/index-hfds9mzp.js +308 -0
- package/dist/chunks/index-kr4x67wt.js +177 -0
- package/dist/chunks/index-kr6bnh34.js +117 -0
- package/dist/chunks/index-kyt9dj0a.js +1421 -0
- package/dist/chunks/index-n1wxzc9w.js +830 -0
- package/dist/chunks/index-pvac426d.js +74 -0
- package/dist/chunks/index-smyds5ce.js +3234 -0
- package/dist/chunks/index-sse8aapb.js +121 -0
- package/dist/chunks/index-v6heph77.js +643 -0
- package/dist/chunks/index-xrdvnv37.js +3448 -0
- package/dist/chunks/index-za4mg94e.js +26 -0
- package/dist/chunks/index-zkqmnjj8.js +167 -0
- package/dist/coordinator.js +9 -500
- package/dist/error.d.ts +2 -0
- package/dist/errors.d.ts +3 -0
- package/dist/guest.d.ts +173 -0
- package/dist/guest.js +26 -0
- package/dist/host.d.ts +36 -30
- package/dist/host.js +37 -3310
- package/dist/http/bytes.d.ts +8 -0
- package/dist/http/events.d.ts +7 -0
- package/dist/http.d.ts +45 -0
- package/dist/http.js +291 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +49 -862
- package/dist/infrastructure/rpc/client.d.ts +2 -1
- package/dist/infrastructure/rpc/resources.d.ts +28 -0
- package/dist/interpreter/abi.d.ts +54 -0
- package/dist/interpreter/acquire.d.ts +18 -0
- package/dist/interpreter/browser-network.d.ts +52 -0
- package/dist/interpreter/browser-port.d.ts +55 -0
- package/dist/interpreter/browser.d.ts +79 -0
- package/dist/interpreter/cli.d.ts +5 -0
- package/dist/interpreter/codec.d.ts +42 -0
- package/dist/interpreter/component/execution.d.ts +42 -0
- package/dist/interpreter/component/host.d.ts +69 -0
- package/dist/interpreter/component/instance.d.ts +40 -0
- package/dist/interpreter/component/kernel.d.ts +120 -0
- package/dist/interpreter/component/names.d.ts +5 -0
- package/dist/interpreter/component/port.d.ts +85 -0
- package/dist/interpreter/component/prepared.d.ts +97 -0
- package/dist/interpreter/component/resources.d.ts +45 -0
- package/dist/interpreter/component/revision.d.ts +87 -0
- package/dist/interpreter/component/scope.d.ts +116 -0
- package/dist/interpreter/component/streams.d.ts +28 -0
- package/dist/interpreter/component/values.d.ts +79 -0
- package/dist/interpreter/component/wasi-filesystem.d.ts +21 -0
- package/dist/interpreter/component/wasi-host.d.ts +64 -0
- package/dist/interpreter/component/wasi-http.d.ts +33 -0
- package/dist/interpreter/component/wasi-sockets.d.ts +183 -0
- package/dist/interpreter/component/wasi.d.ts +41 -0
- package/dist/interpreter/core.d.ts +64 -0
- package/dist/interpreter/fetch-http-provider.d.ts +17 -0
- package/dist/interpreter/filesystem/contract.d.ts +85 -0
- package/dist/interpreter/filesystem/memory.d.ts +3 -0
- package/dist/interpreter/filesystem/opfs.d.ts +3 -0
- package/dist/interpreter/filesystem/scope.d.ts +35 -0
- package/dist/interpreter/filesystem.d.ts +8 -0
- package/dist/interpreter/gate.d.ts +6 -0
- package/dist/interpreter/host-protocol.d.ts +100 -0
- package/dist/interpreter/http-ingress-fetch.d.ts +7 -0
- package/dist/interpreter/http-ingress.d.ts +96 -0
- package/dist/interpreter/http-provider.d.ts +85 -0
- package/dist/interpreter/io.d.ts +20 -0
- package/dist/interpreter/mobile.d.ts +149 -0
- package/dist/interpreter/native-ingress.d.ts +95 -0
- package/dist/interpreter/node-port.d.ts +25 -0
- package/dist/interpreter/node.d.ts +36 -0
- package/dist/interpreter/revision.d.ts +64 -0
- package/dist/interpreter/scope.d.ts +89 -0
- package/dist/interpreter/socket-addresses.d.ts +50 -0
- package/dist/interpreter/socket-provider.d.ts +110 -0
- package/dist/interpreter/standard-cli.d.ts +34 -0
- package/dist/interpreter/standard-codec.d.ts +96 -0
- package/dist/interpreter/standard-filesystem.d.ts +47 -0
- package/dist/interpreter/standard-host.d.ts +71 -0
- package/dist/interpreter/standard-http-values.d.ts +159 -0
- package/dist/interpreter/standard-http.d.ts +9 -0
- package/dist/interpreter/standard-ingress.d.ts +63 -0
- package/dist/interpreter/standard-sockets.d.ts +177 -0
- package/dist/interpreter/standard-streams.d.ts +218 -0
- package/dist/interpreter/standard-system.d.ts +35 -0
- package/dist/interpreter/standard-utf8.d.ts +4 -0
- package/dist/interpreter/standard.d.ts +18 -0
- package/dist/interpreter/types.d.ts +152 -0
- package/dist/interpreter/wit-shape.d.ts +92 -0
- package/dist/manager.js +5 -332
- package/dist/msgpack.js +9 -615
- package/dist/node.d.ts +8 -1
- package/dist/node.js +248 -3321
- package/dist/rpc.js +196 -699
- package/dist/runtime/adapters.d.ts +120 -0
- package/dist/runtime/adapters.js +38 -0
- package/dist/runtime/calls.d.ts +7 -6
- package/dist/runtime/lifecycle.d.ts +3 -1
- package/dist/runtime/orbs.d.ts +5 -4
- package/dist/runtime/references.d.ts +27 -1
- package/dist/runtime/reload.d.ts +5 -3
- package/dist/runtime/scopes.d.ts +1 -1
- package/dist/runtime/signals.d.ts +4 -0
- package/dist/runtime/wit-client.d.ts +38 -0
- package/dist/telemetry.js +4 -25
- package/dist/tooling/build.d.ts +2 -2
- package/dist/tooling/client-resources.d.ts +23 -0
- package/dist/tooling/client.d.ts +10 -0
- package/dist/tooling/component-glue.d.ts +17 -0
- package/dist/tooling/component.d.ts +11 -0
- package/dist/tooling/index.d.ts +3 -1
- package/dist/tooling/index.js +1155 -102
- package/dist/tooling/resolver.d.ts +4 -4
- package/dist/tooling/typecheck.d.ts +2 -1
- package/dist/tooling/types.d.ts +27 -12
- package/dist/tooling/wit.d.ts +9 -0
- package/dist/wasi/wasi-clocks-types.d.ts +2 -0
- package/dist/wasi/wasi-http-client.d.ts +5 -0
- package/dist/wasi/wasi-http-types.d.ts +285 -0
- package/dist/wasi/wasi-sockets-ip-name-lookup.d.ts +23 -0
- package/dist/wasi/wasi-sockets-types.d.ts +146 -0
- package/package.json +62 -35
- package/src/authoring/declaration.ts +1 -1
- package/src/authoring/types.ts +53 -3
- package/src/authoring/wit.ts +15 -0
- package/src/bridge/commands.ts +1105 -0
- package/src/bridge/identifiers.ts +2 -0
- package/src/bridge/kernel.ts +56 -98
- package/src/bridge/protocol.ts +3 -142
- package/src/browser.ts +90 -6
- package/src/context-key.ts +4 -7
- package/src/error.ts +1 -1
- package/src/errors.ts +7 -0
- package/src/guest.ts +327 -0
- package/src/host.ts +128 -23
- package/src/http/bytes.ts +87 -0
- package/src/http/events.ts +34 -0
- package/src/http.ts +272 -0
- package/src/index.ts +3 -0
- package/src/infrastructure/rpc/client.ts +136 -17
- package/src/infrastructure/rpc/resources.ts +123 -0
- package/src/interpreter/abi.ts +69 -0
- package/src/interpreter/acquire.ts +58 -0
- package/src/interpreter/browser-network.ts +170 -0
- package/src/interpreter/browser-port.ts +425 -0
- package/src/interpreter/browser.ts +340 -0
- package/src/interpreter/cli.ts +30 -0
- package/src/interpreter/codec.ts +298 -0
- package/src/interpreter/component/execution.ts +264 -0
- package/src/interpreter/component/host.ts +409 -0
- package/src/interpreter/component/instance.ts +138 -0
- package/src/interpreter/component/kernel.ts +611 -0
- package/src/interpreter/component/names.ts +13 -0
- package/src/interpreter/component/port.ts +598 -0
- package/src/interpreter/component/prepared.ts +194 -0
- package/src/interpreter/component/resources.ts +345 -0
- package/src/interpreter/component/revision.ts +702 -0
- package/src/interpreter/component/scope.ts +447 -0
- package/src/interpreter/component/streams.ts +180 -0
- package/src/interpreter/component/values.ts +254 -0
- package/src/interpreter/component/wasi-filesystem.ts +472 -0
- package/src/interpreter/component/wasi-host.ts +197 -0
- package/src/interpreter/component/wasi-http.ts +939 -0
- package/src/interpreter/component/wasi-sockets.ts +958 -0
- package/src/interpreter/component/wasi.ts +238 -0
- package/src/interpreter/core.ts +386 -0
- package/src/interpreter/fetch-http-provider.ts +98 -0
- package/src/interpreter/filesystem/contract.ts +138 -0
- package/src/interpreter/filesystem/memory.ts +180 -0
- package/src/interpreter/filesystem/opfs.ts +294 -0
- package/src/interpreter/filesystem/scope.ts +390 -0
- package/src/interpreter/filesystem.ts +20 -0
- package/src/interpreter/gate.ts +55 -0
- package/src/interpreter/host-protocol.ts +242 -0
- package/src/interpreter/http-ingress-fetch.ts +133 -0
- package/src/interpreter/http-ingress.ts +203 -0
- package/src/interpreter/http-provider.ts +105 -0
- package/src/interpreter/io.ts +66 -0
- package/src/interpreter/mobile.ts +385 -0
- package/src/interpreter/native-ingress.ts +550 -0
- package/src/interpreter/node-port.ts +218 -0
- package/src/interpreter/node.ts +114 -0
- package/src/interpreter/revision.ts +450 -0
- package/src/interpreter/scope.ts +453 -0
- package/src/interpreter/socket-addresses.ts +246 -0
- package/src/interpreter/socket-provider.ts +126 -0
- package/src/interpreter/standard-cli.ts +184 -0
- package/src/interpreter/standard-codec.ts +403 -0
- package/src/interpreter/standard-filesystem.ts +545 -0
- package/src/interpreter/standard-host.ts +840 -0
- package/src/interpreter/standard-http-values.ts +624 -0
- package/src/interpreter/standard-http.ts +622 -0
- package/src/interpreter/standard-ingress.ts +344 -0
- package/src/interpreter/standard-sockets.ts +847 -0
- package/src/interpreter/standard-streams.ts +710 -0
- package/src/interpreter/standard-system.ts +182 -0
- package/src/interpreter/standard-utf8.ts +72 -0
- package/src/interpreter/standard.ts +283 -0
- package/src/interpreter/types.ts +163 -0
- package/src/interpreter/wit-shape.ts +175 -0
- package/src/node.ts +43 -14
- package/src/runtime/activities.ts +6 -6
- package/src/runtime/adapters.ts +314 -0
- package/src/runtime/calls.ts +213 -90
- package/src/runtime/context.ts +7 -7
- package/src/runtime/lifecycle.ts +36 -17
- package/src/runtime/orbs.ts +81 -39
- package/src/runtime/programs.ts +16 -8
- package/src/runtime/references.ts +128 -11
- package/src/runtime/reload.ts +71 -30
- package/src/runtime/resources.ts +2 -2
- package/src/runtime/scopes.ts +17 -17
- package/src/runtime/session.ts +3 -1
- package/src/runtime/signals.ts +31 -0
- package/src/runtime/wit-client.ts +228 -0
- package/src/tooling/build.ts +152 -54
- package/src/tooling/client-resources.ts +74 -0
- package/src/tooling/client.ts +531 -0
- package/src/tooling/component-glue.ts +288 -0
- package/src/tooling/component.ts +225 -0
- package/src/tooling/index.ts +3 -7
- package/src/tooling/resolver.ts +42 -25
- package/src/tooling/typecheck.ts +59 -4
- package/src/tooling/types.ts +27 -12
- package/src/tooling/wit.ts +188 -0
- package/dist/embedded.d.ts +0 -1
- package/dist/embedded.js +0 -1
- package/src/embedded.ts +0 -301
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// Binary host value profile for generated standard WASI calls.
|
|
2
|
+
//
|
|
3
|
+
// The guest bootstrap and the browser host share this codec. Its layout matches the Rust
|
|
4
|
+
// dispatcher generated from the same WIT: little-endian fixed-width numbers, u32-prefixed
|
|
5
|
+
// strings and byte lists, u8 option/result tags, u32 enum/variant discriminants and flag bits,
|
|
6
|
+
// and safe-integer u64 handles. Values follow jco conventions: 64-bit integers are bigint, list<u8> is a
|
|
7
|
+
// Uint8Array and variants are `{ tag, val }`. Inter-Orb portable values are unrelated.
|
|
8
|
+
import { TYPES } from "./standard.ts";
|
|
9
|
+
import { utf8Decode, utf8Encode } from "./standard-utf8.ts";
|
|
10
|
+
|
|
11
|
+
export { utf8Decode, utf8Encode };
|
|
12
|
+
|
|
13
|
+
/** First packet byte, then the operation byte. */
|
|
14
|
+
export const STANDARD_PACKET = 0;
|
|
15
|
+
export const CALL = 0;
|
|
16
|
+
export const STREAM_READ = 1;
|
|
17
|
+
export const FUTURE_READ = 2;
|
|
18
|
+
export const DROP = 3;
|
|
19
|
+
/** Create a guest-writable input stream of a `TYPES` index; replies with its u64 identity. */
|
|
20
|
+
export const STREAM_NEW = 4;
|
|
21
|
+
/**
|
|
22
|
+
* Write one chunk into a guest-writable input stream; completes once the host accepted it. The
|
|
23
|
+
* reply is one status byte, `WRITE_ACCEPTED` or `WRITE_CLOSED`; every rejection is a failure.
|
|
24
|
+
*/
|
|
25
|
+
export const STREAM_WRITE = 5;
|
|
26
|
+
/** `STREAM_WRITE` status: the chunk was accepted. */
|
|
27
|
+
export const WRITE_ACCEPTED = 0;
|
|
28
|
+
/** `STREAM_WRITE` status: the reader closed the stream, an ordinary end of production. */
|
|
29
|
+
export const WRITE_CLOSED = 1;
|
|
30
|
+
/** Create a guest-writable future of a generated writable `TYPES` index. */
|
|
31
|
+
export const FUTURE_NEW = 6;
|
|
32
|
+
/** Resolve a guest-writable future with one typed payload. */
|
|
33
|
+
export const FUTURE_WRITE = 7;
|
|
34
|
+
/**
|
|
35
|
+
* Asynchronous, incoming HTTP callbacks only: publish the handler's
|
|
36
|
+
* `result<own<response>, error-code>` once. The host publishes the head at once and completes the
|
|
37
|
+
* operation only when the response delivery reached its end, so the callback stays alive while
|
|
38
|
+
* the response streams. The reply is empty.
|
|
39
|
+
*/
|
|
40
|
+
export const INGRESS_HEAD = 8;
|
|
41
|
+
/**
|
|
42
|
+
* Asynchronous, only in a callback whose export returns a whole `stream<u8>`: publish the guest
|
|
43
|
+
* writer `handle` as that result once. The caller receives the stream at once; the operation
|
|
44
|
+
* completes when the written bytes were delivered or the caller closed the stream, so the
|
|
45
|
+
* callback stays alive while it streams. The reply is empty.
|
|
46
|
+
*/
|
|
47
|
+
export const STREAM_RESULT = 9;
|
|
48
|
+
/**
|
|
49
|
+
* Asynchronous, native Rust facade only: call an Orb import whose whole result is a `stream<u8>`.
|
|
50
|
+
* The packet carries the length-prefixed UTF-8 JSON `reference.call` request; the host checks the
|
|
51
|
+
* import's signature before calling it. The reply is the `u64` handle of the readable stream,
|
|
52
|
+
* issued once the producer delivered its head.
|
|
53
|
+
*/
|
|
54
|
+
export const STREAM_IMPORT = 10;
|
|
55
|
+
/** Largest byte chunk either side moves through one stream read or write. */
|
|
56
|
+
export const STREAM_CHUNK = 64 << 10;
|
|
57
|
+
|
|
58
|
+
/** The resource class name jco exposes for a canonical `interface#resource-name`. */
|
|
59
|
+
export function resourceClassName(name: string): string {
|
|
60
|
+
return name
|
|
61
|
+
.split("#")[1]!
|
|
62
|
+
.replace(/(^|-)(\w)/g, (_match, _separator, letter) => letter.toUpperCase());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Handle conversion supplied by the side that owns handle identities. */
|
|
66
|
+
export interface HandleCodec {
|
|
67
|
+
/** Return the handle number for an outgoing value of `kind`. */
|
|
68
|
+
handle(kind: string, detail: unknown, value: unknown): number;
|
|
69
|
+
/** Return the language value for an incoming handle of `kind`. */
|
|
70
|
+
value(kind: string, detail: unknown, handle: number): unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Growable little-endian writer. */
|
|
74
|
+
export class Writer {
|
|
75
|
+
#bytes = new Uint8Array(64);
|
|
76
|
+
#view = new DataView(this.#bytes.buffer);
|
|
77
|
+
#length = 0;
|
|
78
|
+
|
|
79
|
+
/** Reserve space for `count` more bytes. */
|
|
80
|
+
#reserve(count: number): number {
|
|
81
|
+
const start = this.#length;
|
|
82
|
+
if (start + count > this.#bytes.length) {
|
|
83
|
+
const grown = new Uint8Array(Math.max(this.#bytes.length * 2, start + count));
|
|
84
|
+
grown.set(this.#bytes.subarray(0, start));
|
|
85
|
+
this.#bytes = grown;
|
|
86
|
+
this.#view = new DataView(grown.buffer);
|
|
87
|
+
}
|
|
88
|
+
this.#length += count;
|
|
89
|
+
return start;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Reserve before reading `#view`/`#bytes`: growth replaces both, and a receiver read first
|
|
93
|
+
// would write into the discarded allocation.
|
|
94
|
+
u8(value: number): void {
|
|
95
|
+
const offset = this.#reserve(1);
|
|
96
|
+
this.#view.setUint8(offset, value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
u16(value: number): void {
|
|
100
|
+
const offset = this.#reserve(2);
|
|
101
|
+
this.#view.setUint16(offset, value, true);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
u32(value: number): void {
|
|
105
|
+
const offset = this.#reserve(4);
|
|
106
|
+
this.#view.setUint32(offset, value, true);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
u64(value: bigint): void {
|
|
110
|
+
const offset = this.#reserve(8);
|
|
111
|
+
this.#view.setBigUint64(offset, value, true);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Encode a host-issued identity exactly without a process-wide 32-bit ceiling. */
|
|
115
|
+
handle(value: number): void {
|
|
116
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new RangeError("invalid handle identity");
|
|
117
|
+
this.u64(BigInt(value));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
s64(value: bigint): void {
|
|
121
|
+
const offset = this.#reserve(8);
|
|
122
|
+
this.#view.setBigInt64(offset, value, true);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
raw(bytes: Uint8Array): void {
|
|
126
|
+
const offset = this.#reserve(bytes.length);
|
|
127
|
+
this.#bytes.set(bytes, offset);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
bytes(bytes: Uint8Array): void {
|
|
131
|
+
this.u32(bytes.length);
|
|
132
|
+
this.raw(bytes);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Return the written bytes as one exact Uint8Array. */
|
|
136
|
+
finish(): Uint8Array {
|
|
137
|
+
return this.#bytes.slice(0, this.#length);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Bounds-checked little-endian reader. */
|
|
142
|
+
export class Reader {
|
|
143
|
+
#view: DataView;
|
|
144
|
+
#offset = 0;
|
|
145
|
+
|
|
146
|
+
constructor(readonly source: Uint8Array) {
|
|
147
|
+
this.#view = new DataView(source.buffer, source.byteOffset, source.byteLength);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Advance over `count` bytes and return their start. */
|
|
151
|
+
#take(count: number): number {
|
|
152
|
+
const start = this.#offset;
|
|
153
|
+
if (start + count > this.source.length) throw new RangeError("standard packet is truncated");
|
|
154
|
+
this.#offset += count;
|
|
155
|
+
return start;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
u8(): number {
|
|
159
|
+
return this.#view.getUint8(this.#take(1));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
u16(): number {
|
|
163
|
+
return this.#view.getUint16(this.#take(2), true);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
u32(): number {
|
|
167
|
+
return this.#view.getUint32(this.#take(4), true);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
u64(): bigint {
|
|
171
|
+
return this.#view.getBigUint64(this.#take(8), true);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Decode a u64 host identity only when JavaScript can represent it exactly. */
|
|
175
|
+
handle(): number {
|
|
176
|
+
const value = this.u64();
|
|
177
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) throw new RangeError("invalid handle identity");
|
|
178
|
+
return Number(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
s64(): bigint {
|
|
182
|
+
return this.#view.getBigInt64(this.#take(8), true);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Copy one u32-prefixed byte list. */
|
|
186
|
+
bytes(): Uint8Array {
|
|
187
|
+
return this.raw(this.u32());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Copy exactly `length` bytes. */
|
|
191
|
+
raw(length: number): Uint8Array {
|
|
192
|
+
const start = this.#take(length);
|
|
193
|
+
return this.source.slice(start, start + length);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
string(): string {
|
|
197
|
+
return utf8Decode(this.bytes());
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Bytes not yet read. */
|
|
201
|
+
get remaining(): number {
|
|
202
|
+
return this.source.length - this.#offset;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Refuse trailing bytes so one packet has exactly one meaning. */
|
|
206
|
+
end(): void {
|
|
207
|
+
if (this.#offset !== this.source.length)
|
|
208
|
+
throw new RangeError("standard packet has trailing bytes");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const NONE = -1;
|
|
213
|
+
const U64_MAX = (1n << 64n) - 1n;
|
|
214
|
+
const S64_LIMIT = 1n << 63n;
|
|
215
|
+
|
|
216
|
+
const isSurrogate = (code: number) => code >= 0xd800 && code <= 0xdfff;
|
|
217
|
+
|
|
218
|
+
/** Refuse a non-integer or out-of-range number instead of wrapping it. */
|
|
219
|
+
function integer(value: unknown, minimum: number, maximum: number): number {
|
|
220
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < minimum || value > maximum)
|
|
221
|
+
throw new RangeError(`integer ${String(value)} is outside ${minimum}..=${maximum}`);
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Refuse a 64-bit value that is not exactly representable instead of wrapping it. */
|
|
226
|
+
function big(value: unknown, minimum: bigint, maximum: bigint): bigint {
|
|
227
|
+
const exact =
|
|
228
|
+
typeof value === "bigint"
|
|
229
|
+
? value
|
|
230
|
+
: typeof value === "number" && Number.isSafeInteger(value)
|
|
231
|
+
? BigInt(value)
|
|
232
|
+
: undefined;
|
|
233
|
+
if (exact === undefined || exact < minimum || exact > maximum)
|
|
234
|
+
throw new RangeError(`64-bit integer ${String(value)} is outside ${minimum}..=${maximum}`);
|
|
235
|
+
return exact;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Encode one value of generated type `index`. */
|
|
239
|
+
export function encode(w: Writer, index: number, value: any, handles: HandleCodec): void {
|
|
240
|
+
const [kind, a, b] = TYPES[index]!;
|
|
241
|
+
switch (kind) {
|
|
242
|
+
case "bool":
|
|
243
|
+
return w.u8(value ? 1 : 0);
|
|
244
|
+
case "u8":
|
|
245
|
+
return w.u8(integer(value, 0, 0xff));
|
|
246
|
+
case "s8":
|
|
247
|
+
return w.u8(integer(value, -0x80, 0x7f) & 0xff);
|
|
248
|
+
case "u16":
|
|
249
|
+
return w.u16(integer(value, 0, 0xffff));
|
|
250
|
+
case "s16":
|
|
251
|
+
return w.u16(integer(value, -0x8000, 0x7fff) & 0xffff);
|
|
252
|
+
case "u32":
|
|
253
|
+
return w.u32(integer(value, 0, 0xffffffff));
|
|
254
|
+
case "s32":
|
|
255
|
+
return w.u32(integer(value, -0x80000000, 0x7fffffff) >>> 0);
|
|
256
|
+
case "u64":
|
|
257
|
+
return w.u64(big(value, 0n, U64_MAX));
|
|
258
|
+
case "s64":
|
|
259
|
+
return w.s64(big(value, -S64_LIMIT, S64_LIMIT - 1n));
|
|
260
|
+
case "char": {
|
|
261
|
+
const text = String(value);
|
|
262
|
+
const code = text.codePointAt(0);
|
|
263
|
+
if (code === undefined || text.length !== (code > 0xffff ? 2 : 1) || isSurrogate(code))
|
|
264
|
+
throw new TypeError("char requires one Unicode scalar value");
|
|
265
|
+
return w.u32(code);
|
|
266
|
+
}
|
|
267
|
+
case "string":
|
|
268
|
+
return w.bytes(utf8Encode(String(value)));
|
|
269
|
+
case "bytes":
|
|
270
|
+
if (!(value instanceof Uint8Array)) throw new TypeError("list<u8> requires a Uint8Array");
|
|
271
|
+
return w.bytes(value);
|
|
272
|
+
case "list":
|
|
273
|
+
w.u32(value.length);
|
|
274
|
+
for (const item of value) encode(w, a as number, item, handles);
|
|
275
|
+
return;
|
|
276
|
+
case "tuple":
|
|
277
|
+
(a as number[]).forEach((type, position) => encode(w, type, value[position], handles));
|
|
278
|
+
return;
|
|
279
|
+
case "record":
|
|
280
|
+
for (const [name, type] of a as [string, number][]) encode(w, type, value[name], handles);
|
|
281
|
+
return;
|
|
282
|
+
case "flags": {
|
|
283
|
+
let bits = 0;
|
|
284
|
+
(a as string[]).forEach((name, bit) => {
|
|
285
|
+
if (value?.[name]) bits |= 1 << bit;
|
|
286
|
+
});
|
|
287
|
+
return w.u32(bits >>> 0);
|
|
288
|
+
}
|
|
289
|
+
case "enum": {
|
|
290
|
+
const position = (a as string[]).indexOf(value);
|
|
291
|
+
if (position < 0) throw new TypeError(`unknown enum case ${value}`);
|
|
292
|
+
return w.u32(position);
|
|
293
|
+
}
|
|
294
|
+
case "variant": {
|
|
295
|
+
const cases = a as [string, number][];
|
|
296
|
+
const position = cases.findIndex(([tag]) => tag === value?.tag);
|
|
297
|
+
if (position < 0) throw new TypeError(`unknown variant case ${value?.tag}`);
|
|
298
|
+
w.u32(position);
|
|
299
|
+
if (cases[position]![1] !== NONE) encode(w, cases[position]![1], value.val, handles);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
case "option":
|
|
303
|
+
if (value === undefined || value === null) return w.u8(0);
|
|
304
|
+
w.u8(1);
|
|
305
|
+
return encode(w, a as number, value, handles);
|
|
306
|
+
case "result": {
|
|
307
|
+
const ok = value?.tag === "ok";
|
|
308
|
+
if (!ok && value?.tag !== "err") throw new TypeError("result requires { tag, val }");
|
|
309
|
+
w.u8(ok ? 0 : 1);
|
|
310
|
+
const type = (ok ? a : b) as number;
|
|
311
|
+
if (type !== NONE) encode(w, type, value.val, handles);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
default:
|
|
315
|
+
return w.handle(handles.handle(kind, a, value));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Decode one value of generated type `index`. */
|
|
320
|
+
export function decode(r: Reader, index: number, handles: HandleCodec): any {
|
|
321
|
+
const [kind, a, b] = TYPES[index]!;
|
|
322
|
+
switch (kind) {
|
|
323
|
+
case "bool": {
|
|
324
|
+
const tag = r.u8();
|
|
325
|
+
if (tag > 1) throw new RangeError("invalid bool");
|
|
326
|
+
return tag === 1;
|
|
327
|
+
}
|
|
328
|
+
case "u8":
|
|
329
|
+
return r.u8();
|
|
330
|
+
case "s8":
|
|
331
|
+
return (r.u8() << 24) >> 24;
|
|
332
|
+
case "u16":
|
|
333
|
+
return r.u16();
|
|
334
|
+
case "s16":
|
|
335
|
+
return (r.u16() << 16) >> 16;
|
|
336
|
+
case "u32":
|
|
337
|
+
return r.u32();
|
|
338
|
+
case "s32":
|
|
339
|
+
return r.u32() | 0;
|
|
340
|
+
case "u64":
|
|
341
|
+
return r.u64();
|
|
342
|
+
case "s64":
|
|
343
|
+
return r.s64();
|
|
344
|
+
case "char": {
|
|
345
|
+
const code = r.u32();
|
|
346
|
+
if (code > 0x10ffff || isSurrogate(code)) throw new RangeError("invalid char");
|
|
347
|
+
return String.fromCodePoint(code);
|
|
348
|
+
}
|
|
349
|
+
case "string":
|
|
350
|
+
return r.string();
|
|
351
|
+
case "bytes":
|
|
352
|
+
return r.bytes();
|
|
353
|
+
case "list": {
|
|
354
|
+
const items = [];
|
|
355
|
+
for (let count = r.u32(); count > 0; count--) items.push(decode(r, a as number, handles));
|
|
356
|
+
return items;
|
|
357
|
+
}
|
|
358
|
+
case "tuple":
|
|
359
|
+
return (a as number[]).map((type) => decode(r, type, handles));
|
|
360
|
+
case "record": {
|
|
361
|
+
const record: Record<string, unknown> = {};
|
|
362
|
+
for (const [name, type] of a as [string, number][]) {
|
|
363
|
+
const field = decode(r, type, handles);
|
|
364
|
+
if (field !== undefined) record[name] = field;
|
|
365
|
+
}
|
|
366
|
+
return record;
|
|
367
|
+
}
|
|
368
|
+
case "flags": {
|
|
369
|
+
const bits = r.u32();
|
|
370
|
+
const names = a as string[];
|
|
371
|
+
if (names.length < 32 && bits >>> names.length !== 0)
|
|
372
|
+
throw new RangeError("unknown flag bits");
|
|
373
|
+
return Object.fromEntries(
|
|
374
|
+
(a as string[]).map((name, bit) => [name, (bits & (1 << bit)) !== 0]),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
case "enum": {
|
|
378
|
+
const name = (a as string[])[r.u32()];
|
|
379
|
+
if (name === undefined) throw new RangeError("unknown enum case");
|
|
380
|
+
return name;
|
|
381
|
+
}
|
|
382
|
+
case "variant": {
|
|
383
|
+
const entry = (a as [string, number][])[r.u32()];
|
|
384
|
+
if (!entry) throw new RangeError("unknown variant case");
|
|
385
|
+
const [tag, type] = entry;
|
|
386
|
+
return type === NONE ? { tag } : { tag, val: decode(r, type, handles) };
|
|
387
|
+
}
|
|
388
|
+
case "option": {
|
|
389
|
+
const tag = r.u8();
|
|
390
|
+
if (tag > 1) throw new RangeError("invalid option tag");
|
|
391
|
+
return tag === 0 ? undefined : decode(r, a as number, handles);
|
|
392
|
+
}
|
|
393
|
+
case "result": {
|
|
394
|
+
const tag = r.u8();
|
|
395
|
+
if (tag > 1) throw new RangeError("invalid result tag");
|
|
396
|
+
const ok = tag === 0;
|
|
397
|
+
const type = (ok ? a : b) as number;
|
|
398
|
+
return { tag: ok ? "ok" : "err", val: type === NONE ? undefined : decode(r, type, handles) };
|
|
399
|
+
}
|
|
400
|
+
default:
|
|
401
|
+
return handles.value(kind, a, r.handle());
|
|
402
|
+
}
|
|
403
|
+
}
|