@psnext/slingcli 2.4.20260527-3 → 2.4.20260601-1
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/bin/sling.js +17 -26
- package/node_modules/@aws-sdk/core/package.json +3 -3
- package/node_modules/@aws-sdk/credential-provider-env/package.json +3 -3
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/LICENSE +201 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/README.md +9 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-cjs/index.js +888 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/build-abort-error.js +19 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/constants.js +1 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/get-transformed-headers.js +9 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/http2/ClientHttp2SessionRef.js +50 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/index.js +3 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/node-http-handler.js +240 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-manager.js +136 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-pool.js +46 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/node-http2-handler.js +172 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/set-connection-timeout.js +36 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/set-request-timeout.js +21 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/set-socket-keep-alive.js +22 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/set-socket-timeout.js +23 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/stream-collector/collector.js +8 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/stream-collector/index.js +41 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/timing.js +4 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-es/write-request-body.js +63 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/build-abort-error.d.ts +10 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/constants.d.ts +5 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/get-transformed-headers.d.ts +4 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/http2/ClientHttp2SessionRef.d.ts +46 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/index.d.ts +3 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/node-http-handler.d.ts +47 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/node-http2-connection-manager.d.ts +46 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/node-http2-connection-pool.d.ts +36 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/node-http2-handler.d.ts +76 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/set-connection-timeout.d.ts +2 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/set-request-timeout.d.ts +6 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/set-socket-keep-alive.d.ts +6 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/set-socket-timeout.d.ts +2 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/stream-collector/collector.d.ts +5 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/stream-collector/index.d.ts +7 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/timing.d.ts +9 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/dist-types/write-request-body.d.ts +12 -0
- package/node_modules/@aws-sdk/credential-provider-http/node_modules/@smithy/node-http-handler/package.json +66 -0
- package/node_modules/@aws-sdk/credential-provider-http/package.json +5 -5
- package/node_modules/@aws-sdk/credential-provider-ini/package.json +11 -11
- package/node_modules/@aws-sdk/credential-provider-login/package.json +4 -4
- package/node_modules/@aws-sdk/credential-provider-node/package.json +9 -9
- package/node_modules/@aws-sdk/credential-provider-process/package.json +3 -3
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/package.json +4 -4
- package/node_modules/@aws-sdk/credential-provider-sso/package.json +5 -5
- package/node_modules/@aws-sdk/credential-provider-web-identity/package.json +4 -4
- package/node_modules/@aws-sdk/eventstream-handler-node/package.json +2 -2
- package/node_modules/@aws-sdk/middleware-eventstream/package.json +2 -2
- package/node_modules/@aws-sdk/middleware-websocket/package.json +5 -5
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/cognito-identity/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/signin/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso-oidc/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sts/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/LICENSE +201 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/README.md +9 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-cjs/index.js +888 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/build-abort-error.js +19 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/constants.js +1 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/get-transformed-headers.js +9 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/http2/ClientHttp2SessionRef.js +50 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/index.js +3 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/node-http-handler.js +240 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-manager.js +136 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-pool.js +46 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/node-http2-handler.js +172 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/set-connection-timeout.js +36 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/set-request-timeout.js +21 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/set-socket-keep-alive.js +22 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/set-socket-timeout.js +23 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/stream-collector/collector.js +8 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/stream-collector/index.js +41 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/timing.js +4 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-es/write-request-body.js +63 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/build-abort-error.d.ts +10 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/constants.d.ts +5 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/get-transformed-headers.d.ts +4 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/http2/ClientHttp2SessionRef.d.ts +46 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/index.d.ts +3 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/node-http-handler.d.ts +47 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/node-http2-connection-manager.d.ts +46 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/node-http2-connection-pool.d.ts +36 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/node-http2-handler.d.ts +76 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/set-connection-timeout.d.ts +2 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/set-request-timeout.d.ts +6 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/set-socket-keep-alive.d.ts +6 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/set-socket-timeout.d.ts +2 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/stream-collector/collector.d.ts +5 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/stream-collector/index.d.ts +7 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/timing.d.ts +9 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/dist-types/write-request-body.d.ts +12 -0
- package/node_modules/@aws-sdk/nested-clients/node_modules/@smithy/node-http-handler/package.json +66 -0
- package/node_modules/@aws-sdk/nested-clients/package.json +6 -6
- package/node_modules/@aws-sdk/signature-v4-multi-region/package.json +2 -2
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.js +563 -326
- package/node_modules/@nodable/entities/package.json +1 -1
- package/node_modules/@nodable/entities/src/EntityDecoder.js +1 -1
- package/node_modules/@nodable/entities/src/entities.js +0 -18
- package/node_modules/@smithy/credential-provider-imds/dist-cjs/index.js +14 -13
- package/node_modules/@smithy/credential-provider-imds/dist-es/fromContainerMetadata.js +14 -13
- package/node_modules/@smithy/credential-provider-imds/package.json +1 -1
- package/node_modules/protobufjs/dist/light/protobuf.js +7 -5
- package/node_modules/protobufjs/dist/light/protobuf.js.map +1 -1
- package/node_modules/protobufjs/dist/light/protobuf.min.js +3 -3
- package/node_modules/protobufjs/dist/light/protobuf.min.js.map +1 -1
- package/node_modules/protobufjs/dist/minimal/protobuf.js +3 -3
- package/node_modules/protobufjs/dist/minimal/protobuf.js.map +1 -1
- package/node_modules/protobufjs/dist/minimal/protobuf.min.js +3 -3
- package/node_modules/protobufjs/dist/minimal/protobuf.min.js.map +1 -1
- package/node_modules/protobufjs/dist/protobuf.js +7 -5
- package/node_modules/protobufjs/dist/protobuf.js.map +1 -1
- package/node_modules/protobufjs/dist/protobuf.min.js +3 -3
- package/node_modules/protobufjs/dist/protobuf.min.js.map +1 -1
- package/node_modules/protobufjs/package.json +1 -1
- package/node_modules/protobufjs/src/converter.js +4 -2
- package/node_modules/protobufjs/src/roots.js +1 -1
- package/node_modules/undici/lib/global.js +10 -1
- package/node_modules/undici/package.json +1 -1
- package/package.json +2 -2
- package/slingshot/index.js +94 -94
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_https = require('node:https');
|
|
4
|
+
var protocols = require('@smithy/core/protocols');
|
|
5
|
+
var node_stream = require('node:stream');
|
|
6
|
+
var http2 = require('node:http2');
|
|
7
|
+
|
|
8
|
+
function buildAbortError(abortSignal) {
|
|
9
|
+
const reason = abortSignal && typeof abortSignal === "object" && "reason" in abortSignal
|
|
10
|
+
? abortSignal.reason
|
|
11
|
+
: undefined;
|
|
12
|
+
if (reason) {
|
|
13
|
+
if (reason instanceof Error) {
|
|
14
|
+
const abortError = new Error("Request aborted");
|
|
15
|
+
abortError.name = "AbortError";
|
|
16
|
+
abortError.cause = reason;
|
|
17
|
+
return abortError;
|
|
18
|
+
}
|
|
19
|
+
const abortError = new Error(String(reason));
|
|
20
|
+
abortError.name = "AbortError";
|
|
21
|
+
return abortError;
|
|
22
|
+
}
|
|
23
|
+
const abortError = new Error("Request aborted");
|
|
24
|
+
abortError.name = "AbortError";
|
|
25
|
+
return abortError;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const NODEJS_TIMEOUT_ERROR_CODES = ["ECONNRESET", "EPIPE", "ETIMEDOUT"];
|
|
29
|
+
|
|
30
|
+
const getTransformedHeaders = (headers) => {
|
|
31
|
+
const transformedHeaders = {};
|
|
32
|
+
for (const name in headers) {
|
|
33
|
+
const headerValues = headers[name];
|
|
34
|
+
transformedHeaders[name] = Array.isArray(headerValues) ? headerValues.join(",") : headerValues;
|
|
35
|
+
}
|
|
36
|
+
return transformedHeaders;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const timing = {
|
|
40
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
41
|
+
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const DEFER_EVENT_LISTENER_TIME$2 = 1000;
|
|
45
|
+
const setConnectionTimeout = (request, reject, timeoutInMs = 0) => {
|
|
46
|
+
if (!timeoutInMs) {
|
|
47
|
+
return -1;
|
|
48
|
+
}
|
|
49
|
+
const registerTimeout = (offset) => {
|
|
50
|
+
const timeoutId = timing.setTimeout(() => {
|
|
51
|
+
request.destroy();
|
|
52
|
+
reject(Object.assign(new Error(`@smithy/node-http-handler - the request socket did not establish a connection with the server within the configured timeout of ${timeoutInMs} ms.`), {
|
|
53
|
+
name: "TimeoutError",
|
|
54
|
+
}));
|
|
55
|
+
}, timeoutInMs - offset);
|
|
56
|
+
const doWithSocket = (socket) => {
|
|
57
|
+
if (socket?.connecting) {
|
|
58
|
+
socket.on("connect", () => {
|
|
59
|
+
timing.clearTimeout(timeoutId);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
timing.clearTimeout(timeoutId);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
if (request.socket) {
|
|
67
|
+
doWithSocket(request.socket);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
request.on("socket", doWithSocket);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (timeoutInMs < 2000) {
|
|
74
|
+
registerTimeout(0);
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
return timing.setTimeout(registerTimeout.bind(null, DEFER_EVENT_LISTENER_TIME$2), DEFER_EVENT_LISTENER_TIME$2);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const setRequestTimeout = (req, reject, timeoutInMs = 0, throwOnRequestTimeout, logger) => {
|
|
81
|
+
if (timeoutInMs) {
|
|
82
|
+
return timing.setTimeout(() => {
|
|
83
|
+
let msg = `@smithy/node-http-handler - [${throwOnRequestTimeout ? "ERROR" : "WARN"}] a request has exceeded the configured ${timeoutInMs} ms requestTimeout.`;
|
|
84
|
+
if (throwOnRequestTimeout) {
|
|
85
|
+
const error = Object.assign(new Error(msg), {
|
|
86
|
+
name: "TimeoutError",
|
|
87
|
+
code: "ETIMEDOUT",
|
|
88
|
+
});
|
|
89
|
+
req.destroy(error);
|
|
90
|
+
reject(error);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
msg += ` Init client requestHandler with throwOnRequestTimeout=true to turn this into an error.`;
|
|
94
|
+
logger?.warn?.(msg);
|
|
95
|
+
}
|
|
96
|
+
}, timeoutInMs);
|
|
97
|
+
}
|
|
98
|
+
return -1;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const DEFER_EVENT_LISTENER_TIME$1 = 3000;
|
|
102
|
+
const setSocketKeepAlive = (request, { keepAlive, keepAliveMsecs }, deferTimeMs = DEFER_EVENT_LISTENER_TIME$1) => {
|
|
103
|
+
if (keepAlive !== true) {
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
const registerListener = () => {
|
|
107
|
+
if (request.socket) {
|
|
108
|
+
request.socket.setKeepAlive(keepAlive, keepAliveMsecs || 0);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
request.on("socket", (socket) => {
|
|
112
|
+
socket.setKeepAlive(keepAlive, keepAliveMsecs || 0);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
if (deferTimeMs === 0) {
|
|
117
|
+
registerListener();
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
return timing.setTimeout(registerListener, deferTimeMs);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const DEFER_EVENT_LISTENER_TIME = 3000;
|
|
124
|
+
const setSocketTimeout = (request, reject, timeoutInMs = 0) => {
|
|
125
|
+
const registerTimeout = (offset) => {
|
|
126
|
+
const timeout = timeoutInMs - offset;
|
|
127
|
+
const onTimeout = () => {
|
|
128
|
+
request.destroy();
|
|
129
|
+
reject(Object.assign(new Error(`@smithy/node-http-handler - the request socket timed out after ${timeoutInMs} ms of inactivity (configured by client requestHandler).`), { name: "TimeoutError" }));
|
|
130
|
+
};
|
|
131
|
+
if (request.socket) {
|
|
132
|
+
request.socket.setTimeout(timeout, onTimeout);
|
|
133
|
+
request.on("close", () => request.socket?.removeListener("timeout", onTimeout));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
request.setTimeout(timeout, onTimeout);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
if (0 < timeoutInMs && timeoutInMs < 6000) {
|
|
140
|
+
registerTimeout(0);
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
return timing.setTimeout(registerTimeout.bind(null, timeoutInMs === 0 ? 0 : DEFER_EVENT_LISTENER_TIME), DEFER_EVENT_LISTENER_TIME);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const MIN_WAIT_TIME = 6_000;
|
|
147
|
+
async function writeRequestBody(httpRequest, request, maxContinueTimeoutMs = MIN_WAIT_TIME, externalAgent = false) {
|
|
148
|
+
const headers = request.headers;
|
|
149
|
+
const expect = headers ? headers.Expect || headers.expect : undefined;
|
|
150
|
+
let timeoutId = -1;
|
|
151
|
+
let sendBody = true;
|
|
152
|
+
if (!externalAgent && expect === "100-continue") {
|
|
153
|
+
sendBody = await Promise.race([
|
|
154
|
+
new Promise((resolve) => {
|
|
155
|
+
timeoutId = Number(timing.setTimeout(() => resolve(true), Math.max(MIN_WAIT_TIME, maxContinueTimeoutMs)));
|
|
156
|
+
}),
|
|
157
|
+
new Promise((resolve) => {
|
|
158
|
+
httpRequest.on("continue", () => {
|
|
159
|
+
timing.clearTimeout(timeoutId);
|
|
160
|
+
resolve(true);
|
|
161
|
+
});
|
|
162
|
+
httpRequest.on("response", () => {
|
|
163
|
+
timing.clearTimeout(timeoutId);
|
|
164
|
+
resolve(false);
|
|
165
|
+
});
|
|
166
|
+
httpRequest.on("error", () => {
|
|
167
|
+
timing.clearTimeout(timeoutId);
|
|
168
|
+
resolve(false);
|
|
169
|
+
});
|
|
170
|
+
}),
|
|
171
|
+
]);
|
|
172
|
+
}
|
|
173
|
+
if (sendBody) {
|
|
174
|
+
writeBody(httpRequest, request.body);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function writeBody(httpRequest, body) {
|
|
178
|
+
if (body instanceof node_stream.Readable) {
|
|
179
|
+
body.pipe(httpRequest);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (body) {
|
|
183
|
+
const isBuffer = Buffer.isBuffer(body);
|
|
184
|
+
const isString = typeof body === "string";
|
|
185
|
+
if (isBuffer || isString) {
|
|
186
|
+
if (isBuffer && body.byteLength === 0) {
|
|
187
|
+
httpRequest.end();
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
httpRequest.end(body);
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const uint8 = body;
|
|
195
|
+
if (typeof uint8 === "object" &&
|
|
196
|
+
uint8.buffer &&
|
|
197
|
+
typeof uint8.byteOffset === "number" &&
|
|
198
|
+
typeof uint8.byteLength === "number") {
|
|
199
|
+
httpRequest.end(Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
httpRequest.end(Buffer.from(body));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
httpRequest.end();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const DEFAULT_REQUEST_TIMEOUT = 0;
|
|
209
|
+
let hAgent = undefined;
|
|
210
|
+
let hRequest = undefined;
|
|
211
|
+
class NodeHttpHandler {
|
|
212
|
+
config;
|
|
213
|
+
configProvider;
|
|
214
|
+
socketWarningTimestamp = 0;
|
|
215
|
+
externalAgent = false;
|
|
216
|
+
metadata = { handlerProtocol: "http/1.1" };
|
|
217
|
+
static create(instanceOrOptions) {
|
|
218
|
+
if (typeof instanceOrOptions?.handle === "function") {
|
|
219
|
+
return instanceOrOptions;
|
|
220
|
+
}
|
|
221
|
+
return new NodeHttpHandler(instanceOrOptions);
|
|
222
|
+
}
|
|
223
|
+
static checkSocketUsage(agent, socketWarningTimestamp, logger = console) {
|
|
224
|
+
const { sockets, requests, maxSockets } = agent;
|
|
225
|
+
if (typeof maxSockets !== "number" || maxSockets === Infinity) {
|
|
226
|
+
return socketWarningTimestamp;
|
|
227
|
+
}
|
|
228
|
+
const interval = 15_000;
|
|
229
|
+
if (Date.now() - interval < socketWarningTimestamp) {
|
|
230
|
+
return socketWarningTimestamp;
|
|
231
|
+
}
|
|
232
|
+
if (sockets && requests) {
|
|
233
|
+
for (const origin in sockets) {
|
|
234
|
+
const socketsInUse = sockets[origin]?.length ?? 0;
|
|
235
|
+
const requestsEnqueued = requests[origin]?.length ?? 0;
|
|
236
|
+
if (socketsInUse >= maxSockets && requestsEnqueued >= 2 * maxSockets) {
|
|
237
|
+
logger?.warn?.(`@smithy/node-http-handler:WARN - socket usage at capacity=${socketsInUse} and ${requestsEnqueued} additional requests are enqueued.
|
|
238
|
+
See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html
|
|
239
|
+
or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config.`);
|
|
240
|
+
return Date.now();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return socketWarningTimestamp;
|
|
245
|
+
}
|
|
246
|
+
constructor(options) {
|
|
247
|
+
this.configProvider = new Promise((resolve, reject) => {
|
|
248
|
+
if (typeof options === "function") {
|
|
249
|
+
options()
|
|
250
|
+
.then((_options) => {
|
|
251
|
+
resolve(this.resolveDefaultConfig(_options));
|
|
252
|
+
})
|
|
253
|
+
.catch(reject);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
resolve(this.resolveDefaultConfig(options));
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
destroy() {
|
|
261
|
+
this.config?.httpAgent?.destroy();
|
|
262
|
+
this.config?.httpsAgent?.destroy();
|
|
263
|
+
}
|
|
264
|
+
async handle(request, { abortSignal, requestTimeout } = {}) {
|
|
265
|
+
if (!this.config) {
|
|
266
|
+
this.config = await this.configProvider;
|
|
267
|
+
}
|
|
268
|
+
const config = this.config;
|
|
269
|
+
const isSSL = request.protocol === "https:";
|
|
270
|
+
if (!isSSL && !this.config.httpAgent) {
|
|
271
|
+
this.config.httpAgent = await this.config.httpAgentProvider();
|
|
272
|
+
}
|
|
273
|
+
return new Promise((_resolve, _reject) => {
|
|
274
|
+
let writeRequestBodyPromise = undefined;
|
|
275
|
+
let socketWarningTimeoutId = -1;
|
|
276
|
+
let connectionTimeoutId = -1;
|
|
277
|
+
let requestTimeoutId = -1;
|
|
278
|
+
let socketTimeoutId = -1;
|
|
279
|
+
let keepAliveTimeoutId = -1;
|
|
280
|
+
const clearTimeouts = () => {
|
|
281
|
+
timing.clearTimeout(socketWarningTimeoutId);
|
|
282
|
+
timing.clearTimeout(connectionTimeoutId);
|
|
283
|
+
timing.clearTimeout(requestTimeoutId);
|
|
284
|
+
timing.clearTimeout(socketTimeoutId);
|
|
285
|
+
timing.clearTimeout(keepAliveTimeoutId);
|
|
286
|
+
};
|
|
287
|
+
const resolve = async (arg) => {
|
|
288
|
+
await writeRequestBodyPromise;
|
|
289
|
+
clearTimeouts();
|
|
290
|
+
_resolve(arg);
|
|
291
|
+
};
|
|
292
|
+
const reject = async (arg) => {
|
|
293
|
+
await writeRequestBodyPromise;
|
|
294
|
+
clearTimeouts();
|
|
295
|
+
_reject(arg);
|
|
296
|
+
};
|
|
297
|
+
if (abortSignal?.aborted) {
|
|
298
|
+
const abortError = buildAbortError(abortSignal);
|
|
299
|
+
reject(abortError);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const headers = request.headers;
|
|
303
|
+
const expectContinue = headers ? (headers.Expect ?? headers.expect) === "100-continue" : false;
|
|
304
|
+
let agent = isSSL ? config.httpsAgent : config.httpAgent;
|
|
305
|
+
if (expectContinue && !this.externalAgent) {
|
|
306
|
+
agent = new (isSSL ? node_https.Agent : hAgent)({
|
|
307
|
+
keepAlive: false,
|
|
308
|
+
maxSockets: Infinity,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
socketWarningTimeoutId = timing.setTimeout(() => {
|
|
312
|
+
this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage(agent, this.socketWarningTimestamp, config.logger);
|
|
313
|
+
}, config.socketAcquisitionWarningTimeout ?? (config.requestTimeout ?? 2000) + (config.connectionTimeout ?? 1000));
|
|
314
|
+
const queryString = request.query ? protocols.buildQueryString(request.query) : "";
|
|
315
|
+
let auth = undefined;
|
|
316
|
+
if (request.username != null || request.password != null) {
|
|
317
|
+
const username = request.username ?? "";
|
|
318
|
+
const password = request.password ?? "";
|
|
319
|
+
auth = `${username}:${password}`;
|
|
320
|
+
}
|
|
321
|
+
let path = request.path;
|
|
322
|
+
if (queryString) {
|
|
323
|
+
path += `?${queryString}`;
|
|
324
|
+
}
|
|
325
|
+
if (request.fragment) {
|
|
326
|
+
path += `#${request.fragment}`;
|
|
327
|
+
}
|
|
328
|
+
let hostname = request.hostname ?? "";
|
|
329
|
+
if (hostname[0] === "[" && hostname.endsWith("]")) {
|
|
330
|
+
hostname = request.hostname.slice(1, -1);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
hostname = request.hostname;
|
|
334
|
+
}
|
|
335
|
+
const nodeHttpsOptions = {
|
|
336
|
+
headers: request.headers,
|
|
337
|
+
host: hostname,
|
|
338
|
+
method: request.method,
|
|
339
|
+
path,
|
|
340
|
+
port: request.port,
|
|
341
|
+
agent,
|
|
342
|
+
auth,
|
|
343
|
+
};
|
|
344
|
+
const requestFunc = isSSL ? node_https.request : hRequest;
|
|
345
|
+
const req = requestFunc(nodeHttpsOptions, (res) => {
|
|
346
|
+
const httpResponse = new protocols.HttpResponse({
|
|
347
|
+
statusCode: res.statusCode || -1,
|
|
348
|
+
reason: res.statusMessage,
|
|
349
|
+
headers: getTransformedHeaders(res.headers),
|
|
350
|
+
body: res,
|
|
351
|
+
});
|
|
352
|
+
resolve({ response: httpResponse });
|
|
353
|
+
});
|
|
354
|
+
req.on("error", (err) => {
|
|
355
|
+
if (NODEJS_TIMEOUT_ERROR_CODES.includes(err.code)) {
|
|
356
|
+
reject(Object.assign(err, { name: "TimeoutError" }));
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
reject(err);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
if (abortSignal) {
|
|
363
|
+
const onAbort = () => {
|
|
364
|
+
req.destroy();
|
|
365
|
+
const abortError = buildAbortError(abortSignal);
|
|
366
|
+
reject(abortError);
|
|
367
|
+
};
|
|
368
|
+
if (typeof abortSignal.addEventListener === "function") {
|
|
369
|
+
const signal = abortSignal;
|
|
370
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
371
|
+
req.once("close", () => signal.removeEventListener("abort", onAbort));
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
abortSignal.onabort = onAbort;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const effectiveRequestTimeout = requestTimeout ?? config.requestTimeout;
|
|
378
|
+
connectionTimeoutId = setConnectionTimeout(req, reject, config.connectionTimeout);
|
|
379
|
+
requestTimeoutId = setRequestTimeout(req, reject, effectiveRequestTimeout, config.throwOnRequestTimeout, config.logger ?? console);
|
|
380
|
+
socketTimeoutId = setSocketTimeout(req, reject, config.socketTimeout);
|
|
381
|
+
const httpAgent = nodeHttpsOptions.agent;
|
|
382
|
+
if (typeof httpAgent === "object" && "keepAlive" in httpAgent) {
|
|
383
|
+
keepAliveTimeoutId = setSocketKeepAlive(req, {
|
|
384
|
+
keepAlive: httpAgent.keepAlive,
|
|
385
|
+
keepAliveMsecs: httpAgent.keepAliveMsecs,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
writeRequestBodyPromise = writeRequestBody(req, request, effectiveRequestTimeout, this.externalAgent).catch((e) => {
|
|
389
|
+
clearTimeouts();
|
|
390
|
+
return _reject(e);
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
updateHttpClientConfig(key, value) {
|
|
395
|
+
this.config = undefined;
|
|
396
|
+
this.configProvider = this.configProvider.then((config) => {
|
|
397
|
+
return {
|
|
398
|
+
...config,
|
|
399
|
+
[key]: value,
|
|
400
|
+
};
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
httpHandlerConfigs() {
|
|
404
|
+
return this.config ?? {};
|
|
405
|
+
}
|
|
406
|
+
resolveDefaultConfig(options) {
|
|
407
|
+
const { requestTimeout, connectionTimeout, socketTimeout, socketAcquisitionWarningTimeout, httpAgent, httpsAgent, throwOnRequestTimeout, logger, } = options || {};
|
|
408
|
+
const keepAlive = true;
|
|
409
|
+
const maxSockets = 50;
|
|
410
|
+
return {
|
|
411
|
+
connectionTimeout,
|
|
412
|
+
requestTimeout,
|
|
413
|
+
socketTimeout,
|
|
414
|
+
socketAcquisitionWarningTimeout,
|
|
415
|
+
throwOnRequestTimeout,
|
|
416
|
+
httpAgentProvider: async () => {
|
|
417
|
+
const { Agent, request } = await import('node:http');
|
|
418
|
+
hRequest = request;
|
|
419
|
+
hAgent = Agent;
|
|
420
|
+
if (httpAgent instanceof hAgent || typeof httpAgent?.destroy === "function") {
|
|
421
|
+
this.externalAgent = true;
|
|
422
|
+
return httpAgent;
|
|
423
|
+
}
|
|
424
|
+
return new hAgent({ keepAlive, maxSockets, ...httpAgent });
|
|
425
|
+
},
|
|
426
|
+
httpsAgent: (() => {
|
|
427
|
+
if (httpsAgent instanceof node_https.Agent || typeof httpsAgent?.destroy === "function") {
|
|
428
|
+
this.externalAgent = true;
|
|
429
|
+
return httpsAgent;
|
|
430
|
+
}
|
|
431
|
+
return new node_https.Agent({ keepAlive, maxSockets, ...httpsAgent });
|
|
432
|
+
})(),
|
|
433
|
+
logger,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const ids = new Uint16Array(1);
|
|
439
|
+
class ClientHttp2SessionRef {
|
|
440
|
+
id = ids[0]++;
|
|
441
|
+
total = 0;
|
|
442
|
+
max = 0;
|
|
443
|
+
session;
|
|
444
|
+
refs = 0;
|
|
445
|
+
constructor(session) {
|
|
446
|
+
session.unref();
|
|
447
|
+
this.session = session;
|
|
448
|
+
}
|
|
449
|
+
retain() {
|
|
450
|
+
if (this.session.destroyed) {
|
|
451
|
+
throw new Error("@smithy/node-http-handler - cannot acquire reference to destroyed session.");
|
|
452
|
+
}
|
|
453
|
+
this.refs += 1;
|
|
454
|
+
this.total += 1;
|
|
455
|
+
this.max = Math.max(this.refs, this.max);
|
|
456
|
+
this.session.ref();
|
|
457
|
+
}
|
|
458
|
+
free() {
|
|
459
|
+
if (this.session.destroyed) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
this.refs -= 1;
|
|
463
|
+
if (this.refs === 0) {
|
|
464
|
+
this.session.unref();
|
|
465
|
+
}
|
|
466
|
+
if (this.refs < 0) {
|
|
467
|
+
throw new Error("@smithy/node-http-handler - ClientHttp2Session refcount at zero, cannot decrement.");
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
deref() {
|
|
471
|
+
return this.session;
|
|
472
|
+
}
|
|
473
|
+
close() {
|
|
474
|
+
if (!this.session.closed) {
|
|
475
|
+
this.session.close();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
destroy() {
|
|
479
|
+
this.refs = 0;
|
|
480
|
+
if (!this.session.destroyed) {
|
|
481
|
+
this.session.destroy();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
useCount() {
|
|
485
|
+
return this.refs;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
class NodeHttp2ConnectionPool {
|
|
490
|
+
sessions = [];
|
|
491
|
+
maxConcurrency = 0;
|
|
492
|
+
constructor(sessions) {
|
|
493
|
+
this.sessions = (sessions ?? []).map((session) => new ClientHttp2SessionRef(session));
|
|
494
|
+
}
|
|
495
|
+
poll() {
|
|
496
|
+
let cleanup = false;
|
|
497
|
+
for (const session of this.sessions) {
|
|
498
|
+
if (session.deref().destroyed) {
|
|
499
|
+
cleanup = true;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
if (!this.maxConcurrency || session.useCount() < this.maxConcurrency) {
|
|
503
|
+
return session;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (cleanup) {
|
|
507
|
+
for (const session of this.sessions) {
|
|
508
|
+
if (session.deref().destroyed) {
|
|
509
|
+
this.remove(session);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
offerLast(ref) {
|
|
515
|
+
this.sessions.push(ref);
|
|
516
|
+
}
|
|
517
|
+
remove(ref) {
|
|
518
|
+
const ix = this.sessions.indexOf(ref);
|
|
519
|
+
if (ix > -1) {
|
|
520
|
+
this.sessions.splice(ix, 1);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
[Symbol.iterator]() {
|
|
524
|
+
return this.sessions[Symbol.iterator]();
|
|
525
|
+
}
|
|
526
|
+
setMaxConcurrency(maxConcurrency) {
|
|
527
|
+
this.maxConcurrency = maxConcurrency;
|
|
528
|
+
}
|
|
529
|
+
destroy(ref) {
|
|
530
|
+
this.remove(ref);
|
|
531
|
+
ref.destroy();
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
class NodeHttp2ConnectionManager {
|
|
536
|
+
config;
|
|
537
|
+
connectOptions;
|
|
538
|
+
connectionPools = new Map();
|
|
539
|
+
constructor(config) {
|
|
540
|
+
this.config = config;
|
|
541
|
+
if (this.config.maxConcurrency && this.config.maxConcurrency <= 0) {
|
|
542
|
+
throw new RangeError("maxConcurrency must be greater than zero.");
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
lease(requestContext, connectionConfiguration) {
|
|
546
|
+
const url = this.getUrlString(requestContext);
|
|
547
|
+
const pool = this.getPool(url);
|
|
548
|
+
if (!this.config.disableConcurrency && !connectionConfiguration.isEventStream) {
|
|
549
|
+
const available = pool.poll();
|
|
550
|
+
if (available) {
|
|
551
|
+
available.retain();
|
|
552
|
+
return available;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const ref = new ClientHttp2SessionRef(this.connect(url));
|
|
556
|
+
const session = ref.deref();
|
|
557
|
+
if (this.config.maxConcurrency) {
|
|
558
|
+
session.settings({ maxConcurrentStreams: this.config.maxConcurrency }, (err) => {
|
|
559
|
+
if (err) {
|
|
560
|
+
throw new Error("Fail to set maxConcurrentStreams to " +
|
|
561
|
+
this.config.maxConcurrency +
|
|
562
|
+
"when creating new session for " +
|
|
563
|
+
requestContext.destination.toString());
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
const graceful = () => {
|
|
568
|
+
this.removeFromPoolAndClose(url, ref);
|
|
569
|
+
};
|
|
570
|
+
const ensureDestroyed = () => {
|
|
571
|
+
this.removeFromPoolAndCheckedDestroy(url, ref);
|
|
572
|
+
};
|
|
573
|
+
session.on("goaway", graceful);
|
|
574
|
+
session.on("error", ensureDestroyed);
|
|
575
|
+
session.on("frameError", ensureDestroyed);
|
|
576
|
+
session.on("close", ensureDestroyed);
|
|
577
|
+
if (connectionConfiguration.requestTimeout) {
|
|
578
|
+
session.setTimeout(connectionConfiguration.requestTimeout, ensureDestroyed);
|
|
579
|
+
}
|
|
580
|
+
pool.offerLast(ref);
|
|
581
|
+
ref.retain();
|
|
582
|
+
return ref;
|
|
583
|
+
}
|
|
584
|
+
release(_requestContext, ref) {
|
|
585
|
+
ref.free();
|
|
586
|
+
}
|
|
587
|
+
createIsolatedSession(requestContext, connectionConfiguration) {
|
|
588
|
+
const url = this.getUrlString(requestContext);
|
|
589
|
+
const ref = new ClientHttp2SessionRef(this.connect(url));
|
|
590
|
+
const session = ref.deref();
|
|
591
|
+
session.settings({ maxConcurrentStreams: 1 });
|
|
592
|
+
const ensureDestroyed = () => {
|
|
593
|
+
ref.destroy();
|
|
594
|
+
};
|
|
595
|
+
session.on("error", ensureDestroyed);
|
|
596
|
+
session.on("frameError", ensureDestroyed);
|
|
597
|
+
session.on("close", ensureDestroyed);
|
|
598
|
+
if (connectionConfiguration.requestTimeout) {
|
|
599
|
+
session.setTimeout(connectionConfiguration.requestTimeout, ensureDestroyed);
|
|
600
|
+
}
|
|
601
|
+
ref.retain();
|
|
602
|
+
return ref;
|
|
603
|
+
}
|
|
604
|
+
destroy() {
|
|
605
|
+
for (const [url, connectionPool] of this.connectionPools) {
|
|
606
|
+
for (const session of [...connectionPool]) {
|
|
607
|
+
session.destroy();
|
|
608
|
+
}
|
|
609
|
+
this.connectionPools.delete(url);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
setMaxConcurrentStreams(maxConcurrentStreams) {
|
|
613
|
+
if (maxConcurrentStreams && maxConcurrentStreams <= 0) {
|
|
614
|
+
throw new RangeError("maxConcurrentStreams must be greater than zero.");
|
|
615
|
+
}
|
|
616
|
+
this.config.maxConcurrency = maxConcurrentStreams;
|
|
617
|
+
for (const pool of this.connectionPools.values()) {
|
|
618
|
+
pool.setMaxConcurrency(maxConcurrentStreams);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
setDisableConcurrentStreams(disableConcurrentStreams) {
|
|
622
|
+
this.config.disableConcurrency = disableConcurrentStreams;
|
|
623
|
+
}
|
|
624
|
+
setNodeHttp2ConnectOptions(nodeHttp2ConnectOptions) {
|
|
625
|
+
this.connectOptions = nodeHttp2ConnectOptions;
|
|
626
|
+
}
|
|
627
|
+
debug() {
|
|
628
|
+
const pools = {};
|
|
629
|
+
for (const [url, pool] of this.connectionPools) {
|
|
630
|
+
const sessions = [];
|
|
631
|
+
for (const ref of pool) {
|
|
632
|
+
sessions.push({
|
|
633
|
+
id: ref.id,
|
|
634
|
+
active: ref.useCount(),
|
|
635
|
+
maxConcurrent: ref.max,
|
|
636
|
+
totalRequests: ref.total,
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
pools[url] = { sessions };
|
|
640
|
+
}
|
|
641
|
+
return pools;
|
|
642
|
+
}
|
|
643
|
+
removeFromPoolAndClose(authority, ref) {
|
|
644
|
+
this.connectionPools.get(authority)?.remove(ref);
|
|
645
|
+
ref.close();
|
|
646
|
+
}
|
|
647
|
+
removeFromPoolAndCheckedDestroy(authority, ref) {
|
|
648
|
+
this.connectionPools.get(authority)?.remove(ref);
|
|
649
|
+
ref.destroy();
|
|
650
|
+
}
|
|
651
|
+
getPool(url) {
|
|
652
|
+
if (!this.connectionPools.has(url)) {
|
|
653
|
+
const pool = new NodeHttp2ConnectionPool();
|
|
654
|
+
if (this.config.maxConcurrency) {
|
|
655
|
+
pool.setMaxConcurrency(this.config.maxConcurrency);
|
|
656
|
+
}
|
|
657
|
+
this.connectionPools.set(url, pool);
|
|
658
|
+
}
|
|
659
|
+
return this.connectionPools.get(url);
|
|
660
|
+
}
|
|
661
|
+
getUrlString(request) {
|
|
662
|
+
return request.destination.toString();
|
|
663
|
+
}
|
|
664
|
+
connect(url) {
|
|
665
|
+
return this.connectOptions === undefined ? http2.connect(url) : http2.connect(url, this.connectOptions);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
class NodeHttp2Handler {
|
|
670
|
+
config;
|
|
671
|
+
configProvider;
|
|
672
|
+
metadata = { handlerProtocol: "h2" };
|
|
673
|
+
connectionManager = new NodeHttp2ConnectionManager({});
|
|
674
|
+
static create(instanceOrOptions) {
|
|
675
|
+
if (typeof instanceOrOptions?.handle === "function") {
|
|
676
|
+
return instanceOrOptions;
|
|
677
|
+
}
|
|
678
|
+
return new NodeHttp2Handler(instanceOrOptions);
|
|
679
|
+
}
|
|
680
|
+
constructor(options) {
|
|
681
|
+
this.configProvider = new Promise((resolve, reject) => {
|
|
682
|
+
if (typeof options === "function") {
|
|
683
|
+
options()
|
|
684
|
+
.then((opts) => {
|
|
685
|
+
resolve(opts || {});
|
|
686
|
+
})
|
|
687
|
+
.catch(reject);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
resolve(options || {});
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
destroy() {
|
|
695
|
+
this.connectionManager.destroy();
|
|
696
|
+
}
|
|
697
|
+
async handle(request, { abortSignal, requestTimeout, isEventStream } = {}) {
|
|
698
|
+
if (!this.config) {
|
|
699
|
+
this.config = await this.configProvider;
|
|
700
|
+
const { disableConcurrentStreams, maxConcurrentStreams, nodeHttp2ConnectOptions } = this.config;
|
|
701
|
+
this.connectionManager.setDisableConcurrentStreams(disableConcurrentStreams ?? false);
|
|
702
|
+
if (maxConcurrentStreams) {
|
|
703
|
+
this.connectionManager.setMaxConcurrentStreams(maxConcurrentStreams);
|
|
704
|
+
}
|
|
705
|
+
if (nodeHttp2ConnectOptions) {
|
|
706
|
+
this.connectionManager.setNodeHttp2ConnectOptions(nodeHttp2ConnectOptions);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
const { requestTimeout: configRequestTimeout, disableConcurrentStreams } = this.config;
|
|
710
|
+
const useIsolatedSession = disableConcurrentStreams || isEventStream;
|
|
711
|
+
const effectiveRequestTimeout = requestTimeout ?? configRequestTimeout;
|
|
712
|
+
return new Promise((_resolve, _reject) => {
|
|
713
|
+
let fulfilled = false;
|
|
714
|
+
let writeRequestBodyPromise = undefined;
|
|
715
|
+
const resolve = async (arg) => {
|
|
716
|
+
await writeRequestBodyPromise;
|
|
717
|
+
_resolve(arg);
|
|
718
|
+
};
|
|
719
|
+
const reject = async (arg) => {
|
|
720
|
+
await writeRequestBodyPromise;
|
|
721
|
+
_reject(arg);
|
|
722
|
+
};
|
|
723
|
+
if (abortSignal?.aborted) {
|
|
724
|
+
fulfilled = true;
|
|
725
|
+
const abortError = buildAbortError(abortSignal);
|
|
726
|
+
reject(abortError);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
const { hostname, method, port, protocol, query } = request;
|
|
730
|
+
let auth = "";
|
|
731
|
+
if (request.username != null || request.password != null) {
|
|
732
|
+
const username = request.username ?? "";
|
|
733
|
+
const password = request.password ?? "";
|
|
734
|
+
auth = `${username}:${password}@`;
|
|
735
|
+
}
|
|
736
|
+
const authority = `${protocol}//${auth}${hostname}${port ? `:${port}` : ""}`;
|
|
737
|
+
const requestContext = { destination: new URL(authority) };
|
|
738
|
+
const connectConfig = {
|
|
739
|
+
requestTimeout: this.config?.sessionTimeout,
|
|
740
|
+
isEventStream,
|
|
741
|
+
};
|
|
742
|
+
const ref = useIsolatedSession
|
|
743
|
+
? this.connectionManager.createIsolatedSession(requestContext, connectConfig)
|
|
744
|
+
: this.connectionManager.lease(requestContext, connectConfig);
|
|
745
|
+
const session = ref.deref();
|
|
746
|
+
const rejectWithDestroy = (err) => {
|
|
747
|
+
if (useIsolatedSession) {
|
|
748
|
+
ref.destroy();
|
|
749
|
+
}
|
|
750
|
+
fulfilled = true;
|
|
751
|
+
reject(err);
|
|
752
|
+
};
|
|
753
|
+
const queryString = query ? protocols.buildQueryString(query) : "";
|
|
754
|
+
let path = request.path;
|
|
755
|
+
if (queryString) {
|
|
756
|
+
path += `?${queryString}`;
|
|
757
|
+
}
|
|
758
|
+
if (request.fragment) {
|
|
759
|
+
path += `#${request.fragment}`;
|
|
760
|
+
}
|
|
761
|
+
const clientHttp2Stream = session.request({
|
|
762
|
+
...request.headers,
|
|
763
|
+
[http2.constants.HTTP2_HEADER_PATH]: path,
|
|
764
|
+
[http2.constants.HTTP2_HEADER_METHOD]: method,
|
|
765
|
+
});
|
|
766
|
+
if (effectiveRequestTimeout) {
|
|
767
|
+
clientHttp2Stream.setTimeout(effectiveRequestTimeout, () => {
|
|
768
|
+
clientHttp2Stream.close();
|
|
769
|
+
const timeoutError = new Error(`Stream timed out because of no activity for ${effectiveRequestTimeout} ms`);
|
|
770
|
+
timeoutError.name = "TimeoutError";
|
|
771
|
+
rejectWithDestroy(timeoutError);
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
if (abortSignal) {
|
|
775
|
+
const onAbort = () => {
|
|
776
|
+
clientHttp2Stream.close();
|
|
777
|
+
const abortError = buildAbortError(abortSignal);
|
|
778
|
+
rejectWithDestroy(abortError);
|
|
779
|
+
};
|
|
780
|
+
if (typeof abortSignal.addEventListener === "function") {
|
|
781
|
+
const signal = abortSignal;
|
|
782
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
783
|
+
clientHttp2Stream.once("close", () => signal.removeEventListener("abort", onAbort));
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
abortSignal.onabort = onAbort;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
clientHttp2Stream.on("frameError", (type, code, id) => {
|
|
790
|
+
rejectWithDestroy(new Error(`Frame type id ${type} in stream id ${id} has failed with code ${code}.`));
|
|
791
|
+
});
|
|
792
|
+
clientHttp2Stream.on("error", rejectWithDestroy);
|
|
793
|
+
clientHttp2Stream.on("aborted", () => {
|
|
794
|
+
rejectWithDestroy(new Error(`HTTP/2 stream is abnormally aborted in mid-communication with result code ${clientHttp2Stream.rstCode}.`));
|
|
795
|
+
});
|
|
796
|
+
clientHttp2Stream.on("response", (headers) => {
|
|
797
|
+
const httpResponse = new protocols.HttpResponse({
|
|
798
|
+
statusCode: headers[":status"] ?? -1,
|
|
799
|
+
headers: getTransformedHeaders(headers),
|
|
800
|
+
body: clientHttp2Stream,
|
|
801
|
+
});
|
|
802
|
+
fulfilled = true;
|
|
803
|
+
resolve({ response: httpResponse });
|
|
804
|
+
if (useIsolatedSession) {
|
|
805
|
+
session.close();
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
clientHttp2Stream.on("close", () => {
|
|
809
|
+
if (useIsolatedSession) {
|
|
810
|
+
ref.destroy();
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
this.connectionManager.release(requestContext, ref);
|
|
814
|
+
}
|
|
815
|
+
if (!fulfilled) {
|
|
816
|
+
rejectWithDestroy(new Error("Unexpected error: http2 request did not get a response"));
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
writeRequestBodyPromise = writeRequestBody(clientHttp2Stream, request, effectiveRequestTimeout);
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
updateHttpClientConfig(key, value) {
|
|
823
|
+
this.config = undefined;
|
|
824
|
+
this.configProvider = this.configProvider.then((config) => {
|
|
825
|
+
return {
|
|
826
|
+
...config,
|
|
827
|
+
[key]: value,
|
|
828
|
+
};
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
httpHandlerConfigs() {
|
|
832
|
+
return this.config ?? {};
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
class Collector extends node_stream.Writable {
|
|
837
|
+
bufferedBytes = [];
|
|
838
|
+
_write(chunk, encoding, callback) {
|
|
839
|
+
this.bufferedBytes.push(chunk);
|
|
840
|
+
callback();
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
const streamCollector = (stream) => {
|
|
845
|
+
if (isReadableStreamInstance(stream)) {
|
|
846
|
+
return collectReadableStream(stream);
|
|
847
|
+
}
|
|
848
|
+
return new Promise((resolve, reject) => {
|
|
849
|
+
const collector = new Collector();
|
|
850
|
+
stream.pipe(collector);
|
|
851
|
+
stream.on("error", (err) => {
|
|
852
|
+
collector.end();
|
|
853
|
+
reject(err);
|
|
854
|
+
});
|
|
855
|
+
collector.on("error", reject);
|
|
856
|
+
collector.on("finish", function () {
|
|
857
|
+
const bytes = new Uint8Array(Buffer.concat(this.bufferedBytes));
|
|
858
|
+
resolve(bytes);
|
|
859
|
+
});
|
|
860
|
+
});
|
|
861
|
+
};
|
|
862
|
+
const isReadableStreamInstance = (stream) => typeof ReadableStream === "function" && stream instanceof ReadableStream;
|
|
863
|
+
async function collectReadableStream(stream) {
|
|
864
|
+
const chunks = [];
|
|
865
|
+
const reader = stream.getReader();
|
|
866
|
+
let isDone = false;
|
|
867
|
+
let length = 0;
|
|
868
|
+
while (!isDone) {
|
|
869
|
+
const { done, value } = await reader.read();
|
|
870
|
+
if (value) {
|
|
871
|
+
chunks.push(value);
|
|
872
|
+
length += value.length;
|
|
873
|
+
}
|
|
874
|
+
isDone = done;
|
|
875
|
+
}
|
|
876
|
+
const collected = new Uint8Array(length);
|
|
877
|
+
let offset = 0;
|
|
878
|
+
for (const chunk of chunks) {
|
|
879
|
+
collected.set(chunk, offset);
|
|
880
|
+
offset += chunk.length;
|
|
881
|
+
}
|
|
882
|
+
return collected;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
exports.DEFAULT_REQUEST_TIMEOUT = DEFAULT_REQUEST_TIMEOUT;
|
|
886
|
+
exports.NodeHttp2Handler = NodeHttp2Handler;
|
|
887
|
+
exports.NodeHttpHandler = NodeHttpHandler;
|
|
888
|
+
exports.streamCollector = streamCollector;
|