statebus 7.0.32 → 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 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.body))
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.link_from = [{link: 'link_to'}]
309
+ bus.state.fake_link_from = [{link: 'link_to'}]
309
310
  bus.state.link_to = 3
310
- assert(bus.state.link_from[0] !== 3, 'Link got linkified')
311
- console.log('link_from is', bus.state.link_from)
312
- var pass = bus.deep_equals(bus.state.link_from, [{link: 'link_to'}])
313
- console.log('pass is', pass)
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 an on_get handler, the callback doesn't return
490
- // until the handler fires a value
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 getes another?
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,6 +918,77 @@ test(function proxies (done) {
870
918
  // Getting a normal property should do a get
871
919
  })
872
920
 
921
+ test(function path_parts (done) {
922
+ bus.honk = 3
923
+ bus('path/:part1/:part2/stuff/:part3/more*').getter = function (key, path, t) {
924
+ t.done({ key: key, val: path })
925
+ }
926
+
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))
931
+ done()
932
+ })
933
+ })
934
+
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)
940
+ t.done({
941
+ key: key,
942
+ val: {func_args, star}
943
+ })
944
+ }
945
+
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')
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)
990
+ })
991
+
873
992
  test(function only_one (done) {
874
993
  bus('only_one/*').getter = function (k) {
875
994
  var id = k[k.length-1]
@@ -1135,7 +1254,13 @@ test(function default_route (done) {
1135
1254
  function setup_servers () {
1136
1255
  var port = 3000 + Math.floor(Math.random() * 1000)
1137
1256
 
1138
- s = require('../statebus').serve({port, file_store: false})
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
+ })
1139
1264
  s.label = 's'
1140
1265
  log('Saving /far on server')
1141
1266
  s.set({key: 'far', away:'is this'})
@@ -1205,7 +1330,7 @@ test(function setup_server (done) {
1205
1330
  test(function login (done) {
1206
1331
  var {s, c} = setup_servers()
1207
1332
 
1208
- //c.honk = true
1333
+ // c.honk = true
1209
1334
  c(function () {
1210
1335
  var u = c.get('/current_user')
1211
1336
  log('Current user changed!', c.label, JSON.stringify(u))
@@ -1226,7 +1351,7 @@ test(function login (done) {
1226
1351
  var u = c.get('/current_user')
1227
1352
  u.val = {login_as: {name: 'mike', pass: 'yeah'}}
1228
1353
  log('Logging in')
1229
- //s.honk = true
1354
+ // s.honk = true
1230
1355
  c.set(u)
1231
1356
  log('Let\'s see if that login worked!!')
1232
1357
  })
@@ -1309,6 +1434,7 @@ function connections_helper (done, port, options) {
1309
1434
  // Setup a server
1310
1435
  var s = require('../statebus').serve({port: port,
1311
1436
  file_store: false,
1437
+ websocket: true,
1312
1438
  connections: options})
1313
1439
  s.label = 's'
1314
1440
 
@@ -1391,13 +1517,13 @@ function connections_helper (done, port, options) {
1391
1517
  delay(50, _=> done())
1392
1518
  }
1393
1519
 
1394
- test(function connections_1 (done) {
1395
- connections_helper(done, 3951, {include_users: true, edit_others: true})
1396
- })
1520
+ // test(function connections_1 (done) {
1521
+ // connections_helper(done, 3951, {include_users: true, edit_others: true})
1522
+ // })
1397
1523
 
1398
- test(function connections_2 (done) {
1399
- connections_helper(done, 3952, {include_users: false, edit_others: false})
1400
- })
1524
+ // test(function connections_2 (done) {
1525
+ // connections_helper(done, 3952, {include_users: false, edit_others: false})
1526
+ // })
1401
1527
 
1402
1528
  test(function flashbacks (done) {
1403
1529
  // We have an echo canceler. If you set state, it shouldn't send the
@@ -1408,12 +1534,15 @@ test(function flashbacks (done) {
1408
1534
  var port = 3873
1409
1535
 
1410
1536
  // Setup a server
1411
- var s = require('../statebus').serve({port: port, file_store: false})
1537
+ var s = require('../statebus').serve({port: port,
1538
+ websocket: true,
1539
+ file_store: false})
1540
+
1412
1541
  s.label = 's'
1413
1542
 
1414
1543
  s('x').setter = (o, t) => {
1415
1544
  log('Saving x with', o)
1416
- o.x++ // Change the value of o.x a little
1545
+ o.val++ // Change the value of o.x a little
1417
1546
  t.done(o)
1418
1547
  }
1419
1548
 
@@ -1426,20 +1555,24 @@ test(function flashbacks (done) {
1426
1555
  c2.label = 'c2'
1427
1556
  c2.ws_mount('*', 'statei://localhost:' + port)
1428
1557
 
1429
- c1.x = c1.get('x')
1430
- c2.x = c2.get('x')
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')
1431
1564
 
1432
1565
  // Change stuff
1433
1566
  delay(50, _=> {
1434
- c1.x.x = 1
1435
- c1.set(c1.x)
1567
+ x1.val = 1
1568
+ c1.set(x1)
1436
1569
  })
1437
1570
 
1438
1571
  // Test stuff
1439
- delay(50, _=> {
1440
- assert(c2.x.x === 2, 'c2 didn\'t get it')
1441
- assert(c1.x.x === 2, 'c1 didn\'t get it')
1442
- log('complete!', c1.x, c2.x)
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)
1443
1576
  done()
1444
1577
  })
1445
1578
  })
@@ -1625,6 +1758,7 @@ if (false) {
1625
1758
  test(function closet_space (done) {
1626
1759
  var s = require('../statebus').serve({port: 3949,
1627
1760
  file_store: false,
1761
+ websocket: true,
1628
1762
  client: (c)=>{c.honk=true}})
1629
1763
  s.label = 's'
1630
1764
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.0.32",
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.0.7",
23
+ "braid-http": "^1.3.9",
24
24
  "chokidar": "^3.4.0",
25
25
  "cookie": "^0.3.1",
26
26
  "express": "^4.15.3",