Skip to content

Commit

Permalink
Fix bug where Subscription fails to parse expirationTime in IE11 (#78)
Browse files Browse the repository at this point in the history
* Fix bug where Subscription fails to parse expirationTime in IE11
* move iso string conversion logic into parseISOString helper function
  • Loading branch information
u9520107 authored and kirill-konshin committed Jul 26, 2017
1 parent cbff4e9 commit 3310097
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 61 deletions.
117 changes: 71 additions & 46 deletions build/ringcentral.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/ringcentral.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions build/ringcentral.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ringcentral.min.js.map

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions src/subscription/Subscription-spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe('RingCentral.subscription.Subscription', function() {

var pollInterval = 1;
var renewHandicapMs = 1;
var renewHandicapMs = 30;
var expiresIn = 100; // 100 seconds
var quickExpiresIn = 0.1; // 50 ms
var quickExpiresIn = 0.5; // 50 ms

function createSubscription(sdk) {
return sdk.createSubscription({
Expand Down Expand Up @@ -41,6 +41,33 @@ describe('RingCentral.subscription.Subscription', function() {

}));

it('automatically renews subscription with +0000 timezone format', asyncTest(function(sdk) {

subscribeGeneric(quickExpiresIn, null, null, '+0000');
subscribeGeneric(10, 'foo-bar-baz'); // should be a good value for future response

var subscription = createSubscription(sdk);

return subscription
.setEventFilters(['foo', 'bar'])
.register()
.then(function(res) {

expect(res.json().expiresIn).to.equal(quickExpiresIn);

return new Promise(function(resolve, reject) {
subscription.on(subscription.events.automaticRenewError, function(e) {
reject(e);
});
subscription.on(subscription.events.automaticRenewSuccess, function() {
resolve(null);
});
});

});

}));

it('captures automatic subscription renew errors', asyncTest(function(sdk) {

subscribeGeneric(quickExpiresIn);
Expand Down
29 changes: 26 additions & 3 deletions src/subscription/Subscription.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
var EventEmitter = require("events").EventEmitter;

// detect ISO 8601 format string with +00[:00] timezone notations
var ISO_REG_EXP = /(\+[\d]{2}):?([\d]{2})?$/;

function buildIEFriendlyString(match, $1, $2) {
return $1 + ':' + ($2 || '00');
}

/**
*
* @param {string} time
* @return {number}
*/
function parseISOString(time) {
time = time || 0;
if (typeof time === 'string') {
return Date.parse(time.replace(ISO_REG_EXP, buildIEFriendlyString));
}
return time;
}

/**
* @param {Platform} options.platform
* @param {Externals} options.externals
Expand Down Expand Up @@ -87,11 +107,14 @@ Subscription.prototype.alive = function() {
*/
Subscription.prototype.expired = function() {
if (!this.subscribed()) return true;
return !this.subscribed() || Date.now() > this.subscription().expirationTime;
return !this.subscribed() || Date.now() > parseISOString(this.subscription().expirationTime);
};

/**
* @return {number}
*/
Subscription.prototype.expirationTime = function() {
return new Date(this.subscription().expirationTime || 0).getTime() - this._renewHandicapMs;
return parseISOString(this.subscription().expirationTime) - this._renewHandicapMs;
};

/**
Expand Down Expand Up @@ -473,7 +496,7 @@ module.exports = Subscription;
* @property {string} [id]
* @property {string} [uri]
* @property {string[]} [eventFilters]
* @property {string} [expirationTime] Format: 2014-03-12T19:54:35.613Z
* @property {string} [expirationTime] Format: 2014-03-12T19:54:35.613+0000
* @property {int} [expiresIn]
* @property {string} [deliveryMode.transportType]
* @property {boolean} [deliveryMode.encryption]
Expand Down
Loading

0 comments on commit 3310097

Please sign in to comment.