@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,643 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HttpProviderError2
|
|
3
|
+
} from "./index-h8t0n42q.js";
|
|
4
|
+
|
|
5
|
+
// src/interpreter/standard-http-values.ts
|
|
6
|
+
var FIELD_BYTES = 128 << 10;
|
|
7
|
+
var NAME_COST = 32;
|
|
8
|
+
var VALUE_COST = 40;
|
|
9
|
+
var FORBIDDEN = new Set([
|
|
10
|
+
"connection",
|
|
11
|
+
"keep-alive",
|
|
12
|
+
"proxy-authenticate",
|
|
13
|
+
"proxy-authorization",
|
|
14
|
+
"proxy-connection",
|
|
15
|
+
"transfer-encoding",
|
|
16
|
+
"upgrade",
|
|
17
|
+
"host",
|
|
18
|
+
"http2-settings"
|
|
19
|
+
]);
|
|
20
|
+
var TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
21
|
+
var SCHEME = /^[A-Za-z][A-Za-z0-9+\-.]*$/;
|
|
22
|
+
var STANDARD_METHODS = [
|
|
23
|
+
"get",
|
|
24
|
+
"head",
|
|
25
|
+
"post",
|
|
26
|
+
"put",
|
|
27
|
+
"delete",
|
|
28
|
+
"connect",
|
|
29
|
+
"options",
|
|
30
|
+
"trace",
|
|
31
|
+
"patch"
|
|
32
|
+
];
|
|
33
|
+
var U64_MAX = (1n << 64n) - 1n;
|
|
34
|
+
var ok = (val) => ({ tag: "ok", val });
|
|
35
|
+
var err = (val) => ({ tag: "err", val });
|
|
36
|
+
var internal = (message) => ({ tag: "internal-error", val: message });
|
|
37
|
+
var done = ok(undefined);
|
|
38
|
+
var digits = (bytes) => String.fromCharCode(...bytes);
|
|
39
|
+
|
|
40
|
+
class HttpResource {
|
|
41
|
+
borrows = 0;
|
|
42
|
+
#users = 1;
|
|
43
|
+
retain() {
|
|
44
|
+
this.#users++;
|
|
45
|
+
}
|
|
46
|
+
release() {
|
|
47
|
+
if (--this.#users === 0)
|
|
48
|
+
this.close();
|
|
49
|
+
}
|
|
50
|
+
close() {}
|
|
51
|
+
}
|
|
52
|
+
function fieldName(name) {
|
|
53
|
+
return name.length < 65536 && TOKEN.test(name) ? name.toLowerCase() : undefined;
|
|
54
|
+
}
|
|
55
|
+
function fieldValue(name, value) {
|
|
56
|
+
for (const byte of value)
|
|
57
|
+
if (byte < 32 && byte !== 9 || byte === 127)
|
|
58
|
+
return;
|
|
59
|
+
if (name !== "content-length")
|
|
60
|
+
return value;
|
|
61
|
+
if (!value.length || value.some((byte) => byte < 48 || byte > 57))
|
|
62
|
+
return;
|
|
63
|
+
const length = BigInt(digits(value));
|
|
64
|
+
if (length > U64_MAX)
|
|
65
|
+
return;
|
|
66
|
+
return Uint8Array.from(length.toString(), (digit) => digit.charCodeAt(0));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class HttpFields extends HttpResource {
|
|
70
|
+
#entries;
|
|
71
|
+
#size;
|
|
72
|
+
#mutable;
|
|
73
|
+
constructor(entries = new Map, size = 0, mutable = true) {
|
|
74
|
+
super();
|
|
75
|
+
this.#entries = entries;
|
|
76
|
+
this.#size = size;
|
|
77
|
+
this.#mutable = mutable;
|
|
78
|
+
}
|
|
79
|
+
static create() {
|
|
80
|
+
return new HttpFields(new Map, 0, true);
|
|
81
|
+
}
|
|
82
|
+
static fromList(entries) {
|
|
83
|
+
const fields = HttpFields.create();
|
|
84
|
+
for (const [name, value] of entries) {
|
|
85
|
+
const appended = fields.append(name, value);
|
|
86
|
+
if (appended.tag === "err")
|
|
87
|
+
return appended;
|
|
88
|
+
}
|
|
89
|
+
return ok(fields);
|
|
90
|
+
}
|
|
91
|
+
static received(pairs) {
|
|
92
|
+
const entries = new Map;
|
|
93
|
+
let size = 0;
|
|
94
|
+
for (const pair of pairs) {
|
|
95
|
+
const [name, value] = Array.isArray(pair) ? pair : [];
|
|
96
|
+
const key = typeof name === "string" ? fieldName(name) : undefined;
|
|
97
|
+
const checked = key !== undefined && value instanceof Uint8Array ? fieldValue(key, value) : undefined;
|
|
98
|
+
if (key === undefined || !checked)
|
|
99
|
+
throw new HttpProviderError2(internal(`the HTTP provider returned an invalid field`));
|
|
100
|
+
if (FORBIDDEN.has(key))
|
|
101
|
+
continue;
|
|
102
|
+
const values = entries.get(key);
|
|
103
|
+
if (values)
|
|
104
|
+
values.push(checked.slice());
|
|
105
|
+
else {
|
|
106
|
+
entries.set(key, [checked.slice()]);
|
|
107
|
+
size += key.length + NAME_COST;
|
|
108
|
+
}
|
|
109
|
+
size += checked.length + VALUE_COST;
|
|
110
|
+
}
|
|
111
|
+
return new HttpFields(entries, size, false);
|
|
112
|
+
}
|
|
113
|
+
get empty() {
|
|
114
|
+
return this.#entries.size === 0;
|
|
115
|
+
}
|
|
116
|
+
frozen() {
|
|
117
|
+
return new HttpFields(new Map(this.#entries), this.#size, false);
|
|
118
|
+
}
|
|
119
|
+
pairs() {
|
|
120
|
+
const pairs = [];
|
|
121
|
+
for (const [name, values] of this.#entries)
|
|
122
|
+
for (const value of values)
|
|
123
|
+
pairs.push([name, value.slice()]);
|
|
124
|
+
return pairs;
|
|
125
|
+
}
|
|
126
|
+
#refuse(tag) {
|
|
127
|
+
return err({ tag });
|
|
128
|
+
}
|
|
129
|
+
#mutation(name) {
|
|
130
|
+
const key = fieldName(name);
|
|
131
|
+
if (key === undefined)
|
|
132
|
+
return "invalid-syntax";
|
|
133
|
+
if (FORBIDDEN.has(key))
|
|
134
|
+
return "forbidden";
|
|
135
|
+
if (!this.#mutable)
|
|
136
|
+
return "immutable";
|
|
137
|
+
return key;
|
|
138
|
+
}
|
|
139
|
+
get(name) {
|
|
140
|
+
const key = fieldName(name);
|
|
141
|
+
return key === undefined ? [] : [...this.#entries.get(key) ?? []];
|
|
142
|
+
}
|
|
143
|
+
has(name) {
|
|
144
|
+
const key = fieldName(name);
|
|
145
|
+
return key !== undefined && this.#entries.has(key);
|
|
146
|
+
}
|
|
147
|
+
set(name, values) {
|
|
148
|
+
const key = this.#mutation(name);
|
|
149
|
+
if (key === "invalid-syntax" || key === "forbidden" || key === "immutable")
|
|
150
|
+
return this.#refuse(key);
|
|
151
|
+
if (key === "content-length" && values.length > 1)
|
|
152
|
+
return this.#refuse("invalid-syntax");
|
|
153
|
+
const parsed = [];
|
|
154
|
+
for (const value of values) {
|
|
155
|
+
const checked = fieldValue(key, value);
|
|
156
|
+
if (!checked)
|
|
157
|
+
return this.#refuse("invalid-syntax");
|
|
158
|
+
parsed.push(checked.slice());
|
|
159
|
+
}
|
|
160
|
+
const previous = this.#entries.get(key);
|
|
161
|
+
const removed = previous ? key.length + NAME_COST + previous.reduce((sum, v) => sum + v.length + VALUE_COST, 0) : 0;
|
|
162
|
+
const added = parsed.length ? key.length + NAME_COST + parsed.reduce((sum, v) => sum + v.length + VALUE_COST, 0) : 0;
|
|
163
|
+
if (parsed.length && this.#size - removed + added > FIELD_BYTES)
|
|
164
|
+
return this.#refuse("size-exceeded");
|
|
165
|
+
this.#entries.delete(key);
|
|
166
|
+
if (parsed.length)
|
|
167
|
+
this.#entries.set(key, parsed);
|
|
168
|
+
this.#size += added - removed;
|
|
169
|
+
return done;
|
|
170
|
+
}
|
|
171
|
+
delete(name) {
|
|
172
|
+
const removed = this.getAndDelete(name);
|
|
173
|
+
return removed.tag === "err" ? removed : done;
|
|
174
|
+
}
|
|
175
|
+
getAndDelete(name) {
|
|
176
|
+
const key = this.#mutation(name);
|
|
177
|
+
if (key === "invalid-syntax" || key === "forbidden" || key === "immutable")
|
|
178
|
+
return this.#refuse(key);
|
|
179
|
+
const values = this.#entries.get(key);
|
|
180
|
+
if (!values)
|
|
181
|
+
return ok([]);
|
|
182
|
+
this.#entries.delete(key);
|
|
183
|
+
this.#size -= key.length + NAME_COST + values.reduce((sum, v) => sum + v.length + VALUE_COST, 0);
|
|
184
|
+
return ok([...values]);
|
|
185
|
+
}
|
|
186
|
+
append(name, value) {
|
|
187
|
+
const key = fieldName(name);
|
|
188
|
+
if (key === undefined)
|
|
189
|
+
return this.#refuse("invalid-syntax");
|
|
190
|
+
if (FORBIDDEN.has(key))
|
|
191
|
+
return this.#refuse("forbidden");
|
|
192
|
+
const checked = fieldValue(key, value);
|
|
193
|
+
if (!checked)
|
|
194
|
+
return this.#refuse("invalid-syntax");
|
|
195
|
+
if (key === "content-length" && this.#entries.has(key))
|
|
196
|
+
return this.#refuse("invalid-syntax");
|
|
197
|
+
if (!this.#mutable)
|
|
198
|
+
return this.#refuse("immutable");
|
|
199
|
+
const previous = this.#entries.get(key);
|
|
200
|
+
const added = checked.length + VALUE_COST + (previous ? 0 : key.length + NAME_COST);
|
|
201
|
+
if (this.#size + added > FIELD_BYTES)
|
|
202
|
+
return this.#refuse("size-exceeded");
|
|
203
|
+
this.#entries.set(key, [...previous ?? [], checked.slice()]);
|
|
204
|
+
this.#size += added;
|
|
205
|
+
return done;
|
|
206
|
+
}
|
|
207
|
+
copyAll() {
|
|
208
|
+
return this.pairs();
|
|
209
|
+
}
|
|
210
|
+
clone() {
|
|
211
|
+
return new HttpFields(new Map(this.#entries), this.#size, true);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
class HttpRequestOptions extends HttpResource {
|
|
216
|
+
mutable;
|
|
217
|
+
#timeouts;
|
|
218
|
+
constructor(timeouts = {}, mutable = true) {
|
|
219
|
+
super();
|
|
220
|
+
this.mutable = mutable;
|
|
221
|
+
this.#timeouts = timeouts;
|
|
222
|
+
}
|
|
223
|
+
get timeouts() {
|
|
224
|
+
return this.#timeouts;
|
|
225
|
+
}
|
|
226
|
+
#set(change) {
|
|
227
|
+
if (!this.mutable)
|
|
228
|
+
return err({ tag: "immutable" });
|
|
229
|
+
this.#timeouts = { ...this.#timeouts, ...change };
|
|
230
|
+
return done;
|
|
231
|
+
}
|
|
232
|
+
getConnectTimeout() {
|
|
233
|
+
return this.#timeouts.connect;
|
|
234
|
+
}
|
|
235
|
+
setConnectTimeout(duration) {
|
|
236
|
+
return this.#set({ connect: duration });
|
|
237
|
+
}
|
|
238
|
+
getFirstByteTimeout() {
|
|
239
|
+
return this.#timeouts.firstByte;
|
|
240
|
+
}
|
|
241
|
+
setFirstByteTimeout(duration) {
|
|
242
|
+
return this.#set({ firstByte: duration });
|
|
243
|
+
}
|
|
244
|
+
getBetweenBytesTimeout() {
|
|
245
|
+
return this.#timeouts.betweenBytes;
|
|
246
|
+
}
|
|
247
|
+
setBetweenBytesTimeout(duration) {
|
|
248
|
+
return this.#set({ betweenBytes: duration });
|
|
249
|
+
}
|
|
250
|
+
clone() {
|
|
251
|
+
return new HttpRequestOptions(this.#timeouts, true);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
class Message extends HttpResource {
|
|
256
|
+
headers;
|
|
257
|
+
#body;
|
|
258
|
+
constructor(headers, body) {
|
|
259
|
+
super();
|
|
260
|
+
this.headers = headers;
|
|
261
|
+
this.#body = body;
|
|
262
|
+
}
|
|
263
|
+
take() {
|
|
264
|
+
const body = this.#body;
|
|
265
|
+
if (!body)
|
|
266
|
+
throw new Error("HTTP body was already consumed");
|
|
267
|
+
this.#body = undefined;
|
|
268
|
+
return body;
|
|
269
|
+
}
|
|
270
|
+
getHeaders() {
|
|
271
|
+
return this.headers.frozen();
|
|
272
|
+
}
|
|
273
|
+
close() {
|
|
274
|
+
this.#body?.close();
|
|
275
|
+
this.#body = undefined;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function authority(text) {
|
|
279
|
+
if (!text || text.includes("@") || !/^[A-Za-z0-9\-._~!$&'()*+,;=:%[\]]+$/.test(text))
|
|
280
|
+
return false;
|
|
281
|
+
let host = text;
|
|
282
|
+
let port;
|
|
283
|
+
if (text.startsWith("[")) {
|
|
284
|
+
const close = text.indexOf("]");
|
|
285
|
+
if (close < 0 || text.indexOf("[", 1) >= 0 || text.indexOf("]", close + 1) >= 0)
|
|
286
|
+
return false;
|
|
287
|
+
const rest = text.slice(close + 1);
|
|
288
|
+
if (rest && !rest.startsWith(":"))
|
|
289
|
+
return false;
|
|
290
|
+
host = text.slice(0, close + 1);
|
|
291
|
+
port = rest ? rest.slice(1) : undefined;
|
|
292
|
+
} else {
|
|
293
|
+
if (/[[\]]/.test(text))
|
|
294
|
+
return false;
|
|
295
|
+
const colon = text.indexOf(":");
|
|
296
|
+
if (colon >= 0) {
|
|
297
|
+
if (text.indexOf(":", colon + 1) >= 0)
|
|
298
|
+
return false;
|
|
299
|
+
host = text.slice(0, colon);
|
|
300
|
+
port = text.slice(colon + 1);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (port !== undefined && !(/^[0-9]+$/.test(port) && Number(port) <= 65535))
|
|
304
|
+
return false;
|
|
305
|
+
return host.length > 0 || port !== undefined;
|
|
306
|
+
}
|
|
307
|
+
function pathWithQuery(text) {
|
|
308
|
+
if (!text || text.length > 65534 || !(text.startsWith("/") || text.startsWith("?")))
|
|
309
|
+
return;
|
|
310
|
+
let query = false;
|
|
311
|
+
for (let at = 0;at < text.length; at++) {
|
|
312
|
+
const code = text.charCodeAt(at);
|
|
313
|
+
if (code === 35)
|
|
314
|
+
return text.slice(0, at);
|
|
315
|
+
if (code === 63 && !query) {
|
|
316
|
+
query = true;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (code >= 128)
|
|
320
|
+
continue;
|
|
321
|
+
const valid = query ? code === 33 || code >= 36 && code <= 59 || code === 61 || code >= 63 && code <= 126 : code === 33 || code >= 36 && code <= 59 || code === 61 || code >= 64 && code <= 95 || code >= 97 && code <= 122 || code === 124 || code === 126 || code === 34 || code === 123 || code === 125;
|
|
322
|
+
if (!valid)
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
return text;
|
|
326
|
+
}
|
|
327
|
+
var 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)$/;
|
|
328
|
+
var LABEL = /^(?!-)[a-z0-9-]{1,63}(?<!-)$/;
|
|
329
|
+
function canonicalHost(authority, secure) {
|
|
330
|
+
const close = authority.startsWith("[") ? authority.indexOf("]") + 1 : 0;
|
|
331
|
+
const colon = authority.indexOf(":", close);
|
|
332
|
+
const host = colon < 0 ? authority : authority.slice(0, colon);
|
|
333
|
+
const port = colon < 0 ? secure ? 443 : 80 : Number(authority.slice(colon + 1));
|
|
334
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
335
|
+
return;
|
|
336
|
+
if (close) {
|
|
337
|
+
try {
|
|
338
|
+
return { name: new URL(`http://${host}/`).hostname, port };
|
|
339
|
+
} catch {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const name = host.toLowerCase();
|
|
344
|
+
if (IPV4.test(name))
|
|
345
|
+
return { name, port };
|
|
346
|
+
const labels = name.replace(/\.$/, "").split(".");
|
|
347
|
+
const last = labels.at(-1);
|
|
348
|
+
if (name.length > 253 || !labels.every((label) => LABEL.test(label)) || /^\d+$/.test(last) || last.startsWith("0x"))
|
|
349
|
+
return;
|
|
350
|
+
return { name, port };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
class HttpRequest extends Message {
|
|
354
|
+
options;
|
|
355
|
+
#method = { tag: "get" };
|
|
356
|
+
#scheme;
|
|
357
|
+
#authority;
|
|
358
|
+
#path;
|
|
359
|
+
constructor(headers, options, body) {
|
|
360
|
+
super(headers, body);
|
|
361
|
+
this.options = options;
|
|
362
|
+
}
|
|
363
|
+
getMethod() {
|
|
364
|
+
return this.#method;
|
|
365
|
+
}
|
|
366
|
+
setMethod(method) {
|
|
367
|
+
if (method.tag === "other") {
|
|
368
|
+
if (!TOKEN.test(method.val))
|
|
369
|
+
return err(undefined);
|
|
370
|
+
const standard = STANDARD_METHODS.find((name) => name.toUpperCase() === method.val);
|
|
371
|
+
this.#method = standard ? { tag: standard } : { tag: "other", val: method.val };
|
|
372
|
+
} else if (STANDARD_METHODS.includes(method.tag))
|
|
373
|
+
this.#method = { tag: method.tag };
|
|
374
|
+
else
|
|
375
|
+
return err(undefined);
|
|
376
|
+
return ok(undefined);
|
|
377
|
+
}
|
|
378
|
+
getPathWithQuery() {
|
|
379
|
+
return this.#path;
|
|
380
|
+
}
|
|
381
|
+
setPathWithQuery(path) {
|
|
382
|
+
if (path === undefined) {
|
|
383
|
+
this.#path = undefined;
|
|
384
|
+
return ok(undefined);
|
|
385
|
+
}
|
|
386
|
+
const parsed = pathWithQuery(path);
|
|
387
|
+
if (parsed === undefined)
|
|
388
|
+
return err(undefined);
|
|
389
|
+
this.#path = parsed;
|
|
390
|
+
return ok(undefined);
|
|
391
|
+
}
|
|
392
|
+
getScheme() {
|
|
393
|
+
return this.#scheme;
|
|
394
|
+
}
|
|
395
|
+
setScheme(scheme) {
|
|
396
|
+
if (scheme === undefined || scheme.tag === "HTTP" || scheme.tag === "HTTPS") {
|
|
397
|
+
this.#scheme = scheme && { tag: scheme.tag };
|
|
398
|
+
return ok(undefined);
|
|
399
|
+
}
|
|
400
|
+
if (scheme.tag !== "other" || scheme.val.length > 64 || !SCHEME.test(scheme.val))
|
|
401
|
+
return err(undefined);
|
|
402
|
+
const lower = scheme.val.toLowerCase();
|
|
403
|
+
this.#scheme = lower === "http" ? { tag: "HTTP" } : lower === "https" ? { tag: "HTTPS" } : { tag: "other", val: scheme.val };
|
|
404
|
+
return ok(undefined);
|
|
405
|
+
}
|
|
406
|
+
getAuthority() {
|
|
407
|
+
return this.#authority;
|
|
408
|
+
}
|
|
409
|
+
setAuthority(value) {
|
|
410
|
+
if (value !== undefined && !authority(value))
|
|
411
|
+
return err(undefined);
|
|
412
|
+
this.#authority = value;
|
|
413
|
+
return ok(undefined);
|
|
414
|
+
}
|
|
415
|
+
getOptions() {
|
|
416
|
+
return this.options && new HttpRequestOptions(this.options, false);
|
|
417
|
+
}
|
|
418
|
+
get methodName() {
|
|
419
|
+
return this.#method.tag === "other" ? this.#method.val : this.#method.tag.toUpperCase();
|
|
420
|
+
}
|
|
421
|
+
target() {
|
|
422
|
+
const scheme = this.#scheme ?? { tag: "HTTPS" };
|
|
423
|
+
if (scheme.tag === "other")
|
|
424
|
+
return err({ tag: "HTTP-protocol-error" });
|
|
425
|
+
if (this.#authority === undefined || this.#path === undefined)
|
|
426
|
+
return err({ tag: "HTTP-request-URI-invalid" });
|
|
427
|
+
const secure = scheme.tag === "HTTPS";
|
|
428
|
+
const host = canonicalHost(this.#authority, secure);
|
|
429
|
+
if (!host)
|
|
430
|
+
return err({ tag: "HTTP-request-denied" });
|
|
431
|
+
let url;
|
|
432
|
+
try {
|
|
433
|
+
url = new URL(`${secure ? "https" : "http"}://${host.name}:${host.port}${this.#path}`);
|
|
434
|
+
} catch {
|
|
435
|
+
return err({ tag: "HTTP-request-URI-invalid" });
|
|
436
|
+
}
|
|
437
|
+
if (url.hostname !== host.name)
|
|
438
|
+
return err({ tag: "HTTP-request-denied" });
|
|
439
|
+
return ok({ url, pathWithQuery: this.#path });
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
class HttpResponse extends Message {
|
|
444
|
+
#status;
|
|
445
|
+
constructor(headers, body, status = 200) {
|
|
446
|
+
super(headers, body);
|
|
447
|
+
this.#status = status;
|
|
448
|
+
}
|
|
449
|
+
getStatusCode() {
|
|
450
|
+
return this.#status;
|
|
451
|
+
}
|
|
452
|
+
setStatusCode(status) {
|
|
453
|
+
if (status < 100 || status > 599)
|
|
454
|
+
return err(undefined);
|
|
455
|
+
this.#status = status;
|
|
456
|
+
return ok(undefined);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/interpreter/socket-addresses.ts
|
|
461
|
+
var uint = (value, maximum) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= maximum;
|
|
462
|
+
function fromWit(value) {
|
|
463
|
+
const tag = value?.tag;
|
|
464
|
+
const val = value?.val;
|
|
465
|
+
if (!val || tag !== "ipv4" && tag !== "ipv6")
|
|
466
|
+
return;
|
|
467
|
+
const ip = val.address;
|
|
468
|
+
const width = tag === "ipv4" ? 4 : 8;
|
|
469
|
+
const maximum = tag === "ipv4" ? 255 : 65535;
|
|
470
|
+
if (!Array.isArray(ip) || ip.length !== width || !ip.every((part) => uint(part, maximum)))
|
|
471
|
+
return;
|
|
472
|
+
if (!uint(val.port, 65535))
|
|
473
|
+
return;
|
|
474
|
+
if (tag === "ipv4")
|
|
475
|
+
return { family: "ipv4", ip: [...ip], port: val.port, flowInfo: 0, scopeId: 0 };
|
|
476
|
+
if (!uint(val.flowInfo, 4294967295) || !uint(val.scopeId, 4294967295))
|
|
477
|
+
return;
|
|
478
|
+
return {
|
|
479
|
+
family: "ipv6",
|
|
480
|
+
ip: [...ip],
|
|
481
|
+
port: val.port,
|
|
482
|
+
flowInfo: val.flowInfo,
|
|
483
|
+
scopeId: val.scopeId
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
function toWit(address) {
|
|
487
|
+
return address.family === "ipv4" ? {
|
|
488
|
+
tag: "ipv4",
|
|
489
|
+
val: { port: address.port, address: [...address.ip] }
|
|
490
|
+
} : {
|
|
491
|
+
tag: "ipv6",
|
|
492
|
+
val: {
|
|
493
|
+
port: address.port,
|
|
494
|
+
flowInfo: address.flowInfo,
|
|
495
|
+
address: [...address.ip],
|
|
496
|
+
scopeId: address.scopeId
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
var key = (address) => `${address.family}|${address.ip.join(".")}|${address.port}|${address.flowInfo}|${address.scopeId}`;
|
|
501
|
+
var unspecified = (family) => ({
|
|
502
|
+
family,
|
|
503
|
+
ip: new Array(family === "ipv4" ? 4 : 8).fill(0),
|
|
504
|
+
port: 0,
|
|
505
|
+
flowInfo: 0,
|
|
506
|
+
scopeId: 0
|
|
507
|
+
});
|
|
508
|
+
var isZero = (ip) => ip.every((part) => part === 0);
|
|
509
|
+
function mappedIpv4(address) {
|
|
510
|
+
const [a, b, c, d, e, f, g, h] = address.ip;
|
|
511
|
+
if (address.family !== "ipv6" || a || b || c || d || e || f !== 65535)
|
|
512
|
+
return;
|
|
513
|
+
return [g >> 8, g & 255, h >> 8, h & 255];
|
|
514
|
+
}
|
|
515
|
+
function canonicalIp(address) {
|
|
516
|
+
const mapped = mappedIpv4(address);
|
|
517
|
+
return mapped ? { family: "ipv4", ip: mapped } : address;
|
|
518
|
+
}
|
|
519
|
+
function isUnicast(address) {
|
|
520
|
+
const { family, ip } = canonicalIp(address);
|
|
521
|
+
if (family === "ipv4")
|
|
522
|
+
return !(ip[0] >= 224 && ip[0] <= 239) && !ip.every((o) => o === 255);
|
|
523
|
+
return ip[0] >> 8 !== 255;
|
|
524
|
+
}
|
|
525
|
+
var isRemote = (address) => !isZero(canonicalIp(address).ip) && address.port !== 0;
|
|
526
|
+
function hasFamily(address, family) {
|
|
527
|
+
if (address.family !== family)
|
|
528
|
+
return false;
|
|
529
|
+
if (family === "ipv4")
|
|
530
|
+
return true;
|
|
531
|
+
const ip = address.ip;
|
|
532
|
+
const compatible = ip.slice(0, 6).every((part) => part === 0) && !isZero(ip) && !(ip.slice(0, 7).every((part) => part === 0) && ip[7] === 1);
|
|
533
|
+
return !compatible && mappedIpv4(address) === undefined;
|
|
534
|
+
}
|
|
535
|
+
var isImplicit = (address) => isZero(address.ip) && address.port === 0;
|
|
536
|
+
function parseIpv4(text) {
|
|
537
|
+
const parts = text.split(".");
|
|
538
|
+
if (parts.length !== 4)
|
|
539
|
+
return;
|
|
540
|
+
const octets = parts.map((part) => /^(0|[1-9][0-9]{0,2})$/.test(part) ? Number(part) : Number.NaN);
|
|
541
|
+
return octets.every((octet) => octet <= 255) ? octets : undefined;
|
|
542
|
+
}
|
|
543
|
+
function parseIpv6(text) {
|
|
544
|
+
const halves = text.split("::");
|
|
545
|
+
if (halves.length > 2)
|
|
546
|
+
return;
|
|
547
|
+
const groups = (part) => {
|
|
548
|
+
if (part === "")
|
|
549
|
+
return [];
|
|
550
|
+
const pieces = part.split(":");
|
|
551
|
+
const out = [];
|
|
552
|
+
for (const [index, piece] of pieces.entries()) {
|
|
553
|
+
if (index === pieces.length - 1 && piece.includes(".")) {
|
|
554
|
+
const v4 = parseIpv4(piece);
|
|
555
|
+
if (!v4)
|
|
556
|
+
return;
|
|
557
|
+
out.push(v4[0] << 8 | v4[1], v4[2] << 8 | v4[3]);
|
|
558
|
+
} else if (/^[0-9a-fA-F]{1,4}$/.test(piece))
|
|
559
|
+
out.push(Number.parseInt(piece, 16));
|
|
560
|
+
else
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
return out;
|
|
564
|
+
};
|
|
565
|
+
const head = groups(halves[0]);
|
|
566
|
+
if (!head)
|
|
567
|
+
return;
|
|
568
|
+
if (halves.length === 1)
|
|
569
|
+
return head.length === 8 ? head : undefined;
|
|
570
|
+
if (halves[0].includes("."))
|
|
571
|
+
return;
|
|
572
|
+
const tail = groups(halves[1]);
|
|
573
|
+
if (!tail || head.length + tail.length > 7)
|
|
574
|
+
return;
|
|
575
|
+
return [...head, ...new Array(8 - head.length - tail.length).fill(0), ...tail];
|
|
576
|
+
}
|
|
577
|
+
function parseSocketAddress(text) {
|
|
578
|
+
const colon = text.lastIndexOf(":");
|
|
579
|
+
const portText = text.slice(colon + 1);
|
|
580
|
+
if (colon < 0 || !/^[0-9]{1,5}$/.test(portText) || Number(portText) > 65535)
|
|
581
|
+
return;
|
|
582
|
+
const port = Number(portText);
|
|
583
|
+
const host = text.slice(0, colon);
|
|
584
|
+
if (host.startsWith("[") && host.endsWith("]")) {
|
|
585
|
+
const [ipText, scopeText, extra] = host.slice(1, -1).split("%");
|
|
586
|
+
if (extra !== undefined)
|
|
587
|
+
return;
|
|
588
|
+
if (scopeText !== undefined && !(/^[0-9]{1,10}$/.test(scopeText) && Number(scopeText) <= 4294967295))
|
|
589
|
+
return;
|
|
590
|
+
const ip = parseIpv6(ipText);
|
|
591
|
+
return ip && { family: "ipv6", ip, port, flowInfo: 0, scopeId: Number(scopeText ?? 0) };
|
|
592
|
+
}
|
|
593
|
+
const ip = parseIpv4(host);
|
|
594
|
+
return ip && { family: "ipv4", ip, port, flowInfo: 0, scopeId: 0 };
|
|
595
|
+
}
|
|
596
|
+
var ipAddress = (family, ip) => ({ tag: family, val: [...ip] });
|
|
597
|
+
function checkedIpAddress(value) {
|
|
598
|
+
const tag = value?.tag;
|
|
599
|
+
const ip = value?.val;
|
|
600
|
+
const width = tag === "ipv4" ? 4 : tag === "ipv6" ? 8 : 0;
|
|
601
|
+
const maximum = tag === "ipv4" ? 255 : 65535;
|
|
602
|
+
if (!width || !Array.isArray(ip) || ip.length !== width || !ip.every((p) => uint(p, maximum)))
|
|
603
|
+
return;
|
|
604
|
+
const canonical = canonicalIp({
|
|
605
|
+
family: tag,
|
|
606
|
+
ip,
|
|
607
|
+
port: 0,
|
|
608
|
+
flowInfo: 0,
|
|
609
|
+
scopeId: 0
|
|
610
|
+
});
|
|
611
|
+
return ipAddress(canonical.family, canonical.ip);
|
|
612
|
+
}
|
|
613
|
+
var FORBIDDEN_ASCII = new Set('#/:<>?@[\\]^|"');
|
|
614
|
+
var forbidden = (text) => [...text].some((char) => {
|
|
615
|
+
const code = char.charCodeAt(0);
|
|
616
|
+
return code <= 32 || code === 127 || FORBIDDEN_ASCII.has(char);
|
|
617
|
+
});
|
|
618
|
+
function parseHostName(name) {
|
|
619
|
+
if (name.startsWith("[")) {
|
|
620
|
+
if (!name.endsWith("]"))
|
|
621
|
+
return;
|
|
622
|
+
const ip = parseIpv6(name.slice(1, -1));
|
|
623
|
+
return ip && { ip: ipAddress("ipv6", ip) };
|
|
624
|
+
}
|
|
625
|
+
if (name.includes(":")) {
|
|
626
|
+
const ip = parseIpv6(name);
|
|
627
|
+
return ip && { ip: ipAddress("ipv6", ip) };
|
|
628
|
+
}
|
|
629
|
+
if (!name || forbidden(name))
|
|
630
|
+
return;
|
|
631
|
+
let host;
|
|
632
|
+
try {
|
|
633
|
+
host = new URL(`http://${name}/`).hostname;
|
|
634
|
+
} catch {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
if (!host || forbidden(host))
|
|
638
|
+
return;
|
|
639
|
+
const ipv4 = parseIpv4(host);
|
|
640
|
+
return ipv4 ? { ip: ipAddress("ipv4", ipv4) } : { domain: host };
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export { fromWit, toWit, key, unspecified, isUnicast, isRemote, hasFamily, isImplicit, parseSocketAddress, checkedIpAddress, parseHostName, ok, err, internal, done, digits, HttpFields, HttpRequestOptions, HttpRequest, HttpResponse };
|