@bytecodealliance/preview2-shim 0.14.1 → 0.15.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.
- package/README.md +2 -2
- package/lib/browser/filesystem.js +6 -5
- package/lib/browser/random.js +1 -1
- package/lib/browser/sockets.js +0 -14
- package/lib/io/calls.js +80 -16
- package/lib/io/worker-http.js +164 -67
- package/lib/io/worker-io.js +207 -68
- package/lib/io/worker-socket-tcp.js +285 -0
- package/lib/io/worker-socket-udp.js +576 -0
- package/lib/io/worker-sockets.js +371 -0
- package/lib/io/worker-thread.js +793 -399
- package/lib/nodejs/cli.js +29 -13
- package/lib/nodejs/clocks.js +9 -6
- package/lib/nodejs/filesystem.js +170 -57
- package/lib/nodejs/http.js +662 -531
- package/lib/nodejs/index.js +0 -3
- package/lib/nodejs/sockets.js +571 -11
- package/lib/synckit/index.js +25 -41
- package/package.json +2 -2
- package/types/interfaces/wasi-http-types.d.ts +53 -41
- package/types/interfaces/wasi-sockets-tcp.d.ts +5 -0
- package/lib/common/assert.js +0 -7
- package/lib/nodejs/sockets/socket-common.js +0 -116
- package/lib/nodejs/sockets/socketopts-bindings.js +0 -94
- package/lib/nodejs/sockets/tcp-socket-impl.js +0 -794
- package/lib/nodejs/sockets/udp-socket-impl.js +0 -628
- package/lib/nodejs/sockets/wasi-sockets.js +0 -320
- package/lib/synckit/index.d.ts +0 -71
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
WASI Preview2 implementations for Node.js & browsers.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Node.js support is fully tested and conformant against the Wasmtime test suite.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Browser support is considered experimental, and not currently suitable for production applications.
|
|
8
8
|
|
|
9
9
|
# License
|
|
10
10
|
|
|
@@ -39,12 +39,13 @@ function getChildEntry (parentEntry, subpath, openFlags) {
|
|
|
39
39
|
if (!entry || !entry.dir) throw 'not-directory';
|
|
40
40
|
segmentIdx = subpath.indexOf('/');
|
|
41
41
|
const segment = segmentIdx === -1 ? subpath : subpath.slice(0, segmentIdx);
|
|
42
|
-
if (segment === '.' || segment === '') return entry;
|
|
43
42
|
if (segment === '..') throw 'no-entry';
|
|
44
|
-
if (
|
|
43
|
+
if (segment === '.' || segment === '');
|
|
44
|
+
else if (!entry.dir[segment] && openFlags.create)
|
|
45
45
|
entry = entry.dir[segment] = openFlags.directory ? { dir: {} } : { source: new Uint8Array([]) };
|
|
46
46
|
else
|
|
47
47
|
entry = entry.dir[segment];
|
|
48
|
+
subpath = subpath.slice(segmentIdx + 1);
|
|
48
49
|
} while (segmentIdx !== -1)
|
|
49
50
|
if (!entry) throw 'no-entry';
|
|
50
51
|
return entry;
|
|
@@ -179,13 +180,13 @@ class Descriptor {
|
|
|
179
180
|
stat() {
|
|
180
181
|
let type = 'unknown', size = BigInt(0);
|
|
181
182
|
if (this.#entry.source) {
|
|
182
|
-
type = 'directory';
|
|
183
|
-
}
|
|
184
|
-
else if (this.#entry.dir) {
|
|
185
183
|
type = 'regular-file';
|
|
186
184
|
const source = getSource(this.#entry);
|
|
187
185
|
size = BigInt(source.byteLength);
|
|
188
186
|
}
|
|
187
|
+
else if (this.#entry.dir) {
|
|
188
|
+
type = 'directory';
|
|
189
|
+
}
|
|
189
190
|
return {
|
|
190
191
|
type,
|
|
191
192
|
linkCount: BigInt(0),
|
package/lib/browser/random.js
CHANGED
|
@@ -33,7 +33,7 @@ export const random = {
|
|
|
33
33
|
for (var generated = 0; generated < len; generated += MAX_BYTES) {
|
|
34
34
|
// buffer.slice automatically checks if the end is past the end of
|
|
35
35
|
// the buffer so we don't have to here
|
|
36
|
-
crypto.getRandomValues(bytes.
|
|
36
|
+
crypto.getRandomValues(bytes.subarray(generated, generated + MAX_BYTES));
|
|
37
37
|
}
|
|
38
38
|
} else {
|
|
39
39
|
crypto.getRandomValues(bytes);
|
package/lib/browser/sockets.js
CHANGED
|
@@ -64,12 +64,6 @@ export const tcp = {
|
|
|
64
64
|
},
|
|
65
65
|
addressFamily() {
|
|
66
66
|
|
|
67
|
-
},
|
|
68
|
-
ipv6Only() {
|
|
69
|
-
|
|
70
|
-
},
|
|
71
|
-
setIpv6Only() {
|
|
72
|
-
|
|
73
67
|
},
|
|
74
68
|
setListenBacklogSize() {
|
|
75
69
|
|
|
@@ -152,14 +146,6 @@ export const udp = {
|
|
|
152
146
|
|
|
153
147
|
},
|
|
154
148
|
|
|
155
|
-
ipv6Only () {
|
|
156
|
-
|
|
157
|
-
},
|
|
158
|
-
|
|
159
|
-
setIpv6Only () {
|
|
160
|
-
|
|
161
|
-
},
|
|
162
|
-
|
|
163
149
|
unicastHopLimit () {
|
|
164
150
|
|
|
165
151
|
},
|
package/lib/io/calls.js
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
let call_id = 0;
|
|
2
2
|
|
|
3
|
-
// Call is a 32 bit integer, leading
|
|
3
|
+
// Call is a 32 bit integer, leading 8 bits are call number, trailing 24 bits allow custom call types
|
|
4
4
|
export const CALL_MASK = 0xff000000;
|
|
5
5
|
export const CALL_TYPE_MASK = 0x00ffffff;
|
|
6
6
|
export const CALL_SHIFT = 24;
|
|
7
7
|
|
|
8
|
+
// Type indiciator for generic Stream, Future, and Poll calls
|
|
9
|
+
let cnt = 0;
|
|
10
|
+
export const STDIN = ++cnt;
|
|
11
|
+
export const STDOUT = ++cnt;
|
|
12
|
+
export const STDERR = ++cnt;
|
|
13
|
+
export const FILE = ++cnt;
|
|
14
|
+
export const HTTP = ++cnt;
|
|
15
|
+
export const SOCKET_TCP = ++cnt;
|
|
16
|
+
export const SOCKET_UDP = ++cnt;
|
|
17
|
+
export const CLOCKS = ++cnt;
|
|
18
|
+
|
|
8
19
|
// Io Input Stream
|
|
9
20
|
export const INPUT_STREAM_CREATE = ++call_id << CALL_SHIFT;
|
|
10
21
|
export const INPUT_STREAM_READ = ++call_id << CALL_SHIFT;
|
|
@@ -34,31 +45,84 @@ export const OUTPUT_STREAM_GET_TOTAL_BYTES = ++call_id << CALL_SHIFT;
|
|
|
34
45
|
// Io Poll
|
|
35
46
|
export const POLL_POLLABLE_READY = ++call_id << CALL_SHIFT;
|
|
36
47
|
export const POLL_POLLABLE_BLOCK = ++call_id << CALL_SHIFT;
|
|
48
|
+
export const POLL_POLLABLE_DISPOSE = ++call_id << CALL_SHIFT;
|
|
37
49
|
export const POLL_POLL_LIST = ++call_id << CALL_SHIFT;
|
|
38
50
|
|
|
39
51
|
// Futures
|
|
40
|
-
export const FUTURE_GET_VALUE_AND_DISPOSE = ++call_id << CALL_SHIFT;
|
|
41
52
|
export const FUTURE_DISPOSE = ++call_id << CALL_SHIFT;
|
|
53
|
+
export const FUTURE_TAKE_VALUE = ++call_id << CALL_SHIFT;
|
|
54
|
+
export const FUTURE_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
42
55
|
|
|
43
56
|
// Http
|
|
44
57
|
export const HTTP_CREATE_REQUEST = ++call_id << 24;
|
|
45
58
|
export const HTTP_OUTPUT_STREAM_FINISH = ++call_id << CALL_SHIFT;
|
|
59
|
+
// Http server
|
|
60
|
+
export const HTTP_SERVER_START = ++call_id << CALL_SHIFT;
|
|
61
|
+
export const HTTP_SERVER_STOP = ++call_id << CALL_SHIFT;
|
|
62
|
+
export const HTTP_SERVER_INCOMING_HANDLER = ++call_id << CALL_SHIFT;
|
|
63
|
+
export const HTTP_SERVER_SET_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
|
|
64
|
+
export const HTTP_SERVER_CLEAR_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
|
|
65
|
+
export const HTTP_OUTGOING_BODY_DISPOSE = ++call_id << CALL_SHIFT;
|
|
46
66
|
|
|
47
67
|
// Clocks
|
|
48
|
-
export const CLOCKS_NOW = ++call_id <<
|
|
49
|
-
export const CLOCKS_DURATION_SUBSCRIBE = ++call_id <<
|
|
50
|
-
export const CLOCKS_INSTANT_SUBSCRIBE = ++call_id <<
|
|
68
|
+
export const CLOCKS_NOW = ++call_id << CALL_SHIFT;
|
|
69
|
+
export const CLOCKS_DURATION_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
70
|
+
export const CLOCKS_INSTANT_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
51
71
|
|
|
52
72
|
// Sockets
|
|
53
|
-
|
|
54
|
-
export const
|
|
55
|
-
export const
|
|
73
|
+
// Tcp
|
|
74
|
+
export const SOCKET_TCP_CREATE_HANDLE = ++call_id << CALL_SHIFT;
|
|
75
|
+
export const SOCKET_TCP_BIND_START = ++call_id << CALL_SHIFT;
|
|
76
|
+
export const SOCKET_TCP_BIND_FINISH = ++call_id << CALL_SHIFT;
|
|
77
|
+
export const SOCKET_TCP_CONNECT_START = ++call_id << CALL_SHIFT;
|
|
78
|
+
export const SOCKET_TCP_CONNECT_FINISH = ++call_id << CALL_SHIFT;
|
|
79
|
+
export const SOCKET_TCP_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
80
|
+
export const SOCKET_TCP_LISTEN_START = ++call_id << CALL_SHIFT;
|
|
81
|
+
export const SOCKET_TCP_LISTEN_FINISH = ++call_id << CALL_SHIFT;
|
|
82
|
+
export const SOCKET_TCP_IS_LISTENING = ++call_id << CALL_SHIFT;
|
|
83
|
+
export const SOCKET_TCP_ACCEPT = ++call_id << CALL_SHIFT;
|
|
84
|
+
export const SOCKET_TCP_GET_LOCAL_ADDRESS = ++call_id << CALL_SHIFT;
|
|
85
|
+
export const SOCKET_TCP_GET_REMOTE_ADDRESS = ++call_id << CALL_SHIFT;
|
|
86
|
+
export const SOCKET_TCP_SET_KEEP_ALIVE = ++call_id << CALL_SHIFT;
|
|
87
|
+
export const SOCKET_TCP_SET_LISTEN_BACKLOG_SIZE = ++call_id << CALL_SHIFT;
|
|
88
|
+
export const SOCKET_TCP_SHUTDOWN = ++call_id << CALL_SHIFT;
|
|
89
|
+
export const SOCKET_TCP_DISPOSE = ++call_id << CALL_SHIFT;
|
|
90
|
+
// Udp
|
|
91
|
+
export const SOCKET_UDP_CREATE_HANDLE = ++call_id << CALL_SHIFT;
|
|
92
|
+
export const SOCKET_UDP_BIND_START = ++call_id << CALL_SHIFT;
|
|
93
|
+
export const SOCKET_UDP_BIND_FINISH = ++call_id << CALL_SHIFT;
|
|
94
|
+
export const SOCKET_UDP_STREAM = ++call_id << CALL_SHIFT;
|
|
95
|
+
export const SOCKET_UDP_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
96
|
+
export const SOCKET_UDP_DISPOSE = ++call_id << CALL_SHIFT;
|
|
97
|
+
export const SOCKET_UDP_GET_LOCAL_ADDRESS = ++call_id << CALL_SHIFT;
|
|
98
|
+
export const SOCKET_UDP_GET_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
99
|
+
export const SOCKET_UDP_GET_REMOTE_ADDRESS = ++call_id << CALL_SHIFT;
|
|
100
|
+
export const SOCKET_UDP_GET_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
101
|
+
export const SOCKET_UDP_GET_UNICAST_HOP_LIMIT = ++call_id << CALL_SHIFT;
|
|
102
|
+
export const SOCKET_UDP_SET_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
103
|
+
export const SOCKET_UDP_SET_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
104
|
+
export const SOCKET_UDP_SET_UNICAST_HOP_LIMIT = ++call_id << CALL_SHIFT;
|
|
105
|
+
export const SOCKET_INCOMING_DATAGRAM_STREAM_RECEIVE = ++call_id << CALL_SHIFT;
|
|
106
|
+
export const SOCKET_OUTGOING_DATAGRAM_STREAM_CHECK_SEND =
|
|
107
|
+
++call_id << CALL_SHIFT;
|
|
108
|
+
export const SOCKET_OUTGOING_DATAGRAM_STREAM_SEND = ++call_id << CALL_SHIFT;
|
|
109
|
+
export const SOCKET_DATAGRAM_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
110
|
+
export const SOCKET_DATAGRAM_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
|
|
56
111
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
export const
|
|
62
|
-
export const
|
|
63
|
-
export const
|
|
64
|
-
export const
|
|
112
|
+
export const SOCKET_GET_DEFAULT_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
113
|
+
export const SOCKET_GET_DEFAULT_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
114
|
+
|
|
115
|
+
// Name lookup
|
|
116
|
+
export const SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST = ++call_id << CALL_SHIFT;
|
|
117
|
+
export const SOCKET_RESOLVE_ADDRESS_TAKE_REQUEST = ++call_id << CALL_SHIFT;
|
|
118
|
+
export const SOCKET_RESOLVE_ADDRESS_SUBSCRIBE_REQUEST = ++call_id << CALL_SHIFT;
|
|
119
|
+
export const SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST = ++call_id << CALL_SHIFT;
|
|
120
|
+
|
|
121
|
+
export const reverseMap = {};
|
|
122
|
+
|
|
123
|
+
import * as calls from "./calls.js";
|
|
124
|
+
|
|
125
|
+
for (const name of Object.getOwnPropertyNames(calls)) {
|
|
126
|
+
if (name === "reverseMap") continue;
|
|
127
|
+
reverseMap[calls[name]] = name;
|
|
128
|
+
}
|
package/lib/io/worker-http.js
CHANGED
|
@@ -1,14 +1,96 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
createReadableStream,
|
|
3
|
+
getStreamOrThrow,
|
|
4
|
+
} from "./worker-thread.js";
|
|
5
|
+
import {
|
|
6
|
+
createServer,
|
|
7
|
+
request as httpRequest,
|
|
8
|
+
Agent as HttpAgent,
|
|
9
|
+
} from "node:http";
|
|
10
|
+
import { request as httpsRequest, Agent as HttpsAgent } from "node:https";
|
|
11
|
+
import { parentPort } from "node:worker_threads";
|
|
12
|
+
import { HTTP_SERVER_INCOMING_HANDLER } from "./calls.js";
|
|
3
13
|
|
|
4
|
-
|
|
5
|
-
|
|
14
|
+
const agentOptions = {
|
|
15
|
+
keepAlive: true,
|
|
16
|
+
};
|
|
17
|
+
const httpAgent = new HttpAgent(agentOptions);
|
|
18
|
+
const httpsAgent = new HttpsAgent(agentOptions);
|
|
19
|
+
|
|
20
|
+
const servers = new Map();
|
|
21
|
+
|
|
22
|
+
let responseCnt = 0;
|
|
23
|
+
const responses = new Map();
|
|
24
|
+
|
|
25
|
+
export async function stopHttpServer(id) {
|
|
26
|
+
await new Promise((resolve) => servers.get(id).close(resolve));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function clearOutgoingResponse(id) {
|
|
30
|
+
responses.delete(id);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function setOutgoingResponse(
|
|
34
|
+
id,
|
|
35
|
+
{ statusCode, headers, streamId }
|
|
36
|
+
) {
|
|
37
|
+
const response = responses.get(id);
|
|
38
|
+
const textDecoder = new TextDecoder();
|
|
39
|
+
response.writeHead(
|
|
40
|
+
statusCode,
|
|
41
|
+
Object.fromEntries(
|
|
42
|
+
headers.map(([key, val]) => [key, textDecoder.decode(val)])
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
response.flushHeaders();
|
|
46
|
+
const { stream } = getStreamOrThrow(streamId);
|
|
47
|
+
stream.pipe(response);
|
|
48
|
+
responses.delete(id);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function startHttpServer(id, { port, host }) {
|
|
52
|
+
const server = createServer((req, res) => {
|
|
53
|
+
// create the streams and their ids
|
|
54
|
+
const streamId = createReadableStream(req);
|
|
55
|
+
const responseId = ++responseCnt;
|
|
56
|
+
parentPort.postMessage({
|
|
57
|
+
type: HTTP_SERVER_INCOMING_HANDLER,
|
|
58
|
+
id,
|
|
59
|
+
payload: {
|
|
60
|
+
responseId,
|
|
61
|
+
method: req.method,
|
|
62
|
+
host: req.headers.host || host || "localhost",
|
|
63
|
+
pathWithQuery: req.url,
|
|
64
|
+
headers: Object.entries(req.headersDistinct).flatMap(([key, val]) =>
|
|
65
|
+
val.map((val) => [key, val])
|
|
66
|
+
),
|
|
67
|
+
streamId,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
responses.set(responseId, res);
|
|
71
|
+
});
|
|
72
|
+
await new Promise((resolve, reject) => {
|
|
73
|
+
server.listen(port, host, resolve);
|
|
74
|
+
server.on("error", reject);
|
|
75
|
+
});
|
|
76
|
+
servers.set(id, server);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function createHttpRequest(
|
|
80
|
+
method,
|
|
81
|
+
scheme,
|
|
82
|
+
authority,
|
|
83
|
+
pathWithQuery,
|
|
84
|
+
headers,
|
|
85
|
+
bodyId,
|
|
86
|
+
connectTimeout,
|
|
87
|
+
betweenBytesTimeout,
|
|
88
|
+
firstByteTimeout
|
|
89
|
+
) {
|
|
90
|
+
let stream = null;
|
|
6
91
|
if (bodyId) {
|
|
7
92
|
try {
|
|
8
|
-
|
|
9
|
-
body = stream.readableBodyStream;
|
|
10
|
-
// this indicates we're attached
|
|
11
|
-
stream.readableBodyStream = null;
|
|
93
|
+
({ stream } = getStreamOrThrow(bodyId));
|
|
12
94
|
} catch (e) {
|
|
13
95
|
if (e.tag === "closed")
|
|
14
96
|
throw { tag: "internal-error", val: "Unexpected closed body stream" };
|
|
@@ -25,71 +107,86 @@ export async function createHttpRequest(method, url, headers, bodyId) {
|
|
|
25
107
|
}
|
|
26
108
|
}
|
|
27
109
|
try {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
110
|
+
// Make a request
|
|
111
|
+
let req;
|
|
112
|
+
switch (scheme) {
|
|
113
|
+
case "http:":
|
|
114
|
+
req = httpRequest({
|
|
115
|
+
agent: httpAgent,
|
|
116
|
+
method,
|
|
117
|
+
host: authority.split(":")[0],
|
|
118
|
+
port: authority.split(":")[1],
|
|
119
|
+
path: pathWithQuery,
|
|
120
|
+
timeout: connectTimeout && Number(connectTimeout),
|
|
121
|
+
});
|
|
122
|
+
break;
|
|
123
|
+
case "https:":
|
|
124
|
+
req = httpsRequest({
|
|
125
|
+
agent: httpsAgent,
|
|
126
|
+
method,
|
|
127
|
+
host: authority.split(":")[0],
|
|
128
|
+
port: authority.split(":")[1],
|
|
129
|
+
path: pathWithQuery,
|
|
130
|
+
timeout: connectTimeout && Number(connectTimeout),
|
|
131
|
+
});
|
|
132
|
+
break;
|
|
133
|
+
default:
|
|
134
|
+
throw { tag: "HTTP-protocol-error" };
|
|
135
|
+
}
|
|
136
|
+
for (const [key, value] of headers) {
|
|
137
|
+
req.appendHeader(key, value);
|
|
138
|
+
}
|
|
139
|
+
req.flushHeaders();
|
|
140
|
+
if (stream) {
|
|
141
|
+
stream.pipe(req);
|
|
142
|
+
} else {
|
|
143
|
+
req.end();
|
|
144
|
+
}
|
|
145
|
+
const res = await new Promise((resolve, reject) => {
|
|
146
|
+
req.once("response", resolve);
|
|
147
|
+
req.once("close", () => reject);
|
|
148
|
+
req.once("error", reject);
|
|
34
149
|
});
|
|
35
|
-
|
|
150
|
+
if (firstByteTimeout) res.setTimeout(Number(firstByteTimeout));
|
|
151
|
+
if (betweenBytesTimeout)
|
|
152
|
+
res.once("readable", () => {
|
|
153
|
+
res.setTimeout(Number(betweenBytesTimeout));
|
|
154
|
+
});
|
|
155
|
+
const bodyStreamId = createReadableStream(res);
|
|
36
156
|
return {
|
|
37
|
-
status: res.
|
|
38
|
-
headers: Array.from(res.headers),
|
|
39
|
-
bodyStreamId
|
|
157
|
+
status: res.statusCode,
|
|
158
|
+
headers: Array.from(Object.entries(res.headers)),
|
|
159
|
+
bodyStreamId
|
|
40
160
|
};
|
|
41
161
|
} catch (e) {
|
|
42
|
-
if (e?.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
162
|
+
if (e?.tag) throw e;
|
|
163
|
+
const err = getFirstError(e);
|
|
164
|
+
switch (err.code) {
|
|
165
|
+
case "ECONNRESET":
|
|
166
|
+
throw { tag: "HTTP-protocol-error" };
|
|
167
|
+
case "ENOTFOUND":
|
|
46
168
|
throw {
|
|
47
|
-
tag: "
|
|
169
|
+
tag: "DNS-error",
|
|
170
|
+
val: {
|
|
171
|
+
rcode: err.code,
|
|
172
|
+
infoCode: err.errno < 0 ? -err.errno : err.errno,
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
case "ECONNREFUSED":
|
|
176
|
+
throw {
|
|
177
|
+
tag: "connection-refused",
|
|
48
178
|
};
|
|
49
|
-
switch (err.syscall) {
|
|
50
|
-
case "connect": {
|
|
51
|
-
if (err.code === "ECONNREFUSED")
|
|
52
|
-
throw {
|
|
53
|
-
tag: "connection-refused",
|
|
54
|
-
};
|
|
55
|
-
break;
|
|
56
|
-
}
|
|
57
|
-
case "getaddrinfo": {
|
|
58
|
-
const { errno, code } = err;
|
|
59
|
-
throw {
|
|
60
|
-
tag: "DNS-error",
|
|
61
|
-
val: {
|
|
62
|
-
rcode: code,
|
|
63
|
-
infoCode: errno,
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (e?.message?.includes("Failed to parse URL")) {
|
|
70
|
-
throw {
|
|
71
|
-
tag: "HTTP-request-URI-invalid",
|
|
72
|
-
val: undefined,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
if (e?.message?.includes("HTTP")) {
|
|
76
|
-
switch (e?.message.replace(/'[^']+'/, "'{}'")) {
|
|
77
|
-
case "'{}' HTTP method is unsupported.":
|
|
78
|
-
throw {
|
|
79
|
-
tag: "HTTP-protocol-error",
|
|
80
|
-
val: undefined,
|
|
81
|
-
};
|
|
82
|
-
case "'{}' is not a valid HTTP method.":
|
|
83
|
-
throw {
|
|
84
|
-
tag: "HTTP-request-method-invalid",
|
|
85
|
-
val: undefined,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
throw {
|
|
89
|
-
tag: "internal-error",
|
|
90
|
-
val: e.toString(),
|
|
91
|
-
};
|
|
92
179
|
}
|
|
93
|
-
throw
|
|
180
|
+
throw {
|
|
181
|
+
tag: "internal-error",
|
|
182
|
+
val: err.toString(),
|
|
183
|
+
};
|
|
94
184
|
}
|
|
95
185
|
}
|
|
186
|
+
|
|
187
|
+
function getFirstError(e) {
|
|
188
|
+
if (typeof e !== "object" || e === null) return e;
|
|
189
|
+
if (e.cause) return getFirstError(e.cause);
|
|
190
|
+
if (e instanceof AggregateError) return getFirstError(e.errors[0]);
|
|
191
|
+
return e;
|
|
192
|
+
}
|