statebus 7.0.33 → 7.1.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-library.js +7 -3
- package/extras/tests.js +206 -40
- package/package.json +2 -2
- package/server-library.js +222 -232
- package/statebus.js +457 -209
package/client-library.js
CHANGED
|
@@ -40,10 +40,11 @@
|
|
|
40
40
|
return new WebSocket(url + '/' + websocket_prefix + '/websocket')
|
|
41
41
|
// return new SockJS(url + '/' + websocket_prefix)
|
|
42
42
|
}
|
|
43
|
+
bus.make_websocket = make_websocket
|
|
43
44
|
bus.client_creds = function client_creds () {
|
|
44
45
|
// This function is only used for websocket connections.
|
|
45
46
|
// http connections set the cookie on the server.
|
|
46
|
-
var me = JSON.parse(localStorage['ls/me'])
|
|
47
|
+
var me = JSON.parse(localStorage['ls/me'] || '{}')
|
|
47
48
|
bus.log('connect: me is', me)
|
|
48
49
|
if (!me.client) {
|
|
49
50
|
// Create a client id if we have none yet.
|
|
@@ -231,11 +232,14 @@
|
|
|
231
232
|
// As a snapshot body
|
|
232
233
|
t.return({
|
|
233
234
|
key: key,
|
|
234
|
-
val: add_prefixes(JSON.parse(update.
|
|
235
|
+
val: add_prefixes(JSON.parse(update.body_text))
|
|
235
236
|
})
|
|
236
237
|
else if (update.patches)
|
|
237
238
|
// As a patch
|
|
238
|
-
bus.set.fire(key, {patches: update.patches
|
|
239
|
+
bus.set.fire(key, {patches: update.patches.map(patch => ({
|
|
240
|
+
...patch,
|
|
241
|
+
content: patch.content_text
|
|
242
|
+
}))})
|
|
239
243
|
},
|
|
240
244
|
reconnect
|
|
241
245
|
)).catch(reconnect)
|
package/extras/tests.js
CHANGED
|
@@ -200,6 +200,7 @@ test(function serve_options (done) {
|
|
|
200
200
|
//file_store: false,
|
|
201
201
|
file_store: {filename: filename, save_delay: 0, backup_dir: backups},
|
|
202
202
|
certs: {private_key: certs+'/pk', certificate: certs+'/cert'},
|
|
203
|
+
websocket: true
|
|
203
204
|
})
|
|
204
205
|
|
|
205
206
|
b.set({key: 'foo', body: 'is this on disk?'})
|
|
@@ -305,12 +306,12 @@ test(function url_translation (done) {
|
|
|
305
306
|
})
|
|
306
307
|
|
|
307
308
|
test(function proxy_link (done) {
|
|
308
|
-
bus.state.
|
|
309
|
+
bus.state.fake_link_from = [{link: 'link_to'}]
|
|
309
310
|
bus.state.link_to = 3
|
|
310
|
-
assert(bus.state.
|
|
311
|
-
|
|
312
|
-
var pass = bus.deep_equals(bus.state.
|
|
313
|
-
|
|
311
|
+
assert(bus.state.fake_link_from[0] !== 3, 'Link got linkified')
|
|
312
|
+
log('fake_link_from is', bus.state.fake_link_from)
|
|
313
|
+
var pass = bus.deep_equals(bus.state.fake_link_from, [{link: 'link_to'}])
|
|
314
|
+
log('pass is', pass)
|
|
314
315
|
assert(pass, 'Failed proxy link unlinky test')
|
|
315
316
|
|
|
316
317
|
// assert(bus.deep_equals(bus.state.link_from, [{link: 'link_to'}]), 'Link_from looks wrong')
|
|
@@ -318,6 +319,113 @@ test(function proxy_link (done) {
|
|
|
318
319
|
done()
|
|
319
320
|
})
|
|
320
321
|
|
|
322
|
+
test(function proxy_links_through1 (done) {
|
|
323
|
+
var bus = require('../statebus')()
|
|
324
|
+
|
|
325
|
+
// First try to dereference through three links
|
|
326
|
+
|
|
327
|
+
bus.state.a = [bus.link('b')]
|
|
328
|
+
bus.state.b = bus.link('c')
|
|
329
|
+
bus.state.c = 'see'
|
|
330
|
+
|
|
331
|
+
log('state.c is', bus.state.c)
|
|
332
|
+
log('state.b is', bus.state.b, 'from cache', bus.cache.b)
|
|
333
|
+
log('state.a is', bus.state.a)
|
|
334
|
+
log('state.a[0] is', bus.state.a[0])
|
|
335
|
+
log('state.a[0] is', bus.state.a[0])
|
|
336
|
+
assert(bus.state.a[0] === 'see')
|
|
337
|
+
|
|
338
|
+
// Add a little more wrapping and try some more
|
|
339
|
+
|
|
340
|
+
bus.state.nested = [99, {a: bus.link('a')}]
|
|
341
|
+
log('nested is', bus.state.nested)
|
|
342
|
+
log('nested[1].a[0] is', bus.state.nested[1].a[0])
|
|
343
|
+
assert(bus.state.nested[1].a[0] === 'see')
|
|
344
|
+
|
|
345
|
+
// Now try to go raw at the top-level
|
|
346
|
+
|
|
347
|
+
log('Raw is', bus.state[bus.symbols.raw].nested)
|
|
348
|
+
log('Raw is', bus.raw(bus.state).nested)
|
|
349
|
+
assert(bus.deep_equals(
|
|
350
|
+
bus.state[bus.symbols.raw].nested,
|
|
351
|
+
bus.raw(bus.state).nested
|
|
352
|
+
))
|
|
353
|
+
|
|
354
|
+
log('Gonna deep_quals between:',
|
|
355
|
+
bus.raw(bus.state).nested,
|
|
356
|
+
[ 99, { a: { link: 'a' } } ])
|
|
357
|
+
|
|
358
|
+
assert(bus.deep_equals(
|
|
359
|
+
bus.raw(bus.state).nested,
|
|
360
|
+
[ 99, { a: { link: 'a' } } ]
|
|
361
|
+
))
|
|
362
|
+
|
|
363
|
+
// Now try to go raw within
|
|
364
|
+
|
|
365
|
+
log('Raw inner is', bus.raw(bus.state.nested[1]))
|
|
366
|
+
|
|
367
|
+
log('Bad raw call is', bus.raw(bus.state.nested[0]))
|
|
368
|
+
|
|
369
|
+
done()
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
// Disable the version of proxy_links_through where the user has to explicitly
|
|
374
|
+
// traverse links with ()
|
|
375
|
+
if (false)
|
|
376
|
+
test(function proxy_links_through2 (done) {
|
|
377
|
+
var bus = require('../statebus')()
|
|
378
|
+
|
|
379
|
+
// First try to dereference through three links
|
|
380
|
+
|
|
381
|
+
bus.state.a = [bus.link('b')]
|
|
382
|
+
bus.state.b = bus.link('c')
|
|
383
|
+
bus.state.c = 'see'
|
|
384
|
+
|
|
385
|
+
log('state.c is', bus.state.c)
|
|
386
|
+
log('state.b is', bus.state.b, 'from cache', bus.cache.b)
|
|
387
|
+
log('state.a is', bus.state.a)
|
|
388
|
+
log('state.a[0] is', bus.state.a[0])
|
|
389
|
+
log('state.a[0]() is', bus.state.a[0]())
|
|
390
|
+
log('state.a[0]()() is', bus.state.a[0]()())
|
|
391
|
+
assert(bus.state.a[0]()() === 'see')
|
|
392
|
+
|
|
393
|
+
// Add a little more wrapping and try some more
|
|
394
|
+
|
|
395
|
+
bus.state.nested = [99, {a: bus.link('a')}]
|
|
396
|
+
log('nested is', bus.state.nested)
|
|
397
|
+
log('nested[1].a()[0]()() is', bus.state.nested[1].a()[0]()())
|
|
398
|
+
assert(bus.state.nested[1].a()[0]()() === 'see')
|
|
399
|
+
|
|
400
|
+
// Now try to go raw at the top-level
|
|
401
|
+
|
|
402
|
+
log('Raw is', bus.state[bus.symbols.raw].nested)
|
|
403
|
+
log('Raw is', bus.raw(bus.state).nested)
|
|
404
|
+
assert(bus.deep_equals(
|
|
405
|
+
bus.state[bus.symbols.raw].nested,
|
|
406
|
+
bus.raw(bus.state).nested
|
|
407
|
+
))
|
|
408
|
+
|
|
409
|
+
console.log('Gonna deep_quals between:',
|
|
410
|
+
bus.raw(bus.state).nested,
|
|
411
|
+
[ 99, { a: { link: 'a' } } ])
|
|
412
|
+
|
|
413
|
+
assert(bus.deep_equals(
|
|
414
|
+
bus.raw(bus.state).nested,
|
|
415
|
+
[ 99, { a: { link: 'a' } } ]
|
|
416
|
+
))
|
|
417
|
+
|
|
418
|
+
// Now try to go raw within
|
|
419
|
+
|
|
420
|
+
log('Raw inner is', bus.raw(bus.state.nested[1]))
|
|
421
|
+
|
|
422
|
+
log('Bad raw call is', bus.raw(bus.state.nested[0]))
|
|
423
|
+
|
|
424
|
+
done()
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
321
429
|
test(function translate_fields (done) {
|
|
322
430
|
// Translate Statebus -> Proxy format
|
|
323
431
|
var tests1 = [
|
|
@@ -486,8 +594,8 @@ test(function once (done) {
|
|
|
486
594
|
delay(30, _=> done())
|
|
487
595
|
})
|
|
488
596
|
|
|
489
|
-
// If there's
|
|
490
|
-
//
|
|
597
|
+
// If there's a getter, the callback doesn't return until the handler fires a
|
|
598
|
+
// value
|
|
491
599
|
test(function get_remote (done) {
|
|
492
600
|
var count = 0
|
|
493
601
|
|
|
@@ -606,7 +714,7 @@ test(function forgetting (done) {
|
|
|
606
714
|
}, 100)
|
|
607
715
|
})
|
|
608
716
|
|
|
609
|
-
// Can we return an object that
|
|
717
|
+
// Can we return an object that gets another?
|
|
610
718
|
test(function nested_get (done) {
|
|
611
719
|
function outer () { return {inner: bus.get('inner') } }
|
|
612
720
|
bus('outer').getter = outer
|
|
@@ -870,32 +978,75 @@ test(function proxies (done) {
|
|
|
870
978
|
// Getting a normal property should do a get
|
|
871
979
|
})
|
|
872
980
|
|
|
873
|
-
test(function
|
|
981
|
+
test(function path_parts (done) {
|
|
874
982
|
bus.honk = 3
|
|
875
|
-
bus('path/:
|
|
876
|
-
t.done({ key: key, val:
|
|
983
|
+
bus('path/:part1/:part2/stuff/:part3/more*').getter = function (key, path, t) {
|
|
984
|
+
t.done({ key: key, val: path })
|
|
877
985
|
}
|
|
878
986
|
|
|
879
|
-
bus.get_once('path/foo/stuff(yes:maam,okay)', (o) => {
|
|
880
|
-
assert(o.val.
|
|
987
|
+
bus.get_once('path/foo/bar/stuff/baz/more(yes:maam,okay)', (o) => {
|
|
988
|
+
assert(o.val.part1 === 'foo'
|
|
989
|
+
&& o.val.part2 === 'bar'
|
|
990
|
+
&& o.val.part3 === 'baz', 'Dangit, we got: ' + JSON.stringify(o.val))
|
|
881
991
|
done()
|
|
882
992
|
})
|
|
883
993
|
})
|
|
884
994
|
|
|
885
|
-
test(function
|
|
886
|
-
|
|
995
|
+
test(function func_args (done) {
|
|
996
|
+
log('Func args test!!!')
|
|
997
|
+
|
|
998
|
+
bus('func_args/:foo/stuff*()').getter = function (key, func_args, star, t) {
|
|
999
|
+
log('Getting star', star, 'func_args', func_args)
|
|
887
1000
|
t.done({
|
|
888
1001
|
key: key,
|
|
889
|
-
val: {
|
|
1002
|
+
val: {func_args, star}
|
|
890
1003
|
})
|
|
891
1004
|
}
|
|
892
1005
|
|
|
893
|
-
bus.get_once('
|
|
894
|
-
|
|
895
|
-
assert(o.val.
|
|
896
|
-
assert(o.val.
|
|
897
|
-
|
|
1006
|
+
bus.get_once('func_args/foobar/stuff(yes:maam,okay)', (o) => {
|
|
1007
|
+
assert(o.val.func_args.yes === 'maam')
|
|
1008
|
+
assert(o.val.func_args.okay)
|
|
1009
|
+
assert(o.val.star === '')
|
|
1010
|
+
})
|
|
1011
|
+
|
|
1012
|
+
bus.get_once('func_args/foobar/stuff_and_more(yes:maam,okay)', (o) => {
|
|
1013
|
+
assert(o.val.func_args.yes === 'maam')
|
|
1014
|
+
assert(o.val.func_args.okay)
|
|
1015
|
+
assert(o.val.star === '_and_more')
|
|
898
1016
|
})
|
|
1017
|
+
|
|
1018
|
+
bus.get_once('func_args/foobar/stuff_and_more', (o) => {
|
|
1019
|
+
assert(o.val.func_args === undefined)
|
|
1020
|
+
assert(o.val.star === '_and_more')
|
|
1021
|
+
})
|
|
1022
|
+
|
|
1023
|
+
delay(50, done)
|
|
1024
|
+
})
|
|
1025
|
+
|
|
1026
|
+
test(function autodetected_args_called_with_proxy (done) {
|
|
1027
|
+
var count = 0
|
|
1028
|
+
bus('counter', {
|
|
1029
|
+
get: (val, old, t) => {
|
|
1030
|
+
log('Getting counter', {count, val, old})
|
|
1031
|
+
if (count === 0) {
|
|
1032
|
+
assert(val === undefined, 'Get bad val')
|
|
1033
|
+
assert(old === undefined, 'Get bad old')
|
|
1034
|
+
}
|
|
1035
|
+
return count
|
|
1036
|
+
},
|
|
1037
|
+
set: (val, old, t) => {
|
|
1038
|
+
log('Setting counter', count, 'to', val, 'from knowledge of', old)
|
|
1039
|
+
if (count === 0) {
|
|
1040
|
+
assert(old === undefined, 'Set bad old')
|
|
1041
|
+
}
|
|
1042
|
+
count = val
|
|
1043
|
+
t.done()
|
|
1044
|
+
}
|
|
1045
|
+
})
|
|
1046
|
+
|
|
1047
|
+
bus.state.counter = 0
|
|
1048
|
+
|
|
1049
|
+
delay(50, done)
|
|
899
1050
|
})
|
|
900
1051
|
|
|
901
1052
|
test(function only_one (done) {
|
|
@@ -1163,7 +1314,13 @@ test(function default_route (done) {
|
|
|
1163
1314
|
function setup_servers () {
|
|
1164
1315
|
var port = 3000 + Math.floor(Math.random() * 1000)
|
|
1165
1316
|
|
|
1166
|
-
s = require('../statebus').serve({port,
|
|
1317
|
+
s = require('../statebus').serve({port,
|
|
1318
|
+
file_store: false,
|
|
1319
|
+
websocket: true,
|
|
1320
|
+
client: (c) => {
|
|
1321
|
+
// c.honk = true
|
|
1322
|
+
c.shadows(s)}
|
|
1323
|
+
})
|
|
1167
1324
|
s.label = 's'
|
|
1168
1325
|
log('Saving /far on server')
|
|
1169
1326
|
s.set({key: 'far', away:'is this'})
|
|
@@ -1233,7 +1390,7 @@ test(function setup_server (done) {
|
|
|
1233
1390
|
test(function login (done) {
|
|
1234
1391
|
var {s, c} = setup_servers()
|
|
1235
1392
|
|
|
1236
|
-
//c.honk = true
|
|
1393
|
+
// c.honk = true
|
|
1237
1394
|
c(function () {
|
|
1238
1395
|
var u = c.get('/current_user')
|
|
1239
1396
|
log('Current user changed!', c.label, JSON.stringify(u))
|
|
@@ -1254,7 +1411,7 @@ test(function login (done) {
|
|
|
1254
1411
|
var u = c.get('/current_user')
|
|
1255
1412
|
u.val = {login_as: {name: 'mike', pass: 'yeah'}}
|
|
1256
1413
|
log('Logging in')
|
|
1257
|
-
//s.honk = true
|
|
1414
|
+
// s.honk = true
|
|
1258
1415
|
c.set(u)
|
|
1259
1416
|
log('Let\'s see if that login worked!!')
|
|
1260
1417
|
})
|
|
@@ -1337,6 +1494,7 @@ function connections_helper (done, port, options) {
|
|
|
1337
1494
|
// Setup a server
|
|
1338
1495
|
var s = require('../statebus').serve({port: port,
|
|
1339
1496
|
file_store: false,
|
|
1497
|
+
websocket: true,
|
|
1340
1498
|
connections: options})
|
|
1341
1499
|
s.label = 's'
|
|
1342
1500
|
|
|
@@ -1419,13 +1577,13 @@ function connections_helper (done, port, options) {
|
|
|
1419
1577
|
delay(50, _=> done())
|
|
1420
1578
|
}
|
|
1421
1579
|
|
|
1422
|
-
test(function connections_1 (done) {
|
|
1423
|
-
|
|
1424
|
-
})
|
|
1580
|
+
// test(function connections_1 (done) {
|
|
1581
|
+
// connections_helper(done, 3951, {include_users: true, edit_others: true})
|
|
1582
|
+
// })
|
|
1425
1583
|
|
|
1426
|
-
test(function connections_2 (done) {
|
|
1427
|
-
|
|
1428
|
-
})
|
|
1584
|
+
// test(function connections_2 (done) {
|
|
1585
|
+
// connections_helper(done, 3952, {include_users: false, edit_others: false})
|
|
1586
|
+
// })
|
|
1429
1587
|
|
|
1430
1588
|
test(function flashbacks (done) {
|
|
1431
1589
|
// We have an echo canceler. If you set state, it shouldn't send the
|
|
@@ -1436,12 +1594,15 @@ test(function flashbacks (done) {
|
|
|
1436
1594
|
var port = 3873
|
|
1437
1595
|
|
|
1438
1596
|
// Setup a server
|
|
1439
|
-
var s = require('../statebus').serve({port: port,
|
|
1597
|
+
var s = require('../statebus').serve({port: port,
|
|
1598
|
+
websocket: true,
|
|
1599
|
+
file_store: false})
|
|
1600
|
+
|
|
1440
1601
|
s.label = 's'
|
|
1441
1602
|
|
|
1442
1603
|
s('x').setter = (o, t) => {
|
|
1443
1604
|
log('Saving x with', o)
|
|
1444
|
-
o.
|
|
1605
|
+
o.val++ // Change the value of o.x a little
|
|
1445
1606
|
t.done(o)
|
|
1446
1607
|
}
|
|
1447
1608
|
|
|
@@ -1454,20 +1615,24 @@ test(function flashbacks (done) {
|
|
|
1454
1615
|
c2.label = 'c2'
|
|
1455
1616
|
c2.ws_mount('*', 'statei://localhost:' + port)
|
|
1456
1617
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1618
|
+
// s.honk = true
|
|
1619
|
+
// c1.honk = true
|
|
1620
|
+
// c2.honk = true
|
|
1621
|
+
|
|
1622
|
+
var x1 = c1.get('x')
|
|
1623
|
+
var x2 = c2.get('x')
|
|
1459
1624
|
|
|
1460
1625
|
// Change stuff
|
|
1461
1626
|
delay(50, _=> {
|
|
1462
|
-
|
|
1463
|
-
c1.set(
|
|
1627
|
+
x1.val = 1
|
|
1628
|
+
c1.set(x1)
|
|
1464
1629
|
})
|
|
1465
1630
|
|
|
1466
1631
|
// Test stuff
|
|
1467
|
-
delay(
|
|
1468
|
-
assert(
|
|
1469
|
-
assert(
|
|
1470
|
-
log('complete!',
|
|
1632
|
+
delay(500, _=> {
|
|
1633
|
+
assert(x2.val === 2, 'c2 didn\'t get the update')
|
|
1634
|
+
assert(x1.val === 2, 'c1 didn\'t get the update')
|
|
1635
|
+
log('complete!', x1, x2)
|
|
1471
1636
|
done()
|
|
1472
1637
|
})
|
|
1473
1638
|
})
|
|
@@ -1653,6 +1818,7 @@ if (false) {
|
|
|
1653
1818
|
test(function closet_space (done) {
|
|
1654
1819
|
var s = require('../statebus').serve({port: 3949,
|
|
1655
1820
|
file_store: false,
|
|
1821
|
+
websocket: true,
|
|
1656
1822
|
client: (c)=>{c.honk=true}})
|
|
1657
1823
|
s.label = 's'
|
|
1658
1824
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "statebus",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.1",
|
|
4
4
|
"description": "Synchronize state.",
|
|
5
5
|
"main": "statebus.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"bcrypt-nodejs": "0.0.3",
|
|
23
|
-
"braid-http": "^1.3.
|
|
23
|
+
"braid-http": "^1.3.9",
|
|
24
24
|
"chokidar": "^3.4.0",
|
|
25
25
|
"cookie": "^0.3.1",
|
|
26
26
|
"express": "^4.15.3",
|