@syntropika/tembra-sdk 0.2.1 → 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 -3298
- 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 -3276
- 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 -3287
- 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 +9 -3
- 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 +218 -56
- 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 -293
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport lifetime of outgoing standard `wasi:http@0.3.0` in the browser: request and response
|
|
3
|
+
* bodies, the upload handed to an `HttpProvider`, `client.send`, and the binding of every
|
|
4
|
+
* generated `wasi:http` function. Value semantics (fields, options, targets) live in
|
|
5
|
+
* `standard-http-values.ts`; the provider contract lives in `http-provider.ts` and its Fetch
|
|
6
|
+
* adapter in `fetch-http-provider.ts`.
|
|
7
|
+
*
|
|
8
|
+
* Ownership: one exchange owns its `Upload` and its `ReceivedBody`. The host ends both itself,
|
|
9
|
+
* whether or not the provider cooperates: a failed or cancelled send, a response released or
|
|
10
|
+
* dropped before its end, or a failed response body aborts the upload (its input returns to the
|
|
11
|
+
* revision budget and the transmission future settles); revision retirement discards whatever is
|
|
12
|
+
* left. A clean end of the response body ends nothing else, so a duplex upload may still finish.
|
|
13
|
+
*
|
|
14
|
+
* Authority: `send` checks the exact origin grant (through `HttpClient`) before any provider
|
|
15
|
+
* call; a refusal is `error-code.HTTP-request-denied`, never a trap.
|
|
16
|
+
*/
|
|
17
|
+
import type { ErrorCode, Result } from "wasi:http/types@0.3.0";
|
|
18
|
+
import {
|
|
19
|
+
type HttpField,
|
|
20
|
+
type HttpProvider,
|
|
21
|
+
HttpProviderError,
|
|
22
|
+
type HttpProviderRequest,
|
|
23
|
+
type HttpProviderResponse,
|
|
24
|
+
} from "./http-provider.ts";
|
|
25
|
+
import { METHODS, TYPES } from "./standard.ts";
|
|
26
|
+
import { encode, type HandleCodec, Writer } from "./standard-codec.ts";
|
|
27
|
+
import {
|
|
28
|
+
type Body,
|
|
29
|
+
digits,
|
|
30
|
+
done,
|
|
31
|
+
err,
|
|
32
|
+
HttpFields,
|
|
33
|
+
HttpRequest,
|
|
34
|
+
HttpRequestOptions,
|
|
35
|
+
HttpResponse,
|
|
36
|
+
internal,
|
|
37
|
+
type Message,
|
|
38
|
+
ok,
|
|
39
|
+
type Outcome,
|
|
40
|
+
type Trailers,
|
|
41
|
+
} from "./standard-http-values.ts";
|
|
42
|
+
import {
|
|
43
|
+
bindResource,
|
|
44
|
+
type ByteProducer,
|
|
45
|
+
cancelled,
|
|
46
|
+
type FutureReader,
|
|
47
|
+
HostFuture,
|
|
48
|
+
type Implementation,
|
|
49
|
+
type InputStream,
|
|
50
|
+
OutputStream,
|
|
51
|
+
type StandardCall,
|
|
52
|
+
type StandardServices,
|
|
53
|
+
type StreamEnd,
|
|
54
|
+
} from "./standard-streams.ts";
|
|
55
|
+
|
|
56
|
+
const TYPES_INTERFACE = "wasi:http/types@0.3.0";
|
|
57
|
+
const CLIENT_INTERFACE = "wasi:http/client@0.3.0";
|
|
58
|
+
|
|
59
|
+
/** Grant-checked selection of the host transport. */
|
|
60
|
+
export interface HttpClient {
|
|
61
|
+
/** The provider granted for exactly this URL's origin, or `undefined` to deny. */
|
|
62
|
+
provider(url: URL): HttpProvider | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The `error-code` type, read from the generated `client.send` result. */
|
|
66
|
+
const ERROR_CODE = (() => {
|
|
67
|
+
const send = METHODS.find((m) => m.interface === CLIENT_INTERFACE && m.name === "send");
|
|
68
|
+
const result = send?.result === undefined ? undefined : TYPES[send.result];
|
|
69
|
+
return result?.[0] === "result" ? (result[2] as number) : undefined;
|
|
70
|
+
})();
|
|
71
|
+
const NO_HANDLES: HandleCodec = {
|
|
72
|
+
handle() {
|
|
73
|
+
throw new Error("an error code carries no handles");
|
|
74
|
+
},
|
|
75
|
+
value() {
|
|
76
|
+
throw new Error("an error code carries no handles");
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** A provider-supplied code only when it is a valid `error-code` value. */
|
|
81
|
+
function checkedCode(code: unknown): ErrorCode | undefined {
|
|
82
|
+
if (ERROR_CODE === undefined) return undefined;
|
|
83
|
+
try {
|
|
84
|
+
encode(new Writer(), ERROR_CODE, code, NO_HANDLES);
|
|
85
|
+
return code as ErrorCode;
|
|
86
|
+
} catch {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Map a failure to an `error-code`; `fallback` names the phase-specific generic code. */
|
|
92
|
+
function errorCode(error: unknown, fallback: (message: string) => ErrorCode = internal): ErrorCode {
|
|
93
|
+
if (error instanceof HttpProviderError) {
|
|
94
|
+
const code = checkedCode(error.code);
|
|
95
|
+
if (code) return code;
|
|
96
|
+
}
|
|
97
|
+
return fallback(error instanceof Error ? error.message : String(error));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Nanoseconds to timer milliseconds; a duration beyond the timer range means no timer. */
|
|
101
|
+
function millis(duration: bigint | undefined): number | undefined {
|
|
102
|
+
if (duration === undefined) return undefined;
|
|
103
|
+
const ms = (duration + 999_999n) / 1_000_000n;
|
|
104
|
+
return ms > 0x7fffffffn ? undefined : Number(ms);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------------------------
|
|
108
|
+
// guest bodies and uploads
|
|
109
|
+
|
|
110
|
+
const EMPTY: ByteProducer = { next: async () => undefined, close() {} };
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A body the guest builds: an optional input stream, a guest-written trailers future, and the
|
|
114
|
+
* transmission result future returned by `new`, which settles exactly once.
|
|
115
|
+
*/
|
|
116
|
+
class GuestBody implements Body {
|
|
117
|
+
#settle: ((outcome: Outcome) => void) | undefined;
|
|
118
|
+
readonly result: HostFuture<Outcome>;
|
|
119
|
+
|
|
120
|
+
constructor(
|
|
121
|
+
readonly contents: InputStream | undefined,
|
|
122
|
+
readonly trailers: FutureReader<Trailers>,
|
|
123
|
+
) {
|
|
124
|
+
this.result = new HostFuture(
|
|
125
|
+
new Promise<Outcome>((resolve) => {
|
|
126
|
+
this.#settle = resolve;
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Settle the transmission result once. */
|
|
132
|
+
settle(outcome: Outcome): void {
|
|
133
|
+
const settle = this.#settle;
|
|
134
|
+
this.#settle = undefined;
|
|
135
|
+
settle?.(outcome);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
consume(_host: StandardServices, res: FutureReader<Outcome>): [OutputStream, HostFuture] {
|
|
139
|
+
const contents = this.contents;
|
|
140
|
+
const stream = new OutputStream(
|
|
141
|
+
contents ? { next: () => contents.read(), close: () => contents.cancel() } : EMPTY,
|
|
142
|
+
() => {},
|
|
143
|
+
);
|
|
144
|
+
// The consumer reports its handling outcome as this body's transmission result.
|
|
145
|
+
res.take().then((outcome) => this.settle(outcome));
|
|
146
|
+
const trailers = this.trailers.take();
|
|
147
|
+
return [stream, new HostFuture(trailers, () => this.trailers.close())];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Stop the producer side; nothing this body accepted is transmitted. */
|
|
151
|
+
fail(code: ErrorCode): void {
|
|
152
|
+
this.contents?.cancel();
|
|
153
|
+
this.trailers.close();
|
|
154
|
+
this.settle(err(code));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Upstream: a dropped guest body closes its inputs and its result reports success. */
|
|
158
|
+
close(): void {
|
|
159
|
+
this.contents?.cancel();
|
|
160
|
+
this.trailers.close();
|
|
161
|
+
this.settle(done);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* One guest body handed to a provider. It has exactly one terminal state: `completed` (every
|
|
167
|
+
* byte, a matching `Content-Length` and an `ok` trailers result were handed over, and the body
|
|
168
|
+
* stream closed) or a failure (the body stream errored, `trailers` rejected and the guest inputs
|
|
169
|
+
* stopped). A failed upload never closes its stream, so a provider cannot commit a truncated
|
|
170
|
+
* body. Each pull takes the next accepted guest chunk: no read-ahead, no body-wide copy.
|
|
171
|
+
*/
|
|
172
|
+
class Upload {
|
|
173
|
+
readonly body: ReadableStream<Uint8Array> | null;
|
|
174
|
+
readonly trailers: Promise<readonly HttpField[] | undefined>;
|
|
175
|
+
/** Resolves when completed; rejects with the failure. */
|
|
176
|
+
readonly completed: Promise<void>;
|
|
177
|
+
#terminal: "completed" | HttpProviderError | undefined;
|
|
178
|
+
#sent = 0;
|
|
179
|
+
#controller: ReadableStreamDefaultController<Uint8Array> | undefined;
|
|
180
|
+
#complete!: (trailers: readonly HttpField[] | undefined) => void;
|
|
181
|
+
#reject!: (error: HttpProviderError) => void;
|
|
182
|
+
|
|
183
|
+
constructor(
|
|
184
|
+
readonly source: GuestBody,
|
|
185
|
+
/** A declared request `Content-Length`. */
|
|
186
|
+
readonly length: bigint | undefined,
|
|
187
|
+
) {
|
|
188
|
+
let resolveTrailers!: (trailers: readonly HttpField[] | undefined) => void;
|
|
189
|
+
let rejectTrailers!: (error: HttpProviderError) => void;
|
|
190
|
+
this.trailers = new Promise((resolve, reject) => {
|
|
191
|
+
resolveTrailers = resolve;
|
|
192
|
+
rejectTrailers = reject;
|
|
193
|
+
});
|
|
194
|
+
this.completed = new Promise((resolve, reject) => {
|
|
195
|
+
this.#complete = (trailers) => {
|
|
196
|
+
resolveTrailers(trailers);
|
|
197
|
+
resolve();
|
|
198
|
+
};
|
|
199
|
+
this.#reject = (error) => {
|
|
200
|
+
rejectTrailers(error);
|
|
201
|
+
reject(error);
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
// A provider that never reads them must not see unhandled rejections.
|
|
205
|
+
this.trailers.catch(() => {});
|
|
206
|
+
this.completed.catch(() => {});
|
|
207
|
+
const contents = source.contents;
|
|
208
|
+
if (!contents) {
|
|
209
|
+
this.body = null;
|
|
210
|
+
void this.#finish();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
// Retirement or a consumer stop ends the upload even if the provider never pulls again.
|
|
214
|
+
contents.stopped.addEventListener(
|
|
215
|
+
"abort",
|
|
216
|
+
() => this.abort(internal("the request body was discarded")),
|
|
217
|
+
{ once: true },
|
|
218
|
+
);
|
|
219
|
+
this.body = new ReadableStream<Uint8Array>(
|
|
220
|
+
{
|
|
221
|
+
start: (controller) => {
|
|
222
|
+
this.#controller = controller;
|
|
223
|
+
},
|
|
224
|
+
pull: (controller) => this.#pull(contents, controller),
|
|
225
|
+
cancel: (reason) =>
|
|
226
|
+
this.#fail(
|
|
227
|
+
reason instanceof HttpProviderError
|
|
228
|
+
? reason
|
|
229
|
+
: new HttpProviderError(internal("the HTTP provider cancelled the request body")),
|
|
230
|
+
),
|
|
231
|
+
},
|
|
232
|
+
{ highWaterMark: 0 },
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Whether the upload reached its terminal state. */
|
|
237
|
+
get ended(): boolean {
|
|
238
|
+
return this.#terminal !== undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Fail the upload unless it already ended. */
|
|
242
|
+
abort(code: ErrorCode): void {
|
|
243
|
+
this.#fail(new HttpProviderError(code));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
#fail(error: HttpProviderError): void {
|
|
247
|
+
if (this.#terminal) return;
|
|
248
|
+
this.#terminal = error;
|
|
249
|
+
this.source.fail(error.code);
|
|
250
|
+
this.#reject(error);
|
|
251
|
+
this.#controller?.error(error);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#oversized(): HttpProviderError {
|
|
255
|
+
return new HttpProviderError({ tag: "HTTP-request-body-size", val: BigInt(this.#sent) });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async #pull(
|
|
259
|
+
contents: InputStream,
|
|
260
|
+
controller: ReadableStreamDefaultController<Uint8Array>,
|
|
261
|
+
): Promise<void> {
|
|
262
|
+
const chunk = await contents.read();
|
|
263
|
+
if (this.#terminal) return;
|
|
264
|
+
if (!chunk) {
|
|
265
|
+
if (await this.#finish()) controller.close();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.#sent += chunk.byteLength;
|
|
269
|
+
if (this.length !== undefined && BigInt(this.#sent) > this.length)
|
|
270
|
+
return this.#fail(this.#oversized());
|
|
271
|
+
controller.enqueue(chunk);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** After the last chunk: check the length, then wait for the guest's trailers result. */
|
|
275
|
+
async #finish(): Promise<boolean> {
|
|
276
|
+
if (this.length !== undefined && BigInt(this.#sent) !== this.length) {
|
|
277
|
+
this.#fail(this.#oversized());
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
let result: Trailers;
|
|
281
|
+
try {
|
|
282
|
+
result = await this.source.trailers.take();
|
|
283
|
+
} catch (error) {
|
|
284
|
+
this.#fail(new HttpProviderError(errorCode(error)));
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (this.#terminal) return false;
|
|
288
|
+
if (result.tag === "err") {
|
|
289
|
+
this.#fail(new HttpProviderError(result.val));
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
const fields = result.val?.pairs();
|
|
293
|
+
result.val?.release();
|
|
294
|
+
this.#terminal = "completed";
|
|
295
|
+
this.#complete(fields);
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ---------------------------------------------------------------------------------------------
|
|
301
|
+
// received bodies
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* A response body received from a provider, revision-owned once published: reads may span
|
|
305
|
+
* callbacks. It ends in exactly one of three ways, given by the stream's explicit end reason:
|
|
306
|
+
* - `eof`: the body ended with a matching `Content-Length`; trailers are the provider's, and
|
|
307
|
+
* nothing else is aborted (a duplex upload may still finish);
|
|
308
|
+
* - `dropped` (or the response released unconsumed): trailers are `none`, as upstream's
|
|
309
|
+
* `HostBodyStreamProducer` drop reports, and the exchange is released;
|
|
310
|
+
* - `failed`: trailers are the error, and the exchange is released.
|
|
311
|
+
* Releasing aborts the provider's signal, aborts an unfinished upload and cancels the body; the
|
|
312
|
+
* cancel promise and any read still in flight are tracked as physical cleanup.
|
|
313
|
+
*/
|
|
314
|
+
class ReceivedBody implements Body {
|
|
315
|
+
#released = false;
|
|
316
|
+
#received = 0;
|
|
317
|
+
#reading: Promise<unknown> | undefined;
|
|
318
|
+
|
|
319
|
+
constructor(
|
|
320
|
+
readonly host: StandardServices,
|
|
321
|
+
readonly signal: AbortController,
|
|
322
|
+
readonly upload: Upload,
|
|
323
|
+
readonly reader: ReadableStreamDefaultReader<Uint8Array> | undefined,
|
|
324
|
+
readonly trailers: Promise<readonly HttpField[] | undefined> | undefined,
|
|
325
|
+
/** The declared response `Content-Length`, when the response has a framed body. */
|
|
326
|
+
readonly length: bigint | undefined,
|
|
327
|
+
readonly betweenBytes: number | undefined,
|
|
328
|
+
) {
|
|
329
|
+
trailers?.catch(() => {});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
consume(_host: StandardServices, res: FutureReader<Outcome>): [OutputStream, HostFuture] {
|
|
333
|
+
// The transport has no use for the consumer's handling outcome.
|
|
334
|
+
res.close();
|
|
335
|
+
let settle!: (trailers: Trailers) => void;
|
|
336
|
+
const trailers = new Promise<Trailers>((resolve) => {
|
|
337
|
+
settle = resolve;
|
|
338
|
+
});
|
|
339
|
+
const stream = new OutputStream(
|
|
340
|
+
{ next: () => this.#read(), close: (end) => this.#end(end) },
|
|
341
|
+
(failure, end) => {
|
|
342
|
+
if (end === "failed")
|
|
343
|
+
return settle(err(errorCode(failure, () => ({ tag: "HTTP-response-incomplete" }))));
|
|
344
|
+
if (end === "dropped" || !this.trailers) return settle(ok(undefined));
|
|
345
|
+
this.trailers.then(
|
|
346
|
+
(fields) => {
|
|
347
|
+
try {
|
|
348
|
+
settle(ok(fields ? HttpFields.received(fields) : undefined));
|
|
349
|
+
} catch (error) {
|
|
350
|
+
settle(err(errorCode(error)));
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
(error) => settle(err(errorCode(error))),
|
|
354
|
+
);
|
|
355
|
+
},
|
|
356
|
+
);
|
|
357
|
+
return [stream, new HostFuture(trailers)];
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** The next non-empty chunk; empty chunks are legal and are not the end of the body. */
|
|
361
|
+
async #read(): Promise<Uint8Array | undefined> {
|
|
362
|
+
for (;;) {
|
|
363
|
+
const next = this.reader ? await this.#next(this.reader) : undefined;
|
|
364
|
+
if (!next || next.done) {
|
|
365
|
+
if (this.length !== undefined && BigInt(this.#received) !== this.length)
|
|
366
|
+
throw new HttpProviderError({ tag: "HTTP-response-incomplete" });
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
const chunk: unknown = next.value;
|
|
370
|
+
if (!(chunk instanceof Uint8Array))
|
|
371
|
+
throw new HttpProviderError(internal("the HTTP provider produced a non-byte chunk"));
|
|
372
|
+
if (!chunk.byteLength) continue;
|
|
373
|
+
this.#received += chunk.byteLength;
|
|
374
|
+
if (this.length !== undefined && BigInt(this.#received) > this.length)
|
|
375
|
+
throw new HttpProviderError({
|
|
376
|
+
tag: "HTTP-response-body-size",
|
|
377
|
+
val: BigInt(this.#received),
|
|
378
|
+
});
|
|
379
|
+
return chunk;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** One provider read under the between-bytes deadline, enforced whatever the provider does. */
|
|
384
|
+
async #next(
|
|
385
|
+
reader: ReadableStreamDefaultReader<Uint8Array>,
|
|
386
|
+
): ReturnType<ReadableStreamDefaultReader<Uint8Array>["read"]> {
|
|
387
|
+
const read = reader.read();
|
|
388
|
+
this.#reading = read.catch(() => {});
|
|
389
|
+
if (this.betweenBytes === undefined) return read;
|
|
390
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
391
|
+
try {
|
|
392
|
+
return await Promise.race([
|
|
393
|
+
read,
|
|
394
|
+
new Promise<never>((_, reject) => {
|
|
395
|
+
timer = setTimeout(
|
|
396
|
+
() => reject(new HttpProviderError({ tag: "connection-read-timeout" })),
|
|
397
|
+
this.betweenBytes,
|
|
398
|
+
);
|
|
399
|
+
}),
|
|
400
|
+
]);
|
|
401
|
+
} finally {
|
|
402
|
+
clearTimeout(timer);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** The guest stream ended; only an early drop or a failure releases the exchange. */
|
|
407
|
+
#end(end: StreamEnd = "dropped"): void {
|
|
408
|
+
if (end !== "eof") this.close();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Release the exchange: the response was dropped early, failed, or was released unread. */
|
|
412
|
+
close(): void {
|
|
413
|
+
if (this.#released) return;
|
|
414
|
+
this.#released = true;
|
|
415
|
+
this.signal.abort();
|
|
416
|
+
this.upload.abort(internal("the response was released before its request body completed"));
|
|
417
|
+
const reader = this.reader;
|
|
418
|
+
const reading = this.#reading;
|
|
419
|
+
if (reader)
|
|
420
|
+
this.host.cleanup(async () => {
|
|
421
|
+
await reader.cancel();
|
|
422
|
+
await reading;
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ---------------------------------------------------------------------------------------------
|
|
428
|
+
// client.send
|
|
429
|
+
|
|
430
|
+
/** Cancel a response nobody will receive, tracking the provider's cancellation. */
|
|
431
|
+
function discard(host: StandardServices, response: unknown): void {
|
|
432
|
+
const body = (response as Partial<HttpProviderResponse> | undefined)?.body;
|
|
433
|
+
if (body instanceof ReadableStream) host.cleanup(() => body.cancel());
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Wait for the provider's response until the callback is cancelled or the first-byte deadline
|
|
438
|
+
* passes, whichever comes first. Neither depends on the provider honoring its signal.
|
|
439
|
+
*/
|
|
440
|
+
function deadline<T>(
|
|
441
|
+
pending: Promise<T>,
|
|
442
|
+
signal: AbortSignal | undefined,
|
|
443
|
+
ms: number | undefined,
|
|
444
|
+
): Promise<T> {
|
|
445
|
+
return new Promise<T>((resolve, reject) => {
|
|
446
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
447
|
+
const stop = () => {
|
|
448
|
+
clearTimeout(timer);
|
|
449
|
+
signal?.removeEventListener("abort", abort);
|
|
450
|
+
};
|
|
451
|
+
const abort = () => {
|
|
452
|
+
stop();
|
|
453
|
+
reject(cancelled());
|
|
454
|
+
};
|
|
455
|
+
if (signal?.aborted) return abort();
|
|
456
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
457
|
+
if (ms !== undefined)
|
|
458
|
+
timer = setTimeout(() => {
|
|
459
|
+
stop();
|
|
460
|
+
reject(new HttpProviderError({ tag: "connection-read-timeout" }));
|
|
461
|
+
}, ms);
|
|
462
|
+
pending.then(
|
|
463
|
+
(value) => {
|
|
464
|
+
stop();
|
|
465
|
+
resolve(value);
|
|
466
|
+
},
|
|
467
|
+
(error) => {
|
|
468
|
+
stop();
|
|
469
|
+
reject(error);
|
|
470
|
+
},
|
|
471
|
+
);
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** A response's framed body length; `undefined` where HTTP defines no body or none was declared. */
|
|
476
|
+
function framedLength(method: string, status: number, fields: HttpFields): bigint | undefined {
|
|
477
|
+
if (method === "HEAD" || status < 200 || status === 204 || status === 304) return undefined;
|
|
478
|
+
const declared = new Set(fields.get("content-length").map(digits));
|
|
479
|
+
if (declared.size > 1)
|
|
480
|
+
throw new HttpProviderError(internal("the HTTP provider returned conflicting lengths"));
|
|
481
|
+
const [length] = declared;
|
|
482
|
+
return length === undefined ? undefined : BigInt(length);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* `wasi:http/client#send`. Ownership of the request moves at this first turn, so everything
|
|
487
|
+
* the exchange needs from it is captured before the first `await`. The exact origin grant is
|
|
488
|
+
* checked before any provider call. The response is published once its fields arrived, then
|
|
489
|
+
* outlives the callback while the upload may still be streaming.
|
|
490
|
+
*/
|
|
491
|
+
async function send(
|
|
492
|
+
call: StandardCall,
|
|
493
|
+
client: HttpClient | undefined,
|
|
494
|
+
request: HttpRequest,
|
|
495
|
+
): Promise<Result<HttpResponse, ErrorCode>> {
|
|
496
|
+
const body = request.take() as GuestBody;
|
|
497
|
+
const method = request.methodName;
|
|
498
|
+
const timeouts = Object.freeze({ ...request.options });
|
|
499
|
+
const target = request.target();
|
|
500
|
+
if (target.tag === "err") {
|
|
501
|
+
body.fail(target.val);
|
|
502
|
+
return target;
|
|
503
|
+
}
|
|
504
|
+
const provider = client?.provider(target.val.url);
|
|
505
|
+
if (!provider) {
|
|
506
|
+
body.fail({ tag: "HTTP-request-denied" });
|
|
507
|
+
return err({ tag: "HTTP-request-denied" });
|
|
508
|
+
}
|
|
509
|
+
const declared = request.headers.get("content-length")[0];
|
|
510
|
+
const upload = new Upload(body, declared && BigInt(digits(declared)));
|
|
511
|
+
const signal = new AbortController();
|
|
512
|
+
const outgoing: HttpProviderRequest = Object.freeze({
|
|
513
|
+
url: target.val.url,
|
|
514
|
+
pathWithQuery: target.val.pathWithQuery,
|
|
515
|
+
method,
|
|
516
|
+
fields: request.headers.pairs(),
|
|
517
|
+
body: upload.body,
|
|
518
|
+
trailers: upload.trailers,
|
|
519
|
+
timeouts,
|
|
520
|
+
signal: signal.signal,
|
|
521
|
+
});
|
|
522
|
+
// A failed exchange ends the upload and settles the transmission future, even if the upload
|
|
523
|
+
// itself had already completed.
|
|
524
|
+
const fail = (code: ErrorCode) => {
|
|
525
|
+
signal.abort();
|
|
526
|
+
upload.abort(code);
|
|
527
|
+
body.settle(err(code));
|
|
528
|
+
return err(code);
|
|
529
|
+
};
|
|
530
|
+
const pending = call.host.physical(call.signal, () => provider.send(outgoing));
|
|
531
|
+
let response: HttpProviderResponse;
|
|
532
|
+
try {
|
|
533
|
+
response = await deadline(pending, call.signal, millis(timeouts.firstByte));
|
|
534
|
+
} catch (error) {
|
|
535
|
+
// A late response belongs to nobody; its body is cancelled under physical accounting.
|
|
536
|
+
pending.then(
|
|
537
|
+
(late) => discard(call.host, late),
|
|
538
|
+
() => {},
|
|
539
|
+
);
|
|
540
|
+
if (!call.signal?.aborted) return fail(errorCode(error));
|
|
541
|
+
fail(internal("request was cancelled"));
|
|
542
|
+
throw cancelled();
|
|
543
|
+
}
|
|
544
|
+
let fields: HttpFields;
|
|
545
|
+
let length: bigint | undefined;
|
|
546
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
547
|
+
try {
|
|
548
|
+
const status = response?.status;
|
|
549
|
+
if (!Number.isInteger(status) || status < 100 || status > 999)
|
|
550
|
+
throw new HttpProviderError(internal("the HTTP provider returned an invalid status"));
|
|
551
|
+
fields = HttpFields.received(response.fields);
|
|
552
|
+
length = framedLength(method, status, fields);
|
|
553
|
+
if (response.body !== null && !(response.body instanceof ReadableStream))
|
|
554
|
+
throw new HttpProviderError(internal("the HTTP provider returned an invalid body"));
|
|
555
|
+
reader = response.body?.getReader();
|
|
556
|
+
} catch (error) {
|
|
557
|
+
discard(call.host, response);
|
|
558
|
+
return fail(errorCode(error));
|
|
559
|
+
}
|
|
560
|
+
// Transmitted: the response was accepted and, possibly later, the whole upload handed over.
|
|
561
|
+
upload.completed.then(
|
|
562
|
+
() => body.settle(done),
|
|
563
|
+
() => {},
|
|
564
|
+
);
|
|
565
|
+
const received = new ReceivedBody(
|
|
566
|
+
call.host,
|
|
567
|
+
signal,
|
|
568
|
+
upload,
|
|
569
|
+
reader,
|
|
570
|
+
response.trailers,
|
|
571
|
+
length,
|
|
572
|
+
millis(timeouts.betweenBytes),
|
|
573
|
+
);
|
|
574
|
+
return ok(new HttpResponse(fields, received, response.status));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// ---------------------------------------------------------------------------------------------
|
|
578
|
+
// binding
|
|
579
|
+
|
|
580
|
+
/** Every generated `wasi:http` function, keyed for the standard host. */
|
|
581
|
+
export function httpMethods(client: HttpClient | undefined): Record<string, Implementation> {
|
|
582
|
+
const consume = (
|
|
583
|
+
call: StandardCall,
|
|
584
|
+
message: Message,
|
|
585
|
+
res: FutureReader<Outcome>,
|
|
586
|
+
): [OutputStream, HostFuture] => message.take().consume(call.host, res);
|
|
587
|
+
return {
|
|
588
|
+
...bindResource(TYPES_INTERFACE, "fields", HttpFields.prototype, {
|
|
589
|
+
constructor: () => HttpFields.create(),
|
|
590
|
+
fromList: (_call, entries: [string, Uint8Array][]) => HttpFields.fromList(entries),
|
|
591
|
+
}),
|
|
592
|
+
...bindResource(TYPES_INTERFACE, "request-options", HttpRequestOptions.prototype, {
|
|
593
|
+
constructor: () => new HttpRequestOptions(),
|
|
594
|
+
}),
|
|
595
|
+
...bindResource(TYPES_INTERFACE, "request", HttpRequest.prototype, {
|
|
596
|
+
new: (
|
|
597
|
+
_call,
|
|
598
|
+
headers: HttpFields,
|
|
599
|
+
contents: InputStream | undefined,
|
|
600
|
+
trailers: FutureReader<Trailers>,
|
|
601
|
+
options: HttpRequestOptions | undefined,
|
|
602
|
+
) => {
|
|
603
|
+
const body = new GuestBody(contents, trailers);
|
|
604
|
+
return [new HttpRequest(headers.frozen(), options?.timeouts, body), body.result];
|
|
605
|
+
},
|
|
606
|
+
consumeBody: consume,
|
|
607
|
+
}),
|
|
608
|
+
...bindResource(TYPES_INTERFACE, "response", HttpResponse.prototype, {
|
|
609
|
+
new: (
|
|
610
|
+
_call,
|
|
611
|
+
headers: HttpFields,
|
|
612
|
+
contents: InputStream | undefined,
|
|
613
|
+
trailers: FutureReader<Trailers>,
|
|
614
|
+
) => {
|
|
615
|
+
const body = new GuestBody(contents, trailers);
|
|
616
|
+
return [new HttpResponse(headers.frozen(), body), body.result];
|
|
617
|
+
},
|
|
618
|
+
consumeBody: consume,
|
|
619
|
+
}),
|
|
620
|
+
[`${CLIENT_INTERFACE}#send`]: (call, [request]: [HttpRequest]) => send(call, client, request),
|
|
621
|
+
};
|
|
622
|
+
}
|