@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.
Files changed (241) hide show
  1. package/README.md +43 -17
  2. package/dist/authoring/types.d.ts +64 -3
  3. package/dist/authoring/wit.d.ts +6 -0
  4. package/dist/bridge/commands.d.ts +1190 -0
  5. package/dist/bridge/identifiers.d.ts +2 -0
  6. package/dist/bridge/kernel.d.ts +18 -107
  7. package/dist/bridge/protocol.d.ts +3 -138
  8. package/dist/browser.d.ts +14 -1
  9. package/dist/browser.js +141 -3332
  10. package/dist/chunks/browser-b8jamxw2.js +3399 -0
  11. package/dist/chunks/execution-nsqc6nrt.js +1419 -0
  12. package/dist/chunks/index-01zk0xqx.js +160 -0
  13. package/dist/chunks/index-3fjdfdfd.js +152 -0
  14. package/dist/chunks/index-aaymjn7v.js +787 -0
  15. package/dist/chunks/index-c1w7z9hx.js +356 -0
  16. package/dist/chunks/index-h8t0n42q.js +738 -0
  17. package/dist/chunks/index-hfds9mzp.js +308 -0
  18. package/dist/chunks/index-kr4x67wt.js +177 -0
  19. package/dist/chunks/index-kr6bnh34.js +117 -0
  20. package/dist/chunks/index-kyt9dj0a.js +1421 -0
  21. package/dist/chunks/index-n1wxzc9w.js +830 -0
  22. package/dist/chunks/index-pvac426d.js +74 -0
  23. package/dist/chunks/index-smyds5ce.js +3234 -0
  24. package/dist/chunks/index-sse8aapb.js +121 -0
  25. package/dist/chunks/index-v6heph77.js +643 -0
  26. package/dist/chunks/index-xrdvnv37.js +3448 -0
  27. package/dist/chunks/index-za4mg94e.js +26 -0
  28. package/dist/chunks/index-zkqmnjj8.js +167 -0
  29. package/dist/coordinator.js +9 -500
  30. package/dist/error.d.ts +2 -0
  31. package/dist/errors.d.ts +3 -0
  32. package/dist/guest.d.ts +173 -0
  33. package/dist/guest.js +26 -0
  34. package/dist/host.d.ts +36 -30
  35. package/dist/host.js +37 -3310
  36. package/dist/http/bytes.d.ts +8 -0
  37. package/dist/http/events.d.ts +7 -0
  38. package/dist/http.d.ts +45 -0
  39. package/dist/http.js +291 -0
  40. package/dist/index.d.ts +2 -0
  41. package/dist/index.js +49 -862
  42. package/dist/infrastructure/rpc/client.d.ts +2 -1
  43. package/dist/infrastructure/rpc/resources.d.ts +28 -0
  44. package/dist/interpreter/abi.d.ts +54 -0
  45. package/dist/interpreter/acquire.d.ts +18 -0
  46. package/dist/interpreter/browser-network.d.ts +52 -0
  47. package/dist/interpreter/browser-port.d.ts +55 -0
  48. package/dist/interpreter/browser.d.ts +79 -0
  49. package/dist/interpreter/cli.d.ts +5 -0
  50. package/dist/interpreter/codec.d.ts +42 -0
  51. package/dist/interpreter/component/execution.d.ts +42 -0
  52. package/dist/interpreter/component/host.d.ts +69 -0
  53. package/dist/interpreter/component/instance.d.ts +40 -0
  54. package/dist/interpreter/component/kernel.d.ts +120 -0
  55. package/dist/interpreter/component/names.d.ts +5 -0
  56. package/dist/interpreter/component/port.d.ts +85 -0
  57. package/dist/interpreter/component/prepared.d.ts +97 -0
  58. package/dist/interpreter/component/resources.d.ts +45 -0
  59. package/dist/interpreter/component/revision.d.ts +87 -0
  60. package/dist/interpreter/component/scope.d.ts +116 -0
  61. package/dist/interpreter/component/streams.d.ts +28 -0
  62. package/dist/interpreter/component/values.d.ts +79 -0
  63. package/dist/interpreter/component/wasi-filesystem.d.ts +21 -0
  64. package/dist/interpreter/component/wasi-host.d.ts +64 -0
  65. package/dist/interpreter/component/wasi-http.d.ts +33 -0
  66. package/dist/interpreter/component/wasi-sockets.d.ts +183 -0
  67. package/dist/interpreter/component/wasi.d.ts +41 -0
  68. package/dist/interpreter/core.d.ts +64 -0
  69. package/dist/interpreter/fetch-http-provider.d.ts +17 -0
  70. package/dist/interpreter/filesystem/contract.d.ts +85 -0
  71. package/dist/interpreter/filesystem/memory.d.ts +3 -0
  72. package/dist/interpreter/filesystem/opfs.d.ts +3 -0
  73. package/dist/interpreter/filesystem/scope.d.ts +35 -0
  74. package/dist/interpreter/filesystem.d.ts +8 -0
  75. package/dist/interpreter/gate.d.ts +6 -0
  76. package/dist/interpreter/host-protocol.d.ts +100 -0
  77. package/dist/interpreter/http-ingress-fetch.d.ts +7 -0
  78. package/dist/interpreter/http-ingress.d.ts +96 -0
  79. package/dist/interpreter/http-provider.d.ts +85 -0
  80. package/dist/interpreter/io.d.ts +20 -0
  81. package/dist/interpreter/mobile.d.ts +149 -0
  82. package/dist/interpreter/native-ingress.d.ts +95 -0
  83. package/dist/interpreter/node-port.d.ts +25 -0
  84. package/dist/interpreter/node.d.ts +36 -0
  85. package/dist/interpreter/revision.d.ts +64 -0
  86. package/dist/interpreter/scope.d.ts +89 -0
  87. package/dist/interpreter/socket-addresses.d.ts +50 -0
  88. package/dist/interpreter/socket-provider.d.ts +110 -0
  89. package/dist/interpreter/standard-cli.d.ts +34 -0
  90. package/dist/interpreter/standard-codec.d.ts +96 -0
  91. package/dist/interpreter/standard-filesystem.d.ts +47 -0
  92. package/dist/interpreter/standard-host.d.ts +71 -0
  93. package/dist/interpreter/standard-http-values.d.ts +159 -0
  94. package/dist/interpreter/standard-http.d.ts +9 -0
  95. package/dist/interpreter/standard-ingress.d.ts +63 -0
  96. package/dist/interpreter/standard-sockets.d.ts +177 -0
  97. package/dist/interpreter/standard-streams.d.ts +218 -0
  98. package/dist/interpreter/standard-system.d.ts +35 -0
  99. package/dist/interpreter/standard-utf8.d.ts +4 -0
  100. package/dist/interpreter/standard.d.ts +18 -0
  101. package/dist/interpreter/types.d.ts +152 -0
  102. package/dist/interpreter/wit-shape.d.ts +92 -0
  103. package/dist/manager.js +5 -332
  104. package/dist/msgpack.js +9 -615
  105. package/dist/node.d.ts +8 -1
  106. package/dist/node.js +248 -3321
  107. package/dist/rpc.js +196 -699
  108. package/dist/runtime/adapters.d.ts +120 -0
  109. package/dist/runtime/adapters.js +38 -0
  110. package/dist/runtime/calls.d.ts +7 -6
  111. package/dist/runtime/lifecycle.d.ts +3 -1
  112. package/dist/runtime/orbs.d.ts +5 -4
  113. package/dist/runtime/references.d.ts +27 -1
  114. package/dist/runtime/reload.d.ts +5 -3
  115. package/dist/runtime/scopes.d.ts +1 -1
  116. package/dist/runtime/signals.d.ts +4 -0
  117. package/dist/runtime/wit-client.d.ts +38 -0
  118. package/dist/telemetry.js +4 -25
  119. package/dist/tooling/build.d.ts +2 -2
  120. package/dist/tooling/client-resources.d.ts +23 -0
  121. package/dist/tooling/client.d.ts +10 -0
  122. package/dist/tooling/component-glue.d.ts +17 -0
  123. package/dist/tooling/component.d.ts +11 -0
  124. package/dist/tooling/index.d.ts +3 -1
  125. package/dist/tooling/index.js +1155 -102
  126. package/dist/tooling/resolver.d.ts +4 -4
  127. package/dist/tooling/typecheck.d.ts +2 -1
  128. package/dist/tooling/types.d.ts +27 -12
  129. package/dist/tooling/wit.d.ts +9 -0
  130. package/dist/wasi/wasi-clocks-types.d.ts +2 -0
  131. package/dist/wasi/wasi-http-client.d.ts +5 -0
  132. package/dist/wasi/wasi-http-types.d.ts +285 -0
  133. package/dist/wasi/wasi-sockets-ip-name-lookup.d.ts +23 -0
  134. package/dist/wasi/wasi-sockets-types.d.ts +146 -0
  135. package/package.json +62 -35
  136. package/src/authoring/declaration.ts +1 -1
  137. package/src/authoring/types.ts +53 -3
  138. package/src/authoring/wit.ts +15 -0
  139. package/src/bridge/commands.ts +1105 -0
  140. package/src/bridge/identifiers.ts +2 -0
  141. package/src/bridge/kernel.ts +56 -98
  142. package/src/bridge/protocol.ts +3 -142
  143. package/src/browser.ts +90 -6
  144. package/src/context-key.ts +4 -7
  145. package/src/error.ts +1 -1
  146. package/src/errors.ts +7 -0
  147. package/src/guest.ts +327 -0
  148. package/src/host.ts +128 -23
  149. package/src/http/bytes.ts +87 -0
  150. package/src/http/events.ts +34 -0
  151. package/src/http.ts +272 -0
  152. package/src/index.ts +3 -0
  153. package/src/infrastructure/rpc/client.ts +136 -17
  154. package/src/infrastructure/rpc/resources.ts +123 -0
  155. package/src/interpreter/abi.ts +69 -0
  156. package/src/interpreter/acquire.ts +58 -0
  157. package/src/interpreter/browser-network.ts +170 -0
  158. package/src/interpreter/browser-port.ts +425 -0
  159. package/src/interpreter/browser.ts +340 -0
  160. package/src/interpreter/cli.ts +30 -0
  161. package/src/interpreter/codec.ts +298 -0
  162. package/src/interpreter/component/execution.ts +264 -0
  163. package/src/interpreter/component/host.ts +409 -0
  164. package/src/interpreter/component/instance.ts +138 -0
  165. package/src/interpreter/component/kernel.ts +611 -0
  166. package/src/interpreter/component/names.ts +13 -0
  167. package/src/interpreter/component/port.ts +598 -0
  168. package/src/interpreter/component/prepared.ts +194 -0
  169. package/src/interpreter/component/resources.ts +345 -0
  170. package/src/interpreter/component/revision.ts +702 -0
  171. package/src/interpreter/component/scope.ts +447 -0
  172. package/src/interpreter/component/streams.ts +180 -0
  173. package/src/interpreter/component/values.ts +254 -0
  174. package/src/interpreter/component/wasi-filesystem.ts +472 -0
  175. package/src/interpreter/component/wasi-host.ts +197 -0
  176. package/src/interpreter/component/wasi-http.ts +939 -0
  177. package/src/interpreter/component/wasi-sockets.ts +958 -0
  178. package/src/interpreter/component/wasi.ts +238 -0
  179. package/src/interpreter/core.ts +386 -0
  180. package/src/interpreter/fetch-http-provider.ts +98 -0
  181. package/src/interpreter/filesystem/contract.ts +138 -0
  182. package/src/interpreter/filesystem/memory.ts +180 -0
  183. package/src/interpreter/filesystem/opfs.ts +294 -0
  184. package/src/interpreter/filesystem/scope.ts +390 -0
  185. package/src/interpreter/filesystem.ts +20 -0
  186. package/src/interpreter/gate.ts +55 -0
  187. package/src/interpreter/host-protocol.ts +242 -0
  188. package/src/interpreter/http-ingress-fetch.ts +133 -0
  189. package/src/interpreter/http-ingress.ts +203 -0
  190. package/src/interpreter/http-provider.ts +105 -0
  191. package/src/interpreter/io.ts +66 -0
  192. package/src/interpreter/mobile.ts +385 -0
  193. package/src/interpreter/native-ingress.ts +550 -0
  194. package/src/interpreter/node-port.ts +218 -0
  195. package/src/interpreter/node.ts +114 -0
  196. package/src/interpreter/revision.ts +450 -0
  197. package/src/interpreter/scope.ts +453 -0
  198. package/src/interpreter/socket-addresses.ts +246 -0
  199. package/src/interpreter/socket-provider.ts +126 -0
  200. package/src/interpreter/standard-cli.ts +184 -0
  201. package/src/interpreter/standard-codec.ts +403 -0
  202. package/src/interpreter/standard-filesystem.ts +545 -0
  203. package/src/interpreter/standard-host.ts +840 -0
  204. package/src/interpreter/standard-http-values.ts +624 -0
  205. package/src/interpreter/standard-http.ts +622 -0
  206. package/src/interpreter/standard-ingress.ts +344 -0
  207. package/src/interpreter/standard-sockets.ts +847 -0
  208. package/src/interpreter/standard-streams.ts +710 -0
  209. package/src/interpreter/standard-system.ts +182 -0
  210. package/src/interpreter/standard-utf8.ts +72 -0
  211. package/src/interpreter/standard.ts +283 -0
  212. package/src/interpreter/types.ts +163 -0
  213. package/src/interpreter/wit-shape.ts +175 -0
  214. package/src/node.ts +43 -14
  215. package/src/runtime/activities.ts +6 -6
  216. package/src/runtime/adapters.ts +314 -0
  217. package/src/runtime/calls.ts +213 -90
  218. package/src/runtime/context.ts +7 -7
  219. package/src/runtime/lifecycle.ts +36 -17
  220. package/src/runtime/orbs.ts +81 -39
  221. package/src/runtime/programs.ts +16 -8
  222. package/src/runtime/references.ts +128 -11
  223. package/src/runtime/reload.ts +71 -30
  224. package/src/runtime/resources.ts +2 -2
  225. package/src/runtime/scopes.ts +17 -17
  226. package/src/runtime/session.ts +3 -1
  227. package/src/runtime/signals.ts +31 -0
  228. package/src/runtime/wit-client.ts +228 -0
  229. package/src/tooling/build.ts +152 -54
  230. package/src/tooling/client-resources.ts +74 -0
  231. package/src/tooling/client.ts +531 -0
  232. package/src/tooling/component-glue.ts +288 -0
  233. package/src/tooling/component.ts +225 -0
  234. package/src/tooling/index.ts +3 -7
  235. package/src/tooling/resolver.ts +42 -25
  236. package/src/tooling/typecheck.ts +59 -4
  237. package/src/tooling/types.ts +27 -12
  238. package/src/tooling/wit.ts +188 -0
  239. package/dist/embedded.d.ts +0 -1
  240. package/dist/embedded.js +0 -1
  241. package/src/embedded.ts +0 -301
@@ -0,0 +1,847 @@
1
+ /**
2
+ * Standard `wasi:sockets` (TCP, UDP) and `wasi:sockets/ip-name-lookup` for the browser
3
+ * standard host, over an injected `SocketProvider` (`socket-provider.ts`). Browsers have no
4
+ * ambient sockets.
5
+ *
6
+ * Every call follows one order, so a refusal never reaches the provider:
7
+ * 1. argument and state checks, as pinned upstream `wasmtime-wasi` 51.0.0-rc.1;
8
+ * 2. the exact grant of the provider bound to that WIT import (`access-denied`);
9
+ * 3. provider capability (`not-supported`; name lookup `permanent-resolver-failure`);
10
+ * 4. the provider call.
11
+ *
12
+ * Ownership follows WASI's shared model: a socket handle and each stream derived from it hold
13
+ * one reference to the provider backend. Dropping the handle leaves its streams working; a
14
+ * dropped receive stream shuts down reads; the last reference closes the backend. Accepted
15
+ * sockets are independent. Every closing promise is physical work tracked by the host.
16
+ */
17
+ import type {
18
+ ErrorCode,
19
+ IpAddress,
20
+ IpAddressFamily,
21
+ IpSocketAddress,
22
+ } from "wasi:sockets/types@0.3.0";
23
+ import type { ErrorCode as LookupErrorCode } from "wasi:sockets/ip-name-lookup@0.3.0";
24
+ import type { SocketAccess } from "./browser-network.ts";
25
+ import {
26
+ type Address,
27
+ checkedIpAddress,
28
+ fromWit,
29
+ hasFamily,
30
+ isRemote,
31
+ isUnicast,
32
+ key,
33
+ parseHostName,
34
+ toWit,
35
+ unspecified,
36
+ } from "./socket-addresses.ts";
37
+ import {
38
+ NameLookupError,
39
+ type SocketOptions,
40
+ SocketProviderError,
41
+ type TcpBackend,
42
+ type TcpConnection,
43
+ type TcpListener,
44
+ type TcpOptions,
45
+ type UdpBackend,
46
+ type UdpOptions,
47
+ } from "./socket-provider.ts";
48
+ import { METHODS, TYPES } from "./standard.ts";
49
+ import { encode, type HandleCodec, Writer } from "./standard-codec.ts";
50
+ import {
51
+ abortable,
52
+ bindResource,
53
+ cancelled,
54
+ HostFuture,
55
+ type Implementation,
56
+ type InputStream,
57
+ OutputStream,
58
+ type StandardCall,
59
+ type StandardResource,
60
+ type StandardServices,
61
+ } from "./standard-streams.ts";
62
+
63
+ const TYPES_INTERFACE = "wasi:sockets/types@0.3.0";
64
+ const LOOKUP_INTERFACE = "wasi:sockets/ip-name-lookup@0.3.0";
65
+ /** Upstream `MAX_DATAGRAM_SIZE`. */
66
+ const MAX_DATAGRAM = 0xffff;
67
+ /** Upstream listen backlog default and clamp. */
68
+ const DEFAULT_BACKLOG = 128;
69
+ const MAX_BACKLOG = 0x7fffffff;
70
+
71
+ type Result<T, E> = { tag: "ok"; val: T } | { tag: "err"; val: E };
72
+ const ok = <T>(val: T): { tag: "ok"; val: T } => ({ tag: "ok", val });
73
+ const fail = (tag: ErrorCode["tag"]): { tag: "err"; val: ErrorCode } => ({
74
+ tag: "err",
75
+ val: { tag } as ErrorCode,
76
+ });
77
+
78
+ /** The providers bound to this revision's socket and lookup imports. */
79
+ export interface SocketBindings {
80
+ readonly types?: SocketAccess | undefined;
81
+ readonly lookup?: SocketAccess | undefined;
82
+ }
83
+
84
+ // ---------------------------------------------------------------------------------------------
85
+ // errors
86
+
87
+ /** The generated error type of a function's `result<_, E>`, when the table binds it. */
88
+ function errorType(contract: string, name: string): number | undefined {
89
+ const method = METHODS.find((m) => m.interface === contract && m.name === name);
90
+ const result = method?.result === undefined ? undefined : TYPES[method.result];
91
+ return result?.[0] === "result" ? (result[2] as number) : undefined;
92
+ }
93
+ const NO_HANDLES: HandleCodec = {
94
+ handle() {
95
+ throw new Error("an error code carries no handles");
96
+ },
97
+ value() {
98
+ throw new Error("an error code carries no handles");
99
+ },
100
+ };
101
+
102
+ /** A provider code only when it encodes as the generated type; otherwise `other(message)`. */
103
+ function code<C extends { tag: string }>(
104
+ error: unknown,
105
+ type: number | undefined,
106
+ typed: unknown,
107
+ ): C {
108
+ if (typed !== undefined && type !== undefined) {
109
+ try {
110
+ encode(new Writer(), type, typed, NO_HANDLES);
111
+ return typed as C;
112
+ } catch {
113
+ /* Not a standard code: reported as `other` below. */
114
+ }
115
+ }
116
+ return {
117
+ tag: "other",
118
+ val: error instanceof Error ? error.message : String(error),
119
+ } as unknown as C;
120
+ }
121
+
122
+ const socketCode = (error: unknown): ErrorCode =>
123
+ code(
124
+ error,
125
+ errorType(TYPES_INTERFACE, "connect"),
126
+ error instanceof SocketProviderError ? error.code : undefined,
127
+ );
128
+ const lookupCode = (error: unknown): LookupErrorCode =>
129
+ code(
130
+ error,
131
+ errorType(LOOKUP_INTERFACE, "resolveAddresses"),
132
+ error instanceof NameLookupError ? error.code : undefined,
133
+ );
134
+
135
+ /** Run a synchronous provider call, reporting its failure as a standard result. */
136
+ function attempt<T>(operation: () => T): Result<T, ErrorCode> {
137
+ try {
138
+ return ok(operation());
139
+ } catch (error) {
140
+ return { tag: "err", val: socketCode(error) };
141
+ }
142
+ }
143
+
144
+ /** A provider-returned address as a standard result; malformed is the provider's failure. */
145
+ function localAddress(value: IpSocketAddress | undefined): Result<IpSocketAddress, ErrorCode> {
146
+ if (value === undefined) return fail("invalid-state");
147
+ const address = fromWit(value);
148
+ return address
149
+ ? ok(toWit(address))
150
+ : { tag: "err", val: { tag: "other", val: "invalid provider address" } };
151
+ }
152
+
153
+ /** A provider-returned address, validated; a malformed one is the provider's failure. */
154
+ function provided(value: unknown): Address {
155
+ const address = fromWit(value);
156
+ if (!address) throw new Error("invalid provider address");
157
+ return address;
158
+ }
159
+
160
+ // ---------------------------------------------------------------------------------------------
161
+ // shared physical ownership
162
+
163
+ /** One provider backend shared by a socket handle and its streams; the last user closes it. */
164
+ class Shared<B extends { close(): Promise<void> }> {
165
+ #users = 1;
166
+
167
+ constructor(
168
+ readonly host: StandardServices,
169
+ readonly backend: B,
170
+ ) {}
171
+
172
+ retain(): void {
173
+ this.#users++;
174
+ }
175
+
176
+ release(): void {
177
+ if (--this.#users === 0) this.host.cleanup(() => this.backend.close());
178
+ }
179
+ }
180
+
181
+ /** Handle-table reference counting, as for every standard resource. */
182
+ abstract class SocketResource implements StandardResource {
183
+ borrows = 0;
184
+ #users = 1;
185
+
186
+ retain(): void {
187
+ this.#users++;
188
+ }
189
+
190
+ release(): void {
191
+ if (--this.#users === 0) this.close();
192
+ }
193
+
194
+ protected abstract close(): void;
195
+ }
196
+
197
+ /** Independent option access: an absent getter or setter is `not-supported`, not invented. */
198
+ function getOption<O, K extends keyof O>(
199
+ backend: SocketOptions<O>,
200
+ name: K,
201
+ valid: (value: unknown) => boolean,
202
+ ): Result<O[K], ErrorCode> {
203
+ if (!backend.getOption) return fail("not-supported");
204
+ const value = attempt(() => backend.getOption!(name));
205
+ if (value.tag === "ok" && !valid(value.val))
206
+ return { tag: "err", val: { tag: "other", val: `invalid provider ${String(name)}` } };
207
+ return value;
208
+ }
209
+
210
+ function setOption<O, K extends keyof O>(
211
+ backend: SocketOptions<O>,
212
+ name: K,
213
+ value: O[K],
214
+ ): Result<void, ErrorCode> {
215
+ if (!backend.setOption) return fail("not-supported");
216
+ return attempt(() => backend.setOption!(name, value));
217
+ }
218
+
219
+ const isU8 = (v: unknown) => typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= 0xff;
220
+ const isU32 = (v: unknown) =>
221
+ typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= 0xffffffff;
222
+ const isU64 = (v: unknown) => typeof v === "bigint" && v > 0n && v < 1n << 64n;
223
+ const isBool = (v: unknown) => typeof v === "boolean";
224
+
225
+ // ---------------------------------------------------------------------------------------------
226
+ // TCP
227
+
228
+ type TcpState =
229
+ | { kind: "default" }
230
+ | { kind: "connecting" }
231
+ | { kind: "connected"; connection: TcpConnection }
232
+ | { kind: "listening"; listener: TcpListener }
233
+ | { kind: "closed"; code: ErrorCode };
234
+
235
+ /** Close a connection nobody will receive; each promise is its physical acknowledgement. */
236
+ function discardConnection(host: StandardServices, connection: TcpConnection): void {
237
+ host.cleanup(() => connection.readable.cancel());
238
+ host.cleanup(() => connection.writable.abort());
239
+ }
240
+
241
+ /** Validate a provider connection; the host never trusts its shape. */
242
+ function checkedConnection(value: unknown): TcpConnection {
243
+ const connection = value as TcpConnection;
244
+ if (
245
+ !(connection?.readable instanceof ReadableStream) ||
246
+ !(connection.writable instanceof WritableStream)
247
+ )
248
+ throw new Error("invalid provider connection");
249
+ provided(connection.remote);
250
+ return connection;
251
+ }
252
+
253
+ /** `wasi:sockets/types#tcp-socket`. */
254
+ export class TcpSocket extends SocketResource {
255
+ #state: TcpState;
256
+ #bound: boolean;
257
+ #backlog = DEFAULT_BACKLOG;
258
+ #sent = false;
259
+ #received = false;
260
+
261
+ constructor(
262
+ readonly access: SocketAccess,
263
+ readonly family: IpAddressFamily,
264
+ readonly shared: Shared<TcpBackend>,
265
+ connection?: TcpConnection,
266
+ ) {
267
+ super();
268
+ this.#state = connection ? { kind: "connected", connection } : { kind: "default" };
269
+ this.#bound = connection !== undefined;
270
+ }
271
+
272
+ protected close(): void {
273
+ this.shared.release();
274
+ }
275
+
276
+ get #backend(): TcpBackend {
277
+ return this.shared.backend;
278
+ }
279
+
280
+ get #grants() {
281
+ return this.access.grants;
282
+ }
283
+
284
+ bind(local: IpSocketAddress): Result<void, ErrorCode> {
285
+ if (this.#bound || this.#state.kind !== "default") return fail("invalid-state");
286
+ const address = fromWit(local)!;
287
+ if (!isUnicast(address) || !hasFamily(address, this.family)) return fail("invalid-argument");
288
+ if (!this.#grants.allows(address, "tcp-bind")) return fail("access-denied");
289
+ const backend = this.#backend;
290
+ if (!backend.bind) return fail("not-supported");
291
+ const bound = attempt(() => backend.bind!(toWit(address)));
292
+ if (bound.tag === "ok") this.#bound = true;
293
+ return bound;
294
+ }
295
+
296
+ async connect(remote: IpSocketAddress, call: StandardCall): Promise<Result<void, ErrorCode>> {
297
+ if (this.#state.kind !== "default") return fail("invalid-state");
298
+ const address = fromWit(remote)!;
299
+ if (!isUnicast(address) || !isRemote(address) || !hasFamily(address, this.family))
300
+ return fail("invalid-argument");
301
+ if (!this.#bound && !this.#grants.allows(unspecified(this.family), "tcp-bind"))
302
+ return fail("access-denied");
303
+ if (!this.#grants.allows(address, "tcp-connect")) return fail("access-denied");
304
+ this.#state = { kind: "connecting" };
305
+ const abort = new AbortController();
306
+ const stop = () => abort.abort();
307
+ call.signal?.addEventListener("abort", stop, { once: true });
308
+ const pending = call.host.physical(call.signal, () =>
309
+ this.#backend.connect(toWit(address), abort.signal),
310
+ );
311
+ try {
312
+ const connection = checkedConnection(await abortable(pending, call.signal));
313
+ this.#state = { kind: "connected", connection };
314
+ this.#bound = true;
315
+ return ok(undefined);
316
+ } catch (error) {
317
+ this.#state = { kind: "closed", code: { tag: "invalid-state" } };
318
+ if (!call.signal?.aborted) {
319
+ const failure = socketCode(error);
320
+ this.#state = { kind: "closed", code: failure };
321
+ return { tag: "err", val: failure };
322
+ }
323
+ // A connection that arrives after cancellation is closed and never published.
324
+ pending.then(
325
+ (late) => discardConnection(call.host, late),
326
+ () => {},
327
+ );
328
+ throw cancelled();
329
+ } finally {
330
+ call.signal?.removeEventListener("abort", stop);
331
+ }
332
+ }
333
+
334
+ listen(call: StandardCall): Result<OutputStream, ErrorCode> {
335
+ if (this.#state.kind !== "default") return fail("invalid-state");
336
+ const implicit = unspecified(this.family);
337
+ if (this.#bound) {
338
+ const local = localAddress(this.#backend.local);
339
+ if (local.tag === "err") return local;
340
+ if (!this.#grants.allows(fromWit(local.val)!, "tcp-listen")) return fail("access-denied");
341
+ } else if (
342
+ !this.#grants.allows(implicit, "tcp-bind") ||
343
+ !this.#grants.allows(implicit, "tcp-listen")
344
+ )
345
+ return fail("access-denied");
346
+ const backend = this.#backend;
347
+ if (!backend.listen) return fail("not-supported");
348
+ const listened = attempt(() => backend.listen!(this.#backlog));
349
+ if (listened.tag === "err") return listened;
350
+ const listener = listened.val;
351
+ if (typeof listener?.accept !== "function")
352
+ return { tag: "err", val: { tag: "other", val: "invalid provider listener" } };
353
+ this.#state = { kind: "listening", listener };
354
+ this.#bound = true;
355
+ return ok(this.#acceptStream(call.host, listener));
356
+ }
357
+
358
+ /**
359
+ * The listen stream holds its own backend reference and one revision-owned accept pull at a
360
+ * time. Dropping it aborts that pull, waits for its settlement (the physical acknowledgement),
361
+ * then releases the reference; the socket handle's reference is independent.
362
+ */
363
+ #acceptStream(host: StandardServices, listener: TcpListener): OutputStream {
364
+ this.shared.retain();
365
+ const stop = new AbortController();
366
+ let accepting: Promise<unknown> | undefined;
367
+ const next = async (): Promise<TcpSocket> => {
368
+ for (;;) {
369
+ const pull = listener.accept(stop.signal);
370
+ accepting = pull;
371
+ const { socket, connection } = await pull;
372
+ let checked: TcpConnection;
373
+ try {
374
+ checked = checkedConnection(connection);
375
+ if (typeof socket?.close !== "function") throw new Error("invalid provider socket");
376
+ } catch (error) {
377
+ host.cleanup(() => socket?.close?.());
378
+ throw error;
379
+ }
380
+ const child = new TcpSocket(this.access, this.family, new Shared(host, socket), checked);
381
+ // Native accepts any peer once listening on a granted address (`tcp-accept`).
382
+ if (this.#grants.allows(provided(checked.remote), "tcp-accept")) return child;
383
+ child.release();
384
+ }
385
+ };
386
+ return new OutputStream(
387
+ {
388
+ next,
389
+ close: () => {
390
+ stop.abort();
391
+ const pending = accepting;
392
+ host.cleanup(async () => {
393
+ await pending?.then(
394
+ () => {},
395
+ () => {},
396
+ );
397
+ this.shared.release();
398
+ });
399
+ },
400
+ },
401
+ () => {},
402
+ (value) => (value as StandardResource).release(),
403
+ );
404
+ }
405
+
406
+ send(data: InputStream, call: StandardCall): HostFuture {
407
+ if (this.#state.kind !== "connected" || this.#sent) {
408
+ data.cancel();
409
+ return HostFuture.of(fail("invalid-state"));
410
+ }
411
+ const connection = this.#state.connection;
412
+ const locked = attempt(() => connection.writable.getWriter());
413
+ if (locked.tag === "err") {
414
+ data.cancel();
415
+ return HostFuture.of(locked);
416
+ }
417
+ this.#sent = true;
418
+ const host = call.host;
419
+ const writer = locked.val;
420
+ this.shared.retain();
421
+ const value = (async (): Promise<Result<void, ErrorCode>> => {
422
+ try {
423
+ for (let chunk = await data.read(); chunk; chunk = await data.read())
424
+ await writer.write(chunk);
425
+ if (data.cancelled) throw new Error("send stopped");
426
+ // A clean end of the guest stream is a write shutdown, acknowledged by its promise.
427
+ const closing = writer.close();
428
+ host.cleanup(() => closing);
429
+ await closing;
430
+ return ok(undefined);
431
+ } catch (error) {
432
+ data.cancel();
433
+ const aborting = writer.abort(error);
434
+ host.cleanup(() => aborting);
435
+ return { tag: "err", val: socketCode(error) };
436
+ } finally {
437
+ this.shared.release();
438
+ }
439
+ })();
440
+ return new HostFuture(value);
441
+ }
442
+
443
+ receive(call: StandardCall): [OutputStream, HostFuture] {
444
+ const empty = { next: async () => undefined, close() {} };
445
+ if (this.#state.kind !== "connected" || this.#received)
446
+ return [new OutputStream(empty, () => {}), HostFuture.of(fail("invalid-state"))];
447
+ const connection = this.#state.connection;
448
+ const locked = attempt(() => connection.readable.getReader());
449
+ if (locked.tag === "err") return [new OutputStream(empty, () => {}), HostFuture.of(locked)];
450
+ this.#received = true;
451
+ const host = call.host;
452
+ const reader = locked.val;
453
+ this.shared.retain();
454
+ let settle!: (result: Result<void, ErrorCode>) => void;
455
+ const done = new Promise<Result<void, ErrorCode>>((resolve) => {
456
+ settle = resolve;
457
+ });
458
+ const stream = new OutputStream(
459
+ {
460
+ async next() {
461
+ for (;;) {
462
+ const { done: end, value } = await reader.read();
463
+ if (end) return undefined;
464
+ if (!(value instanceof Uint8Array)) throw new Error("invalid provider chunk");
465
+ // Empty chunks are legal and are not the end of the stream.
466
+ if (value.byteLength) return value.slice();
467
+ }
468
+ },
469
+ close: (end) => {
470
+ host.cleanup(async () => {
471
+ // A drop or failure shuts down reads; its promise is the acknowledgement.
472
+ if (end !== "eof") await reader.cancel();
473
+ this.shared.release();
474
+ });
475
+ },
476
+ },
477
+ // Upstream: end of stream and an early drop are both `ok`; a failure is its code.
478
+ (failure, end) =>
479
+ settle(end === "failed" ? { tag: "err", val: socketCode(failure) } : ok(undefined)),
480
+ );
481
+ return [stream, new HostFuture(done)];
482
+ }
483
+
484
+ getLocalAddress(): Result<IpSocketAddress, ErrorCode> {
485
+ if (this.#state.kind === "closed") return { tag: "err", val: this.#state.code };
486
+ if (!this.#bound || this.#state.kind === "connecting") return fail("invalid-state");
487
+ return localAddress(this.#backend.local);
488
+ }
489
+
490
+ getRemoteAddress(): Result<IpSocketAddress, ErrorCode> {
491
+ if (this.#state.kind === "closed") return { tag: "err", val: this.#state.code };
492
+ if (this.#state.kind !== "connected") return fail("invalid-state");
493
+ return ok(toWit(provided(this.#state.connection.remote)));
494
+ }
495
+
496
+ getIsListening(): boolean {
497
+ return this.#state.kind === "listening";
498
+ }
499
+
500
+ getAddressFamily(): IpAddressFamily {
501
+ return this.family;
502
+ }
503
+
504
+ setListenBacklogSize(value: bigint): Result<void, ErrorCode> {
505
+ if (value === 0n) return fail("invalid-argument");
506
+ const backlog = value > BigInt(MAX_BACKLOG) ? MAX_BACKLOG : Number(value);
507
+ switch (this.#state.kind) {
508
+ case "default":
509
+ this.#backlog = backlog;
510
+ return ok(undefined);
511
+ case "listening":
512
+ return setOption<TcpOptions, "listenBacklogSize">(
513
+ this.#backend,
514
+ "listenBacklogSize",
515
+ BigInt(backlog),
516
+ );
517
+ case "closed":
518
+ return { tag: "err", val: this.#state.code };
519
+ default:
520
+ return fail("invalid-state");
521
+ }
522
+ }
523
+
524
+ #get<K extends keyof TcpOptions>(name: K, valid: (v: unknown) => boolean) {
525
+ if (this.#state.kind === "closed") return { tag: "err" as const, val: this.#state.code };
526
+ return getOption<TcpOptions, K>(this.#backend, name, valid);
527
+ }
528
+
529
+ #set<K extends keyof TcpOptions>(name: K, value: TcpOptions[K], positive: boolean) {
530
+ if (positive && (value === 0 || value === 0n)) return fail("invalid-argument");
531
+ if (this.#state.kind === "closed") return { tag: "err" as const, val: this.#state.code };
532
+ return setOption<TcpOptions, K>(this.#backend, name, value);
533
+ }
534
+
535
+ getKeepAliveEnabled() {
536
+ return this.#get("keepAliveEnabled", isBool);
537
+ }
538
+ setKeepAliveEnabled(value: boolean) {
539
+ return this.#set("keepAliveEnabled", value, false);
540
+ }
541
+ getKeepAliveIdleTime() {
542
+ return this.#get("keepAliveIdleTime", isU64);
543
+ }
544
+ setKeepAliveIdleTime(value: bigint) {
545
+ return this.#set("keepAliveIdleTime", value, true);
546
+ }
547
+ getKeepAliveInterval() {
548
+ return this.#get("keepAliveInterval", isU64);
549
+ }
550
+ setKeepAliveInterval(value: bigint) {
551
+ return this.#set("keepAliveInterval", value, true);
552
+ }
553
+ getKeepAliveCount() {
554
+ return this.#get("keepAliveCount", isU32);
555
+ }
556
+ setKeepAliveCount(value: number) {
557
+ return this.#set("keepAliveCount", value, true);
558
+ }
559
+ getHopLimit() {
560
+ return this.#get("hopLimit", isU8);
561
+ }
562
+ setHopLimit(value: number) {
563
+ return this.#set("hopLimit", value, true);
564
+ }
565
+ getReceiveBufferSize() {
566
+ return this.#get("receiveBufferSize", isU64);
567
+ }
568
+ setReceiveBufferSize(value: bigint) {
569
+ return this.#set("receiveBufferSize", value, true);
570
+ }
571
+ getSendBufferSize() {
572
+ return this.#get("sendBufferSize", isU64);
573
+ }
574
+ setSendBufferSize(value: bigint) {
575
+ return this.#set("sendBufferSize", value, true);
576
+ }
577
+ }
578
+
579
+ // ---------------------------------------------------------------------------------------------
580
+ // UDP
581
+
582
+ /** `wasi:sockets/types#udp-socket`. Peer association and receive filtering are host state. */
583
+ export class UdpSocket extends SocketResource {
584
+ #bound = false;
585
+ #remote: Address | undefined;
586
+
587
+ constructor(
588
+ readonly access: SocketAccess,
589
+ readonly family: IpAddressFamily,
590
+ readonly shared: Shared<UdpBackend>,
591
+ ) {
592
+ super();
593
+ }
594
+
595
+ protected close(): void {
596
+ this.shared.release();
597
+ }
598
+
599
+ get #backend(): UdpBackend {
600
+ return this.shared.backend;
601
+ }
602
+
603
+ get #grants() {
604
+ return this.access.grants;
605
+ }
606
+
607
+ bind(local: IpSocketAddress): Result<void, ErrorCode> {
608
+ if (this.#bound) return fail("invalid-state");
609
+ const address = fromWit(local)!;
610
+ if (!hasFamily(address, this.family)) return fail("invalid-argument");
611
+ if (!this.#grants.allows(address, "udp-bind")) return fail("access-denied");
612
+ return this.#bind(address);
613
+ }
614
+
615
+ #bind(address: Address): Result<void, ErrorCode> {
616
+ const backend = this.#backend;
617
+ if (!backend.bind) return fail("not-supported");
618
+ const bound = attempt(() => backend.bind!(toWit(address)));
619
+ if (bound.tag === "ok") this.#bound = true;
620
+ return bound;
621
+ }
622
+
623
+ connect(remote: IpSocketAddress): Result<void, ErrorCode> {
624
+ const address = fromWit(remote)!;
625
+ if (!hasFamily(address, this.family) || !isRemote(address)) return fail("invalid-argument");
626
+ const implicit = unspecified(this.family);
627
+ if (!this.#bound && !this.#grants.allows(implicit, "udp-bind")) return fail("access-denied");
628
+ if (!this.#grants.allows(address, "udp-send") || !this.#grants.allows(address, "udp-receive"))
629
+ return fail("access-denied");
630
+ if (!this.#bound) {
631
+ const bound = this.#bind(implicit);
632
+ if (bound.tag === "err") return bound;
633
+ }
634
+ this.#remote = address;
635
+ return ok(undefined);
636
+ }
637
+
638
+ disconnect(): Result<void, ErrorCode> {
639
+ if (!this.#remote) return fail("invalid-state");
640
+ this.#remote = undefined;
641
+ return ok(undefined);
642
+ }
643
+
644
+ async send(
645
+ data: Uint8Array,
646
+ remote: IpSocketAddress | undefined,
647
+ call: StandardCall,
648
+ ): Promise<Result<void, ErrorCode>> {
649
+ if (data.byteLength > MAX_DATAGRAM) return fail("datagram-too-large");
650
+ let target: Address;
651
+ if (remote !== undefined) {
652
+ target = fromWit(remote)!;
653
+ if (!isRemote(target) || !hasFamily(target, this.family)) return fail("invalid-argument");
654
+ if (this.#remote && key(this.#remote) !== key(target)) return fail("invalid-argument");
655
+ } else if (this.#remote) target = this.#remote;
656
+ else return fail("invalid-argument");
657
+ if (!this.#bound && !this.#grants.allows(unspecified(this.family), "udp-bind"))
658
+ return fail("access-denied");
659
+ if (!this.#grants.allows(target, "udp-send")) return fail("access-denied");
660
+ const abort = new AbortController();
661
+ const stop = () => abort.abort();
662
+ call.signal?.addEventListener("abort", stop, { once: true });
663
+ try {
664
+ await abortable(
665
+ call.host.physical(call.signal, () =>
666
+ this.#backend.send(data.slice(), toWit(target), abort.signal),
667
+ ),
668
+ call.signal,
669
+ );
670
+ this.#bound = true;
671
+ return ok(undefined);
672
+ } catch (error) {
673
+ if (call.signal?.aborted) throw cancelled();
674
+ return { tag: "err", val: socketCode(error) };
675
+ } finally {
676
+ call.signal?.removeEventListener("abort", stop);
677
+ }
678
+ }
679
+
680
+ async receive(call: StandardCall): Promise<Result<[Uint8Array, IpSocketAddress], ErrorCode>> {
681
+ if (!this.#bound) return fail("invalid-state");
682
+ const abort = new AbortController();
683
+ const stop = () => abort.abort();
684
+ call.signal?.addEventListener("abort", stop, { once: true });
685
+ try {
686
+ for (;;) {
687
+ // A datagram arriving after cancellation is dropped, like network loss.
688
+ const datagram = await abortable(
689
+ call.host.physical(call.signal, () => this.#backend.receive(abort.signal)),
690
+ call.signal,
691
+ );
692
+ const sender = provided(datagram?.remote);
693
+ if (!(datagram.data instanceof Uint8Array) || datagram.data.byteLength > MAX_DATAGRAM)
694
+ throw new Error("invalid provider datagram");
695
+ // Datagrams from ungranted or unconnected peers are dropped, never delivered.
696
+ if (this.#remote && key(this.#remote) !== key(sender)) continue;
697
+ if (!this.#grants.allows(sender, "udp-receive")) continue;
698
+ return ok([datagram.data.slice(), toWit(sender)] as [Uint8Array, IpSocketAddress]);
699
+ }
700
+ } catch (error) {
701
+ if (call.signal?.aborted) throw cancelled();
702
+ return { tag: "err", val: socketCode(error) };
703
+ } finally {
704
+ call.signal?.removeEventListener("abort", stop);
705
+ }
706
+ }
707
+
708
+ getLocalAddress(): Result<IpSocketAddress, ErrorCode> {
709
+ return this.#bound ? localAddress(this.#backend.local) : fail("invalid-state");
710
+ }
711
+
712
+ getRemoteAddress(): Result<IpSocketAddress, ErrorCode> {
713
+ return this.#remote ? ok(toWit(this.#remote)) : fail("invalid-state");
714
+ }
715
+
716
+ getAddressFamily(): IpAddressFamily {
717
+ return this.family;
718
+ }
719
+
720
+ #set<K extends keyof UdpOptions>(name: K, value: UdpOptions[K]) {
721
+ if (value === 0 || value === 0n) return fail("invalid-argument");
722
+ return setOption<UdpOptions, K>(this.#backend, name, value);
723
+ }
724
+
725
+ getUnicastHopLimit() {
726
+ return getOption<UdpOptions, "unicastHopLimit">(this.#backend, "unicastHopLimit", isU8);
727
+ }
728
+ setUnicastHopLimit(value: number) {
729
+ return this.#set("unicastHopLimit", value);
730
+ }
731
+ getReceiveBufferSize() {
732
+ return getOption<UdpOptions, "receiveBufferSize">(this.#backend, "receiveBufferSize", isU64);
733
+ }
734
+ setReceiveBufferSize(value: bigint) {
735
+ return this.#set("receiveBufferSize", value);
736
+ }
737
+ getSendBufferSize() {
738
+ return getOption<UdpOptions, "sendBufferSize">(this.#backend, "sendBufferSize", isU64);
739
+ }
740
+ setSendBufferSize(value: bigint) {
741
+ return this.#set("sendBufferSize", value);
742
+ }
743
+ }
744
+
745
+ // ---------------------------------------------------------------------------------------------
746
+ // name lookup and binding
747
+
748
+ /** `resolve-addresses`: grant first (upstream order), then `url::Host::parse`, then provider. */
749
+ async function resolveAddresses(
750
+ access: SocketAccess | undefined,
751
+ name: string,
752
+ call: StandardCall,
753
+ ): Promise<Result<IpAddress[], LookupErrorCode>> {
754
+ const failure = (tag: LookupErrorCode["tag"]) => ({
755
+ tag: "err" as const,
756
+ val: { tag } as LookupErrorCode,
757
+ });
758
+ if (!access?.grants.resolve) return failure("access-denied");
759
+ const parsed = parseHostName(name);
760
+ if (!parsed) return failure("invalid-argument");
761
+ if ("ip" in parsed) return ok([parsed.ip]);
762
+ const resolve = access.provider?.resolve;
763
+ if (!resolve) return failure("permanent-resolver-failure");
764
+ const abort = new AbortController();
765
+ const stop = () => abort.abort();
766
+ call.signal?.addEventListener("abort", stop, { once: true });
767
+ try {
768
+ const found = await abortable(
769
+ call.host.physical(call.signal, () =>
770
+ resolve.call(access.provider, parsed.domain, abort.signal),
771
+ ),
772
+ call.signal,
773
+ );
774
+ if (!Array.isArray(found)) throw new Error("invalid provider result");
775
+ const addresses = found.map(checkedIpAddress);
776
+ if (addresses.some((address) => address === undefined))
777
+ throw new Error("invalid provider address");
778
+ // The standard never succeeds with no addresses.
779
+ if (!addresses.length) return failure("name-unresolvable");
780
+ return ok(addresses as IpAddress[]);
781
+ } catch (error) {
782
+ if (call.signal?.aborted) throw cancelled();
783
+ return { tag: "err", val: lookupCode(error) };
784
+ } finally {
785
+ call.signal?.removeEventListener("abort", stop);
786
+ }
787
+ }
788
+
789
+ /** `create` for one protocol: grant, then capability, then the provider's synchronous create. */
790
+ function create<B extends { close(): Promise<void> }, S>(
791
+ access: SocketAccess | undefined,
792
+ allowed: boolean,
793
+ factory: { create(family: IpAddressFamily): B } | undefined,
794
+ family: IpAddressFamily,
795
+ host: StandardServices,
796
+ make: (access: SocketAccess, shared: Shared<B>) => S,
797
+ ): Result<S, ErrorCode> {
798
+ if (!access || !allowed) return fail("access-denied");
799
+ if (!factory) return fail("not-supported");
800
+ const created = attempt(() => factory.create(family));
801
+ if (created.tag === "err") return created;
802
+ if (typeof created.val?.close !== "function")
803
+ return { tag: "err", val: { tag: "other", val: "invalid provider socket" } };
804
+ return ok(make(access, new Shared(host, created.val)));
805
+ }
806
+
807
+ /** The constructors and free function behind the generated binding, keyed by WIT role. */
808
+ export function socketStatics(bindings: SocketBindings) {
809
+ const types = bindings.types;
810
+ return {
811
+ tcpCreate: (call: StandardCall, family: IpAddressFamily) =>
812
+ create(
813
+ types,
814
+ types?.grants.tcp ?? false,
815
+ types?.provider?.tcp,
816
+ family,
817
+ call.host,
818
+ (access, shared) => new TcpSocket(access, family, shared),
819
+ ),
820
+ udpCreate: (call: StandardCall, family: IpAddressFamily) =>
821
+ create(
822
+ types,
823
+ types?.grants.udp ?? false,
824
+ types?.provider?.udp,
825
+ family,
826
+ call.host,
827
+ (access, shared) => new UdpSocket(access, family, shared),
828
+ ),
829
+ resolveAddresses: (call: StandardCall, name: string) =>
830
+ resolveAddresses(bindings.lookup, name, call),
831
+ };
832
+ }
833
+
834
+ /** Every generated `wasi:sockets` function, keyed for the standard host. */
835
+ export function socketMethods(bindings: SocketBindings): Record<string, Implementation> {
836
+ const statics = socketStatics(bindings);
837
+ return {
838
+ ...bindResource(TYPES_INTERFACE, "tcp-socket", TcpSocket.prototype, {
839
+ create: statics.tcpCreate,
840
+ }),
841
+ ...bindResource(TYPES_INTERFACE, "udp-socket", UdpSocket.prototype, {
842
+ create: statics.udpCreate,
843
+ }),
844
+ [`${LOOKUP_INTERFACE}#resolveAddresses`]: (call, [name]: [string]) =>
845
+ statics.resolveAddresses(call, name),
846
+ };
847
+ }