@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,624 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous value semantics of outgoing standard `wasi:http@0.3.0` in the browser: the
|
|
3
|
+
* `fields`, `request-options`, `request` and `response` resources and request-target
|
|
4
|
+
* validation. Nothing here performs I/O or uses host services; bodies and transport lifetime
|
|
5
|
+
* live in the source or compiled transport adapter.
|
|
6
|
+
*
|
|
7
|
+
* Semantics follow pinned upstream `wasmtime-wasi-http` 51.0.0-rc.1 (`p3/host/types.rs`,
|
|
8
|
+
* `field_map.rs`, `p3/request.rs`): raw byte field values, explicit duplicates, the
|
|
9
|
+
* forbidden-field filter and the 128 KiB field budget, immutable headers/options views, and the
|
|
10
|
+
* same request target defaults and errors. Target hosts follow the native host's
|
|
11
|
+
* `Target::parse` rules.
|
|
12
|
+
*/
|
|
13
|
+
import type {
|
|
14
|
+
ErrorCode,
|
|
15
|
+
HeaderError,
|
|
16
|
+
Method,
|
|
17
|
+
RequestOptionsError,
|
|
18
|
+
Result,
|
|
19
|
+
Scheme,
|
|
20
|
+
} from "wasi:http/types@0.3.0";
|
|
21
|
+
import { type HttpField, HttpProviderError, type HttpTimeouts } from "./http-provider.ts";
|
|
22
|
+
import type {
|
|
23
|
+
HostFuture,
|
|
24
|
+
OutputStream,
|
|
25
|
+
StandardResource,
|
|
26
|
+
StandardServices,
|
|
27
|
+
FutureReader,
|
|
28
|
+
} from "./standard-streams.ts";
|
|
29
|
+
|
|
30
|
+
/** Upstream `DEFAULT_FIELD_SIZE_LIMIT`. */
|
|
31
|
+
const FIELD_BYTES = 128 << 10;
|
|
32
|
+
/** Upstream accounts each name and value with its in-memory header size. */
|
|
33
|
+
const NAME_COST = 32;
|
|
34
|
+
const VALUE_COST = 40;
|
|
35
|
+
/** Upstream `DEFAULT_FORBIDDEN_HEADERS`. */
|
|
36
|
+
const FORBIDDEN = new Set([
|
|
37
|
+
"connection",
|
|
38
|
+
"keep-alive",
|
|
39
|
+
"proxy-authenticate",
|
|
40
|
+
"proxy-authorization",
|
|
41
|
+
"proxy-connection",
|
|
42
|
+
"transfer-encoding",
|
|
43
|
+
"upgrade",
|
|
44
|
+
"host",
|
|
45
|
+
"http2-settings",
|
|
46
|
+
]);
|
|
47
|
+
const TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
48
|
+
const SCHEME = /^[A-Za-z][A-Za-z0-9+\-.]*$/;
|
|
49
|
+
const STANDARD_METHODS = [
|
|
50
|
+
"get",
|
|
51
|
+
"head",
|
|
52
|
+
"post",
|
|
53
|
+
"put",
|
|
54
|
+
"delete",
|
|
55
|
+
"connect",
|
|
56
|
+
"options",
|
|
57
|
+
"trace",
|
|
58
|
+
"patch",
|
|
59
|
+
] as const;
|
|
60
|
+
const U64_MAX = (1n << 64n) - 1n;
|
|
61
|
+
|
|
62
|
+
export type Outcome = Result<void, ErrorCode>;
|
|
63
|
+
export type Trailers = Result<HttpFields | undefined, ErrorCode>;
|
|
64
|
+
|
|
65
|
+
export const ok = <T>(val: T): { tag: "ok"; val: T } => ({ tag: "ok", val });
|
|
66
|
+
export const err = <E>(val: E): { tag: "err"; val: E } => ({ tag: "err", val });
|
|
67
|
+
export const internal = (message: string): ErrorCode => ({ tag: "internal-error", val: message });
|
|
68
|
+
export const done: Outcome = ok(undefined);
|
|
69
|
+
|
|
70
|
+
/** ASCII digits of a validated `Content-Length` value. */
|
|
71
|
+
export const digits = (bytes: Uint8Array): string => String.fromCharCode(...bytes);
|
|
72
|
+
|
|
73
|
+
/** Reference counting for resources a handle, a call or a value may hold independently. */
|
|
74
|
+
export abstract class HttpResource implements StandardResource {
|
|
75
|
+
borrows = 0;
|
|
76
|
+
#users = 1;
|
|
77
|
+
|
|
78
|
+
retain(): void {
|
|
79
|
+
this.#users++;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
release(): void {
|
|
83
|
+
if (--this.#users === 0) this.close();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The last user went away. */
|
|
87
|
+
protected close(): void {}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ---------------------------------------------------------------------------------------------
|
|
91
|
+
// fields
|
|
92
|
+
|
|
93
|
+
type FieldFailure = HeaderError["tag"];
|
|
94
|
+
|
|
95
|
+
/** A field name in the canonical lowercase form upstream's `HeaderName` stores. */
|
|
96
|
+
function fieldName(name: string): string | undefined {
|
|
97
|
+
return name.length < 0x10000 && TOKEN.test(name) ? name.toLowerCase() : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Upstream `HeaderValue` bytes: visible ASCII, obs-text and tab, never controls or DEL. */
|
|
101
|
+
function fieldValue(name: string, value: Uint8Array): Uint8Array | undefined {
|
|
102
|
+
for (const byte of value) if ((byte < 0x20 && byte !== 0x09) || byte === 0x7f) return undefined;
|
|
103
|
+
if (name !== "content-length") return value;
|
|
104
|
+
// `Content-Length` is exactly `1*DIGIT` within u64, stored normalized.
|
|
105
|
+
if (!value.length || value.some((byte) => byte < 0x30 || byte > 0x39)) return undefined;
|
|
106
|
+
const length = BigInt(digits(value));
|
|
107
|
+
if (length > U64_MAX) return undefined;
|
|
108
|
+
return Uint8Array.from(length.toString(), (digit) => digit.charCodeAt(0));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** `wasi:http/types#fields`: names in canonical lowercase, values as raw bytes in order. */
|
|
112
|
+
export class HttpFields extends HttpResource {
|
|
113
|
+
readonly #entries: Map<string, readonly Uint8Array[]>;
|
|
114
|
+
#size: number;
|
|
115
|
+
readonly #mutable: boolean;
|
|
116
|
+
|
|
117
|
+
/** Standard `fields` constructor creates empty mutable fields. */
|
|
118
|
+
constructor(entries: Map<string, readonly Uint8Array[]> = new Map(), size = 0, mutable = true) {
|
|
119
|
+
super();
|
|
120
|
+
this.#entries = entries;
|
|
121
|
+
this.#size = size;
|
|
122
|
+
this.#mutable = mutable;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `constructor`: empty and mutable. */
|
|
126
|
+
static create(): HttpFields {
|
|
127
|
+
return new HttpFields(new Map(), 0, true);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** `[static] from-list`: mutable; the first refused entry refuses the whole list. */
|
|
131
|
+
static fromList(entries: [string, Uint8Array][]): Result<HttpFields, HeaderError> {
|
|
132
|
+
const fields = HttpFields.create();
|
|
133
|
+
for (const [name, value] of entries) {
|
|
134
|
+
const appended = fields.append(name, value);
|
|
135
|
+
if (appended.tag === "err") return appended;
|
|
136
|
+
}
|
|
137
|
+
return ok(fields);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Immutable fields received from a provider. Forbidden fields are stripped before they can
|
|
142
|
+
* reach the guest, as upstream does; a malformed name or value is the provider's error.
|
|
143
|
+
*/
|
|
144
|
+
static received(pairs: readonly HttpField[]): HttpFields {
|
|
145
|
+
const entries = new Map<string, Uint8Array[]>();
|
|
146
|
+
let size = 0;
|
|
147
|
+
for (const pair of pairs) {
|
|
148
|
+
const [name, value] = Array.isArray(pair) ? pair : [];
|
|
149
|
+
const key = typeof name === "string" ? fieldName(name) : undefined;
|
|
150
|
+
const checked =
|
|
151
|
+
key !== undefined && value instanceof Uint8Array ? fieldValue(key, value) : undefined;
|
|
152
|
+
if (key === undefined || !checked)
|
|
153
|
+
throw new HttpProviderError(internal(`the HTTP provider returned an invalid field`));
|
|
154
|
+
if (FORBIDDEN.has(key)) continue;
|
|
155
|
+
const values = entries.get(key);
|
|
156
|
+
if (values) values.push(checked.slice());
|
|
157
|
+
else {
|
|
158
|
+
entries.set(key, [checked.slice()]);
|
|
159
|
+
size += key.length + NAME_COST;
|
|
160
|
+
}
|
|
161
|
+
size += checked.length + VALUE_COST;
|
|
162
|
+
}
|
|
163
|
+
return new HttpFields(entries, size, false);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Whether any field is present. */
|
|
167
|
+
get empty(): boolean {
|
|
168
|
+
return this.#entries.size === 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** An immutable snapshot, as upstream marks fields moved into a request or response. */
|
|
172
|
+
frozen(): HttpFields {
|
|
173
|
+
return new HttpFields(new Map(this.#entries), this.#size, false);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Every name/value pair in serialization order. */
|
|
177
|
+
/** Every name/value pair in serialization order; values are copies the caller owns. */
|
|
178
|
+
pairs(): [string, Uint8Array][] {
|
|
179
|
+
const pairs: [string, Uint8Array][] = [];
|
|
180
|
+
for (const [name, values] of this.#entries)
|
|
181
|
+
for (const value of values) pairs.push([name, value.slice()]);
|
|
182
|
+
return pairs;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
#refuse(tag: FieldFailure): Result<never, HeaderError> {
|
|
186
|
+
return err({ tag } as HeaderError);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Common name checks of every mutation, in upstream order. */
|
|
190
|
+
#mutation(name: string): string | FieldFailure {
|
|
191
|
+
const key = fieldName(name);
|
|
192
|
+
if (key === undefined) return "invalid-syntax";
|
|
193
|
+
if (FORBIDDEN.has(key)) return "forbidden";
|
|
194
|
+
if (!this.#mutable) return "immutable";
|
|
195
|
+
return key;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
get(name: string): Uint8Array[] {
|
|
199
|
+
const key = fieldName(name);
|
|
200
|
+
return key === undefined ? [] : [...(this.#entries.get(key) ?? [])];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
has(name: string): boolean {
|
|
204
|
+
const key = fieldName(name);
|
|
205
|
+
return key !== undefined && this.#entries.has(key);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
set(name: string, values: Uint8Array[]): Result<void, HeaderError> {
|
|
209
|
+
const key = this.#mutation(name);
|
|
210
|
+
if (key === "invalid-syntax" || key === "forbidden" || key === "immutable")
|
|
211
|
+
return this.#refuse(key);
|
|
212
|
+
if (key === "content-length" && values.length > 1) return this.#refuse("invalid-syntax");
|
|
213
|
+
const parsed: Uint8Array[] = [];
|
|
214
|
+
for (const value of values) {
|
|
215
|
+
const checked = fieldValue(key, value);
|
|
216
|
+
if (!checked) return this.#refuse("invalid-syntax");
|
|
217
|
+
parsed.push(checked.slice());
|
|
218
|
+
}
|
|
219
|
+
const previous = this.#entries.get(key);
|
|
220
|
+
const removed = previous
|
|
221
|
+
? key.length + NAME_COST + previous.reduce((sum, v) => sum + v.length + VALUE_COST, 0)
|
|
222
|
+
: 0;
|
|
223
|
+
const added = parsed.length
|
|
224
|
+
? key.length + NAME_COST + parsed.reduce((sum, v) => sum + v.length + VALUE_COST, 0)
|
|
225
|
+
: 0;
|
|
226
|
+
if (parsed.length && this.#size - removed + added > FIELD_BYTES)
|
|
227
|
+
return this.#refuse("size-exceeded");
|
|
228
|
+
this.#entries.delete(key);
|
|
229
|
+
if (parsed.length) this.#entries.set(key, parsed);
|
|
230
|
+
this.#size += added - removed;
|
|
231
|
+
return done as Result<void, HeaderError>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
delete(name: string): Result<void, HeaderError> {
|
|
235
|
+
const removed = this.getAndDelete(name);
|
|
236
|
+
return removed.tag === "err" ? removed : (done as Result<void, HeaderError>);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
getAndDelete(name: string): Result<Uint8Array[], HeaderError> {
|
|
240
|
+
const key = this.#mutation(name);
|
|
241
|
+
if (key === "invalid-syntax" || key === "forbidden" || key === "immutable")
|
|
242
|
+
return this.#refuse(key);
|
|
243
|
+
const values = this.#entries.get(key);
|
|
244
|
+
if (!values) return ok([]);
|
|
245
|
+
this.#entries.delete(key);
|
|
246
|
+
this.#size -=
|
|
247
|
+
key.length + NAME_COST + values.reduce((sum, v) => sum + v.length + VALUE_COST, 0);
|
|
248
|
+
return ok([...values]);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
append(name: string, value: Uint8Array): Result<void, HeaderError> {
|
|
252
|
+
const key = fieldName(name);
|
|
253
|
+
if (key === undefined) return this.#refuse("invalid-syntax");
|
|
254
|
+
if (FORBIDDEN.has(key)) return this.#refuse("forbidden");
|
|
255
|
+
const checked = fieldValue(key, value);
|
|
256
|
+
if (!checked) return this.#refuse("invalid-syntax");
|
|
257
|
+
// Several `Content-Length` values would make message framing ambiguous.
|
|
258
|
+
if (key === "content-length" && this.#entries.has(key)) return this.#refuse("invalid-syntax");
|
|
259
|
+
if (!this.#mutable) return this.#refuse("immutable");
|
|
260
|
+
const previous = this.#entries.get(key);
|
|
261
|
+
const added = checked.length + VALUE_COST + (previous ? 0 : key.length + NAME_COST);
|
|
262
|
+
if (this.#size + added > FIELD_BYTES) return this.#refuse("size-exceeded");
|
|
263
|
+
this.#entries.set(key, [...(previous ?? []), checked.slice()]);
|
|
264
|
+
this.#size += added;
|
|
265
|
+
return done as Result<void, HeaderError>;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
copyAll(): [string, Uint8Array][] {
|
|
269
|
+
return this.pairs();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A deep, mutable copy. */
|
|
273
|
+
clone(): HttpFields {
|
|
274
|
+
return new HttpFields(new Map(this.#entries), this.#size, true);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ---------------------------------------------------------------------------------------------
|
|
279
|
+
// request-options
|
|
280
|
+
|
|
281
|
+
type OptionsResult = Result<void, RequestOptionsError>;
|
|
282
|
+
|
|
283
|
+
/** `wasi:http/types#request-options`; views obtained from a request are immutable. */
|
|
284
|
+
export class HttpRequestOptions extends HttpResource {
|
|
285
|
+
#timeouts: HttpTimeouts;
|
|
286
|
+
|
|
287
|
+
constructor(
|
|
288
|
+
timeouts: HttpTimeouts = {},
|
|
289
|
+
readonly mutable = true,
|
|
290
|
+
) {
|
|
291
|
+
super();
|
|
292
|
+
this.#timeouts = timeouts;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The current timeouts, as a request keeps them once the options moved into it. */
|
|
296
|
+
get timeouts(): HttpTimeouts {
|
|
297
|
+
return this.#timeouts;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
#set(change: HttpTimeouts): OptionsResult {
|
|
301
|
+
if (!this.mutable) return err({ tag: "immutable" } as RequestOptionsError);
|
|
302
|
+
this.#timeouts = { ...this.#timeouts, ...change };
|
|
303
|
+
return done as OptionsResult;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
getConnectTimeout(): bigint | undefined {
|
|
307
|
+
return this.#timeouts.connect;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Kept for the provider, which honors or refuses it at send. */
|
|
311
|
+
setConnectTimeout(duration: bigint | undefined): OptionsResult {
|
|
312
|
+
return this.#set({ connect: duration });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
getFirstByteTimeout(): bigint | undefined {
|
|
316
|
+
return this.#timeouts.firstByte;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
setFirstByteTimeout(duration: bigint | undefined): OptionsResult {
|
|
320
|
+
return this.#set({ firstByte: duration });
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
getBetweenBytesTimeout(): bigint | undefined {
|
|
324
|
+
return this.#timeouts.betweenBytes;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
setBetweenBytesTimeout(duration: bigint | undefined): OptionsResult {
|
|
328
|
+
return this.#set({ betweenBytes: duration });
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
clone(): HttpRequestOptions {
|
|
332
|
+
return new HttpRequestOptions(this.#timeouts, true);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ---------------------------------------------------------------------------------------------
|
|
337
|
+
// bodies (owned by each transport adapter)
|
|
338
|
+
|
|
339
|
+
/** Minimum lifetime contract shared by both transports. */
|
|
340
|
+
export interface HttpBody {
|
|
341
|
+
close(): void;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Source transport contents and trailers, consumed or dropped once. */
|
|
345
|
+
export interface Body extends HttpBody {
|
|
346
|
+
/** `consume-body`: the contents stream and trailers future; `res` reports the consumer. */
|
|
347
|
+
consume(host: StandardServices, res: FutureReader<Outcome>): [OutputStream, HostFuture];
|
|
348
|
+
/** Dropped unconsumed. */
|
|
349
|
+
close(): void;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ---------------------------------------------------------------------------------------------
|
|
353
|
+
// request and response
|
|
354
|
+
|
|
355
|
+
/** State shared by requests and responses: immutable headers and a body that moves once. */
|
|
356
|
+
export abstract class Message<B extends HttpBody = Body> extends HttpResource {
|
|
357
|
+
#body: B | undefined;
|
|
358
|
+
|
|
359
|
+
constructor(
|
|
360
|
+
readonly headers: HttpFields,
|
|
361
|
+
body: B,
|
|
362
|
+
) {
|
|
363
|
+
super();
|
|
364
|
+
this.#body = body;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Move the body out; afterwards the message holds only its metadata. */
|
|
368
|
+
take(): B {
|
|
369
|
+
const body = this.#body;
|
|
370
|
+
if (!body) throw new Error("HTTP body was already consumed");
|
|
371
|
+
this.#body = undefined;
|
|
372
|
+
return body;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** An immutable view; each call issues a new resource. */
|
|
376
|
+
getHeaders(): HttpFields {
|
|
377
|
+
return this.headers.frozen();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
protected override close(): void {
|
|
381
|
+
this.#body?.close();
|
|
382
|
+
this.#body = undefined;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Upstream `parse_authority`: an `http::uri::Authority` without user info and a valid port. */
|
|
387
|
+
function authority(text: string): boolean {
|
|
388
|
+
if (!text || text.includes("@") || !/^[A-Za-z0-9\-._~!$&'()*+,;=:%[\]]+$/.test(text))
|
|
389
|
+
return false;
|
|
390
|
+
let host = text;
|
|
391
|
+
let port: string | undefined;
|
|
392
|
+
if (text.startsWith("[")) {
|
|
393
|
+
const close = text.indexOf("]");
|
|
394
|
+
if (close < 0 || text.indexOf("[", 1) >= 0 || text.indexOf("]", close + 1) >= 0) return false;
|
|
395
|
+
const rest = text.slice(close + 1);
|
|
396
|
+
if (rest && !rest.startsWith(":")) return false;
|
|
397
|
+
host = text.slice(0, close + 1);
|
|
398
|
+
port = rest ? rest.slice(1) : undefined;
|
|
399
|
+
} else {
|
|
400
|
+
if (/[[\]]/.test(text)) return false;
|
|
401
|
+
const colon = text.indexOf(":");
|
|
402
|
+
if (colon >= 0) {
|
|
403
|
+
if (text.indexOf(":", colon + 1) >= 0) return false;
|
|
404
|
+
host = text.slice(0, colon);
|
|
405
|
+
port = text.slice(colon + 1);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (port !== undefined && !(/^[0-9]+$/.test(port) && Number(port) <= 0xffff)) return false;
|
|
409
|
+
return host.length > 0 || port !== undefined;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Upstream `parse_path_with_query`: an origin-form path and query, fragment discarded. */
|
|
413
|
+
function pathWithQuery(text: string): string | undefined {
|
|
414
|
+
if (!text || text.length > 0xfffe || !(text.startsWith("/") || text.startsWith("?")))
|
|
415
|
+
return undefined;
|
|
416
|
+
let query = false;
|
|
417
|
+
for (let at = 0; at < text.length; at++) {
|
|
418
|
+
const code = text.charCodeAt(at);
|
|
419
|
+
if (code === 0x23) return text.slice(0, at);
|
|
420
|
+
if (code === 0x3f && !query) {
|
|
421
|
+
query = true;
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
if (code >= 0x80) continue;
|
|
425
|
+
const valid = query
|
|
426
|
+
? code === 0x21 ||
|
|
427
|
+
(code >= 0x24 && code <= 0x3b) ||
|
|
428
|
+
code === 0x3d ||
|
|
429
|
+
(code >= 0x3f && code <= 0x7e)
|
|
430
|
+
: code === 0x21 ||
|
|
431
|
+
(code >= 0x24 && code <= 0x3b) ||
|
|
432
|
+
code === 0x3d ||
|
|
433
|
+
(code >= 0x40 && code <= 0x5f) ||
|
|
434
|
+
(code >= 0x61 && code <= 0x7a) ||
|
|
435
|
+
code === 0x7c ||
|
|
436
|
+
code === 0x7e ||
|
|
437
|
+
code === 0x22 ||
|
|
438
|
+
code === 0x7b ||
|
|
439
|
+
code === 0x7d;
|
|
440
|
+
if (!valid) return undefined;
|
|
441
|
+
}
|
|
442
|
+
return text;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** A checked request target. */
|
|
446
|
+
export interface Target {
|
|
447
|
+
/** Canonical origin plus the WHATWG-normalized path, for grants and URL-based transports. */
|
|
448
|
+
readonly url: URL;
|
|
449
|
+
/** The validated raw origin-form path and query, as the guest set it. */
|
|
450
|
+
readonly pathWithQuery: string;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** A dotted quad of decimal octets without leading zeros, as Rust's `Ipv4Addr` parses it. */
|
|
454
|
+
const IPV4 = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
|
|
455
|
+
const LABEL = /^(?!-)[a-z0-9-]{1,63}(?<!-)$/;
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* The native host's `Target::parse` host rules, applied to an authority that already passed
|
|
459
|
+
* `authority`: an IP literal (strict dotted quad, or any valid bracketed IPv6 spelling in its
|
|
460
|
+
* canonical form) or a lowercase LDH DNS name whose last label is not numeric (so URL parsers
|
|
461
|
+
* cannot read it as an IPv4 shorthand), plus a u16 port (leading zeros allowed, 0 refused).
|
|
462
|
+
*/
|
|
463
|
+
export function canonicalHost(
|
|
464
|
+
authority: string,
|
|
465
|
+
secure: boolean,
|
|
466
|
+
): { name: string; port: number } | undefined {
|
|
467
|
+
const close = authority.startsWith("[") ? authority.indexOf("]") + 1 : 0;
|
|
468
|
+
const colon = authority.indexOf(":", close);
|
|
469
|
+
const host = colon < 0 ? authority : authority.slice(0, colon);
|
|
470
|
+
const port = colon < 0 ? (secure ? 443 : 80) : Number(authority.slice(colon + 1));
|
|
471
|
+
if (!Number.isInteger(port) || port < 1 || port > 0xffff) return undefined;
|
|
472
|
+
if (close) {
|
|
473
|
+
try {
|
|
474
|
+
// URL validates the literal and prints its canonical form.
|
|
475
|
+
return { name: new URL(`http://${host}/`).hostname, port };
|
|
476
|
+
} catch {
|
|
477
|
+
return undefined;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const name = host.toLowerCase();
|
|
481
|
+
if (IPV4.test(name)) return { name, port };
|
|
482
|
+
const labels = name.replace(/\.$/, "").split(".");
|
|
483
|
+
const last = labels.at(-1)!;
|
|
484
|
+
if (
|
|
485
|
+
name.length > 253 ||
|
|
486
|
+
!labels.every((label) => LABEL.test(label)) ||
|
|
487
|
+
/^\d+$/.test(last) ||
|
|
488
|
+
last.startsWith("0x")
|
|
489
|
+
)
|
|
490
|
+
return undefined;
|
|
491
|
+
return { name, port };
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** `wasi:http/types#request` built by the guest. */
|
|
495
|
+
export class HttpRequest<B extends HttpBody = Body> extends Message<B> {
|
|
496
|
+
#method: Method = { tag: "get" };
|
|
497
|
+
#scheme: Scheme | undefined;
|
|
498
|
+
#authority: string | undefined;
|
|
499
|
+
#path: string | undefined;
|
|
500
|
+
|
|
501
|
+
constructor(
|
|
502
|
+
headers: HttpFields,
|
|
503
|
+
readonly options: HttpTimeouts | undefined,
|
|
504
|
+
body: B,
|
|
505
|
+
) {
|
|
506
|
+
super(headers, body);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
getMethod(): Method {
|
|
510
|
+
return this.#method;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** Standard names normalize to their cases, as upstream's `http::Method` does. */
|
|
514
|
+
setMethod(method: Method): Result<void, void> {
|
|
515
|
+
if (method.tag === "other") {
|
|
516
|
+
if (!TOKEN.test(method.val)) return err(undefined);
|
|
517
|
+
const standard = STANDARD_METHODS.find((name) => name.toUpperCase() === method.val);
|
|
518
|
+
this.#method = standard ? ({ tag: standard } as Method) : { tag: "other", val: method.val };
|
|
519
|
+
} else if (STANDARD_METHODS.includes(method.tag)) this.#method = { tag: method.tag } as Method;
|
|
520
|
+
else return err(undefined);
|
|
521
|
+
return ok(undefined);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
getPathWithQuery(): string | undefined {
|
|
525
|
+
return this.#path;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
setPathWithQuery(path: string | undefined): Result<void, void> {
|
|
529
|
+
if (path === undefined) {
|
|
530
|
+
this.#path = undefined;
|
|
531
|
+
return ok(undefined);
|
|
532
|
+
}
|
|
533
|
+
const parsed = pathWithQuery(path);
|
|
534
|
+
if (parsed === undefined) return err(undefined);
|
|
535
|
+
this.#path = parsed;
|
|
536
|
+
return ok(undefined);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
getScheme(): Scheme | undefined {
|
|
540
|
+
return this.#scheme;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
setScheme(scheme: Scheme | undefined): Result<void, void> {
|
|
544
|
+
if (scheme === undefined || scheme.tag === "HTTP" || scheme.tag === "HTTPS") {
|
|
545
|
+
this.#scheme = scheme && ({ tag: scheme.tag } as Scheme);
|
|
546
|
+
return ok(undefined);
|
|
547
|
+
}
|
|
548
|
+
if (scheme.tag !== "other" || scheme.val.length > 64 || !SCHEME.test(scheme.val))
|
|
549
|
+
return err(undefined);
|
|
550
|
+
const lower = scheme.val.toLowerCase();
|
|
551
|
+
this.#scheme =
|
|
552
|
+
lower === "http"
|
|
553
|
+
? { tag: "HTTP" }
|
|
554
|
+
: lower === "https"
|
|
555
|
+
? { tag: "HTTPS" }
|
|
556
|
+
: { tag: "other", val: scheme.val };
|
|
557
|
+
return ok(undefined);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
getAuthority(): string | undefined {
|
|
561
|
+
return this.#authority;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
setAuthority(value: string | undefined): Result<void, void> {
|
|
565
|
+
if (value !== undefined && !authority(value)) return err(undefined);
|
|
566
|
+
this.#authority = value;
|
|
567
|
+
return ok(undefined);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
getOptions(): HttpRequestOptions | undefined {
|
|
571
|
+
return this.options && new HttpRequestOptions(this.options, false);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** The method as sent on the wire: a standard name in uppercase, or the extension token. */
|
|
575
|
+
get methodName(): string {
|
|
576
|
+
return this.#method.tag === "other" ? this.#method.val : this.#method.tag.toUpperCase();
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* The checked target, with upstream's defaults and errors: no scheme means HTTPS, only
|
|
581
|
+
* HTTP(S) is supported, and authority and path are required. The host must pass the native
|
|
582
|
+
* host's `Target::parse` rules (see `canonicalHost`), otherwise the request is denied. `url`
|
|
583
|
+
* names the canonical origin the grant is checked against; `pathWithQuery` stays exactly as
|
|
584
|
+
* the guest validated it, independent of URL normalization.
|
|
585
|
+
*/
|
|
586
|
+
target(): Result<Target, ErrorCode> {
|
|
587
|
+
const scheme = this.#scheme ?? { tag: "HTTPS" };
|
|
588
|
+
if (scheme.tag === "other") return err({ tag: "HTTP-protocol-error" });
|
|
589
|
+
if (this.#authority === undefined || this.#path === undefined)
|
|
590
|
+
return err({ tag: "HTTP-request-URI-invalid" });
|
|
591
|
+
const secure = scheme.tag === "HTTPS";
|
|
592
|
+
const host = canonicalHost(this.#authority, secure);
|
|
593
|
+
if (!host) return err({ tag: "HTTP-request-denied" });
|
|
594
|
+
let url: URL;
|
|
595
|
+
try {
|
|
596
|
+
url = new URL(`${secure ? "https" : "http"}://${host.name}:${host.port}${this.#path}`);
|
|
597
|
+
} catch {
|
|
598
|
+
return err({ tag: "HTTP-request-URI-invalid" });
|
|
599
|
+
}
|
|
600
|
+
// The canonical host is a fixed point of URL parsing; anything else is refused.
|
|
601
|
+
if (url.hostname !== host.name) return err({ tag: "HTTP-request-denied" });
|
|
602
|
+
return ok({ url, pathWithQuery: this.#path });
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/** `wasi:http/types#response`: received from a provider, or built by the guest. */
|
|
607
|
+
export class HttpResponse<B extends HttpBody = Body> extends Message<B> {
|
|
608
|
+
#status: number;
|
|
609
|
+
|
|
610
|
+
constructor(headers: HttpFields, body: B, status = 200) {
|
|
611
|
+
super(headers, body);
|
|
612
|
+
this.#status = status;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
getStatusCode(): number {
|
|
616
|
+
return this.#status;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
setStatusCode(status: number): Result<void, void> {
|
|
620
|
+
if (status < 100 || status > 599) return err(undefined);
|
|
621
|
+
this.#status = status;
|
|
622
|
+
return ok(undefined);
|
|
623
|
+
}
|
|
624
|
+
}
|