diff --git a/CHANGELOG.md b/CHANGELOG.md index 292659d..fd265c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ --- +## [9.3.0](https://github.com/AndrewBarba/apns2/releases/tag/9.3.0) + +1. Update token refresh logic to avoid `TooManyProviderTokenUpdates` + ## [9.2.0](https://github.com/AndrewBarba/apns2/releases/tag/9.2.0) 1. Allow disabling pingInterval diff --git a/lib/apns.js b/lib/apns.js index 8b24738..de50243 100644 --- a/lib/apns.js +++ b/lib/apns.js @@ -69,10 +69,7 @@ class APNS extends EventEmitter { this._signingKey = signingKey this._defaultTopic = defaultTopic this._client = new Http2Client(host, { port, requestTimeout, pingInterval }) - this._resetTokenInterval = setInterval( - () => this._resetSigningToken(), - RESET_TOKEN_INTERVAL_MS - ).unref() + this._token = null this.on(Errors.expiredProviderToken, () => this._resetSigningToken()) } @@ -175,8 +172,8 @@ class APNS extends EventEmitter { * @return {String} */ _getSigningToken() { - if (this._token) { - return this._token + if (this._token && Date.now() - this._token.timestamp < RESET_TOKEN_INTERVAL_MS) { + return this._token.value } const claims = { @@ -203,7 +200,10 @@ class APNS extends EventEmitter { this.emit(Errors.invalidSigningKey) } - this._token = token + this._token = { + value: token, + timestamp: Date.now() + } return token } @@ -214,7 +214,6 @@ class APNS extends EventEmitter { */ _resetSigningToken() { this._token = null - return this._getSigningToken() } } diff --git a/package.json b/package.json index 227fb08..9312493 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apns2", - "version": "9.2.0", + "version": "9.3.0", "description": "Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.", "author": "Andrew Barba ", "main": "lib/apns.js",