@stryke/http 0.12.25 → 0.12.27
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/CHANGELOG.md +13 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/agent.cjs +96 -2
- package/dist/agent.mjs +93 -2
- package/dist/agent.mjs.map +1 -1
- package/dist/fetch.cjs +30 -1
- package/dist/fetch.mjs +28 -1
- package/dist/fetch.mjs.map +1 -1
- package/dist/format-data-uri.cjs +70 -1
- package/dist/format-data-uri.mjs +68 -1
- package/dist/format-data-uri.mjs.map +1 -1
- package/dist/get-free-port.cjs +23 -1
- package/dist/get-free-port.mjs +22 -1
- package/dist/get-free-port.mjs.map +1 -1
- package/dist/http-proxy.cjs +82 -3
- package/dist/http-proxy.mjs +79 -3
- package/dist/http-proxy.mjs.map +1 -1
- package/dist/https-proxy.cjs +106 -1
- package/dist/https-proxy.mjs +102 -1
- package/dist/https-proxy.mjs.map +1 -1
- package/dist/index.cjs +19 -1
- package/dist/index.mjs +10 -1
- package/dist/parse-response.cjs +83 -4
- package/dist/parse-response.mjs +82 -4
- package/dist/parse-response.mjs.map +1 -1
- package/dist/proxy-agent.cjs +18 -1
- package/dist/proxy-agent.mjs +18 -1
- package/dist/proxy-agent.mjs.map +1 -1
- package/dist/type-checks/src/index.cjs +10 -1
- package/dist/type-checks/src/index.mjs +12 -1
- package/dist/type-checks/src/is-boolean.cjs +18 -1
- package/dist/type-checks/src/is-boolean.mjs +17 -1
- package/dist/type-checks/src/is-boolean.mjs.map +1 -1
- package/dist/type-checks/src/is-buffer.cjs +12 -1
- package/dist/type-checks/src/is-buffer.mjs +11 -1
- package/dist/type-checks/src/is-buffer.mjs.map +1 -1
- package/dist/type-checks/src/is-collection.cjs +1 -1
- package/dist/type-checks/src/is-collection.mjs +3 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/type-detect.cjs +15 -1
- package/dist/type-checks/src/type-detect.mjs +16 -1
- package/dist/type-checks/src/type-detect.mjs.map +1 -1
- package/dist/url/src/helpers.cjs +14 -1
- package/dist/url/src/helpers.mjs +13 -1
- package/dist/url/src/helpers.mjs.map +1 -1
- package/package.json +4 -4
package/dist/http-proxy.cjs
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_agent = require('./agent.cjs');
|
|
3
|
+
let node_net = require("node:net");
|
|
4
|
+
node_net = require_rolldown_runtime.__toESM(node_net);
|
|
5
|
+
let node_buffer = require("node:buffer");
|
|
6
|
+
let node_events = require("node:events");
|
|
7
|
+
let node_tls = require("node:tls");
|
|
8
|
+
node_tls = require_rolldown_runtime.__toESM(node_tls);
|
|
9
|
+
let node_url = require("node:url");
|
|
10
|
+
|
|
11
|
+
//#region src/http-proxy.ts
|
|
12
|
+
/**
|
|
13
|
+
* The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the specified "HTTP proxy server" in order to proxy HTTP requests.
|
|
14
|
+
*/
|
|
15
|
+
var HttpProxyAgent = class extends require_agent.Agent {
|
|
16
|
+
static protocols = ["http", "https"];
|
|
17
|
+
proxy;
|
|
18
|
+
proxyHeaders;
|
|
19
|
+
connectOpts;
|
|
20
|
+
constructor(proxy, opts) {
|
|
21
|
+
super(opts);
|
|
22
|
+
this.proxy = typeof proxy === "string" ? new node_url.URL(proxy) : proxy;
|
|
23
|
+
this.proxyHeaders = opts?.headers ?? {};
|
|
24
|
+
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, "");
|
|
25
|
+
const port = this.proxy.port ? Number.parseInt(this.proxy.port, 10) : this.proxy.protocol === "https:" ? 443 : 80;
|
|
26
|
+
this.connectOpts = {
|
|
27
|
+
...opts ? omit(opts, "headers") : null,
|
|
28
|
+
host,
|
|
29
|
+
port
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
addRequest(req, opts) {
|
|
33
|
+
req._header = null;
|
|
34
|
+
this.setRequestProps(req, opts);
|
|
35
|
+
super.addRequest(req, opts);
|
|
36
|
+
}
|
|
37
|
+
setRequestProps(req, opts) {
|
|
38
|
+
const { proxy } = this;
|
|
39
|
+
const protocol = opts.secureEndpoint ? "https:" : "http:";
|
|
40
|
+
const hostname = req.getHeader("host") || "localhost";
|
|
41
|
+
const base = `${protocol}//${Array.isArray(hostname) ? hostname.join("") : hostname}`;
|
|
42
|
+
const url = new node_url.URL(req.path, base);
|
|
43
|
+
if (opts.port !== 80) url.port = String(opts.port);
|
|
44
|
+
req.path = String(url);
|
|
45
|
+
const headers = typeof this.proxyHeaders === "function" ? this.proxyHeaders() : { ...this.proxyHeaders };
|
|
46
|
+
if (proxy.username || proxy.password) {
|
|
47
|
+
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
48
|
+
headers["Proxy-Authorization"] = `Basic ${node_buffer.Buffer.from(auth).toString("base64")}`;
|
|
49
|
+
}
|
|
50
|
+
if (!headers["Proxy-Connection"]) headers["Proxy-Connection"] = this.keepAlive ? "Keep-Alive" : "close";
|
|
51
|
+
for (const name of Object.keys(headers)) {
|
|
52
|
+
const value = headers[name];
|
|
53
|
+
if (value) req.setHeader(name, value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async connect(req, opts) {
|
|
57
|
+
req._header = null;
|
|
58
|
+
if (!req.path.includes("://")) this.setRequestProps(req, opts);
|
|
59
|
+
let first;
|
|
60
|
+
let endOfHeaders;
|
|
61
|
+
req._implicitHeader();
|
|
62
|
+
if (req.outputData && req.outputData.length > 0) {
|
|
63
|
+
first = req.outputData[0].data;
|
|
64
|
+
endOfHeaders = first.indexOf("\r\n\r\n") + 4;
|
|
65
|
+
req.outputData[0].data = req._header + first.substring(endOfHeaders);
|
|
66
|
+
}
|
|
67
|
+
let socket;
|
|
68
|
+
if (this.proxy.protocol === "https:") socket = node_tls.connect(this.connectOpts);
|
|
69
|
+
else socket = node_net.connect(this.connectOpts);
|
|
70
|
+
await (0, node_events.once)(socket, "connect");
|
|
71
|
+
return socket;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
function omit(obj, ...keys) {
|
|
75
|
+
const ret = {};
|
|
76
|
+
let key;
|
|
77
|
+
for (key in obj) if (!keys.includes(key)) ret[key] = obj[key];
|
|
78
|
+
return ret;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
exports.HttpProxyAgent = HttpProxyAgent;
|
package/dist/http-proxy.mjs
CHANGED
|
@@ -1,4 +1,80 @@
|
|
|
1
|
-
import{Agent
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Agent } from "./agent.mjs";
|
|
2
|
+
import * as net from "node:net";
|
|
3
|
+
import { Buffer } from "node:buffer";
|
|
4
|
+
import { once } from "node:events";
|
|
5
|
+
import * as tls from "node:tls";
|
|
6
|
+
import { URL } from "node:url";
|
|
7
|
+
|
|
8
|
+
//#region src/http-proxy.ts
|
|
9
|
+
/**
|
|
10
|
+
* The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the specified "HTTP proxy server" in order to proxy HTTP requests.
|
|
11
|
+
*/
|
|
12
|
+
var HttpProxyAgent = class extends Agent {
|
|
13
|
+
static protocols = ["http", "https"];
|
|
14
|
+
proxy;
|
|
15
|
+
proxyHeaders;
|
|
16
|
+
connectOpts;
|
|
17
|
+
constructor(proxy, opts) {
|
|
18
|
+
super(opts);
|
|
19
|
+
this.proxy = typeof proxy === "string" ? new URL(proxy) : proxy;
|
|
20
|
+
this.proxyHeaders = opts?.headers ?? {};
|
|
21
|
+
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, "");
|
|
22
|
+
const port = this.proxy.port ? Number.parseInt(this.proxy.port, 10) : this.proxy.protocol === "https:" ? 443 : 80;
|
|
23
|
+
this.connectOpts = {
|
|
24
|
+
...opts ? omit(opts, "headers") : null,
|
|
25
|
+
host,
|
|
26
|
+
port
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
addRequest(req, opts) {
|
|
30
|
+
req._header = null;
|
|
31
|
+
this.setRequestProps(req, opts);
|
|
32
|
+
super.addRequest(req, opts);
|
|
33
|
+
}
|
|
34
|
+
setRequestProps(req, opts) {
|
|
35
|
+
const { proxy } = this;
|
|
36
|
+
const protocol = opts.secureEndpoint ? "https:" : "http:";
|
|
37
|
+
const hostname = req.getHeader("host") || "localhost";
|
|
38
|
+
const base = `${protocol}//${Array.isArray(hostname) ? hostname.join("") : hostname}`;
|
|
39
|
+
const url = new URL(req.path, base);
|
|
40
|
+
if (opts.port !== 80) url.port = String(opts.port);
|
|
41
|
+
req.path = String(url);
|
|
42
|
+
const headers = typeof this.proxyHeaders === "function" ? this.proxyHeaders() : { ...this.proxyHeaders };
|
|
43
|
+
if (proxy.username || proxy.password) {
|
|
44
|
+
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
45
|
+
headers["Proxy-Authorization"] = `Basic ${Buffer.from(auth).toString("base64")}`;
|
|
46
|
+
}
|
|
47
|
+
if (!headers["Proxy-Connection"]) headers["Proxy-Connection"] = this.keepAlive ? "Keep-Alive" : "close";
|
|
48
|
+
for (const name of Object.keys(headers)) {
|
|
49
|
+
const value = headers[name];
|
|
50
|
+
if (value) req.setHeader(name, value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async connect(req, opts) {
|
|
54
|
+
req._header = null;
|
|
55
|
+
if (!req.path.includes("://")) this.setRequestProps(req, opts);
|
|
56
|
+
let first;
|
|
57
|
+
let endOfHeaders;
|
|
58
|
+
req._implicitHeader();
|
|
59
|
+
if (req.outputData && req.outputData.length > 0) {
|
|
60
|
+
first = req.outputData[0].data;
|
|
61
|
+
endOfHeaders = first.indexOf("\r\n\r\n") + 4;
|
|
62
|
+
req.outputData[0].data = req._header + first.substring(endOfHeaders);
|
|
63
|
+
}
|
|
64
|
+
let socket;
|
|
65
|
+
if (this.proxy.protocol === "https:") socket = tls.connect(this.connectOpts);
|
|
66
|
+
else socket = net.connect(this.connectOpts);
|
|
67
|
+
await once(socket, "connect");
|
|
68
|
+
return socket;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function omit(obj, ...keys) {
|
|
72
|
+
const ret = {};
|
|
73
|
+
let key;
|
|
74
|
+
for (key in obj) if (!keys.includes(key)) ret[key] = obj[key];
|
|
75
|
+
return ret;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { HttpProxyAgent };
|
|
4
80
|
//# sourceMappingURL=http-proxy.mjs.map
|
package/dist/http-proxy.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-proxy.mjs","names":["headers: OutgoingHttpHeaders","first: string","endOfHeaders: number","socket: net.Socket","key: keyof typeof obj"],"sources":["../src/http-proxy.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { AgentConnectOpts } from \"agent-base\";\nimport { Buffer } from \"node:buffer\";\nimport { once } from \"node:events\";\nimport type {\n AgentOptions,\n ClientRequest,\n OutgoingHttpHeaders\n} from \"node:http\";\nimport * as net from \"node:net\";\nimport * as tls from \"node:tls\";\nimport { URL } from \"node:url\";\nimport { Agent } from \"./agent\";\n\ntype Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;\n\ninterface ConnectOptsMap {\n http: Omit<net.TcpNetConnectOpts, \"host\" | \"port\">;\n https: Omit<tls.ConnectionOptions, \"host\" | \"port\">;\n}\n\ntype ConnectOpts<T> = {\n [P in keyof ConnectOptsMap]: Protocol<T> extends P\n ? ConnectOptsMap[P]\n : never;\n}[keyof ConnectOptsMap];\n\nexport type HttpProxyAgentOptions<T> = ConnectOpts<T> &\n AgentOptions & {\n headers?: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n };\n\ninterface HttpProxyAgentClientRequest extends ClientRequest {\n outputData?: {\n data: string;\n }[];\n _header?: string | null;\n _implicitHeader: () => void;\n}\n\n/**\n * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the specified \"HTTP proxy server\" in order to proxy HTTP requests.\n */\nexport class HttpProxyAgent<Uri extends string> extends Agent {\n static protocols = [\"http\", \"https\"] as const;\n\n readonly proxy: URL;\n\n proxyHeaders: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n\n connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;\n\n constructor(proxy: Uri | URL, opts?: HttpProxyAgentOptions<Uri>) {\n super(opts);\n this.proxy = typeof proxy === \"string\" ? new URL(proxy) : proxy;\n this.proxyHeaders = opts?.headers ?? {};\n\n // Trim off the brackets from IPv6 addresses\n const host = (this.proxy.hostname || this.proxy.host).replace(\n /^\\[|\\]$/g,\n \"\"\n );\n const port = this.proxy.port\n ? Number.parseInt(this.proxy.port, 10)\n : this.proxy.protocol === \"https:\"\n ? 443\n : 80;\n this.connectOpts = {\n ...(opts ? omit(opts, \"headers\") : null),\n host,\n port\n };\n }\n\n addRequest(req: HttpProxyAgentClientRequest, opts: AgentConnectOpts): void {\n req._header = null;\n this.setRequestProps(req, opts);\n\n // @ts-expect-error `addRequest()` isn't defined in `@types/node`\n // eslint-disable-next-line ts/no-unsafe-call\n super.addRequest(req, opts);\n }\n\n setRequestProps(\n req: HttpProxyAgentClientRequest,\n opts: AgentConnectOpts\n ): void {\n const { proxy } = this;\n const protocol = opts.secureEndpoint ? \"https:\" : \"http:\";\n const hostname = req.getHeader(\"host\") || \"localhost\";\n const base = `${protocol}//${Array.isArray(hostname) ? hostname.join(\"\") : hostname}`;\n const url = new URL(req.path, base);\n if (opts.port !== 80) {\n url.port = String(opts.port);\n }\n\n // Change the `http.ClientRequest` instance's \"path\" field\n // to the absolute path of the URL that will be requested.\n req.path = String(url);\n\n // Inject the `Proxy-Authorization` header if necessary.\n\n const headers: OutgoingHttpHeaders =\n typeof this.proxyHeaders === \"function\"\n ? this.proxyHeaders()\n : { ...this.proxyHeaders };\n if (proxy.username || proxy.password) {\n const auth = `${decodeURIComponent(\n proxy.username\n )}:${decodeURIComponent(proxy.password)}`;\n headers[\"Proxy-Authorization\"] = `Basic ${Buffer.from(auth).toString(\n \"base64\"\n )}`;\n }\n\n if (!headers[\"Proxy-Connection\"]) {\n headers[\"Proxy-Connection\"] = this.keepAlive ? \"Keep-Alive\" : \"close\";\n }\n for (const name of Object.keys(headers)) {\n const value = headers[name];\n if (value) {\n req.setHeader(name, value);\n }\n }\n }\n\n async connect(\n req: HttpProxyAgentClientRequest,\n opts: AgentConnectOpts\n ): Promise<net.Socket> {\n req._header = null;\n\n if (!req.path.includes(\"://\")) {\n this.setRequestProps(req, opts);\n }\n\n // At this point, the http ClientRequest's internal `_header` field\n // might have already been set. If this is the case then we'll need\n // to re-generate the string since we just changed the `req.path`.\n let first: string;\n let endOfHeaders: number;\n\n req._implicitHeader();\n if (req.outputData && req.outputData.length > 0) {\n first = req.outputData[0]!.data;\n endOfHeaders = first.indexOf(\"\\r\\n\\r\\n\") + 4;\n req.outputData[0]!.data = req._header + first.substring(endOfHeaders);\n }\n\n // Create a socket connection to the proxy server.\n let socket: net.Socket;\n if (this.proxy.protocol === \"https:\") {\n socket = tls.connect(this.connectOpts);\n } else {\n socket = net.connect(this.connectOpts);\n }\n\n // Wait for the socket's `connect` event, so that this `callback()`\n // function throws instead of the `http` request machinery. This is\n // important for i.e. `PacProxyAgent` which determines a failed proxy\n // connection via the `callback()` function throwing.\n await once(socket, \"connect\");\n\n return socket;\n }\n}\n\nfunction omit<T extends object, K extends [...(keyof T)[]]>(\n obj: T,\n ...keys: K\n): {\n [K2 in Exclude<keyof T, K[number]>]: T[K2];\n} {\n const ret = {} as {\n [K in keyof typeof obj]: (typeof obj)[K];\n };\n let key: keyof typeof obj;\n for (key in obj) {\n if (!keys.includes(key)) {\n ret[key] = obj[key];\n }\n }\n return ret;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"http-proxy.mjs","names":["headers: OutgoingHttpHeaders","first: string","endOfHeaders: number","socket: net.Socket","key: keyof typeof obj"],"sources":["../src/http-proxy.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { AgentConnectOpts } from \"agent-base\";\nimport { Buffer } from \"node:buffer\";\nimport { once } from \"node:events\";\nimport type {\n AgentOptions,\n ClientRequest,\n OutgoingHttpHeaders\n} from \"node:http\";\nimport * as net from \"node:net\";\nimport * as tls from \"node:tls\";\nimport { URL } from \"node:url\";\nimport { Agent } from \"./agent\";\n\ntype Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;\n\ninterface ConnectOptsMap {\n http: Omit<net.TcpNetConnectOpts, \"host\" | \"port\">;\n https: Omit<tls.ConnectionOptions, \"host\" | \"port\">;\n}\n\ntype ConnectOpts<T> = {\n [P in keyof ConnectOptsMap]: Protocol<T> extends P\n ? ConnectOptsMap[P]\n : never;\n}[keyof ConnectOptsMap];\n\nexport type HttpProxyAgentOptions<T> = ConnectOpts<T> &\n AgentOptions & {\n headers?: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n };\n\ninterface HttpProxyAgentClientRequest extends ClientRequest {\n outputData?: {\n data: string;\n }[];\n _header?: string | null;\n _implicitHeader: () => void;\n}\n\n/**\n * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the specified \"HTTP proxy server\" in order to proxy HTTP requests.\n */\nexport class HttpProxyAgent<Uri extends string> extends Agent {\n static protocols = [\"http\", \"https\"] as const;\n\n readonly proxy: URL;\n\n proxyHeaders: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n\n connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;\n\n constructor(proxy: Uri | URL, opts?: HttpProxyAgentOptions<Uri>) {\n super(opts);\n this.proxy = typeof proxy === \"string\" ? new URL(proxy) : proxy;\n this.proxyHeaders = opts?.headers ?? {};\n\n // Trim off the brackets from IPv6 addresses\n const host = (this.proxy.hostname || this.proxy.host).replace(\n /^\\[|\\]$/g,\n \"\"\n );\n const port = this.proxy.port\n ? Number.parseInt(this.proxy.port, 10)\n : this.proxy.protocol === \"https:\"\n ? 443\n : 80;\n this.connectOpts = {\n ...(opts ? omit(opts, \"headers\") : null),\n host,\n port\n };\n }\n\n addRequest(req: HttpProxyAgentClientRequest, opts: AgentConnectOpts): void {\n req._header = null;\n this.setRequestProps(req, opts);\n\n // @ts-expect-error `addRequest()` isn't defined in `@types/node`\n // eslint-disable-next-line ts/no-unsafe-call\n super.addRequest(req, opts);\n }\n\n setRequestProps(\n req: HttpProxyAgentClientRequest,\n opts: AgentConnectOpts\n ): void {\n const { proxy } = this;\n const protocol = opts.secureEndpoint ? \"https:\" : \"http:\";\n const hostname = req.getHeader(\"host\") || \"localhost\";\n const base = `${protocol}//${Array.isArray(hostname) ? hostname.join(\"\") : hostname}`;\n const url = new URL(req.path, base);\n if (opts.port !== 80) {\n url.port = String(opts.port);\n }\n\n // Change the `http.ClientRequest` instance's \"path\" field\n // to the absolute path of the URL that will be requested.\n req.path = String(url);\n\n // Inject the `Proxy-Authorization` header if necessary.\n\n const headers: OutgoingHttpHeaders =\n typeof this.proxyHeaders === \"function\"\n ? this.proxyHeaders()\n : { ...this.proxyHeaders };\n if (proxy.username || proxy.password) {\n const auth = `${decodeURIComponent(\n proxy.username\n )}:${decodeURIComponent(proxy.password)}`;\n headers[\"Proxy-Authorization\"] = `Basic ${Buffer.from(auth).toString(\n \"base64\"\n )}`;\n }\n\n if (!headers[\"Proxy-Connection\"]) {\n headers[\"Proxy-Connection\"] = this.keepAlive ? \"Keep-Alive\" : \"close\";\n }\n for (const name of Object.keys(headers)) {\n const value = headers[name];\n if (value) {\n req.setHeader(name, value);\n }\n }\n }\n\n async connect(\n req: HttpProxyAgentClientRequest,\n opts: AgentConnectOpts\n ): Promise<net.Socket> {\n req._header = null;\n\n if (!req.path.includes(\"://\")) {\n this.setRequestProps(req, opts);\n }\n\n // At this point, the http ClientRequest's internal `_header` field\n // might have already been set. If this is the case then we'll need\n // to re-generate the string since we just changed the `req.path`.\n let first: string;\n let endOfHeaders: number;\n\n req._implicitHeader();\n if (req.outputData && req.outputData.length > 0) {\n first = req.outputData[0]!.data;\n endOfHeaders = first.indexOf(\"\\r\\n\\r\\n\") + 4;\n req.outputData[0]!.data = req._header + first.substring(endOfHeaders);\n }\n\n // Create a socket connection to the proxy server.\n let socket: net.Socket;\n if (this.proxy.protocol === \"https:\") {\n socket = tls.connect(this.connectOpts);\n } else {\n socket = net.connect(this.connectOpts);\n }\n\n // Wait for the socket's `connect` event, so that this `callback()`\n // function throws instead of the `http` request machinery. This is\n // important for i.e. `PacProxyAgent` which determines a failed proxy\n // connection via the `callback()` function throwing.\n await once(socket, \"connect\");\n\n return socket;\n }\n}\n\nfunction omit<T extends object, K extends [...(keyof T)[]]>(\n obj: T,\n ...keys: K\n): {\n [K2 in Exclude<keyof T, K[number]>]: T[K2];\n} {\n const ret = {} as {\n [K in keyof typeof obj]: (typeof obj)[K];\n };\n let key: keyof typeof obj;\n for (key in obj) {\n if (!keys.includes(key)) {\n ret[key] = obj[key];\n }\n }\n return ret;\n}\n"],"mappings":";;;;;;;;;;;AA4DA,IAAa,iBAAb,cAAwD,MAAM;CAC5D,OAAO,YAAY,CAAC,QAAQ,QAAQ;CAEpC,AAAS;CAET;CAEA;CAEA,YAAY,OAAkB,MAAmC;AAC/D,QAAM,KAAK;AACX,OAAK,QAAQ,OAAO,UAAU,WAAW,IAAI,IAAI,MAAM,GAAG;AAC1D,OAAK,eAAe,MAAM,WAAW,EAAE;EAGvC,MAAM,QAAQ,KAAK,MAAM,YAAY,KAAK,MAAM,MAAM,QACpD,YACA,GACD;EACD,MAAM,OAAO,KAAK,MAAM,OACpB,OAAO,SAAS,KAAK,MAAM,MAAM,GAAG,GACpC,KAAK,MAAM,aAAa,WACtB,MACA;AACN,OAAK,cAAc;GACjB,GAAI,OAAO,KAAK,MAAM,UAAU,GAAG;GACnC;GACA;GACD;;CAGH,WAAW,KAAkC,MAA8B;AACzE,MAAI,UAAU;AACd,OAAK,gBAAgB,KAAK,KAAK;AAI/B,QAAM,WAAW,KAAK,KAAK;;CAG7B,gBACE,KACA,MACM;EACN,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I,QAAQ,KAAK,YAAY;AAOxC,QAAM,KAAK,QAAQ,UAAU;AAE7B,SAAO;;;AAIX,SAAS,KACP,KACA,GAAG,MAGH;CACA,MAAM,MAAM,EAAE;CAGd,IAAIC;AACJ,MAAK,OAAO,IACV,KAAI,CAAC,KAAK,SAAS,IAAI,CACrB,KAAI,OAAO,IAAI;AAGnB,QAAO"}
|
package/dist/https-proxy.cjs
CHANGED
|
@@ -1 +1,106 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_parse_response = require('./parse-response.cjs');
|
|
3
|
+
let node_net = require("node:net");
|
|
4
|
+
node_net = require_rolldown_runtime.__toESM(node_net);
|
|
5
|
+
let node_buffer = require("node:buffer");
|
|
6
|
+
let node_tls = require("node:tls");
|
|
7
|
+
node_tls = require_rolldown_runtime.__toESM(node_tls);
|
|
8
|
+
let node_url = require("node:url");
|
|
9
|
+
let agent_base = require("agent-base");
|
|
10
|
+
let node_assert = require("node:assert");
|
|
11
|
+
node_assert = require_rolldown_runtime.__toESM(node_assert);
|
|
12
|
+
|
|
13
|
+
//#region src/https-proxy.ts
|
|
14
|
+
const setServernameFromNonIpHost = (options) => {
|
|
15
|
+
if (options.servername === void 0 && options.host && !node_net.isIP(options.host)) return {
|
|
16
|
+
...options,
|
|
17
|
+
servername: options.host
|
|
18
|
+
};
|
|
19
|
+
return options;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to
|
|
23
|
+
* the specified "HTTP(s) proxy server" in order to proxy HTTPS requests.
|
|
24
|
+
*
|
|
25
|
+
* Outgoing HTTP requests are first tunneled through the proxy server using the
|
|
26
|
+
* `CONNECT` HTTP request method to establish a connection to the proxy server,
|
|
27
|
+
* and then the proxy server connects to the destination target and issues the
|
|
28
|
+
* HTTP request from the proxy server.
|
|
29
|
+
*
|
|
30
|
+
* `https:` requests have their socket connection upgraded to TLS once
|
|
31
|
+
* the connection to the proxy server has been established.
|
|
32
|
+
*/
|
|
33
|
+
var HttpsProxyAgent = class extends agent_base.Agent {
|
|
34
|
+
static protocols = ["http", "https"];
|
|
35
|
+
proxy;
|
|
36
|
+
proxyHeaders;
|
|
37
|
+
connectOpts;
|
|
38
|
+
constructor(proxy, opts) {
|
|
39
|
+
super(opts);
|
|
40
|
+
this.options = { path: void 0 };
|
|
41
|
+
this.proxy = typeof proxy === "string" ? new node_url.URL(proxy) : proxy;
|
|
42
|
+
this.proxyHeaders = opts?.headers ?? {};
|
|
43
|
+
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, "");
|
|
44
|
+
const port = this.proxy.port ? Number.parseInt(this.proxy.port, 10) : this.proxy.protocol === "https:" ? 443 : 80;
|
|
45
|
+
this.connectOpts = {
|
|
46
|
+
ALPNProtocols: ["http/1.1"],
|
|
47
|
+
...opts ? omit(opts, "headers") : null,
|
|
48
|
+
host,
|
|
49
|
+
port
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Called when the node-core HTTP client library is creating a new HTTP request.
|
|
54
|
+
*/
|
|
55
|
+
async connect(req, opts) {
|
|
56
|
+
const { proxy } = this;
|
|
57
|
+
if (!opts.host) throw new TypeError("No \"host\" provided");
|
|
58
|
+
let socket;
|
|
59
|
+
if (proxy.protocol === "https:") socket = node_tls.connect(setServernameFromNonIpHost(this.connectOpts));
|
|
60
|
+
else socket = node_net.connect(this.connectOpts);
|
|
61
|
+
const headers = typeof this.proxyHeaders === "function" ? this.proxyHeaders() : { ...this.proxyHeaders };
|
|
62
|
+
const host = node_net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
|
|
63
|
+
let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r\n`;
|
|
64
|
+
if (proxy.username || proxy.password) {
|
|
65
|
+
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
66
|
+
headers["Proxy-Authorization"] = `Basic ${node_buffer.Buffer.from(auth).toString("base64")}`;
|
|
67
|
+
}
|
|
68
|
+
headers.Host = `${host}:${opts.port}`;
|
|
69
|
+
if (!headers["Proxy-Connection"]) headers["Proxy-Connection"] = this.keepAlive ? "Keep-Alive" : "close";
|
|
70
|
+
for (const name of Object.keys(headers)) payload += `${name}: ${headers[name]?.toString()}\r\n`;
|
|
71
|
+
const proxyResponsePromise = require_parse_response.parseProxyResponse(socket);
|
|
72
|
+
socket.write(`${payload}\r\n`);
|
|
73
|
+
const { connect, buffered } = await proxyResponsePromise;
|
|
74
|
+
req.emit("proxyConnect", connect);
|
|
75
|
+
this.emit("proxyConnect", connect, req);
|
|
76
|
+
if (connect.statusCode === 200) {
|
|
77
|
+
req.once("socket", resume);
|
|
78
|
+
if (opts.secureEndpoint) return node_tls.connect({
|
|
79
|
+
...omit(setServernameFromNonIpHost(opts), "host", "path", "port"),
|
|
80
|
+
socket
|
|
81
|
+
});
|
|
82
|
+
return socket;
|
|
83
|
+
}
|
|
84
|
+
socket.destroy();
|
|
85
|
+
const fakeSocket = new node_net.Socket({ writable: false });
|
|
86
|
+
fakeSocket.readable = true;
|
|
87
|
+
req.once("socket", (s) => {
|
|
88
|
+
(0, node_assert.default)(s.listenerCount("data") > 0);
|
|
89
|
+
s.push(buffered);
|
|
90
|
+
s.push(null);
|
|
91
|
+
});
|
|
92
|
+
return fakeSocket;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
function resume(socket) {
|
|
96
|
+
socket.resume();
|
|
97
|
+
}
|
|
98
|
+
function omit(obj, ...keys) {
|
|
99
|
+
const ret = {};
|
|
100
|
+
let key;
|
|
101
|
+
for (key in obj) if (!keys.includes(key)) ret[key] = obj[key];
|
|
102
|
+
return ret;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
exports.HttpsProxyAgent = HttpsProxyAgent;
|
package/dist/https-proxy.mjs
CHANGED
|
@@ -1,2 +1,103 @@
|
|
|
1
|
-
import{parseProxyResponse
|
|
1
|
+
import { parseProxyResponse } from "./parse-response.mjs";
|
|
2
|
+
import * as net from "node:net";
|
|
3
|
+
import { Buffer } from "node:buffer";
|
|
4
|
+
import * as tls from "node:tls";
|
|
5
|
+
import { URL } from "node:url";
|
|
6
|
+
import { Agent } from "agent-base";
|
|
7
|
+
import assert from "node:assert";
|
|
8
|
+
|
|
9
|
+
//#region src/https-proxy.ts
|
|
10
|
+
const setServernameFromNonIpHost = (options) => {
|
|
11
|
+
if (options.servername === void 0 && options.host && !net.isIP(options.host)) return {
|
|
12
|
+
...options,
|
|
13
|
+
servername: options.host
|
|
14
|
+
};
|
|
15
|
+
return options;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to
|
|
19
|
+
* the specified "HTTP(s) proxy server" in order to proxy HTTPS requests.
|
|
20
|
+
*
|
|
21
|
+
* Outgoing HTTP requests are first tunneled through the proxy server using the
|
|
22
|
+
* `CONNECT` HTTP request method to establish a connection to the proxy server,
|
|
23
|
+
* and then the proxy server connects to the destination target and issues the
|
|
24
|
+
* HTTP request from the proxy server.
|
|
25
|
+
*
|
|
26
|
+
* `https:` requests have their socket connection upgraded to TLS once
|
|
27
|
+
* the connection to the proxy server has been established.
|
|
28
|
+
*/
|
|
29
|
+
var HttpsProxyAgent = class extends Agent {
|
|
30
|
+
static protocols = ["http", "https"];
|
|
31
|
+
proxy;
|
|
32
|
+
proxyHeaders;
|
|
33
|
+
connectOpts;
|
|
34
|
+
constructor(proxy, opts) {
|
|
35
|
+
super(opts);
|
|
36
|
+
this.options = { path: void 0 };
|
|
37
|
+
this.proxy = typeof proxy === "string" ? new URL(proxy) : proxy;
|
|
38
|
+
this.proxyHeaders = opts?.headers ?? {};
|
|
39
|
+
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, "");
|
|
40
|
+
const port = this.proxy.port ? Number.parseInt(this.proxy.port, 10) : this.proxy.protocol === "https:" ? 443 : 80;
|
|
41
|
+
this.connectOpts = {
|
|
42
|
+
ALPNProtocols: ["http/1.1"],
|
|
43
|
+
...opts ? omit(opts, "headers") : null,
|
|
44
|
+
host,
|
|
45
|
+
port
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Called when the node-core HTTP client library is creating a new HTTP request.
|
|
50
|
+
*/
|
|
51
|
+
async connect(req, opts) {
|
|
52
|
+
const { proxy } = this;
|
|
53
|
+
if (!opts.host) throw new TypeError("No \"host\" provided");
|
|
54
|
+
let socket;
|
|
55
|
+
if (proxy.protocol === "https:") socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));
|
|
56
|
+
else socket = net.connect(this.connectOpts);
|
|
57
|
+
const headers = typeof this.proxyHeaders === "function" ? this.proxyHeaders() : { ...this.proxyHeaders };
|
|
58
|
+
const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
|
|
59
|
+
let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r\n`;
|
|
60
|
+
if (proxy.username || proxy.password) {
|
|
61
|
+
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
62
|
+
headers["Proxy-Authorization"] = `Basic ${Buffer.from(auth).toString("base64")}`;
|
|
63
|
+
}
|
|
64
|
+
headers.Host = `${host}:${opts.port}`;
|
|
65
|
+
if (!headers["Proxy-Connection"]) headers["Proxy-Connection"] = this.keepAlive ? "Keep-Alive" : "close";
|
|
66
|
+
for (const name of Object.keys(headers)) payload += `${name}: ${headers[name]?.toString()}\r\n`;
|
|
67
|
+
const proxyResponsePromise = parseProxyResponse(socket);
|
|
68
|
+
socket.write(`${payload}\r\n`);
|
|
69
|
+
const { connect, buffered } = await proxyResponsePromise;
|
|
70
|
+
req.emit("proxyConnect", connect);
|
|
71
|
+
this.emit("proxyConnect", connect, req);
|
|
72
|
+
if (connect.statusCode === 200) {
|
|
73
|
+
req.once("socket", resume);
|
|
74
|
+
if (opts.secureEndpoint) return tls.connect({
|
|
75
|
+
...omit(setServernameFromNonIpHost(opts), "host", "path", "port"),
|
|
76
|
+
socket
|
|
77
|
+
});
|
|
78
|
+
return socket;
|
|
79
|
+
}
|
|
80
|
+
socket.destroy();
|
|
81
|
+
const fakeSocket = new net.Socket({ writable: false });
|
|
82
|
+
fakeSocket.readable = true;
|
|
83
|
+
req.once("socket", (s) => {
|
|
84
|
+
assert(s.listenerCount("data") > 0);
|
|
85
|
+
s.push(buffered);
|
|
86
|
+
s.push(null);
|
|
87
|
+
});
|
|
88
|
+
return fakeSocket;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
function resume(socket) {
|
|
92
|
+
socket.resume();
|
|
93
|
+
}
|
|
94
|
+
function omit(obj, ...keys) {
|
|
95
|
+
const ret = {};
|
|
96
|
+
let key;
|
|
97
|
+
for (key in obj) if (!keys.includes(key)) ret[key] = obj[key];
|
|
98
|
+
return ret;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
export { HttpsProxyAgent };
|
|
2
103
|
//# sourceMappingURL=https-proxy.mjs.map
|
package/dist/https-proxy.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"https-proxy.mjs","names":["socket: net.Socket","headers: OutgoingHttpHeaders","key: keyof typeof obj"],"sources":["../src/https-proxy.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { AgentConnectOpts } from \"agent-base\";\nimport { Agent } from \"agent-base\";\nimport assert from \"node:assert\";\nimport { Buffer } from \"node:buffer\";\nimport type * as http from \"node:http\";\nimport type { OutgoingHttpHeaders } from \"node:http\";\nimport * as net from \"node:net\";\nimport * as tls from \"node:tls\";\nimport { URL } from \"node:url\";\nimport { parseProxyResponse } from \"./parse-response\";\n\nconst setServernameFromNonIpHost = <\n T extends { host?: string; servername?: string }\n>(\n options: T\n) => {\n if (\n options.servername === undefined &&\n options.host &&\n !net.isIP(options.host)\n ) {\n return {\n ...options,\n servername: options.host\n };\n }\n return options;\n};\n\ntype Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;\n\ninterface ConnectOptsMap {\n http: Omit<net.TcpNetConnectOpts, \"host\" | \"port\">;\n https: Omit<tls.ConnectionOptions, \"host\" | \"port\">;\n}\n\ntype ConnectOpts<T> = {\n [P in keyof ConnectOptsMap]: Protocol<T> extends P\n ? ConnectOptsMap[P]\n : never;\n}[keyof ConnectOptsMap];\n\nexport type HttpsProxyAgentOptions<T> = ConnectOpts<T> &\n http.AgentOptions & {\n headers?: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n };\n\n/**\n * The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to\n * the specified \"HTTP(s) proxy server\" in order to proxy HTTPS requests.\n *\n * Outgoing HTTP requests are first tunneled through the proxy server using the\n * `CONNECT` HTTP request method to establish a connection to the proxy server,\n * and then the proxy server connects to the destination target and issues the\n * HTTP request from the proxy server.\n *\n * `https:` requests have their socket connection upgraded to TLS once\n * the connection to the proxy server has been established.\n */\nexport class HttpsProxyAgent<Uri extends string> extends Agent {\n static protocols = [\"http\", \"https\"] as const;\n\n readonly proxy: URL;\n\n proxyHeaders: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n\n connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;\n\n constructor(proxy: Uri | URL, opts?: HttpsProxyAgentOptions<Uri>) {\n super(opts);\n this.options = { path: undefined };\n this.proxy = typeof proxy === \"string\" ? new URL(proxy) : proxy;\n this.proxyHeaders = opts?.headers ?? {};\n\n // Trim off the brackets from IPv6 addresses\n const host = (this.proxy.hostname || this.proxy.host).replace(\n /^\\[|\\]$/g,\n \"\"\n );\n const port = this.proxy.port\n ? Number.parseInt(this.proxy.port, 10)\n : this.proxy.protocol === \"https:\"\n ? 443\n : 80;\n this.connectOpts = {\n // Attempt to negotiate http/1.1 for proxy servers that support http/2\n ALPNProtocols: [\"http/1.1\"],\n ...(opts ? omit(opts, \"headers\") : null),\n host,\n port\n };\n }\n\n /**\n * Called when the node-core HTTP client library is creating a new HTTP request.\n */\n async connect(\n req: http.ClientRequest,\n opts: AgentConnectOpts\n ): Promise<net.Socket> {\n const { proxy } = this;\n\n if (!opts.host) {\n throw new TypeError('No \"host\" provided');\n }\n\n // Create a socket connection to the proxy server.\n let socket: net.Socket;\n if (proxy.protocol === \"https:\") {\n socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));\n } else {\n socket = net.connect(this.connectOpts);\n }\n\n const headers: OutgoingHttpHeaders =\n typeof this.proxyHeaders === \"function\"\n ? this.proxyHeaders()\n : { ...this.proxyHeaders };\n const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;\n let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\\r\\n`;\n\n // Inject the `Proxy-Authorization` header if necessary.\n if (proxy.username || proxy.password) {\n const auth = `${decodeURIComponent(\n proxy.username\n )}:${decodeURIComponent(proxy.password)}`;\n headers[\"Proxy-Authorization\"] = `Basic ${Buffer.from(auth).toString(\n \"base64\"\n )}`;\n }\n\n headers.Host = `${host}:${opts.port}`;\n\n if (!headers[\"Proxy-Connection\"]) {\n headers[\"Proxy-Connection\"] = this.keepAlive ? \"Keep-Alive\" : \"close\";\n }\n for (const name of Object.keys(headers)) {\n payload += `${name}: ${headers[name]?.toString()}\\r\\n`;\n }\n\n const proxyResponsePromise = parseProxyResponse(socket);\n\n socket.write(`${payload}\\r\\n`);\n\n const { connect, buffered } = await proxyResponsePromise;\n req.emit(\"proxyConnect\", connect);\n this.emit(\"proxyConnect\", connect, req);\n\n if (connect.statusCode === 200) {\n req.once(\"socket\", resume);\n\n if (opts.secureEndpoint) {\n // The proxy is connecting to a TLS server, so upgrade\n // this socket connection to a TLS connection.\n return tls.connect({\n ...omit(setServernameFromNonIpHost(opts), \"host\", \"path\", \"port\"),\n socket\n });\n }\n\n return socket;\n }\n\n // Some other status code that's not 200... need to re-play the HTTP\n // header \"data\" events onto the socket once the HTTP machinery is\n // attached so that the node core `http` can parse and handle the\n // error status code.\n\n // Close the original socket, and a new \"fake\" socket is returned\n // instead, so that the proxy doesn't get the HTTP request\n // written to it (which may contain `Authorization` headers or other\n // sensitive data).\n //\n // See: https://hackerone.com/reports/541502\n socket.destroy();\n\n const fakeSocket = new net.Socket({ writable: false });\n fakeSocket.readable = true;\n\n // Need to wait for the \"socket\" event to re-play the \"data\" events.\n req.once(\"socket\", (s: net.Socket) => {\n assert(s.listenerCount(\"data\") > 0);\n\n // Replay the \"buffered\" Buffer onto the fake `socket`, since at\n // this point the HTTP module machinery has been hooked up for\n // the user.\n s.push(buffered);\n s.push(null);\n });\n\n return fakeSocket;\n }\n}\n\nfunction resume(socket: net.Socket | tls.TLSSocket): void {\n socket.resume();\n}\n\nfunction omit<T extends object, K extends [...(keyof T)[]]>(\n obj: T,\n ...keys: K\n): {\n [K2 in Exclude<keyof T, K[number]>]: T[K2];\n} {\n const ret = {} as {\n [K in keyof typeof obj]: (typeof obj)[K];\n };\n let key: keyof typeof obj;\n for (key in obj) {\n if (!keys.includes(key)) {\n ret[key] = obj[key];\n }\n }\n return ret;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"https-proxy.mjs","names":["socket: net.Socket","headers: OutgoingHttpHeaders","key: keyof typeof obj"],"sources":["../src/https-proxy.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { AgentConnectOpts } from \"agent-base\";\nimport { Agent } from \"agent-base\";\nimport assert from \"node:assert\";\nimport { Buffer } from \"node:buffer\";\nimport type * as http from \"node:http\";\nimport type { OutgoingHttpHeaders } from \"node:http\";\nimport * as net from \"node:net\";\nimport * as tls from \"node:tls\";\nimport { URL } from \"node:url\";\nimport { parseProxyResponse } from \"./parse-response\";\n\nconst setServernameFromNonIpHost = <\n T extends { host?: string; servername?: string }\n>(\n options: T\n) => {\n if (\n options.servername === undefined &&\n options.host &&\n !net.isIP(options.host)\n ) {\n return {\n ...options,\n servername: options.host\n };\n }\n return options;\n};\n\ntype Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;\n\ninterface ConnectOptsMap {\n http: Omit<net.TcpNetConnectOpts, \"host\" | \"port\">;\n https: Omit<tls.ConnectionOptions, \"host\" | \"port\">;\n}\n\ntype ConnectOpts<T> = {\n [P in keyof ConnectOptsMap]: Protocol<T> extends P\n ? ConnectOptsMap[P]\n : never;\n}[keyof ConnectOptsMap];\n\nexport type HttpsProxyAgentOptions<T> = ConnectOpts<T> &\n http.AgentOptions & {\n headers?: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n };\n\n/**\n * The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to\n * the specified \"HTTP(s) proxy server\" in order to proxy HTTPS requests.\n *\n * Outgoing HTTP requests are first tunneled through the proxy server using the\n * `CONNECT` HTTP request method to establish a connection to the proxy server,\n * and then the proxy server connects to the destination target and issues the\n * HTTP request from the proxy server.\n *\n * `https:` requests have their socket connection upgraded to TLS once\n * the connection to the proxy server has been established.\n */\nexport class HttpsProxyAgent<Uri extends string> extends Agent {\n static protocols = [\"http\", \"https\"] as const;\n\n readonly proxy: URL;\n\n proxyHeaders: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);\n\n connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;\n\n constructor(proxy: Uri | URL, opts?: HttpsProxyAgentOptions<Uri>) {\n super(opts);\n this.options = { path: undefined };\n this.proxy = typeof proxy === \"string\" ? new URL(proxy) : proxy;\n this.proxyHeaders = opts?.headers ?? {};\n\n // Trim off the brackets from IPv6 addresses\n const host = (this.proxy.hostname || this.proxy.host).replace(\n /^\\[|\\]$/g,\n \"\"\n );\n const port = this.proxy.port\n ? Number.parseInt(this.proxy.port, 10)\n : this.proxy.protocol === \"https:\"\n ? 443\n : 80;\n this.connectOpts = {\n // Attempt to negotiate http/1.1 for proxy servers that support http/2\n ALPNProtocols: [\"http/1.1\"],\n ...(opts ? omit(opts, \"headers\") : null),\n host,\n port\n };\n }\n\n /**\n * Called when the node-core HTTP client library is creating a new HTTP request.\n */\n async connect(\n req: http.ClientRequest,\n opts: AgentConnectOpts\n ): Promise<net.Socket> {\n const { proxy } = this;\n\n if (!opts.host) {\n throw new TypeError('No \"host\" provided');\n }\n\n // Create a socket connection to the proxy server.\n let socket: net.Socket;\n if (proxy.protocol === \"https:\") {\n socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));\n } else {\n socket = net.connect(this.connectOpts);\n }\n\n const headers: OutgoingHttpHeaders =\n typeof this.proxyHeaders === \"function\"\n ? this.proxyHeaders()\n : { ...this.proxyHeaders };\n const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;\n let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\\r\\n`;\n\n // Inject the `Proxy-Authorization` header if necessary.\n if (proxy.username || proxy.password) {\n const auth = `${decodeURIComponent(\n proxy.username\n )}:${decodeURIComponent(proxy.password)}`;\n headers[\"Proxy-Authorization\"] = `Basic ${Buffer.from(auth).toString(\n \"base64\"\n )}`;\n }\n\n headers.Host = `${host}:${opts.port}`;\n\n if (!headers[\"Proxy-Connection\"]) {\n headers[\"Proxy-Connection\"] = this.keepAlive ? \"Keep-Alive\" : \"close\";\n }\n for (const name of Object.keys(headers)) {\n payload += `${name}: ${headers[name]?.toString()}\\r\\n`;\n }\n\n const proxyResponsePromise = parseProxyResponse(socket);\n\n socket.write(`${payload}\\r\\n`);\n\n const { connect, buffered } = await proxyResponsePromise;\n req.emit(\"proxyConnect\", connect);\n this.emit(\"proxyConnect\", connect, req);\n\n if (connect.statusCode === 200) {\n req.once(\"socket\", resume);\n\n if (opts.secureEndpoint) {\n // The proxy is connecting to a TLS server, so upgrade\n // this socket connection to a TLS connection.\n return tls.connect({\n ...omit(setServernameFromNonIpHost(opts), \"host\", \"path\", \"port\"),\n socket\n });\n }\n\n return socket;\n }\n\n // Some other status code that's not 200... need to re-play the HTTP\n // header \"data\" events onto the socket once the HTTP machinery is\n // attached so that the node core `http` can parse and handle the\n // error status code.\n\n // Close the original socket, and a new \"fake\" socket is returned\n // instead, so that the proxy doesn't get the HTTP request\n // written to it (which may contain `Authorization` headers or other\n // sensitive data).\n //\n // See: https://hackerone.com/reports/541502\n socket.destroy();\n\n const fakeSocket = new net.Socket({ writable: false });\n fakeSocket.readable = true;\n\n // Need to wait for the \"socket\" event to re-play the \"data\" events.\n req.once(\"socket\", (s: net.Socket) => {\n assert(s.listenerCount(\"data\") > 0);\n\n // Replay the \"buffered\" Buffer onto the fake `socket`, since at\n // this point the HTTP module machinery has been hooked up for\n // the user.\n s.push(buffered);\n s.push(null);\n });\n\n return fakeSocket;\n }\n}\n\nfunction resume(socket: net.Socket | tls.TLSSocket): void {\n socket.resume();\n}\n\nfunction omit<T extends object, K extends [...(keyof T)[]]>(\n obj: T,\n ...keys: K\n): {\n [K2 in Exclude<keyof T, K[number]>]: T[K2];\n} {\n const ret = {} as {\n [K in keyof typeof obj]: (typeof obj)[K];\n };\n let key: keyof typeof obj;\n for (key in obj) {\n if (!keys.includes(key)) {\n ret[key] = obj[key];\n }\n }\n return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 -1 +1,19 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_agent = require('./agent.cjs');
|
|
2
|
+
const require_http_proxy = require('./http-proxy.cjs');
|
|
3
|
+
const require_parse_response = require('./parse-response.cjs');
|
|
4
|
+
const require_https_proxy = require('./https-proxy.cjs');
|
|
5
|
+
const require_proxy_agent = require('./proxy-agent.cjs');
|
|
6
|
+
const require_fetch = require('./fetch.cjs');
|
|
7
|
+
const require_format_data_uri = require('./format-data-uri.cjs');
|
|
8
|
+
const require_get_free_port = require('./get-free-port.cjs');
|
|
9
|
+
|
|
10
|
+
exports.Agent = require_agent.Agent;
|
|
11
|
+
exports.HttpProxyAgent = require_http_proxy.HttpProxyAgent;
|
|
12
|
+
exports.HttpsProxyAgent = require_https_proxy.HttpsProxyAgent;
|
|
13
|
+
exports.fetch = require_fetch.fetch;
|
|
14
|
+
exports.fetchRequest = require_fetch.fetchRequest;
|
|
15
|
+
exports.formatDataURI = require_format_data_uri.formatDataURI;
|
|
16
|
+
exports.getFreePort = require_get_free_port.getFreePort;
|
|
17
|
+
exports.getProxyAgent = require_proxy_agent.getProxyAgent;
|
|
18
|
+
exports.makeDataUriToBuffer = require_format_data_uri.makeDataUriToBuffer;
|
|
19
|
+
exports.parseProxyResponse = require_parse_response.parseProxyResponse;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Agent } from "./agent.mjs";
|
|
2
|
+
import { HttpProxyAgent } from "./http-proxy.mjs";
|
|
3
|
+
import { parseProxyResponse } from "./parse-response.mjs";
|
|
4
|
+
import { HttpsProxyAgent } from "./https-proxy.mjs";
|
|
5
|
+
import { getProxyAgent } from "./proxy-agent.mjs";
|
|
6
|
+
import { fetch, fetchRequest } from "./fetch.mjs";
|
|
7
|
+
import { formatDataURI, makeDataUriToBuffer } from "./format-data-uri.mjs";
|
|
8
|
+
import { getFreePort } from "./get-free-port.mjs";
|
|
9
|
+
|
|
10
|
+
export { Agent, HttpProxyAgent, HttpsProxyAgent, fetch, fetchRequest, formatDataURI, getFreePort, getProxyAgent, makeDataUriToBuffer, parseProxyResponse };
|
package/dist/parse-response.cjs
CHANGED
|
@@ -1,4 +1,83 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let node_buffer = require("node:buffer");
|
|
3
|
+
|
|
4
|
+
//#region src/parse-response.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses the proxy CONNECT response from the given socket.
|
|
7
|
+
*
|
|
8
|
+
* @param socket - The socket to read the response from.
|
|
9
|
+
* @returns A promise that resolves to the CONNECT response and any buffered data.
|
|
10
|
+
*/
|
|
11
|
+
async function parseProxyResponse(socket) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
let buffersLength = 0;
|
|
14
|
+
const buffers = [];
|
|
15
|
+
function read() {
|
|
16
|
+
const b = socket.read();
|
|
17
|
+
if (b) ondata(b);
|
|
18
|
+
else socket.once("readable", read);
|
|
19
|
+
}
|
|
20
|
+
function cleanup() {
|
|
21
|
+
socket.removeListener("end", onend);
|
|
22
|
+
socket.removeListener("error", onerror);
|
|
23
|
+
socket.removeListener("readable", read);
|
|
24
|
+
}
|
|
25
|
+
function onend() {
|
|
26
|
+
cleanup();
|
|
27
|
+
reject(/* @__PURE__ */ new Error("Proxy connection ended before receiving CONNECT response"));
|
|
28
|
+
}
|
|
29
|
+
function onerror(err) {
|
|
30
|
+
cleanup();
|
|
31
|
+
reject(err);
|
|
32
|
+
}
|
|
33
|
+
function ondata(b) {
|
|
34
|
+
buffers.push(b);
|
|
35
|
+
buffersLength += b.length;
|
|
36
|
+
const buffered = node_buffer.Buffer.concat(buffers, buffersLength);
|
|
37
|
+
const endOfHeaders = buffered.indexOf("\r\n\r\n");
|
|
38
|
+
if (endOfHeaders === -1) {
|
|
39
|
+
read();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const headerParts = buffered.subarray(0, endOfHeaders).toString("ascii").split("\r\n");
|
|
43
|
+
const firstLine = headerParts.shift();
|
|
44
|
+
if (!firstLine) {
|
|
45
|
+
socket.destroy();
|
|
46
|
+
return reject(/* @__PURE__ */ new Error("No header received from proxy CONNECT response"));
|
|
47
|
+
}
|
|
48
|
+
const firstLineParts = firstLine.split(" ");
|
|
49
|
+
const statusCode = +firstLineParts[1];
|
|
50
|
+
const statusText = firstLineParts.slice(2).join(" ");
|
|
51
|
+
const headers = {};
|
|
52
|
+
for (const header of headerParts) {
|
|
53
|
+
if (!header) continue;
|
|
54
|
+
const firstColon = header.indexOf(":");
|
|
55
|
+
if (firstColon === -1) {
|
|
56
|
+
socket.destroy();
|
|
57
|
+
return reject(/* @__PURE__ */ new Error(`Invalid header from proxy CONNECT response: "${header}"`));
|
|
58
|
+
}
|
|
59
|
+
const key = header.slice(0, firstColon).toLowerCase();
|
|
60
|
+
const value = header.slice(firstColon + 1).trimStart();
|
|
61
|
+
const current = headers[key];
|
|
62
|
+
if (typeof current === "string") headers[key] = [current, value];
|
|
63
|
+
else if (Array.isArray(current)) current.push(value);
|
|
64
|
+
else headers[key] = value;
|
|
65
|
+
}
|
|
66
|
+
cleanup();
|
|
67
|
+
resolve({
|
|
68
|
+
connect: {
|
|
69
|
+
statusCode,
|
|
70
|
+
statusText,
|
|
71
|
+
headers
|
|
72
|
+
},
|
|
73
|
+
buffered
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
socket.on("error", onerror);
|
|
77
|
+
socket.on("end", onend);
|
|
78
|
+
read();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
exports.parseProxyResponse = parseProxyResponse;
|