Skip to content

Commit

Permalink
enable keepalives in node
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-frank committed Dec 28, 2021
1 parent fc5e0d0 commit fa230b6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion fetch-npm-node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
"use strict";

var http = require('http');
var https = require('https');
var realFetch = require('node-fetch');

const httpAgent = new http.Agent({
keepAlive: true
});
const httpsAgent = new https.Agent({
keepAlive: true
});

const agent = function(_parsedURL) {
if (_parsedURL.protocol == 'http:') {
return httpAgent;
} else {
return httpsAgent;
}
};

module.exports = function(url, options) {
if (/^\/\//.test(url)) {
url = 'https:' + url;
}
return realFetch.call(this, url, options);
return realFetch.call(this, url, {agent, ...options});
};

if (!global.fetch) {
Expand Down

0 comments on commit fa230b6

Please sign in to comment.