@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,69 @@
|
|
|
1
|
+
// Generated from crates/executor-wasm/src/abi.rs. Do not edit.
|
|
2
|
+
// Regenerate with: TEMBRA_BLESS=1 cargo test -p tembra-executor-wasm --lib abi
|
|
3
|
+
export const IMPORT_MODULE =
|
|
4
|
+
"tembra-interpreter-v13-d82248b7b421e5294c2536726adcbbe2c178bc87142fc7ee04faf691081c101b";
|
|
5
|
+
export const SYNC_IMPORT = "sync";
|
|
6
|
+
export const SYNC_READ_IMPORT = "sync_read";
|
|
7
|
+
export const MEMORY_MODULE = "env";
|
|
8
|
+
export const MEMORY_IMPORT = "memory";
|
|
9
|
+
export const INITIALIZE_EXPORT = "_initialize";
|
|
10
|
+
export const ALLOC_EXPORT = "tembra_alloc";
|
|
11
|
+
export const CALL_EXPORT = "tembra_call";
|
|
12
|
+
export const WASI_MODULE = "wasi_snapshot_preview1";
|
|
13
|
+
export const SYNC_ERROR = 2147483648;
|
|
14
|
+
export const STANDARD_PACKET = 0;
|
|
15
|
+
export const MEMORY_INITIAL_PAGES = 32;
|
|
16
|
+
export const PAGE_BYTES = 65536;
|
|
17
|
+
export const LENGTH_LIMIT = 2147483647;
|
|
18
|
+
export const EFFECT_LIMIT = 128;
|
|
19
|
+
export const IDENTITY_LIMIT = 9007199254740991;
|
|
20
|
+
export const REPLY_HEADER_BYTES = 20;
|
|
21
|
+
export const EFFECT_RECORD_BYTES = 12;
|
|
22
|
+
export const COMPLETION_HEADER_BYTES = 4;
|
|
23
|
+
export const COMPLETION_RECORD_BYTES = 16;
|
|
24
|
+
export const ERRNO_SUCCESS = 0;
|
|
25
|
+
export const ERRNO_BADF = 8;
|
|
26
|
+
export const ERRNO_FAULT = 21;
|
|
27
|
+
export const ERRNO_INVAL = 28;
|
|
28
|
+
export const ERRNO_NOTDIR = 54;
|
|
29
|
+
export const ERRNO_SPIPE = 70;
|
|
30
|
+
export const CLOCK_REALTIME = 0;
|
|
31
|
+
export const CLOCK_MONOTONIC = 1;
|
|
32
|
+
export const STDIO_DESCRIPTORS = 3;
|
|
33
|
+
export const FILETYPE_CHARACTER_DEVICE = 2;
|
|
34
|
+
export const RIGHT_FD_WRITE = 64;
|
|
35
|
+
export const FDSTAT_BYTES = 24;
|
|
36
|
+
export const STDIO_LIMIT = 4096;
|
|
37
|
+
export const WASI_IMPORTS = Object.freeze([
|
|
38
|
+
"clock_time_get",
|
|
39
|
+
"environ_get",
|
|
40
|
+
"environ_sizes_get",
|
|
41
|
+
"fd_close",
|
|
42
|
+
"fd_fdstat_get",
|
|
43
|
+
"fd_prestat_dir_name",
|
|
44
|
+
"fd_prestat_get",
|
|
45
|
+
"fd_seek",
|
|
46
|
+
"fd_write",
|
|
47
|
+
"path_open",
|
|
48
|
+
"proc_exit",
|
|
49
|
+
] as const);
|
|
50
|
+
export const Operation = Object.freeze({
|
|
51
|
+
Load: 0,
|
|
52
|
+
Declare: 1,
|
|
53
|
+
Start: 2,
|
|
54
|
+
Resume: 3,
|
|
55
|
+
Cancel: 4,
|
|
56
|
+
Capture: 5,
|
|
57
|
+
} as const);
|
|
58
|
+
export type Operation = (typeof Operation)[keyof typeof Operation];
|
|
59
|
+
export const ReplyKind = Object.freeze({
|
|
60
|
+
Pending: 0,
|
|
61
|
+
Ok: 1,
|
|
62
|
+
Error: 2,
|
|
63
|
+
} as const);
|
|
64
|
+
export type ReplyKind = (typeof ReplyKind)[keyof typeof ReplyKind];
|
|
65
|
+
export const Status = Object.freeze({
|
|
66
|
+
Ok: 0,
|
|
67
|
+
Error: 1,
|
|
68
|
+
} as const);
|
|
69
|
+
export type Status = (typeof Status)[keyof typeof Status];
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Context.get` acquisition shared by source scopes and compiled component scopes.
|
|
3
|
+
*
|
|
4
|
+
* The guest names only an import binding (already resolved to its qualified service) and a key.
|
|
5
|
+
* The host resolves the installed requirement's API version and methods, acquires under the
|
|
6
|
+
* admitted invocation's root (and therefore its principal), and awaits readiness under the
|
|
7
|
+
* callback's cancellation. A claim whose wait, authority or publication fails is released before
|
|
8
|
+
* any guest handle exists; the runtime's root claim list releases a published claim when its root
|
|
9
|
+
* ends.
|
|
10
|
+
*/
|
|
11
|
+
import type { Declaration } from "../authoring/declaration.ts";
|
|
12
|
+
import type { DynamicReference } from "../authoring/types.ts";
|
|
13
|
+
import type { Context } from "../runtime/context.ts";
|
|
14
|
+
import { checkSignal, combinedSignal } from "../runtime/signals.ts";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Acquire `service` at `key` for the admitted invocation of `context` and publish it with
|
|
18
|
+
* `publish` once ready. `ended` is the issuing callback's own lifetime.
|
|
19
|
+
*/
|
|
20
|
+
export async function acquireReference<T>(
|
|
21
|
+
declaration: Declaration,
|
|
22
|
+
service: string,
|
|
23
|
+
key: readonly string[],
|
|
24
|
+
context: Context,
|
|
25
|
+
ended: AbortSignal,
|
|
26
|
+
publish: (reference: DynamicReference) => T,
|
|
27
|
+
): Promise<T> {
|
|
28
|
+
context.check();
|
|
29
|
+
// Only an admitted method call selects instances. A cleanup context (a resource `close`) has
|
|
30
|
+
// no invocation; acquiring there would choose instances by the cleanup principal.
|
|
31
|
+
if (!context.invocation) throw new Error("Reference acquisition requires an admitted invocation");
|
|
32
|
+
const session = declaration.session;
|
|
33
|
+
const requirement = session.scopes.requirement(declaration.environment, service);
|
|
34
|
+
const reference = session.handles.hold(
|
|
35
|
+
requirement.service,
|
|
36
|
+
key,
|
|
37
|
+
{
|
|
38
|
+
...(requirement.apiVersion ? { apiVersion: requirement.apiVersion } : {}),
|
|
39
|
+
...(requirement.methods ? { methods: requirement.methods } : {}),
|
|
40
|
+
},
|
|
41
|
+
context,
|
|
42
|
+
);
|
|
43
|
+
try {
|
|
44
|
+
await session.calls.observe(reference.ready, combinedSignal(context.signal, ended));
|
|
45
|
+
// The invocation, root or callback may have ended while readiness was pending.
|
|
46
|
+
context.check();
|
|
47
|
+
checkSignal(ended);
|
|
48
|
+
return publish(reference);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// A secondary release failure is reported to the session instead of replacing the cause.
|
|
51
|
+
try {
|
|
52
|
+
reference.release();
|
|
53
|
+
} catch (release) {
|
|
54
|
+
session.report(release);
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { Grants } from "../authoring/types.ts";
|
|
2
|
+
import type { HttpProvider } from "./http-provider.ts";
|
|
3
|
+
import { type Address, isImplicit, key, parseSocketAddress } from "./socket-addresses.ts";
|
|
4
|
+
import type { SocketProvider } from "./socket-provider.ts";
|
|
5
|
+
import type { HttpClient } from "./standard-http.ts";
|
|
6
|
+
|
|
7
|
+
export interface BrowserNetworkProviders {
|
|
8
|
+
http?: Readonly<Record<string, HttpProvider>>;
|
|
9
|
+
network?: Readonly<Record<string, SocketProvider>>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Address uses checked before a socket side effect, as upstream `SocketAddrUse`. */
|
|
13
|
+
export type SocketUse =
|
|
14
|
+
| "tcp-bind"
|
|
15
|
+
| "tcp-listen"
|
|
16
|
+
| "tcp-accept"
|
|
17
|
+
| "tcp-connect"
|
|
18
|
+
| "udp-bind"
|
|
19
|
+
| "udp-send"
|
|
20
|
+
| "udp-receive";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The socket and lookup authority of exactly one provider: the grants naming that provider,
|
|
24
|
+
* never another provider's. Rules mirror native `Sockets::allows`.
|
|
25
|
+
*/
|
|
26
|
+
export class SocketGrants {
|
|
27
|
+
readonly #connect = new Set<string>();
|
|
28
|
+
readonly #listen: Address[] = [];
|
|
29
|
+
readonly #listenKeys = new Set<string>();
|
|
30
|
+
readonly #bind = new Set<string>();
|
|
31
|
+
readonly #datagrams = new Set<string>();
|
|
32
|
+
readonly resolve: boolean;
|
|
33
|
+
|
|
34
|
+
constructor(grants: Grants, provider: string) {
|
|
35
|
+
let resolve = false;
|
|
36
|
+
const parse = (text: string) => {
|
|
37
|
+
const address = parseSocketAddress(text);
|
|
38
|
+
if (!address) throw new Error(`Invalid network grant address ${text}`);
|
|
39
|
+
return address;
|
|
40
|
+
};
|
|
41
|
+
for (const grant of grants.network ?? []) {
|
|
42
|
+
if (grant.provider !== provider) continue;
|
|
43
|
+
for (const text of grant.connect ?? []) this.#connect.add(key(parse(text)));
|
|
44
|
+
for (const text of grant.listen ?? []) {
|
|
45
|
+
const address = parse(text);
|
|
46
|
+
this.#listen.push(address);
|
|
47
|
+
this.#listenKeys.add(key(address));
|
|
48
|
+
}
|
|
49
|
+
for (const text of grant.bind ?? []) this.#bind.add(key(parse(text)));
|
|
50
|
+
for (const text of grant.datagrams ?? []) this.#datagrams.add(key(parse(text)));
|
|
51
|
+
resolve ||= grant.resolve === true;
|
|
52
|
+
}
|
|
53
|
+
this.resolve = resolve;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Any TCP authority (`allow_tcp`). */
|
|
57
|
+
get tcp(): boolean {
|
|
58
|
+
return this.#connect.size > 0 || this.#listen.length > 0 || this.#bind.size > 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Any UDP authority (`allow_udp`). */
|
|
62
|
+
get udp(): boolean {
|
|
63
|
+
return this.#datagrams.size > 0 || this.#bind.size > 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
allows(address: Address, use: SocketUse): boolean {
|
|
67
|
+
const exact = key(address);
|
|
68
|
+
switch (use) {
|
|
69
|
+
case "tcp-connect":
|
|
70
|
+
return this.#connect.has(exact);
|
|
71
|
+
case "tcp-bind":
|
|
72
|
+
return (
|
|
73
|
+
(isImplicit(address) && this.tcp) || this.#bind.has(exact) || this.#listenKeys.has(exact)
|
|
74
|
+
);
|
|
75
|
+
case "tcp-listen":
|
|
76
|
+
// Rust compares `IpAddr` here, so flow information and scope do not participate.
|
|
77
|
+
return this.#listen.some(
|
|
78
|
+
(granted) =>
|
|
79
|
+
granted.family === address.family &&
|
|
80
|
+
granted.ip.every((part, index) => part === address.ip[index]) &&
|
|
81
|
+
(granted.port === 0 || granted.port === address.port),
|
|
82
|
+
);
|
|
83
|
+
case "tcp-accept":
|
|
84
|
+
return this.#listen.length > 0;
|
|
85
|
+
case "udp-bind":
|
|
86
|
+
return (isImplicit(address) && this.#datagrams.size > 0) || this.#bind.has(exact);
|
|
87
|
+
case "udp-send":
|
|
88
|
+
case "udp-receive":
|
|
89
|
+
return this.#datagrams.has(exact);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The provider and authority behind one Orb's `wasi:sockets` import. */
|
|
95
|
+
export interface SocketAccess {
|
|
96
|
+
/** `undefined` when the bound provider name has no registered socket provider. */
|
|
97
|
+
readonly provider: SocketProvider | undefined;
|
|
98
|
+
readonly grants: SocketGrants;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Granted network selections of one Orb:
|
|
103
|
+
* - HTTP is served only through standard `wasi:http` (`provider`, checked by the standard host
|
|
104
|
+
* before any provider call);
|
|
105
|
+
* - sockets and name lookup use the providers bound to the Orb's exact WIT imports (`sockets`).
|
|
106
|
+
*/
|
|
107
|
+
export class BrowserNetwork implements HttpClient {
|
|
108
|
+
#disposed = false;
|
|
109
|
+
|
|
110
|
+
constructor(
|
|
111
|
+
readonly grants: Grants,
|
|
112
|
+
readonly providers: BrowserNetworkProviders,
|
|
113
|
+
) {
|
|
114
|
+
for (const grant of grants.http ?? []) {
|
|
115
|
+
if (!grant.provider.trim() || !grant.origins.length)
|
|
116
|
+
throw new Error("HTTP grants require a provider and at least one origin");
|
|
117
|
+
for (const origin of grant.origins) {
|
|
118
|
+
const url = new URL(origin);
|
|
119
|
+
if (
|
|
120
|
+
!["http:", "https:"].includes(url.protocol) ||
|
|
121
|
+
!url.hostname ||
|
|
122
|
+
url.username ||
|
|
123
|
+
url.password ||
|
|
124
|
+
url.href.includes("#") ||
|
|
125
|
+
url.pathname !== "/" ||
|
|
126
|
+
url.href.includes("?") ||
|
|
127
|
+
url.hostname.includes("*") ||
|
|
128
|
+
url.port === "0"
|
|
129
|
+
)
|
|
130
|
+
throw new Error(
|
|
131
|
+
"HTTP grants require exact http(s) origins without credentials, paths, queries, wildcards or port 0",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Native checks these addresses at installation too; refuse a malformed grant early here.
|
|
136
|
+
for (const grant of grants.network ?? []) new SocketGrants(grants, grant.provider);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The provider granted for exactly `url`'s origin. Standard `wasi:http` sends call this before
|
|
141
|
+
* any provider work; HTTP grants never follow raw socket grants or the reverse.
|
|
142
|
+
*/
|
|
143
|
+
provider(url: URL): HttpProvider | undefined {
|
|
144
|
+
if (
|
|
145
|
+
this.#disposed ||
|
|
146
|
+
(url.protocol !== "http:" && url.protocol !== "https:") ||
|
|
147
|
+
url.username ||
|
|
148
|
+
url.password
|
|
149
|
+
)
|
|
150
|
+
return undefined;
|
|
151
|
+
const grant = this.grants.http?.find((grant) =>
|
|
152
|
+
grant.origins.some((origin) => new URL(origin).origin === url.origin),
|
|
153
|
+
);
|
|
154
|
+
return grant && this.providers.http?.[grant.provider];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The socket provider and authority for the provider name an import binding selected. */
|
|
158
|
+
sockets(provider: string | undefined): SocketAccess | undefined {
|
|
159
|
+
if (provider === undefined) return undefined;
|
|
160
|
+
return {
|
|
161
|
+
provider: this.providers.network?.[provider],
|
|
162
|
+
grants: new SocketGrants(this.grants, provider),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Physical sockets are owned and closed by the revision's standard host. */
|
|
167
|
+
dispose(): void {
|
|
168
|
+
this.#disposed = true;
|
|
169
|
+
}
|
|
170
|
+
}
|