@bytecodealliance/preview2-shim 0.17.7 → 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,37 +1,37 @@
1
1
  import {
2
- createFuture,
3
- createReadableStream,
4
- createReadableStreamPollState,
5
- createWritableStream,
6
- futureDispose,
7
- futureTakeValue,
8
- pollStateReady,
9
- verifyPollsDroppedForDrop,
10
- } from './worker-thread.js';
11
- import process from 'node:process';
12
- const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
2
+ createFuture,
3
+ createReadableStream,
4
+ createReadableStreamPollState,
5
+ createWritableStream,
6
+ futureDispose,
7
+ futureTakeValue,
8
+ pollStateReady,
9
+ verifyPollsDroppedForDrop,
10
+ } from "./worker-thread.js";
11
+ import process from "node:process";
12
+ const { TCP, constants: TCPConstants } = process.binding("tcp_wrap");
13
13
  import {
14
- convertSocketError,
15
- convertSocketErrorCode,
16
- ipSocketAddress,
17
- isIPv4MappedAddress,
18
- isMulticastIpAddress,
19
- isUnicastIpAddress,
20
- isWildcardAddress,
21
- noLookup,
22
- serializeIpAddress,
23
- SOCKET_STATE_BIND,
24
- SOCKET_STATE_BOUND,
25
- SOCKET_STATE_CLOSED,
26
- SOCKET_STATE_CONNECT,
27
- SOCKET_STATE_CONNECTION,
28
- SOCKET_STATE_INIT,
29
- SOCKET_STATE_LISTEN,
30
- SOCKET_STATE_LISTENER,
31
- } from './worker-sockets.js';
32
- import { Socket, Server } from 'node:net';
14
+ convertSocketError,
15
+ convertSocketErrorCode,
16
+ ipSocketAddress,
17
+ isIPv4MappedAddress,
18
+ isMulticastIpAddress,
19
+ isUnicastIpAddress,
20
+ isWildcardAddress,
21
+ noLookup,
22
+ serializeIpAddress,
23
+ SOCKET_STATE_BIND,
24
+ SOCKET_STATE_BOUND,
25
+ SOCKET_STATE_CLOSED,
26
+ SOCKET_STATE_CONNECT,
27
+ SOCKET_STATE_CONNECTION,
28
+ SOCKET_STATE_INIT,
29
+ SOCKET_STATE_LISTEN,
30
+ SOCKET_STATE_LISTENER,
31
+ } from "./worker-sockets.js";
32
+ import { Socket, Server } from "node:net";
33
33
 
34
- const win = process.platform === 'win32';
34
+ const win = process.platform === "win32";
35
35
 
36
36
  /**
37
37
  * @typedef {import("../../types/interfaces/wasi-sockets-network.js").IpSocketAddress} IpSocketAddress
@@ -66,278 +66,266 @@ let tcpSocketCnt = 0;
66
66
  * @param {IpAddressFamily} addressFamily
67
67
  */
68
68
  export function createTcpSocket() {
69
- const handle = new TCP(TCPConstants.SOCKET);
70
- tcpSockets.set(++tcpSocketCnt, {
71
- state: SOCKET_STATE_INIT,
72
- future: null,
73
- tcpSocket: null,
74
- listenBacklogSize: 128,
75
- handle,
76
- pendingAccepts: [],
77
- pollState: {
78
- ready: true,
79
- listener: null,
80
- polls: [],
81
- parentStream: null,
82
- },
83
- });
84
- return tcpSocketCnt;
69
+ const handle = new TCP(TCPConstants.SOCKET);
70
+ tcpSockets.set(++tcpSocketCnt, {
71
+ state: SOCKET_STATE_INIT,
72
+ future: null,
73
+ tcpSocket: null,
74
+ listenBacklogSize: 128,
75
+ handle,
76
+ pendingAccepts: [],
77
+ pollState: {
78
+ ready: true,
79
+ listener: null,
80
+ polls: [],
81
+ parentStream: null,
82
+ },
83
+ });
84
+ return tcpSocketCnt;
85
85
  }
86
86
 
87
87
  export function socketTcpFinish(id, fromState, toState) {
88
- const socket = tcpSockets.get(id);
89
- if (socket.state !== fromState) {
90
- throw 'not-in-progress';
91
- }
92
- if (!socket.pollState.ready) {
93
- throw 'would-block';
94
- }
95
- const { tag, val } = futureTakeValue(socket.future).val;
96
- futureDispose(socket.future, false);
97
- socket.future = null;
98
- if (tag === 'err') {
99
- socket.state = SOCKET_STATE_CLOSED;
100
- throw val;
101
- } else {
102
- socket.state = toState;
103
- // for the listener, we must immediately transition back to unresolved
104
- if (toState === SOCKET_STATE_LISTENER) {
105
- socket.pollState.ready = false;
106
- }
107
- return val;
88
+ const socket = tcpSockets.get(id);
89
+ if (socket.state !== fromState) {
90
+ throw "not-in-progress";
91
+ }
92
+ if (!socket.pollState.ready) {
93
+ throw "would-block";
94
+ }
95
+ const { tag, val } = futureTakeValue(socket.future).val;
96
+ futureDispose(socket.future, false);
97
+ socket.future = null;
98
+ if (tag === "err") {
99
+ socket.state = SOCKET_STATE_CLOSED;
100
+ throw val;
101
+ } else {
102
+ socket.state = toState;
103
+ // for the listener, we must immediately transition back to unresolved
104
+ if (toState === SOCKET_STATE_LISTENER) {
105
+ socket.pollState.ready = false;
108
106
  }
107
+ return val;
108
+ }
109
109
  }
110
110
 
111
111
  export function socketTcpBindStart(id, localAddress, family) {
112
- const socket = tcpSockets.get(id);
113
- if (socket.state !== SOCKET_STATE_INIT) {
114
- throw 'invalid-state';
115
- }
116
- if (
117
- family !== localAddress.tag ||
118
- !isUnicastIpAddress(localAddress) ||
119
- isIPv4MappedAddress(localAddress)
120
- ) {
121
- throw 'invalid-argument';
122
- }
123
- socket.state = SOCKET_STATE_BIND;
124
- const { handle } = socket;
125
- socket.future = createFuture(
126
- (async () => {
127
- const address = serializeIpAddress(localAddress);
128
- const port = localAddress.val.port;
129
- const code =
130
- localAddress.tag === 'ipv6'
131
- ? handle.bind6(address, port, TCPConstants.UV_TCP_IPV6ONLY)
132
- : handle.bind(address, port);
133
- if (code !== 0) {
134
- throw convertSocketErrorCode(-code);
135
- }
136
- // This is a Node.js / libuv quirk to force the bind error to be thrown
137
- // (specifically address-in-use).
138
- {
139
- const out = {};
140
- const code = handle.getsockname(out);
141
- if (code !== 0) {
142
- throw convertSocketErrorCode(-code);
143
- }
144
- }
145
- })(),
146
- socket.pollState
147
- );
112
+ const socket = tcpSockets.get(id);
113
+ if (socket.state !== SOCKET_STATE_INIT) {
114
+ throw "invalid-state";
115
+ }
116
+ if (
117
+ family !== localAddress.tag ||
118
+ !isUnicastIpAddress(localAddress) ||
119
+ isIPv4MappedAddress(localAddress)
120
+ ) {
121
+ throw "invalid-argument";
122
+ }
123
+ socket.state = SOCKET_STATE_BIND;
124
+ const { handle } = socket;
125
+ socket.future = createFuture(
126
+ (async () => {
127
+ const address = serializeIpAddress(localAddress);
128
+ const port = localAddress.val.port;
129
+ const code =
130
+ localAddress.tag === "ipv6"
131
+ ? handle.bind6(address, port, TCPConstants.UV_TCP_IPV6ONLY)
132
+ : handle.bind(address, port);
133
+ if (code !== 0) {
134
+ throw convertSocketErrorCode(-code);
135
+ }
136
+ // This is a Node.js / libuv quirk to force the bind error to be thrown
137
+ // (specifically address-in-use).
138
+ {
139
+ const out = {};
140
+ const code = handle.getsockname(out);
141
+ if (code !== 0) {
142
+ throw convertSocketErrorCode(-code);
143
+ }
144
+ }
145
+ })(),
146
+ socket.pollState,
147
+ );
148
148
  }
149
149
 
150
150
  export function socketTcpConnectStart(id, remoteAddress, family) {
151
- const socket = tcpSockets.get(id);
152
- if (
153
- socket.state !== SOCKET_STATE_INIT &&
154
- socket.state !== SOCKET_STATE_BOUND
155
- ) {
156
- throw 'invalid-state';
157
- }
158
- if (
159
- isWildcardAddress(remoteAddress) ||
160
- family !== remoteAddress.tag ||
161
- !isUnicastIpAddress(remoteAddress) ||
162
- isMulticastIpAddress(remoteAddress) ||
163
- remoteAddress.val.port === 0 ||
164
- isIPv4MappedAddress(remoteAddress)
165
- ) {
166
- throw 'invalid-argument';
167
- }
168
- socket.state = SOCKET_STATE_CONNECT;
169
- socket.future = createFuture(
170
- new Promise((resolve, reject) => {
171
- const tcpSocket = (socket.tcpSocket = new Socket({
172
- handle: socket.handle,
173
- pauseOnCreate: true,
174
- allowHalfOpen: true,
175
- }));
176
- function handleErr(err) {
177
- tcpSocket.off('connect', handleConnect);
178
- reject(convertSocketError(err));
179
- }
180
- function handleConnect() {
181
- tcpSocket.off('error', handleErr);
182
- resolve([
183
- createReadableStream(tcpSocket),
184
- createWritableStream(tcpSocket),
185
- ]);
186
- }
187
- tcpSocket.once('connect', handleConnect);
188
- tcpSocket.once('error', handleErr);
189
- tcpSocket.connect({
190
- port: remoteAddress.val.port,
191
- host: serializeIpAddress(remoteAddress),
192
- lookup: noLookup,
193
- });
194
- }),
195
- socket.pollState
196
- );
151
+ const socket = tcpSockets.get(id);
152
+ if (socket.state !== SOCKET_STATE_INIT && socket.state !== SOCKET_STATE_BOUND) {
153
+ throw "invalid-state";
154
+ }
155
+ if (
156
+ isWildcardAddress(remoteAddress) ||
157
+ family !== remoteAddress.tag ||
158
+ !isUnicastIpAddress(remoteAddress) ||
159
+ isMulticastIpAddress(remoteAddress) ||
160
+ remoteAddress.val.port === 0 ||
161
+ isIPv4MappedAddress(remoteAddress)
162
+ ) {
163
+ throw "invalid-argument";
164
+ }
165
+ socket.state = SOCKET_STATE_CONNECT;
166
+ socket.future = createFuture(
167
+ new Promise((resolve, reject) => {
168
+ const tcpSocket = (socket.tcpSocket = new Socket({
169
+ handle: socket.handle,
170
+ pauseOnCreate: true,
171
+ allowHalfOpen: true,
172
+ }));
173
+ function handleErr(err) {
174
+ tcpSocket.off("connect", handleConnect);
175
+ reject(convertSocketError(err));
176
+ }
177
+ function handleConnect() {
178
+ tcpSocket.off("error", handleErr);
179
+ resolve([createReadableStream(tcpSocket), createWritableStream(tcpSocket)]);
180
+ }
181
+ tcpSocket.once("connect", handleConnect);
182
+ tcpSocket.once("error", handleErr);
183
+ tcpSocket.connect({
184
+ port: remoteAddress.val.port,
185
+ host: serializeIpAddress(remoteAddress),
186
+ lookup: noLookup,
187
+ });
188
+ }),
189
+ socket.pollState,
190
+ );
197
191
  }
198
192
 
199
193
  export function socketTcpListenStart(id) {
200
- const socket = tcpSockets.get(id);
201
- if (socket.state !== SOCKET_STATE_BOUND) {
202
- throw 'invalid-state';
203
- }
204
- const { handle } = socket;
205
- socket.state = SOCKET_STATE_LISTEN;
206
- socket.future = createFuture(
207
- new Promise((resolve, reject) => {
208
- const server = new Server({
209
- pauseOnConnect: true,
210
- allowHalfOpen: true,
211
- });
212
- function handleErr(err) {
213
- server.off('listening', handleListen);
214
- reject(convertSocketError(err));
215
- }
216
- function handleListen() {
217
- server.off('error', handleErr);
218
- server.on('connection', (tcpSocket) => {
219
- pollStateReady(socket.pollState);
220
- const pollState = createReadableStreamPollState(tcpSocket);
221
- socket.pendingAccepts.push({
222
- tcpSocket,
223
- err: null,
224
- pollState,
225
- });
226
- });
227
- server.on('error', (err) => {
228
- pollStateReady(socket.pollState);
229
- socket.pendingAccepts.push({
230
- tcpSocket: null,
231
- err,
232
- pollState: null,
233
- });
234
- });
235
- resolve();
236
- }
237
- server.once('listening', handleListen);
238
- server.once('error', handleErr);
239
- server.listen(handle, socket.listenBacklogSize);
240
- }),
241
- socket.pollState
242
- );
194
+ const socket = tcpSockets.get(id);
195
+ if (socket.state !== SOCKET_STATE_BOUND) {
196
+ throw "invalid-state";
197
+ }
198
+ const { handle } = socket;
199
+ socket.state = SOCKET_STATE_LISTEN;
200
+ socket.future = createFuture(
201
+ new Promise((resolve, reject) => {
202
+ const server = new Server({
203
+ pauseOnConnect: true,
204
+ allowHalfOpen: true,
205
+ });
206
+ function handleErr(err) {
207
+ server.off("listening", handleListen);
208
+ reject(convertSocketError(err));
209
+ }
210
+ function handleListen() {
211
+ server.off("error", handleErr);
212
+ server.on("connection", (tcpSocket) => {
213
+ pollStateReady(socket.pollState);
214
+ const pollState = createReadableStreamPollState(tcpSocket);
215
+ socket.pendingAccepts.push({
216
+ tcpSocket,
217
+ err: null,
218
+ pollState,
219
+ });
220
+ });
221
+ server.on("error", (err) => {
222
+ pollStateReady(socket.pollState);
223
+ socket.pendingAccepts.push({
224
+ tcpSocket: null,
225
+ err,
226
+ pollState: null,
227
+ });
228
+ });
229
+ resolve();
230
+ }
231
+ server.once("listening", handleListen);
232
+ server.once("error", handleErr);
233
+ server.listen(handle, socket.listenBacklogSize);
234
+ }),
235
+ socket.pollState,
236
+ );
243
237
  }
244
238
 
245
239
  export function socketTcpAccept(id) {
246
- const socket = tcpSockets.get(id);
247
- if (socket.state !== SOCKET_STATE_LISTENER) {
248
- throw 'invalid-state';
249
- }
250
- if (socket.pendingAccepts.length === 0) {
251
- throw 'would-block';
252
- }
253
- const accept = socket.pendingAccepts.shift();
254
- if (accept.err) {
255
- socket.state = SOCKET_STATE_CLOSED;
256
- throw convertSocketError(accept.err);
257
- }
258
- if (socket.pendingAccepts.length === 0) {
259
- socket.pollState.ready = false;
260
- }
261
- tcpSockets.set(++tcpSocketCnt, {
262
- state: SOCKET_STATE_CONNECTION,
263
- future: null,
264
- tcpSocket: accept.tcpSocket,
265
- listenBacklogSize: 128,
266
- handle: accept.tcpSocket._handle,
267
- pendingAccepts: [],
268
- pollState: accept.pollState,
269
- });
270
- return [
271
- tcpSocketCnt,
272
- createReadableStream(accept.tcpSocket, accept.pollState),
273
- createWritableStream(accept.tcpSocket),
274
- ];
240
+ const socket = tcpSockets.get(id);
241
+ if (socket.state !== SOCKET_STATE_LISTENER) {
242
+ throw "invalid-state";
243
+ }
244
+ if (socket.pendingAccepts.length === 0) {
245
+ throw "would-block";
246
+ }
247
+ const accept = socket.pendingAccepts.shift();
248
+ if (accept.err) {
249
+ socket.state = SOCKET_STATE_CLOSED;
250
+ throw convertSocketError(accept.err);
251
+ }
252
+ if (socket.pendingAccepts.length === 0) {
253
+ socket.pollState.ready = false;
254
+ }
255
+ tcpSockets.set(++tcpSocketCnt, {
256
+ state: SOCKET_STATE_CONNECTION,
257
+ future: null,
258
+ tcpSocket: accept.tcpSocket,
259
+ listenBacklogSize: 128,
260
+ handle: accept.tcpSocket._handle,
261
+ pendingAccepts: [],
262
+ pollState: accept.pollState,
263
+ });
264
+ return [
265
+ tcpSocketCnt,
266
+ createReadableStream(accept.tcpSocket, accept.pollState),
267
+ createWritableStream(accept.tcpSocket),
268
+ ];
275
269
  }
276
270
 
277
271
  export function socketTcpSetListenBacklogSize(id, backlogSize) {
278
- const socket = tcpSockets.get(id);
279
- if (
280
- socket.state === SOCKET_STATE_LISTEN ||
281
- socket.state === SOCKET_STATE_LISTENER
282
- ) {
283
- throw 'not-supported';
284
- }
285
- if (
286
- socket.state !== SOCKET_STATE_INIT &&
287
- socket.state !== SOCKET_STATE_BIND &&
288
- socket.state !== SOCKET_STATE_BOUND
289
- ) {
290
- throw 'invalid-state';
291
- }
292
- socket.listenBacklogSize = Number(backlogSize);
272
+ const socket = tcpSockets.get(id);
273
+ if (socket.state === SOCKET_STATE_LISTEN || socket.state === SOCKET_STATE_LISTENER) {
274
+ throw "not-supported";
275
+ }
276
+ if (
277
+ socket.state !== SOCKET_STATE_INIT &&
278
+ socket.state !== SOCKET_STATE_BIND &&
279
+ socket.state !== SOCKET_STATE_BOUND
280
+ ) {
281
+ throw "invalid-state";
282
+ }
283
+ socket.listenBacklogSize = Number(backlogSize);
293
284
  }
294
285
 
295
286
  export function socketTcpGetLocalAddress(id) {
296
- const { handle } = tcpSockets.get(id);
297
- const out = {};
298
- const code = handle.getsockname(out);
299
- if (code !== 0) {
300
- throw convertSocketErrorCode(-code);
301
- }
302
- return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
287
+ const { handle } = tcpSockets.get(id);
288
+ const out = {};
289
+ const code = handle.getsockname(out);
290
+ if (code !== 0) {
291
+ throw convertSocketErrorCode(-code);
292
+ }
293
+ return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
303
294
  }
304
295
 
305
296
  export function socketTcpGetRemoteAddress(id) {
306
- const { handle } = tcpSockets.get(id);
307
- const out = {};
308
- const code = handle.getpeername(out);
309
- if (code !== 0) {
310
- throw convertSocketErrorCode(-code);
311
- }
312
- return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
297
+ const { handle } = tcpSockets.get(id);
298
+ const out = {};
299
+ const code = handle.getpeername(out);
300
+ if (code !== 0) {
301
+ throw convertSocketErrorCode(-code);
302
+ }
303
+ return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
313
304
  }
314
305
 
315
306
  export function socketTcpShutdown(id, _shutdownType) {
316
- const socket = tcpSockets.get(id);
317
- if (socket.state !== SOCKET_STATE_CONNECTION) {
318
- throw 'invalid-state';
319
- }
320
- if (win && socket.tcpSocket.destroySoon) {
321
- socket.tcpSocket.destroySoon();
322
- } else {
323
- socket.tcpSocket.destroy();
324
- }
307
+ const socket = tcpSockets.get(id);
308
+ if (socket.state !== SOCKET_STATE_CONNECTION) {
309
+ throw "invalid-state";
310
+ }
311
+ if (win && socket.tcpSocket.destroySoon) {
312
+ socket.tcpSocket.destroySoon();
313
+ } else {
314
+ socket.tcpSocket.destroy();
315
+ }
325
316
  }
326
317
 
327
318
  export function socketTcpSetKeepAlive(id, { keepAlive, keepAliveIdleTime }) {
328
- const { handle } = tcpSockets.get(id);
329
- const code = handle.setKeepAlive(
330
- keepAlive,
331
- Number(keepAliveIdleTime / 1_000_000_000n)
332
- );
333
- if (code !== 0) {
334
- throw convertSocketErrorCode(-code);
335
- }
319
+ const { handle } = tcpSockets.get(id);
320
+ const code = handle.setKeepAlive(keepAlive, Number(keepAliveIdleTime / 1_000_000_000n));
321
+ if (code !== 0) {
322
+ throw convertSocketErrorCode(-code);
323
+ }
336
324
  }
337
325
 
338
326
  export function socketTcpDispose(id) {
339
- const socket = tcpSockets.get(id);
340
- verifyPollsDroppedForDrop(socket.pollState, 'tcp socket');
341
- socket.handle.close();
342
- tcpSockets.delete(id);
327
+ const socket = tcpSockets.get(id);
328
+ verifyPollsDroppedForDrop(socket.pollState, "tcp socket");
329
+ socket.handle.close();
330
+ tcpSockets.delete(id);
343
331
  }