@bytecodealliance/preview3-shim 0.1.1 → 0.2.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 (112) hide show
  1. package/dist/nodejs/cli.d.ts +51 -0
  2. package/dist/nodejs/cli.js +106 -0
  3. package/dist/nodejs/clocks.d.ts +55 -0
  4. package/dist/nodejs/clocks.js +83 -0
  5. package/dist/nodejs/filesystem/descriptor.d.ts +443 -0
  6. package/dist/nodejs/filesystem/descriptor.js +1010 -0
  7. package/dist/nodejs/filesystem/error.d.ts +61 -0
  8. package/dist/nodejs/filesystem/error.js +86 -0
  9. package/dist/nodejs/filesystem/utils.d.ts +8 -0
  10. package/dist/nodejs/filesystem/utils.js +25 -0
  11. package/dist/nodejs/filesystem.js +2 -0
  12. package/dist/nodejs/finalization.d.ts +2 -0
  13. package/{lib → dist}/nodejs/finalization.js +15 -21
  14. package/dist/nodejs/future.d.ts +98 -0
  15. package/dist/nodejs/future.js +176 -0
  16. package/dist/nodejs/http/client.d.ts +22 -0
  17. package/dist/nodejs/http/client.js +138 -0
  18. package/dist/nodejs/http/error.d.ts +18 -0
  19. package/dist/nodejs/http/error.js +62 -0
  20. package/dist/nodejs/http/fields.d.ts +144 -0
  21. package/dist/nodejs/http/fields.js +327 -0
  22. package/dist/nodejs/http/request.d.ts +256 -0
  23. package/dist/nodejs/http/request.js +606 -0
  24. package/dist/nodejs/http/response.d.ts +82 -0
  25. package/dist/nodejs/http/response.js +202 -0
  26. package/dist/nodejs/http/server.d.ts +13 -0
  27. package/dist/nodejs/http/server.js +127 -0
  28. package/dist/nodejs/http.d.ts +19 -0
  29. package/{lib → dist}/nodejs/http.js +13 -18
  30. package/dist/nodejs/index.js +8 -0
  31. package/dist/nodejs/random.d.ts +5 -0
  32. package/{lib → dist}/nodejs/random.js +2 -8
  33. package/dist/nodejs/sockets/address.d.ts +78 -0
  34. package/dist/nodejs/sockets/address.js +120 -0
  35. package/dist/nodejs/sockets/error.d.ts +83 -0
  36. package/dist/nodejs/sockets/error.js +120 -0
  37. package/dist/nodejs/sockets/ip-name-lookup.d.ts +18 -0
  38. package/dist/nodejs/sockets/ip-name-lookup.js +84 -0
  39. package/dist/nodejs/sockets/tcp.d.ts +333 -0
  40. package/dist/nodejs/sockets/tcp.js +723 -0
  41. package/dist/nodejs/sockets/udp.d.ts +224 -0
  42. package/dist/nodejs/sockets/udp.js +475 -0
  43. package/dist/nodejs/sockets.d.ts +11 -0
  44. package/{lib → dist}/nodejs/sockets.js +2 -5
  45. package/dist/nodejs/stream.d.ts +161 -0
  46. package/dist/nodejs/stream.js +388 -0
  47. package/dist/nodejs/workers/cli-worker.d.ts +1 -0
  48. package/dist/nodejs/workers/cli-worker.js +41 -0
  49. package/dist/nodejs/workers/filesystem-worker.d.ts +1 -0
  50. package/dist/nodejs/workers/filesystem-worker.js +102 -0
  51. package/dist/nodejs/workers/http-worker.d.ts +1 -0
  52. package/dist/nodejs/workers/http-worker.js +355 -0
  53. package/dist/nodejs/workers/resource-worker.d.ts +13 -0
  54. package/dist/nodejs/workers/resource-worker.js +152 -0
  55. package/dist/nodejs/workers/tcp-worker.d.ts +2 -0
  56. package/dist/nodejs/workers/tcp-worker.js +318 -0
  57. package/dist/nodejs/workers/udp-worker.d.ts +1 -0
  58. package/dist/nodejs/workers/udp-worker.js +156 -0
  59. package/package.json +20 -12
  60. package/types/interfaces/wasi-cli-environment.d.ts +3 -3
  61. package/types/interfaces/wasi-cli-exit.d.ts +12 -1
  62. package/types/interfaces/wasi-cli-run.d.ts +1 -1
  63. package/types/interfaces/wasi-cli-stderr.d.ts +3 -3
  64. package/types/interfaces/wasi-cli-stdin.d.ts +5 -5
  65. package/types/interfaces/wasi-cli-stdout.d.ts +3 -3
  66. package/types/interfaces/wasi-cli-terminal-input.d.ts +1 -1
  67. package/types/interfaces/wasi-cli-terminal-output.d.ts +1 -1
  68. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
  69. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
  70. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
  71. package/types/interfaces/wasi-cli-types.d.ts +5 -5
  72. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +3 -3
  73. package/types/interfaces/wasi-clocks-system-clock.d.ts +6 -6
  74. package/types/interfaces/wasi-clocks-types.d.ts +1 -1
  75. package/types/interfaces/wasi-filesystem-preopens.d.ts +1 -1
  76. package/types/interfaces/wasi-filesystem-types.d.ts +74 -74
  77. package/types/interfaces/wasi-http-client.d.ts +1 -1
  78. package/types/interfaces/wasi-http-handler.d.ts +1 -1
  79. package/types/interfaces/wasi-http-types.d.ts +34 -34
  80. package/types/interfaces/wasi-random-insecure-seed.d.ts +5 -5
  81. package/types/interfaces/wasi-random-insecure.d.ts +5 -5
  82. package/types/interfaces/wasi-random-random.d.ts +5 -5
  83. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +11 -11
  84. package/types/interfaces/wasi-sockets-types.d.ts +121 -121
  85. package/types/wasi-cli-command.d.ts +23 -23
  86. package/types/wasi-http-service.d.ts +14 -14
  87. package/lib/nodejs/cli.js +0 -127
  88. package/lib/nodejs/clocks.js +0 -100
  89. package/lib/nodejs/filesystem/descriptor.js +0 -1093
  90. package/lib/nodejs/filesystem/error.js +0 -92
  91. package/lib/nodejs/filesystem/utils.js +0 -19
  92. package/lib/nodejs/future.js +0 -190
  93. package/lib/nodejs/http/client.js +0 -161
  94. package/lib/nodejs/http/error.js +0 -71
  95. package/lib/nodejs/http/fields.js +0 -373
  96. package/lib/nodejs/http/request.js +0 -691
  97. package/lib/nodejs/http/response.js +0 -227
  98. package/lib/nodejs/http/server.js +0 -157
  99. package/lib/nodejs/sockets/address.js +0 -146
  100. package/lib/nodejs/sockets/error.js +0 -123
  101. package/lib/nodejs/sockets/ip-name-lookup.js +0 -93
  102. package/lib/nodejs/sockets/tcp.js +0 -801
  103. package/lib/nodejs/sockets/udp.js +0 -516
  104. package/lib/nodejs/stream.js +0 -437
  105. package/lib/nodejs/workers/cli-worker.js +0 -49
  106. package/lib/nodejs/workers/filesystem-worker.js +0 -119
  107. package/lib/nodejs/workers/http-worker.js +0 -417
  108. package/lib/nodejs/workers/resource-worker.js +0 -195
  109. package/lib/nodejs/workers/tcp-worker.js +0 -370
  110. package/lib/nodejs/workers/udp-worker.js +0 -194
  111. /package/{lib/nodejs/filesystem.js → dist/nodejs/filesystem.d.ts} +0 -0
  112. /package/{lib/nodejs/index.js → dist/nodejs/index.d.ts} +0 -0
@@ -1,373 +0,0 @@
1
- import { HttpError } from "./error.js";
2
-
3
- import * as http from "node:http";
4
- const { validateHeaderName = () => {}, validateHeaderValue = () => {} } = http;
5
-
6
- let FORBIDDEN_HEADERS = null;
7
- export const _forbiddenHeaders = {
8
- get value() {
9
- return (FORBIDDEN_HEADERS ??= new Set([
10
- "connection",
11
- "keep-alive",
12
- "host",
13
- "custom-forbidden-header",
14
- ]));
15
- },
16
- };
17
-
18
- /**
19
- * Implements the WASI Preview3 fields resource for HTTP headers and trailers
20
- */
21
- export class Fields {
22
- #immutable = false;
23
- /** @type {[string, Uint8Array][]} */
24
- #entries = [];
25
- /** @type {Map<string, [string, Uint8Array[]][]>} */
26
- #table = new Map();
27
-
28
- /**
29
- * Constructs an empty HTTP Fields
30
- * WIT:
31
- * ```
32
- * constructor();
33
- * ```
34
- */
35
- constructor() {}
36
-
37
- /**
38
- * Constructs a Fields instance from a list of name-value pairs
39
- * WIT:
40
- * ```
41
- * from-list: static func(entries: list<tuple<field-name,field-value>>) -> result<fields, header-error>;
42
- * ```
43
- *
44
- * @param {[string, Uint8Array[]][]} entries - List of field name and value pairs
45
- * @returns {Fields} A new Fields instance
46
- * @throws {HttpError} With payload.tag 'invalid-syntax' if any name or value is invalid
47
- * @throws {HttpError} With payload.tag 'forbidden' if any name is forbidden
48
- */
49
- static fromList(entries) {
50
- const fields = new Fields();
51
-
52
- for (const [name, values] of entries) {
53
- for (const value of Array.isArray(values) ? values : [values]) {
54
- fields.append(name, value);
55
- }
56
- }
57
- return fields;
58
- }
59
-
60
- /**
61
- * Get all values for a given field name
62
- * WIT:
63
- * ```
64
- * get: func(name: field-name) -> list<field-value>;
65
- * ```
66
- *
67
- * @param {string} name - The field name to get values for
68
- * @returns {Uint8Array[]} List of values for the field
69
- */
70
- get(name) {
71
- const tableEntries = this.#table.get(name.toLowerCase());
72
- if (!tableEntries) {
73
- return [];
74
- }
75
- return tableEntries.flatMap(([, v]) => v);
76
- }
77
-
78
- /**
79
- * Check if a field name exists in the collection
80
- * WIT:
81
- * ```
82
- * has: func(name: field-name) -> bool;
83
- * ```
84
- *
85
- * @param {string} name - The field name to check
86
- * @returns {boolean} True if the field exists
87
- */
88
- has(name) {
89
- return this.#table.has(name.toLowerCase());
90
- }
91
-
92
- /**
93
- * Set values for a field name, replacing any existing values
94
- * WIT:
95
- * ```
96
- * set: func(name: field-name, value: list<field-value>) -> result<_, header-error>;
97
- * ```
98
- *
99
- * @param {string} name - The field name
100
- * @param {Uint8Array[]} values - The values to set
101
- * @throws {HttpError} With payload.tag 'immutable' if the fields are immutable
102
- * @throws {HttpError} With payload.tag 'invalid-syntax' if name or values are invalid
103
- * @throws {HttpError} With payload.tag 'forbidden' if name is forbidden
104
- */
105
- set(name, values) {
106
- this.#ensureMutable();
107
-
108
- this.#validateName(name);
109
- for (const value of values) {
110
- this.#validateValue(name, value);
111
- }
112
-
113
- const lowercased = name.toLowerCase();
114
-
115
- let bucket = this.#table.get(lowercased);
116
- if (bucket) {
117
- this.#entries = this.#entries.filter((e) => !bucket.includes(e));
118
- this.#table.delete(lowercased);
119
- }
120
-
121
- if (values.length === 0) {
122
- return;
123
- }
124
-
125
- bucket = [];
126
- this.#table.set(lowercased, bucket);
127
- for (const value of values) {
128
- const entry = [name, value];
129
- this.#entries.push(entry);
130
- bucket.push(entry);
131
- }
132
- }
133
-
134
- /**
135
- * Delete all values for a field name
136
- * WIT:
137
- * ```
138
- * delete: func(name: field-name) -> result<_, header-error>;
139
- * ```
140
- *
141
- * @param {string} name - The field name to delete
142
- * @throws {HttpError} With payload.tag 'immutable' if the fields are immutable
143
- */
144
- delete(name) {
145
- this.#ensureMutable();
146
- this.#validateName(name);
147
- const lowercased = name.toLowerCase();
148
- const tableEntries = this.#table.get(lowercased);
149
-
150
- if (tableEntries) {
151
- this.#entries = this.#entries.filter((entry) => !tableEntries.includes(entry));
152
- this.#table.delete(lowercased);
153
- }
154
- }
155
-
156
- /**
157
- * Get all values for a field name and then delete them
158
- * WIT:
159
- * ```
160
- * get-and-delete: func(name: field-name) -> result<list<field-value>, header-error>;
161
- * ```
162
- *
163
- * @param {string} name - The field name to get and delete
164
- * @returns {Uint8Array[]} List of values that were deleted
165
- * @throws {HttpError} With payload.tag 'immutable' if the fields are immutable
166
- */
167
- getAndDelete(name) {
168
- this.#ensureMutable();
169
- this.#validateName(name);
170
-
171
- const values = this.get(name);
172
- this.delete(name);
173
- return values;
174
- }
175
-
176
- /**
177
- * Append a value to a field name
178
- * WIT:
179
- * ```
180
- * append: func(name: field-name, value: field-value) -> result<_, header-error>;
181
- * ```
182
- *
183
- * @param {string} name - The field name
184
- * @param {Uint8Array} value - The value to append
185
- * @throws {HttpError} With payload.tag 'immutable' if the fields are immutable
186
- * @throws {HttpError} With payload.tag 'invalid-syntax' if name or value is invalid
187
- * @throws {HttpError} With payload.tag 'forbidden' if name is forbidden
188
- */
189
- append(name, value) {
190
- this.#ensureMutable();
191
-
192
- this.#validateName(name);
193
- this.#validateValue(name, value);
194
-
195
- const lowercased = name.toLowerCase();
196
-
197
- const tableEntries = this.#table.get(lowercased);
198
- const entryName = tableEntries?.[0]?.[0] ?? name;
199
- const entry = [entryName, value];
200
- this.#entries.push(entry);
201
- if (tableEntries) {
202
- tableEntries.push(entry);
203
- } else {
204
- this.#table.set(lowercased, [entry]);
205
- }
206
- }
207
-
208
- /**
209
- * Get all entries as a list of name-value pairs
210
- * WIT:
211
- * ```
212
- * copy-all: func() -> list<tuple<field-name,field-value>>;
213
- * ```
214
- *
215
- * @returns {[string, Uint8Array][]} List of all entries
216
- */
217
- copyAll() {
218
- return this.#entries.slice();
219
- }
220
-
221
- /**
222
- * Create a deep copy of this Fields instance
223
- * WIT:
224
- * ```
225
- * clone: func() -> fields;
226
- * ```
227
- *
228
- * @returns {Fields} A new mutable Fields instance with the same entries
229
- */
230
- clone() {
231
- const entries = this.#entries.slice();
232
- return _fieldsFromEntriesChecked(entries);
233
- }
234
-
235
- /**
236
- * Mark fields as immutable
237
- *
238
- * @param {Fields} fields - The Fields instance to mark as immutable
239
- * @returns {Fields} The same instance, now immutable
240
- */
241
- static _lock(fields) {
242
- fields.#immutable = true;
243
- return fields;
244
- }
245
-
246
- /**
247
- * Create a Fields instance from pre-validated entries
248
- *
249
- * @param {[string, Uint8Array][]} entries - Validated entries to use
250
- * @returns {Fields} A new Fields instance with the provided entries
251
- */
252
- static _fromEntriesChecked(entries) {
253
- const fields = new Fields();
254
- fields.#entries = entries;
255
-
256
- for (const entry of entries) {
257
- const lowercase = entry[0].toLowerCase();
258
- const existing = fields.#table.get(lowercase);
259
-
260
- if (existing) {
261
- existing.push(entry);
262
- } else {
263
- fields.#table.set(lowercase, [entry]);
264
- }
265
- }
266
- return fields;
267
- }
268
-
269
- #validateName(name) {
270
- try {
271
- validateHeaderName(name);
272
- } catch {
273
- throw new HttpError("invalid-syntax", `Invalid header name: ${name}`);
274
- }
275
-
276
- if (_forbiddenHeaders.value.has(name.toLowerCase())) {
277
- throw new HttpError("forbidden", `Header ${name} is forbidden`);
278
- }
279
- }
280
-
281
- #validateValue(name, value) {
282
- try {
283
- validateHeaderValue(name, bytesToHeaderValueString(value));
284
- } catch {
285
- throw new HttpError("invalid-syntax", `Invalid header value for ${name}`);
286
- }
287
- }
288
-
289
- #ensureMutable() {
290
- if (this.#immutable) {
291
- throw new HttpError("immutable", "Cannot modify immutable fields");
292
- }
293
- }
294
- }
295
-
296
- function bytesToHeaderValueString(value) {
297
- let result = "";
298
- for (let i = 0; i < value.length; i += 0x8000) {
299
- result += String.fromCharCode(...value.subarray(i, i + 0x8000));
300
- }
301
- return result;
302
- }
303
-
304
- export function _fieldsLock(fields) {
305
- return Fields._lock(fields);
306
- }
307
-
308
- export function _fieldsFromEntriesChecked(entries) {
309
- return Fields._fromEntriesChecked(entries);
310
- }
311
-
312
- export async function _readTrailersForTransport(trailers) {
313
- const fields = _unwrapTrailersResult(await trailers.read());
314
-
315
- if (fields == null) {
316
- return undefined;
317
- }
318
-
319
- if (fields instanceof Fields) {
320
- return fields.copyAll();
321
- }
322
-
323
- if (Array.isArray(fields)) {
324
- return fields;
325
- }
326
-
327
- throw new HttpError("invalid-argument", "trailers must be Fields or a list of entries");
328
- }
329
-
330
- export function _trailerResultFromEntries(entries) {
331
- return {
332
- tag: "ok",
333
- val: entries && entries.length ? _fieldsFromEntriesChecked(entries) : null,
334
- };
335
- }
336
-
337
- function _unwrapTrailersResult(result) {
338
- if (result == null) {
339
- return undefined;
340
- }
341
-
342
- if (result instanceof Fields || Array.isArray(result)) {
343
- return result;
344
- }
345
-
346
- if (typeof result === "object") {
347
- if (result.tag === "ok") {
348
- return _unwrapTrailersResult(result.val);
349
- }
350
-
351
- if (result.tag === "some") {
352
- return _unwrapTrailersResult(result.val);
353
- }
354
-
355
- if (result.tag === "none") {
356
- return undefined;
357
- }
358
-
359
- if (result.tag === "err") {
360
- throw HttpError.from(result.val);
361
- }
362
-
363
- if (Object.hasOwn(result, "ok")) {
364
- return _unwrapTrailersResult(result.ok);
365
- }
366
-
367
- if (Object.hasOwn(result, "err")) {
368
- throw HttpError.from(result.err);
369
- }
370
- }
371
-
372
- return result;
373
- }