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,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StreamServer = void 0;
|
|
7
|
+
const permessage_deflate_1 = __importDefault(require("permessage-deflate"));
|
|
8
|
+
const sockjs_1 = __importDefault(require("sockjs"));
|
|
9
|
+
const url_1 = __importDefault(require("url"));
|
|
10
|
+
// By default, we use the permessage-deflate extension with default
|
|
11
|
+
// configuration.
|
|
12
|
+
class StreamServer {
|
|
13
|
+
constructor(httpServer) {
|
|
14
|
+
this.httpServer = httpServer;
|
|
15
|
+
this.registration_callbacks = [];
|
|
16
|
+
this.open_sockets = [];
|
|
17
|
+
// Because we are installing directly onto WebApp.httpServer instead of using
|
|
18
|
+
// WebApp.app, we have to process the path prefix ourselves.
|
|
19
|
+
this.prefix = '/sockjs';
|
|
20
|
+
//RoutePolicy.declare(this.prefix + '/', 'network');
|
|
21
|
+
// set up sockjs
|
|
22
|
+
const serverOptions = {
|
|
23
|
+
prefix: this.prefix,
|
|
24
|
+
log: function () { },
|
|
25
|
+
// this is the default, but we code it explicitly because we depend
|
|
26
|
+
// on it in stream_client:HEARTBEAT_TIMEOUT
|
|
27
|
+
heartbeat_delay: 45000,
|
|
28
|
+
// The default disconnect_delay is 5 seconds, but if the server ends up CPU
|
|
29
|
+
// bound for that much time, SockJS might not notice that the user has
|
|
30
|
+
// reconnected because the timer (of disconnect_delay ms) can fire before
|
|
31
|
+
// SockJS processes the new connection. Eventually we'll fix this by not
|
|
32
|
+
// combining CPU-heavy processing with SockJS termination (eg a proxy which
|
|
33
|
+
// converts to Unix sockets) but for now, raise the delay.
|
|
34
|
+
disconnect_delay: 60 * 1000,
|
|
35
|
+
// Set the USE_JSESSIONID environment variable to enable setting the
|
|
36
|
+
// JSESSIONID cookie. This is useful for setting up proxies with
|
|
37
|
+
// session affinity.
|
|
38
|
+
jsessionid: !!process.env.USE_JSESSIONID,
|
|
39
|
+
websocket: true,
|
|
40
|
+
faye_server_options: null
|
|
41
|
+
};
|
|
42
|
+
// If you know your server environment (eg, proxies) will prevent websockets
|
|
43
|
+
// from ever working, set $DISABLE_WEBSOCKETS and SockJS clients (ie,
|
|
44
|
+
// browsers) will not waste time attempting to use them.
|
|
45
|
+
// (Your server will still have a /websocket endpoint.)
|
|
46
|
+
if (process.env.DISABLE_WEBSOCKETS) {
|
|
47
|
+
serverOptions.websocket = false;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
serverOptions.faye_server_options = {
|
|
51
|
+
extensions: [permessage_deflate_1.default.configure({})]
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
this.server = sockjs_1.default.createServer(serverOptions);
|
|
55
|
+
// Install the sockjs handlers, but we want to keep around our own particular
|
|
56
|
+
// request handler that adjusts idle timeouts while we have an outstanding
|
|
57
|
+
// request. This compensates for the fact that sockjs removes all listeners
|
|
58
|
+
// for "request" to add its own.
|
|
59
|
+
httpServer.removeListener('request', _timeoutAdjustmentRequestCallback);
|
|
60
|
+
this.server.installHandlers(httpServer);
|
|
61
|
+
httpServer.addListener('request', _timeoutAdjustmentRequestCallback);
|
|
62
|
+
// Support the /websocket endpoint
|
|
63
|
+
this._redirectWebsocketEndpoint();
|
|
64
|
+
this.server.on('connection', (socket) => {
|
|
65
|
+
// sockjs sometimes passes us null instead of a socket object
|
|
66
|
+
// so we need to guard against that. see:
|
|
67
|
+
// https://github.com/sockjs/sockjs-node/issues/121
|
|
68
|
+
// https://github.com/meteor/meteor/issues/10468
|
|
69
|
+
if (!socket)
|
|
70
|
+
return;
|
|
71
|
+
// We want to make sure that if a client connects to us and does the initial
|
|
72
|
+
// Websocket handshake but never gets to the DDP handshake, that we
|
|
73
|
+
// eventually kill the socket. Once the DDP handshake happens, DDP
|
|
74
|
+
// heartbeating will work. And before the Websocket handshake, the timeouts
|
|
75
|
+
// we set at the server level in webapp_server.js will work. But
|
|
76
|
+
// faye-websocket calls setTimeout(0) on any socket it takes over, so there
|
|
77
|
+
// is an "in between" state where this doesn't happen. We work around this
|
|
78
|
+
// by explicitly setting the socket timeout to a relatively large time here,
|
|
79
|
+
// and setting it back to zero when we set up the heartbeat in
|
|
80
|
+
// livedata_server.js.
|
|
81
|
+
socket.setWebsocketTimeout = function (timeout) {
|
|
82
|
+
if ((socket.protocol === 'websocket' ||
|
|
83
|
+
socket.protocol === 'websocket-raw')
|
|
84
|
+
&& socket._session.recv) {
|
|
85
|
+
socket._session.recv.connection.setTimeout(timeout);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
socket.setWebsocketTimeout(45 * 1000);
|
|
89
|
+
socket.send = (data) => {
|
|
90
|
+
socket.write(data);
|
|
91
|
+
};
|
|
92
|
+
socket.on('close', () => {
|
|
93
|
+
this.open_sockets = this.open_sockets.filter(s => s !== socket);
|
|
94
|
+
});
|
|
95
|
+
this.open_sockets.push(socket);
|
|
96
|
+
// only to send a message after connection on tests, useful for
|
|
97
|
+
// socket-stream-client/server-tests.js
|
|
98
|
+
if (process.env.TEST_METADATA && process.env.TEST_METADATA !== "{}") {
|
|
99
|
+
socket.send(JSON.stringify({ testMessageOnConnect: true }));
|
|
100
|
+
}
|
|
101
|
+
// call all our callbacks when we get a new socket. they will do the
|
|
102
|
+
// work of setting up handlers and such for specific messages.
|
|
103
|
+
for (const callback of this.registration_callbacks) {
|
|
104
|
+
callback(socket);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
;
|
|
109
|
+
// call my callback when a new socket connects.
|
|
110
|
+
// also call it for all current connections.
|
|
111
|
+
register(callback) {
|
|
112
|
+
var self = this;
|
|
113
|
+
self.registration_callbacks.push(callback);
|
|
114
|
+
for (const socket of self.all_sockets()) {
|
|
115
|
+
callback(socket);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// get a list of all sockets
|
|
119
|
+
all_sockets() {
|
|
120
|
+
return Object.values(this.open_sockets);
|
|
121
|
+
}
|
|
122
|
+
// Redirect /websocket to /sockjs/websocket in order to not expose
|
|
123
|
+
// sockjs to clients that want to use raw websockets
|
|
124
|
+
_redirectWebsocketEndpoint() {
|
|
125
|
+
var self = this;
|
|
126
|
+
// Unfortunately we can't use a connect middleware here since
|
|
127
|
+
// sockjs installs itself prior to all existing listeners
|
|
128
|
+
// (meaning prior to any connect middlewares) so we need to take
|
|
129
|
+
// an approach similar to overshadowListeners in
|
|
130
|
+
// https://github.com/sockjs/sockjs-node/blob/cf820c55af6a9953e16558555a31decea554f70e/src/utils.coffee
|
|
131
|
+
['request', 'upgrade'].forEach((event) => {
|
|
132
|
+
var oldHttpServerListeners = this.httpServer.listeners(event).slice(0);
|
|
133
|
+
this.httpServer.removeAllListeners(event);
|
|
134
|
+
// request and upgrade have different arguments passed but
|
|
135
|
+
// we only care about the first one which is always request
|
|
136
|
+
var newListener = function (request /*, moreArguments */) {
|
|
137
|
+
// Store arguments for use within the closure below
|
|
138
|
+
var args = arguments;
|
|
139
|
+
// Rewrite /websocket and /websocket/ urls to /sockjs/websocket while
|
|
140
|
+
// preserving query string.
|
|
141
|
+
var parsedUrl = url_1.default.parse(request.url);
|
|
142
|
+
if (parsedUrl.pathname === '/websocket' ||
|
|
143
|
+
parsedUrl.pathname === '/websocket/') {
|
|
144
|
+
parsedUrl.pathname = self.prefix + '/websocket';
|
|
145
|
+
request.url = url_1.default.format(parsedUrl);
|
|
146
|
+
}
|
|
147
|
+
for (const oldListener of oldHttpServerListeners) {
|
|
148
|
+
oldListener.apply(this.httpServer, args);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
this.httpServer.addListener(event, newListener);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.StreamServer = StreamServer;
|
|
156
|
+
const SHORT_SOCKET_TIMEOUT = 5 * 1000;
|
|
157
|
+
const LONG_SOCKET_TIMEOUT = 120 * 1000;
|
|
158
|
+
// When we have a request pending, we want the socket timeout to be long, to
|
|
159
|
+
// give ourselves a while to serve it, and to allow sockjs long polls to
|
|
160
|
+
// complete. On the other hand, we want to close idle sockets relatively
|
|
161
|
+
// quickly, so that we can shut down relatively promptly but cleanly, without
|
|
162
|
+
// cutting off anyone's response.
|
|
163
|
+
function _timeoutAdjustmentRequestCallback(req, res) {
|
|
164
|
+
// this is really just req.socket.setTimeout(LONG_SOCKET_TIMEOUT);
|
|
165
|
+
req.setTimeout(LONG_SOCKET_TIMEOUT);
|
|
166
|
+
// Insert our new finish listener to run BEFORE the existing one which removes
|
|
167
|
+
// the response from the socket.
|
|
168
|
+
var finishListeners = res.listeners('finish');
|
|
169
|
+
// XXX Apparently in Node 0.12 this event was called 'prefinish'.
|
|
170
|
+
// https://github.com/joyent/node/commit/7c9b6070
|
|
171
|
+
// But it has switched back to 'finish' in Node v4:
|
|
172
|
+
// https://github.com/nodejs/node/pull/1411
|
|
173
|
+
res.removeAllListeners('finish');
|
|
174
|
+
res.on('finish', function () {
|
|
175
|
+
res.setTimeout(SHORT_SOCKET_TIMEOUT);
|
|
176
|
+
});
|
|
177
|
+
for (const l of finishListeners) {
|
|
178
|
+
res.on('finish', l);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ctor for a sub handle: the input to each publish function
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Subscription = void 0;
|
|
5
|
+
const ejson_1 = require("../ejson/ejson");
|
|
6
|
+
const main_1 = require("../random/main");
|
|
7
|
+
const livedata_server_1 = require("./livedata_server");
|
|
8
|
+
// Instance name is this because it's usually referred to as this inside a
|
|
9
|
+
// publish
|
|
10
|
+
/**
|
|
11
|
+
* @summary The server's side of a subscription
|
|
12
|
+
* @class Subscription
|
|
13
|
+
* @instanceName this
|
|
14
|
+
* @showInstanceName true
|
|
15
|
+
*/
|
|
16
|
+
class Subscription {
|
|
17
|
+
constructor(_session, _handler, _subscriptionId, _params = [], _name) {
|
|
18
|
+
this._session = _session;
|
|
19
|
+
this._handler = _handler;
|
|
20
|
+
this._subscriptionId = _subscriptionId;
|
|
21
|
+
this._params = _params;
|
|
22
|
+
this._name = _name;
|
|
23
|
+
// Has _deactivate been called?
|
|
24
|
+
this._deactivated = false;
|
|
25
|
+
// Stop callbacks to g/c this sub. called w/ zero arguments.
|
|
26
|
+
this._stopCallbacks = [];
|
|
27
|
+
// The set of (collection, documentid) that this subscription has
|
|
28
|
+
// an opinion about.
|
|
29
|
+
this._documents = new Map();
|
|
30
|
+
// Remember if we are ready.
|
|
31
|
+
this._ready = false;
|
|
32
|
+
// For now, the id filter is going to default to
|
|
33
|
+
// the to/from DDP methods on MongoID, to
|
|
34
|
+
// specifically deal with mongo/minimongo ObjectIds.
|
|
35
|
+
// Later, you will be able to make this be "raw"
|
|
36
|
+
// if you want to publish a collection that you know
|
|
37
|
+
// just has strings for keys and no funny business, to
|
|
38
|
+
// a DDP consumer that isn't minimongo.
|
|
39
|
+
this._idFilter = {
|
|
40
|
+
idStringify: id => id /*MongoID.idStringify*/,
|
|
41
|
+
idParse: id => id /*MongoID.idParse*/
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* @summary Access inside the publish function. The incoming [connection](#meteor_onconnection) for this subscription.
|
|
45
|
+
* @locus Server
|
|
46
|
+
* @name connection
|
|
47
|
+
* @memberOf Subscription
|
|
48
|
+
* @instance
|
|
49
|
+
*/
|
|
50
|
+
this.connection = _session.connectionHandle; // public API object
|
|
51
|
+
// Only named subscriptions have IDs, but we need some sort of string
|
|
52
|
+
// internally to keep track of all subscriptions inside
|
|
53
|
+
// SessionDocumentViews. We use this subscriptionHandle for that.
|
|
54
|
+
if (this._subscriptionId) {
|
|
55
|
+
this._subscriptionHandle = `N${this._subscriptionId}`;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
this._subscriptionHandle = `U${main_1.Random.id()}`;
|
|
59
|
+
}
|
|
60
|
+
// Part of the public API: the user of this sub.
|
|
61
|
+
/**
|
|
62
|
+
* @summary Access inside the publish function. The id of the logged-in user, or `null` if no user is logged in.
|
|
63
|
+
* @locus Server
|
|
64
|
+
* @memberOf Subscription
|
|
65
|
+
* @name userId
|
|
66
|
+
* @instance
|
|
67
|
+
*/
|
|
68
|
+
this.userId = this._session.userId;
|
|
69
|
+
}
|
|
70
|
+
;
|
|
71
|
+
_runHandler() {
|
|
72
|
+
// XXX should we unblock() here? Either before running the publish
|
|
73
|
+
// function, or before running _publishCursor.
|
|
74
|
+
//
|
|
75
|
+
// Right now, each publish function blocks all future publishes and
|
|
76
|
+
// methods waiting on data from Mongo (or whatever else the function
|
|
77
|
+
// blocks on). This probably slows page load in common cases.
|
|
78
|
+
let resultOrThenable = null;
|
|
79
|
+
const oldInvocation = livedata_server_1.DDP._CurrentPublicationInvocation;
|
|
80
|
+
try {
|
|
81
|
+
livedata_server_1.DDP._CurrentPublicationInvocation = this;
|
|
82
|
+
resultOrThenable = (0, livedata_server_1.maybeAuditArgumentChecks)(this._handler, this, (0, ejson_1.clone)(this._params),
|
|
83
|
+
// It's OK that this would look weird for universal subscriptions,
|
|
84
|
+
// because they have no arguments so there can never be an
|
|
85
|
+
// audit-argument-checks failure.
|
|
86
|
+
"publisher '" + this._name + "'");
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
this.error(e);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
livedata_server_1.DDP._CurrentPublicationInvocation = oldInvocation;
|
|
94
|
+
}
|
|
95
|
+
// Did the handler call this.error or this.stop?
|
|
96
|
+
if (this._isDeactivated())
|
|
97
|
+
return;
|
|
98
|
+
// Both conventional and async publish handler functions are supported.
|
|
99
|
+
// If an object is returned with a then() function, it is either a promise
|
|
100
|
+
// or thenable and will be resolved asynchronously.
|
|
101
|
+
const isThenable = resultOrThenable && typeof resultOrThenable.then === 'function';
|
|
102
|
+
if (isThenable) {
|
|
103
|
+
Promise.resolve(resultOrThenable).then((...args) => this._publishHandlerResult.bind(this)(...args), e => this.error(e));
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this._publishHandlerResult(resultOrThenable);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_publishHandlerResult(res) {
|
|
110
|
+
// SPECIAL CASE: Instead of writing their own callbacks that invoke
|
|
111
|
+
// this.added/changed/ready/etc, the user can just return a collection
|
|
112
|
+
// cursor or array of cursors from the publish function; we call their
|
|
113
|
+
// _publishCursor method which starts observing the cursor and publishes the
|
|
114
|
+
// results. Note that _publishCursor does NOT call ready().
|
|
115
|
+
//
|
|
116
|
+
// XXX This uses an undocumented interface which only the Mongo cursor
|
|
117
|
+
// interface publishes. Should we make this interface public and encourage
|
|
118
|
+
// users to implement it themselves? Arguably, it's unnecessary; users can
|
|
119
|
+
// already write their own functions like
|
|
120
|
+
// var publishMyReactiveThingy = function (name, handler) {
|
|
121
|
+
// Meteor.publish(name, function () {
|
|
122
|
+
// var reactiveThingy = handler();
|
|
123
|
+
// reactiveThingy.publishMe();
|
|
124
|
+
// });
|
|
125
|
+
// };
|
|
126
|
+
var self = this;
|
|
127
|
+
var isCursor = function (c) {
|
|
128
|
+
return c && c._publishCursor;
|
|
129
|
+
};
|
|
130
|
+
if (isCursor(res)) {
|
|
131
|
+
try {
|
|
132
|
+
res._publishCursor(self);
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
self.error(e);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// _publishCursor only returns after the initial added callbacks have run.
|
|
139
|
+
// mark subscription as ready.
|
|
140
|
+
self.ready();
|
|
141
|
+
}
|
|
142
|
+
else if (Array.isArray(res)) {
|
|
143
|
+
// Check all the elements are cursors
|
|
144
|
+
if (!res.every(isCursor)) {
|
|
145
|
+
self.error(new Error("Publish function returned an array of non-Cursors"));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// Find duplicate collection names
|
|
149
|
+
// XXX we should support overlapping cursors, but that would require the
|
|
150
|
+
// merge box to allow overlap within a subscription
|
|
151
|
+
var collectionNames = {};
|
|
152
|
+
for (var i = 0; i < res.length; ++i) {
|
|
153
|
+
var collectionName = res[i].cursorDescription.collectionName;
|
|
154
|
+
if (collectionNames.hasOwnProperty(collectionName)) {
|
|
155
|
+
self.error(new Error("Publish function returned multiple cursors for collection " +
|
|
156
|
+
collectionName));
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
collectionNames[collectionName] = true;
|
|
160
|
+
}
|
|
161
|
+
;
|
|
162
|
+
try {
|
|
163
|
+
for (const cur of res) {
|
|
164
|
+
cur._publishCursor(self);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
self.error(e);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
self.ready();
|
|
172
|
+
}
|
|
173
|
+
else if (res) {
|
|
174
|
+
// Truthy values other than cursors or arrays are probably a
|
|
175
|
+
// user mistake (possible returning a Mongo document via, say,
|
|
176
|
+
// `coll.findOne()`).
|
|
177
|
+
self.error(new Error("Publish function can only return a Cursor or "
|
|
178
|
+
+ "an array of Cursors"));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// This calls all stop callbacks and prevents the handler from updating any
|
|
182
|
+
// SessionCollectionViews further. It's used when the user unsubscribes or
|
|
183
|
+
// disconnects, as well as during setUserId re-runs. It does *NOT* send
|
|
184
|
+
// removed messages for the published objects; if that is necessary, call
|
|
185
|
+
// _removeAllDocuments first.
|
|
186
|
+
_deactivate() {
|
|
187
|
+
var self = this;
|
|
188
|
+
if (self._deactivated)
|
|
189
|
+
return;
|
|
190
|
+
self._deactivated = true;
|
|
191
|
+
self._callStopCallbacks();
|
|
192
|
+
/*Package['facts-base'] && Package['facts-base'].Facts.incrementServerFact(
|
|
193
|
+
"livedata", "subscriptions", -1);*/
|
|
194
|
+
}
|
|
195
|
+
_callStopCallbacks() {
|
|
196
|
+
var self = this;
|
|
197
|
+
// Tell listeners, so they can clean up
|
|
198
|
+
var callbacks = self._stopCallbacks;
|
|
199
|
+
self._stopCallbacks = [];
|
|
200
|
+
for (const callback of callbacks) {
|
|
201
|
+
callback();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Send remove messages for every document.
|
|
205
|
+
_removeAllDocuments() {
|
|
206
|
+
var self = this;
|
|
207
|
+
self._documents.forEach(function (collectionDocs, collectionName) {
|
|
208
|
+
collectionDocs.forEach(function (strId) {
|
|
209
|
+
self.removed(collectionName, self._idFilter.idParse(strId));
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
// Returns a new Subscription for the same session with the same
|
|
214
|
+
// initial creation parameters. This isn't a clone: it doesn't have
|
|
215
|
+
// the same _documents cache, stopped state or callbacks; may have a
|
|
216
|
+
// different _subscriptionHandle, and gets its userId from the
|
|
217
|
+
// session, not from this object.
|
|
218
|
+
_recreate() {
|
|
219
|
+
var self = this;
|
|
220
|
+
return new Subscription(self._session, self._handler, self._subscriptionId, self._params, self._name);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* @summary Call inside the publish function. Stops this client's subscription, triggering a call on the client to the `onStop` callback passed to [`Meteor.subscribe`](#meteor_subscribe), if any. If `error` is not a [`Meteor.Error`](#meteor_error), it will be [sanitized](#meteor_error).
|
|
224
|
+
* @locus Server
|
|
225
|
+
* @param {Error} error The error to pass to the client.
|
|
226
|
+
* @instance
|
|
227
|
+
* @memberOf Subscription
|
|
228
|
+
*/
|
|
229
|
+
error(error) {
|
|
230
|
+
var self = this;
|
|
231
|
+
if (self._isDeactivated())
|
|
232
|
+
return;
|
|
233
|
+
self._session._stopSubscription(self._subscriptionId, error);
|
|
234
|
+
}
|
|
235
|
+
// Note that while our DDP client will notice that you've called stop() on the
|
|
236
|
+
// server (and clean up its _subscriptions table) we don't actually provide a
|
|
237
|
+
// mechanism for an app to notice this (the subscribe onError callback only
|
|
238
|
+
// triggers if there is an error).
|
|
239
|
+
/**
|
|
240
|
+
* @summary Call inside the publish function. Stops this client's subscription and invokes the client's `onStop` callback with no error.
|
|
241
|
+
* @locus Server
|
|
242
|
+
* @instance
|
|
243
|
+
* @memberOf Subscription
|
|
244
|
+
*/
|
|
245
|
+
stop() {
|
|
246
|
+
var self = this;
|
|
247
|
+
if (self._isDeactivated())
|
|
248
|
+
return;
|
|
249
|
+
self._session._stopSubscription(self._subscriptionId);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @summary Call inside the publish function. Registers a callback function to run when the subscription is stopped.
|
|
253
|
+
* @locus Server
|
|
254
|
+
* @memberOf Subscription
|
|
255
|
+
* @instance
|
|
256
|
+
* @param {Function} func The callback function
|
|
257
|
+
*/
|
|
258
|
+
onStop(func) {
|
|
259
|
+
var self = this;
|
|
260
|
+
if (self._isDeactivated())
|
|
261
|
+
func();
|
|
262
|
+
else
|
|
263
|
+
self._stopCallbacks.push(func);
|
|
264
|
+
}
|
|
265
|
+
// This returns true if the sub has been deactivated, *OR* if the session was
|
|
266
|
+
// destroyed but the deferred call to _deactivateAllSubscriptions hasn't
|
|
267
|
+
// happened yet.
|
|
268
|
+
_isDeactivated() {
|
|
269
|
+
var self = this;
|
|
270
|
+
return self._deactivated || self._session.inQueue === null;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* @summary Call inside the publish function. Informs the subscriber that a document has been added to the record set.
|
|
274
|
+
* @locus Server
|
|
275
|
+
* @memberOf Subscription
|
|
276
|
+
* @instance
|
|
277
|
+
* @param {String} collection The name of the collection that contains the new document.
|
|
278
|
+
* @param {String} id The new document's ID.
|
|
279
|
+
* @param {Object} fields The fields in the new document. If `_id` is present it is ignored.
|
|
280
|
+
*/
|
|
281
|
+
added(collectionName, id, fields) {
|
|
282
|
+
if (this._isDeactivated())
|
|
283
|
+
return;
|
|
284
|
+
id = this._idFilter.idStringify(id);
|
|
285
|
+
if (this._session.server.getPublicationStrategy(collectionName).doAccountingForCollection) {
|
|
286
|
+
let ids = this._documents.get(collectionName);
|
|
287
|
+
if (ids == null) {
|
|
288
|
+
ids = new Set();
|
|
289
|
+
this._documents.set(collectionName, ids);
|
|
290
|
+
}
|
|
291
|
+
ids.add(id);
|
|
292
|
+
}
|
|
293
|
+
this._session.added(this._subscriptionHandle, collectionName, id, fields);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @summary Call inside the publish function. Informs the subscriber that a document in the record set has been modified.
|
|
297
|
+
* @locus Server
|
|
298
|
+
* @memberOf Subscription
|
|
299
|
+
* @instance
|
|
300
|
+
* @param {String} collection The name of the collection that contains the changed document.
|
|
301
|
+
* @param {String} id The changed document's ID.
|
|
302
|
+
* @param {Object} fields The fields in the document that have changed, together with their new values. If a field is not present in `fields` it was left unchanged; if it is present in `fields` and has a value of `undefined` it was removed from the document. If `_id` is present it is ignored.
|
|
303
|
+
*/
|
|
304
|
+
changed(collectionName, id, fields) {
|
|
305
|
+
if (this._isDeactivated())
|
|
306
|
+
return;
|
|
307
|
+
id = this._idFilter.idStringify(id);
|
|
308
|
+
this._session.changed(this._subscriptionHandle, collectionName, id, fields);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* @summary Call inside the publish function. Informs the subscriber that a document has been removed from the record set.
|
|
312
|
+
* @locus Server
|
|
313
|
+
* @memberOf Subscription
|
|
314
|
+
* @instance
|
|
315
|
+
* @param {String} collection The name of the collection that the document has been removed from.
|
|
316
|
+
* @param {String} id The ID of the document that has been removed.
|
|
317
|
+
*/
|
|
318
|
+
removed(collectionName, id) {
|
|
319
|
+
if (this._isDeactivated())
|
|
320
|
+
return;
|
|
321
|
+
id = this._idFilter.idStringify(id);
|
|
322
|
+
if (this._session.server.getPublicationStrategy(collectionName).doAccountingForCollection) {
|
|
323
|
+
// We don't bother to delete sets of things in a collection if the
|
|
324
|
+
// collection is empty. It could break _removeAllDocuments.
|
|
325
|
+
this._documents.get(collectionName).delete(id);
|
|
326
|
+
}
|
|
327
|
+
this._session.removed(this._subscriptionHandle, collectionName, id);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* @summary Call inside the publish function. Informs the subscriber that an initial, complete snapshot of the record set has been sent. This will trigger a call on the client to the `onReady` callback passed to [`Meteor.subscribe`](#meteor_subscribe), if any.
|
|
331
|
+
* @locus Server
|
|
332
|
+
* @memberOf Subscription
|
|
333
|
+
* @instance
|
|
334
|
+
*/
|
|
335
|
+
ready() {
|
|
336
|
+
var self = this;
|
|
337
|
+
if (self._isDeactivated())
|
|
338
|
+
return;
|
|
339
|
+
if (!self._subscriptionId)
|
|
340
|
+
return; // Unnecessary but ignored for universal sub
|
|
341
|
+
if (!self._ready) {
|
|
342
|
+
self._session.sendReady([self._subscriptionId]);
|
|
343
|
+
self._ready = true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
exports.Subscription = Subscription;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringifyDDP = exports.parseDDP = exports.SUPPORTED_DDP_VERSIONS = exports.last = exports.isEmpty = exports.keys = exports.slice = exports.hasOwn = void 0;
|
|
4
|
+
const ejson_1 = require("../ejson/ejson");
|
|
5
|
+
exports.hasOwn = Object.prototype.hasOwnProperty;
|
|
6
|
+
exports.slice = Array.prototype.slice;
|
|
7
|
+
function keys(obj) {
|
|
8
|
+
return Object.keys(Object(obj));
|
|
9
|
+
}
|
|
10
|
+
exports.keys = keys;
|
|
11
|
+
function isEmpty(obj) {
|
|
12
|
+
if (obj == null) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(obj) ||
|
|
16
|
+
typeof obj === "string") {
|
|
17
|
+
return obj.length === 0;
|
|
18
|
+
}
|
|
19
|
+
for (const key in obj) {
|
|
20
|
+
if (exports.hasOwn.call(obj, key)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
exports.isEmpty = isEmpty;
|
|
27
|
+
function last(array, n, guard) {
|
|
28
|
+
if (array == null) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if ((n == null) || guard) {
|
|
32
|
+
return array[array.length - 1];
|
|
33
|
+
}
|
|
34
|
+
return exports.slice.call(array, Math.max(array.length - n, 0));
|
|
35
|
+
}
|
|
36
|
+
exports.last = last;
|
|
37
|
+
exports.SUPPORTED_DDP_VERSIONS = ['1', 'pre2', 'pre1'];
|
|
38
|
+
function parseDDP(stringMessage) {
|
|
39
|
+
try {
|
|
40
|
+
var msg = JSON.parse(stringMessage);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
console.log("Discarding message with invalid JSON", stringMessage);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
// DDP messages must be objects.
|
|
47
|
+
if (msg === null || typeof msg !== 'object') {
|
|
48
|
+
console.log("Discarding non-object DDP message", stringMessage);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
// massage msg to get it into "abstract ddp" rather than "wire ddp" format.
|
|
52
|
+
// switch between "cleared" rep of unsetting fields and "undefined"
|
|
53
|
+
// rep of same
|
|
54
|
+
if (exports.hasOwn.call(msg, 'cleared')) {
|
|
55
|
+
if (!exports.hasOwn.call(msg, 'fields')) {
|
|
56
|
+
msg.fields = {};
|
|
57
|
+
}
|
|
58
|
+
msg.cleared.forEach(clearKey => {
|
|
59
|
+
msg.fields[clearKey] = undefined;
|
|
60
|
+
});
|
|
61
|
+
delete msg.cleared;
|
|
62
|
+
}
|
|
63
|
+
['fields', 'params', 'result'].forEach(field => {
|
|
64
|
+
if (exports.hasOwn.call(msg, field)) {
|
|
65
|
+
msg[field] = (0, ejson_1._adjustTypesFromJSONValue)(msg[field]);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return msg;
|
|
69
|
+
}
|
|
70
|
+
exports.parseDDP = parseDDP;
|
|
71
|
+
;
|
|
72
|
+
function stringifyDDP(msg) {
|
|
73
|
+
const copy = (0, ejson_1.clone)(msg);
|
|
74
|
+
// swizzle 'changed' messages from 'fields undefined' rep to 'fields
|
|
75
|
+
// and cleared' rep
|
|
76
|
+
if (exports.hasOwn.call(msg, 'fields')) {
|
|
77
|
+
const cleared = [];
|
|
78
|
+
Object.keys(msg.fields).forEach(key => {
|
|
79
|
+
const value = msg.fields[key];
|
|
80
|
+
if (typeof value === "undefined") {
|
|
81
|
+
cleared.push(key);
|
|
82
|
+
delete copy.fields[key];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (!isEmpty(cleared)) {
|
|
86
|
+
copy.cleared = cleared;
|
|
87
|
+
}
|
|
88
|
+
if (isEmpty(copy.fields)) {
|
|
89
|
+
delete copy.fields;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// adjust types to basic
|
|
93
|
+
['fields', 'params', 'result'].forEach(field => {
|
|
94
|
+
if (exports.hasOwn.call(copy, field)) {
|
|
95
|
+
copy[field] = (0, ejson_1._adjustTypesToJSONValue)(copy[field]);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
if (msg.id && typeof msg.id !== 'string') {
|
|
99
|
+
throw new Error("Message id is not a string");
|
|
100
|
+
}
|
|
101
|
+
return JSON.stringify(copy);
|
|
102
|
+
}
|
|
103
|
+
exports.stringifyDDP = stringifyDDP;
|
|
104
|
+
;
|