Enable keepAlive by default on v3 (#347)
* Enable keepAlive by default on v3 * Extract to file * Update type
This commit is contained in:
Родитель
d2d1c473eb
Коммит
9a65092742
|
@ -260,9 +260,9 @@
|
|||
"dev": true
|
||||
},
|
||||
"@types/node-fetch": {
|
||||
"version": "2.1.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.1.7.tgz",
|
||||
"integrity": "sha512-TZozHCDVrs0Aj1B9ZR5F4Q9MknDNcVd+hO5lxXOCzz07ELBey6s1gMUSZHUYHlPfRFKJFXiTnNuD7ePiI6S4/g==",
|
||||
"version": "2.3.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.3.7.tgz",
|
||||
"integrity": "sha512-+bKtuxhj/TYSSP1r4CZhfmyA0vm/aDRQNo7vbAgf6/cZajn0SAniGGST07yvI4Q+q169WTa2/x9gEHfJrkcALw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
|
@ -3545,9 +3545,9 @@
|
|||
"integrity": "sha512-w07Dwqd/SWv9Lqrlhlx3mo4i4EWsuN3majbIIj4d6twBWGZUKtB9zvT9W+D5Rko56uas55CLO0YZ4zMrf6AKMw=="
|
||||
},
|
||||
"node-fetch": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.3.0.tgz",
|
||||
"integrity": "sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA=="
|
||||
"version": "2.6.0",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
|
||||
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
|
||||
},
|
||||
"normalize-path": {
|
||||
"version": "2.1.1",
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
"@types/fast-json-stable-stringify": "2.0.0",
|
||||
"@types/mocha": "^5.2.5",
|
||||
"@types/node": "^10.5.8",
|
||||
"@types/node-fetch": "2.1.7",
|
||||
"@types/node-fetch": "2.3.7",
|
||||
"@types/priorityqueuejs": "^1.0.1",
|
||||
"@types/semaphore": "^1.1.0",
|
||||
"@types/sinon": "7.0.10",
|
||||
|
@ -86,7 +86,7 @@
|
|||
"crypto-hash": "1.1.0",
|
||||
"fast-json-stable-stringify": "2.0.0",
|
||||
"node-abort-controller": "1.0.3",
|
||||
"node-fetch": "2.3.0",
|
||||
"node-fetch": "2.6.0",
|
||||
"priorityqueuejs": "1.0.0",
|
||||
"semaphore": "1.0.5",
|
||||
"tslib": "^1.9.3",
|
||||
|
|
|
@ -243,3 +243,7 @@ export function getResourceIdFromPath(resourcePath: string) {
|
|||
|
||||
return pathSegments[pathSegments.length - 1];
|
||||
}
|
||||
|
||||
// https://github.com/iliakan/detect-node/blob/master/index.js
|
||||
export const isNode: boolean =
|
||||
Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]";
|
||||
|
|
|
@ -4,6 +4,7 @@ import { trimSlashes } from "../common";
|
|||
import { Constants } from "../common/constants";
|
||||
import { executePlugins, PluginOn } from "../plugins/Plugin";
|
||||
import * as RetryUtility from "../retry/retryUtility";
|
||||
import { defaultHttpAgent, defaultHttpsAgent } from "./defaultAgent";
|
||||
import { ErrorResponse } from "./ErrorResponse";
|
||||
import { bodyFromData } from "./request";
|
||||
import { RequestContext } from "./RequestContext";
|
||||
|
@ -45,7 +46,12 @@ async function httpRequest(requestContext: RequestContext) {
|
|||
response = await fetch(trimSlashes(requestContext.endpoint) + requestContext.path, {
|
||||
method: requestContext.method,
|
||||
headers: requestContext.headers as any,
|
||||
agent: requestContext.requestAgent,
|
||||
agent: (parsedUrl: URL) => {
|
||||
if (requestContext.requestAgent) {
|
||||
return requestContext.requestAgent;
|
||||
}
|
||||
return parsedUrl.protocol === "http" ? defaultHttpAgent : defaultHttpsAgent;
|
||||
},
|
||||
signal,
|
||||
body: requestContext.body
|
||||
} as RequestInit);
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
import { Agent } from "http";
|
||||
import { isNode } from "../common";
|
||||
|
||||
export let defaultHttpAgent: Agent;
|
||||
export let defaultHttpsAgent: Agent;
|
||||
|
||||
if (isNode) {
|
||||
// tslint:disable-next-line:no-var-requires
|
||||
const https = require("https");
|
||||
defaultHttpsAgent = new https.Agent({
|
||||
keepAlive: true
|
||||
});
|
||||
// tslint:disable-next-line:no-var-requires
|
||||
const http = require("http");
|
||||
defaultHttpAgent = new http.Agent({
|
||||
keepAlive: true
|
||||
});
|
||||
}
|
Загрузка…
Ссылка в новой задаче