@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,8 @@
|
|
|
1
|
+
/** UTF-8 codecs use ECMAScript primitives available in every supported guest engine. */
|
|
2
|
+
export declare function encodeText(text: string): Uint8Array;
|
|
3
|
+
/** Replace invalid UTF-8 sequences and retain at most one partial scalar between chunks. */
|
|
4
|
+
export declare class Utf8Decoder {
|
|
5
|
+
#private;
|
|
6
|
+
decode(bytes?: Uint8Array, final?: boolean): string;
|
|
7
|
+
}
|
|
8
|
+
export declare function decodeText(body: AsyncIterable<Uint8Array>, limit: number): Promise<string>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface ServerSentEvent {
|
|
2
|
+
readonly event: string;
|
|
3
|
+
readonly data: string;
|
|
4
|
+
readonly id: string;
|
|
5
|
+
}
|
|
6
|
+
/** Adapt the maintained SSE parser to a bounded byte stream; reconnection belongs to the caller. */
|
|
7
|
+
export declare function events(body: AsyncIterable<Uint8Array>): AsyncIterable<ServerSentEvent>;
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type ErrorCode, type HeaderError, type RequestOptionsError } from "./wasi/wasi-http-types.js";
|
|
2
|
+
import { type ServerSentEvent } from "./http/events.ts";
|
|
3
|
+
export type { ServerSentEvent } from "./http/events.ts";
|
|
4
|
+
export type HeaderInput = Readonly<Record<string, string>> | Iterable<readonly [string, string | Uint8Array]>;
|
|
5
|
+
/** A snapshot of standard HTTP fields. Iteration and getAll preserve duplicates and bytes. */
|
|
6
|
+
export declare class HttpHeaders implements Iterable<readonly [string, Uint8Array]> {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(entries?: Iterable<readonly [string, Uint8Array]>);
|
|
9
|
+
get(name: string): string | undefined;
|
|
10
|
+
getAll(name: string): readonly Uint8Array[];
|
|
11
|
+
[Symbol.iterator](): IterableIterator<readonly [string, Uint8Array]>;
|
|
12
|
+
}
|
|
13
|
+
export interface HttpRequest {
|
|
14
|
+
method?: string;
|
|
15
|
+
headers?: HeaderInput;
|
|
16
|
+
body?: string | Uint8Array | AsyncIterable<Uint8Array>;
|
|
17
|
+
/** Optional trailing fields; native HTTP/1 also requires their names in the Trailer header. */
|
|
18
|
+
trailers?: PromiseLike<HeaderInput | undefined>;
|
|
19
|
+
/** Standard WASI durations, in nanoseconds. */
|
|
20
|
+
timeouts?: {
|
|
21
|
+
connect?: bigint;
|
|
22
|
+
firstByte?: bigint;
|
|
23
|
+
betweenBytes?: bigint;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Consume a response once. Call close when the body is no longer needed. */
|
|
27
|
+
export interface HttpResponse {
|
|
28
|
+
readonly status: number;
|
|
29
|
+
readonly ok: boolean;
|
|
30
|
+
readonly headers: HttpHeaders;
|
|
31
|
+
readonly body: AsyncIterable<Uint8Array>;
|
|
32
|
+
text(): Promise<string>;
|
|
33
|
+
json<T = unknown>(): Promise<T>;
|
|
34
|
+
events(): AsyncIterable<ServerSentEvent>;
|
|
35
|
+
/** Resolves after the response body has been consumed. */
|
|
36
|
+
trailers(): Promise<HttpHeaders>;
|
|
37
|
+
close(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/** A failed standard HTTP operation; its typed WIT error remains available as code. */
|
|
40
|
+
export declare class HttpError extends Error {
|
|
41
|
+
readonly code: ErrorCode | HeaderError | RequestOptionsError | undefined;
|
|
42
|
+
constructor(code: ErrorCode | HeaderError | RequestOptionsError | undefined);
|
|
43
|
+
}
|
|
44
|
+
/** HTTP convenience over standard WASI imports. Network I/O and TLS belong to the host. */
|
|
45
|
+
export declare function fetch(url: string, options?: HttpRequest): Promise<HttpResponse>;
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readChunks2,
|
|
3
|
+
writeChunks2
|
|
4
|
+
} from "./chunks/index-kr6bnh34.js";
|
|
5
|
+
|
|
6
|
+
// src/http.ts
|
|
7
|
+
import { encodeHost, parseURL } from "ufo";
|
|
8
|
+
import { send } from "wasi:http/client@0.3.0";
|
|
9
|
+
import {
|
|
10
|
+
Fields,
|
|
11
|
+
Request,
|
|
12
|
+
RequestOptions,
|
|
13
|
+
Response
|
|
14
|
+
} from "wasi:http/types@0.3.0";
|
|
15
|
+
|
|
16
|
+
// src/http/bytes.ts
|
|
17
|
+
function encodeText(text) {
|
|
18
|
+
const bytes = [];
|
|
19
|
+
for (const character of text) {
|
|
20
|
+
let point = character.codePointAt(0);
|
|
21
|
+
if (point >= 55296 && point <= 57343)
|
|
22
|
+
point = 65533;
|
|
23
|
+
if (point < 128)
|
|
24
|
+
bytes.push(point);
|
|
25
|
+
else if (point < 2048)
|
|
26
|
+
bytes.push(192 | point >> 6, 128 | point & 63);
|
|
27
|
+
else if (point < 65536)
|
|
28
|
+
bytes.push(224 | point >> 12, 128 | point >> 6 & 63, 128 | point & 63);
|
|
29
|
+
else
|
|
30
|
+
bytes.push(240 | point >> 18, 128 | point >> 12 & 63, 128 | point >> 6 & 63, 128 | point & 63);
|
|
31
|
+
}
|
|
32
|
+
return Uint8Array.from(bytes);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class Utf8Decoder {
|
|
36
|
+
#pending = [];
|
|
37
|
+
decode(bytes = new Uint8Array, final = false) {
|
|
38
|
+
const input = this.#pending.length ? Uint8Array.from([...this.#pending, ...bytes]) : bytes;
|
|
39
|
+
this.#pending = [];
|
|
40
|
+
const output = [];
|
|
41
|
+
for (let index = 0;index < input.length; ) {
|
|
42
|
+
const first = input[index];
|
|
43
|
+
const width = first < 128 ? 1 : first >= 194 && first <= 223 ? 2 : first >= 224 && first <= 239 ? 3 : first >= 240 && first <= 244 ? 4 : 0;
|
|
44
|
+
if (!width) {
|
|
45
|
+
output.push("�");
|
|
46
|
+
index++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (index + width > input.length && !final) {
|
|
50
|
+
this.#pending = [...input.subarray(index)];
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
let point = first & (width === 1 ? 127 : (1 << 7 - width) - 1);
|
|
54
|
+
let count = 1;
|
|
55
|
+
while (count < width) {
|
|
56
|
+
const next = input[index + count];
|
|
57
|
+
if (next === undefined || (next & 192) !== 128 || count === 1 && (first === 224 && next < 160 || first === 237 && next >= 160 || first === 240 && next < 144 || first === 244 && next >= 144))
|
|
58
|
+
break;
|
|
59
|
+
point = point << 6 | next & 63;
|
|
60
|
+
count++;
|
|
61
|
+
}
|
|
62
|
+
output.push(count === width ? String.fromCodePoint(point) : "�");
|
|
63
|
+
index += count;
|
|
64
|
+
}
|
|
65
|
+
return output.join("");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function decodeText(body, limit) {
|
|
69
|
+
const decoder = new Utf8Decoder;
|
|
70
|
+
const output = [];
|
|
71
|
+
let bytes = 0;
|
|
72
|
+
for await (const chunk of body) {
|
|
73
|
+
bytes += chunk.length;
|
|
74
|
+
if (bytes > limit)
|
|
75
|
+
throw new Error("HTTP text body exceeds the limit; consume the byte stream instead");
|
|
76
|
+
output.push(decoder.decode(chunk));
|
|
77
|
+
}
|
|
78
|
+
output.push(decoder.decode(undefined, true));
|
|
79
|
+
return output.join("");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/http/events.ts
|
|
83
|
+
import { createParser } from "eventsource-parser";
|
|
84
|
+
async function* events(body) {
|
|
85
|
+
const decoder = new Utf8Decoder;
|
|
86
|
+
const pending = [];
|
|
87
|
+
let id = "";
|
|
88
|
+
const parser = createParser({
|
|
89
|
+
maxBufferSize: 1 << 20,
|
|
90
|
+
onId(value) {
|
|
91
|
+
id = value;
|
|
92
|
+
},
|
|
93
|
+
onEvent(event) {
|
|
94
|
+
pending.push({ event: event.event ?? "message", data: event.data, id: event.id ?? id });
|
|
95
|
+
},
|
|
96
|
+
onError(error) {
|
|
97
|
+
if (error.type === "max-buffer-size-exceeded")
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
for await (const bytes of body) {
|
|
102
|
+
parser.feed(decoder.decode(bytes));
|
|
103
|
+
yield* pending;
|
|
104
|
+
pending.length = 0;
|
|
105
|
+
}
|
|
106
|
+
parser.feed(decoder.decode(undefined, true));
|
|
107
|
+
yield* pending;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/http.ts
|
|
111
|
+
class HttpHeaders {
|
|
112
|
+
#entries;
|
|
113
|
+
constructor(entries = []) {
|
|
114
|
+
this.#entries = [...entries].map(([name, bytes]) => [name.toLowerCase(), bytes.slice()]);
|
|
115
|
+
}
|
|
116
|
+
get(name) {
|
|
117
|
+
const value = this.#entries.find(([key]) => key === name.toLowerCase())?.[1];
|
|
118
|
+
return value === undefined ? undefined : new Utf8Decoder().decode(value, true);
|
|
119
|
+
}
|
|
120
|
+
getAll(name) {
|
|
121
|
+
return this.#entries.filter(([key]) => key === name.toLowerCase()).map(([, bytes]) => bytes.slice());
|
|
122
|
+
}
|
|
123
|
+
*[Symbol.iterator]() {
|
|
124
|
+
for (const [name, bytes] of this.#entries)
|
|
125
|
+
yield [name, bytes.slice()];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class HttpError extends Error {
|
|
130
|
+
code;
|
|
131
|
+
constructor(code) {
|
|
132
|
+
super(`HTTP operation failed: ${code?.tag ?? "request rejected"}`);
|
|
133
|
+
this.code = code;
|
|
134
|
+
this.name = "HttpError";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function fields(input = {}) {
|
|
138
|
+
const entries = Symbol.iterator in input ? [...input] : Object.entries(input);
|
|
139
|
+
return Fields.fromList(entries.map(([name, value]) => [name, typeof value === "string" ? encodeText(value) : value]));
|
|
140
|
+
}
|
|
141
|
+
function drop(value) {
|
|
142
|
+
try {
|
|
143
|
+
value?.[Symbol.dispose]?.();
|
|
144
|
+
} catch {}
|
|
145
|
+
}
|
|
146
|
+
function result(value) {
|
|
147
|
+
if (value.tag === "err")
|
|
148
|
+
throw new HttpError(value.val);
|
|
149
|
+
return value.val;
|
|
150
|
+
}
|
|
151
|
+
async function fetch(url, options = {}) {
|
|
152
|
+
const parsed = parseURL(url);
|
|
153
|
+
const scheme = parsed.protocol?.toLowerCase();
|
|
154
|
+
if (scheme !== "http:" && scheme !== "https:" || !parsed.host || parsed.auth)
|
|
155
|
+
throw new TypeError("HTTP requires an absolute http or https URL without credentials");
|
|
156
|
+
let method = options.method ?? "GET";
|
|
157
|
+
const standard = ["DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH"];
|
|
158
|
+
if (standard.includes(method.toUpperCase()))
|
|
159
|
+
method = method.toUpperCase();
|
|
160
|
+
if (["CONNECT", "TRACE", "TRACK"].includes(method.toUpperCase()))
|
|
161
|
+
throw new TypeError(`HTTP method ${method} is forbidden`);
|
|
162
|
+
if ((method === "GET" || method === "HEAD") && options.body !== undefined)
|
|
163
|
+
throw new TypeError(`${method} requests cannot have a body`);
|
|
164
|
+
if (options.body !== undefined && typeof options.body !== "string" && !(options.body instanceof Uint8Array) && typeof options.body?.[Symbol.asyncIterator] !== "function")
|
|
165
|
+
throw new TypeError("HTTP body requires text, bytes or an AsyncIterable<Uint8Array>");
|
|
166
|
+
if ([...parsed.host].some((character) => character.charCodeAt(0) <= 32 || character.charCodeAt(0) === 127))
|
|
167
|
+
throw new TypeError("HTTP URL host contains whitespace or controls");
|
|
168
|
+
let headers;
|
|
169
|
+
let timeouts;
|
|
170
|
+
let request;
|
|
171
|
+
let upload;
|
|
172
|
+
try {
|
|
173
|
+
headers = fields(options.headers);
|
|
174
|
+
if (options.timeouts) {
|
|
175
|
+
timeouts = new RequestOptions;
|
|
176
|
+
timeouts.setConnectTimeout(options.timeouts.connect);
|
|
177
|
+
timeouts.setFirstByteTimeout(options.timeouts.firstByte);
|
|
178
|
+
timeouts.setBetweenBytesTimeout(options.timeouts.betweenBytes);
|
|
179
|
+
}
|
|
180
|
+
const body = typeof options.body === "string" ? [encodeText(options.body)] : options.body instanceof Uint8Array ? [options.body.slice()] : options.body;
|
|
181
|
+
const trailers = Promise.resolve(options.trailers).then((value) => ({ tag: "ok", val: value ? fields(value) : undefined }), (error) => ({ tag: "err", val: { tag: "internal-error", val: String(error) } }));
|
|
182
|
+
const [created, done] = Request.new(headers, body === undefined ? undefined : writeChunks2(body), trailers, timeouts);
|
|
183
|
+
request = created;
|
|
184
|
+
upload = done;
|
|
185
|
+
request.setMethod(standard.includes(method) ? { tag: method.toLowerCase() } : { tag: "other", val: method });
|
|
186
|
+
request.setScheme({ tag: scheme === "https:" ? "HTTPS" : "HTTP" });
|
|
187
|
+
request.setAuthority(encodeHost(parsed.host));
|
|
188
|
+
request.setPathWithQuery((parsed.pathname || "/") + (parsed.search || ""));
|
|
189
|
+
const pending = send(request);
|
|
190
|
+
request = undefined;
|
|
191
|
+
return response(await pending, upload);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
drop(request);
|
|
194
|
+
drop(upload);
|
|
195
|
+
if (error && typeof error === "object" && "payload" in error)
|
|
196
|
+
throw new HttpError(error.payload);
|
|
197
|
+
throw error;
|
|
198
|
+
} finally {
|
|
199
|
+
drop(headers);
|
|
200
|
+
drop(timeouts);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function response(value, upload) {
|
|
204
|
+
const status = value.getStatusCode();
|
|
205
|
+
const fields = value.getHeaders();
|
|
206
|
+
let headers;
|
|
207
|
+
try {
|
|
208
|
+
headers = new HttpHeaders(fields.copyAll());
|
|
209
|
+
} finally {
|
|
210
|
+
drop(fields);
|
|
211
|
+
}
|
|
212
|
+
let finish;
|
|
213
|
+
const handled = new Promise((resolve) => {
|
|
214
|
+
finish = resolve;
|
|
215
|
+
});
|
|
216
|
+
let stream;
|
|
217
|
+
let complete;
|
|
218
|
+
let consumed = false;
|
|
219
|
+
let ended = false;
|
|
220
|
+
let closed = false;
|
|
221
|
+
let trailing;
|
|
222
|
+
const trailers = () => {
|
|
223
|
+
if (!complete)
|
|
224
|
+
return Promise.reject(new Error("Consume the HTTP body before reading trailers"));
|
|
225
|
+
return trailing ??= Promise.resolve(complete).then((state) => {
|
|
226
|
+
const fields = result(state);
|
|
227
|
+
try {
|
|
228
|
+
return new HttpHeaders(fields?.copyAll());
|
|
229
|
+
} finally {
|
|
230
|
+
drop(fields);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
const close = async () => {
|
|
235
|
+
if (closed)
|
|
236
|
+
return;
|
|
237
|
+
closed = true;
|
|
238
|
+
drop(upload);
|
|
239
|
+
if (!ended) {
|
|
240
|
+
finish({ tag: "err", val: { tag: "internal-error", val: "response closed" } });
|
|
241
|
+
if (stream) {
|
|
242
|
+
drop(stream);
|
|
243
|
+
drop(complete);
|
|
244
|
+
} else
|
|
245
|
+
drop(value);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const body = {
|
|
249
|
+
async* [Symbol.asyncIterator]() {
|
|
250
|
+
if (consumed)
|
|
251
|
+
throw new Error("HTTP body has already been consumed");
|
|
252
|
+
if (closed)
|
|
253
|
+
throw new Error("HTTP response is closed");
|
|
254
|
+
consumed = true;
|
|
255
|
+
try {
|
|
256
|
+
[stream, complete] = Response.consumeBody(value, handled);
|
|
257
|
+
yield* readChunks2(stream);
|
|
258
|
+
finish({ tag: "ok", val: undefined });
|
|
259
|
+
await trailers();
|
|
260
|
+
result(await upload);
|
|
261
|
+
ended = true;
|
|
262
|
+
} finally {
|
|
263
|
+
await close();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
return Object.freeze({
|
|
268
|
+
status,
|
|
269
|
+
ok: status >= 200 && status < 300,
|
|
270
|
+
headers,
|
|
271
|
+
body,
|
|
272
|
+
text: () => decodeText(body, 1 << 20),
|
|
273
|
+
json: async () => JSON.parse(await decodeText(body, 1 << 20)),
|
|
274
|
+
events: async function* () {
|
|
275
|
+
try {
|
|
276
|
+
if (headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() !== "text/event-stream")
|
|
277
|
+
throw new Error("SSE requires text/event-stream");
|
|
278
|
+
yield* events(body);
|
|
279
|
+
} finally {
|
|
280
|
+
await close();
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
trailers,
|
|
284
|
+
close
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
export {
|
|
288
|
+
HttpError,
|
|
289
|
+
HttpHeaders,
|
|
290
|
+
fetch
|
|
291
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -10,3 +10,5 @@ export type { Readable, State, StateOptions } from "./state.ts";
|
|
|
10
10
|
export { Code, HookCapability, Lifetime, Reason, ReloadStatus, Status, } from "./bridge/identifiers.ts";
|
|
11
11
|
export { Errors, type ErrorDescriptor } from "./errors.ts";
|
|
12
12
|
export { ContextKey, ContextKind, opaqueFrom, type OpaqueContextValue } from "./context-key.ts";
|
|
13
|
+
export type { WitMethods } from "./authoring/wit.ts";
|
|
14
|
+
export { witClient, type WitResource, type WitClientSchema } from "./runtime/wit-client.ts";
|