Skip to content

Commit f3ebed3

Browse files
author
Alec Gibson
committed
Add Agent.src field
This change adds an `Agent.src` field, which will accept a client-provided ID. This is kept separate from the Agent-generated `clientId` so that the agent's ID does not change during the handshake (which could confuse any consumers relying on a stable client ID in the `connect` middleware). Any internal references to `clientId` then give precedence to the consumer-provided `src`. If it hasn't been set (ie we're dealing with a legacy client), then we fall back to using `clientId`.
1 parent 6107c7d commit f3ebed3

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/agent.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ function Agent(backend, stream) {
2020
this.stream = stream;
2121

2222
this.clientId = hat();
23+
// src is a client-configurable "id" which the client will set in its handshake,
24+
// and attach to its ops. This should take precedence over clientId if set.
25+
// Only legacy clients, or new clients connecting for the first time will use the
26+
// Agent-provided clientId. Ideally we'll deprecate clientId in favour of src
27+
// in the next breaking change.
28+
this.src = null;
2329
this.connectTime = Date.now();
2430

2531
// We need to track which documents are subscribed by the client. This is a
@@ -46,7 +52,7 @@ module.exports = Agent;
4652
// Close the agent with the client.
4753
Agent.prototype.close = function(err) {
4854
if (err) {
49-
logger.warn('Agent closed due to error', this.clientId, err.stack || err);
55+
logger.warn('Agent closed due to error', this._src(), err.stack || err);
5056
}
5157
if (this.closed) return;
5258
// This will end the writable stream and emit 'finish'
@@ -185,7 +191,7 @@ Agent.prototype._isOwnOp = function(collection, op) {
185191
// Detect ops from this client on the same projection. Since the client sent
186192
// these in, the submit reply will be sufficient and we can silently ignore
187193
// them in the streams for subscribed documents or queries
188-
return (this.clientId === op.src) && (collection === (op.i || op.c));
194+
return (this._src() === op.src) && (collection === (op.i || op.c));
189195
};
190196

191197
Agent.prototype.send = function(message) {
@@ -328,7 +334,7 @@ Agent.prototype._handleMessage = function(request, callback) {
328334

329335
switch (request.a) {
330336
case 'hs':
331-
if (request.id) this.clientId = request.id;
337+
if (request.id) this.src = request.id;
332338
return callback(null, this._initMessage('hs'));
333339
case 'qf':
334340
return this._queryFetch(request.id, request.c, request.q, getQueryOptions(request), callback);
@@ -350,7 +356,7 @@ Agent.prototype._handleMessage = function(request, callback) {
350356
return this._unsubscribe(request.c, request.d, callback);
351357
case 'op':
352358
// Normalize the properties submitted
353-
var op = createClientOp(request, this.clientId);
359+
var op = createClientOp(request, this._src());
354360
if (op.seq >= util.MAX_SAFE_INTEGER) {
355361
return callback(new ShareDBError(
356362
ERROR_CODE.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW,
@@ -654,11 +660,15 @@ Agent.prototype._initMessage = function(action) {
654660
a: action,
655661
protocol: 1,
656662
protocolMinor: 1,
657-
id: this.clientId,
663+
id: this._src(),
658664
type: types.defaultType.uri
659665
};
660666
};
661667

668+
Agent.prototype._src = function() {
669+
return this.src || this.clientId;
670+
};
671+
662672

663673
function createClientOp(request, clientId) {
664674
// src can be provided if it is not the same as the current agent,

lib/client/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function Connection(socket) {
5252
// A unique message number for the given id
5353
this.seq = 1;
5454

55-
// Equals agent.clientId on the server
55+
// Equals agent.src on the server
5656
this.id = null;
5757

5858
// This direct reference from connection to agent is not used internal to

0 commit comments

Comments
 (0)