sharedb 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/agent.js CHANGED
@@ -447,6 +447,8 @@ Agent.prototype._handleMessage = function(request, callback) {
447
447
  return this._subscribePresence(request.ch, request.seq, callback);
448
448
  case ACTIONS.presenceUnsubscribe:
449
449
  return this._unsubscribePresence(request.ch, request.seq, callback);
450
+ case ACTIONS.pingPong:
451
+ return this._pingPong(callback);
450
452
  default:
451
453
  callback(new ShareDBError(ERROR_CODE.ERR_MESSAGE_BADLY_FORMED, 'Invalid or unknown message'));
452
454
  }
@@ -540,6 +542,14 @@ Agent.prototype._querySubscribe = function(queryId, collection, query, options,
540
542
  });
541
543
  };
542
544
 
545
+ Agent.prototype._pingPong = function(callback) {
546
+ var error = null;
547
+ var message = {
548
+ a: ACTIONS.pingPong
549
+ };
550
+ callback(error, message);
551
+ };
552
+
543
553
  function getResultsData(results) {
544
554
  var items = [];
545
555
  for (var i = 0; i < results.length; i++) {
@@ -253,6 +253,8 @@ Connection.prototype.handleMessage = function(message) {
253
253
  return this._handlePresenceUnsubscribe(err, message);
254
254
  case ACTIONS.presenceRequest:
255
255
  return this._handlePresenceRequest(err, message);
256
+ case ACTIONS.pingPong:
257
+ return this._handlePingPong(err);
256
258
 
257
259
  default:
258
260
  logger.warn('Ignoring unrecognized message', message);
@@ -476,6 +478,19 @@ Connection.prototype.send = function(message) {
476
478
  this.socket.send(JSON.stringify(message));
477
479
  };
478
480
 
481
+ Connection.prototype.ping = function() {
482
+ if (!this.canSend) {
483
+ throw new ShareDBError(
484
+ ERROR_CODE.ERR_CANNOT_PING_OFFLINE,
485
+ 'Socket must be CONNECTED to ping'
486
+ );
487
+ }
488
+
489
+ var message = {
490
+ a: ACTIONS.pingPong
491
+ };
492
+ this.send(message);
493
+ };
479
494
 
480
495
  /**
481
496
  * Closes the socket and emits 'closed'
@@ -725,6 +740,11 @@ Connection.prototype._handleHandshake = function(error, message) {
725
740
  this._initialize(message);
726
741
  };
727
742
 
743
+ Connection.prototype._handlePingPong = function(error) {
744
+ if (error) return this.emit('error', error);
745
+ this.emit('pong');
746
+ };
747
+
728
748
  Connection.prototype._initialize = function(message) {
729
749
  if (this.state !== 'connecting') return;
730
750
 
package/lib/error.js CHANGED
@@ -16,6 +16,7 @@ ShareDBError.CODES = {
16
16
  ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT: 'ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT',
17
17
  ERR_APPLY_SNAPSHOT_NOT_PROVIDED: 'ERR_APPLY_SNAPSHOT_NOT_PROVIDED',
18
18
  ERR_CLIENT_ID_BADLY_FORMED: 'ERR_CLIENT_ID_BADLY_FORMED',
19
+ ERR_CANNOT_PING_OFFLINE: 'ERR_CANNOT_PING_OFFLINE',
19
20
  ERR_CONNECTION_SEQ_INTEGER_OVERFLOW: 'ERR_CONNECTION_SEQ_INTEGER_OVERFLOW',
20
21
  ERR_CONNECTION_STATE_TRANSITION_INVALID: 'ERR_CONNECTION_STATE_TRANSITION_INVALID',
21
22
  ERR_DATABASE_ADAPTER_NOT_FOUND: 'ERR_DATABASE_ADAPTER_NOT_FOUND',
@@ -14,6 +14,7 @@ exports.ACTIONS = {
14
14
  op: 'op',
15
15
  snapshotFetch: 'nf',
16
16
  snapshotFetchByTimestamp: 'nt',
17
+ pingPong: 'pp',
17
18
  presence: 'p',
18
19
  presenceSubscribe: 'ps',
19
20
  presenceUnsubscribe: 'pu',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sharedb",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "JSON OT database backend",
5
5
  "main": "lib/index.js",
6
6
  "dependencies": {
@@ -124,6 +124,45 @@ describe('client connection', function() {
124
124
  });
125
125
  });
126
126
 
127
+ describe('ping/pong', function() {
128
+ it('pings the backend', function(done) {
129
+ var connection = this.backend.connect();
130
+
131
+ connection.on('pong', function() {
132
+ done();
133
+ });
134
+
135
+ connection.on('connected', function() {
136
+ connection.ping();
137
+ });
138
+ });
139
+
140
+ it('handles errors', function(done) {
141
+ this.backend.use('receive', function(request, next) {
142
+ var error = request.data.a === 'pp' && new Error('bad');
143
+ next(error);
144
+ });
145
+
146
+ var connection = this.backend.connect();
147
+
148
+ connection.on('error', function(error) {
149
+ expect(error.message).to.equal('bad');
150
+ done();
151
+ });
152
+
153
+ connection.on('connected', function() {
154
+ connection.ping();
155
+ });
156
+ });
157
+
158
+ it('throws if pinging offline', function() {
159
+ var connection = this.backend.connect();
160
+ expect(function() {
161
+ connection.ping();
162
+ }).to.throw('Socket must be CONNECTED to ping');
163
+ });
164
+ });
165
+
127
166
  describe('backend.agentsCount', function() {
128
167
  it('updates after connect and connection.close()', function(done) {
129
168
  var backend = this.backend;