Enable keepAlive by default on v3 (#347)

* Enable keepAlive by default on v3

* Extract to file

* Update type
This commit is contained in:
Steve Faulkner 2019-06-20 14:10:26 -07:00 коммит произвёл GitHub
Родитель d2d1c473eb
Коммит 9a65092742
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 37 добавлений и 9 удалений

12
package-lock.json сгенерированный
Просмотреть файл

@ -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
});
}