-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
426 lines (348 loc) · 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const Auto = require('pigeon').auto;
const WebSocket = require('ws');
const wss = new WebSocket.Server({ noServer: true });
const Redis = require('ioredis');
const fs = require('fs-promise');
const mkdirp = require('mkdirp');
const { backfill } = require('./helpers');
const Local = require('./local');
const connections = new WeakMap;
const docs = {};
const streams = {};
const loads = {};
const config = {
reader: reader,
writer: writer,
redis: null,
channel: '',
window: 5 * 1000,
validator: validator,
};
function prevWindowTs(ts=Date.now()) {
return ((ts / config.window | 0) - 1) * config.window;
}
let subscriber;
let publisher;
let snapshotter;
let CHANNEL;
let Bus;
const handlers = {};
function on(event, handler) {
handlers[event] = handler;
}
function serialize(doc) {
return Auto.save(doc);
}
function initialize(args={}) {
for (const k of Object.keys(args)) {
if (k in args) {
config[k] = args[k];
}
}
CHANNEL = config.channel || `syncable:${process.env.NODE_ENV}`;
Bus = config.redis ? Redis : Local;
subscriber = new Bus(config.redis);
publisher = new Bus(config.redis);
snapshotter = new Bus(config.redis);
subscriber.on('message', async (channel, message) => {
const { key, action } = JSON.parse(message);
if (!key) return;
if (action == 'SYNCABLE_UNLOAD') {
return _unload(key, false);
}
if (action == 'SYNCABLE_UNLOAD_PREFIX') {
return _unloadByPrefix(key);
}
});
subscriber.subscribe(CHANNEL, (err, count) => {
if (err) throw new Error(err);
});
}
function publish({ key, action }) {
const message = JSON.stringify({ key, action });
publisher.publish(CHANNEL, message);
}
function broadcast(key, data, exclude) {
for (const c of wss.clients) {
if (connections.get(c).key != key) continue;
if (c.readyState !== WebSocket.OPEN) continue;
if (exclude && exclude(c)) continue;
try {
c.send(data);
} catch (e) {
console.warn("trouble broadcasting over websocket", e);
}
}
}
async function _syncDoc(key, doc, fn) {
const changeDoc = Auto.change(doc, fn);
const changes = Auto.getChanges(doc, changeDoc);
if (!changes.diff.length) {
return Promise.resolve(doc);
}
docs[key] = changeDoc;
broadcast(
key,
JSON.stringify({ action: 'change', data: { changes } }),
);
const stream = _getStream(key);
stream.xadd(key, '*', 'message', JSON.stringify({ action: 'change', data: { changes } }));
write({ key, doc: docs[key] });
return Promise.resolve(docs[key]);
}
const syncable = (template = {}) => {
const pendingHandlers = new Map();
const eventHandlers = {};
const _id = Math.random().toString(36).slice(2);
const handler = (req, res, next) => {
if (req.headers.upgrade !== 'websocket') {
return res.send("please connect via WebSocket");
}
wss.handleUpgrade(req, req.socket, { length: 0 }, async ws => {
const pending = pendingHandlers.get(_id);
if (pending) {
eventHandlers[req.route.path] = {};
for (const name of Object.keys(pending)) {
eventHandlers[req.route.path][name] = pending[name];
}
}
await handleConnect(ws, req);
});
};
handler.on = (eventName, eventHandler) => {
let pending = pendingHandlers.get(_id);
if (pending) {
if (pending[eventName]) {
pending[eventName].push(eventHandler);
} else {
pending[eventName] = [ eventHandler ];
}
} else {
pending = {};
pending[eventName] = [ eventHandler ];
}
pendingHandlers.set(_id, pending);
};
async function handleConnect (ws, req) {
const defaultKey = req.baseUrl + req.path;
const key = config.keyFormatter ? config.keyFormatter(defaultKey) : defaultKey;
connections.set(ws, { key });
if (eventHandlers[req.route.path] && ('initialize' in eventHandlers[req.route.path])) {
for (const h of eventHandlers[req.route.path].initialize) {
await h(ws, req);
}
}
let fromTemplate = template;
if (typeof template == 'function') {
fromTemplate = template(req);
}
await load(key, fromTemplate);
ws.send(JSON.stringify({
action: 'init',
doc: Auto.save(docs[key])
}));
ws.on('message', async data => {
const message = JSON.parse(data);
if (message.action == 'time') {
return ws.send(JSON.stringify({ serverTime: Date.now(), clientTime: message.data }));
}
if (message.action !== 'change') return;
const doc = docs[key];
if (!doc) {
console.log(`no doc for key ${key}`);
return;
}
const validated = await config.validator(ws, req, message.data);
if (!validated) {
return ws.send(JSON.stringify({ error: 'rejected', message }));
}
const changeTs = message.data.changes.ts;
const twoSnapshotsAgoTime =
((Date.now() / config.window | 0) * config.window) - config.window;
if (twoSnapshotsAgoTime > changeTs) {
console.log("rejecting too old stale change", changeTs, twoSnapshotsAgoTime)
ws.send(JSON.stringify({ error: 'stale_change', snapshotTime: twoSnapshotsAgoTime, changeTs }));
return;
}
docs[key] = Auto.applyChangesInPlace(doc, message.data.changes);
write({ key, doc: docs[key] });
broadcast(key, data, c => c == ws);
const stream = _getStream(key);
stream.xadd(key, '*', 'message', data);
});
if (eventHandlers[req.route.path] && ('connection' in eventHandlers[req.route.path])) {
for (const h of eventHandlers[req.route.path].connection) {
await h(ws, req);
}
}
}
async function sync(key, fn) {
docs[key] = await _syncDoc(key, docs[key], fn);
return docs[key];
}
return handler;
}
async function load(key, template = {}) {
// return from memory if we've loaded already
if (docs[key]) return proxy(key, docs[key]);
const stream = _getStream(key);
// if multiple concurrent load requests arrive, handle them together
loads[key] ||= new Promise(async (resolve, reject) => {
try {
var data = await config.reader(key);
} catch(e) {
reject(e);
}
if (data) {
// if we've data then load and backfill it
doc = Auto.load(data);
doc = await _syncDoc(key, doc, d => backfill(d, template));
} else {
// if it's a new doc then start with the template and do our initial write
doc = Auto.from(template);
write({ key, doc });
}
docs[key] = doc;
let minId = Auto.getHistory(docs[key]).slice(-1)[0]?.ts;
// catch up with any changes in the stream not yet persisted
const response = await stream.xread('STREAMS', key, minId - config.window);
if (response) {
const [ [ _name, entries ] ] = response;
for (const [ messageId, [ MESSAGE, message ] ] of entries || []) {
_handleMessage(message);
minId = messageId;
}
}
// listen for changes going forward
setTimeout(async _ => {
let cursor = '$';
while (stream) {
const [ [ _name, entries ] ] = await stream.xread('BLOCK', 0, 'STREAMS', key, cursor);
for (const [ messageId, [ MESSAGE, message ] ] of entries || []) {
_handleMessage(message);
}
cursor = entries.slice(-1)[0][0];
}
})
async function _handleMessage(data) {
const { data: { changes } } = JSON.parse(data);
if (!changes) {
console.warn("bad message", data);
return;
}
const doc = docs[key];
docs[key] = Auto.applyChangesInPlace(doc, changes);
broadcast(key, JSON.stringify({ action: 'change', data: { changes } }));
}
docs[key] = doc;
resolve(proxy(key, docs[key]));
});
return loads[key];
}
function unload(key) {
publish({ key, action: 'SYNCABLE_UNLOAD' });
}
function unloadByPrefix(prefix) {
publish({ key: prefix, action: 'SYNCABLE_UNLOAD_PREFIX' });
}
function _unload(key) {
if (handlers.unload) handlers.unload(docs[key]);
delete loads[key];
delete docs[key];
}
function _unloadByPrefix(prefix) {
if (!(prefix && prefix.length)) return;
for (const key of Object.keys(docs)) {
if (key.startsWith(prefix)) {
_unload(key);
}
}
}
async function write({ key, doc }) {
write._tv ||= {};
write._last ||= {};
write._last[key] ||= -Infinity;
if (write._last[key] < Date.now() - config.window) {
write._last[key] = Date.now();
const data = Auto.save(doc);
config.writer(key, data);
} else {
const delay = Math.max(0, write._last[key] + config.window - Date.now());
write._tv[key] ||= setTimeout(async _ => {
delete write._tv[key];
try {
write._last[key] = Date.now();
const data = Auto.save(doc);
await config.writer(key, data);
} catch(e) {
console.warn("write failed, retrying once next window", e);
write._tv[key] ||= setTimeout(_ => {
delete write._tv[key];
write._last[key] = Date.now();
const data = Auto.save(doc);
config.writer(key, data);
}, config.window);
}
}, delay);
}
}
async function find(finder) {
const keys = await finder(Object.keys(docs));
return keys;
}
async function reader(key) {
const data = await snapshotter.get(`syncable:${key}`);
return data;
}
async function writer(key, data) {
try {
await snapshotter.set(`syncable:${key}`, data);
} catch (e) {
console.error("couldn't write ${key}", e);
}
}
async function validator(ws, req) {
return true;
}
function applyChanges(doc, changes) {
return Auto.applyChanges(doc, changes);
}
function _getStream(key) {
if (streams[key]) return streams[key];
streams[key] = new Bus(config.redis);
return streams[key];
}
function proxy(key) {
const handler = {
get: function(target, prop) {
const doc = docs[key];
if (prop in doc) {
return doc[prop];
} else if (prop == 'sync') {
return async function sync(fn) {
const s = await _syncDoc(key, doc, fn)
docs[key] = s
}
} else {
return Reflect.get(...arguments);
}
},
set: function(obj, prop, value) {
throw new Error(`please call sync to change this document`);
}
}
return new Proxy(doc, handler);
}
syncable.initialize = initialize;
syncable.client = require('./client');
syncable.load = load;
syncable.unload = unload;
syncable.unloadByPrefix = unloadByPrefix;
syncable.find = find;
syncable.wss = wss;
syncable.serialize = serialize;
syncable.on = on;
syncable.Auto = Auto;
syncable._config = config;
syncable.handle = syncable;
module.exports = syncable;