Skip to content

Commit d7839a8

Browse files
authored
Optional timeout option for send notification requests (#569)
Fixes #568
1 parent b7d566e commit d7839a8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const options = {
129129
publicKey: '< URL Safe Base64 Encoded Public Key >',
130130
privateKey: '< URL Safe Base64 Encoded Private Key >'
131131
},
132+
timeout: <Number>
132133
TTL: <Number>,
133134
headers: {
134135
'< header name >': '< header value >'
@@ -179,6 +180,7 @@ any of the following values defined, although none of them are required.
179180
request only. This overrides any API key set via `setGCMAPIKey()`.
180181
- **vapidDetails** should be an object with *subject*, *publicKey* and
181182
*privateKey* values defined. These values should follow the [VAPID Spec](https://tools.ietf.org/html/draft-thomson-webpush-vapid).
183+
- **timeout** is a value in milliseconds that specifies how long the library must wait for a response from the push service before timing out (by default undefined).
182184
- **TTL** is a value in seconds that describes how long a push message is
183185
retained by the push service (by default, four weeks).
184186
- **headers** is an object with all the extra headers you want to add to the request.

src/web-push-lib.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
111111
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
112112
let proxy;
113113
let agent;
114+
let timeout;
114115

115116
if (options) {
116117
const validOptionKeys = [
@@ -120,7 +121,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
120121
'TTL',
121122
'contentEncoding',
122123
'proxy',
123-
'agent'
124+
'agent',
125+
'timeout'
124126
];
125127
const optionKeys = Object.keys(options);
126128
for (let i = 0; i < optionKeys.length; i += 1) {
@@ -191,6 +193,10 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
191193
console.warn('Wrong type for the agent option, it should be an instance of https.Agent.');
192194
}
193195
}
196+
197+
if (typeof options.timeout === 'number') {
198+
timeout = options.timeout;
199+
}
194200
}
195201

196202
if (typeof timeToLive === 'undefined') {
@@ -277,6 +283,10 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
277283
requestDetails.agent = agent;
278284
}
279285

286+
if (timeout) {
287+
requestDetails.timeout = timeout;
288+
}
289+
280290
return requestDetails;
281291
};
282292

@@ -312,6 +322,10 @@ WebPushLib.prototype.sendNotification = function(subscription, payload, options)
312322
httpsOptions.headers = requestDetails.headers;
313323
httpsOptions.method = requestDetails.method;
314324

325+
if (requestDetails.timeout) {
326+
httpsOptions.timeout = requestDetails.timeout;
327+
}
328+
315329
if (requestDetails.agent) {
316330
httpsOptions.agent = requestDetails.agent;
317331
}

0 commit comments

Comments
 (0)