sharedb 4.1.3 → 4.1.5
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/backend.js +1 -1
- package/lib/next-tick.js +26 -0
- package/lib/op-stream.js +1 -2
- package/lib/stream-socket.js +1 -2
- package/lib/util.js +20 -14
- package/package.json +1 -1
- package/test/client/snapshot-version-request.js +34 -0
- package/test/next-tick.js +26 -0
- package/test/util-test.js +0 -50
package/lib/backend.js
CHANGED
|
@@ -909,7 +909,7 @@ Backend.prototype._fetchSnapshotByTimestamp = function(collection, id, timestamp
|
|
|
909
909
|
};
|
|
910
910
|
|
|
911
911
|
Backend.prototype._buildSnapshotFromOps = function(id, startingSnapshot, ops, callback) {
|
|
912
|
-
var snapshot = startingSnapshot || new Snapshot(id, 0, null, undefined, null);
|
|
912
|
+
var snapshot = util.clone(startingSnapshot) || new Snapshot(id, 0, null, undefined, null);
|
|
913
913
|
var error = ot.applyOps(snapshot, ops, {_normalizeLegacyJson0Ops: true});
|
|
914
914
|
callback(error, snapshot);
|
|
915
915
|
};
|
package/lib/next-tick.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
exports.messageChannel = function() {
|
|
2
|
+
var triggerCallback = createNextTickTrigger(arguments);
|
|
3
|
+
var channel = new MessageChannel();
|
|
4
|
+
channel.port1.onmessage = function() {
|
|
5
|
+
triggerCallback();
|
|
6
|
+
channel.port1.close();
|
|
7
|
+
};
|
|
8
|
+
channel.port2.postMessage('');
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
exports.setTimeout = function() {
|
|
12
|
+
var triggerCallback = createNextTickTrigger(arguments);
|
|
13
|
+
setTimeout(triggerCallback);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function createNextTickTrigger(args) {
|
|
17
|
+
var callback = args[0];
|
|
18
|
+
var _args = [];
|
|
19
|
+
for (var i = 1; i < args.length; i++) {
|
|
20
|
+
_args[i - 1] = args[i];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return function triggerCallback() {
|
|
24
|
+
callback.apply(null, _args);
|
|
25
|
+
};
|
|
26
|
+
}
|
package/lib/op-stream.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
var inherits = require('util').inherits;
|
|
2
1
|
var Readable = require('stream').Readable;
|
|
3
2
|
var util = require('./util');
|
|
4
3
|
|
|
@@ -10,7 +9,7 @@ function OpStream() {
|
|
|
10
9
|
}
|
|
11
10
|
module.exports = OpStream;
|
|
12
11
|
|
|
13
|
-
inherits(OpStream, Readable);
|
|
12
|
+
util.inherits(OpStream, Readable);
|
|
14
13
|
|
|
15
14
|
// This function is for notifying us that the stream is empty and needs data.
|
|
16
15
|
// For now, we'll just ignore the signal and assume the reader reads as fast
|
package/lib/stream-socket.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
var Duplex = require('stream').Duplex;
|
|
2
|
-
var inherits = require('util').inherits;
|
|
3
2
|
var logger = require('./logger');
|
|
4
3
|
var util = require('./util');
|
|
5
4
|
|
|
@@ -47,7 +46,7 @@ function ServerStream(socket) {
|
|
|
47
46
|
socket.close('stopped');
|
|
48
47
|
});
|
|
49
48
|
}
|
|
50
|
-
inherits(ServerStream, Duplex);
|
|
49
|
+
util.inherits(ServerStream, Duplex);
|
|
51
50
|
|
|
52
51
|
ServerStream.prototype.isServer = true;
|
|
53
52
|
|
package/lib/util.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
var nextTickImpl = require('./next-tick');
|
|
1
2
|
|
|
2
3
|
exports.doNothing = doNothing;
|
|
3
4
|
function doNothing() {}
|
|
@@ -85,20 +86,13 @@ exports.truthy = function(arg) {
|
|
|
85
86
|
return !!arg;
|
|
86
87
|
};
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
args[i - 1] = arguments[i];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
setTimeout(function() {
|
|
99
|
-
callback.apply(null, args);
|
|
100
|
-
});
|
|
101
|
-
};
|
|
89
|
+
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
|
|
90
|
+
exports.nextTick = process.nextTick;
|
|
91
|
+
} else if (typeof MessageChannel !== 'undefined') {
|
|
92
|
+
exports.nextTick = nextTickImpl.messageChannel;
|
|
93
|
+
} else {
|
|
94
|
+
exports.nextTick = nextTickImpl.setTimeout;
|
|
95
|
+
}
|
|
102
96
|
|
|
103
97
|
exports.clone = function(obj) {
|
|
104
98
|
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
|
|
@@ -113,3 +107,15 @@ Object.getOwnPropertyNames(Object.prototype).forEach(function(prop) {
|
|
|
113
107
|
exports.isDangerousProperty = function(propName) {
|
|
114
108
|
return propName === '__proto__' || objectProtoPropNames[propName];
|
|
115
109
|
};
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
var util = require('util');
|
|
113
|
+
if (typeof util.inherits !== 'function') throw new Error('Could not find util.inherits()');
|
|
114
|
+
exports.inherits = util.inherits;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
try {
|
|
117
|
+
exports.inherits = require('inherits');
|
|
118
|
+
} catch (e) {
|
|
119
|
+
throw new Error('If running sharedb in a browser, please install the "inherits" or "util" package');
|
|
120
|
+
}
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@ var sinon = require('sinon');
|
|
|
6
6
|
var async = require('async');
|
|
7
7
|
var json0v2 = require('ot-json0-v2').type;
|
|
8
8
|
var types = require('../../lib/types');
|
|
9
|
+
var clone = require('../../lib/util').clone;
|
|
9
10
|
|
|
10
11
|
describe('SnapshotVersionRequest', function() {
|
|
11
12
|
var backend;
|
|
@@ -440,6 +441,39 @@ describe('SnapshotVersionRequest', function() {
|
|
|
440
441
|
], done);
|
|
441
442
|
});
|
|
442
443
|
|
|
444
|
+
it('does not mutate the milestone snapshot', function(done) {
|
|
445
|
+
var connection = backendWithMilestones.connect();
|
|
446
|
+
var doc = connection.get('books', 'mocking-bird');
|
|
447
|
+
var milestoneDb = backendWithMilestones.milestoneDb;
|
|
448
|
+
var milestone;
|
|
449
|
+
|
|
450
|
+
async.waterfall([
|
|
451
|
+
doc.create.bind(doc, {title: 'To Kill a Mocking Bird'}),
|
|
452
|
+
doc.submitOp.bind(doc, {p: ['author'], oi: 'Harper Lea'}),
|
|
453
|
+
doc.submitOp.bind(doc, {p: ['author'], od: 'Harper Lea', oi: 'Harper Lee'}),
|
|
454
|
+
milestoneDb.getMilestoneSnapshot.bind(milestoneDb, 'books', 'mocking-bird', 3),
|
|
455
|
+
function(snapshot, next) {
|
|
456
|
+
milestone = clone(snapshot);
|
|
457
|
+
next();
|
|
458
|
+
},
|
|
459
|
+
function(next) {
|
|
460
|
+
// Internally, this calls ot.applyOps(), which mutates the snapshot it's passed.
|
|
461
|
+
// This call shouldn't cause the in-memory milestone snapshot to be mutated.
|
|
462
|
+
connection.fetchSnapshot('books', 'mocking-bird', 3, next);
|
|
463
|
+
},
|
|
464
|
+
function(snapshot, next) {
|
|
465
|
+
expect(snapshot.v).to.equal(3);
|
|
466
|
+
expect(snapshot.data).to.eql({title: 'To Kill a Mocking Bird', author: 'Harper Lee'});
|
|
467
|
+
next();
|
|
468
|
+
},
|
|
469
|
+
milestoneDb.getMilestoneSnapshot.bind(milestoneDb, 'books', 'mocking-bird', 3),
|
|
470
|
+
function(snapshot, next) {
|
|
471
|
+
expect(snapshot).to.eql(milestone);
|
|
472
|
+
next();
|
|
473
|
+
}
|
|
474
|
+
], done);
|
|
475
|
+
});
|
|
476
|
+
|
|
443
477
|
describe('with version null', function() {
|
|
444
478
|
it('fetches a latest version', function(done) {
|
|
445
479
|
var doc = backendWithMilestones.connect().get('books', 'mocking-bird');
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var nextTickImpl = require('../lib/next-tick');
|
|
2
|
+
var expect = require('chai').expect;
|
|
3
|
+
|
|
4
|
+
describe('nextTick', function() {
|
|
5
|
+
['messageChannel', 'setTimeout'].forEach(function(name) {
|
|
6
|
+
var tick = nextTickImpl[name];
|
|
7
|
+
|
|
8
|
+
it('passes args', function(done) {
|
|
9
|
+
tick(function(arg1, arg2, arg3) {
|
|
10
|
+
expect(arg1).to.equal('foo');
|
|
11
|
+
expect(arg2).to.equal(123);
|
|
12
|
+
expect(arg3).to.be.undefined;
|
|
13
|
+
done();
|
|
14
|
+
}, 'foo', 123);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('calls asynchronously', function(done) {
|
|
18
|
+
var called = false;
|
|
19
|
+
tick(function() {
|
|
20
|
+
called = true;
|
|
21
|
+
done();
|
|
22
|
+
});
|
|
23
|
+
expect(called).to.be.false;
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
package/test/util-test.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
var util = require('../lib/util');
|
|
2
|
-
var expect = require('chai').expect;
|
|
3
|
-
|
|
4
|
-
describe('util', function() {
|
|
5
|
-
describe('nextTick', function() {
|
|
6
|
-
it('uses process.nextTick if present', function(done) {
|
|
7
|
-
expect(process.nextTick).to.be.ok;
|
|
8
|
-
|
|
9
|
-
util.nextTick(function(arg1, arg2, arg3) {
|
|
10
|
-
expect(arg1).to.equal('foo');
|
|
11
|
-
expect(arg2).to.equal(123);
|
|
12
|
-
expect(arg3).to.be.undefined;
|
|
13
|
-
done();
|
|
14
|
-
}, 'foo', 123);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
describe('without nextTick', function() {
|
|
18
|
-
var nextTick;
|
|
19
|
-
|
|
20
|
-
before(function() {
|
|
21
|
-
nextTick = process.nextTick;
|
|
22
|
-
delete process.nextTick;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
after(function() {
|
|
26
|
-
process.nextTick = nextTick;
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('uses a ponyfill if process.nextTick is not present', function(done) {
|
|
30
|
-
expect(process.nextTick).to.be.undefined;
|
|
31
|
-
|
|
32
|
-
util.nextTick(function(arg1, arg2, arg3) {
|
|
33
|
-
expect(arg1).to.equal('foo');
|
|
34
|
-
expect(arg2).to.equal(123);
|
|
35
|
-
expect(arg3).to.be.undefined;
|
|
36
|
-
done();
|
|
37
|
-
}, 'foo', 123);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('calls asynchronously', function(done) {
|
|
41
|
-
var called = false;
|
|
42
|
-
util.nextTick(function() {
|
|
43
|
-
called = true;
|
|
44
|
-
done();
|
|
45
|
-
});
|
|
46
|
-
expect(called).to.be.false;
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|