From 7bda588ba056d1d568be8c3d60b112e7ea591b34 Mon Sep 17 00:00:00 2001 From: Sergey Date: Fri, 9 Aug 2019 00:30:42 +0300 Subject: [PATCH] Add an option to specify the http agent to use (#490) Co-Authored-By: Marco --- README.md | 4 ++- src/web-push-lib.js | 24 +++++++++++++++- test/test-generate-request-details.js | 41 +++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9490f555..aac33b1e 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,8 @@ const options = { '< header name >': '< header value >' }, contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >', - proxy: '< proxy server options >' + proxy: '< proxy server options >', + agent: '< https.Agent instance >' } webpush.sendNotification( @@ -185,6 +186,7 @@ retained by the push service (by default, four weeks). - **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options) that may either be a string URI of the proxy server (eg. http://< hostname >:< port >) or an "options" object with more specific properties. +- **agent** is the [HTTPS Agent instance](https://nodejs.org/dist/latest/docs/api/https.html#https_class_https_agent) which will be used in the `https.request` method. If the `proxy` options defined, `agent` will be ignored! ### Returns diff --git a/src/web-push-lib.js b/src/web-push-lib.js index 2a89574f..3a38ae30 100644 --- a/src/web-push-lib.js +++ b/src/web-push-lib.js @@ -110,6 +110,7 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op let extraHeaders = {}; let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM; let proxy; + let agent; if (options) { const validOptionKeys = [ @@ -118,7 +119,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op 'vapidDetails', 'TTL', 'contentEncoding', - 'proxy' + 'proxy', + 'agent' ]; const optionKeys = Object.keys(options); for (let i = 0; i < optionKeys.length; i += 1) { @@ -174,6 +176,18 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op console.warn('Attempt to use proxy option, but invalid type it should be a string or proxy options object.'); } } + + if (options.agent) { + if (options.agent instanceof https.Agent) { + if (proxy) { + console.warn('Agent option will be ignored because proxy option is defined.'); + } + + agent = options.agent; + } else { + console.warn('Wrong type for the agent option, it should be an instance of https.Agent.'); + } + } } if (typeof timeToLive === 'undefined') { @@ -265,6 +279,10 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op requestDetails.proxy = proxy; } + if (agent) { + requestDetails.agent = agent; + } + return requestDetails; }; @@ -300,6 +318,10 @@ WebPushLib.prototype.sendNotification = function(subscription, payload, options) httpsOptions.headers = requestDetails.headers; httpsOptions.method = requestDetails.method; + if (requestDetails.agent) { + httpsOptions.agent = requestDetails.agent; + } + if (requestDetails.proxy) { httpsOptions.agent = new HttpsProxyAgent(requestDetails.proxy); } diff --git a/test/test-generate-request-details.js b/test/test-generate-request-details.js index 3d81691c..d10fc11c 100644 --- a/test/test-generate-request-details.js +++ b/test/test-generate-request-details.js @@ -6,6 +6,7 @@ const webPush = require('../src/index'); const crypto = require('crypto'); const jws = require('jws'); const urlParse = require('url').parse; +const https = require('https'); suite('Test Generate Request Details', function() { test('is defined', function() { @@ -201,6 +202,31 @@ suite('Test Generate Request Details', function() { } } } + }, { + testTitle: 'invalid agent', + requestOptions: { + subscription: { + keys: VALID_KEYS + }, + message: 'hello', + addEndpoint: true, + extraOptions: { + agent: 'agent' + } + } + }, { + testTitle: 'ignore valid agent if proxy denifed', + requestOptions: { + subscription: { + keys: VALID_KEYS + }, + message: 'hello', + addEndpoint: true, + extraOptions: { + agent: new https.Agent({ keepAlive: true }), + proxy: 'http://localhost:3000' + } + } } ]; @@ -329,4 +355,19 @@ suite('Test Generate Request Details', function() { ); assert.equal(details.proxy, extraOptions.proxy); }); + + test('Agent option as an https.Agent instance', function() { + let subscription = { + endpoint: 'https://127.0.0.1:8080' + }; + let extraOptions = { + agent: new https.Agent({ keepAlive: true }) + }; + let details = webPush.generateRequestDetails( + subscription, + null, + extraOptions + ); + assert.equal(details.agent, extraOptions.agent); + }); });