statebus 7.0.33 → 7.1.0
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 +146 -40
- package/package.json +2 -2
- package/server-library.js +222 -232
- package/statebus.js +344 -112
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,53 @@ test(function proxy_link (done) {
|
|
|
318
319
|
done()
|
|
319
320
|
})
|
|
320
321
|
|
|
322
|
+
test(function proxy_links_through (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
|
+
log('state.a[0]()() is', bus.state.a[0]()())
|
|
337
|
+
assert(bus.state.a[0]()() === 'see')
|
|
338
|
+
|
|
339
|
+
// Add a little more wrapping and try some more
|
|
340
|
+
|
|
341
|
+
bus.state.nested = [99, {a: bus.link('a')}]
|
|
342
|
+
log('nested is', bus.state.nested)
|
|
343
|
+
log('nested[1].a()[0]()() is', bus.state.nested[1].a()[0]()())
|
|
344
|
+
assert(bus.state.nested[1].a()[0]()() === 'see')
|
|
345
|
+
|
|
346
|
+
// Now try to go raw at the top-level
|
|
347
|
+
|
|
348
|
+
log('Raw is', bus.state[bus.symbols.raw].nested)
|
|
349
|
+
log('Raw is', bus.raw(bus.state).nested)
|
|
350
|
+
assert(bus.deep_equals(
|
|
351
|
+
bus.state[bus.symbols.raw].nested,
|
|
352
|
+
bus.raw(bus.state).nested
|
|
353
|
+
))
|
|
354
|
+
assert(bus.deep_equals(
|
|
355
|
+
bus.raw(bus.state).nested,
|
|
356
|
+
[ 99, { a: { link: 'a' } } ]
|
|
357
|
+
))
|
|
358
|
+
|
|
359
|
+
// Now try to go raw within
|
|
360
|
+
|
|
361
|
+
log('Raw inner is', bus.raw(bus.state.nested[1]))
|
|
362
|
+
|
|
363
|
+
log('Bad raw call is', bus.raw(bus.state.nested[0]))
|
|
364
|
+
|
|
365
|
+
done()
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
|
|
321
369
|
test(function translate_fields (done) {
|
|
322
370
|
// Translate Statebus -> Proxy format
|
|
323
371
|
var tests1 = [
|
|
@@ -486,8 +534,8 @@ test(function once (done) {
|
|
|
486
534
|
delay(30, _=> done())
|
|
487
535
|
})
|
|
488
536
|
|
|
489
|
-
// If there's
|
|
490
|
-
//
|
|
537
|
+
// If there's a getter, the callback doesn't return until the handler fires a
|
|
538
|
+
// value
|
|
491
539
|
test(function get_remote (done) {
|
|
492
540
|
var count = 0
|
|
493
541
|
|
|
@@ -606,7 +654,7 @@ test(function forgetting (done) {
|
|
|
606
654
|
}, 100)
|
|
607
655
|
})
|
|
608
656
|
|
|
609
|
-
// Can we return an object that
|
|
657
|
+
// Can we return an object that gets another?
|
|
610
658
|
test(function nested_get (done) {
|
|
611
659
|
function outer () { return {inner: bus.get('inner') } }
|
|
612
660
|
bus('outer').getter = outer
|
|
@@ -870,32 +918,75 @@ test(function proxies (done) {
|
|
|
870
918
|
// Getting a normal property should do a get
|
|
871
919
|
})
|
|
872
920
|
|
|
873
|
-
test(function
|
|
921
|
+
test(function path_parts (done) {
|
|
874
922
|
bus.honk = 3
|
|
875
|
-
bus('path/:
|
|
876
|
-
t.done({ key: key, val:
|
|
923
|
+
bus('path/:part1/:part2/stuff/:part3/more*').getter = function (key, path, t) {
|
|
924
|
+
t.done({ key: key, val: path })
|
|
877
925
|
}
|
|
878
926
|
|
|
879
|
-
bus.get_once('path/foo/stuff(yes:maam,okay)', (o) => {
|
|
880
|
-
assert(o.val.
|
|
927
|
+
bus.get_once('path/foo/bar/stuff/baz/more(yes:maam,okay)', (o) => {
|
|
928
|
+
assert(o.val.part1 === 'foo'
|
|
929
|
+
&& o.val.part2 === 'bar'
|
|
930
|
+
&& o.val.part3 === 'baz', 'Dangit, we got: ' + JSON.stringify(o.val))
|
|
881
931
|
done()
|
|
882
932
|
})
|
|
883
933
|
})
|
|
884
934
|
|
|
885
|
-
test(function
|
|
886
|
-
|
|
935
|
+
test(function func_args (done) {
|
|
936
|
+
log('Func args test!!!')
|
|
937
|
+
|
|
938
|
+
bus('func_args/:foo/stuff*()').getter = function (key, func_args, star, t) {
|
|
939
|
+
log('Getting star', star, 'func_args', func_args)
|
|
887
940
|
t.done({
|
|
888
941
|
key: key,
|
|
889
|
-
val: {
|
|
942
|
+
val: {func_args, star}
|
|
890
943
|
})
|
|
891
944
|
}
|
|
892
945
|
|
|
893
|
-
bus.get_once('
|
|
894
|
-
|
|
895
|
-
assert(o.val.
|
|
896
|
-
assert(o.val.
|
|
897
|
-
|
|
946
|
+
bus.get_once('func_args/foobar/stuff(yes:maam,okay)', (o) => {
|
|
947
|
+
assert(o.val.func_args.yes === 'maam')
|
|
948
|
+
assert(o.val.func_args.okay)
|
|
949
|
+
assert(o.val.star === '')
|
|
950
|
+
})
|
|
951
|
+
|
|
952
|
+
bus.get_once('func_args/foobar/stuff_and_more(yes:maam,okay)', (o) => {
|
|
953
|
+
assert(o.val.func_args.yes === 'maam')
|
|
954
|
+
assert(o.val.func_args.okay)
|
|
955
|
+
assert(o.val.star === '_and_more')
|
|
956
|
+
})
|
|
957
|
+
|
|
958
|
+
bus.get_once('func_args/foobar/stuff_and_more', (o) => {
|
|
959
|
+
assert(o.val.func_args === undefined)
|
|
960
|
+
assert(o.val.star === '_and_more')
|
|
898
961
|
})
|
|
962
|
+
|
|
963
|
+
delay(50, done)
|
|
964
|
+
})
|
|
965
|
+
|
|
966
|
+
test(function autodetected_args_called_with_proxy (done) {
|
|
967
|
+
var count = 0
|
|
968
|
+
bus('counter', {
|
|
969
|
+
get: (val, old, t) => {
|
|
970
|
+
log('Getting counter', {count, val, old})
|
|
971
|
+
if (count === 0) {
|
|
972
|
+
assert(val === undefined, 'Get bad val')
|
|
973
|
+
assert(old === undefined, 'Get bad old')
|
|
974
|
+
}
|
|
975
|
+
return count
|
|
976
|
+
},
|
|
977
|
+
set: (val, old, t) => {
|
|
978
|
+
log('Setting counter', count, 'to', val, 'from knowledge of', old)
|
|
979
|
+
if (count === 0) {
|
|
980
|
+
assert(old === undefined, 'Set bad old')
|
|
981
|
+
}
|
|
982
|
+
count = val
|
|
983
|
+
t.done()
|
|
984
|
+
}
|
|
985
|
+
})
|
|
986
|
+
|
|
987
|
+
bus.state.counter = 0
|
|
988
|
+
|
|
989
|
+
delay(50, done)
|
|
899
990
|
})
|
|
900
991
|
|
|
901
992
|
test(function only_one (done) {
|
|
@@ -1163,7 +1254,13 @@ test(function default_route (done) {
|
|
|
1163
1254
|
function setup_servers () {
|
|
1164
1255
|
var port = 3000 + Math.floor(Math.random() * 1000)
|
|
1165
1256
|
|
|
1166
|
-
s = require('../statebus').serve({port,
|
|
1257
|
+
s = require('../statebus').serve({port,
|
|
1258
|
+
file_store: false,
|
|
1259
|
+
websocket: true,
|
|
1260
|
+
client: (c) => {
|
|
1261
|
+
// c.honk = true
|
|
1262
|
+
c.shadows(s)}
|
|
1263
|
+
})
|
|
1167
1264
|
s.label = 's'
|
|
1168
1265
|
log('Saving /far on server')
|
|
1169
1266
|
s.set({key: 'far', away:'is this'})
|
|
@@ -1233,7 +1330,7 @@ test(function setup_server (done) {
|
|
|
1233
1330
|
test(function login (done) {
|
|
1234
1331
|
var {s, c} = setup_servers()
|
|
1235
1332
|
|
|
1236
|
-
//c.honk = true
|
|
1333
|
+
// c.honk = true
|
|
1237
1334
|
c(function () {
|
|
1238
1335
|
var u = c.get('/current_user')
|
|
1239
1336
|
log('Current user changed!', c.label, JSON.stringify(u))
|
|
@@ -1254,7 +1351,7 @@ test(function login (done) {
|
|
|
1254
1351
|
var u = c.get('/current_user')
|
|
1255
1352
|
u.val = {login_as: {name: 'mike', pass: 'yeah'}}
|
|
1256
1353
|
log('Logging in')
|
|
1257
|
-
//s.honk = true
|
|
1354
|
+
// s.honk = true
|
|
1258
1355
|
c.set(u)
|
|
1259
1356
|
log('Let\'s see if that login worked!!')
|
|
1260
1357
|
})
|
|
@@ -1337,6 +1434,7 @@ function connections_helper (done, port, options) {
|
|
|
1337
1434
|
// Setup a server
|
|
1338
1435
|
var s = require('../statebus').serve({port: port,
|
|
1339
1436
|
file_store: false,
|
|
1437
|
+
websocket: true,
|
|
1340
1438
|
connections: options})
|
|
1341
1439
|
s.label = 's'
|
|
1342
1440
|
|
|
@@ -1419,13 +1517,13 @@ function connections_helper (done, port, options) {
|
|
|
1419
1517
|
delay(50, _=> done())
|
|
1420
1518
|
}
|
|
1421
1519
|
|
|
1422
|
-
test(function connections_1 (done) {
|
|
1423
|
-
|
|
1424
|
-
})
|
|
1520
|
+
// test(function connections_1 (done) {
|
|
1521
|
+
// connections_helper(done, 3951, {include_users: true, edit_others: true})
|
|
1522
|
+
// })
|
|
1425
1523
|
|
|
1426
|
-
test(function connections_2 (done) {
|
|
1427
|
-
|
|
1428
|
-
})
|
|
1524
|
+
// test(function connections_2 (done) {
|
|
1525
|
+
// connections_helper(done, 3952, {include_users: false, edit_others: false})
|
|
1526
|
+
// })
|
|
1429
1527
|
|
|
1430
1528
|
test(function flashbacks (done) {
|
|
1431
1529
|
// We have an echo canceler. If you set state, it shouldn't send the
|
|
@@ -1436,12 +1534,15 @@ test(function flashbacks (done) {
|
|
|
1436
1534
|
var port = 3873
|
|
1437
1535
|
|
|
1438
1536
|
// Setup a server
|
|
1439
|
-
var s = require('../statebus').serve({port: port,
|
|
1537
|
+
var s = require('../statebus').serve({port: port,
|
|
1538
|
+
websocket: true,
|
|
1539
|
+
file_store: false})
|
|
1540
|
+
|
|
1440
1541
|
s.label = 's'
|
|
1441
1542
|
|
|
1442
1543
|
s('x').setter = (o, t) => {
|
|
1443
1544
|
log('Saving x with', o)
|
|
1444
|
-
o.
|
|
1545
|
+
o.val++ // Change the value of o.x a little
|
|
1445
1546
|
t.done(o)
|
|
1446
1547
|
}
|
|
1447
1548
|
|
|
@@ -1454,20 +1555,24 @@ test(function flashbacks (done) {
|
|
|
1454
1555
|
c2.label = 'c2'
|
|
1455
1556
|
c2.ws_mount('*', 'statei://localhost:' + port)
|
|
1456
1557
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1558
|
+
// s.honk = true
|
|
1559
|
+
// c1.honk = true
|
|
1560
|
+
// c2.honk = true
|
|
1561
|
+
|
|
1562
|
+
var x1 = c1.get('x')
|
|
1563
|
+
var x2 = c2.get('x')
|
|
1459
1564
|
|
|
1460
1565
|
// Change stuff
|
|
1461
1566
|
delay(50, _=> {
|
|
1462
|
-
|
|
1463
|
-
c1.set(
|
|
1567
|
+
x1.val = 1
|
|
1568
|
+
c1.set(x1)
|
|
1464
1569
|
})
|
|
1465
1570
|
|
|
1466
1571
|
// Test stuff
|
|
1467
|
-
delay(
|
|
1468
|
-
assert(
|
|
1469
|
-
assert(
|
|
1470
|
-
log('complete!',
|
|
1572
|
+
delay(500, _=> {
|
|
1573
|
+
assert(x2.val === 2, 'c2 didn\'t get the update')
|
|
1574
|
+
assert(x1.val === 2, 'c1 didn\'t get the update')
|
|
1575
|
+
log('complete!', x1, x2)
|
|
1471
1576
|
done()
|
|
1472
1577
|
})
|
|
1473
1578
|
})
|
|
@@ -1653,6 +1758,7 @@ if (false) {
|
|
|
1653
1758
|
test(function closet_space (done) {
|
|
1654
1759
|
var s = require('../statebus').serve({port: 3949,
|
|
1655
1760
|
file_store: false,
|
|
1761
|
+
websocket: true,
|
|
1656
1762
|
client: (c)=>{c.honk=true}})
|
|
1657
1763
|
s.label = 's'
|
|
1658
1764
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "statebus",
|
|
3
|
-
"version": "7.0
|
|
3
|
+
"version": "7.1.0",
|
|
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",
|