@cosmonic-labs/openapi2mcp 0.10.0 → 0.10.2
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/dist/interfaces/wasi-cli-environment.d.ts +1 -1
- package/dist/interfaces/wasi-cli-exit.d.ts +1 -1
- package/dist/interfaces/wasi-cli-stderr.d.ts +1 -1
- package/dist/interfaces/wasi-cli-stdin.d.ts +1 -1
- package/dist/interfaces/wasi-cli-stdout.d.ts +1 -1
- package/dist/interfaces/wasi-cli-terminal-input.d.ts +1 -1
- package/dist/interfaces/wasi-cli-terminal-output.d.ts +1 -1
- package/dist/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
- package/dist/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
- package/dist/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
- package/dist/interfaces/wasi-clocks-monotonic-clock.d.ts +4 -0
- package/dist/interfaces/wasi-clocks-wall-clock.d.ts +1 -1
- package/dist/interfaces/wasi-filesystem-preopens.d.ts +1 -1
- package/dist/interfaces/wasi-filesystem-types.d.ts +1 -1
- package/dist/interfaces/wasi-io-error.d.ts +1 -1
- package/dist/interfaces/wasi-io-poll.d.ts +2 -1
- package/dist/interfaces/wasi-io-streams.d.ts +2 -2
- package/dist/interfaces/wasi-random-insecure-seed.d.ts +1 -1
- package/dist/openapi2mcp.core.wasm +0 -0
- package/dist/openapi2mcp.d.ts +18 -17
- package/dist/openapi2mcp.js +655 -359
- package/node_modules/@bytecodealliance/preview2-shim/LICENSE +220 -0
- package/node_modules/@bytecodealliance/preview2-shim/README.md +113 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/cli.js +135 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/clocks.js +39 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/config.js +9 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/environment.js +29 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/filesystem.js +485 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/http.js +707 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/index.js +9 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/io.js +262 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/random.js +56 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/browser/sockets.js +88 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/common/instantiation.js +253 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/calls.js +128 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-http.js +206 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-io.js +468 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-socket-tcp.js +331 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-socket-udp.js +587 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-sockets.js +364 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/io/worker-thread.js +1051 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/cli.js +109 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/clocks.js +67 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/filesystem.js +829 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/http.js +764 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/index.js +9 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/io.js +1 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/random.js +49 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/nodejs/sockets.js +565 -0
- package/node_modules/@bytecodealliance/preview2-shim/lib/synckit/index.js +127 -0
- package/node_modules/@bytecodealliance/preview2-shim/package.json +65 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/cli.d.ts +11 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/clocks.d.ts +2 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/filesystem.d.ts +29 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/http.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/index.d.ts +6 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/instantiation.d.ts +199 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-environment.d.ts +21 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-exit.d.ts +6 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-run.d.ts +5 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-stderr.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-stdin.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-stdout.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-terminal-input.d.ts +8 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-terminal-output.d.ts +8 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-terminal-stderr.d.ts +7 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-terminal-stdin.d.ts +7 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-cli-terminal-stdout.d.ts +7 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-clocks-monotonic-clock.d.ts +34 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-clocks-wall-clock.d.ts +30 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-filesystem-preopens.d.ts +6 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-filesystem-types.d.ts +669 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-http-incoming-handler.d.ts +16 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-http-outgoing-handler.d.ts +18 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-http-types.d.ts +748 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-io-error.d.ts +18 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-io-poll.d.ts +44 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-io-streams.d.ts +243 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-random-insecure-seed.d.ts +21 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-random-insecure.d.ts +19 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-random-random.d.ts +23 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-instance-network.d.ts +6 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +58 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-network.d.ts +164 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-tcp-create-socket.d.ts +28 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-tcp.d.ts +359 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-udp-create-socket.d.ts +28 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/interfaces/wasi-sockets-udp.d.ts +301 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/io.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/random.d.ts +3 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/sockets.d.ts +7 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/wasi-cli-command.d.ts +29 -0
- package/node_modules/@bytecodealliance/preview2-shim/types/wasi-http-proxy.d.ts +13 -0
- package/package.json +9 -6
- package/patches/@bytecodealliance+preview2-shim+0.18.0.patch +13 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
let call_id = 0;
|
|
2
|
+
|
|
3
|
+
// Call is a 32 bit integer, leading 8 bits are call number, trailing 24 bits allow custom call types
|
|
4
|
+
export const CALL_MASK = 0xff000000;
|
|
5
|
+
export const CALL_TYPE_MASK = 0x00ffffff;
|
|
6
|
+
export const CALL_SHIFT = 24;
|
|
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
|
+
|
|
19
|
+
// Io Input Stream
|
|
20
|
+
export const INPUT_STREAM_CREATE = ++call_id << CALL_SHIFT;
|
|
21
|
+
export const INPUT_STREAM_READ = ++call_id << CALL_SHIFT;
|
|
22
|
+
export const INPUT_STREAM_BLOCKING_READ = ++call_id << CALL_SHIFT;
|
|
23
|
+
export const INPUT_STREAM_SKIP = ++call_id << CALL_SHIFT;
|
|
24
|
+
export const INPUT_STREAM_BLOCKING_SKIP = ++call_id << CALL_SHIFT;
|
|
25
|
+
export const INPUT_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
26
|
+
export const INPUT_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
|
|
27
|
+
|
|
28
|
+
// Io Output Stream
|
|
29
|
+
export const OUTPUT_STREAM_CREATE = ++call_id << CALL_SHIFT;
|
|
30
|
+
export const OUTPUT_STREAM_CHECK_WRITE = ++call_id << CALL_SHIFT;
|
|
31
|
+
export const OUTPUT_STREAM_WRITE = ++call_id << CALL_SHIFT;
|
|
32
|
+
export const OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH = ++call_id << CALL_SHIFT;
|
|
33
|
+
export const OUTPUT_STREAM_FLUSH = ++call_id << CALL_SHIFT;
|
|
34
|
+
export const OUTPUT_STREAM_BLOCKING_FLUSH = ++call_id << CALL_SHIFT;
|
|
35
|
+
export const OUTPUT_STREAM_WRITE_ZEROES = ++call_id << CALL_SHIFT;
|
|
36
|
+
export const OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH = ++call_id << CALL_SHIFT;
|
|
37
|
+
export const OUTPUT_STREAM_SPLICE = ++call_id << CALL_SHIFT;
|
|
38
|
+
export const OUTPUT_STREAM_BLOCKING_SPLICE = ++call_id << CALL_SHIFT;
|
|
39
|
+
export const OUTPUT_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
40
|
+
export const OUTPUT_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
|
|
41
|
+
|
|
42
|
+
export const OUTPUT_STREAM_GET_TOTAL_BYTES = ++call_id << CALL_SHIFT;
|
|
43
|
+
|
|
44
|
+
// Io Poll
|
|
45
|
+
export const POLL_POLLABLE_READY = ++call_id << CALL_SHIFT;
|
|
46
|
+
export const POLL_POLLABLE_BLOCK = ++call_id << CALL_SHIFT;
|
|
47
|
+
export const POLL_POLLABLE_DISPOSE = ++call_id << CALL_SHIFT;
|
|
48
|
+
export const POLL_POLL_LIST = ++call_id << CALL_SHIFT;
|
|
49
|
+
|
|
50
|
+
// Futures
|
|
51
|
+
export const FUTURE_DISPOSE = ++call_id << CALL_SHIFT;
|
|
52
|
+
export const FUTURE_TAKE_VALUE = ++call_id << CALL_SHIFT;
|
|
53
|
+
export const FUTURE_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
54
|
+
|
|
55
|
+
// Http
|
|
56
|
+
export const HTTP_CREATE_REQUEST = ++call_id << 24;
|
|
57
|
+
export const HTTP_OUTPUT_STREAM_FINISH = ++call_id << CALL_SHIFT;
|
|
58
|
+
// Http server
|
|
59
|
+
export const HTTP_SERVER_START = ++call_id << CALL_SHIFT;
|
|
60
|
+
export const HTTP_SERVER_STOP = ++call_id << CALL_SHIFT;
|
|
61
|
+
export const HTTP_SERVER_INCOMING_HANDLER = ++call_id << CALL_SHIFT;
|
|
62
|
+
export const HTTP_SERVER_SET_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
|
|
63
|
+
export const HTTP_SERVER_CLEAR_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
|
|
64
|
+
export const HTTP_SERVER_GET_ADDRESS = ++call_id << CALL_SHIFT;
|
|
65
|
+
export const HTTP_OUTGOING_BODY_DISPOSE = ++call_id << CALL_SHIFT;
|
|
66
|
+
|
|
67
|
+
// Clocks
|
|
68
|
+
export const CLOCKS_DURATION_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
69
|
+
export const CLOCKS_INSTANT_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
70
|
+
|
|
71
|
+
// Sockets
|
|
72
|
+
// Tcp
|
|
73
|
+
export const SOCKET_TCP_CREATE_HANDLE = ++call_id << CALL_SHIFT;
|
|
74
|
+
export const SOCKET_TCP_BIND_START = ++call_id << CALL_SHIFT;
|
|
75
|
+
export const SOCKET_TCP_BIND_FINISH = ++call_id << CALL_SHIFT;
|
|
76
|
+
export const SOCKET_TCP_CONNECT_START = ++call_id << CALL_SHIFT;
|
|
77
|
+
export const SOCKET_TCP_CONNECT_FINISH = ++call_id << CALL_SHIFT;
|
|
78
|
+
export const SOCKET_TCP_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
79
|
+
export const SOCKET_TCP_LISTEN_START = ++call_id << CALL_SHIFT;
|
|
80
|
+
export const SOCKET_TCP_LISTEN_FINISH = ++call_id << CALL_SHIFT;
|
|
81
|
+
export const SOCKET_TCP_IS_LISTENING = ++call_id << CALL_SHIFT;
|
|
82
|
+
export const SOCKET_TCP_ACCEPT = ++call_id << CALL_SHIFT;
|
|
83
|
+
export const SOCKET_TCP_GET_LOCAL_ADDRESS = ++call_id << CALL_SHIFT;
|
|
84
|
+
export const SOCKET_TCP_GET_REMOTE_ADDRESS = ++call_id << CALL_SHIFT;
|
|
85
|
+
export const SOCKET_TCP_SET_KEEP_ALIVE = ++call_id << CALL_SHIFT;
|
|
86
|
+
export const SOCKET_TCP_SET_LISTEN_BACKLOG_SIZE = ++call_id << CALL_SHIFT;
|
|
87
|
+
export const SOCKET_TCP_SHUTDOWN = ++call_id << CALL_SHIFT;
|
|
88
|
+
export const SOCKET_TCP_DISPOSE = ++call_id << CALL_SHIFT;
|
|
89
|
+
// Udp
|
|
90
|
+
export const SOCKET_UDP_CREATE_HANDLE = ++call_id << CALL_SHIFT;
|
|
91
|
+
export const SOCKET_UDP_BIND_START = ++call_id << CALL_SHIFT;
|
|
92
|
+
export const SOCKET_UDP_BIND_FINISH = ++call_id << CALL_SHIFT;
|
|
93
|
+
export const SOCKET_UDP_STREAM = ++call_id << CALL_SHIFT;
|
|
94
|
+
export const SOCKET_UDP_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
95
|
+
export const SOCKET_UDP_DISPOSE = ++call_id << CALL_SHIFT;
|
|
96
|
+
export const SOCKET_UDP_GET_LOCAL_ADDRESS = ++call_id << CALL_SHIFT;
|
|
97
|
+
export const SOCKET_UDP_GET_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
98
|
+
export const SOCKET_UDP_GET_REMOTE_ADDRESS = ++call_id << CALL_SHIFT;
|
|
99
|
+
export const SOCKET_UDP_GET_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
100
|
+
export const SOCKET_UDP_GET_UNICAST_HOP_LIMIT = ++call_id << CALL_SHIFT;
|
|
101
|
+
export const SOCKET_UDP_SET_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
102
|
+
export const SOCKET_UDP_SET_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
103
|
+
export const SOCKET_UDP_SET_UNICAST_HOP_LIMIT = ++call_id << CALL_SHIFT;
|
|
104
|
+
export const SOCKET_INCOMING_DATAGRAM_STREAM_RECEIVE = ++call_id << CALL_SHIFT;
|
|
105
|
+
export const SOCKET_OUTGOING_DATAGRAM_STREAM_CHECK_SEND = ++call_id << CALL_SHIFT;
|
|
106
|
+
export const SOCKET_OUTGOING_DATAGRAM_STREAM_SEND = ++call_id << CALL_SHIFT;
|
|
107
|
+
export const SOCKET_DATAGRAM_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
|
|
108
|
+
export const SOCKET_DATAGRAM_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
|
|
109
|
+
|
|
110
|
+
export const SOCKET_GET_DEFAULT_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
111
|
+
export const SOCKET_GET_DEFAULT_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
|
|
112
|
+
|
|
113
|
+
// Name lookup
|
|
114
|
+
export const SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST = ++call_id << CALL_SHIFT;
|
|
115
|
+
export const SOCKET_RESOLVE_ADDRESS_TAKE_REQUEST = ++call_id << CALL_SHIFT;
|
|
116
|
+
export const SOCKET_RESOLVE_ADDRESS_SUBSCRIBE_REQUEST = ++call_id << CALL_SHIFT;
|
|
117
|
+
export const SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST = ++call_id << CALL_SHIFT;
|
|
118
|
+
|
|
119
|
+
export const reverseMap = {};
|
|
120
|
+
|
|
121
|
+
import * as calls from "./calls.js";
|
|
122
|
+
|
|
123
|
+
for (const name of Object.getOwnPropertyNames(calls)) {
|
|
124
|
+
if (name === "reverseMap") {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
reverseMap[calls[name]] = name;
|
|
128
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { createReadableStream, getStreamOrThrow } from "./worker-thread.js";
|
|
2
|
+
import { createServer, request as httpRequest, Agent as HttpAgent } from "node:http";
|
|
3
|
+
import { request as httpsRequest, Agent as HttpsAgent } from "node:https";
|
|
4
|
+
import { parentPort } from "node:worker_threads";
|
|
5
|
+
import { HTTP_SERVER_INCOMING_HANDLER } from "./calls.js";
|
|
6
|
+
|
|
7
|
+
const agentOptions = {
|
|
8
|
+
keepAlive: true,
|
|
9
|
+
};
|
|
10
|
+
const httpAgent = new HttpAgent(agentOptions);
|
|
11
|
+
const httpsAgent = new HttpsAgent(agentOptions);
|
|
12
|
+
|
|
13
|
+
const servers = new Map();
|
|
14
|
+
|
|
15
|
+
let responseCnt = 0;
|
|
16
|
+
const responses = new Map();
|
|
17
|
+
|
|
18
|
+
export async function stopHttpServer(id) {
|
|
19
|
+
await new Promise((resolve) => servers.get(id).close(resolve));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function clearOutgoingResponse(id) {
|
|
23
|
+
responses.delete(id);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function getHttpServerAddress(id) {
|
|
27
|
+
return servers.get(id).address();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function setOutgoingResponse(id, { statusCode, headers, streamId }) {
|
|
31
|
+
const response = responses.get(id);
|
|
32
|
+
const textDecoder = new TextDecoder();
|
|
33
|
+
response.writeHead(
|
|
34
|
+
statusCode,
|
|
35
|
+
Object.fromEntries(headers.map(([key, val]) => [key, textDecoder.decode(val)])),
|
|
36
|
+
);
|
|
37
|
+
response.flushHeaders();
|
|
38
|
+
const { stream } = getStreamOrThrow(streamId);
|
|
39
|
+
stream.pipe(response);
|
|
40
|
+
responses.delete(id);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function startHttpServer(id, { port, host }) {
|
|
44
|
+
const server = createServer((req, res) => {
|
|
45
|
+
// create the streams and their ids
|
|
46
|
+
const streamId = createReadableStream(req);
|
|
47
|
+
const responseId = ++responseCnt;
|
|
48
|
+
parentPort.postMessage({
|
|
49
|
+
type: HTTP_SERVER_INCOMING_HANDLER,
|
|
50
|
+
id,
|
|
51
|
+
payload: {
|
|
52
|
+
responseId,
|
|
53
|
+
method: req.method,
|
|
54
|
+
host: req.headers.host || host || "localhost",
|
|
55
|
+
pathWithQuery: req.url,
|
|
56
|
+
headers: Object.entries(req.headersDistinct).flatMap(([key, val]) =>
|
|
57
|
+
val.map((val) => [key, val]),
|
|
58
|
+
),
|
|
59
|
+
streamId,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
responses.set(responseId, res);
|
|
63
|
+
});
|
|
64
|
+
await new Promise((resolve, reject) => {
|
|
65
|
+
server.listen(port, host, resolve);
|
|
66
|
+
server.on("error", reject);
|
|
67
|
+
});
|
|
68
|
+
servers.set(id, server);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function createHttpRequest(
|
|
72
|
+
method,
|
|
73
|
+
scheme,
|
|
74
|
+
authority,
|
|
75
|
+
pathWithQuery,
|
|
76
|
+
headers,
|
|
77
|
+
bodyId,
|
|
78
|
+
connectTimeout,
|
|
79
|
+
betweenBytesTimeout,
|
|
80
|
+
firstByteTimeout,
|
|
81
|
+
) {
|
|
82
|
+
let stream = null;
|
|
83
|
+
if (bodyId) {
|
|
84
|
+
try {
|
|
85
|
+
({ stream } = getStreamOrThrow(bodyId));
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (e.tag === "closed") {
|
|
88
|
+
throw {
|
|
89
|
+
tag: "internal-error",
|
|
90
|
+
val: "Unexpected closed body stream",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// it should never be possible for the body stream to already
|
|
94
|
+
// be closed, or for there to be a write error
|
|
95
|
+
// we therefore just throw internal error here
|
|
96
|
+
if (e.tag === "last-operation-failed") {
|
|
97
|
+
throw {
|
|
98
|
+
tag: "internal-error",
|
|
99
|
+
val: e.val,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// entirely unknown error -> trap
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
// Make a request
|
|
108
|
+
let req;
|
|
109
|
+
switch (scheme) {
|
|
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();
|
|
141
|
+
}
|
|
142
|
+
const res = await new Promise((resolve, reject) => {
|
|
143
|
+
req.once("timeout", () => {
|
|
144
|
+
reject({
|
|
145
|
+
tag: "connection-timeout",
|
|
146
|
+
});
|
|
147
|
+
req.destroy();
|
|
148
|
+
});
|
|
149
|
+
req.once("response", resolve);
|
|
150
|
+
req.once("close", () => reject);
|
|
151
|
+
req.once("error", reject);
|
|
152
|
+
});
|
|
153
|
+
if (firstByteTimeout) {
|
|
154
|
+
res.setTimeout(Number(firstByteTimeout / 1_000_000n));
|
|
155
|
+
}
|
|
156
|
+
if (betweenBytesTimeout) {
|
|
157
|
+
res.once("readable", () => {
|
|
158
|
+
res.setTimeout(Number(betweenBytesTimeout / 1_000_000n));
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
const bodyStreamId = createReadableStream(res);
|
|
162
|
+
return {
|
|
163
|
+
status: res.statusCode,
|
|
164
|
+
headers: Array.from(Object.entries(res.headers)),
|
|
165
|
+
bodyStreamId,
|
|
166
|
+
};
|
|
167
|
+
} catch (e) {
|
|
168
|
+
if (e?.tag) {
|
|
169
|
+
throw e;
|
|
170
|
+
}
|
|
171
|
+
const err = getFirstError(e);
|
|
172
|
+
switch (err.code) {
|
|
173
|
+
case "ECONNRESET":
|
|
174
|
+
throw { tag: "HTTP-protocol-error" };
|
|
175
|
+
case "ENOTFOUND":
|
|
176
|
+
throw {
|
|
177
|
+
tag: "DNS-error",
|
|
178
|
+
val: {
|
|
179
|
+
rcode: err.code,
|
|
180
|
+
infoCode: err.errno < 0 ? -err.errno : err.errno,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
case "ECONNREFUSED":
|
|
184
|
+
throw {
|
|
185
|
+
tag: "connection-refused",
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
throw {
|
|
189
|
+
tag: "internal-error",
|
|
190
|
+
val: err.toString(),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function getFirstError(e) {
|
|
196
|
+
if (typeof e !== "object" || e === null) {
|
|
197
|
+
return e;
|
|
198
|
+
}
|
|
199
|
+
if (e.cause) {
|
|
200
|
+
return getFirstError(e.cause);
|
|
201
|
+
}
|
|
202
|
+
if (e instanceof AggregateError) {
|
|
203
|
+
return getFirstError(e.errors[0]);
|
|
204
|
+
}
|
|
205
|
+
return e;
|
|
206
|
+
}
|