@songtonyli/dsh-cli 0.1.85 → 0.1.87

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.
Files changed (77) hide show
  1. package/README.md +1 -1
  2. package/bin/dsh.mjs +1 -1
  3. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/LICENSE +201 -0
  4. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/README.md +9 -0
  5. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-cjs/index.js +859 -0
  6. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/build-abort-error.js +19 -0
  7. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/constants.js +1 -0
  8. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/get-transformed-headers.js +12 -0
  9. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/http2/ClientHttp2SessionRef.js +51 -0
  10. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/index.js +3 -0
  11. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-http-handler.js +251 -0
  12. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-manager.js +135 -0
  13. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-pool.js +46 -0
  14. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-http2-handler.js +180 -0
  15. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-http2.js +2 -0
  16. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/node-https.js +2 -0
  17. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/set-connection-timeout.js +36 -0
  18. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/set-request-timeout.js +21 -0
  19. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/set-socket-keep-alive.js +22 -0
  20. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/set-socket-timeout.js +23 -0
  21. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/timing.js +4 -0
  22. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/dist-es/write-request-body.js +63 -0
  23. package/node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/node-http-handler/package.json +56 -0
  24. package/node_modules/@deepseek-ai/dsh-client-modules/lib/index.js +1 -1
  25. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  26. package/node_modules/@deepseek-ai/dsh-llm-pi-ai/lib/index.js +39 -52
  27. package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +6 -6
  28. package/node_modules/@deepseek-ai/dsh-tui-app/README.md +3 -2
  29. package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +3 -2
  30. package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +243 -25
  31. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/LICENSE +201 -0
  32. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/README.md +9 -0
  33. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-cjs/index.js +859 -0
  34. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/build-abort-error.js +19 -0
  35. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/constants.js +1 -0
  36. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/get-transformed-headers.js +12 -0
  37. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/http2/ClientHttp2SessionRef.js +51 -0
  38. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/index.js +3 -0
  39. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-http-handler.js +251 -0
  40. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-manager.js +135 -0
  41. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-http2-connection-pool.js +46 -0
  42. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-http2-handler.js +180 -0
  43. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-http2.js +2 -0
  44. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/node-https.js +2 -0
  45. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/set-connection-timeout.js +36 -0
  46. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/set-request-timeout.js +21 -0
  47. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/set-socket-keep-alive.js +22 -0
  48. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/set-socket-timeout.js +23 -0
  49. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/timing.js +4 -0
  50. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/dist-es/write-request-body.js +63 -0
  51. package/node_modules/@earendil-works/pi-ai/node_modules/@smithy/node-http-handler/package.json +56 -0
  52. package/node_modules/@smithy/core/dist-cjs/submodules/cbor/index.js +13 -37
  53. package/node_modules/@smithy/core/dist-cjs/submodules/protocols/index.js +44 -29
  54. package/node_modules/@smithy/core/dist-es/submodules/cbor/SmithyRpcV2CborProtocol.js +12 -37
  55. package/node_modules/@smithy/core/dist-es/submodules/protocols/AbstractRpcV2Protocol.js +35 -0
  56. package/node_modules/@smithy/core/dist-es/submodules/protocols/RpcProtocol.js +4 -1
  57. package/node_modules/@smithy/core/dist-es/submodules/protocols/index.js +1 -0
  58. package/node_modules/@smithy/core/dist-es/submodules/protocols/json/SmithyRpcV2JsonProtocol.js +8 -31
  59. package/node_modules/@smithy/core/package.json +1 -1
  60. package/node_modules/@smithy/node-http-handler/dist-cjs/index.js +48 -6
  61. package/node_modules/@smithy/node-http-handler/dist-es/node-http-handler.js +9 -1
  62. package/node_modules/@smithy/node-http-handler/dist-es/node-http2-handler.js +20 -2
  63. package/node_modules/@smithy/node-http-handler/dist-es/resolve-default-socket-timeout.js +4 -0
  64. package/node_modules/@smithy/node-http-handler/dist-es/set-socket-timeout.js +16 -3
  65. package/node_modules/@smithy/node-http-handler/package.json +3 -3
  66. package/node_modules/execa/package.json +1 -1
  67. package/node_modules/unist-util-visit/lib/index.js +173 -161
  68. package/node_modules/unist-util-visit/license +1 -1
  69. package/node_modules/unist-util-visit/package.json +46 -56
  70. package/node_modules/unist-util-visit/readme.md +87 -27
  71. package/node_modules/unist-util-visit-parents/lib/color.js +4 -0
  72. package/node_modules/unist-util-visit-parents/lib/color.node.js +4 -0
  73. package/node_modules/unist-util-visit-parents/lib/index.js +235 -52
  74. package/node_modules/unist-util-visit-parents/license +1 -1
  75. package/node_modules/unist-util-visit-parents/package.json +44 -54
  76. package/node_modules/unist-util-visit-parents/readme.md +185 -74
  77. package/package.json +6 -6
@@ -0,0 +1,19 @@
1
+ export function buildAbortError(abortSignal) {
2
+ const reason = abortSignal && typeof abortSignal === "object" && "reason" in abortSignal
3
+ ? abortSignal.reason
4
+ : undefined;
5
+ if (reason) {
6
+ if (reason instanceof Error) {
7
+ const abortError = new Error("Request aborted");
8
+ abortError.name = "AbortError";
9
+ abortError.cause = reason;
10
+ return abortError;
11
+ }
12
+ const abortError = new Error(String(reason));
13
+ abortError.name = "AbortError";
14
+ return abortError;
15
+ }
16
+ const abortError = new Error("Request aborted");
17
+ abortError.name = "AbortError";
18
+ return abortError;
19
+ }
@@ -0,0 +1 @@
1
+ export const NODEJS_TIMEOUT_ERROR_CODES = ["ECONNRESET", "EPIPE", "ETIMEDOUT"];
@@ -0,0 +1,12 @@
1
+ import { hasOwn } from "@smithy/core/serde";
2
+ const getTransformedHeaders = (headers) => {
3
+ const transformedHeaders = {};
4
+ for (const name in headers) {
5
+ if (!hasOwn(headers, name))
6
+ continue;
7
+ const headerValues = headers[name];
8
+ transformedHeaders[name] = Array.isArray(headerValues) ? headerValues.join(",") : headerValues;
9
+ }
10
+ return transformedHeaders;
11
+ };
12
+ export { getTransformedHeaders };
@@ -0,0 +1,51 @@
1
+ const ids = new Uint16Array(1);
2
+ export class ClientHttp2SessionRef {
3
+ id = ids[0]++;
4
+ total = 0;
5
+ max = 0;
6
+ session;
7
+ refs = 0;
8
+ constructor(session) {
9
+ session.unref();
10
+ this.session = session;
11
+ }
12
+ retain() {
13
+ if (this.session.destroyed) {
14
+ throw new Error("@smithy/node-http-handler - cannot acquire reference to destroyed session.");
15
+ }
16
+ this.refs += 1;
17
+ this.total += 1;
18
+ this.max = Math.max(this.refs, this.max);
19
+ this.session.ref();
20
+ }
21
+ free() {
22
+ if (this.session.destroyed) {
23
+ return;
24
+ }
25
+ this.refs -= 1;
26
+ if (this.refs === 0) {
27
+ this.session.unref();
28
+ }
29
+ if (this.refs < 0) {
30
+ throw new Error("@smithy/node-http-handler - ClientHttp2Session refcount at zero, cannot decrement.");
31
+ }
32
+ }
33
+ deref() {
34
+ return this.session;
35
+ }
36
+ close() {
37
+ if (!this.session.closed) {
38
+ this.session.close();
39
+ }
40
+ }
41
+ destroy() {
42
+ this.refs = 0;
43
+ if (!this.session.destroyed) {
44
+ this.session.setTimeout(0);
45
+ this.session.destroy();
46
+ }
47
+ }
48
+ useCount() {
49
+ return this.refs;
50
+ }
51
+ }
@@ -0,0 +1,3 @@
1
+ export { DEFAULT_REQUEST_TIMEOUT, NodeHttpHandler } from "./node-http-handler";
2
+ export { NodeHttp2Handler } from "./node-http2-handler";
3
+ export { streamCollector } from "@smithy/core/serde";
@@ -0,0 +1,251 @@
1
+ import { hasOwn } from "@smithy/core/serde";
2
+ import { HttpResponse, buildQueryString } from "@smithy/core/protocols";
3
+ import { buildAbortError } from "./build-abort-error";
4
+ import { NODEJS_TIMEOUT_ERROR_CODES } from "./constants";
5
+ import { getTransformedHeaders } from "./get-transformed-headers";
6
+ import { node_https } from "./node-https";
7
+ import { setConnectionTimeout } from "./set-connection-timeout";
8
+ import { setRequestTimeout } from "./set-request-timeout";
9
+ import { setSocketKeepAlive } from "./set-socket-keep-alive";
10
+ import { setSocketTimeout } from "./set-socket-timeout";
11
+ import { timing } from "./timing";
12
+ import { writeRequestBody } from "./write-request-body";
13
+ export const DEFAULT_REQUEST_TIMEOUT = 0;
14
+ let hAgent = undefined;
15
+ let hRequest = undefined;
16
+ export class NodeHttpHandler {
17
+ config;
18
+ configProvider;
19
+ socketWarningTimestamp = 0;
20
+ externalAgent = false;
21
+ metadata = { handlerProtocol: "http/1.1" };
22
+ static create(instanceOrOptions) {
23
+ if (typeof instanceOrOptions?.handle === "function") {
24
+ return instanceOrOptions;
25
+ }
26
+ return new NodeHttpHandler(instanceOrOptions);
27
+ }
28
+ static checkSocketUsage(agent, socketWarningTimestamp, logger = console) {
29
+ const { sockets, requests, maxSockets } = agent;
30
+ if (typeof maxSockets !== "number" || maxSockets === Infinity) {
31
+ return socketWarningTimestamp;
32
+ }
33
+ const interval = 15_000;
34
+ if (Date.now() - interval < socketWarningTimestamp) {
35
+ return socketWarningTimestamp;
36
+ }
37
+ if (sockets && requests) {
38
+ for (const origin in sockets) {
39
+ if (!hasOwn(sockets, origin))
40
+ continue;
41
+ const socketsInUse = sockets[origin]?.length ?? 0;
42
+ const requestsEnqueued = requests[origin]?.length ?? 0;
43
+ if (socketsInUse >= maxSockets && requestsEnqueued >= 2 * maxSockets) {
44
+ logger?.warn?.(`@smithy/node-http-handler:WARN - socket usage at capacity=${socketsInUse} and ${requestsEnqueued} additional requests are enqueued.
45
+ See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html
46
+ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config.`);
47
+ return Date.now();
48
+ }
49
+ }
50
+ }
51
+ return socketWarningTimestamp;
52
+ }
53
+ constructor(options) {
54
+ this.configProvider = new Promise((resolve, reject) => {
55
+ if (typeof options === "function") {
56
+ options()
57
+ .then((_options) => {
58
+ resolve(this.resolveDefaultConfig(_options));
59
+ })
60
+ .catch(reject);
61
+ }
62
+ else {
63
+ resolve(this.resolveDefaultConfig(options));
64
+ }
65
+ });
66
+ }
67
+ destroy() {
68
+ this.config?.httpAgent?.destroy();
69
+ this.config?.httpsAgent?.destroy();
70
+ }
71
+ async handle(request, { abortSignal, requestTimeout } = {}) {
72
+ if (!this.config) {
73
+ this.config = await this.configProvider;
74
+ }
75
+ const config = this.config;
76
+ const logger = config.logger;
77
+ const isSSL = request.protocol === "https:";
78
+ if (!isSSL && !this.config.httpAgent) {
79
+ this.config.httpAgent = await this.config.httpAgentProvider();
80
+ }
81
+ return new Promise((_resolve, _reject) => {
82
+ let writeRequestBodyPromise = undefined;
83
+ let socketWarningTimeoutId = -1;
84
+ let connectionTimeoutId = -1;
85
+ let requestTimeoutId = -1;
86
+ let socketTimeoutId = -1;
87
+ let keepAliveTimeoutId = -1;
88
+ const clearTimeouts = () => {
89
+ timing.clearTimeout(socketWarningTimeoutId);
90
+ timing.clearTimeout(connectionTimeoutId);
91
+ timing.clearTimeout(requestTimeoutId);
92
+ timing.clearTimeout(socketTimeoutId);
93
+ timing.clearTimeout(keepAliveTimeoutId);
94
+ };
95
+ const resolve = async (arg) => {
96
+ await writeRequestBodyPromise;
97
+ clearTimeouts();
98
+ _resolve(arg);
99
+ };
100
+ const reject = async (arg) => {
101
+ await writeRequestBodyPromise;
102
+ clearTimeouts();
103
+ _reject(arg);
104
+ };
105
+ if (abortSignal?.aborted) {
106
+ const abortError = buildAbortError(abortSignal);
107
+ reject(abortError);
108
+ return;
109
+ }
110
+ const headers = request.headers;
111
+ const expectContinue = headers ? (headers.Expect ?? headers.expect) === "100-continue" : false;
112
+ let agent = isSSL ? config.httpsAgent : config.httpAgent;
113
+ if (expectContinue && !this.externalAgent) {
114
+ agent = new (isSSL ? node_https.Agent : hAgent)({
115
+ keepAlive: false,
116
+ maxSockets: Infinity,
117
+ });
118
+ }
119
+ socketWarningTimeoutId = timing.setTimeout(() => {
120
+ this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage(agent, this.socketWarningTimestamp, logger);
121
+ }, config.socketAcquisitionWarningTimeout ?? (config.requestTimeout ?? 2000) + (config.connectionTimeout ?? 1000));
122
+ const queryString = request.query ? buildQueryString(request.query) : "";
123
+ let auth = undefined;
124
+ if (request.username != null || request.password != null) {
125
+ const username = request.username ?? "";
126
+ const password = request.password ?? "";
127
+ auth = `${username}:${password}`;
128
+ }
129
+ let path = request.path;
130
+ if (queryString) {
131
+ path += `?${queryString}`;
132
+ }
133
+ if (request.fragment) {
134
+ path += `#${request.fragment}`;
135
+ }
136
+ let hostname = request.hostname ?? "";
137
+ if (hostname[0] === "[" && hostname.endsWith("]")) {
138
+ hostname = request.hostname.slice(1, -1);
139
+ }
140
+ else {
141
+ hostname = request.hostname;
142
+ }
143
+ const nodeHttpsOptions = {
144
+ headers: request.headers,
145
+ host: hostname,
146
+ method: request.method,
147
+ path,
148
+ port: request.port,
149
+ agent,
150
+ auth,
151
+ };
152
+ const requestFunc = isSSL ? node_https.request : hRequest;
153
+ const req = requestFunc(nodeHttpsOptions, (res) => {
154
+ const httpResponse = new HttpResponse({
155
+ statusCode: res.statusCode || -1,
156
+ reason: res.statusMessage,
157
+ headers: getTransformedHeaders(res.headers),
158
+ body: res,
159
+ });
160
+ resolve({ response: httpResponse });
161
+ });
162
+ req.on("error", (err) => {
163
+ if (NODEJS_TIMEOUT_ERROR_CODES.includes(err.code)) {
164
+ reject(Object.assign(err, { name: "TimeoutError" }));
165
+ }
166
+ else {
167
+ reject(err);
168
+ }
169
+ });
170
+ if (abortSignal) {
171
+ const onAbort = () => {
172
+ req.destroy();
173
+ const abortError = buildAbortError(abortSignal);
174
+ reject(abortError);
175
+ };
176
+ if (typeof abortSignal.addEventListener === "function") {
177
+ const signal = abortSignal;
178
+ signal.addEventListener("abort", onAbort, { once: true });
179
+ req.once("close", () => signal.removeEventListener("abort", onAbort));
180
+ }
181
+ else {
182
+ abortSignal.onabort = onAbort;
183
+ }
184
+ }
185
+ const effectiveRequestTimeout = requestTimeout ?? config.requestTimeout;
186
+ connectionTimeoutId = setConnectionTimeout(req, reject, config.connectionTimeout);
187
+ requestTimeoutId = setRequestTimeout(req, reject, effectiveRequestTimeout, config.throwOnRequestTimeout, logger ?? console);
188
+ socketTimeoutId = setSocketTimeout(req, reject, config.socketTimeout);
189
+ const httpAgent = nodeHttpsOptions.agent;
190
+ if (typeof httpAgent === "object" && "keepAlive" in httpAgent) {
191
+ keepAliveTimeoutId = setSocketKeepAlive(req, {
192
+ keepAlive: httpAgent.keepAlive,
193
+ keepAliveMsecs: httpAgent.keepAliveMsecs,
194
+ });
195
+ }
196
+ writeRequestBodyPromise = writeRequestBody(req, request, effectiveRequestTimeout, this.externalAgent).catch((e) => {
197
+ clearTimeouts();
198
+ return _reject(e);
199
+ });
200
+ });
201
+ }
202
+ updateHttpClientConfig(key, value) {
203
+ this.config = undefined;
204
+ this.configProvider = this.configProvider.then((config) => {
205
+ if (key === Symbol.for("logger")) {
206
+ return {
207
+ ...config,
208
+ logger: config.logger ?? value,
209
+ };
210
+ }
211
+ return {
212
+ ...config,
213
+ [key]: value,
214
+ };
215
+ });
216
+ }
217
+ httpHandlerConfigs() {
218
+ return this.config ?? {};
219
+ }
220
+ resolveDefaultConfig(options) {
221
+ const { requestTimeout, connectionTimeout, socketTimeout, socketAcquisitionWarningTimeout, httpAgent, httpsAgent, throwOnRequestTimeout, logger, } = options || {};
222
+ const keepAlive = true;
223
+ const maxSockets = 50;
224
+ return {
225
+ connectionTimeout,
226
+ requestTimeout,
227
+ socketTimeout,
228
+ socketAcquisitionWarningTimeout,
229
+ throwOnRequestTimeout,
230
+ httpAgentProvider: async () => {
231
+ const node_http = await import("node:http");
232
+ const { Agent, request } = node_http.default ?? node_http;
233
+ hRequest = request;
234
+ hAgent = Agent;
235
+ if (httpAgent instanceof hAgent || typeof httpAgent?.destroy === "function") {
236
+ this.externalAgent = true;
237
+ return httpAgent;
238
+ }
239
+ return new hAgent({ keepAlive, maxSockets, ...httpAgent });
240
+ },
241
+ httpsAgent: (() => {
242
+ if (httpsAgent instanceof node_https.Agent || typeof httpsAgent?.destroy === "function") {
243
+ this.externalAgent = true;
244
+ return httpsAgent;
245
+ }
246
+ return new node_https.Agent({ keepAlive, maxSockets, ...httpsAgent });
247
+ })(),
248
+ logger,
249
+ };
250
+ }
251
+ }
@@ -0,0 +1,135 @@
1
+ import http2 from "node:http2";
2
+ import { ClientHttp2SessionRef } from "./http2/ClientHttp2SessionRef";
3
+ import { NodeHttp2ConnectionPool } from "./node-http2-connection-pool";
4
+ export class NodeHttp2ConnectionManager {
5
+ config;
6
+ connectOptions;
7
+ connectionPools = new Map();
8
+ constructor(config) {
9
+ this.config = config;
10
+ if (this.config.maxConcurrency && this.config.maxConcurrency <= 0) {
11
+ throw new RangeError("maxConcurrency must be greater than zero.");
12
+ }
13
+ }
14
+ lease(requestContext, connectionConfiguration) {
15
+ const url = this.getUrlString(requestContext);
16
+ const pool = this.getPool(url);
17
+ if (!this.config.disableConcurrency && !connectionConfiguration.isEventStream) {
18
+ const available = pool.poll();
19
+ if (available) {
20
+ available.retain();
21
+ return available;
22
+ }
23
+ }
24
+ const ref = new ClientHttp2SessionRef(this.connect(url));
25
+ const session = ref.deref();
26
+ if (this.config.maxConcurrency) {
27
+ session.settings({ maxConcurrentStreams: this.config.maxConcurrency }, (err) => {
28
+ if (err) {
29
+ throw new Error("Fail to set maxConcurrentStreams to " +
30
+ this.config.maxConcurrency +
31
+ "when creating new session for " +
32
+ requestContext.destination.toString());
33
+ }
34
+ });
35
+ }
36
+ const graceful = () => {
37
+ this.removeFromPoolAndClose(url, ref);
38
+ };
39
+ const ensureDestroyed = () => {
40
+ this.removeFromPoolAndCheckedDestroy(url, ref);
41
+ };
42
+ session.on("goaway", graceful);
43
+ session.on("error", ensureDestroyed);
44
+ session.on("frameError", ensureDestroyed);
45
+ session.on("close", ensureDestroyed);
46
+ if (connectionConfiguration.requestTimeout) {
47
+ session.setTimeout(connectionConfiguration.requestTimeout, ensureDestroyed);
48
+ }
49
+ pool.offerLast(ref);
50
+ ref.retain();
51
+ return ref;
52
+ }
53
+ release(_requestContext, ref) {
54
+ ref.free();
55
+ }
56
+ createIsolatedSession(requestContext, connectionConfiguration) {
57
+ const url = this.getUrlString(requestContext);
58
+ const ref = new ClientHttp2SessionRef(this.connect(url));
59
+ const session = ref.deref();
60
+ session.settings({ maxConcurrentStreams: 1 });
61
+ const ensureDestroyed = () => {
62
+ ref.destroy();
63
+ };
64
+ session.on("error", ensureDestroyed);
65
+ session.on("frameError", ensureDestroyed);
66
+ session.on("close", ensureDestroyed);
67
+ const timeout = connectionConfiguration.requestTimeout ?? 300_000;
68
+ session.setTimeout(timeout, ensureDestroyed);
69
+ ref.retain();
70
+ return ref;
71
+ }
72
+ destroy() {
73
+ for (const [url, connectionPool] of this.connectionPools) {
74
+ for (const session of [...connectionPool]) {
75
+ session.destroy();
76
+ }
77
+ this.connectionPools.delete(url);
78
+ }
79
+ }
80
+ setMaxConcurrentStreams(maxConcurrentStreams) {
81
+ if (maxConcurrentStreams && maxConcurrentStreams <= 0) {
82
+ throw new RangeError("maxConcurrentStreams must be greater than zero.");
83
+ }
84
+ this.config.maxConcurrency = maxConcurrentStreams;
85
+ for (const pool of this.connectionPools.values()) {
86
+ pool.setMaxConcurrency(maxConcurrentStreams);
87
+ }
88
+ }
89
+ setDisableConcurrentStreams(disableConcurrentStreams) {
90
+ this.config.disableConcurrency = disableConcurrentStreams;
91
+ }
92
+ setNodeHttp2ConnectOptions(nodeHttp2ConnectOptions) {
93
+ this.connectOptions = nodeHttp2ConnectOptions;
94
+ }
95
+ debug() {
96
+ const pools = {};
97
+ for (const [url, pool] of this.connectionPools) {
98
+ const sessions = [];
99
+ for (const ref of pool) {
100
+ sessions.push({
101
+ id: ref.id,
102
+ active: ref.useCount(),
103
+ maxConcurrent: ref.max,
104
+ totalRequests: ref.total,
105
+ });
106
+ }
107
+ pools[url] = { sessions };
108
+ }
109
+ return pools;
110
+ }
111
+ removeFromPoolAndClose(authority, ref) {
112
+ this.connectionPools.get(authority)?.remove(ref);
113
+ ref.close();
114
+ }
115
+ removeFromPoolAndCheckedDestroy(authority, ref) {
116
+ this.connectionPools.get(authority)?.remove(ref);
117
+ ref.destroy();
118
+ }
119
+ getPool(url) {
120
+ if (!this.connectionPools.has(url)) {
121
+ const pool = new NodeHttp2ConnectionPool();
122
+ if (this.config.maxConcurrency) {
123
+ pool.setMaxConcurrency(this.config.maxConcurrency);
124
+ }
125
+ this.connectionPools.set(url, pool);
126
+ }
127
+ return this.connectionPools.get(url);
128
+ }
129
+ getUrlString(request) {
130
+ return request.destination.toString();
131
+ }
132
+ connect(url) {
133
+ return this.connectOptions === undefined ? http2.connect(url) : http2.connect(url, this.connectOptions);
134
+ }
135
+ }
@@ -0,0 +1,46 @@
1
+ import { ClientHttp2SessionRef } from "./http2/ClientHttp2SessionRef";
2
+ export class NodeHttp2ConnectionPool {
3
+ sessions = [];
4
+ maxConcurrency = 0;
5
+ constructor(sessions) {
6
+ this.sessions = (sessions ?? []).map((session) => new ClientHttp2SessionRef(session));
7
+ }
8
+ poll() {
9
+ let cleanup = false;
10
+ for (const session of this.sessions) {
11
+ if (session.deref().destroyed) {
12
+ cleanup = true;
13
+ continue;
14
+ }
15
+ if (!this.maxConcurrency || session.useCount() < this.maxConcurrency) {
16
+ return session;
17
+ }
18
+ }
19
+ if (cleanup) {
20
+ for (const session of this.sessions) {
21
+ if (session.deref().destroyed) {
22
+ this.remove(session);
23
+ }
24
+ }
25
+ }
26
+ }
27
+ offerLast(ref) {
28
+ this.sessions.push(ref);
29
+ }
30
+ remove(ref) {
31
+ const ix = this.sessions.indexOf(ref);
32
+ if (ix > -1) {
33
+ this.sessions.splice(ix, 1);
34
+ }
35
+ }
36
+ [Symbol.iterator]() {
37
+ return this.sessions[Symbol.iterator]();
38
+ }
39
+ setMaxConcurrency(maxConcurrency) {
40
+ this.maxConcurrency = maxConcurrency;
41
+ }
42
+ destroy(ref) {
43
+ this.remove(ref);
44
+ ref.destroy();
45
+ }
46
+ }