mongodb-livedata-server 0.0.1
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/README.md +63 -0
- package/dist/livedata_server.js +9 -0
- package/dist/meteor/binary-heap/max_heap.js +186 -0
- package/dist/meteor/binary-heap/min_heap.js +17 -0
- package/dist/meteor/binary-heap/min_max_heap.js +48 -0
- package/dist/meteor/callback-hook/hook.js +78 -0
- package/dist/meteor/ddp/crossbar.js +136 -0
- package/dist/meteor/ddp/heartbeat.js +77 -0
- package/dist/meteor/ddp/livedata_server.js +403 -0
- package/dist/meteor/ddp/method-invocation.js +72 -0
- package/dist/meteor/ddp/random-stream.js +100 -0
- package/dist/meteor/ddp/session-collection-view.js +106 -0
- package/dist/meteor/ddp/session-document-view.js +82 -0
- package/dist/meteor/ddp/session.js +570 -0
- package/dist/meteor/ddp/stream_server.js +181 -0
- package/dist/meteor/ddp/subscription.js +347 -0
- package/dist/meteor/ddp/utils.js +104 -0
- package/dist/meteor/ddp/writefence.js +111 -0
- package/dist/meteor/diff-sequence/diff.js +257 -0
- package/dist/meteor/ejson/ejson.js +569 -0
- package/dist/meteor/ejson/stringify.js +119 -0
- package/dist/meteor/ejson/utils.js +42 -0
- package/dist/meteor/id-map/id_map.js +92 -0
- package/dist/meteor/mongo/caching_change_observer.js +94 -0
- package/dist/meteor/mongo/doc_fetcher.js +53 -0
- package/dist/meteor/mongo/geojson_utils.js +41 -0
- package/dist/meteor/mongo/live_connection.js +264 -0
- package/dist/meteor/mongo/live_cursor.js +57 -0
- package/dist/meteor/mongo/minimongo_common.js +2002 -0
- package/dist/meteor/mongo/minimongo_matcher.js +217 -0
- package/dist/meteor/mongo/minimongo_sorter.js +268 -0
- package/dist/meteor/mongo/observe_driver_utils.js +73 -0
- package/dist/meteor/mongo/observe_multiplexer.js +228 -0
- package/dist/meteor/mongo/oplog-observe-driver.js +919 -0
- package/dist/meteor/mongo/oplog_tailing.js +352 -0
- package/dist/meteor/mongo/oplog_v2_converter.js +126 -0
- package/dist/meteor/mongo/polling_observe_driver.js +195 -0
- package/dist/meteor/mongo/synchronous-cursor.js +261 -0
- package/dist/meteor/mongo/synchronous-queue.js +110 -0
- package/dist/meteor/ordered-dict/ordered_dict.js +198 -0
- package/dist/meteor/random/AbstractRandomGenerator.js +92 -0
- package/dist/meteor/random/AleaRandomGenerator.js +90 -0
- package/dist/meteor/random/NodeRandomGenerator.js +42 -0
- package/dist/meteor/random/createAleaGenerator.js +32 -0
- package/dist/meteor/random/createRandom.js +22 -0
- package/dist/meteor/random/main.js +12 -0
- package/livedata_server.ts +3 -0
- package/meteor/LICENSE +28 -0
- package/meteor/binary-heap/max_heap.ts +225 -0
- package/meteor/binary-heap/min_heap.ts +15 -0
- package/meteor/binary-heap/min_max_heap.ts +53 -0
- package/meteor/callback-hook/hook.ts +85 -0
- package/meteor/ddp/crossbar.ts +148 -0
- package/meteor/ddp/heartbeat.ts +97 -0
- package/meteor/ddp/livedata_server.ts +473 -0
- package/meteor/ddp/method-invocation.ts +86 -0
- package/meteor/ddp/random-stream.ts +102 -0
- package/meteor/ddp/session-collection-view.ts +119 -0
- package/meteor/ddp/session-document-view.ts +92 -0
- package/meteor/ddp/session.ts +708 -0
- package/meteor/ddp/stream_server.ts +204 -0
- package/meteor/ddp/subscription.ts +392 -0
- package/meteor/ddp/utils.ts +119 -0
- package/meteor/ddp/writefence.ts +130 -0
- package/meteor/diff-sequence/diff.ts +295 -0
- package/meteor/ejson/ejson.ts +601 -0
- package/meteor/ejson/stringify.ts +122 -0
- package/meteor/ejson/utils.ts +38 -0
- package/meteor/id-map/id_map.ts +84 -0
- package/meteor/mongo/caching_change_observer.ts +120 -0
- package/meteor/mongo/doc_fetcher.ts +52 -0
- package/meteor/mongo/geojson_utils.ts +42 -0
- package/meteor/mongo/live_connection.ts +302 -0
- package/meteor/mongo/live_cursor.ts +79 -0
- package/meteor/mongo/minimongo_common.ts +2440 -0
- package/meteor/mongo/minimongo_matcher.ts +275 -0
- package/meteor/mongo/minimongo_sorter.ts +331 -0
- package/meteor/mongo/observe_driver_utils.ts +79 -0
- package/meteor/mongo/observe_multiplexer.ts +256 -0
- package/meteor/mongo/oplog-observe-driver.ts +1049 -0
- package/meteor/mongo/oplog_tailing.ts +414 -0
- package/meteor/mongo/oplog_v2_converter.ts +124 -0
- package/meteor/mongo/polling_observe_driver.ts +247 -0
- package/meteor/mongo/synchronous-cursor.ts +293 -0
- package/meteor/mongo/synchronous-queue.ts +119 -0
- package/meteor/ordered-dict/ordered_dict.ts +229 -0
- package/meteor/random/AbstractRandomGenerator.ts +99 -0
- package/meteor/random/AleaRandomGenerator.ts +96 -0
- package/meteor/random/NodeRandomGenerator.ts +37 -0
- package/meteor/random/createAleaGenerator.ts +31 -0
- package/meteor/random/createRandom.ts +19 -0
- package/meteor/random/main.ts +8 -0
- package/package.json +30 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.ObserveHandle = exports.ObserveMultiplexer = void 0;
|
|
15
|
+
const ejson_1 = require("../ejson/ejson");
|
|
16
|
+
const caching_change_observer_1 = require("./caching_change_observer");
|
|
17
|
+
const synchronous_queue_1 = require("./synchronous-queue");
|
|
18
|
+
class ObserveMultiplexer {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this._readyFuture = { isResolved: false };
|
|
21
|
+
var self = this;
|
|
22
|
+
if (!options || !options.hasOwnProperty('ordered'))
|
|
23
|
+
throw Error("must specify ordered");
|
|
24
|
+
self._ordered = options.ordered;
|
|
25
|
+
self._onStop = options.onStop || function () { };
|
|
26
|
+
self._queue = new synchronous_queue_1._SynchronousQueue();
|
|
27
|
+
self._handles = {};
|
|
28
|
+
self._readyFuture.promise = new Promise((resolve, reject) => {
|
|
29
|
+
self._readyFuture.resolve = resolve;
|
|
30
|
+
self._readyFuture.reject = reject;
|
|
31
|
+
});
|
|
32
|
+
self._cache = new caching_change_observer_1._CachingChangeObserver({ ordered: options.ordered });
|
|
33
|
+
// Number of addHandleAndSendInitialAdds tasks scheduled but not yet
|
|
34
|
+
// running. removeHandle uses this to know if it's time to call the onStop
|
|
35
|
+
// callback.
|
|
36
|
+
self._addHandleTasksScheduledButNotPerformed = 0;
|
|
37
|
+
for (const callbackName of self.callbackNames()) {
|
|
38
|
+
self[callbackName] = function ( /* ... */) {
|
|
39
|
+
self._applyCallback(callbackName, Array.from(arguments));
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async addHandleAndSendInitialAdds(handle) {
|
|
44
|
+
var self = this;
|
|
45
|
+
// Check this before calling runTask (even though runTask does the same
|
|
46
|
+
// check) so that we don't leak an ObserveMultiplexer on error by
|
|
47
|
+
// incrementing _addHandleTasksScheduledButNotPerformed and never
|
|
48
|
+
// decrementing it.
|
|
49
|
+
//if (!self._queue.safeToRunTask())
|
|
50
|
+
// throw new Error("Can't call observeChanges from an observe callback on the same query");
|
|
51
|
+
++self._addHandleTasksScheduledButNotPerformed;
|
|
52
|
+
await self._queue.runTask(async () => {
|
|
53
|
+
self._handles[handle._id] = handle;
|
|
54
|
+
// Send out whatever adds we have so far (whether or not we the
|
|
55
|
+
// multiplexer is ready).
|
|
56
|
+
self._sendAdds(handle);
|
|
57
|
+
--self._addHandleTasksScheduledButNotPerformed;
|
|
58
|
+
});
|
|
59
|
+
// *outside* the task, since otherwise we'd deadlock
|
|
60
|
+
await self._readyFuture.promise;
|
|
61
|
+
}
|
|
62
|
+
// Remove an observe handle. If it was the last observe handle, call the
|
|
63
|
+
// onStop callback; you cannot add any more observe handles after this.
|
|
64
|
+
//
|
|
65
|
+
// This is not synchronized with polls and handle additions: this means that
|
|
66
|
+
// you can safely call it from within an observe callback, but it also means
|
|
67
|
+
// that we have to be careful when we iterate over _handles.
|
|
68
|
+
removeHandle(id) {
|
|
69
|
+
var self = this;
|
|
70
|
+
// This should not be possible: you can only call removeHandle by having
|
|
71
|
+
// access to the ObserveHandle, which isn't returned to user code until the
|
|
72
|
+
// multiplex is ready.
|
|
73
|
+
if (!self._ready())
|
|
74
|
+
throw new Error("Can't remove handles until the multiplex is ready");
|
|
75
|
+
delete self._handles[id];
|
|
76
|
+
if (Object.keys(self._handles).length === 0 && self._addHandleTasksScheduledButNotPerformed === 0) {
|
|
77
|
+
self._stop();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
_stop(options) {
|
|
81
|
+
var self = this;
|
|
82
|
+
options = options || {};
|
|
83
|
+
// It shouldn't be possible for us to stop when all our handles still
|
|
84
|
+
// haven't been returned from observeChanges!
|
|
85
|
+
if (!self._ready() && !options.fromQueryError)
|
|
86
|
+
throw Error("surprising _stop: not ready");
|
|
87
|
+
// Call stop callback (which kills the underlying process which sends us
|
|
88
|
+
// callbacks and removes us from the connection's dictionary).
|
|
89
|
+
self._onStop();
|
|
90
|
+
// Cause future addHandleAndSendInitialAdds calls to throw (but the onStop
|
|
91
|
+
// callback should make our connection forget about us).
|
|
92
|
+
self._handles = null;
|
|
93
|
+
}
|
|
94
|
+
// Allows all addHandleAndSendInitialAdds calls to return, once all preceding
|
|
95
|
+
// adds have been processed. Does not block.
|
|
96
|
+
ready() {
|
|
97
|
+
var self = this;
|
|
98
|
+
self._queue.queueTask(async () => {
|
|
99
|
+
if (self._ready())
|
|
100
|
+
throw Error("can't make ObserveMultiplex ready twice!");
|
|
101
|
+
self._readyFuture.resolve();
|
|
102
|
+
self._readyFuture.isResolved = true;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
// If trying to execute the query results in an error, call this. This is
|
|
106
|
+
// intended for permanent errors, not transient network errors that could be
|
|
107
|
+
// fixed. It should only be called before ready(), because if you called ready
|
|
108
|
+
// that meant that you managed to run the query once. It will stop this
|
|
109
|
+
// ObserveMultiplex and cause addHandleAndSendInitialAdds calls (and thus
|
|
110
|
+
// observeChanges calls) to throw the error.
|
|
111
|
+
queryError(err) {
|
|
112
|
+
var self = this;
|
|
113
|
+
self._queue.runTask(async () => {
|
|
114
|
+
if (self._ready())
|
|
115
|
+
throw Error("can't claim query has an error after it worked!");
|
|
116
|
+
self._stop({ fromQueryError: true });
|
|
117
|
+
self._readyFuture.reject(err);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// Calls "cb" once the effects of all "ready", "addHandleAndSendInitialAdds"
|
|
121
|
+
// and observe callbacks which came before this call have been propagated to
|
|
122
|
+
// all handles. "ready" must have already been called on this multiplexer.
|
|
123
|
+
onFlush(cb) {
|
|
124
|
+
var self = this;
|
|
125
|
+
self._queue.queueTask(async () => {
|
|
126
|
+
if (!self._ready())
|
|
127
|
+
throw Error("only call onFlush on a multiplexer that will be ready");
|
|
128
|
+
cb();
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
callbackNames() {
|
|
132
|
+
var self = this;
|
|
133
|
+
if (self._ordered)
|
|
134
|
+
return ["addedBefore", "changed", "movedBefore", "removed"];
|
|
135
|
+
else
|
|
136
|
+
return ["added", "changed", "removed"];
|
|
137
|
+
}
|
|
138
|
+
_ready() {
|
|
139
|
+
return this._readyFuture.isResolved;
|
|
140
|
+
}
|
|
141
|
+
_applyCallback(callbackName, args) {
|
|
142
|
+
var self = this;
|
|
143
|
+
self._queue.queueTask(async () => {
|
|
144
|
+
// If we stopped in the meantime, do nothing.
|
|
145
|
+
if (!self._handles)
|
|
146
|
+
return;
|
|
147
|
+
// First, apply the change to the cache.
|
|
148
|
+
self._cache.applyChange[callbackName].apply(null, args);
|
|
149
|
+
// If we haven't finished the initial adds, then we should only be getting
|
|
150
|
+
// adds.
|
|
151
|
+
if (!self._ready() &&
|
|
152
|
+
(callbackName !== 'added' && callbackName !== 'addedBefore')) {
|
|
153
|
+
throw new Error("Got " + callbackName + " during initial adds");
|
|
154
|
+
}
|
|
155
|
+
// Now multiplex the callbacks out to all observe handles. It's OK if
|
|
156
|
+
// these calls yield; since we're inside a task, no other use of our queue
|
|
157
|
+
// can continue until these are done. (But we do have to be careful to not
|
|
158
|
+
// use a handle that got removed, because removeHandle does not use the
|
|
159
|
+
// queue; thus, we iterate over an array of keys that we control.)
|
|
160
|
+
for (const handleId of Object.keys(self._handles)) {
|
|
161
|
+
var handle = self._handles && self._handles[handleId];
|
|
162
|
+
if (!handle)
|
|
163
|
+
return;
|
|
164
|
+
var callback = handle['_' + callbackName];
|
|
165
|
+
// clone arguments so that callbacks can mutate their arguments
|
|
166
|
+
callback && callback.apply(null, handle.nonMutatingCallbacks ? args : (0, ejson_1.clone)(args));
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
// Sends initial adds to a handle. It should only be called from within a task
|
|
171
|
+
// (the task that is processing the addHandleAndSendInitialAdds call). It
|
|
172
|
+
// synchronously invokes the handle's added or addedBefore; there's no need to
|
|
173
|
+
// flush the queue afterwards to ensure that the callbacks get out.
|
|
174
|
+
_sendAdds(handle) {
|
|
175
|
+
var self = this;
|
|
176
|
+
//if (self._queue.safeToRunTask())
|
|
177
|
+
// throw Error("_sendAdds may only be called from within a task!");
|
|
178
|
+
var add = self._ordered ? handle._addedBefore : handle._added;
|
|
179
|
+
if (!add)
|
|
180
|
+
return;
|
|
181
|
+
// note: docs may be an _IdMap or an OrderedDict
|
|
182
|
+
self._cache.docs.forEach(function (doc, id) {
|
|
183
|
+
if (!self._handles.hasOwnProperty(handle._id))
|
|
184
|
+
throw Error("handle got removed before sending initial adds!");
|
|
185
|
+
const _a = handle.nonMutatingCallbacks ? doc : (0, ejson_1.clone)(doc), { _id } = _a, fields = __rest(_a, ["_id"]);
|
|
186
|
+
if (self._ordered)
|
|
187
|
+
add(id, fields, null); // we're going in order, so add at end
|
|
188
|
+
else
|
|
189
|
+
add(id, fields);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.ObserveMultiplexer = ObserveMultiplexer;
|
|
194
|
+
let nextObserveHandleId = 1;
|
|
195
|
+
// When the callbacks do not mutate the arguments, we can skip a lot of data clones
|
|
196
|
+
class ObserveHandle {
|
|
197
|
+
constructor(_multiplexer, callbacks, nonMutatingCallbacks = false) {
|
|
198
|
+
this._multiplexer = _multiplexer;
|
|
199
|
+
this.nonMutatingCallbacks = nonMutatingCallbacks;
|
|
200
|
+
var self = this;
|
|
201
|
+
// The end user is only supposed to call stop(). The other fields are
|
|
202
|
+
// accessible to the multiplexer, though.
|
|
203
|
+
for (const name of _multiplexer.callbackNames()) {
|
|
204
|
+
if (callbacks[name]) {
|
|
205
|
+
self['_' + name] = callbacks[name];
|
|
206
|
+
}
|
|
207
|
+
else if (name === "addedBefore" && callbacks.added) {
|
|
208
|
+
// Special case: if you specify "added" and "movedBefore", you get an
|
|
209
|
+
// ordered observe where for some reason you don't get ordering data on
|
|
210
|
+
// the adds. I dunno, we wrote tests for it, there must have been a
|
|
211
|
+
// reason.
|
|
212
|
+
self._addedBefore = function (id, fields, before) {
|
|
213
|
+
callbacks.added(id, fields);
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
self._stopped = false;
|
|
218
|
+
self._id = nextObserveHandleId++;
|
|
219
|
+
}
|
|
220
|
+
stop() {
|
|
221
|
+
var self = this;
|
|
222
|
+
if (self._stopped)
|
|
223
|
+
return;
|
|
224
|
+
self._stopped = true;
|
|
225
|
+
self._multiplexer.removeHandle(self._id);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.ObserveHandle = ObserveHandle;
|