Socket
Socket
Sign inDemoInstall

twilio-sync

Package Overview
Dependencies
Maintainers
2
Versions
608
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twilio-sync - npm Package Compare versions

Comparing version 0.3.0-alpha.17 to 0.3.0-build.126

106

lib/client.js

@@ -164,6 +164,8 @@ 'use strict';

var productId = options.productId || SYNC_PRODUCT_ID;
options.productId = options.productId || SYNC_PRODUCT_ID;
var productId = options.productId;
options.twilsockClient = options.twilsockClient || new _twilsock2.default(token, options);
options.transport = options.transport || new _twilioTransport2.default(options.twilsockClient);
options.notificationsClient = options.notificationsClient || new _twilioNotifications2.default(productId, token, options);
options.notificationsClient = options.notificationsClient || new _twilioNotifications2.default(token, options);

@@ -195,21 +197,10 @@ var transport = options.transport;

},
_connectionState: { value: Client.connectionState.CONNECTING, writable: true },
connectionState: { get: function get() {
return _this._connectionState;
return _this._notifications.connectionState;
}, enumerable: true }
});
notifications.on('transportReady', function (state) {
if (state) {
_this._connectionState = Client.connectionState.CONNECTED;
} else {
switch (_this._twilsock.state) {
case 'rejected':
_this._connectionState = Client.connectionState.DENIED;
break;
default:
_this._connectionState = Client.connectionState.CONNECTING;
}
}
_this.emit('connectionStateChanged', _this._connectionState);
notifications.on('connectionStateChanged', function () {
_this.emit('connectionStateChanged', _this._notifications.connectionState);
});

@@ -277,4 +268,9 @@ return _this;

var cached = this._entities.get(id, type);
return cached ? _promise2.default.resolve(cached) : null;
return cached ? _promise2.default.resolve(cached._subscribe()) : null;
}
}, {
key: '_removeFromCache',
value: function _removeFromCache(sid) {
this._entities.remove(sid);
}

@@ -293,13 +289,19 @@ /**

var _decompose = decompose(arg);
var _decompose = decompose(arg),
id = _decompose.id,
purpose = _decompose.purpose,
data = _decompose.data,
mode = _decompose.mode;
var id = _decompose.id;
var purpose = _decompose.purpose;
var data = _decompose.data;
var mode = _decompose.mode;
return this._getCached(id, 'doc') || this._getDocument(id).then(function (body) {
return body || (mode !== 'open' ? _this2._createDocument(id, purpose, data) : new _syncerror2.default('Not found', 404));
return this._getCached(id, 'document') || this._getDocument(id).then(function (body) {
if (body) {
return body;
} else if (mode !== 'open') {
return _this2._createDocument(id, purpose, data);
}
throw new _syncerror2.default('Not found', 404);
}).then(function (body) {
return new _syncdocument2.default(_this2._dependencies, body);
return new _syncdocument2.default(_this2._dependencies, body, function (sid) {
return _this2._removeFromCache(sid);
});
}).then(function (entity) {

@@ -322,13 +324,19 @@ return _this2._entities.store(entity);

var _decompose2 = decompose(arg);
var _decompose2 = decompose(arg),
id = _decompose2.id,
purpose = _decompose2.purpose,
context = _decompose2.context,
mode = _decompose2.mode;
var id = _decompose2.id;
var purpose = _decompose2.purpose;
var context = _decompose2.context;
var mode = _decompose2.mode;
return this._getCached(id, 'map') || this._getMap(id).then(function (body) {
return body || (mode !== 'open' ? _this3._createMap(id, purpose, context) : new _syncerror2.default('Not found', 404));
if (body) {
return body;
} else if (mode !== 'open') {
return _this3._createMap(id, purpose, context);
}
throw new _syncerror2.default('Not found', 404);
}).then(function (body) {
return new _syncmap2.default(_this3._dependencies, body);
return new _syncmap2.default(_this3._dependencies, body, function (sid) {
return _this3._removeFromCache(sid);
});
}).then(function (entity) {

@@ -351,13 +359,19 @@ return _this3._entities.store(entity);

var _decompose3 = decompose(arg);
var _decompose3 = decompose(arg),
id = _decompose3.id,
purpose = _decompose3.purpose,
context = _decompose3.context,
mode = _decompose3.mode;
var id = _decompose3.id;
var purpose = _decompose3.purpose;
var context = _decompose3.context;
var mode = _decompose3.mode;
return this._getCached(id, 'list') || this._getList(id).then(function (body) {
return body || (mode !== 'open' ? _this4._createList(id, purpose, context) : new _syncerror2.default('Not found', 404));
if (body) {
return body;
} else if (mode !== 'open') {
return _this4._createList(id, purpose, context);
}
throw new _syncerror2.default('Not found', 404);
}).then(function (body) {
return new _synclist2.default(_this4._dependencies, body);
return new _synclist2.default(_this4._dependencies, body, function (sid) {
return _this4._removeFromCache(sid);
});
}).then(function (entity) {

@@ -378,3 +392,7 @@ return _this4._entities.store(entity);

value: function shutdown() {
return this._twilsock.disconnect();
var _this5 = this;
return this._subscriptions.shutdown().then(function () {
return _this5._twilsock.disconnect();
}).then(function () {});
}

@@ -381,0 +399,0 @@

@@ -59,2 +59,13 @@ 'use strict';

}, {
key: 'remove',
value: function remove(sid) {
var cached = this._entities.get(sid);
if (cached) {
this._entities.delete(sid);
if (cached.uniqueName) {
this._names.delete(cached.type + '::' + cached.uniqueName);
}
}
}
}, {
key: '_getResolved',

@@ -61,0 +72,0 @@ value: function _getResolved(id, type) {

@@ -60,3 +60,4 @@ 'use strict';

value: function _subscribe() {
return this._deps.router.subscribe(this.sid, this);
this._deps.router.subscribe(this.sid, this);
return this;
}

@@ -72,3 +73,4 @@

value: function _unsubscribe() {
return this._deps.router.unsubscribe(this.sid, this);
this._deps.router.unsubscribe(this.sid, this);
return this;
}

@@ -75,0 +77,0 @@

@@ -6,6 +6,10 @@ 'use strict';

* since vanilla has the bug in path calculation and maintaner doesn't accept the patch.
*
*
* Should return to the using of original module when situation will change
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof2 = require('babel-runtime/helpers/typeof');

@@ -257,2 +261,3 @@

module.exports = JsonDiff;
exports.default = JsonDiff;
module.exports = exports['default'];

@@ -7,6 +7,2 @@ 'use strict';

var _freeze = require('babel-runtime/core-js/object/freeze');
var _freeze2 = _interopRequireDefault(_freeze);
var _defineProperties = require('babel-runtime/core-js/object/define-properties');

@@ -145,5 +141,2 @@

exports.default = Router;
(0, _freeze2.default)(Router);
module.exports = exports['default'];

@@ -7,6 +7,2 @@ 'use strict';

var _freeze = require('babel-runtime/core-js/object/freeze');
var _freeze2 = _interopRequireDefault(_freeze);
var _set = require('babel-runtime/core-js/set');

@@ -28,2 +24,6 @@

var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

@@ -37,2 +37,10 @@

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _backoff = require('backoff');

@@ -42,6 +50,2 @@

var _eventToPromise = require('event-to-promise');
var _eventToPromise2 = _interopRequireDefault(_eventToPromise);
var _logger = require('./logger');

@@ -53,2 +57,36 @@

function substract(p1, p2) {
var result = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(p1), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
key = _step$value[0],
value = _step$value[1];
if (!p2.has(key)) {
result.push({ sid: key, type: value.type });
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return result;
}
/**

@@ -58,90 +96,81 @@ * @class Subscriptions

*/
var Subscriptions = function () {
/*
/**
* @constructor
*/
function Subscriptions(config, network) {
var _this = this;
(0, _classCallCheck3.default)(this, Subscriptions);
(0, _defineProperties2.default)(this, {
_config: { value: config },
_network: { value: network },
_subscriptions: { value: new _map2.default() },
_serverSubscriptions: { value: new _map2.default() }
});
}
/**
* @private
*/
(0, _createClass3.default)(Subscriptions, [{
key: '_createBackoff',
value: function _createBackoff() {
function createBackoff() {
var defaultBackoffConfig = {
randomisationFactor: 0.2,
initialDelay: 1,
initialDelay: 50,
maxDelay: 2 * 60 * 1000
};
return _backoff2.default.exponential(this._config.backoffConfig || defaultBackoffConfig);
return _backoff2.default.exponential((0, _extends3.default)(defaultBackoffConfig, config.backoffConfig || {}));
}
/**
* @private
*/
(0, _defineProperties2.default)(this, {
_config: { value: config },
_network: { value: network },
_backoff: { value: createBackoff() },
}, {
key: '_subscribeOnServer',
value: function _subscribeOnServer(sid, entity) {
var _this = this;
_types: { value: new _map2.default() },
_subscriptions: { value: new _map2.default() },
_persisted: { value: new _map2.default() }
});
var backoff = this._createBackoff();
var shouldRetry = function shouldRetry() {
return _this._subscriptions.has(sid);
};
return this._subscribeWithBackoff(sid, entity, backoff, shouldRetry);
}
this._backoff.on('ready', function () {
var _getAction2 = _this._getAction(),
action = _getAction2.action,
list = _getAction2.list;
/**
* @private
*/
if (action) {
_this._request(action, list.map(function (x) {
return { object_sid: x.sid, object_type: x.type };
})) // eslint-disable-line camelcase
.then(function () {
_this._backoff.reset();
_this._backoff.backoff();
}).catch(function (e) {
_logger2.default.error('Error while persisting subscriptions, retrying', action, e);
_this._backoff.backoff(e);
});
} else {
_this._backoff.reset();
_logger2.default.info('Subscription list persisted');
}
});
}
}, {
key: '_subscribeWithBackoff',
value: function _subscribeWithBackoff(sid, entity, backoff, shouldRetry) {
(0, _createClass3.default)(Subscriptions, [{
key: '_getAction',
value: function _getAction() {
var _this2 = this;
/* eslint-disable camelcase */
var requestBody = {
event_protocol_version: 3,
action: 'establish',
requests: [{
object_type: entity.type,
object_sid: sid
}]
};
/* eslint-enable camelcase */
var listToAdd = substract(this._subscriptions, this._persisted);
if (listToAdd.length > 0) {
return { action: 'establish', list: listToAdd };
}
var promise = (0, _eventToPromise2.default)(backoff, 'ready').then(function () {
if (!shouldRetry()) {
return false;
}
var listToRemove = substract(this._persisted, this._subscriptions).map(function (x) {
return { sid: x.sid, type: _this2._types.get(x.sid) };
});
if (listToRemove.length > 0) {
return { action: 'cancel', list: listToRemove };
}
return _this2._network.post(_this2._config.subscriptionsUri, requestBody).then(function (response) {
response.body.results.forEach(function (result) {
var subscription = _this2._subscriptions.get(result.object_sid);
if (subscription && subscription.state === 'added') {
subscription.state = 'synced';
}
});
return true;
}).catch(function (err) {
_logger2.default.error('Subscription failed:', err);
return _this2._subscribeWithBackoff(sid, entity, backoff, shouldRetry);
});
});
backoff.backoff();
return promise;
return {};
}
}, {
key: '_persist',
value: function _persist() {
try {
this._backoff.backoff();
} catch (e) {} // eslint-disable-line no-empty
}

@@ -153,52 +182,28 @@ /**

}, {
key: '_unsubscribeOnServer',
value: function _unsubscribeOnServer(sid, entity) {
key: '_request',
value: function _request(action, requests) {
var _this3 = this;
var backoff = this._createBackoff();
var shouldRetry = function shouldRetry() {
return !_this3._subscriptions.has(sid);
};
return this._unsubscribeWithBackoff(sid, entity, backoff, shouldRetry);
}
_logger2.default.debug('Modifying server subscriptions: ', action, requests);
/**
* @private
*/
}, {
key: '_unsubscribeWithBackoff',
value: function _unsubscribeWithBackoff(sid, entity, backoff, shouldRetry) {
var _this4 = this;
/* eslint-disable camelcase */
var requestBody = {
event_protocol_version: 3,
action: 'cancel',
requests: [{
object_type: entity.type,
object_sid: sid
}]
event_protocol_version: 3, // eslint-disable-line camelcase
action: action,
requests: requests
};
/* eslint-enable camelcase */
var promise = (0, _eventToPromise2.default)(backoff, 'ready').then(function () {
if (!shouldRetry()) {
return false;
}
return _this4._network.post(_this4._config.subscriptionsUri, requestBody).then(function (response) {
response.body.results.forEach(function (result) {
var subscription = _this4._subscriptions.get(result.object_sid);
if (subscription && subscription.state === 'removed') {
_this4._serverSubscriptions.delete(result.object_sid);
return this._network.post(this._config.subscriptionsUri, requestBody).then(function (response) {
response.body.results.forEach(function (persisted) {
if (!persisted.subscription) {
_this3._persisted.delete(persisted.object_sid);
_this3._types.delete(persisted.object_sid);
} else {
_this3._persisted.set(persisted.object_sid, persisted.subscription);
var subscription = _this3._subscriptions.get(persisted.object_sid);
if (subscription) {
subscription.resolve(true);
}
});
return true;
}).catch(function (err) {
_logger2.default.error('Subscription failed:', err);
return _this4._subscribeWithBackoff(sid, entity, backoff, shouldRetry);
}
});
});
backoff.backoff();
return promise;
}

@@ -216,15 +221,31 @@

value: function add(sid, entity) {
if (this._subscriptions.has(sid)) {
var subscription = this._subscriptions.get(sid);
subscription.subscribers.add(entity);
var _this4 = this;
var subscribed = this._subscriptions.has(sid);
var persisted = this._persisted.has(sid);
if (subscribed && persisted) {
var _subscription = this._subscriptions.get(sid);
_subscription.subscribers.add(entity);
return _promise2.default.resolve(false);
} else if (this._subscribed && !persisted) {
var _subscription2 = this._subscriptions.get(sid);
return _subscription2.promise;
}
var subscribers = new _set2.default();
subscribers.add(entity);
this._subscriptions.set(sid, {
state: 'added',
subscribers: subscribers
var subscription = { type: entity.type,
subscribers: new _set2.default([entity]),
promise: null,
resolve: null,
reject: null
};
this._subscriptions.set(sid, subscription);
var promiseToSubscribe = new _promise2.default(function (resolve, reject) {
subscription.resolve = resolve;
subscription.reject = reject;
_this4._persist();
});
return this._subscribeOnServer(sid, entity);
subscription.promise = promiseToSubscribe;
return promiseToSubscribe;
}

@@ -246,18 +267,18 @@

var subscription = this._subscriptions.get(id);
var subscribers = subscription.subscribers;
if (!this._subscriptions.get(id).subscribers.has(entity)) {
if (!subscription.subscribers.has(entity)) {
return _promise2.default.resolve(false);
}
subscribers.delete(entity);
if (subscribers.size > 0) {
subscription.subscribers.delete(entity);
if (subscription.subscribers.size > 0) {
return _promise2.default.resolve(false);
}
// It's a hack: since server responce doesn't contain a type, but we need it
// we should always keep it locally
this._types.set(id, subscription.type);
this._subscriptions.delete(id);
if (!this._serverSubscriptions.has(id)) {
return _promise2.default.resolve(true);
}
return this._unsubscribeOnServer(id, entity);
this._persist();
return _promise2.default.resolve(true);
}

@@ -274,3 +295,3 @@

var subscription = this._subscriptions.get(id);
return subscription ? subscription.subscribers : new _set2.default();
return subscription && subscription.subscribers ? subscription.subscribers : new _set2.default();
}

@@ -292,2 +313,47 @@

}
/**
* Currently just resets retries
* Should remove all subscriptions
*/
}, {
key: 'shutdown',
value: function shutdown() {
this._backoff.reset();
this._subscriptions.clear();
var listToRemove = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(this._persisted), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _step2$value = (0, _slicedToArray3.default)(_step2.value, 2),
key = _step2$value[0],
value = _step2$value[1];
listToRemove.push({ object_sid: key, type: value.type }); // eslint-disable-line camelcase
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
if (listToRemove.length > 0) {
return this._request('cancel', listToRemove).catch(function () {});
}
return _promise2.default.resolve();
}
}]);

@@ -298,5 +364,2 @@ return Subscriptions;

exports.default = Subscriptions;
(0, _freeze2.default)(Subscriptions);
module.exports = exports['default'];

@@ -59,2 +59,3 @@ 'use strict';

* @property {String} sid SyncDocument unique id
* @property {String} uniqueName Unique name of a document
* @property {Object} value Value of a document

@@ -74,3 +75,3 @@ *

*/
function SyncDocument(params, descriptor) {
function SyncDocument(params, descriptor, onRemoveDocument) {
(0, _classCallCheck3.default)(this, SyncDocument);

@@ -88,2 +89,3 @@

_pendingListeners: { value: {} },
_onRemoveDocument: { value: onRemoveDocument },

@@ -96,5 +98,6 @@ _descriptor: { value: descriptor },

uri: { enumerable: true, get: function get() {
uri: { get: function get() {
return _this._descriptor.url;
} },
sid: { enumerable: true, get: function get() {

@@ -105,2 +108,5 @@ return _this._sid;

return _this._data;
} },
uniqueName: { enumerable: true, get: function get() {
return _this._descriptor.unique_name || null;
} }

@@ -374,2 +380,3 @@ });

this._onRemoveDocument(this.sid);
return this._network.delete(this.uri).then(function () {

@@ -376,0 +383,0 @@ _this8.emit('removed', true);

@@ -92,3 +92,3 @@ 'use strict';

*/
function SyncList(deps, descriptor) {
function SyncList(deps, descriptor, onRemoveList) {
(0, _classCallCheck3.default)(this, SyncList);

@@ -106,2 +106,3 @@

_cache: { value: new _cache2.default() },
_onRemoveList: { value: onRemoveList },

@@ -124,3 +125,3 @@ _links: { get: function get() {

uniqueName: { enumerable: true, get: function get() {
return _this._descriptor.uniqueName;
return _this._descriptor.unique_name || null;
} }

@@ -437,2 +438,3 @@ });

this._onRemoveList(this.sid);
return this._deps.network.delete(this.uri).then(function () {

@@ -439,0 +441,0 @@ _this12.emit('collectionRemoved', true);

@@ -90,3 +90,3 @@ 'use strict';

*/
function SyncMap(deps, descriptor) {
function SyncMap(deps, descriptor, onRemoveMap) {
(0, _classCallCheck3.default)(this, SyncMap);

@@ -106,2 +106,3 @@

_updateQueue: { value: new _retryingqueue2.default() },
_onRemoveMap: { value: onRemoveMap },

@@ -124,3 +125,3 @@ _links: { get: function get() {

uniqueName: { enumerable: true, get: function get() {
return _this._descriptor.uniqueName;
return _this._descriptor.unique_name || null;
} }

@@ -684,2 +685,3 @@ });

this._onRemoveMap(this.sid);
return this._deps.network.delete(this.uri).then(function () {

@@ -686,0 +688,0 @@ _this13.emit('collectionRemoved', true);

{
"name": "twilio-sync",
"version": "0.3.0-alpha.17",
"version": "0.3.0-build.126",
"description": "Twilio Sync client library",

@@ -17,8 +17,7 @@ "main": "lib/index.js",

"babel-runtime": "^6.11.6",
"event-to-promise": "^0.7.0",
"loglevel": "^1.4.1",
"platform": "^1.3.1",
"twilio-notifications": "^0.2.0-alpha",
"twilio-transport": "^0.0.5",
"twilsock": "^0.2.0-alpha",
"twilio-notifications": "^0.2.0",
"twilio-transport": "^0.0.8",
"twilsock": "^0.2.0",
"uuid": "^2.0.2"

@@ -40,2 +39,3 @@ },

"del": "^2.2.1",
"event-to-promise": "^0.7.0",
"gulp": "^3.9.1",

@@ -57,3 +57,2 @@ "gulp-derequire": "^2.1.0",

"jsonwebtoken": "^7.1.6",
"mocha.parallel": "^0.13.0",
"run-sequence": "^1.2.2",

@@ -60,0 +59,0 @@ "sinon": "^1.17.5",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc