statebus 7.0.27 → 7.0.30

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
@@ -147,14 +147,8 @@
147
147
  if (t.parents) put.parents = t.parents
148
148
 
149
149
  // Do we have patches?
150
- if (t.patch) {
151
- put.patches = t.patch.map(patch => {
152
- var match = patch.match(/(.*) = (.*)/),
153
- range = match[1],
154
- content = match[2]
155
- return {unit: 'json', range, content}
156
- })
157
- }
150
+ if (t.patches) put.patches = t.patches
151
+
158
152
  // Then we just have a simple body.
159
153
  else put.body = JSON.stringify(obj.val)
160
154
 
@@ -222,7 +216,7 @@
222
216
  // credentials: 'include'
223
217
  }
224
218
  ).then(res => res.subscribe(
225
- new_version => {
219
+ update => {
226
220
  // New update received!
227
221
  if (subscriptions[key].status === 'connecting') {
228
222
  console.log('%c[*] opened ' + key,
@@ -233,17 +227,15 @@
233
227
  }
234
228
 
235
229
  // Return the update
236
- if (new_version.body)
230
+ if (update.body)
237
231
  // As a snapshot body
238
232
  t.return({
239
233
  key: key,
240
- val: add_prefixes(JSON.parse(new_version.body))
234
+ val: add_prefixes(JSON.parse(update.body))
241
235
  })
242
- else if (new_version.patches)
236
+ else if (update.patches)
243
237
  // As a patch
244
- bus.set.fire(key, {patch: new_version.patches.map(
245
- patch => patch.range + ' = ' + patch.content
246
- )})
238
+ bus.set.fire(key, {patches: update.patches})
247
239
  },
248
240
  reconnect
249
241
  )).catch(reconnect)
package/extras/tests.js CHANGED
@@ -107,6 +107,15 @@ test(function applying_patches (done) {
107
107
  [[1,2,3], '[-1:-0] = [0, 0, 0]', [1, 2, 0, 0, 0]]
108
108
  ]
109
109
 
110
+ // Convert to braid patch format
111
+ for (var i=0; i<tests.length; i++) {
112
+ var match = tests[i][1].match(/(.*) = (.*)/)
113
+ tests[i][1] = {unit: 'json',
114
+ range: match[1],
115
+ content: match[2]}
116
+ }
117
+
118
+ // Test them
110
119
  for (var i=0; i<tests.length; i++) {
111
120
  var x = bus.apply_patch(tests[i][0], tests[i][1])
112
121
  assert(bus.deep_equals(x, tests[i][2]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.0.27",
3
+ "version": "7.0.30",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/server-library.js CHANGED
@@ -229,13 +229,10 @@ function import_server (bus, make_statebus, options)
229
229
 
230
230
  var patches = req.patches().then(patches => {
231
231
  console.log("...and we see these patches:", patches)
232
- var statebus_patches = patches.map(patch =>
233
- patch.range + ' = ' + patch.content
234
- )
235
232
  var cbus = bus.bus_for_http_client(req, res)
236
233
  var key = req.url.substr(1)
237
234
  cbus.get_once(key, (obj) => {
238
- cbus.set(key, {patch: statebus_patches})
235
+ cbus.set(key, {patches})
239
236
  res.sendStatus(200)
240
237
  console.log('We just processed a patch!')
241
238
  })
@@ -463,8 +460,8 @@ function import_server (bus, make_statebus, options)
463
460
  var msg = {set: obj}
464
461
  if (t.version) msg.version = t.version
465
462
  if (t.parents) msg.parents = t.parents
466
- if (t.patch) msg.patch = t.patch
467
- if (t.patch) msg.set = msg.set.key
463
+ if (t.patches) msg.patches = t.patches
464
+ if (t.patches) msg.set = msg.set.key
468
465
  msg = JSON.stringify(msg)
469
466
 
470
467
  if (master.simulate_network_delay) {
@@ -489,7 +486,7 @@ function import_server (bus, make_statebus, options)
489
486
  ||
490
487
  (method === 'set'
491
488
  && master.validate(message, {set: '*',
492
- '?parents': 'array', '?version': 'string', '?patch': 'array'})
489
+ '?parents': 'array', '?version': 'string', '?patches': 'array'})
493
490
  && (typeof(message.set) === 'string'
494
491
  || (typeof(message.set) === 'object'
495
492
  && typeof(message.set.key === 'string'))))
@@ -522,10 +519,10 @@ function import_server (bus, make_statebus, options)
522
519
  break
523
520
  case 'set':
524
521
  message.version = message.version || client.new_version()
525
- if (message.patch) {
522
+ if (message.patches) {
526
523
  var o = bus.cache[message.set] || {key: message.set}
527
524
  try {
528
- message.set = bus.apply_patch(o.val, message.patch[0])
525
+ message.set = bus.apply_patch(o.val, message.patches[0])
529
526
  } catch (e) {
530
527
  console.error('Received bad sockjs message from '
531
528
  + conn.remoteAddress +': ', message, e)
@@ -535,7 +532,7 @@ function import_server (bus, make_statebus, options)
535
532
  client.set(message.set,
536
533
  {version: message.version,
537
534
  parents: message.parents,
538
- patch: message.patch})
535
+ patches: message.patches})
539
536
  if (our_gets_in[message.set.key]) { // Store what we've seen if we
540
537
  // might have to publish it later
541
538
  client.log('Adding', message.set.key+'#'+message.version,
package/statebus.js CHANGED
@@ -95,22 +95,22 @@
95
95
  function set (obj, t) {
96
96
  // First let's handle patches. We receive them as:
97
97
  //
98
- // set("foo", {patch: ['.foo.bar = "blah"]})
98
+ // set("foo", {patches: [{...}, ...]})
99
99
  //
100
100
  // Note the `obj` will actually be a string, set to the key in this case.
101
101
  //
102
- if (typeof obj === 'string' && t && t.patch) {
103
- if (typeof t.patch == 'string') t.patch = [t.patch]
102
+ if (typeof obj === 'string' && t && t.patches) {
103
+ if (!Array.isArray(t.patches)) t.patches = [t.patches]
104
104
  // Apply the patch locally
105
105
  var key = obj,
106
106
  obj = bus.cache[key]
107
107
 
108
- console.log('set: applying the patch!', {
108
+ console.log('set: applying the patches!', {
109
109
  key: key,
110
110
  obj: obj,
111
- patch: t.patch[0]
111
+ patches: t.patches[0]
112
112
  })
113
- obj.val = apply_patch(obj.val, t.patch[0])
113
+ obj.val = apply_patch(obj.val, t.patches[0])
114
114
  }
115
115
 
116
116
  if (!('key' in obj) || typeof obj.key !== 'string') {
@@ -188,22 +188,22 @@
188
188
 
189
189
  // Handle patches. We receive them as:
190
190
  //
191
- // set.fire("foo", {patch: ['.foo.bar = "blah"]})
191
+ // set.fire("foo", {patches: [{...}, ...]})
192
192
  //
193
193
  // Note the `obj` will actually be a string, set to the key in this case.
194
194
  //
195
195
  if (typeof obj === 'string' && t && t.patch) {
196
- if (typeof t.patch == 'string') t.patch = [t.patch]
197
- // Apply the patch locally
196
+ if (!Array.isArray(t.patches)) t.patches = [t.patches]
197
+ // Apply the patches locally
198
198
  var key = obj,
199
199
  obj = bus.cache[key]
200
200
 
201
- // console.log('set.fire: applying the patch!', {
201
+ // console.log('set.fire: applying the patches!', {
202
202
  // key: key,
203
203
  // obj: obj,
204
- // patch: t.patch[0]
204
+ // patch: t.patches[0]
205
205
  // })
206
- obj.val = apply_patch(obj.val, t.patch[0])
206
+ obj.val = apply_patch(obj.val, t.patches[0])
207
207
  }
208
208
 
209
209
  // Print a statelog entry
@@ -1337,7 +1337,7 @@
1337
1337
 
1338
1338
 
1339
1339
  return new Proxy(o, {
1340
- get: function get(o, k) {
1340
+ get: function (o, k) {
1341
1341
  if (k === 'inspect' || k === 'valueOf')
1342
1342
  return undefined
1343
1343
  // if (custom_inspect && k === custom_inspect)
@@ -1349,31 +1349,41 @@
1349
1349
 
1350
1350
  // Compute the new path
1351
1351
  var new_path = path + '[' + JSON.stringify(k) + ']'
1352
- return item_proxy(base, new_path, o[escape_field_to_nelson(escape_field_to_bus(k))])
1352
+ return item_proxy(base, new_path, o[escape_field_to_bus(escape_field_to_nelson(k))])
1353
1353
  },
1354
- set: function set(o, k, v) {
1354
+ set: function (o, k, v) {
1355
1355
  var value = translate_fields(v, field => escape_field_to_bus(escape_field_to_nelson(field)))
1356
- o[escape_field_to_bus(k)] = value
1356
+ o[escape_field_to_bus(escape_field_to_nelson(k))] = value
1357
1357
  var new_path = path + '[' + JSON.stringify(k) + ']'
1358
1358
  bus.set(
1359
1359
  base,
1360
- // Forward the patch too
1361
- {patch: [new_path + ' = ' + JSON.stringify(v)]}
1360
+ // Forward the patches too
1361
+ {patches: [{unit: 'json',
1362
+ range: new_path,
1363
+ content: JSON.stringify(v)}]}
1362
1364
  )
1363
1365
  return true
1364
1366
  },
1365
- has: function has(o, k) {
1367
+ has: function (o, k) {
1366
1368
  // if (custom_inspect && k === custom_inspect)
1367
1369
  // return true
1368
- return o.hasOwnProperty(escape_field_to_bus(k))
1370
+ return o.hasOwnProperty(escape_field_to_bus(escape_field_to_nelson(k)))
1371
+ },
1372
+ ownKeys: function () {
1373
+ return Object.keys(o).map(unescape_field_from_nelson).map(unescape_field_from_bus)
1374
+ },
1375
+ getOwnPropertyDescriptor: function (target, key) {
1376
+ return { enumerable: true, configurable: true, value: this.get(o, key) }
1369
1377
  },
1370
1378
  deleteProperty: function del (o, k) {
1371
1379
  var new_path = path + '[' + JSON.stringify(k) + ']'
1372
- delete o[escape_field_to_bus(k)]
1380
+ delete o[escape_field_to_bus(escape_field_to_nelson(k))]
1373
1381
  bus.set(
1374
1382
  base,
1375
- // Forward the patch too
1376
- {patch: [new_path + ' = ']}
1383
+ // Forward the patches too
1384
+ {patches: [{unit: 'json',
1385
+ range: new_path,
1386
+ content: undefined}]}
1377
1387
  )
1378
1388
  return true // Report success to Proxy
1379
1389
  }
@@ -1429,9 +1439,10 @@
1429
1439
  if (!nodejs)
1430
1440
  window.devtoolsFormatters = [{
1431
1441
  header: function (x) {
1432
- if (x[symbols.is_proxy])
1442
+ if (x[symbols.is_proxy]) {
1433
1443
  return ['span', {style: 'background-color: #fffbe5; padding: 3px;'},
1434
- JSON.stringify(unescape_from_nelson(unescape_from_bus(x)))]
1444
+ JSON.stringify(x)]
1445
+ }
1435
1446
  // For function proxies:
1436
1447
  // JSON.stringify(x(), null, 2)]
1437
1448
  },
@@ -1510,8 +1521,8 @@
1510
1521
  var x = {set: obj}
1511
1522
  if (t.version) x.version = t.version
1512
1523
  if (t.parents) x.parents = t.parents
1513
- if (t.patch) x.patch = t.patch
1514
- if (t.patch) x.set = rem_prefix(x.set.key)
1524
+ if (t.patches) x.patches = t.patches
1525
+ if (t.patches) x.set = rem_prefix(x.set.key)
1515
1526
  send(x)
1516
1527
  }
1517
1528
  bus(prefix).getter = function (key) { send({get: key}),
@@ -1612,24 +1623,24 @@
1612
1623
  bus.log('net client received', message)
1613
1624
  var t = {version: message.version,
1614
1625
  parents: message.parents,
1615
- patch: message.patch}
1626
+ patches: message.patches}
1616
1627
 
1617
1628
  var obj
1618
1629
 
1619
- // Are we receiving a patch?
1620
- if (message.patch && typeof message.set === 'string') {
1630
+ // Are we receiving a patches?
1631
+ if (message.patches && typeof message.set === 'string') {
1621
1632
  // Then message.set is a key, and we are applying a
1622
1633
  // patch to the data at that key
1623
1634
  var key = message.set
1624
1635
  obj = apply_patch(bus.cache[key] && bus.cache[key].val,
1625
- message.patch[0])
1636
+ message.patches[0])
1626
1637
  }
1627
1638
 
1628
1639
  // Then we're receiving the full state as an object
1629
1640
  else
1630
1641
  obj = message.set
1631
1642
 
1632
- if (!(t.version||t.parents||t.patch))
1643
+ if (!(t.version||t.parents||t.patches))
1633
1644
  t = undefined
1634
1645
 
1635
1646
  bus.set.fire(add_prefixes(message.set), t)
@@ -1789,9 +1800,9 @@
1789
1800
  // The final object can be a slice
1790
1801
  // Set the value in the final object
1791
1802
 
1792
- var match = patch.match(/(.*) = (.*)/),
1793
- path = match[1],
1794
- new_stuff = match[2].length ? JSON.parse(match[2]) : undefined
1803
+ console.assert(patch.unit === 'json', "Can't apply non-json patches")
1804
+
1805
+ var path = patch.range, new_stuff = JSON.parse(patch.content)
1795
1806
 
1796
1807
  var path_segment = /^(\.?([^\.\[]+))|(\[((-?\d+):)?(-?\d+)\])|\[("(\\"|[^"])*")\]/
1797
1808
  var curr_obj = obj,