From 84530fdf95338e267a2175af6c35acdd383c0fc1 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Wed, 5 Aug 2020 08:07:02 +0200 Subject: [PATCH] adding keep-alive to http(s) calls --- package.json | 8 ++++---- src/load.js | 27 ++++++++++++++++++++++++++- yarn.lock | 23 ++++++++++++++--------- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index e9275b0..c5e5a10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swrlab/node-storage-wrapper", - "version": "0.0.7", + "version": "0.0.8", "description": "Wrapping AWS S3, GCP GCS, file storage", "main": "./src/index.js", "engines": { @@ -17,13 +17,13 @@ }, "dependencies": { "@google-cloud/storage": "5.1.2", - "aws-sdk": "2.719.0", + "aws-sdk": "2.726.0", "node-fetch": "2.6.0", - "uuid": "8.2.0" + "uuid": "8.3.0" }, "devDependencies": { "@swrlab/swr-prettier-config": "0.0.2", - "eslint": "^7.5.0", + "eslint": "^7.6.0", "eslint-plugin-swr": "0.0.3" }, "prettier": "@swrlab/swr-prettier-config" diff --git a/src/load.js b/src/load.js index dc69e50..6983353 100644 --- a/src/load.js +++ b/src/load.js @@ -6,6 +6,16 @@ // load node utils const fetch = require('node-fetch'); +const http = require('http'); +const https = require('https'); + +// enable keepalive +const httpAgent = new http.Agent({ + keepAlive: true, +}); +const httpsAgent = new https.Agent({ + keepAlive: true, +}); const loadLocalFile = (that, uri) => new Promise((resolve, reject) => { @@ -59,7 +69,22 @@ module.exports = async function (uri) { this.sdk.log(this, 'log', ['storage.load.https >', uri]); // public http(s) endpoint - let file = await fetch(uri); + let file = await fetch(uri, { + method: 'get', + + headers: { + 'User-Agent': 'node-storage-wrapper', + Connection: 'keep-alive', + }, + + agent: function (_parsedURL) { + if (_parsedURL.protocol == 'http:') { + return httpAgent; + } else { + return httpsAgent; + } + }, + }); if (file.ok) { file = await file.buffer(); diff --git a/yarn.lock b/yarn.lock index c37bed9..68443c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -305,10 +305,10 @@ astral-regex@^1.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9" integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg== -aws-sdk@2.719.0: - version "2.719.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.719.0.tgz#346421f3335a7ded2cc6184fbcdbf71e42544d08" - integrity sha512-vqVAeZ2C8VLvL1hJIBCRFnKMomIoWSIUeYjULRiwRlBYH95EgcDY559Mq2rkx+E0733jdf3wNHA+eRVmwHyXvQ== +aws-sdk@2.726.0: + version "2.726.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.726.0.tgz#075aae1922e67e21285abae1bb081252cff4fec3" + integrity sha512-QRQ7MaW5dprdr/T3vCTC+J8TeUfpM45yWsBuATPcCV/oO8afFHVySwygvGLY4oJuo5Mf4mJn3+JYTquo6CqiaA== dependencies: buffer "4.9.2" events "1.1.1" @@ -785,10 +785,10 @@ eslint-visitor-keys@^1.0.0, eslint-visitor-keys@^1.1.0, eslint-visitor-keys@^1.3 resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz#30ebd1ef7c2fdff01c3a4f151044af25fab0523e" integrity sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ== -eslint@^7.5.0: - version "7.5.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.5.0.tgz#9ecbfad62216d223b82ac9ffea7ef3444671d135" - integrity sha512-vlUP10xse9sWt9SGRtcr1LAC67BENcQMFeV+w5EvLEoFe3xJ8cF1Skd0msziRx/VMC+72B4DxreCE+OR12OA6Q== +eslint@^7.6.0: + version "7.6.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.6.0.tgz#522d67cfaea09724d96949c70e7a0550614d64d6" + integrity sha512-QlAManNtqr7sozWm5TF4wIH9gmUm2hE3vNRUvyoYAa4y1l5/jxD/PQStEjBMQtCqZmSep8UxrcecI60hOpe61w== dependencies: "@babel/code-frame" "^7.0.0" ajv "^6.10.0" @@ -2140,7 +2140,12 @@ uuid@3.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== -uuid@8.2.0, uuid@^8.0.0: +uuid@8.3.0: + version "8.3.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea" + integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ== + +uuid@^8.0.0: version "8.2.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.2.0.tgz#cb10dd6b118e2dada7d0cd9730ba7417c93d920e" integrity sha512-CYpGiFTUrmI6OBMkAdjSDM0k5h8SkkiTP4WAjQgDgNB1S3Ou9VBEvr6q0Kv2H1mMk7IWfxYGpMH5sd5AvcIV2Q==