@bytecodealliance/preview2-shim 0.17.2 → 0.17.4
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.
- package/README.md +47 -0
- package/lib/browser/cli.js +76 -103
- package/lib/browser/clocks.js +30 -29
- package/lib/browser/config.js +6 -0
- package/lib/browser/environment.js +29 -0
- package/lib/browser/filesystem.js +299 -255
- package/lib/browser/http.js +129 -128
- package/lib/browser/index.js +8 -16
- package/lib/browser/io.js +143 -135
- package/lib/browser/random.js +44 -42
- package/lib/browser/sockets.js +68 -166
- package/lib/common/instantiation.js +134 -0
- package/lib/io/calls.js +8 -5
- package/lib/io/worker-http.js +179 -157
- package/lib/io/worker-io.js +402 -386
- package/lib/io/worker-socket-tcp.js +271 -219
- package/lib/io/worker-socket-udp.js +494 -429
- package/lib/io/worker-sockets.js +255 -241
- package/lib/io/worker-thread.js +837 -754
- package/lib/nodejs/cli.js +64 -63
- package/lib/nodejs/clocks.js +51 -45
- package/lib/nodejs/filesystem.js +785 -651
- package/lib/nodejs/http.js +697 -617
- package/lib/nodejs/index.js +8 -16
- package/lib/nodejs/random.js +32 -28
- package/lib/nodejs/sockets.js +538 -474
- package/lib/synckit/index.js +94 -85
- package/package.json +11 -5
- package/types/index.d.ts +0 -1
- package/types/instantiation.d.ts +136 -0
package/lib/io/worker-http.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { createReadableStream, getStreamOrThrow } from
|
|
1
|
+
import { createReadableStream, getStreamOrThrow } from './worker-thread.js';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from
|
|
7
|
-
import { request as httpsRequest, Agent as HttpsAgent } from
|
|
8
|
-
import { parentPort } from
|
|
9
|
-
import { HTTP_SERVER_INCOMING_HANDLER } from
|
|
3
|
+
createServer,
|
|
4
|
+
request as httpRequest,
|
|
5
|
+
Agent as HttpAgent,
|
|
6
|
+
} from 'node:http';
|
|
7
|
+
import { request as httpsRequest, Agent as HttpsAgent } from 'node:https';
|
|
8
|
+
import { parentPort } from 'node:worker_threads';
|
|
9
|
+
import { HTTP_SERVER_INCOMING_HANDLER } from './calls.js';
|
|
10
10
|
|
|
11
11
|
const agentOptions = {
|
|
12
|
-
|
|
12
|
+
keepAlive: true,
|
|
13
13
|
};
|
|
14
14
|
const httpAgent = new HttpAgent(agentOptions);
|
|
15
15
|
const httpsAgent = new HttpsAgent(agentOptions);
|
|
@@ -20,176 +20,198 @@ let responseCnt = 0;
|
|
|
20
20
|
const responses = new Map();
|
|
21
21
|
|
|
22
22
|
export async function stopHttpServer(id) {
|
|
23
|
-
|
|
23
|
+
await new Promise((resolve) => servers.get(id).close(resolve));
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export function clearOutgoingResponse(id) {
|
|
27
|
-
|
|
27
|
+
responses.delete(id);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function getHttpServerAddress(id) {
|
|
31
|
+
return servers.get(id).address();
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
export async function setOutgoingResponse(
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
id,
|
|
36
|
+
{ statusCode, headers, streamId }
|
|
33
37
|
) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
const response = responses.get(id);
|
|
39
|
+
const textDecoder = new TextDecoder();
|
|
40
|
+
response.writeHead(
|
|
41
|
+
statusCode,
|
|
42
|
+
Object.fromEntries(
|
|
43
|
+
headers.map(([key, val]) => [key, textDecoder.decode(val)])
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
response.flushHeaders();
|
|
47
|
+
const { stream } = getStreamOrThrow(streamId);
|
|
48
|
+
stream.pipe(response);
|
|
49
|
+
responses.delete(id);
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
export async function startHttpServer(id, { port, host }) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
const server = createServer((req, res) => {
|
|
54
|
+
// create the streams and their ids
|
|
55
|
+
const streamId = createReadableStream(req);
|
|
56
|
+
const responseId = ++responseCnt;
|
|
57
|
+
parentPort.postMessage({
|
|
58
|
+
type: HTTP_SERVER_INCOMING_HANDLER,
|
|
59
|
+
id,
|
|
60
|
+
payload: {
|
|
61
|
+
responseId,
|
|
62
|
+
method: req.method,
|
|
63
|
+
host: req.headers.host || host || 'localhost',
|
|
64
|
+
pathWithQuery: req.url,
|
|
65
|
+
headers: Object.entries(req.headersDistinct).flatMap(
|
|
66
|
+
([key, val]) => val.map((val) => [key, val])
|
|
67
|
+
),
|
|
68
|
+
streamId,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
responses.set(responseId, res);
|
|
66
72
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
73
|
-
servers.set(id, server);
|
|
73
|
+
await new Promise((resolve, reject) => {
|
|
74
|
+
server.listen(port, host, resolve);
|
|
75
|
+
server.on('error', reject);
|
|
76
|
+
});
|
|
77
|
+
servers.set(id, server);
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
export async function createHttpRequest(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
method,
|
|
82
|
+
scheme,
|
|
83
|
+
authority,
|
|
84
|
+
pathWithQuery,
|
|
85
|
+
headers,
|
|
86
|
+
bodyId,
|
|
87
|
+
connectTimeout,
|
|
88
|
+
betweenBytesTimeout,
|
|
89
|
+
firstByteTimeout
|
|
86
90
|
) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
case "http:":
|
|
111
|
-
req = httpRequest({
|
|
112
|
-
agent: httpAgent,
|
|
113
|
-
method,
|
|
114
|
-
host: authority.split(":")[0],
|
|
115
|
-
port: authority.split(":")[1],
|
|
116
|
-
path: pathWithQuery,
|
|
117
|
-
timeout: connectTimeout && Number(connectTimeout / 1_000_000n),
|
|
118
|
-
});
|
|
119
|
-
break;
|
|
120
|
-
case "https:":
|
|
121
|
-
req = httpsRequest({
|
|
122
|
-
agent: httpsAgent,
|
|
123
|
-
method,
|
|
124
|
-
host: authority.split(":")[0],
|
|
125
|
-
port: authority.split(":")[1],
|
|
126
|
-
path: pathWithQuery,
|
|
127
|
-
timeout: connectTimeout && Number(connectTimeout / 1_000_000n),
|
|
128
|
-
});
|
|
129
|
-
break;
|
|
130
|
-
default:
|
|
131
|
-
throw { tag: "HTTP-protocol-error" };
|
|
132
|
-
}
|
|
133
|
-
for (const [key, value] of headers) {
|
|
134
|
-
req.appendHeader(key, value);
|
|
135
|
-
}
|
|
136
|
-
req.flushHeaders();
|
|
137
|
-
if (stream) {
|
|
138
|
-
stream.pipe(req);
|
|
139
|
-
} else {
|
|
140
|
-
req.end();
|
|
91
|
+
let stream = null;
|
|
92
|
+
if (bodyId) {
|
|
93
|
+
try {
|
|
94
|
+
({ stream } = getStreamOrThrow(bodyId));
|
|
95
|
+
} catch (e) {
|
|
96
|
+
if (e.tag === 'closed') {
|
|
97
|
+
throw {
|
|
98
|
+
tag: 'internal-error',
|
|
99
|
+
val: 'Unexpected closed body stream',
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// it should never be possible for the body stream to already
|
|
103
|
+
// be closed, or for there to be a write error
|
|
104
|
+
// we therefore just throw internal error here
|
|
105
|
+
if (e.tag === 'last-operation-failed') {
|
|
106
|
+
throw {
|
|
107
|
+
tag: 'internal-error',
|
|
108
|
+
val: e.val,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// entirely unknown error -> trap
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
141
114
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
115
|
+
try {
|
|
116
|
+
// Make a request
|
|
117
|
+
let req;
|
|
118
|
+
switch (scheme) {
|
|
119
|
+
case 'http:':
|
|
120
|
+
req = httpRequest({
|
|
121
|
+
agent: httpAgent,
|
|
122
|
+
method,
|
|
123
|
+
host: authority.split(':')[0],
|
|
124
|
+
port: authority.split(':')[1],
|
|
125
|
+
path: pathWithQuery,
|
|
126
|
+
timeout:
|
|
127
|
+
connectTimeout && Number(connectTimeout / 1_000_000n),
|
|
128
|
+
});
|
|
129
|
+
break;
|
|
130
|
+
case 'https:':
|
|
131
|
+
req = httpsRequest({
|
|
132
|
+
agent: httpsAgent,
|
|
133
|
+
method,
|
|
134
|
+
host: authority.split(':')[0],
|
|
135
|
+
port: authority.split(':')[1],
|
|
136
|
+
path: pathWithQuery,
|
|
137
|
+
timeout:
|
|
138
|
+
connectTimeout && Number(connectTimeout / 1_000_000n),
|
|
139
|
+
});
|
|
140
|
+
break;
|
|
141
|
+
default:
|
|
142
|
+
throw { tag: 'HTTP-protocol-error' };
|
|
143
|
+
}
|
|
144
|
+
for (const [key, value] of headers) {
|
|
145
|
+
req.appendHeader(key, value);
|
|
146
|
+
}
|
|
147
|
+
req.flushHeaders();
|
|
148
|
+
if (stream) {
|
|
149
|
+
stream.pipe(req);
|
|
150
|
+
} else {
|
|
151
|
+
req.end();
|
|
152
|
+
}
|
|
153
|
+
const res = await new Promise((resolve, reject) => {
|
|
154
|
+
req.once('timeout', () => {
|
|
155
|
+
reject({
|
|
156
|
+
tag: 'connection-timeout',
|
|
157
|
+
});
|
|
158
|
+
req.destroy();
|
|
159
|
+
});
|
|
160
|
+
req.once('response', resolve);
|
|
161
|
+
req.once('close', () => reject);
|
|
162
|
+
req.once('error', reject);
|
|
146
163
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
status: res.statusCode,
|
|
161
|
-
headers: Array.from(Object.entries(res.headers)),
|
|
162
|
-
bodyStreamId,
|
|
163
|
-
};
|
|
164
|
-
} catch (e) {
|
|
165
|
-
if (e?.tag) throw e;
|
|
166
|
-
const err = getFirstError(e);
|
|
167
|
-
switch (err.code) {
|
|
168
|
-
case "ECONNRESET":
|
|
169
|
-
throw { tag: "HTTP-protocol-error" };
|
|
170
|
-
case "ENOTFOUND":
|
|
171
|
-
throw {
|
|
172
|
-
tag: "DNS-error",
|
|
173
|
-
val: {
|
|
174
|
-
rcode: err.code,
|
|
175
|
-
infoCode: err.errno < 0 ? -err.errno : err.errno,
|
|
176
|
-
},
|
|
164
|
+
if (firstByteTimeout) {
|
|
165
|
+
res.setTimeout(Number(firstByteTimeout / 1_000_000n));
|
|
166
|
+
}
|
|
167
|
+
if (betweenBytesTimeout) {
|
|
168
|
+
res.once('readable', () => {
|
|
169
|
+
res.setTimeout(Number(betweenBytesTimeout / 1_000_000n));
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
const bodyStreamId = createReadableStream(res);
|
|
173
|
+
return {
|
|
174
|
+
status: res.statusCode,
|
|
175
|
+
headers: Array.from(Object.entries(res.headers)),
|
|
176
|
+
bodyStreamId,
|
|
177
177
|
};
|
|
178
|
-
|
|
178
|
+
} catch (e) {
|
|
179
|
+
if (e?.tag) {
|
|
180
|
+
throw e;
|
|
181
|
+
}
|
|
182
|
+
const err = getFirstError(e);
|
|
183
|
+
switch (err.code) {
|
|
184
|
+
case 'ECONNRESET':
|
|
185
|
+
throw { tag: 'HTTP-protocol-error' };
|
|
186
|
+
case 'ENOTFOUND':
|
|
187
|
+
throw {
|
|
188
|
+
tag: 'DNS-error',
|
|
189
|
+
val: {
|
|
190
|
+
rcode: err.code,
|
|
191
|
+
infoCode: err.errno < 0 ? -err.errno : err.errno,
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
case 'ECONNREFUSED':
|
|
195
|
+
throw {
|
|
196
|
+
tag: 'connection-refused',
|
|
197
|
+
};
|
|
198
|
+
}
|
|
179
199
|
throw {
|
|
180
|
-
|
|
200
|
+
tag: 'internal-error',
|
|
201
|
+
val: err.toString(),
|
|
181
202
|
};
|
|
182
203
|
}
|
|
183
|
-
throw {
|
|
184
|
-
tag: "internal-error",
|
|
185
|
-
val: err.toString(),
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
204
|
}
|
|
189
205
|
|
|
190
206
|
function getFirstError(e) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
207
|
+
if (typeof e !== 'object' || e === null) {
|
|
208
|
+
return e;
|
|
209
|
+
}
|
|
210
|
+
if (e.cause) {
|
|
211
|
+
return getFirstError(e.cause);
|
|
212
|
+
}
|
|
213
|
+
if (e instanceof AggregateError) {
|
|
214
|
+
return getFirstError(e.errors[0]);
|
|
215
|
+
}
|
|
216
|
+
return e;
|
|
195
217
|
}
|