@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,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wasi:filesystem@0.3.0` for trusted JCO bindings over the granted provider mounts
|
|
3
|
+
* (`filesystem.ts`: MemoryFS, OPFS or embedder providers).
|
|
4
|
+
*
|
|
5
|
+
* Tembra checks descriptor kind, rights and lexical confinement before any provider call, then
|
|
6
|
+
* providers enforce their own scope as well. Error codes follow upstream Wasmtime: a wrong
|
|
7
|
+
* descriptor kind or a missing file right is `bad-descriptor` (`not-directory` for directory
|
|
8
|
+
* methods), and a directory mutation without `mutate-directory` is `not-permitted`. Browser
|
|
9
|
+
* providers have no settable timestamps, hard links or symbolic links: those methods report
|
|
10
|
+
* `unsupported` after their authority checks.
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
FilesystemError,
|
|
14
|
+
type Descriptor as ProviderDescriptor,
|
|
15
|
+
type DirectoryDescriptor,
|
|
16
|
+
type FileDescriptor,
|
|
17
|
+
type FileStat,
|
|
18
|
+
type MountedFilesystem,
|
|
19
|
+
} from "../filesystem.ts";
|
|
20
|
+
import {
|
|
21
|
+
err,
|
|
22
|
+
guestBytes,
|
|
23
|
+
hostBytes,
|
|
24
|
+
hostItems,
|
|
25
|
+
ok,
|
|
26
|
+
type WasiHost,
|
|
27
|
+
type WasiInterfaces,
|
|
28
|
+
type WasiResult,
|
|
29
|
+
} from "./wasi-host.ts";
|
|
30
|
+
|
|
31
|
+
/** A mount granted to this revision. */
|
|
32
|
+
export interface StandardMount {
|
|
33
|
+
readonly path: string;
|
|
34
|
+
readonly writable: boolean;
|
|
35
|
+
readonly filesystem: MountedFilesystem;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface DescriptorFlags {
|
|
39
|
+
read: boolean;
|
|
40
|
+
write: boolean;
|
|
41
|
+
fileIntegritySync: boolean;
|
|
42
|
+
dataIntegritySync: boolean;
|
|
43
|
+
requestedWriteSync: boolean;
|
|
44
|
+
mutateDirectory: boolean;
|
|
45
|
+
}
|
|
46
|
+
type Timestamp = { tag: "no-change" | "now" | "timestamp"; val?: unknown };
|
|
47
|
+
const U64 = (1n << 64n) - 1n;
|
|
48
|
+
const failure = (code: string): WasiResult => err({ tag: code });
|
|
49
|
+
|
|
50
|
+
/** Map a provider failure to its WASI error code; anything else is `io`. */
|
|
51
|
+
function code(error: unknown): string {
|
|
52
|
+
return error instanceof FilesystemError ? error.code : "io";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Refuse a path lexically before any provider call; `undefined` admits it to the provider. */
|
|
56
|
+
function refusal(path: string): string | undefined {
|
|
57
|
+
if (path.includes("\0")) return "invalid";
|
|
58
|
+
if (path.startsWith("/")) return "not-permitted";
|
|
59
|
+
let depth = 0;
|
|
60
|
+
for (const part of path.split("/")) {
|
|
61
|
+
if (part === "" || part === ".") continue;
|
|
62
|
+
if (part !== "..") depth++;
|
|
63
|
+
else if (--depth < 0) return "not-permitted";
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function instant(milliseconds: number | undefined) {
|
|
69
|
+
if (milliseconds === undefined || !Number.isFinite(milliseconds)) return undefined;
|
|
70
|
+
const whole = Math.floor(milliseconds);
|
|
71
|
+
const seconds = Math.floor(whole / 1000);
|
|
72
|
+
return {
|
|
73
|
+
seconds: BigInt(seconds),
|
|
74
|
+
nanoseconds: (whole - seconds * 1000) * 1_000_000,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const descriptorType = (type: "file" | "directory") => ({
|
|
79
|
+
tag: type === "file" ? "regular-file" : "directory",
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const descriptorStat = (stat: FileStat) => ({
|
|
83
|
+
type: descriptorType(stat.type),
|
|
84
|
+
linkCount: 1n,
|
|
85
|
+
size: stat.size,
|
|
86
|
+
dataAccessTimestamp: undefined,
|
|
87
|
+
dataModificationTimestamp: instant(stat.modified),
|
|
88
|
+
statusChangeTimestamp: undefined,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/** FNV-1a over the metadata browser providers report; equal metadata gives an equal hash. */
|
|
92
|
+
function metadataHash(stat: FileStat) {
|
|
93
|
+
const hash = (seed: bigint) => {
|
|
94
|
+
let value = seed;
|
|
95
|
+
const mix = (word: bigint) => {
|
|
96
|
+
for (let shift = 0n; shift < 64n; shift += 8n) {
|
|
97
|
+
value ^= (word >> shift) & 0xffn;
|
|
98
|
+
value = (value * 0x100000001b3n) & U64;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
mix(stat.type === "file" ? 1n : 2n);
|
|
102
|
+
mix(BigInt.asUintN(64, stat.size));
|
|
103
|
+
mix(stat.modified === undefined ? U64 : BigInt.asUintN(64, BigInt(Math.floor(stat.modified))));
|
|
104
|
+
return value;
|
|
105
|
+
};
|
|
106
|
+
return { lower: hash(0xcbf29ce484222325n), upper: hash(0x84222325cbf29ce4n) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The `wasi:filesystem/types` and `preopens` import objects of one instance. */
|
|
110
|
+
export function filesystemImports(
|
|
111
|
+
host: WasiHost,
|
|
112
|
+
mounts: () => readonly StandardMount[],
|
|
113
|
+
): WasiInterfaces<"types" | "preopens"> {
|
|
114
|
+
/** A provider descriptor behind one guest handle; streams retain it independently. */
|
|
115
|
+
class Descriptor {
|
|
116
|
+
#users = 1;
|
|
117
|
+
#closed = false;
|
|
118
|
+
constructor(
|
|
119
|
+
readonly provider: ProviderDescriptor,
|
|
120
|
+
readonly flags: Readonly<DescriptorFlags>,
|
|
121
|
+
) {}
|
|
122
|
+
|
|
123
|
+
get file(): FileDescriptor | undefined {
|
|
124
|
+
return this.provider.type === "file" ? this.provider : undefined;
|
|
125
|
+
}
|
|
126
|
+
get directory(): DirectoryDescriptor | undefined {
|
|
127
|
+
return this.provider.type === "directory" ? this.provider : undefined;
|
|
128
|
+
}
|
|
129
|
+
get mutable(): boolean {
|
|
130
|
+
return this.provider.type === "directory" && this.flags.mutateDirectory;
|
|
131
|
+
}
|
|
132
|
+
retain(): void {
|
|
133
|
+
this.#users++;
|
|
134
|
+
}
|
|
135
|
+
release(): void {
|
|
136
|
+
if (--this.#users === 0) host.cleanup(() => this.provider.close() as unknown);
|
|
137
|
+
}
|
|
138
|
+
/** The guest dropped its handle; streams may still hold the provider descriptor. */
|
|
139
|
+
[Symbol.dispose](): void {
|
|
140
|
+
if (this.#closed) return;
|
|
141
|
+
this.#closed = true;
|
|
142
|
+
this.release();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
readViaStream(offset: bigint) {
|
|
146
|
+
const file = this.file;
|
|
147
|
+
if (!file || !this.flags.read) return failedOutput("bad-descriptor");
|
|
148
|
+
let position = offset;
|
|
149
|
+
return output(this, async (limit) => {
|
|
150
|
+
const bytes = await host.physical(() => file.read(position, limit));
|
|
151
|
+
position += BigInt(bytes.byteLength);
|
|
152
|
+
return bytes;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
writeViaStream(data: unknown, offset: bigint) {
|
|
156
|
+
return consume(this, data, offset);
|
|
157
|
+
}
|
|
158
|
+
appendViaStream(data: unknown) {
|
|
159
|
+
return consume(this, data, undefined);
|
|
160
|
+
}
|
|
161
|
+
readDirectory() {
|
|
162
|
+
const directory = this.directory;
|
|
163
|
+
if (!directory)
|
|
164
|
+
return [
|
|
165
|
+
hostItems(
|
|
166
|
+
async () => undefined,
|
|
167
|
+
() => {},
|
|
168
|
+
host.retired,
|
|
169
|
+
),
|
|
170
|
+
Promise.resolve(failure("not-directory")),
|
|
171
|
+
];
|
|
172
|
+
let entries: ReturnType<DirectoryDescriptor["readDirectory"]>;
|
|
173
|
+
try {
|
|
174
|
+
entries = directory.readDirectory();
|
|
175
|
+
} catch (error) {
|
|
176
|
+
return [
|
|
177
|
+
hostItems(
|
|
178
|
+
async () => undefined,
|
|
179
|
+
() => {},
|
|
180
|
+
host.retired,
|
|
181
|
+
),
|
|
182
|
+
Promise.resolve(failure(code(error))),
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
let settle!: (value: WasiResult) => void;
|
|
186
|
+
const result = new Promise<WasiResult>((resolve) => (settle = resolve));
|
|
187
|
+
let done = false;
|
|
188
|
+
this.retain();
|
|
189
|
+
const stream = hostItems(
|
|
190
|
+
async () => {
|
|
191
|
+
const next = await host.physical(() => entries.next());
|
|
192
|
+
if (next.done) {
|
|
193
|
+
done = true;
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
type: descriptorType(next.value.type),
|
|
198
|
+
name: next.value.name,
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
(end, error) => {
|
|
202
|
+
if (!done) host.cleanup(() => entries.return?.());
|
|
203
|
+
this.release();
|
|
204
|
+
settle(end === "failed" ? failure(code(error)) : ok());
|
|
205
|
+
},
|
|
206
|
+
host.retired,
|
|
207
|
+
);
|
|
208
|
+
return [stream, result];
|
|
209
|
+
}
|
|
210
|
+
async advise() {
|
|
211
|
+
return this.file ? ok() : failure("bad-descriptor");
|
|
212
|
+
}
|
|
213
|
+
sync() {
|
|
214
|
+
return attempt(this.provider, (p) => p.sync());
|
|
215
|
+
}
|
|
216
|
+
syncData() {
|
|
217
|
+
return attempt(this.provider, (p) => p.sync());
|
|
218
|
+
}
|
|
219
|
+
async getFlags() {
|
|
220
|
+
return ok({ ...this.flags });
|
|
221
|
+
}
|
|
222
|
+
async getType() {
|
|
223
|
+
return ok(descriptorType(this.provider.type));
|
|
224
|
+
}
|
|
225
|
+
setSize(size: bigint) {
|
|
226
|
+
return attempt(
|
|
227
|
+
this.file && this.flags.write ? this.file : failure("bad-descriptor"),
|
|
228
|
+
(file) => file.setSize(size),
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
async setTimes(accessed: Timestamp, modified: Timestamp) {
|
|
232
|
+
if (this.file ? !this.flags.write : !this.mutable)
|
|
233
|
+
return failure(this.file ? "bad-descriptor" : "not-permitted");
|
|
234
|
+
return accessed.tag === "no-change" && modified.tag === "no-change"
|
|
235
|
+
? ok()
|
|
236
|
+
: failure("unsupported");
|
|
237
|
+
}
|
|
238
|
+
async setTimesAt(_flags: unknown, path: string, accessed: Timestamp, modified: Timestamp) {
|
|
239
|
+
const checked = mutableAt(this, [path]);
|
|
240
|
+
if (isResult(checked)) return checked;
|
|
241
|
+
if (accessed.tag !== "no-change" || modified.tag !== "no-change")
|
|
242
|
+
return failure("unsupported");
|
|
243
|
+
return attempt(checked, (directory) => directory.statAt(path).then(() => undefined));
|
|
244
|
+
}
|
|
245
|
+
stat() {
|
|
246
|
+
return attempt(this.provider, (p) => p.stat().then(descriptorStat));
|
|
247
|
+
}
|
|
248
|
+
statAt(_flags: unknown, path: string) {
|
|
249
|
+
return attempt(directoryAt(this, path), (directory) =>
|
|
250
|
+
directory.statAt(path).then(descriptorStat),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
async openAt(
|
|
254
|
+
_flags: unknown,
|
|
255
|
+
path: string,
|
|
256
|
+
open: {
|
|
257
|
+
create?: boolean;
|
|
258
|
+
directory?: boolean;
|
|
259
|
+
exclusive?: boolean;
|
|
260
|
+
truncate?: boolean;
|
|
261
|
+
},
|
|
262
|
+
flags: Partial<DescriptorFlags>,
|
|
263
|
+
) {
|
|
264
|
+
const write = Boolean(flags.write || flags.mutateDirectory);
|
|
265
|
+
const directory = this.directory;
|
|
266
|
+
if (!directory) return failure("not-directory");
|
|
267
|
+
// Write authority comes only from a mutable directory, checked before the provider.
|
|
268
|
+
if ((open.create || open.truncate || write) && !this.mutable) return failure("not-permitted");
|
|
269
|
+
const refused = refusal(path);
|
|
270
|
+
if (refused) return failure(refused);
|
|
271
|
+
let opened: ProviderDescriptor;
|
|
272
|
+
try {
|
|
273
|
+
opened = await host.physical(() =>
|
|
274
|
+
directory.openAt(path, {
|
|
275
|
+
create: Boolean(open.create),
|
|
276
|
+
exclusive: Boolean(open.exclusive),
|
|
277
|
+
truncate: Boolean(open.truncate),
|
|
278
|
+
directory: Boolean(open.directory),
|
|
279
|
+
write,
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
return failure(code(error));
|
|
284
|
+
}
|
|
285
|
+
const granted = opened.writable && write;
|
|
286
|
+
return ok(
|
|
287
|
+
new Descriptor(opened, {
|
|
288
|
+
read: opened.type === "directory" || Boolean(flags.read),
|
|
289
|
+
write: opened.type === "file" && granted,
|
|
290
|
+
fileIntegritySync: Boolean(flags.fileIntegritySync),
|
|
291
|
+
dataIntegritySync: Boolean(flags.dataIntegritySync),
|
|
292
|
+
requestedWriteSync: Boolean(flags.requestedWriteSync),
|
|
293
|
+
mutateDirectory: opened.type === "directory" && granted,
|
|
294
|
+
}),
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
async readlinkAt(path: string) {
|
|
298
|
+
// There are no symbolic links: an existing entry is not one (`invalid`), as in POSIX.
|
|
299
|
+
const result = await attempt(directoryAt(this, path), (directory) => directory.statAt(path));
|
|
300
|
+
return result.tag === "ok" ? failure("invalid") : result;
|
|
301
|
+
}
|
|
302
|
+
createDirectoryAt(path: string) {
|
|
303
|
+
return attempt(mutableAt(this, [path]), (directory) => directory.createDirectoryAt(path));
|
|
304
|
+
}
|
|
305
|
+
removeDirectoryAt(path: string) {
|
|
306
|
+
return attempt(mutableAt(this, [path]), (directory) => directory.removeDirectoryAt(path));
|
|
307
|
+
}
|
|
308
|
+
unlinkFileAt(path: string) {
|
|
309
|
+
return attempt(mutableAt(this, [path]), (directory) => directory.unlinkFileAt(path));
|
|
310
|
+
}
|
|
311
|
+
renameAt(from: string, target: Descriptor, to: string) {
|
|
312
|
+
own(target);
|
|
313
|
+
return attempt(mutableAt(this, [from, to], target), (directory) =>
|
|
314
|
+
directory.renameAt(from, target.directory!, to),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
async linkAt(_flags: unknown, from: string, target: Descriptor, to: string) {
|
|
318
|
+
own(target);
|
|
319
|
+
const checked = mutableAt(this, [from, to], target);
|
|
320
|
+
return isResult(checked) ? checked : failure("unsupported");
|
|
321
|
+
}
|
|
322
|
+
async symlinkAt(_from: string, to: string) {
|
|
323
|
+
const checked = mutableAt(this, [to]);
|
|
324
|
+
return isResult(checked) ? checked : failure("unsupported");
|
|
325
|
+
}
|
|
326
|
+
async isSameObject(other: Descriptor) {
|
|
327
|
+
own(other);
|
|
328
|
+
try {
|
|
329
|
+
return await host.physical(() => this.provider.isSameObject(other.provider));
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (!(error instanceof FilesystemError)) throw error;
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
metadataHash() {
|
|
336
|
+
return attempt(this.provider, (p) => p.stat().then(metadataHash));
|
|
337
|
+
}
|
|
338
|
+
metadataHashAt(_flags: unknown, path: string) {
|
|
339
|
+
return attempt(directoryAt(this, path), (directory) =>
|
|
340
|
+
directory.statAt(path).then(metadataHash),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** A borrowed descriptor must be one this instance issued. */
|
|
346
|
+
const own = (value: unknown): void => {
|
|
347
|
+
if (!(value instanceof Descriptor)) throw new TypeError("Foreign filesystem descriptor");
|
|
348
|
+
};
|
|
349
|
+
const isResult = (value: unknown): value is WasiResult =>
|
|
350
|
+
typeof value === "object" && value !== null && "tag" in value && !(value instanceof Descriptor);
|
|
351
|
+
|
|
352
|
+
function directoryAt(self: Descriptor, ...paths: string[]): DirectoryDescriptor | WasiResult {
|
|
353
|
+
const directory = self.directory;
|
|
354
|
+
if (!directory) return failure("not-directory");
|
|
355
|
+
for (const path of paths) {
|
|
356
|
+
const refused = refusal(path);
|
|
357
|
+
if (refused) return failure(refused);
|
|
358
|
+
}
|
|
359
|
+
return directory;
|
|
360
|
+
}
|
|
361
|
+
function mutableAt(
|
|
362
|
+
self: Descriptor,
|
|
363
|
+
paths: string[],
|
|
364
|
+
other?: Descriptor,
|
|
365
|
+
): DirectoryDescriptor | WasiResult {
|
|
366
|
+
if (!self.directory || (other && !other.directory)) return failure("not-directory");
|
|
367
|
+
if (!self.mutable || (other && !other.mutable)) return failure("not-permitted");
|
|
368
|
+
return directoryAt(self, ...paths);
|
|
369
|
+
}
|
|
370
|
+
async function attempt<T>(
|
|
371
|
+
checked: T | WasiResult,
|
|
372
|
+
operation: (target: T) => Promise<unknown>,
|
|
373
|
+
): Promise<WasiResult> {
|
|
374
|
+
if (isResult(checked)) return checked;
|
|
375
|
+
try {
|
|
376
|
+
return ok(await host.physical(() => operation(checked)));
|
|
377
|
+
} catch (error) {
|
|
378
|
+
if (!(error instanceof FilesystemError) && host.retired.aborted) throw error;
|
|
379
|
+
return failure(code(error));
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/** A stream method's failure is reported through its future, as upstream does. */
|
|
383
|
+
function failedOutput(reason: string) {
|
|
384
|
+
return [
|
|
385
|
+
hostBytes(
|
|
386
|
+
async () => undefined,
|
|
387
|
+
() => {},
|
|
388
|
+
host.retired,
|
|
389
|
+
),
|
|
390
|
+
Promise.resolve(failure(reason)),
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
function output(self: Descriptor, next: (limit: number) => Promise<Uint8Array>) {
|
|
394
|
+
let settle!: (value: WasiResult) => void;
|
|
395
|
+
const result = new Promise<WasiResult>((resolve) => (settle = resolve));
|
|
396
|
+
// The stream keeps its descriptor's provider object open after the handle is dropped.
|
|
397
|
+
self.retain();
|
|
398
|
+
const stream = hostBytes(
|
|
399
|
+
next,
|
|
400
|
+
(end, error) => {
|
|
401
|
+
self.release();
|
|
402
|
+
settle(end === "failed" ? failure(code(error)) : ok());
|
|
403
|
+
},
|
|
404
|
+
host.retired,
|
|
405
|
+
);
|
|
406
|
+
return [stream, result];
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Consume a guest stream into a file; the future reports the outcome. A provider with
|
|
410
|
+
* `writeStream` receives the whole stream in one call (one OPFS writer and commit); otherwise
|
|
411
|
+
* each chunk is a sequential `write`/`append`. The descriptor stays open and the work stays
|
|
412
|
+
* physically owned until the provider settles.
|
|
413
|
+
*/
|
|
414
|
+
function consume(
|
|
415
|
+
self: Descriptor,
|
|
416
|
+
data: unknown,
|
|
417
|
+
offset: bigint | undefined,
|
|
418
|
+
): Promise<WasiResult> {
|
|
419
|
+
const input = guestBytes(data, host.retired);
|
|
420
|
+
const file = self.file;
|
|
421
|
+
if (!file || !self.flags.write) {
|
|
422
|
+
void input.return!();
|
|
423
|
+
return Promise.resolve(failure("bad-descriptor"));
|
|
424
|
+
}
|
|
425
|
+
self.retain();
|
|
426
|
+
const outcome = (async (): Promise<WasiResult> => {
|
|
427
|
+
try {
|
|
428
|
+
if (file.writeStream) {
|
|
429
|
+
await host.physical(() => file.writeStream!(input, offset));
|
|
430
|
+
return ok();
|
|
431
|
+
}
|
|
432
|
+
let position = offset;
|
|
433
|
+
for await (const bytes of input) {
|
|
434
|
+
if (position === undefined) await host.physical(() => file.append(bytes));
|
|
435
|
+
else {
|
|
436
|
+
const at = position;
|
|
437
|
+
await host.physical(() => file.write(at, bytes));
|
|
438
|
+
position += BigInt(bytes.byteLength);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return ok();
|
|
442
|
+
} catch (error) {
|
|
443
|
+
return failure(code(error));
|
|
444
|
+
} finally {
|
|
445
|
+
void input.return!();
|
|
446
|
+
self.release();
|
|
447
|
+
}
|
|
448
|
+
})();
|
|
449
|
+
host.cleanup(() => outcome);
|
|
450
|
+
return outcome;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const root = (mount: StandardMount) =>
|
|
454
|
+
new Descriptor(mount.filesystem.openRoot(), {
|
|
455
|
+
read: true,
|
|
456
|
+
write: false,
|
|
457
|
+
fileIntegritySync: false,
|
|
458
|
+
dataIntegritySync: false,
|
|
459
|
+
requestedWriteSync: false,
|
|
460
|
+
mutateDirectory: mount.writable,
|
|
461
|
+
});
|
|
462
|
+
return {
|
|
463
|
+
types: {
|
|
464
|
+
Descriptor,
|
|
465
|
+
filesystemErrorCode: () => undefined,
|
|
466
|
+
},
|
|
467
|
+
preopens: {
|
|
468
|
+
// Like upstream, every call issues fresh owned descriptors for the preopened mounts.
|
|
469
|
+
getDirectories: () => mounts().map((mount) => [root(mount), mount.path]),
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared pieces of the WASI 0.3 adapters for trusted JCO bindings: physical accounting of
|
|
3
|
+
* provider work, revision retirement, and the two directions of `stream<u8>`.
|
|
4
|
+
*
|
|
5
|
+
* JCO hands an imported function a guest `stream<u8>` as an object with `read({ count })`
|
|
6
|
+
* (resolving `{ value, done }`) and `Symbol.dispose`; it accepts any async iterable from the host
|
|
7
|
+
* as a stream it produces, and any thenable as a future. A pending guest-stream read is never
|
|
8
|
+
* awaited without a bound: after a guest trap JCO never settles it, so every read races the
|
|
9
|
+
* revision's retirement signal, which aborts on quarantine and disposal.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Host services an adapter uses; implemented by the component port. */
|
|
13
|
+
export interface WasiHost {
|
|
14
|
+
/**
|
|
15
|
+
* Run one provider operation under physical accounting: it belongs to the current callback
|
|
16
|
+
* (or the revision between callbacks) until it settles, even when the caller stops waiting.
|
|
17
|
+
* The returned promise rejects promptly when the current callback is cancelled.
|
|
18
|
+
*/
|
|
19
|
+
physical<T>(operation: () => Promise<T>): Promise<T>;
|
|
20
|
+
/** Retain cleanup/background work in its callback, or in the revision between callbacks. */
|
|
21
|
+
cleanup(operation: () => unknown): void;
|
|
22
|
+
/** Aborts when the revision quarantines or retires. */
|
|
23
|
+
readonly retired: AbortSignal;
|
|
24
|
+
/** Cancellation of the callback serving the current operation (its end, its context, retirement). */
|
|
25
|
+
signal(): AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Import objects of one package, keyed by interface; resource classes stay private. */
|
|
29
|
+
export type WasiInterfaces<K extends string> = Record<K, Record<string, unknown>>;
|
|
30
|
+
|
|
31
|
+
/** WASI `result` values in JCO form. */
|
|
32
|
+
export type WasiResult = { tag: "ok"; val?: unknown } | { tag: "err"; val: unknown };
|
|
33
|
+
export const ok = (val?: unknown): WasiResult =>
|
|
34
|
+
val === undefined ? { tag: "ok" } : { tag: "ok", val };
|
|
35
|
+
export const err = (code: unknown): WasiResult => ({ tag: "err", val: code });
|
|
36
|
+
|
|
37
|
+
export const cancelled = (): Error => new Error("Operation cancelled");
|
|
38
|
+
|
|
39
|
+
/** Reject when `signal` aborts; the operation's own settlement is unaffected. */
|
|
40
|
+
export function abortable<T>(operation: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
|
|
41
|
+
if (!signal) return operation;
|
|
42
|
+
if (signal.aborted) return Promise.reject(cancelled());
|
|
43
|
+
return new Promise<T>((resolve, reject) => {
|
|
44
|
+
const abort = () => reject(cancelled());
|
|
45
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
46
|
+
operation.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Run a closing operation (a provider close, cancel or abort) as physical work. Started while a
|
|
52
|
+
* callback is attributed, it belongs to that callback, which then completes only after it settled
|
|
53
|
+
* (bounded by its deadline), so the next callback never overlaps it. Otherwise, and after
|
|
54
|
+
* retirement, the revision owns it until disposal.
|
|
55
|
+
*/
|
|
56
|
+
export function closing(host: WasiHost, operation: () => unknown): void {
|
|
57
|
+
host.cleanup(operation);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A JCO guest stream as seen by the host. */
|
|
61
|
+
interface GuestStream {
|
|
62
|
+
read(options: { count: number }): Promise<{ value?: unknown; done?: boolean }>;
|
|
63
|
+
[Symbol.dispose]?(): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One chunk of a JCO `stream<u8>` read. */
|
|
67
|
+
function chunk(value: unknown): Uint8Array {
|
|
68
|
+
if (value instanceof Uint8Array) return value;
|
|
69
|
+
if (Array.isArray(value)) return Uint8Array.from(value as number[]);
|
|
70
|
+
if (ArrayBuffer.isView(value))
|
|
71
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
72
|
+
if (value === undefined) return new Uint8Array(0);
|
|
73
|
+
throw new TypeError("byte stream values must be bytes");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Largest chunk requested from a guest stream at once. */
|
|
77
|
+
export const STREAM_CHUNK = 64 * 1024;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Consume a guest `stream<u8>` as chunks pulled on demand (backpressure). Every read is bounded
|
|
81
|
+
* by `retired`; `return()` (an early stop by the consumer) drops the guest stream.
|
|
82
|
+
*/
|
|
83
|
+
export function guestBytes(
|
|
84
|
+
stream: unknown,
|
|
85
|
+
retired: AbortSignal,
|
|
86
|
+
): AsyncIterableIterator<Uint8Array> {
|
|
87
|
+
const source = stream as GuestStream;
|
|
88
|
+
if (typeof source?.read !== "function") throw new TypeError("expected a guest byte stream");
|
|
89
|
+
let finished = false;
|
|
90
|
+
const finish = () => {
|
|
91
|
+
if (finished) return;
|
|
92
|
+
finished = true;
|
|
93
|
+
try {
|
|
94
|
+
source[Symbol.dispose]?.();
|
|
95
|
+
} catch {
|
|
96
|
+
/* A trapped instance may refuse the drop; the stream is gone either way. */
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const iterator: AsyncIterableIterator<Uint8Array> = {
|
|
100
|
+
[Symbol.asyncIterator]: () => iterator,
|
|
101
|
+
async next() {
|
|
102
|
+
while (!finished) {
|
|
103
|
+
let result: { value?: unknown; done?: boolean };
|
|
104
|
+
try {
|
|
105
|
+
result = await abortable(source.read({ count: STREAM_CHUNK }), retired);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
finish();
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
const bytes = chunk(result.value);
|
|
111
|
+
if (bytes.byteLength) return { done: false, value: bytes };
|
|
112
|
+
if (result.done) break;
|
|
113
|
+
}
|
|
114
|
+
finish();
|
|
115
|
+
return { done: true, value: undefined };
|
|
116
|
+
},
|
|
117
|
+
async return() {
|
|
118
|
+
finish();
|
|
119
|
+
return { done: true, value: undefined };
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
return iterator;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A host-produced `stream<u8>` for JCO: chunks come from `next(limit)` (`undefined` or empty is
|
|
127
|
+
* the end). `close` runs exactly once with how the stream ended. A producer failure ends the
|
|
128
|
+
* stream and is reported only through `close` (whose owner settles the stream's companion
|
|
129
|
+
* future with the error, as WASI streams carry no error); it is never thrown to JCO, which would
|
|
130
|
+
* turn it into a guest trap.
|
|
131
|
+
*/
|
|
132
|
+
export function hostBytes(
|
|
133
|
+
next: (limit: number) => Promise<Uint8Array | undefined>,
|
|
134
|
+
close: (end: "eof" | "dropped" | "failed", failure?: unknown) => void,
|
|
135
|
+
retired: AbortSignal,
|
|
136
|
+
): AsyncIterableIterator<Uint8Array> {
|
|
137
|
+
let ended = false;
|
|
138
|
+
const end = (how: "eof" | "dropped" | "failed", failure?: unknown) => {
|
|
139
|
+
if (ended) return;
|
|
140
|
+
ended = true;
|
|
141
|
+
close(how, failure);
|
|
142
|
+
};
|
|
143
|
+
retired.addEventListener("abort", () => end("dropped"), { once: true });
|
|
144
|
+
const iterator: AsyncIterableIterator<Uint8Array> = {
|
|
145
|
+
[Symbol.asyncIterator]: () => iterator,
|
|
146
|
+
async next() {
|
|
147
|
+
if (ended) return { done: true, value: undefined };
|
|
148
|
+
try {
|
|
149
|
+
const bytes = await abortable(next(STREAM_CHUNK), retired);
|
|
150
|
+
if (bytes && bytes.byteLength) return { done: false, value: bytes };
|
|
151
|
+
end("eof");
|
|
152
|
+
} catch (error) {
|
|
153
|
+
end(retired.aborted ? "dropped" : "failed", error);
|
|
154
|
+
}
|
|
155
|
+
return { done: true, value: undefined };
|
|
156
|
+
},
|
|
157
|
+
async return() {
|
|
158
|
+
end("dropped");
|
|
159
|
+
return { done: true, value: undefined };
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
return iterator;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A host-produced typed `stream<T>` whose items come one at a time from `next()`; failures as `hostBytes`. */
|
|
166
|
+
export function hostItems(
|
|
167
|
+
next: () => Promise<unknown>,
|
|
168
|
+
close: (end: "eof" | "dropped" | "failed", failure?: unknown) => void,
|
|
169
|
+
retired: AbortSignal,
|
|
170
|
+
): AsyncIterableIterator<unknown> {
|
|
171
|
+
let ended = false;
|
|
172
|
+
const end = (how: "eof" | "dropped" | "failed", failure?: unknown) => {
|
|
173
|
+
if (ended) return;
|
|
174
|
+
ended = true;
|
|
175
|
+
close(how, failure);
|
|
176
|
+
};
|
|
177
|
+
retired.addEventListener("abort", () => end("dropped"), { once: true });
|
|
178
|
+
const iterator: AsyncIterableIterator<unknown> = {
|
|
179
|
+
[Symbol.asyncIterator]: () => iterator,
|
|
180
|
+
async next() {
|
|
181
|
+
if (ended) return { done: true, value: undefined };
|
|
182
|
+
try {
|
|
183
|
+
const item = await abortable(next(), retired);
|
|
184
|
+
if (item !== undefined) return { done: false, value: item };
|
|
185
|
+
end("eof");
|
|
186
|
+
} catch (error) {
|
|
187
|
+
end(retired.aborted ? "dropped" : "failed", error);
|
|
188
|
+
}
|
|
189
|
+
return { done: true, value: undefined };
|
|
190
|
+
},
|
|
191
|
+
async return() {
|
|
192
|
+
end("dropped");
|
|
193
|
+
return { done: true, value: undefined };
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
return iterator;
|
|
197
|
+
}
|