@bytecodealliance/preview2-shim 0.17.8 → 0.17.9

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.
@@ -1,9 +1,9 @@
1
- import * as clocks from './clocks.js';
2
- import * as filesystem from './filesystem.js';
3
- import * as http from './http.js';
4
- import * as io from './io.js';
5
- import * as random from './random.js';
6
- import * as sockets from './sockets.js';
7
- import * as cli from './cli.js';
1
+ import * as clocks from "./clocks.js";
2
+ import * as filesystem from "./filesystem.js";
3
+ import * as http from "./http.js";
4
+ import * as io from "./io.js";
5
+ import * as random from "./random.js";
6
+ import * as sockets from "./sockets.js";
7
+ import * as cli from "./cli.js";
8
8
 
9
9
  export { clocks, filesystem, http, io, random, sockets, cli };
package/lib/browser/io.js CHANGED
@@ -1,14 +1,14 @@
1
1
  let id = 0;
2
2
 
3
- const symbolDispose = Symbol.dispose || Symbol.for('dispose');
3
+ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
4
4
 
5
5
  const IoError = class Error {
6
- constructor(msg) {
7
- this.msg = msg;
8
- }
9
- toDebugString() {
10
- return this.msg;
11
- }
6
+ constructor(msg) {
7
+ this.msg = msg;
8
+ }
9
+ toDebugString() {
10
+ return this.msg;
11
+ }
12
12
  };
13
13
 
14
14
  /**
@@ -40,142 +40,142 @@ const IoError = class Error {
40
40
  **/
41
41
 
42
42
  class InputStream {
43
- /**
44
- * @param {InputStreamHandler} handler
45
- */
46
- constructor(handler) {
47
- if (!handler) {
48
- console.trace('no handler');
49
- }
50
- this.id = ++id;
51
- this.handler = handler;
43
+ /**
44
+ * @param {InputStreamHandler} handler
45
+ */
46
+ constructor(handler) {
47
+ if (!handler) {
48
+ console.trace("no handler");
52
49
  }
53
- read(len) {
54
- if (this.handler.read) {
55
- return this.handler.read(len);
56
- }
57
- return this.handler.blockingRead.call(this, len);
50
+ this.id = ++id;
51
+ this.handler = handler;
52
+ }
53
+ read(len) {
54
+ if (this.handler.read) {
55
+ return this.handler.read(len);
58
56
  }
59
- blockingRead(len) {
60
- return this.handler.blockingRead.call(this, len);
57
+ return this.handler.blockingRead.call(this, len);
58
+ }
59
+ blockingRead(len) {
60
+ return this.handler.blockingRead.call(this, len);
61
+ }
62
+ skip(len) {
63
+ if (this.handler.skip) {
64
+ return this.handler.skip.call(this, len);
61
65
  }
62
- skip(len) {
63
- if (this.handler.skip) {
64
- return this.handler.skip.call(this, len);
65
- }
66
- if (this.handler.read) {
67
- const bytes = this.handler.read.call(this, len);
68
- return BigInt(bytes.byteLength);
69
- }
70
- return this.blockingSkip.call(this, len);
66
+ if (this.handler.read) {
67
+ const bytes = this.handler.read.call(this, len);
68
+ return BigInt(bytes.byteLength);
71
69
  }
72
- blockingSkip(len) {
73
- if (this.handler.blockingSkip) {
74
- return this.handler.blockingSkip.call(this, len);
75
- }
76
- const bytes = this.handler.blockingRead.call(this, len);
77
- return BigInt(bytes.byteLength);
70
+ return this.blockingSkip.call(this, len);
71
+ }
72
+ blockingSkip(len) {
73
+ if (this.handler.blockingSkip) {
74
+ return this.handler.blockingSkip.call(this, len);
78
75
  }
79
- subscribe() {
80
- if (this.handler.subscribe) {
81
- return this.handler.subscribe();
82
- }
83
- return new Pollable();
76
+ const bytes = this.handler.blockingRead.call(this, len);
77
+ return BigInt(bytes.byteLength);
78
+ }
79
+ subscribe() {
80
+ if (this.handler.subscribe) {
81
+ return this.handler.subscribe();
84
82
  }
85
- [symbolDispose]() {
86
- if (this.handler.drop) {
87
- this.handler.drop.call(this);
88
- }
83
+ return new Pollable();
84
+ }
85
+ [symbolDispose]() {
86
+ if (this.handler.drop) {
87
+ this.handler.drop.call(this);
89
88
  }
89
+ }
90
90
  }
91
91
 
92
92
  class OutputStream {
93
- /**
94
- * @param {OutputStreamHandler} handler
95
- */
96
- constructor(handler) {
97
- if (!handler) {
98
- console.trace('no handler');
99
- }
100
- this.id = ++id;
101
- this.open = true;
102
- this.handler = handler;
93
+ /**
94
+ * @param {OutputStreamHandler} handler
95
+ */
96
+ constructor(handler) {
97
+ if (!handler) {
98
+ console.trace("no handler");
103
99
  }
104
- checkWrite(len) {
105
- if (!this.open) {
106
- return 0n;
107
- }
108
- if (this.handler.checkWrite) {
109
- return this.handler.checkWrite.call(this, len);
110
- }
111
- return 1_000_000n;
100
+ this.id = ++id;
101
+ this.open = true;
102
+ this.handler = handler;
103
+ }
104
+ checkWrite(len) {
105
+ if (!this.open) {
106
+ return 0n;
112
107
  }
113
- write(buf) {
114
- this.handler.write.call(this, buf);
115
- }
116
- blockingWriteAndFlush(buf) {
117
- /// Perform a write of up to 4096 bytes, and then flush the stream. Block
118
- /// until all of these operations are complete, or an error occurs.
119
- ///
120
- /// This is a convenience wrapper around the use of `check-write`,
121
- /// `subscribe`, `write`, and `flush`, and is implemented with the
122
- /// following pseudo-code:
123
- ///
124
- /// ```text
125
- /// let pollable = this.subscribe();
126
- /// while !contents.is_empty() {
127
- /// // Wait for the stream to become writable
128
- /// poll-one(pollable);
129
- /// let Ok(n) = this.check-write(); // eliding error handling
130
- /// let len = min(n, contents.len());
131
- /// let (chunk, rest) = contents.split_at(len);
132
- /// this.write(chunk ); // eliding error handling
133
- /// contents = rest;
134
- /// }
135
- /// this.flush();
136
- /// // Wait for completion of `flush`
137
- /// poll-one(pollable);
138
- /// // Check for any errors that arose during `flush`
139
- /// let _ = this.check-write(); // eliding error handling
140
- /// ```
141
- this.handler.write.call(this, buf);
142
- }
143
- flush() {
144
- if (this.handler.flush) {
145
- this.handler.flush.call(this);
146
- }
108
+ if (this.handler.checkWrite) {
109
+ return this.handler.checkWrite.call(this, len);
147
110
  }
148
- blockingFlush() {
149
- this.open = true;
111
+ return 1_000_000n;
112
+ }
113
+ write(buf) {
114
+ this.handler.write.call(this, buf);
115
+ }
116
+ blockingWriteAndFlush(buf) {
117
+ /// Perform a write of up to 4096 bytes, and then flush the stream. Block
118
+ /// until all of these operations are complete, or an error occurs.
119
+ ///
120
+ /// This is a convenience wrapper around the use of `check-write`,
121
+ /// `subscribe`, `write`, and `flush`, and is implemented with the
122
+ /// following pseudo-code:
123
+ ///
124
+ /// ```text
125
+ /// let pollable = this.subscribe();
126
+ /// while !contents.is_empty() {
127
+ /// // Wait for the stream to become writable
128
+ /// poll-one(pollable);
129
+ /// let Ok(n) = this.check-write(); // eliding error handling
130
+ /// let len = min(n, contents.len());
131
+ /// let (chunk, rest) = contents.split_at(len);
132
+ /// this.write(chunk ); // eliding error handling
133
+ /// contents = rest;
134
+ /// }
135
+ /// this.flush();
136
+ /// // Wait for completion of `flush`
137
+ /// poll-one(pollable);
138
+ /// // Check for any errors that arose during `flush`
139
+ /// let _ = this.check-write(); // eliding error handling
140
+ /// ```
141
+ this.handler.write.call(this, buf);
142
+ }
143
+ flush() {
144
+ if (this.handler.flush) {
145
+ this.handler.flush.call(this);
150
146
  }
151
- writeZeroes(len) {
152
- this.write.call(this, new Uint8Array(Number(len)));
147
+ }
148
+ blockingFlush() {
149
+ this.open = true;
150
+ }
151
+ writeZeroes(len) {
152
+ this.write.call(this, new Uint8Array(Number(len)));
153
+ }
154
+ blockingWriteZeroes(len) {
155
+ this.blockingWrite.call(this, new Uint8Array(Number(len)));
156
+ }
157
+ blockingWriteZeroesAndFlush(len) {
158
+ this.blockingWriteAndFlush.call(this, new Uint8Array(Number(len)));
159
+ }
160
+ splice(src, len) {
161
+ const spliceLen = Math.min(len, this.checkWrite.call(this));
162
+ const bytes = src.read(spliceLen);
163
+ this.write.call(this, bytes);
164
+ return bytes.byteLength;
165
+ }
166
+ blockingSplice(_src, _len) {
167
+ console.log(`[streams] Blocking splice ${this.id}`);
168
+ }
169
+ forward(_src) {
170
+ console.log(`[streams] Forward ${this.id}`);
171
+ }
172
+ subscribe() {
173
+ if (this.handler.subscribe) {
174
+ return this.handler.subscribe();
153
175
  }
154
- blockingWriteZeroes(len) {
155
- this.blockingWrite.call(this, new Uint8Array(Number(len)));
156
- }
157
- blockingWriteZeroesAndFlush(len) {
158
- this.blockingWriteAndFlush.call(this, new Uint8Array(Number(len)));
159
- }
160
- splice(src, len) {
161
- const spliceLen = Math.min(len, this.checkWrite.call(this));
162
- const bytes = src.read(spliceLen);
163
- this.write.call(this, bytes);
164
- return bytes.byteLength;
165
- }
166
- blockingSplice(_src, _len) {
167
- console.log(`[streams] Blocking splice ${this.id}`);
168
- }
169
- forward(_src) {
170
- console.log(`[streams] Forward ${this.id}`);
171
- }
172
- subscribe() {
173
- if (this.handler.subscribe) {
174
- return this.handler.subscribe();
175
- }
176
- return new Pollable();
177
- }
178
- [symbolDispose]() {}
176
+ return new Pollable();
177
+ }
178
+ [symbolDispose]() {}
179
179
  }
180
180
 
181
181
  export const error = { Error: IoError };
@@ -183,74 +183,80 @@ export const error = { Error: IoError };
183
183
  export const streams = { InputStream, OutputStream };
184
184
 
185
185
  class Pollable {
186
- #ready = false;
187
- #promise = null;
186
+ #ready = false;
187
+ #promise = null;
188
188
 
189
- constructor(promise) {
190
- if (!promise) {
191
- this.#ready = true;
192
- } else {
193
- this.#promise = promise.then(
194
- () => { this.#ready = true; },
195
- () => { this.#ready = true; }
196
- );
197
- }
189
+ constructor(promise) {
190
+ if (!promise) {
191
+ this.#ready = true;
192
+ } else {
193
+ this.#promise = promise.then(
194
+ () => {
195
+ this.#ready = true;
196
+ },
197
+ () => {
198
+ this.#ready = true;
199
+ },
200
+ );
198
201
  }
202
+ }
199
203
 
200
- ready() {
201
- return this.#ready;
202
- }
204
+ ready() {
205
+ return this.#ready;
206
+ }
203
207
 
204
- block() {
205
- if (this.#ready) {
206
- return Promise.resolve();
207
- }
208
- return this.#promise;
208
+ block() {
209
+ if (this.#ready) {
210
+ return Promise.resolve();
209
211
  }
212
+ return this.#promise;
213
+ }
210
214
 
211
- [symbolDispose]() {
212
- this.#promise = null;
213
- }
215
+ [symbolDispose]() {
216
+ this.#promise = null;
217
+ }
214
218
  }
215
219
 
216
220
  function pollList(list) {
217
- if (list.length === 0) {
218
- throw new Error('poll list must not be empty');
221
+ if (list.length === 0) {
222
+ throw new Error("poll list must not be empty");
223
+ }
224
+ if (list.length > 0xffffffff) {
225
+ throw new Error("poll list length exceeds u32 index range");
226
+ }
227
+ const ready = [];
228
+ for (let i = 0; i < list.length; i++) {
229
+ if (list[i].ready()) {
230
+ ready.push(i);
219
231
  }
220
- if (list.length > 0xFFFFFFFF) {
221
- throw new Error('poll list length exceeds u32 index range');
222
- }
223
- const ready = [];
224
- for (let i = 0; i < list.length; i++) {
225
- if (list[i].ready()) {
226
- ready.push(i);
232
+ }
233
+ if (ready.length > 0) {
234
+ return new Uint32Array(ready);
235
+ }
236
+ // None ready synchronously. Wait for the first to resolve via Promise.race,
237
+ // then sweep for any others that became ready concurrently.
238
+ return Promise.race(
239
+ list.map((p, i) =>
240
+ p.block().then(() => {
241
+ const result = [i];
242
+ for (let j = 0; j < list.length; j++) {
243
+ if (j !== i && list[j].ready()) {
244
+ result.push(j);
245
+ }
227
246
  }
228
- }
229
- if (ready.length > 0) {
230
- return new Uint32Array(ready);
231
- }
232
- // None ready synchronously. Wait for the first to resolve via Promise.race,
233
- // then sweep for any others that became ready concurrently.
234
- return Promise.race(
235
- list.map((p, i) => p.block().then(() => {
236
- const result = [i];
237
- for (let j = 0; j < list.length; j++) {
238
- if (j !== i && list[j].ready()) {
239
- result.push(j);
240
- }
241
- }
242
- return new Uint32Array(result);
243
- }))
244
- );
247
+ return new Uint32Array(result);
248
+ }),
249
+ ),
250
+ );
245
251
  }
246
252
 
247
253
  function pollOne(poll) {
248
- return poll.block();
254
+ return poll.block();
249
255
  }
250
256
 
251
257
  export const poll = {
252
- Pollable,
253
- pollList,
254
- pollOne,
255
- poll: pollList,
258
+ Pollable,
259
+ pollList,
260
+ pollOne,
261
+ poll: pollList,
256
262
  };
@@ -3,56 +3,54 @@ const MAX_BYTES = 65536;
3
3
  let insecureRandomValue1, insecureRandomValue2;
4
4
 
5
5
  export const insecure = {
6
- getInsecureRandomBytes(len) {
7
- return random.getRandomBytes(len);
8
- },
9
- getInsecureRandomU64() {
10
- return random.getRandomU64();
11
- },
6
+ getInsecureRandomBytes(len) {
7
+ return random.getRandomBytes(len);
8
+ },
9
+ getInsecureRandomU64() {
10
+ return random.getRandomU64();
11
+ },
12
12
  };
13
13
 
14
14
  let insecureSeedValue1, insecureSeedValue2;
15
15
 
16
16
  export const insecureSeed = {
17
- insecureSeed() {
18
- if (insecureSeedValue1 === undefined) {
19
- insecureSeedValue1 = random.getRandomU64();
20
- insecureSeedValue2 = random.getRandomU64();
21
- }
22
- return [insecureSeedValue1, insecureSeedValue2];
23
- },
17
+ insecureSeed() {
18
+ if (insecureSeedValue1 === undefined) {
19
+ insecureSeedValue1 = random.getRandomU64();
20
+ insecureSeedValue2 = random.getRandomU64();
21
+ }
22
+ return [insecureSeedValue1, insecureSeedValue2];
23
+ },
24
24
  };
25
25
 
26
26
  export const random = {
27
- getRandomBytes(len) {
28
- const bytes = new Uint8Array(Number(len));
29
-
30
- if (len > MAX_BYTES) {
31
- // this is the max bytes crypto.getRandomValues
32
- // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
33
- for (var generated = 0; generated < len; generated += MAX_BYTES) {
34
- // buffer.slice automatically checks if the end is past the end of
35
- // the buffer so we don't have to here
36
- crypto.getRandomValues(
37
- bytes.subarray(generated, generated + MAX_BYTES)
38
- );
39
- }
40
- } else {
41
- crypto.getRandomValues(bytes);
42
- }
43
-
44
- return bytes;
45
- },
46
-
47
- getRandomU64() {
48
- return crypto.getRandomValues(new BigUint64Array(1))[0];
49
- },
50
-
51
- insecureRandom() {
52
- if (insecureRandomValue1 === undefined) {
53
- insecureRandomValue1 = random.getRandomU64();
54
- insecureRandomValue2 = random.getRandomU64();
55
- }
56
- return [insecureRandomValue1, insecureRandomValue2];
57
- },
27
+ getRandomBytes(len) {
28
+ const bytes = new Uint8Array(Number(len));
29
+
30
+ if (len > MAX_BYTES) {
31
+ // this is the max bytes crypto.getRandomValues
32
+ // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
33
+ for (var generated = 0; generated < len; generated += MAX_BYTES) {
34
+ // buffer.slice automatically checks if the end is past the end of
35
+ // the buffer so we don't have to here
36
+ crypto.getRandomValues(bytes.subarray(generated, generated + MAX_BYTES));
37
+ }
38
+ } else {
39
+ crypto.getRandomValues(bytes);
40
+ }
41
+
42
+ return bytes;
43
+ },
44
+
45
+ getRandomU64() {
46
+ return crypto.getRandomValues(new BigUint64Array(1))[0];
47
+ },
48
+
49
+ insecureRandom() {
50
+ if (insecureRandomValue1 === undefined) {
51
+ insecureRandomValue1 = random.getRandomU64();
52
+ insecureRandomValue2 = random.getRandomU64();
53
+ }
54
+ return [insecureRandomValue1, insecureRandomValue2];
55
+ },
58
56
  };
@@ -1,88 +1,88 @@
1
1
  export const instanceNetwork = {
2
- instanceNetwork() {
3
- console.log(`[sockets] instance network`);
4
- },
2
+ instanceNetwork() {
3
+ console.log(`[sockets] instance network`);
4
+ },
5
5
  };
6
6
 
7
7
  export const ipNameLookup = {
8
- dropResolveAddressStream() {},
9
- subscribe() {},
10
- resolveAddresses() {},
11
- resolveNextAddress() {},
12
- nonBlocking() {},
13
- setNonBlocking() {},
8
+ dropResolveAddressStream() {},
9
+ subscribe() {},
10
+ resolveAddresses() {},
11
+ resolveNextAddress() {},
12
+ nonBlocking() {},
13
+ setNonBlocking() {},
14
14
  };
15
15
 
16
16
  export const network = {
17
- dropNetwork() {},
17
+ dropNetwork() {},
18
18
  };
19
19
 
20
20
  export const tcpCreateSocket = {
21
- createTcpSocket() {},
21
+ createTcpSocket() {},
22
22
  };
23
23
 
24
24
  export const tcp = {
25
- subscribe() {},
26
- dropTcpSocket() {},
27
- bind() {},
28
- connect() {},
29
- listen() {},
30
- accept() {},
31
- localAddress() {},
32
- remoteAddress() {},
33
- addressFamily() {},
34
- setListenBacklogSize() {},
35
- keepAlive() {},
36
- setKeepAlive() {},
37
- noDelay() {},
38
- setNoDelay() {},
39
- unicastHopLimit() {},
40
- setUnicastHopLimit() {},
41
- receiveBufferSize() {},
42
- setReceiveBufferSize() {},
43
- sendBufferSize() {},
44
- setSendBufferSize() {},
45
- nonBlocking() {},
46
- setNonBlocking() {},
47
- shutdown() {},
25
+ subscribe() {},
26
+ dropTcpSocket() {},
27
+ bind() {},
28
+ connect() {},
29
+ listen() {},
30
+ accept() {},
31
+ localAddress() {},
32
+ remoteAddress() {},
33
+ addressFamily() {},
34
+ setListenBacklogSize() {},
35
+ keepAlive() {},
36
+ setKeepAlive() {},
37
+ noDelay() {},
38
+ setNoDelay() {},
39
+ unicastHopLimit() {},
40
+ setUnicastHopLimit() {},
41
+ receiveBufferSize() {},
42
+ setReceiveBufferSize() {},
43
+ sendBufferSize() {},
44
+ setSendBufferSize() {},
45
+ nonBlocking() {},
46
+ setNonBlocking() {},
47
+ shutdown() {},
48
48
  };
49
49
 
50
50
  export const udp = {
51
- subscribe() {},
51
+ subscribe() {},
52
52
 
53
- dropUdpSocket() {},
53
+ dropUdpSocket() {},
54
54
 
55
- bind() {},
55
+ bind() {},
56
56
 
57
- connect() {},
57
+ connect() {},
58
58
 
59
- receive() {},
59
+ receive() {},
60
60
 
61
- send() {},
61
+ send() {},
62
62
 
63
- localAddress() {},
63
+ localAddress() {},
64
64
 
65
- remoteAddress() {},
65
+ remoteAddress() {},
66
66
 
67
- addressFamily() {},
67
+ addressFamily() {},
68
68
 
69
- unicastHopLimit() {},
69
+ unicastHopLimit() {},
70
70
 
71
- setUnicastHopLimit() {},
71
+ setUnicastHopLimit() {},
72
72
 
73
- receiveBufferSize() {},
73
+ receiveBufferSize() {},
74
74
 
75
- setReceiveBufferSize() {},
75
+ setReceiveBufferSize() {},
76
76
 
77
- sendBufferSize() {},
77
+ sendBufferSize() {},
78
78
 
79
- setSendBufferSize() {},
79
+ setSendBufferSize() {},
80
80
 
81
- nonBlocking() {},
81
+ nonBlocking() {},
82
82
 
83
- setNonBlocking() {},
83
+ setNonBlocking() {},
84
84
  };
85
85
 
86
86
  export const udpCreateSocket = {
87
- createUdpSocket() {},
87
+ createUdpSocket() {},
88
88
  };