Skip to content

Commit

Permalink
Refactor token refresh logic (#45)
Browse files Browse the repository at this point in the history
* Refactor token refresh logic

* 9.3.0
  • Loading branch information
AndrewBarba authored Nov 18, 2020
1 parent 822bdbb commit bb93f08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lib/apns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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 = {
Expand All @@ -203,7 +200,10 @@ class APNS extends EventEmitter {
this.emit(Errors.invalidSigningKey)
}

this._token = token
this._token = {
value: token,
timestamp: Date.now()
}

return token
}
Expand All @@ -214,7 +214,6 @@ class APNS extends EventEmitter {
*/
_resetSigningToken() {
this._token = null
return this._getSigningToken()
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"main": "lib/apns.js",
Expand Down

0 comments on commit bb93f08

Please sign in to comment.