@bytecodealliance/preview2-shim 0.0.8 → 0.0.10

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 (86) hide show
  1. package/README.md +1 -0
  2. package/lib/browser/cli-base.js +48 -0
  3. package/lib/browser/clocks.js +44 -0
  4. package/lib/browser/filesystem.js +134 -141
  5. package/lib/browser/http.js +114 -0
  6. package/lib/browser/index.js +15 -37
  7. package/lib/browser/io.js +60 -20
  8. package/lib/browser/{console.js → logging.js} +7 -5
  9. package/lib/browser/poll.js +9 -7
  10. package/lib/browser/random.js +48 -24
  11. package/lib/browser/sockets.js +200 -0
  12. package/lib/nodejs/{preopens.js → cli-base.js} +38 -8
  13. package/lib/nodejs/clocks.js +44 -0
  14. package/lib/nodejs/filesystem.js +240 -237
  15. package/lib/nodejs/http.js +114 -0
  16. package/lib/nodejs/index.js +15 -37
  17. package/lib/nodejs/io.js +148 -19
  18. package/lib/nodejs/{console.js → logging.js} +7 -5
  19. package/lib/nodejs/poll.js +9 -7
  20. package/lib/nodejs/random.js +27 -13
  21. package/lib/nodejs/sockets.js +200 -0
  22. package/package.json +1 -1
  23. package/types/exports/{HTTP.d.ts → wasi-http-incoming-handler.d.ts} +1 -1
  24. package/types/imports/environment.d.ts +14 -1
  25. package/types/imports/exit.d.ts +4 -1
  26. package/types/imports/filesystem.d.ts +695 -48
  27. package/types/imports/handler.d.ts +40 -0
  28. package/types/imports/insecure-seed.d.ts +22 -0
  29. package/types/imports/insecure.d.ts +20 -0
  30. package/types/imports/instance-network.d.ts +4 -1
  31. package/types/imports/ip-name-lookup.d.ts +66 -9
  32. package/types/imports/monotonic-clock.d.ts +18 -2
  33. package/types/imports/network.d.ts +171 -9
  34. package/types/imports/{default-outgoing-HTTP.d.ts → outgoing-handler.d.ts} +1 -1
  35. package/types/imports/poll.d.ts +38 -2
  36. package/types/imports/preopens.d.ts +6 -9
  37. package/types/imports/random.d.ts +19 -2
  38. package/types/imports/stderr.d.ts +5 -0
  39. package/types/imports/stdin.d.ts +5 -0
  40. package/types/imports/stdout.d.ts +5 -0
  41. package/types/imports/streams.d.ts +161 -4
  42. package/types/imports/tcp-create-socket.d.ts +27 -3
  43. package/types/imports/tcp.d.ts +248 -15
  44. package/types/imports/timezone.d.ts +60 -2
  45. package/types/imports/types.d.ts +28 -28
  46. package/types/imports/udp-create-socket.d.ts +27 -3
  47. package/types/imports/udp.d.ts +195 -12
  48. package/types/imports/wall-clock.d.ts +24 -1
  49. package/types/wasi-command.d.ts +23 -0
  50. package/types/wasi-proxy.d.ts +10 -44
  51. package/types/wasi-reactor.d.ts +23 -70
  52. package/lib/browser/default-outgoing-HTTP.js +0 -3
  53. package/lib/browser/environment.js +0 -17
  54. package/lib/browser/exit.js +0 -21
  55. package/lib/browser/instance-network.js +0 -3
  56. package/lib/browser/ip-name-lookup.js +0 -23
  57. package/lib/browser/monotonic-clock.js +0 -14
  58. package/lib/browser/network.js +0 -3
  59. package/lib/browser/preopens.js +0 -11
  60. package/lib/browser/stderr.js +0 -4
  61. package/lib/browser/streams.js +0 -60
  62. package/lib/browser/tcp-create-socket.js +0 -3
  63. package/lib/browser/tcp.js +0 -75
  64. package/lib/browser/timezone.js +0 -12
  65. package/lib/browser/types.js +0 -99
  66. package/lib/browser/udp-create-socket.js +0 -3
  67. package/lib/browser/udp.js +0 -75
  68. package/lib/browser/wall-clock.js +0 -9
  69. package/lib/nodejs/default-outgoing-HTTP.js +0 -3
  70. package/lib/nodejs/environment.js +0 -19
  71. package/lib/nodejs/exit.js +0 -3
  72. package/lib/nodejs/instance-network.js +0 -3
  73. package/lib/nodejs/ip-name-lookup.js +0 -23
  74. package/lib/nodejs/monotonic-clock.js +0 -14
  75. package/lib/nodejs/network.js +0 -3
  76. package/lib/nodejs/stderr.js +0 -4
  77. package/lib/nodejs/streams.js +0 -97
  78. package/lib/nodejs/tcp-create-socket.js +0 -3
  79. package/lib/nodejs/tcp.js +0 -75
  80. package/lib/nodejs/timezone.js +0 -12
  81. package/lib/nodejs/types.js +0 -99
  82. package/lib/nodejs/udp-create-socket.js +0 -3
  83. package/lib/nodejs/udp.js +0 -75
  84. package/lib/nodejs/wall-clock.js +0 -9
  85. package/types/imports/console.d.ts +0 -17
  86. package/types/index.d.ts +0 -8
package/lib/nodejs/io.js CHANGED
@@ -1,25 +1,154 @@
1
- export function read(s, len) {
2
- switch (s) {
3
- case 0:
4
- return [process.stdin.read(len), true];
5
- default:
6
- throw new Error(`TODO: write ${s}`);
1
+ import { readSync as fsReadSync } from 'node:fs';
2
+
3
+ function _convertFsError (e) {
4
+ switch (e.code) {
5
+ case 'EACCES': throw 'access';
6
+ case 'EAGAIN':
7
+ case 'EWOULDBLOCK': throw 'would-block';
8
+ case 'EALREADY': throw 'already';
9
+ case 'EBADF': throw 'bad-descriptor';
10
+ case 'EBUSY': throw 'busy';
11
+ case 'EDEADLK': throw 'deadlock';
12
+ case 'EDQUOT': throw 'quota';
13
+ case 'EEXIST': throw 'exist';
14
+ case 'EFBIG': throw 'file-too-large';
15
+ case 'EILSEQ': throw 'illegal-byte-sequence';
16
+ case 'EINPROGRESS': throw 'in-progress';
17
+ case 'EINTR': throw 'interrupted';
18
+ case 'EINVAL': throw 'invalid';
19
+ case 'EIO': throw 'io';
20
+ case 'EISDIR': throw 'is-directory';
21
+ case 'ELOOP': throw 'loop';
22
+ case 'EMLINK': throw 'too-many-links';
23
+ case 'EMSGSIZE': throw 'message-size';
24
+ case 'ENAMETOOLONG': throw 'name-too-long'
25
+ case 'ENODEV': throw 'no-device';
26
+ case 'ENOENT': throw 'no-entry';
27
+ case 'ENOLCK': throw 'no-lock';
28
+ case 'ENOMEM': throw 'insufficient-memory';
29
+ case 'ENOSPC': throw 'insufficient-space';
30
+ case 'ENOTDIR': throw 'not-directory';
31
+ case 'ENOTEMPTY': throw 'not-empty';
32
+ case 'ENOTRECOVERABLE': throw 'not-recoverable';
33
+ case 'ENOTSUP': throw 'unsupported';
34
+ case 'ENOTTY': throw 'no-tty';
35
+ case 'ENXIO': throw 'no-such-device';
36
+ case 'EOVERFLOW': throw 'overflow';
37
+ case 'EPERM': throw 'not-permitted';
38
+ case 'EPIPE': throw 'pipe';
39
+ case 'EROFS': throw 'read-only';
40
+ case 'ESPIPE': throw 'invalid-seek';
41
+ case 'ETXTBSY': throw 'text-file-busy';
42
+ case 'EXDEV': throw 'cross-device';
43
+ default: throw e;
7
44
  }
8
45
  }
9
46
 
10
- export function write(s, buf) {
11
- switch (s) {
12
- case 0:
13
- throw new Error(`TODO: write stdin`);
14
- case 1: {
15
- process.stdout.write(buf);
16
- return BigInt(buf.byteLength);
47
+ export let _streams = {};
48
+ let streamCnt = 0;
49
+ export function _createFsStream(fd, type, context) {
50
+ _streams[streamCnt] = {
51
+ type,
52
+ fd,
53
+ context
54
+ };
55
+ return streamCnt++;
56
+ }
57
+
58
+ export function _getFsStreamContext(stream, type) {
59
+ const entry = _streams[stream];
60
+ if (!entry)
61
+ throw new Error(`No '${type}' stream found at stream ${stream}`);
62
+ if (entry.type !== type)
63
+ throw new Error(`Unexpected '${entry.type}' stream found at stream ${stream}, expected '${type}'`);
64
+ return entry.context;
65
+ }
66
+
67
+ export function _dropFsStream(stream) {
68
+ // TODO: recycling?
69
+ delete _streams[stream];
70
+ }
71
+
72
+ export const streams = {
73
+ read(s, len) {
74
+ switch (s) {
75
+ case 0:
76
+ return [process.stdin.read(len), true];
77
+ default:
78
+ throw new Error(`TODO: write ${s}`);
17
79
  }
18
- case 2: {
19
- process.stderr.write(buf);
20
- return BigInt(buf.byteLength);
80
+ },
81
+ blockingRead(s, len) {
82
+ len = Number(len);
83
+ const stream = _streams[s];
84
+ if (!stream) throw null;
85
+ switch (stream.type) {
86
+ case 'file': {
87
+ const buf = Buffer.alloc(Number(len));
88
+ try {
89
+ const readBytes = fsReadSync(stream.fd, buf, 0, Number(len));
90
+ if (readBytes < Number(len))
91
+ return [new Uint8Array(), true];
92
+ return [new Uint8Array(buf.buffer, 0, readBytes), false];
93
+ }
94
+ catch (e) {
95
+ _convertFsError(e);
96
+ }
97
+ break;
98
+ }
99
+ default: throw null;
21
100
  }
22
- default:
23
- throw new Error(`TODO: write ${s}`);
101
+ },
102
+ skip(s, _len) {
103
+ console.log(`[streams] Skip ${s}`);
104
+ },
105
+ blockingSkip(s, _len) {
106
+ console.log(`[streams] Blocking skip ${s}`);
107
+ },
108
+ subscribeToInputStream(s) {
109
+ console.log(`[streams] Subscribe to input stream ${s}`);
110
+ },
111
+ dropInputStream(s) {
112
+ delete _streams[s];
113
+ },
114
+ write(s, buf) {
115
+ switch (s) {
116
+ case 0:
117
+ throw new Error(`TODO: write stdin`);
118
+ case 1: {
119
+ process.stdout.write(buf);
120
+ return BigInt(buf.byteLength);
121
+ }
122
+ case 2: {
123
+ process.stderr.write(buf);
124
+ return BigInt(buf.byteLength);
125
+ }
126
+ default:
127
+ throw new Error(`TODO: write ${s}`);
128
+ }
129
+ },
130
+ blockingWrite(s, _buf) {
131
+ console.log(`[streams] Blocking write ${s}`);
132
+ },
133
+ writeZeroes(s, _len) {
134
+ console.log(`[streams] Write zeroes ${s}`);
135
+ },
136
+ blockingWriteZeroes(s, _len) {
137
+ console.log(`[streams] Blocking write zeroes ${s}`);
138
+ },
139
+ splice(s, _src, _len) {
140
+ console.log(`[streams] Splice ${s}`);
141
+ },
142
+ blockingSplice(s, _src, _len) {
143
+ console.log(`[streams] Blocking splice ${s}`);
144
+ },
145
+ forward(s, _src) {
146
+ console.log(`[streams] Forward ${s}`);
147
+ },
148
+ subscribeToOutputStream(s) {
149
+ console.log(`[streams] Subscribe to output stream ${s}`);
150
+ },
151
+ dropOutputStream(s) {
152
+ console.log(`[streams] Drop output stream ${s}`);
24
153
  }
25
- }
154
+ };
@@ -2,11 +2,13 @@ const levels = ["trace", "debug", "info", "warn", "error"];
2
2
 
3
3
  let logLevel = levels.indexOf("warn");
4
4
 
5
+ export const handler = {
6
+ log(level, context, msg) {
7
+ if (logLevel > levels.indexOf(level)) return;
8
+ process.stdout.write(`${level}: (${context}) ${msg}\n`);
9
+ }
10
+ };
11
+
5
12
  export function setLevel(level) {
6
13
  logLevel = levels.indexOf(level);
7
14
  }
8
-
9
- export function log(level, context, msg) {
10
- if (logLevel > levels.indexOf(level)) return;
11
- process.stdout.write(`${level}: (${context}) ${msg}\n`);
12
- }
@@ -1,7 +1,9 @@
1
- export function dropPollable(p) {
2
- console.log(`[poll] Drop pollable ${p}`);
3
- }
4
-
5
- export function pollOneoff(f) {
6
- console.log(`[poll] Poll oneoff ${f}`);
7
- }
1
+ export const poll = {
2
+ dropPollable (pollable) {
3
+ console.log(`[poll] Drop (${pollable})`);
4
+ },
5
+ pollOneoff (input) {
6
+ console.log(`[poll] Oneoff (${input})`);
7
+ return [];
8
+ }
9
+ };
@@ -1,18 +1,32 @@
1
1
  import { randomBytes } from "node:crypto";
2
2
 
3
- export function getRandomBytes(len) {
4
- return randomBytes(Number(len));
5
- }
3
+ export const insecure = {
4
+ getInsecureRandomBytes (len) {
5
+ return randomBytes(Number(len));
6
+ },
7
+ getInsecureRandomU64 () {
8
+ return new BigUint64Array(randomBytes(8).buffer)[0];
9
+ }
10
+ };
11
+
12
+ let insecureSeedValue1, insecureSeedValue2;
13
+
14
+ export const insecureSeed = {
15
+ insecureSeed () {
16
+ if (insecureSeedValue1 === undefined) {
17
+ insecureSeedValue1 = random.getRandomU64();
18
+ insecureSeedValue2 = random.getRandomU64();
19
+ }
20
+ return [insecureSeedValue1, insecureSeedValue2];
21
+ }
22
+ };
6
23
 
7
- export function getRandomU64 () {
8
- return new BigUint64Array(randomBytes(8).buffer)[0];
9
- }
24
+ export const random = {
25
+ getRandomBytes(len) {
26
+ return randomBytes(Number(len));
27
+ },
10
28
 
11
- let insecureRandomValue1, insecureRandomValue2;
12
- export function insecureRandom () {
13
- if (insecureRandomValue1 === undefined) {
14
- insecureRandomValue1 = getRandomU64();
15
- insecureRandomValue2 = getRandomU64();
29
+ getRandomU64 () {
30
+ return new BigUint64Array(randomBytes(8).buffer)[0];
16
31
  }
17
- return [insecureRandomValue1, insecureRandomValue2];
18
- }
32
+ };
@@ -0,0 +1,200 @@
1
+ export const instanceNetwork = {
2
+ instanceNetwork () {
3
+ console.log(`[sockets] instance network`);
4
+ }
5
+ };
6
+
7
+ export const ipNameLookup = {
8
+ dropResolveAddressStream () {
9
+
10
+ },
11
+ subscribe () {
12
+
13
+ },
14
+ resolveAddresses () {
15
+
16
+ },
17
+ resolveNextAddress () {
18
+
19
+ },
20
+ nonBlocking () {
21
+
22
+ },
23
+ setNonBlocking () {
24
+
25
+ },
26
+ };
27
+
28
+ export const network = {
29
+ dropNetwork () {
30
+
31
+ }
32
+ };
33
+
34
+ export const tcpCreateSocket = {
35
+ createTcpSocket () {
36
+
37
+ }
38
+ };
39
+
40
+ export const tcp = {
41
+ subscribe () {
42
+
43
+ },
44
+ dropTcpSocket() {
45
+
46
+ },
47
+ bind() {
48
+
49
+ },
50
+ connect() {
51
+
52
+ },
53
+ listen() {
54
+
55
+ },
56
+ accept() {
57
+
58
+ },
59
+ localAddress() {
60
+
61
+ },
62
+ remoteAddress() {
63
+
64
+ },
65
+ addressFamily() {
66
+
67
+ },
68
+ ipv6Only() {
69
+
70
+ },
71
+ setIpv6Only() {
72
+
73
+ },
74
+ setListenBacklogSize() {
75
+
76
+ },
77
+ keepAlive() {
78
+
79
+ },
80
+ setKeepAlive() {
81
+
82
+ },
83
+ noDelay() {
84
+
85
+ },
86
+ setNoDelay() {
87
+
88
+ },
89
+ unicastHopLimit() {
90
+
91
+ },
92
+ setUnicastHopLimit() {
93
+
94
+ },
95
+ receiveBufferSize() {
96
+
97
+ },
98
+ setReceiveBufferSize() {
99
+
100
+ },
101
+ sendBufferSize() {
102
+
103
+ },
104
+ setSendBufferSize() {
105
+
106
+ },
107
+ nonBlocking() {
108
+
109
+ },
110
+ setNonBlocking() {
111
+
112
+ },
113
+ shutdown() {
114
+
115
+ }
116
+ };
117
+
118
+ export const udp = {
119
+ subscribe () {
120
+
121
+ },
122
+
123
+ dropUdpSocket () {
124
+
125
+ },
126
+
127
+ bind () {
128
+
129
+ },
130
+
131
+ connect () {
132
+
133
+ },
134
+
135
+ receive () {
136
+
137
+ },
138
+
139
+ send () {
140
+
141
+ },
142
+
143
+ localAddress () {
144
+
145
+ },
146
+
147
+ remoteAddress () {
148
+
149
+ },
150
+
151
+ addressFamily () {
152
+
153
+ },
154
+
155
+ ipv6Only () {
156
+
157
+ },
158
+
159
+ setIpv6Only () {
160
+
161
+ },
162
+
163
+ unicastHopLimit () {
164
+
165
+ },
166
+
167
+ setUnicastHopLimit () {
168
+
169
+ },
170
+
171
+ receiveBufferSize () {
172
+
173
+ },
174
+
175
+ setReceiveBufferSize () {
176
+
177
+ },
178
+
179
+ sendBufferSize () {
180
+
181
+ },
182
+
183
+ setSendBufferSize () {
184
+
185
+ },
186
+
187
+ nonBlocking () {
188
+
189
+ },
190
+
191
+ setNonBlocking () {
192
+
193
+ }
194
+ };
195
+
196
+ export const udpCreateSocket = {
197
+ createTcpSocket () {
198
+
199
+ }
200
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "WASI Preview2 shim for JS environments",
5
5
  "author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- export namespace Http {
1
+ export namespace ExportsWasiHttpIncomingHandler {
2
2
  export function handle(request: IncomingRequest, responseOut: ResponseOutparam): void;
3
3
  }
4
4
  import type { IncomingRequest } from '../imports/types';
@@ -1,4 +1,17 @@
1
- export namespace Environment {
1
+ export namespace ImportsEnvironment {
2
+ /**
3
+ * Get the POSIX-style environment variables.
4
+ *
5
+ * Each environment variable is provided as a pair of string variable names
6
+ * and string value.
7
+ *
8
+ * Morally, these are a value import, but until value imports are available
9
+ * in the component model, this import function should return the same
10
+ * values each time it is called.
11
+ */
2
12
  export function getEnvironment(): [string, string][];
13
+ /**
14
+ * Get the POSIX-style arguments to the program.
15
+ */
3
16
  export function getArguments(): string[];
4
17
  }
@@ -1,4 +1,7 @@
1
- export namespace Exit {
1
+ export namespace ImportsExit {
2
+ /**
3
+ * Exit the curerent instance and any linked instances.
4
+ */
2
5
  export function exit(status: Result<void, void>): void;
3
6
  }
4
7
  export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };