statebus 6.1.21 → 7.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/{client.js → clients.js} +258 -146
- package/extras/tests.js +292 -412
- package/package.json +9 -3
- package/{server.js → servers.js} +291 -337
- package/statebus.js +297 -620
- package/extras/coffee.js +0 -12
- package/extras/dash.coffee +0 -302
- package/extras/diffsync.js +0 -1462
- package/extras/getCaretCoordinates.js +0 -141
- package/extras/react.js +0 -18147
- package/extras/sockjs.js +0 -27
package/{server.js → servers.js}
RENAMED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
var fs = require('fs'),
|
|
2
2
|
util = require('util')
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
try {
|
|
6
|
-
var braidify = require('braidify').http_server
|
|
7
|
-
console.log('Found braidify library. Braid-HTTP enabled!')
|
|
8
|
-
} catch (e) {
|
|
9
|
-
var braidify = undefined
|
|
10
|
-
}
|
|
4
|
+
var braidify = require('braidify').http_server
|
|
11
5
|
|
|
12
6
|
|
|
13
7
|
function default_options (bus) { return {
|
|
@@ -77,7 +71,6 @@ function import_server (bus, options)
|
|
|
77
71
|
bus.express = express()
|
|
78
72
|
bus.http = express.Router()
|
|
79
73
|
bus.install_express(bus.express)
|
|
80
|
-
bus.initialize_braid_http()
|
|
81
74
|
|
|
82
75
|
// use gzip compression if available
|
|
83
76
|
try { bus.http.use(require('compression')())
|
|
@@ -106,24 +99,11 @@ function import_server (bus, options)
|
|
|
106
99
|
bus.sync_files(x.state_path, x.fs_path)
|
|
107
100
|
})
|
|
108
101
|
|
|
109
|
-
// Free the CORS for Braid requests!
|
|
110
|
-
bus.express.use((req, res, next) => {
|
|
111
|
-
// If this requests knows about Braid then we presume the
|
|
112
|
-
// programmer knows that any client might make this cross-origin
|
|
113
|
-
// request.
|
|
114
|
-
if (req.version || req.parents || req.subscribe
|
|
115
|
-
|| req.headers.subscribe
|
|
116
|
-
|| req.method === 'OPTIONS')
|
|
117
|
-
free_the_cors(req, res, next)
|
|
118
|
-
else
|
|
119
|
-
next()
|
|
120
|
-
})
|
|
121
|
-
|
|
122
102
|
// User will put his routes in here
|
|
123
103
|
bus.express.use(bus.http)
|
|
124
104
|
|
|
125
105
|
// Connect bus to the HTTP server
|
|
126
|
-
bus.express.use(bus.
|
|
106
|
+
bus.express.use(bus.http_in)
|
|
127
107
|
|
|
128
108
|
// Serve Client Coffee
|
|
129
109
|
bus.serve_client_coffee()
|
|
@@ -142,18 +122,18 @@ function import_server (bus, options)
|
|
|
142
122
|
// Now handle backup handlers
|
|
143
123
|
if (count === 0) {
|
|
144
124
|
|
|
145
|
-
// A
|
|
125
|
+
// A set to master without a to_set defined should be
|
|
146
126
|
// automatically reflected to all clients.
|
|
147
|
-
if (method === '
|
|
148
|
-
bus.
|
|
127
|
+
if (method === 'to_set') {
|
|
128
|
+
bus.set.fire(arg, t)
|
|
149
129
|
bus.route(arg.key, 'on_set_sync', arg, t)
|
|
150
130
|
count++
|
|
151
131
|
}
|
|
152
132
|
|
|
153
|
-
// A
|
|
133
|
+
// A get to master without a to_get defined should go to
|
|
154
134
|
// the database, if we have one.
|
|
155
|
-
if (method === '
|
|
156
|
-
bus.
|
|
135
|
+
if (method === 'to_get') {
|
|
136
|
+
bus.db_get(key)
|
|
157
137
|
// This basically renders the count useless-- you probably
|
|
158
138
|
// don't want to wrap this route() with another route().
|
|
159
139
|
count++
|
|
@@ -173,28 +153,18 @@ function import_server (bus, options)
|
|
|
173
153
|
}
|
|
174
154
|
},
|
|
175
155
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (bus.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
} else
|
|
182
|
-
bus.to_http_body = (o) => {
|
|
183
|
-
var unwrap = (Object.keys(o).length === 2
|
|
184
|
-
&& '_' in o
|
|
185
|
-
&& typeof o._ === 'string')
|
|
186
|
-
// To do: translate pointers as keys
|
|
187
|
-
return unwrap ? o._ : JSON.stringify(o)
|
|
188
|
-
}
|
|
156
|
+
// Connect HTTP GET and PUT to our Get and Set
|
|
157
|
+
http_in: function (req, res, next) {
|
|
158
|
+
if (bus.honk)
|
|
159
|
+
console.log(req.method, req.url, req.headers.subscribe
|
|
160
|
+
? 'Subscribe: ' + req.headers.subscribe : '')
|
|
189
161
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
162
|
+
// If this requests knows about Braid then we presume the programmer
|
|
163
|
+
// knows that any client might make this cross-origin request.
|
|
164
|
+
if (req.headers.version || req.headers.parents || req.headers.subscribe
|
|
165
|
+
|| req.method === 'OPTIONS')
|
|
166
|
+
free_the_cors(req, res)
|
|
195
167
|
|
|
196
|
-
// Connect HTTP GET and PUT to our Fetch and Save
|
|
197
|
-
http_handlers: function (req, res, next) {
|
|
198
168
|
if (req.method === 'GET') {
|
|
199
169
|
|
|
200
170
|
// Make a temporary client bus
|
|
@@ -206,44 +176,32 @@ function import_server (bus, options)
|
|
|
206
176
|
res.end()
|
|
207
177
|
}
|
|
208
178
|
|
|
209
|
-
braidify
|
|
210
|
-
if (req.subscribe)
|
|
179
|
+
braidify(req, res)
|
|
180
|
+
if (req.subscribe)
|
|
211
181
|
res.startSubscription({ onClose: end_it_all })
|
|
212
|
-
|
|
213
|
-
} else
|
|
182
|
+
else
|
|
214
183
|
res.statusCode = 200
|
|
215
184
|
|
|
216
|
-
// Do the
|
|
217
|
-
cbus.honk = 'statelog'
|
|
185
|
+
// Do the get
|
|
186
|
+
// cbus.honk = 'statelog'
|
|
218
187
|
var key = req.path.substr(1)
|
|
219
|
-
cbus.
|
|
220
|
-
var body =
|
|
221
|
-
|
|
222
|
-
//
|
|
223
|
-
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
if (braidify)
|
|
232
|
-
res.sendVersion({body: body || 'null'})
|
|
233
|
-
|
|
234
|
-
// Or just return the current version
|
|
235
|
-
else
|
|
236
|
-
res.send(body)
|
|
237
|
-
|
|
238
|
-
// And shut down the connection if nothing's left to do
|
|
239
|
-
if (!braidify || !req.subscribe || !body)
|
|
188
|
+
cbus.get(key, cb = (o) => {
|
|
189
|
+
var body = to_http_body(o)
|
|
190
|
+
|
|
191
|
+
// If we're braiding, send via subscription
|
|
192
|
+
|
|
193
|
+
// Note: if body === undefined, we need to send the
|
|
194
|
+
// equivalent of a 404. This is missing in braid spec:
|
|
195
|
+
// https://github.com/braid-org/braid-spec/issues/110
|
|
196
|
+
res.sendVersion({body: body || 'null'})
|
|
197
|
+
|
|
198
|
+
// And shut down the connection if there's no subscription
|
|
199
|
+
if (!req.subscribe)
|
|
240
200
|
end_it_all()
|
|
241
201
|
})
|
|
242
202
|
}
|
|
243
203
|
|
|
244
204
|
else if (req.method === 'PUT') {
|
|
245
|
-
console.log('Got a put with body', req.body)
|
|
246
|
-
|
|
247
205
|
// Make a temporary client bus
|
|
248
206
|
var cbus = bus.bus_for_http_client(req, res)
|
|
249
207
|
|
|
@@ -265,16 +223,15 @@ function import_server (bus, options)
|
|
|
265
223
|
req.on('data', chunk => {body += chunk.toString()})
|
|
266
224
|
req.on('end', () => {
|
|
267
225
|
try {
|
|
268
|
-
console.log('gonna parse', body)
|
|
269
226
|
var path = req.url.substr(1)
|
|
270
|
-
var obj =
|
|
227
|
+
var obj = from_http_body(path, body)
|
|
271
228
|
} catch (e) {
|
|
272
229
|
console.error('Error: PUT body was not valid json', e)
|
|
273
230
|
res.statusCode = 500
|
|
274
231
|
res.end()
|
|
275
232
|
return
|
|
276
233
|
}
|
|
277
|
-
cbus.
|
|
234
|
+
cbus.set(obj)
|
|
278
235
|
res.statusCode = 200
|
|
279
236
|
res.end()
|
|
280
237
|
})
|
|
@@ -295,7 +252,7 @@ function import_server (bus, options)
|
|
|
295
252
|
// Log in as the client
|
|
296
253
|
cbus.serves_auth({remoteAddress: req.connection.remoteAddress}, bus)
|
|
297
254
|
bus.options.client(cbus)
|
|
298
|
-
cbus.
|
|
255
|
+
cbus.set({key: 'current_user', client: req.client})
|
|
299
256
|
return cbus
|
|
300
257
|
},
|
|
301
258
|
|
|
@@ -345,7 +302,7 @@ function import_server (bus, options)
|
|
|
345
302
|
bus.port = next_port_attempt
|
|
346
303
|
return result
|
|
347
304
|
} catch (e) {
|
|
348
|
-
if (next_port_attempt <
|
|
305
|
+
if (next_port_attempt < 3007) next_port_attempt = 3007
|
|
349
306
|
else next_port_attempt++
|
|
350
307
|
}
|
|
351
308
|
}
|
|
@@ -398,8 +355,8 @@ function import_server (bus, options)
|
|
|
398
355
|
// var client_busses = {} // XXX work in progress
|
|
399
356
|
var log = master.log
|
|
400
357
|
if (client_bus_func) {
|
|
401
|
-
master.
|
|
402
|
-
var connections = master.
|
|
358
|
+
master.set({key: 'connections'}) // Clean out old sessions
|
|
359
|
+
var connections = master.get('connections')
|
|
403
360
|
}
|
|
404
361
|
var s = require('sockjs').createServer({
|
|
405
362
|
sockjs_url: 'https://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js',
|
|
@@ -418,7 +375,7 @@ function import_server (bus, options)
|
|
|
418
375
|
|
|
419
376
|
connections[conn.id] = {client: conn.id, // client is deprecated
|
|
420
377
|
id: conn.id}
|
|
421
|
-
master.
|
|
378
|
+
master.set(connections)
|
|
422
379
|
|
|
423
380
|
var client = require('./statebus')()
|
|
424
381
|
client.label = 'client' + client_num++
|
|
@@ -428,15 +385,15 @@ function import_server (bus, options)
|
|
|
428
385
|
} else
|
|
429
386
|
var client = master
|
|
430
387
|
|
|
431
|
-
var
|
|
388
|
+
var our_gets_in = {} // Every key that this socket has got
|
|
432
389
|
log('sockjs_s: New connection from', conn.remoteAddress)
|
|
433
390
|
function sockjs_pubber (obj, t) {
|
|
434
391
|
// log('sockjs_pubber:', obj, t)
|
|
435
|
-
var msg = {
|
|
392
|
+
var msg = {set: obj}
|
|
436
393
|
if (t.version) msg.version = t.version
|
|
437
394
|
if (t.parents) msg.parents = t.parents
|
|
438
395
|
if (t.patch) msg.patch = t.patch
|
|
439
|
-
if (t.patch) msg.
|
|
396
|
+
if (t.patch) msg.set = msg.set.key
|
|
440
397
|
msg = JSON.stringify(msg)
|
|
441
398
|
|
|
442
399
|
if (master.simulate_network_delay) {
|
|
@@ -455,16 +412,16 @@ function import_server (bus, options)
|
|
|
455
412
|
var method = bus.message_method(message)
|
|
456
413
|
|
|
457
414
|
// Validate the message
|
|
458
|
-
if (!((method === '
|
|
459
|
-
&& master.validate(message, {
|
|
415
|
+
if (!((method === 'get'
|
|
416
|
+
&& master.validate(message, {get: 'string',
|
|
460
417
|
'?parent': 'string', '?version': 'string'}))
|
|
461
418
|
||
|
|
462
|
-
(method === '
|
|
463
|
-
&& master.validate(message, {
|
|
419
|
+
(method === 'set'
|
|
420
|
+
&& master.validate(message, {set: '*',
|
|
464
421
|
'?parents': 'array', '?version': 'string', '?patch': 'array'})
|
|
465
|
-
&& (typeof(message.
|
|
466
|
-
|| (typeof(message.
|
|
467
|
-
&& typeof(message.
|
|
422
|
+
&& (typeof(message.set) === 'string'
|
|
423
|
+
|| (typeof(message.set) === 'object'
|
|
424
|
+
&& typeof(message.set.key === 'string'))))
|
|
468
425
|
||
|
|
469
426
|
(method === 'forget'
|
|
470
427
|
&& master.validate(message, {forget: 'string'}))
|
|
@@ -481,55 +438,55 @@ function import_server (bus, options)
|
|
|
481
438
|
}
|
|
482
439
|
|
|
483
440
|
switch (method) {
|
|
484
|
-
case '
|
|
485
|
-
|
|
486
|
-
client.
|
|
441
|
+
case 'get':
|
|
442
|
+
our_gets_in[message.get] = true
|
|
443
|
+
client.get(message.get, sockjs_pubber)
|
|
487
444
|
break
|
|
488
445
|
case 'forget':
|
|
489
|
-
delete
|
|
446
|
+
delete our_gets_in[message.forget]
|
|
490
447
|
client.forget(message.forget, sockjs_pubber)
|
|
491
448
|
break
|
|
492
449
|
case 'delete':
|
|
493
450
|
client.delete(message['delete'])
|
|
494
451
|
break
|
|
495
|
-
case '
|
|
452
|
+
case 'set':
|
|
496
453
|
message.version = message.version || client.new_version()
|
|
497
454
|
if (message.patch) {
|
|
498
|
-
var o = bus.cache[message.
|
|
455
|
+
var o = bus.cache[message.set] || {key: message.set}
|
|
499
456
|
try {
|
|
500
|
-
message.
|
|
457
|
+
message.set = bus.apply_patch(o, message.patch[0])
|
|
501
458
|
} catch (e) {
|
|
502
459
|
console.error('Received bad sockjs message from '
|
|
503
460
|
+ conn.remoteAddress +': ', message, e)
|
|
504
461
|
return
|
|
505
462
|
}
|
|
506
463
|
}
|
|
507
|
-
client.
|
|
464
|
+
client.set(message.set,
|
|
508
465
|
{version: message.version,
|
|
509
466
|
parents: message.parents,
|
|
510
467
|
patch: message.patch})
|
|
511
|
-
if (
|
|
468
|
+
if (our_gets_in[message.set.key]) { // Store what we've seen if we
|
|
512
469
|
// might have to publish it later
|
|
513
|
-
client.log('Adding', message.
|
|
470
|
+
client.log('Adding', message.set.key+'#'+message.version,
|
|
514
471
|
'to pubber!')
|
|
515
|
-
sockjs_pubber.has_seen(client, message.
|
|
472
|
+
sockjs_pubber.has_seen(client, message.set.key, message.version)
|
|
516
473
|
}
|
|
517
474
|
break
|
|
518
475
|
}
|
|
519
476
|
|
|
520
|
-
// validate that our
|
|
521
|
-
for (var key in
|
|
522
|
-
if (!client.
|
|
477
|
+
// validate that our gets_in are all in the bus
|
|
478
|
+
for (var key in our_gets_in)
|
|
479
|
+
if (!client.gets_in.has(key, master.funk_key(sockjs_pubber)))
|
|
523
480
|
console.trace("***\n****\nFound errant key", key,
|
|
524
481
|
'when receiving a sockjs', method, 'of', message)
|
|
525
482
|
//log('sockjs_s: done with message')
|
|
526
483
|
})
|
|
527
484
|
conn.on('close', function() {
|
|
528
485
|
log('sockjs_s: disconnected from', conn.remoteAddress, conn.id, client.id)
|
|
529
|
-
for (var key in
|
|
486
|
+
for (var key in our_gets_in)
|
|
530
487
|
client.forget(key, sockjs_pubber)
|
|
531
488
|
if (client_bus_func) {
|
|
532
|
-
delete connections[conn.id]; master.
|
|
489
|
+
delete connections[conn.id]; master.set(connections)
|
|
533
490
|
master.delete('connection/' + conn.id)
|
|
534
491
|
client.delete_bus()
|
|
535
492
|
}
|
|
@@ -539,15 +496,15 @@ function import_server (bus, options)
|
|
|
539
496
|
if (client_bus_func && !master.options.__secure) {
|
|
540
497
|
|
|
541
498
|
// A connection
|
|
542
|
-
client('connection/*').
|
|
543
|
-
var result = bus.clone(master.
|
|
499
|
+
client('connection/*').to_get = function (key, star) {
|
|
500
|
+
var result = bus.clone(master.get(key))
|
|
544
501
|
if (master.options.connections.include_users && result.user)
|
|
545
|
-
result.user = client.
|
|
502
|
+
result.user = client.get(result.user.key)
|
|
546
503
|
result.id = star
|
|
547
504
|
result.client = star // Deprecated. Delete this line in v7.
|
|
548
505
|
return result
|
|
549
506
|
}
|
|
550
|
-
client('connection/*').
|
|
507
|
+
client('connection/*').to_set = function (o, star, t) {
|
|
551
508
|
// Check permissions before editing
|
|
552
509
|
if (star !== conn.id && !master.options.connections.edit_others) {
|
|
553
510
|
t.abort()
|
|
@@ -555,38 +512,39 @@ function import_server (bus, options)
|
|
|
555
512
|
}
|
|
556
513
|
o.id = star
|
|
557
514
|
o.client = star // Deprecated. Delete this line in v7.
|
|
558
|
-
master.
|
|
515
|
+
master.set(client.clone(o))
|
|
559
516
|
}
|
|
560
517
|
|
|
561
518
|
// Your connection
|
|
562
|
-
client('connection').
|
|
519
|
+
client('connection').to_get = function () {
|
|
563
520
|
// subscribe to changes in authentication
|
|
564
|
-
client.
|
|
521
|
+
client.get('current_user')
|
|
565
522
|
|
|
566
|
-
var result = client.clone(client.
|
|
523
|
+
var result = client.clone(client.get('connection/' + conn.id))
|
|
567
524
|
delete result.key
|
|
568
525
|
return result
|
|
569
526
|
}
|
|
570
|
-
client('connection').
|
|
527
|
+
client('connection').to_set = function (o) {
|
|
571
528
|
o = client.clone(o)
|
|
572
529
|
o.key = 'connection/' + conn.id
|
|
573
|
-
client.
|
|
530
|
+
client.set(o)
|
|
574
531
|
}
|
|
575
532
|
|
|
576
533
|
// All connections
|
|
577
|
-
client('connections').
|
|
578
|
-
client('connections').
|
|
534
|
+
client('connections').to_set = function noop (t) {t.abort()}
|
|
535
|
+
client('connections').to_get = function () {
|
|
579
536
|
var result = []
|
|
580
|
-
var conns = master.
|
|
537
|
+
var conns = master.get('connections')
|
|
581
538
|
for (var connid in conns)
|
|
582
539
|
if (connid !== 'key')
|
|
583
|
-
result.push(client.
|
|
540
|
+
result.push(client.get('connection/' + connid))
|
|
584
541
|
|
|
585
542
|
return {all: result}
|
|
586
543
|
}
|
|
587
544
|
}
|
|
588
545
|
})
|
|
589
546
|
|
|
547
|
+
// console.log('websocket listening on', '/' + bus.options.websocket_path)
|
|
590
548
|
s.installHandlers(httpserver, {prefix:'/' + bus.options.websocket_path})
|
|
591
549
|
},
|
|
592
550
|
|
|
@@ -595,7 +553,7 @@ function import_server (bus, options)
|
|
|
595
553
|
url = url.replace(/^istate:\/\//, 'ws://')
|
|
596
554
|
url = url.replace(/^statei:\/\//, 'ws://')
|
|
597
555
|
WebSocket = require('websocket').w3cwebsocket
|
|
598
|
-
return new WebSocket(url+'/'+bus.options.websocket_path+'/websocket')
|
|
556
|
+
return new WebSocket(url + '/' + bus.options.websocket_path + '/websocket')
|
|
599
557
|
},
|
|
600
558
|
client_creds: function client_creds (server_url) {
|
|
601
559
|
// Right now the server just creates a different random id each time
|
|
@@ -605,14 +563,6 @@ function import_server (bus, options)
|
|
|
605
563
|
+ Math.random().toString(36).substring(2))}
|
|
606
564
|
},
|
|
607
565
|
|
|
608
|
-
// Deprecated
|
|
609
|
-
ws_client: function (prefix, url, account) {
|
|
610
|
-
console.error('ws_client() is deprecated; use net_mount() instead')
|
|
611
|
-
bus.net_mount(prefix, url, account) },
|
|
612
|
-
// Deprecated
|
|
613
|
-
universal_ws_client: function () {
|
|
614
|
-
console.error('calling universal_ws_client is deprecated and no longer necessary') },
|
|
615
|
-
|
|
616
566
|
file_store: (function () {
|
|
617
567
|
// Make a database
|
|
618
568
|
var fs = require('fs')
|
|
@@ -631,9 +581,9 @@ function import_server (bus, options)
|
|
|
631
581
|
(fs.writeFileSync(filename, '{}'), bus.log('Made a new db file'))
|
|
632
582
|
db = JSON.parse(fs.readFileSync(filename))
|
|
633
583
|
db_is_ok = true
|
|
634
|
-
// If we
|
|
584
|
+
// If we set before anything else is connected, we'll get this
|
|
635
585
|
// into the cache but not affect anything else
|
|
636
|
-
bus.
|
|
586
|
+
bus.set.fire(global.pointerify ? inline_pointers(db) : db)
|
|
637
587
|
bus.log('Read db')
|
|
638
588
|
} catch (e) {
|
|
639
589
|
console.error(e)
|
|
@@ -644,7 +594,7 @@ function import_server (bus, options)
|
|
|
644
594
|
function save_db() {
|
|
645
595
|
if (!db_is_ok) return
|
|
646
596
|
|
|
647
|
-
console.time('saved db')
|
|
597
|
+
// console.time('saved db')
|
|
648
598
|
|
|
649
599
|
fs.writeFile(filename+'.tmp', JSON.stringify(db, null, 1), function(err) {
|
|
650
600
|
if (err) {
|
|
@@ -656,7 +606,7 @@ function import_server (bus, options)
|
|
|
656
606
|
console.error('Crap !! DB IS DYING !!!!', err)
|
|
657
607
|
db_is_ok = false
|
|
658
608
|
} else {
|
|
659
|
-
console.timeEnd('saved db')
|
|
609
|
+
// console.timeEnd('saved db')
|
|
660
610
|
pending_save = null
|
|
661
611
|
}
|
|
662
612
|
})
|
|
@@ -687,12 +637,12 @@ function import_server (bus, options)
|
|
|
687
637
|
else return o
|
|
688
638
|
})
|
|
689
639
|
}
|
|
690
|
-
function
|
|
640
|
+
function on_set (obj) {
|
|
691
641
|
db[obj.key] = global.pointerify ? abstract_pointers(obj) : obj
|
|
692
642
|
if (active) save_later()
|
|
693
643
|
}
|
|
694
|
-
|
|
695
|
-
bus(prefix).
|
|
644
|
+
on_set.priority = true
|
|
645
|
+
bus(prefix).on_set = on_set
|
|
696
646
|
bus(prefix).to_delete = function (key) {
|
|
697
647
|
delete db[key]
|
|
698
648
|
if (active) save_later()
|
|
@@ -759,17 +709,17 @@ function import_server (bus, options)
|
|
|
759
709
|
return decodeURIComponent(k.replace('%2E', '.'))
|
|
760
710
|
}
|
|
761
711
|
|
|
762
|
-
bus(prefix).
|
|
712
|
+
bus(prefix).to_get = function (key, t) {
|
|
763
713
|
firebase_ref.child(encode_firebase_key(key)).on('value', function (x) {
|
|
764
714
|
t.done(x.val() || {})
|
|
765
715
|
}, function (err) { t.abort() })
|
|
766
716
|
}
|
|
767
717
|
|
|
768
|
-
bus(prefix).
|
|
718
|
+
bus(prefix).on_set = function (o) {
|
|
769
719
|
firebase_ref.child(encode_firebase_key(o.key)).set(o)
|
|
770
720
|
}
|
|
771
721
|
|
|
772
|
-
// bus(prefix).
|
|
722
|
+
// bus(prefix).to_set = function (o, t) {
|
|
773
723
|
// firebase_ref.child(encode_firebase_key(o.key)).set(o, (err) => {
|
|
774
724
|
// err ? t.abort() : t.done()
|
|
775
725
|
// })
|
|
@@ -786,7 +736,7 @@ function import_server (bus, options)
|
|
|
786
736
|
}
|
|
787
737
|
},
|
|
788
738
|
|
|
789
|
-
|
|
739
|
+
db_get: function db_get (key) {/* does nothing unless overridden */},
|
|
790
740
|
lazy_sqlite_store: function lazy_sqlite_store (opts) {
|
|
791
741
|
if (!opts) opts = {}
|
|
792
742
|
opts.lazy = true
|
|
@@ -838,7 +788,7 @@ function import_server (bus, options)
|
|
|
838
788
|
if (options.dont_fire)
|
|
839
789
|
bus.cache[key] = temp_db[key]
|
|
840
790
|
else
|
|
841
|
-
bus.
|
|
791
|
+
bus.set.fire(temp_db[key])
|
|
842
792
|
temp_db[key] = undefined
|
|
843
793
|
}
|
|
844
794
|
}
|
|
@@ -856,10 +806,10 @@ function import_server (bus, options)
|
|
|
856
806
|
return x ? JSON.parse(x.obj) : {}
|
|
857
807
|
}
|
|
858
808
|
if (opts.lazy) {
|
|
859
|
-
var
|
|
860
|
-
bus.
|
|
861
|
-
if (
|
|
862
|
-
|
|
809
|
+
var db_getted_keys = {}
|
|
810
|
+
bus.db_get = bus.reactive(function (key) {
|
|
811
|
+
if (db_getted_keys[key]) return
|
|
812
|
+
db_getted_keys[key] = true
|
|
863
813
|
|
|
864
814
|
// Get it from the database
|
|
865
815
|
var obj = sqlite_get(key)
|
|
@@ -873,12 +823,12 @@ function import_server (bus, options)
|
|
|
873
823
|
: o))
|
|
874
824
|
|
|
875
825
|
// Publish this to the bus
|
|
876
|
-
bus.
|
|
826
|
+
bus.set.fire(obj, {to_get: true})
|
|
877
827
|
})
|
|
878
828
|
}
|
|
879
829
|
|
|
880
|
-
// Add
|
|
881
|
-
function
|
|
830
|
+
// Add set handlers
|
|
831
|
+
function on_set (obj) {
|
|
882
832
|
if (opts.inline_pointers)
|
|
883
833
|
obj = abstract_pointers(obj)
|
|
884
834
|
|
|
@@ -899,14 +849,14 @@ function import_server (bus, options)
|
|
|
899
849
|
})
|
|
900
850
|
}
|
|
901
851
|
}
|
|
902
|
-
if (opts.
|
|
852
|
+
if (opts.set_sync) {
|
|
903
853
|
var old_route = bus.route
|
|
904
854
|
bus.route = function (key, method, arg, t) {
|
|
905
|
-
if (method === '
|
|
855
|
+
if (method === 'to_set') on_set(arg)
|
|
906
856
|
return old_route(key, method, arg, t)
|
|
907
857
|
}
|
|
908
858
|
} else
|
|
909
|
-
bus(prefix).on_set_sync =
|
|
859
|
+
bus(prefix).on_set_sync = on_set
|
|
910
860
|
|
|
911
861
|
bus(prefix).to_delete = function (key) {
|
|
912
862
|
if (opts.use_transactions && !open_transaction){
|
|
@@ -983,12 +933,12 @@ function import_server (bus, options)
|
|
|
983
933
|
try {
|
|
984
934
|
var db = new require('pg-native')()
|
|
985
935
|
bus.pg_db = db
|
|
986
|
-
bus.
|
|
936
|
+
bus.pg_set = pg_set
|
|
987
937
|
db.connectSync(opts.url)
|
|
988
938
|
db.querySync('create table if not exists store (key text primary key, value jsonb)')
|
|
989
939
|
|
|
990
940
|
var rows = db.querySync('select * from store')
|
|
991
|
-
rows.forEach(r => bus.
|
|
941
|
+
rows.forEach(r => bus.set(inline_pointers(r.value, bus)))
|
|
992
942
|
|
|
993
943
|
bus.log('Read ' + opts.url)
|
|
994
944
|
} catch (e) {
|
|
@@ -996,16 +946,16 @@ function import_server (bus, options)
|
|
|
996
946
|
console.error('Bad pg db')
|
|
997
947
|
}
|
|
998
948
|
|
|
999
|
-
// Add
|
|
1000
|
-
function
|
|
949
|
+
// Add set handlers
|
|
950
|
+
function pg_set (obj) {
|
|
1001
951
|
obj = abstract_pointers(obj)
|
|
1002
952
|
|
|
1003
953
|
db.querySync('insert into store (key, value) values ($1, $2) '
|
|
1004
954
|
+ 'on conflict (key) do update set value = $2',
|
|
1005
955
|
[obj.key, JSON.stringify(obj)])
|
|
1006
956
|
}
|
|
1007
|
-
|
|
1008
|
-
bus(opts.prefix).
|
|
957
|
+
pg_set.priority = true
|
|
958
|
+
bus(opts.prefix).on_set = pg_set
|
|
1009
959
|
bus(opts.prefix).to_delete = function (key) {
|
|
1010
960
|
db.query('delete from store where key = $1', [key])
|
|
1011
961
|
}
|
|
@@ -1066,8 +1016,8 @@ function import_server (bus, options)
|
|
|
1066
1016
|
bus.usage_log_nots = nots
|
|
1067
1017
|
|
|
1068
1018
|
// Aggregate all accesses by day, to get daily active users
|
|
1069
|
-
bus('usage').
|
|
1070
|
-
bus.
|
|
1019
|
+
bus('usage').to_get = () => {
|
|
1020
|
+
bus.get('time/' + refresh_interval)
|
|
1071
1021
|
var days = []
|
|
1072
1022
|
var last_day
|
|
1073
1023
|
for (var row of db.prepare('select * from usage where '
|
|
@@ -1105,8 +1055,8 @@ function import_server (bus, options)
|
|
|
1105
1055
|
return {_: days}
|
|
1106
1056
|
}
|
|
1107
1057
|
|
|
1108
|
-
bus('recent_hits/*').
|
|
1109
|
-
bus.
|
|
1058
|
+
bus('recent_hits/*').to_get = (rest) => {
|
|
1059
|
+
bus.get('time/' + refresh_interval)
|
|
1110
1060
|
var result = []
|
|
1111
1061
|
for (var row of db.prepare('select * from usage where '
|
|
1112
1062
|
+ nots + ' order by date desc limit ?').iterate(
|
|
@@ -1121,8 +1071,8 @@ function import_server (bus, options)
|
|
|
1121
1071
|
return {_: result}
|
|
1122
1072
|
}
|
|
1123
1073
|
|
|
1124
|
-
bus('recent_referers/*').
|
|
1125
|
-
bus.
|
|
1074
|
+
bus('recent_referers/*').to_get = (rest) => {
|
|
1075
|
+
bus.get('time/' + refresh_interval)
|
|
1126
1076
|
var result = []
|
|
1127
1077
|
for (var row of db.prepare('select * from usage where '
|
|
1128
1078
|
+ nots + ' order by date desc limit ?').iterate(
|
|
@@ -1170,7 +1120,7 @@ function import_server (bus, options)
|
|
|
1170
1120
|
return times
|
|
1171
1121
|
}
|
|
1172
1122
|
|
|
1173
|
-
bus('socket_load_times').
|
|
1123
|
+
bus('socket_load_times').to_get = () => {
|
|
1174
1124
|
return {_: sock_open_times()}
|
|
1175
1125
|
}
|
|
1176
1126
|
},
|
|
@@ -1181,26 +1131,26 @@ function import_server (bus, options)
|
|
|
1181
1131
|
JSON.stringify(details)])
|
|
1182
1132
|
},
|
|
1183
1133
|
|
|
1184
|
-
|
|
1185
|
-
if (bus('time*').
|
|
1134
|
+
time () {
|
|
1135
|
+
if (bus('time*').to_get.length > 0)
|
|
1186
1136
|
// Then it's already installed
|
|
1187
1137
|
return
|
|
1188
1138
|
|
|
1189
1139
|
// Time ticker
|
|
1190
1140
|
var timeouts = {}
|
|
1191
|
-
bus('time*'
|
|
1192
|
-
|
|
1141
|
+
bus('time*', {
|
|
1142
|
+
to_get: (key, rest, t) => {
|
|
1193
1143
|
if (key === 'time') rest = '/1000'
|
|
1194
1144
|
timeout = parseInt(rest.substr(1))
|
|
1195
|
-
function f () { bus.
|
|
1145
|
+
function f () { bus.set.fire({key: key, val: Date.now()}) }
|
|
1196
1146
|
timeouts[key] = setInterval(f, timeout)
|
|
1197
1147
|
f()
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
function (key, rest) {
|
|
1148
|
+
},
|
|
1149
|
+
to_forget: (key, rest) => {
|
|
1201
1150
|
if (key === 'time') key = 'time/1000'
|
|
1202
1151
|
clearTimeout(timeouts[key])
|
|
1203
1152
|
}
|
|
1153
|
+
})
|
|
1204
1154
|
},
|
|
1205
1155
|
|
|
1206
1156
|
smtp (opts) {
|
|
@@ -1250,7 +1200,7 @@ function import_server (bus, options)
|
|
|
1250
1200
|
}
|
|
1251
1201
|
}
|
|
1252
1202
|
|
|
1253
|
-
bus.
|
|
1203
|
+
bus.set(email)
|
|
1254
1204
|
})
|
|
1255
1205
|
}
|
|
1256
1206
|
},
|
|
@@ -1267,8 +1217,8 @@ function import_server (bus, options)
|
|
|
1267
1217
|
// - Is there security hole if users have a ? or a / in their name?
|
|
1268
1218
|
// - Make the master.posts/ state not require /
|
|
1269
1219
|
// - Make standard url tools for optional slashes, and ? query params
|
|
1270
|
-
// - Should a e.g. client
|
|
1271
|
-
// - e.g. if master('posts*').
|
|
1220
|
+
// - Should a e.g. client to_set abort if it calls set that aborts?
|
|
1221
|
+
// - e.g. if master('posts*').to_set aborts
|
|
1272
1222
|
|
|
1273
1223
|
// Helpers
|
|
1274
1224
|
function get_posts (args) {
|
|
@@ -1323,15 +1273,15 @@ function import_server (bus, options)
|
|
|
1323
1273
|
// }
|
|
1324
1274
|
|
|
1325
1275
|
// Define state on master
|
|
1326
|
-
if (master('posts_for/*').
|
|
1276
|
+
if (master('posts_for/*').to_get.length === 0) {
|
|
1327
1277
|
// Get posts for each user
|
|
1328
|
-
master('posts_for/*').
|
|
1278
|
+
master('posts_for/*').to_get = (json) => {
|
|
1329
1279
|
watch_for_dirt('posts-for/' + json.for)
|
|
1330
1280
|
return {_: get_posts(json)}
|
|
1331
1281
|
}
|
|
1332
1282
|
// Saving any post will dirty the list for all users mentioned in
|
|
1333
1283
|
// the post
|
|
1334
|
-
master('post/*').
|
|
1284
|
+
master('post/*').to_set = (old, New, t) => {
|
|
1335
1285
|
// To do: diff the cc, to, and from lists, and only dirty
|
|
1336
1286
|
// posts_for people who have been changed
|
|
1337
1287
|
|
|
@@ -1360,38 +1310,38 @@ function import_server (bus, options)
|
|
|
1360
1310
|
return u.name + '@' + opts.domain
|
|
1361
1311
|
}
|
|
1362
1312
|
function current_addy () {
|
|
1363
|
-
var c = client.
|
|
1313
|
+
var c = client.get('current_user')
|
|
1364
1314
|
return c.logged_in ? user_addy(c.user) : 'public'
|
|
1365
1315
|
}
|
|
1366
1316
|
function is_author (post) {
|
|
1367
1317
|
var from = post._.from
|
|
1368
|
-
var c = client.
|
|
1318
|
+
var c = client.get('current_user')
|
|
1369
1319
|
return from.includes(current_addy())
|
|
1370
1320
|
}
|
|
1371
1321
|
function can_see (post) {
|
|
1372
|
-
var c = client.
|
|
1322
|
+
var c = client.get('current_user')
|
|
1373
1323
|
var allowed = post._.to.concat(post._.cc).concat(post._.from)
|
|
1374
1324
|
return allowed.includes(current_addy()) || allowed.includes('public')
|
|
1375
1325
|
}
|
|
1376
1326
|
var drtbus = master//require('./statebus')()
|
|
1377
|
-
function dirty (key) { drtbus.
|
|
1378
|
-
function watch_for_dirt (key) { drtbus.
|
|
1327
|
+
function dirty (key) { drtbus.set({key: 'dirty-'+key, n: Math.random()}) }
|
|
1328
|
+
function watch_for_dirt (key) { drtbus.get('dirty-'+key) }
|
|
1379
1329
|
|
|
1380
|
-
client('current_email').
|
|
1330
|
+
client('current_email').to_get = () => {
|
|
1381
1331
|
return {_: current_addy()}
|
|
1382
1332
|
}
|
|
1383
1333
|
|
|
1384
1334
|
// Define state on client
|
|
1385
|
-
client('posts*').
|
|
1335
|
+
client('posts*').to_get = (k, rest) => {
|
|
1386
1336
|
var args = bus.parse(rest.substr(1))
|
|
1387
|
-
var c = client.
|
|
1337
|
+
var c = client.get('current_user')
|
|
1388
1338
|
args.for = current_addy()
|
|
1389
|
-
var e = master.
|
|
1390
|
-
return {_: master.clone(e._).map(x=>client.
|
|
1339
|
+
var e = master.get('posts_for/' + JSON.stringify(args))
|
|
1340
|
+
return {_: master.clone(e._).map(x=>client.get(x.key))}
|
|
1391
1341
|
}
|
|
1392
1342
|
|
|
1393
|
-
client('post/*').
|
|
1394
|
-
var e = master.clone(master.
|
|
1343
|
+
client('post/*').to_get = (k) => {
|
|
1344
|
+
var e = master.clone(master.get(k))
|
|
1395
1345
|
if (!e._) return {}
|
|
1396
1346
|
if (!can_see(e))
|
|
1397
1347
|
return {}
|
|
@@ -1402,7 +1352,7 @@ function import_server (bus, options)
|
|
|
1402
1352
|
return e
|
|
1403
1353
|
}
|
|
1404
1354
|
|
|
1405
|
-
client('post/*').
|
|
1355
|
+
client('post/*').to_set = (o, t) => {
|
|
1406
1356
|
if (!(client.validate(o, {key: 'string',
|
|
1407
1357
|
_: {to: 'array',
|
|
1408
1358
|
cc: 'array',
|
|
@@ -1420,7 +1370,7 @@ function import_server (bus, options)
|
|
|
1420
1370
|
return
|
|
1421
1371
|
}
|
|
1422
1372
|
|
|
1423
|
-
var c = client.
|
|
1373
|
+
var c = client.get('current_user')
|
|
1424
1374
|
|
|
1425
1375
|
// Make sure this user is an author
|
|
1426
1376
|
var from = o._.from
|
|
@@ -1433,7 +1383,7 @@ function import_server (bus, options)
|
|
|
1433
1383
|
o = client.clone(o)
|
|
1434
1384
|
delete o.children
|
|
1435
1385
|
|
|
1436
|
-
master.
|
|
1386
|
+
master.set(o)
|
|
1437
1387
|
t.done()
|
|
1438
1388
|
}
|
|
1439
1389
|
|
|
@@ -1461,21 +1411,21 @@ function import_server (bus, options)
|
|
|
1461
1411
|
t.done()
|
|
1462
1412
|
}
|
|
1463
1413
|
|
|
1464
|
-
client('friends').
|
|
1465
|
-
return {_: (master.
|
|
1466
|
-
.map(u=>client.
|
|
1467
|
-
.concat([client.
|
|
1414
|
+
client('friends').to_get = t => {
|
|
1415
|
+
return {_: (master.get('users').all||[])
|
|
1416
|
+
.map(u=>client.get('email/' + user_addy(u)))
|
|
1417
|
+
.concat([client.get('email/public')])
|
|
1468
1418
|
}
|
|
1469
1419
|
}
|
|
1470
1420
|
|
|
1471
|
-
client('email/*').
|
|
1421
|
+
client('email/*').to_get = (rest) => {
|
|
1472
1422
|
var m = rest.match(email_regex)
|
|
1473
1423
|
var result = {address: rest}
|
|
1474
1424
|
if (rest === 'public') {
|
|
1475
1425
|
result.name = 'public'
|
|
1476
1426
|
}
|
|
1477
1427
|
else if (m && m[5].toLowerCase() === opts.domain) {
|
|
1478
|
-
result.user = client.
|
|
1428
|
+
result.user = client.get('user/' + m[1])
|
|
1479
1429
|
result.name = result.user.name
|
|
1480
1430
|
result.pic = result.user.pic
|
|
1481
1431
|
result.upgraded = true
|
|
@@ -1490,14 +1440,14 @@ function import_server (bus, options)
|
|
|
1490
1440
|
},
|
|
1491
1441
|
|
|
1492
1442
|
sqlite_query_server: function sqlite_query_server (db) {
|
|
1493
|
-
var
|
|
1494
|
-
bus('table_columns/*').
|
|
1495
|
-
function
|
|
1443
|
+
var get = bus.get
|
|
1444
|
+
bus('table_columns/*').to_get =
|
|
1445
|
+
function get_table_columns (key, rest) {
|
|
1496
1446
|
if (typeof key !== 'string')
|
|
1497
1447
|
console.log(handlers.hash)
|
|
1498
1448
|
var table_name = rest
|
|
1499
|
-
var columns =
|
|
1500
|
-
var foreign_keys =
|
|
1449
|
+
var columns = get('sql/PRAGMA table_info(' + table_name + ')').rows.slice()
|
|
1450
|
+
var foreign_keys = get('table_foreign_keys/' + table_name)
|
|
1501
1451
|
var column_info = {}
|
|
1502
1452
|
for (var i=0;i< columns .length;i++) {
|
|
1503
1453
|
var col = columns[i].name
|
|
@@ -1514,10 +1464,10 @@ function import_server (bus, options)
|
|
|
1514
1464
|
}
|
|
1515
1465
|
|
|
1516
1466
|
|
|
1517
|
-
bus('table_foreign_keys/*').
|
|
1467
|
+
bus('table_foreign_keys/*').to_get =
|
|
1518
1468
|
function table_foreign_keys (key, rest) {
|
|
1519
1469
|
var table_name = rest
|
|
1520
|
-
var foreign_keys =
|
|
1470
|
+
var foreign_keys = get('sql/PRAGMA foreign_key_list(' + table_name + ')').rows
|
|
1521
1471
|
var result = {}
|
|
1522
1472
|
for (var i=0;i< foreign_keys .length;i++)
|
|
1523
1473
|
result[foreign_keys[i].from] = foreign_keys[i]
|
|
@@ -1526,26 +1476,26 @@ function import_server (bus, options)
|
|
|
1526
1476
|
return result
|
|
1527
1477
|
}
|
|
1528
1478
|
|
|
1529
|
-
bus('sql/*').
|
|
1479
|
+
bus('sql/*').to_get =
|
|
1530
1480
|
function sql (key, rest) {
|
|
1531
|
-
|
|
1481
|
+
get('time/60000')
|
|
1532
1482
|
var query = rest
|
|
1533
1483
|
try { query = JSON.parse(query) }
|
|
1534
1484
|
catch (e) { query = {stmt: query, args: []} }
|
|
1535
1485
|
|
|
1536
1486
|
db.all(query.stmt, query.args,
|
|
1537
1487
|
function (err, rows) {
|
|
1538
|
-
if (rows) bus.
|
|
1488
|
+
if (rows) bus.set.fire({key:key, rows: rows})
|
|
1539
1489
|
else console.error('Bad sqlite query', key, err)
|
|
1540
1490
|
}.bind(this))
|
|
1541
1491
|
}
|
|
1542
1492
|
},
|
|
1543
1493
|
|
|
1544
1494
|
sqlite_table_server: function sqlite_table_server(db, table_name) {
|
|
1545
|
-
var
|
|
1546
|
-
var table_columns =
|
|
1547
|
-
var foreign_keys =
|
|
1548
|
-
var remapped_keys =
|
|
1495
|
+
var set = bus.set, get = bus.get
|
|
1496
|
+
var table_columns = get('table_columns/'+table_name) // This will fail if used too soon
|
|
1497
|
+
var foreign_keys = get('table_foreign_keys/'+table_name)
|
|
1498
|
+
var remapped_keys = get('remapped_keys')
|
|
1549
1499
|
remapped_keys.keys = remapped_keys.keys || {}
|
|
1550
1500
|
remapped_keys.revs = remapped_keys.revs || {}
|
|
1551
1501
|
function row_json (row) {
|
|
@@ -1586,15 +1536,15 @@ function import_server (bus, options)
|
|
|
1586
1536
|
if (err) console.error('Problem with table!', table_name, err)
|
|
1587
1537
|
for (var i=0; i<rows.length; i++)
|
|
1588
1538
|
result[i] = row_json(rows[i])
|
|
1589
|
-
bus.
|
|
1539
|
+
bus.set.fire({key: table_name, rows: result})
|
|
1590
1540
|
})
|
|
1591
1541
|
}
|
|
1592
1542
|
function render_row(obj) {
|
|
1593
|
-
bus.
|
|
1543
|
+
bus.set.fire(row_json(obj))
|
|
1594
1544
|
if (remapped_keys.revs[obj.key]) {
|
|
1595
1545
|
var alias = bus.clone(obj)
|
|
1596
1546
|
alias.key = remapped_keys.revs[obj.key]
|
|
1597
|
-
bus.
|
|
1547
|
+
bus.set.fire(row_json(alias))
|
|
1598
1548
|
}
|
|
1599
1549
|
}
|
|
1600
1550
|
|
|
@@ -1602,8 +1552,8 @@ function import_server (bus, options)
|
|
|
1602
1552
|
// Handlers!
|
|
1603
1553
|
// ************************
|
|
1604
1554
|
|
|
1605
|
-
//
|
|
1606
|
-
bus(table_name + '*').
|
|
1555
|
+
// Getting the whole table, or a single row
|
|
1556
|
+
bus(table_name + '*').to_get = function (key, rest) {
|
|
1607
1557
|
if (rest === '')
|
|
1608
1558
|
// Return the whole table
|
|
1609
1559
|
return render_table()
|
|
@@ -1623,7 +1573,7 @@ function import_server (bus, options)
|
|
|
1623
1573
|
}
|
|
1624
1574
|
|
|
1625
1575
|
// Saving a row
|
|
1626
|
-
bus(table_name + '/*').
|
|
1576
|
+
bus(table_name + '/*').to_set = function (obj, rest) {
|
|
1627
1577
|
var columns = table_columns.columns
|
|
1628
1578
|
var key = remapped_keys.keys[obj.key] || obj.key
|
|
1629
1579
|
|
|
@@ -1649,7 +1599,7 @@ function import_server (bus, options)
|
|
|
1649
1599
|
}
|
|
1650
1600
|
|
|
1651
1601
|
// Inserting a new row
|
|
1652
|
-
bus('new/' + table_name + '/*').
|
|
1602
|
+
bus('new/' + table_name + '/*').to_set = function (obj) {
|
|
1653
1603
|
var columns = table_columns.columns
|
|
1654
1604
|
var stmt = ('insert into ' + table_name + ' (' + columns.join(',')
|
|
1655
1605
|
+ ') values (' + new Array(columns.length).join('?,') + '?)')
|
|
@@ -1662,7 +1612,7 @@ function import_server (bus, options)
|
|
|
1662
1612
|
console.log('insert complete, got id', this.lastID)
|
|
1663
1613
|
remapped_keys.keys[obj.key] = table_name + '/' + this.lastID
|
|
1664
1614
|
remapped_keys.revs[remapped_keys.keys[obj.key]] = obj.key
|
|
1665
|
-
|
|
1615
|
+
bus.set(remapped_keys)
|
|
1666
1616
|
render_table()
|
|
1667
1617
|
})
|
|
1668
1618
|
}
|
|
@@ -1692,29 +1642,29 @@ function import_server (bus, options)
|
|
|
1692
1642
|
var client = this // to keep me straight while programming
|
|
1693
1643
|
|
|
1694
1644
|
function logout (key) {
|
|
1695
|
-
var clients = master.
|
|
1645
|
+
var clients = master.get('logged_in_clients')
|
|
1696
1646
|
for (var k in clients)
|
|
1697
1647
|
if (k !== 'key')
|
|
1698
1648
|
if (Object.keys(clients[k]).length === 0) {
|
|
1699
1649
|
client.log('Found a deleted user. Removing.', k, clients[k])
|
|
1700
1650
|
delete clients[k]
|
|
1701
|
-
master.
|
|
1651
|
+
master.set(clients)
|
|
1702
1652
|
} else if (clients[k].key === key) {
|
|
1703
1653
|
client.log('Logging out!', k, clients[k])
|
|
1704
1654
|
delete clients[k]
|
|
1705
|
-
master.
|
|
1655
|
+
master.set(clients)
|
|
1706
1656
|
}
|
|
1707
1657
|
}
|
|
1708
1658
|
|
|
1709
1659
|
// Initialize master
|
|
1710
1660
|
if (!master.auth_initialized) {
|
|
1711
|
-
master('users/passwords').
|
|
1712
|
-
master.log('users/passwords.
|
|
1661
|
+
master('users/passwords').to_get = function (k) {
|
|
1662
|
+
master.log('users/passwords.to_get: Computing!')
|
|
1713
1663
|
var result = {key: 'users/passwords'}
|
|
1714
|
-
var users = master.
|
|
1664
|
+
var users = master.get('users')
|
|
1715
1665
|
users.all = users.all || []
|
|
1716
1666
|
for (var i=0; i<users.all.length; i++) {
|
|
1717
|
-
var u = master.
|
|
1667
|
+
var u = master.get(users.all[i])
|
|
1718
1668
|
if (!(u.login || u.name)) {
|
|
1719
1669
|
console.error('upass: this user has bogus name/login', u.key, u.name, u.login)
|
|
1720
1670
|
continue
|
|
@@ -1730,14 +1680,14 @@ function import_server (bus, options)
|
|
|
1730
1680
|
return result
|
|
1731
1681
|
}
|
|
1732
1682
|
master.auth_initialized = true
|
|
1733
|
-
master.
|
|
1683
|
+
master.get('users/passwords')
|
|
1734
1684
|
|
|
1735
1685
|
master('user/*').to_delete = (key, t) => {
|
|
1736
1686
|
master.log('Deleteinggg!!!', key)
|
|
1737
1687
|
// Remove from users.all
|
|
1738
|
-
var users = master.
|
|
1688
|
+
var users = master.get('users')
|
|
1739
1689
|
users.all = users.all.filter(u => u.key && u.key !== key)
|
|
1740
|
-
master.
|
|
1690
|
+
master.set(users)
|
|
1741
1691
|
|
|
1742
1692
|
// Log out
|
|
1743
1693
|
master.log('Logging out', key)
|
|
@@ -1745,14 +1695,14 @@ function import_server (bus, options)
|
|
|
1745
1695
|
|
|
1746
1696
|
// Remove connection
|
|
1747
1697
|
master.log('Removing connections for', key)
|
|
1748
|
-
var conns = master.
|
|
1698
|
+
var conns = master.get('connections')
|
|
1749
1699
|
for (var k in conns) {
|
|
1750
1700
|
console.log('Trying key', k)
|
|
1751
1701
|
if (k !== 'key')
|
|
1752
1702
|
if (conns[k].user && !conns[k].user.key) {
|
|
1753
1703
|
console.log('Cleaning keyless user', conss[k].user)
|
|
1754
1704
|
delete conns[k].user
|
|
1755
|
-
master.
|
|
1705
|
+
master.set(conns)
|
|
1756
1706
|
continue
|
|
1757
1707
|
}
|
|
1758
1708
|
}
|
|
@@ -1767,9 +1717,9 @@ function import_server (bus, options)
|
|
|
1767
1717
|
|
|
1768
1718
|
// Authentication functions
|
|
1769
1719
|
function authenticate (login, pass) {
|
|
1770
|
-
var userpass = master.
|
|
1720
|
+
var userpass = master.get('users/passwords')[login.toLowerCase()]
|
|
1771
1721
|
master.log('authenticate: we see', {
|
|
1772
|
-
passwords: master.
|
|
1722
|
+
passwords: master.get('users/passwords'),
|
|
1773
1723
|
hash_to_match: userpass && userpass.pass,
|
|
1774
1724
|
password_guess: pass
|
|
1775
1725
|
})
|
|
@@ -1780,7 +1730,7 @@ function import_server (bus, options)
|
|
|
1780
1730
|
|
|
1781
1731
|
// console.log('comparing passwords', pass, userpass.pass)
|
|
1782
1732
|
if (require('bcrypt-nodejs').compareSync(pass, userpass.pass))
|
|
1783
|
-
return master.
|
|
1733
|
+
return master.get(userpass.user)
|
|
1784
1734
|
}
|
|
1785
1735
|
function create_account (params) {
|
|
1786
1736
|
if (typeof (params.login || params.name) !== 'string')
|
|
@@ -1792,7 +1742,7 @@ function import_server (bus, options)
|
|
|
1792
1742
|
'?key': undefined, '*': '*'}))
|
|
1793
1743
|
throw 'invalid name, login, pass, or email'
|
|
1794
1744
|
|
|
1795
|
-
var passes = master.
|
|
1745
|
+
var passes = master.get('users/passwords')
|
|
1796
1746
|
if (passes.hasOwnProperty(login))
|
|
1797
1747
|
throw 'there is already a user with that login or name'
|
|
1798
1748
|
|
|
@@ -1802,7 +1752,7 @@ function import_server (bus, options)
|
|
|
1802
1752
|
// Choose account key
|
|
1803
1753
|
//
|
|
1804
1754
|
// NOTE: Checking if master.cache has the key probably sucks,
|
|
1805
|
-
// because if something
|
|
1755
|
+
// because if something gets that key (hoping it exists), it
|
|
1806
1756
|
// might create it, even though the account hasn't been created
|
|
1807
1757
|
// yet, and then this will rename it to some random ID.
|
|
1808
1758
|
//
|
|
@@ -1819,31 +1769,31 @@ function import_server (bus, options)
|
|
|
1819
1769
|
pass: params.pass,
|
|
1820
1770
|
email: params.email }
|
|
1821
1771
|
for (var k in new_account) if (!new_account[k]) delete new_account[k]
|
|
1822
|
-
master.
|
|
1772
|
+
master.set(new_account)
|
|
1823
1773
|
|
|
1824
|
-
var users = master.
|
|
1774
|
+
var users = master.get('users')
|
|
1825
1775
|
users.all = users.all || []
|
|
1826
1776
|
users.all.push(new_account)
|
|
1827
1777
|
passes[login] = {user: new_account.key, pass: new_account.pass}
|
|
1828
|
-
master.
|
|
1829
|
-
master.
|
|
1778
|
+
master.set(users)
|
|
1779
|
+
master.set(passes)
|
|
1830
1780
|
}
|
|
1831
1781
|
|
|
1832
1782
|
// Current User
|
|
1833
|
-
client('current_user').
|
|
1834
|
-
client.log('*
|
|
1783
|
+
client('current_user').to_get = function (k) {
|
|
1784
|
+
client.log('* getting: current_user')
|
|
1835
1785
|
if (!conn.client) return
|
|
1836
|
-
var u = master.
|
|
1786
|
+
var u = master.get('logged_in_clients')[conn.client]
|
|
1837
1787
|
u = u && user_obj(u.key, true)
|
|
1838
1788
|
return {user: u || null, logged_in: !!u}
|
|
1839
1789
|
}
|
|
1840
1790
|
|
|
1841
|
-
client('current_user').
|
|
1791
|
+
client('current_user').to_set = function (o, t) {
|
|
1842
1792
|
function error (msg) {
|
|
1843
|
-
client.
|
|
1844
|
-
var c = client.
|
|
1793
|
+
client.set.abort(o)
|
|
1794
|
+
var c = client.get('current_user')
|
|
1845
1795
|
c.error = msg
|
|
1846
|
-
client.
|
|
1796
|
+
client.set.fire(c)
|
|
1847
1797
|
}
|
|
1848
1798
|
|
|
1849
1799
|
client.log('* saving: current_user!')
|
|
@@ -1854,9 +1804,9 @@ function import_server (bus, options)
|
|
|
1854
1804
|
client.client_ip = conn.remoteAddress
|
|
1855
1805
|
|
|
1856
1806
|
if (conn.id) {
|
|
1857
|
-
var connections = master.
|
|
1858
|
-
connections[conn.id].user = master.
|
|
1859
|
-
master.
|
|
1807
|
+
var connections = master.get('connections')
|
|
1808
|
+
connections[conn.id].user = master.get('logged_in_clients')[conn.client]
|
|
1809
|
+
master.set(connections)
|
|
1860
1810
|
}
|
|
1861
1811
|
}
|
|
1862
1812
|
else {
|
|
@@ -1865,9 +1815,9 @@ function import_server (bus, options)
|
|
|
1865
1815
|
try {
|
|
1866
1816
|
create_account(o.create_account)
|
|
1867
1817
|
client.log('Success creating account!')
|
|
1868
|
-
var cu = client.
|
|
1818
|
+
var cu = client.get('current_user')
|
|
1869
1819
|
cu.create_account = null
|
|
1870
|
-
client.
|
|
1820
|
+
client.set.fire(cu)
|
|
1871
1821
|
} catch (e) {
|
|
1872
1822
|
error('Cannot create that account because ' + e)
|
|
1873
1823
|
return
|
|
@@ -1889,14 +1839,14 @@ function import_server (bus, options)
|
|
|
1889
1839
|
// Associate this user with this session
|
|
1890
1840
|
// user.log('Logging the user in!', u)
|
|
1891
1841
|
|
|
1892
|
-
var clients = master.
|
|
1893
|
-
var connections = master.
|
|
1842
|
+
var clients = master.get('logged_in_clients')
|
|
1843
|
+
var connections = master.get('connections')
|
|
1894
1844
|
|
|
1895
1845
|
clients[conn.client] = u
|
|
1896
1846
|
connections[conn.id].user = u
|
|
1897
1847
|
|
|
1898
|
-
master.
|
|
1899
|
-
master.
|
|
1848
|
+
master.set(clients)
|
|
1849
|
+
master.set(connections)
|
|
1900
1850
|
|
|
1901
1851
|
client.log('current_user: success logging in!')
|
|
1902
1852
|
}
|
|
@@ -1913,18 +1863,18 @@ function import_server (bus, options)
|
|
|
1913
1863
|
|
|
1914
1864
|
else if (o.logout && conn.id) {
|
|
1915
1865
|
client.log('current_user: logging out')
|
|
1916
|
-
var clients = master.
|
|
1917
|
-
var connections = master.
|
|
1866
|
+
var clients = master.get('logged_in_clients')
|
|
1867
|
+
var connections = master.get('connections')
|
|
1918
1868
|
|
|
1919
1869
|
delete clients[conn.client]
|
|
1920
1870
|
connections[conn.id].user = null
|
|
1921
1871
|
|
|
1922
|
-
master.
|
|
1923
|
-
master.
|
|
1872
|
+
master.set(clients)
|
|
1873
|
+
master.set(connections)
|
|
1924
1874
|
}
|
|
1925
1875
|
}
|
|
1926
1876
|
|
|
1927
|
-
t.
|
|
1877
|
+
t.reget()
|
|
1928
1878
|
}
|
|
1929
1879
|
client('current_user').to_delete = function () {}
|
|
1930
1880
|
|
|
@@ -1933,8 +1883,8 @@ function import_server (bus, options)
|
|
|
1933
1883
|
var private_closet_space_key = /^user\/[^\/]+\/private.*/
|
|
1934
1884
|
|
|
1935
1885
|
// User
|
|
1936
|
-
client('user/*').
|
|
1937
|
-
var c = client.
|
|
1886
|
+
client('user/*').to_set = function (o, t) {
|
|
1887
|
+
var c = client.get('current_user')
|
|
1938
1888
|
var user_key = o.key.match(/^user\/([^\/]+)/)
|
|
1939
1889
|
user_key = user_key && ('user/' + user_key[1])
|
|
1940
1890
|
|
|
@@ -1943,14 +1893,14 @@ function import_server (bus, options)
|
|
|
1943
1893
|
client.log('Only the current user can touch themself.',
|
|
1944
1894
|
{logged_in: c.logged_in, as: c.user && c.user.key,
|
|
1945
1895
|
touching: user_key})
|
|
1946
|
-
client.
|
|
1896
|
+
client.set.abort(o)
|
|
1947
1897
|
return
|
|
1948
1898
|
}
|
|
1949
1899
|
|
|
1950
1900
|
// Users have closet space at /user/<name>/*
|
|
1951
1901
|
if (o.key.match(closet_space_key)) {
|
|
1952
1902
|
client.log('saving closet data')
|
|
1953
|
-
master.
|
|
1903
|
+
master.set(o, t)
|
|
1954
1904
|
return
|
|
1955
1905
|
}
|
|
1956
1906
|
|
|
@@ -1962,7 +1912,7 @@ function import_server (bus, options)
|
|
|
1962
1912
|
'?pass': 'string', '?email': 'string', /*'?pic': 'string',*/
|
|
1963
1913
|
'*':'*'})) {
|
|
1964
1914
|
client.log('This user change fails validation.')
|
|
1965
|
-
client.
|
|
1915
|
+
client.set.abort(o)
|
|
1966
1916
|
return
|
|
1967
1917
|
}
|
|
1968
1918
|
|
|
@@ -1974,23 +1924,23 @@ function import_server (bus, options)
|
|
|
1974
1924
|
var login = o.login || o.name
|
|
1975
1925
|
if (!login) {
|
|
1976
1926
|
client.log('User must have a login or a name')
|
|
1977
|
-
client.
|
|
1927
|
+
client.set.abort(o)
|
|
1978
1928
|
return
|
|
1979
1929
|
}
|
|
1980
1930
|
|
|
1981
|
-
var u = master.
|
|
1982
|
-
var userpass = master.
|
|
1931
|
+
var u = master.get(o.key)
|
|
1932
|
+
var userpass = master.get('users/passwords')
|
|
1983
1933
|
|
|
1984
1934
|
// Validate that the login/name is not changed to something clobberish
|
|
1985
1935
|
var old_login = u.login || u.name
|
|
1986
1936
|
if (old_login.toLowerCase() !== login.toLowerCase()
|
|
1987
1937
|
&& userpass.hasOwnProperty(login)) {
|
|
1988
1938
|
client.log('The login', login, 'is already taken. Aborting.')
|
|
1989
|
-
client.
|
|
1939
|
+
client.set.abort(o) // Abort
|
|
1990
1940
|
|
|
1991
|
-
o = client.
|
|
1941
|
+
o = client.get(o.key) // Add error message
|
|
1992
1942
|
o.error = 'The login "' + login + '" is already taken'
|
|
1993
|
-
client.
|
|
1943
|
+
client.set.fire(o)
|
|
1994
1944
|
|
|
1995
1945
|
return // And exit
|
|
1996
1946
|
}
|
|
@@ -2027,11 +1977,11 @@ function import_server (bus, options)
|
|
|
2027
1977
|
if (!protected.hasOwnProperty(k) && !o.hasOwnProperty(k))
|
|
2028
1978
|
delete u[k]
|
|
2029
1979
|
|
|
2030
|
-
master.
|
|
1980
|
+
master.set(u)
|
|
2031
1981
|
}
|
|
2032
|
-
client('user/*').
|
|
2033
|
-
var c = client.
|
|
2034
|
-
client.log('*
|
|
1982
|
+
client('user/*').to_get = function user_getter (k) {
|
|
1983
|
+
var c = client.get('current_user')
|
|
1984
|
+
client.log('* getting:', k, 'as', c.user)
|
|
2035
1985
|
|
|
2036
1986
|
// Users have closet space at /user/<name>/*
|
|
2037
1987
|
if (k.match(closet_space_key)) {
|
|
@@ -2041,8 +1991,8 @@ function import_server (bus, options)
|
|
|
2041
1991
|
client.log('hiding private closet data')
|
|
2042
1992
|
return {}
|
|
2043
1993
|
}
|
|
2044
|
-
client.log('
|
|
2045
|
-
return client.clone(master.
|
|
1994
|
+
client.log('getting closet data')
|
|
1995
|
+
return client.clone(master.get(k))
|
|
2046
1996
|
}
|
|
2047
1997
|
|
|
2048
1998
|
// Otherwise return the actual user
|
|
@@ -2050,7 +2000,7 @@ function import_server (bus, options)
|
|
|
2050
2000
|
}
|
|
2051
2001
|
client('user/*').to_delete = function () {}
|
|
2052
2002
|
function user_obj (k, logged_in) {
|
|
2053
|
-
var o = master.clone(master.
|
|
2003
|
+
var o = master.clone(master.get(k))
|
|
2054
2004
|
if (k.match(/^user\/([^\/]+)\/private\/(.*)$/))
|
|
2055
2005
|
return logged_in ? o : {key: k}
|
|
2056
2006
|
|
|
@@ -2062,8 +2012,8 @@ function import_server (bus, options)
|
|
|
2062
2012
|
// Blacklist sensitive stuff on master, in case we have a shadow set up
|
|
2063
2013
|
var blacklist = 'users users/passwords logged_in_clients'.split(' ')
|
|
2064
2014
|
for (var i=0; i<blacklist.length; i++) {
|
|
2065
|
-
client(blacklist[i]).
|
|
2066
|
-
client(blacklist[i]).
|
|
2015
|
+
client(blacklist[i]).to_get = function () {}
|
|
2016
|
+
client(blacklist[i]).to_set = function () {}
|
|
2067
2017
|
client(blacklist[i]).to_delete = function () {}
|
|
2068
2018
|
client(blacklist[i]).to_forget = function () {}
|
|
2069
2019
|
}
|
|
@@ -2083,24 +2033,24 @@ function import_server (bus, options)
|
|
|
2083
2033
|
var old_prefix = 'client/' + client.client_id
|
|
2084
2034
|
var new_prefix = 'client/' + user.key.substr('user/'.length)
|
|
2085
2035
|
|
|
2086
|
-
var keys = client.master.
|
|
2036
|
+
var keys = client.master.get('persisted_keys/' + client.client_id)
|
|
2087
2037
|
if (!keys.val) return
|
|
2088
2038
|
for (var old_key in keys.val) {
|
|
2089
2039
|
var new_key = new_prefix + old_key.substr(old_prefix.length)
|
|
2090
|
-
var o = client.clone(client.master.
|
|
2040
|
+
var o = client.clone(client.master.get(old_key))
|
|
2091
2041
|
// Delete the old
|
|
2092
2042
|
client.master.del(old_key)
|
|
2093
2043
|
|
|
2094
|
-
var new_o = client.master.
|
|
2044
|
+
var new_o = client.master.get(new_key)
|
|
2095
2045
|
// If the new key doesn't clobber any existing data on the user...
|
|
2096
2046
|
if (Object.keys(new_o).length === 1) {
|
|
2097
|
-
//
|
|
2047
|
+
// Set the new
|
|
2098
2048
|
o.key = new_key
|
|
2099
|
-
client.master.
|
|
2049
|
+
client.master.set(o)
|
|
2100
2050
|
}
|
|
2101
2051
|
}
|
|
2102
2052
|
keys.val = {}
|
|
2103
|
-
client.master.
|
|
2053
|
+
client.master.set(keys)
|
|
2104
2054
|
|
|
2105
2055
|
// var cache = client.master.cache
|
|
2106
2056
|
|
|
@@ -2115,9 +2065,9 @@ function import_server (bus, options)
|
|
|
2115
2065
|
// client.master.del(old_key)
|
|
2116
2066
|
|
|
2117
2067
|
// if (!(cache.hasOwnProperty(new_key))) {
|
|
2118
|
-
// //
|
|
2068
|
+
// // Set the new
|
|
2119
2069
|
// o.key = new_key
|
|
2120
|
-
// client.master.
|
|
2070
|
+
// client.master.set(o)
|
|
2121
2071
|
// }
|
|
2122
2072
|
// }
|
|
2123
2073
|
// }
|
|
@@ -2125,7 +2075,7 @@ function import_server (bus, options)
|
|
|
2125
2075
|
|
|
2126
2076
|
// Copy client to user if we log in
|
|
2127
2077
|
client(_=>{
|
|
2128
|
-
var c = client.
|
|
2078
|
+
var c = client.get('current_user')
|
|
2129
2079
|
if (client.loading()) return
|
|
2130
2080
|
if (was_logged_in == false && c.logged_in)
|
|
2131
2081
|
// User just logged in! Let's copy his stuff over
|
|
@@ -2133,13 +2083,13 @@ function import_server (bus, options)
|
|
|
2133
2083
|
was_logged_in = c.logged_in
|
|
2134
2084
|
})
|
|
2135
2085
|
|
|
2136
|
-
client(prefix_to_sync).
|
|
2137
|
-
var c = client.
|
|
2086
|
+
client(prefix_to_sync).to_get = function (key) {
|
|
2087
|
+
var c = client.get('current_user')
|
|
2138
2088
|
if (client.loading()) return
|
|
2139
2089
|
var prefix = client_prefix(c)
|
|
2140
2090
|
|
|
2141
2091
|
// Get the state from master
|
|
2142
|
-
var obj = client.clone(client.master.
|
|
2092
|
+
var obj = client.clone(client.master.get(prefix + key))
|
|
2143
2093
|
|
|
2144
2094
|
// Translate it back to client
|
|
2145
2095
|
obj = client.deep_map(obj, function (o) {
|
|
@@ -2150,14 +2100,14 @@ function import_server (bus, options)
|
|
|
2150
2100
|
return obj
|
|
2151
2101
|
}
|
|
2152
2102
|
|
|
2153
|
-
client(prefix_to_sync).
|
|
2103
|
+
client(prefix_to_sync).to_set = function (obj) {
|
|
2154
2104
|
if (validate && !validate(obj)) {
|
|
2155
2105
|
console.warn('Validation failed on', obj)
|
|
2156
|
-
client.
|
|
2106
|
+
client.set.abort(obj)
|
|
2157
2107
|
return
|
|
2158
2108
|
}
|
|
2159
2109
|
|
|
2160
|
-
var c = client.
|
|
2110
|
+
var c = client.get('current_user')
|
|
2161
2111
|
if (client.loading()) return
|
|
2162
2112
|
var prefix = client_prefix(c)
|
|
2163
2113
|
|
|
@@ -2173,23 +2123,23 @@ function import_server (bus, options)
|
|
|
2173
2123
|
return o
|
|
2174
2124
|
})
|
|
2175
2125
|
|
|
2176
|
-
//
|
|
2177
|
-
client.master.
|
|
2178
|
-
p_keys && client.master.
|
|
2126
|
+
// Set to master
|
|
2127
|
+
client.master.set(obj)
|
|
2128
|
+
p_keys && client.master.set(p_keys)
|
|
2179
2129
|
}
|
|
2180
2130
|
|
|
2181
2131
|
client(prefix_to_sync).to_delete = function (k) {
|
|
2182
|
-
k = client_prefix(client.
|
|
2132
|
+
k = client_prefix(client.get('current_user')) + k
|
|
2183
2133
|
client.master.delete(k)
|
|
2184
2134
|
|
|
2185
2135
|
var p_keys = client_persisted_keys()
|
|
2186
2136
|
delete p_keys.val[k]
|
|
2187
|
-
client.master.
|
|
2137
|
+
client.master.set(p_keys)
|
|
2188
2138
|
}
|
|
2189
2139
|
|
|
2190
2140
|
function client_persisted_keys () {
|
|
2191
|
-
if (client.
|
|
2192
|
-
var result = client.master.
|
|
2141
|
+
if (client.get('current_user').logged_in) return
|
|
2142
|
+
var result = client.master.get('persisted_keys/' + client.client_id)
|
|
2193
2143
|
if (result && !result.val) result.val = {}
|
|
2194
2144
|
return result
|
|
2195
2145
|
}
|
|
@@ -2204,17 +2154,17 @@ function import_server (bus, options)
|
|
|
2204
2154
|
// to the global cache
|
|
2205
2155
|
if (count === 0) {
|
|
2206
2156
|
count++
|
|
2207
|
-
if (method === '
|
|
2157
|
+
if (method === 'to_get')
|
|
2208
2158
|
bus.run_handler(function get_from_master (k) {
|
|
2209
|
-
// console.log('DEFAULT
|
|
2210
|
-
var r = master_bus.
|
|
2211
|
-
// console.log('DEFAULT
|
|
2212
|
-
bus.
|
|
2159
|
+
// console.log('DEFAULT GETting', k)
|
|
2160
|
+
var r = master_bus.get(k)
|
|
2161
|
+
// console.log('DEFAULT GETted', r)
|
|
2162
|
+
bus.set.fire(r, {version: master_bus.versions[r.key]})
|
|
2213
2163
|
}, method, arg)
|
|
2214
|
-
else if (method === '
|
|
2215
|
-
bus.run_handler(function
|
|
2164
|
+
else if (method === 'to_set')
|
|
2165
|
+
bus.run_handler(function set_to_master (o, t) {
|
|
2216
2166
|
// console.log('DEFAULT ROUTE', t)
|
|
2217
|
-
master_bus.
|
|
2167
|
+
master_bus.set(bus.clone(o), t)
|
|
2218
2168
|
}, method, arg, {t: t})
|
|
2219
2169
|
else if (method == 'to_delete')
|
|
2220
2170
|
bus.run_handler(function delete_from_master (k, t) {
|
|
@@ -2281,7 +2231,7 @@ function import_server (bus, options)
|
|
|
2281
2231
|
var buffer = {}
|
|
2282
2232
|
var full_file_path = require('path').join(__dirname, file_path)
|
|
2283
2233
|
|
|
2284
|
-
bus(state_path + '*').
|
|
2234
|
+
bus(state_path + '*').to_get = (rest) => {
|
|
2285
2235
|
// We DO want to handle:
|
|
2286
2236
|
// - "foo"
|
|
2287
2237
|
// - "foo/*"
|
|
@@ -2307,7 +2257,7 @@ function import_server (bus, options)
|
|
|
2307
2257
|
return {_:f}
|
|
2308
2258
|
}
|
|
2309
2259
|
|
|
2310
|
-
bus(state_path + '/*').
|
|
2260
|
+
bus(state_path + '/*').to_set = (o, rest, t) => {
|
|
2311
2261
|
if (rest.length>0 && rest[0] !== '/') return
|
|
2312
2262
|
var f = Buffer.from(o._, 'base64')
|
|
2313
2263
|
require('fs').writeFile(file_path + rest, f,
|
|
@@ -2318,15 +2268,15 @@ function import_server (bus, options)
|
|
|
2318
2268
|
bus.http.use('/'+state_path, require('express').static(full_file_path))
|
|
2319
2269
|
},
|
|
2320
2270
|
|
|
2321
|
-
// Installs a GET handler at route that gets state from a
|
|
2271
|
+
// Installs a GET handler at route that gets state from a getter function
|
|
2322
2272
|
// Note: Makes too many textbusses. Should re-use one.
|
|
2323
|
-
http_serve: function http_serve (route,
|
|
2273
|
+
http_serve: function http_serve (route, getter) {
|
|
2324
2274
|
var textbus = require('./statebus')()
|
|
2325
2275
|
textbus.label = 'textbus'
|
|
2326
2276
|
var watched = new Set()
|
|
2327
|
-
textbus('*').
|
|
2277
|
+
textbus('*').to_get = (filename, old) => {
|
|
2328
2278
|
return {etag: Math.random() + '',
|
|
2329
|
-
_:
|
|
2279
|
+
_: getter(filename)}
|
|
2330
2280
|
}
|
|
2331
2281
|
bus.http.get(route, (req, res) => {
|
|
2332
2282
|
var path = req.path
|
|
@@ -2336,8 +2286,8 @@ function import_server (bus, options)
|
|
|
2336
2286
|
return
|
|
2337
2287
|
}
|
|
2338
2288
|
|
|
2339
|
-
textbus.
|
|
2340
|
-
textbus.
|
|
2289
|
+
textbus.get(req.path) // So that textbus never clears the cache
|
|
2290
|
+
textbus.get(req.path, function cb (o) {
|
|
2341
2291
|
res.setHeader('Cache-Control', 'public')
|
|
2342
2292
|
// res.setHeader('Cache-Control', 'public, max-age='
|
|
2343
2293
|
// + (60 * 60 * 24 * 30)) // 1 month
|
|
@@ -2392,16 +2342,15 @@ function import_server (bus, options)
|
|
|
2392
2342
|
var files =
|
|
2393
2343
|
['extras/coffee.js', 'extras/sockjs.js', 'extras/react.js',
|
|
2394
2344
|
'statebus.js', 'client.js'].map((f) => fs.readFileSync('node_modules/statebus/' + f))
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
))
|
|
2345
|
+
files.unshift(fs.readFileSync(
|
|
2346
|
+
'node_modules/braidify/braidify-client.js'
|
|
2347
|
+
))
|
|
2399
2348
|
res.send(files.join(';\n'))
|
|
2400
2349
|
})
|
|
2401
2350
|
},
|
|
2402
2351
|
|
|
2403
2352
|
serve_wiki: () => {
|
|
2404
|
-
bus('edit/*').
|
|
2353
|
+
bus('edit/*').to_get = () => ({_: require('./extras/wiki.coffee').code})
|
|
2405
2354
|
},
|
|
2406
2355
|
|
|
2407
2356
|
unix_socket_repl: function (filename) {
|
|
@@ -2468,13 +2417,15 @@ function import_server (bus, options)
|
|
|
2468
2417
|
}
|
|
2469
2418
|
}
|
|
2470
2419
|
// Add methods to bus object
|
|
2471
|
-
for (var m in extra_methods)
|
|
2420
|
+
for (var m in extra_methods) {
|
|
2472
2421
|
bus[m] = extra_methods[m]
|
|
2422
|
+
bus.libs[m] = extra_methods[m]
|
|
2423
|
+
}
|
|
2473
2424
|
|
|
2474
2425
|
bus.options = default_options(bus)
|
|
2475
2426
|
set_options(bus, options)
|
|
2476
2427
|
|
|
2477
|
-
// Automatically make state://
|
|
2428
|
+
// Automatically make state:// get over a websocket
|
|
2478
2429
|
bus.net_automount()
|
|
2479
2430
|
return bus
|
|
2480
2431
|
}
|
|
@@ -2482,8 +2433,6 @@ function import_server (bus, options)
|
|
|
2482
2433
|
|
|
2483
2434
|
// Disables CORS in HTTP servers
|
|
2484
2435
|
function free_the_cors (req, res, next) {
|
|
2485
|
-
console.log('free the cors!', req.method, req.url)
|
|
2486
|
-
|
|
2487
2436
|
var free_the_cors = {
|
|
2488
2437
|
"Access-Control-Allow-Origin": "*",
|
|
2489
2438
|
"Access-Control-Allow-Methods": "OPTIONS, HEAD, GET, PUT, UNSUBSCRIBE",
|
|
@@ -2494,7 +2443,7 @@ function free_the_cors (req, res, next) {
|
|
|
2494
2443
|
res.writeHead(200)
|
|
2495
2444
|
res.end()
|
|
2496
2445
|
} else
|
|
2497
|
-
next()
|
|
2446
|
+
next && next()
|
|
2498
2447
|
}
|
|
2499
2448
|
|
|
2500
2449
|
|
|
@@ -2535,6 +2484,11 @@ delay.init = _=> delay_so_far = 0
|
|
|
2535
2484
|
var delay_so_far = 0
|
|
2536
2485
|
|
|
2537
2486
|
|
|
2487
|
+
// Set up default linked json converters
|
|
2488
|
+
var to_http_body = (o) => JSON.stringify(o.val)
|
|
2489
|
+
var from_http_body = (key, body) => ({ key, val: JSON.parse(body) })
|
|
2490
|
+
|
|
2491
|
+
|
|
2538
2492
|
// Now export everything
|
|
2539
2493
|
module.exports.import_server = import_server
|
|
2540
2494
|
module.exports.run_server = function (bus, options) { bus.serve(options) }
|
|
@@ -2543,7 +2497,7 @@ module.exports.import_module = function (statebus) {
|
|
|
2543
2497
|
|
|
2544
2498
|
statebus.serve = function serve (options) {
|
|
2545
2499
|
var bus = statebus()
|
|
2546
|
-
require('./
|
|
2500
|
+
require('./servers').run_server(bus, options)
|
|
2547
2501
|
return bus
|
|
2548
2502
|
}
|
|
2549
2503
|
|