statebus 6.1.16 → 6.1.20
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 +20 -14
- package/extras/tests.js +3 -3
- package/package.json +1 -1
- package/server.js +25 -20
- package/statebus.js +27 -8
package/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
(function () {
|
|
2
|
-
var
|
|
2
|
+
var websocket_prefix = (clientjs_option('websocket_path')
|
|
3
|
+
|| '_connect_to_statebus_')
|
|
3
4
|
|
|
4
5
|
window.dom = window.dom || new Proxy({}, {
|
|
5
6
|
get: function (o, k) { return o[k] },
|
|
@@ -37,8 +38,8 @@
|
|
|
37
38
|
// url = link.href
|
|
38
39
|
// }
|
|
39
40
|
console.log('opening websocket to', url)
|
|
40
|
-
return new WebSocket(url + '/' +
|
|
41
|
-
// return new SockJS(url + '/' +
|
|
41
|
+
return new WebSocket(url + '/' + websocket_prefix + '/websocket')
|
|
42
|
+
// return new SockJS(url + '/' + websocket_prefix)
|
|
42
43
|
}
|
|
43
44
|
function client_creds (server_url) {
|
|
44
45
|
var me = bus.fetch('ls/me')
|
|
@@ -62,7 +63,7 @@
|
|
|
62
63
|
var has_prefix = new RegExp('^' + preprefix)
|
|
63
64
|
var is_absolute = /^https?:\/\//
|
|
64
65
|
var client_fetched_keys = new bus.Set()
|
|
65
|
-
if (url[url.length-1]=='/') url = url.substr(0,url.length-1)
|
|
66
|
+
//if (url[url.length-1]=='/') url = url.substr(0,url.length-1)
|
|
66
67
|
function add_prefix (key) {
|
|
67
68
|
return is_absolute.test(key) ? key : preprefix + key }
|
|
68
69
|
function rem_prefix (key) {
|
|
@@ -73,7 +74,6 @@
|
|
|
73
74
|
return bus.translate_keys(bus.clone(obj), rem_prefix) }
|
|
74
75
|
|
|
75
76
|
bus(prefix).to_save = function (obj, t) {
|
|
76
|
-
console.log('saving', prefix, obj)
|
|
77
77
|
bus.save.fire(obj)
|
|
78
78
|
|
|
79
79
|
// var x = {save: obj}
|
|
@@ -82,9 +82,10 @@
|
|
|
82
82
|
// if (t.patch) x.patch = t.patch
|
|
83
83
|
// if (t.patch) x.save = rem_prefix(x.save.key)
|
|
84
84
|
|
|
85
|
-
braid_fetch(url + obj.key,
|
|
85
|
+
braid_fetch(url + rem_prefix(obj.key),
|
|
86
86
|
{
|
|
87
87
|
method: 'put',
|
|
88
|
+
'content-type': 'application/json',
|
|
88
89
|
body: JSON.stringify(obj.val)
|
|
89
90
|
})
|
|
90
91
|
}
|
|
@@ -94,11 +95,12 @@
|
|
|
94
95
|
braid_fetch(url + rem_prefix(key),
|
|
95
96
|
{
|
|
96
97
|
method: 'get',
|
|
97
|
-
subscribe: true
|
|
98
|
+
subscribe: true,
|
|
99
|
+
headers: {accept: 'application/json'}
|
|
98
100
|
}
|
|
99
|
-
).andThen( x
|
|
101
|
+
).andThen( function (x) {
|
|
100
102
|
t.return({
|
|
101
|
-
key,
|
|
103
|
+
key: key,
|
|
102
104
|
val: add_prefixes(JSON.parse(x.body))
|
|
103
105
|
})
|
|
104
106
|
})
|
|
@@ -439,7 +441,7 @@
|
|
|
439
441
|
make_client_statebus_maker()
|
|
440
442
|
window.bus = window.statebus()
|
|
441
443
|
bus.label = 'bus'
|
|
442
|
-
if (clientjs_option('
|
|
444
|
+
if (clientjs_option('braid_mode'))
|
|
443
445
|
bus.state = bus.braid_proxy()
|
|
444
446
|
else
|
|
445
447
|
window.sb = bus.sb
|
|
@@ -450,9 +452,13 @@
|
|
|
450
452
|
improve_react()
|
|
451
453
|
window.ignore_flashbacks = false
|
|
452
454
|
if (statebus_server !== 'none') {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
braid_http_mount ('
|
|
455
|
+
if (clientjs_option('braid_mode')) {
|
|
456
|
+
console.log('We socket-free!')
|
|
457
|
+
braid_http_mount ('/*', statebus_server)
|
|
458
|
+
} else {
|
|
459
|
+
bus.net_mount ('/*', statebus_server)
|
|
460
|
+
console.log('We going websocket!')
|
|
461
|
+
}
|
|
456
462
|
}
|
|
457
463
|
|
|
458
464
|
if (window.statebus_backdoor) {
|
|
@@ -462,7 +468,7 @@
|
|
|
462
468
|
bus.net_automount()
|
|
463
469
|
|
|
464
470
|
// This /new/* code is deprecated
|
|
465
|
-
if (!clientjs_option('
|
|
471
|
+
if (!clientjs_option('braid_mode')) {
|
|
466
472
|
bus('/new/*').to_save = function (o) {
|
|
467
473
|
if (o.key.split('/').length > 3) return
|
|
468
474
|
|
package/extras/tests.js
CHANGED
|
@@ -872,7 +872,7 @@ test(function proxies2 (done) {
|
|
|
872
872
|
})
|
|
873
873
|
|
|
874
874
|
test(function braid_proxies (done) {
|
|
875
|
-
var bus = require('../statebus').serve({
|
|
875
|
+
var bus = require('../statebus').serve({braid_mode: true,
|
|
876
876
|
file_store: false})
|
|
877
877
|
var state = bus.state
|
|
878
878
|
|
|
@@ -896,8 +896,8 @@ test(function braid_proxies (done) {
|
|
|
896
896
|
log('array:', state.array)
|
|
897
897
|
log('bar:', state.bar)
|
|
898
898
|
assert(state.bar.a[1] !== 2,
|
|
899
|
-
"Array ref linked.\n\
|
|
900
|
-
+ JSON.stringify(bus.cache.bar) + '\n\
|
|
899
|
+
"Array ref linked.\n\tbar: "
|
|
900
|
+
+ JSON.stringify(bus.cache.bar) + '\n\tarray: '
|
|
901
901
|
+ JSON.stringify(bus.cache.array))
|
|
902
902
|
|
|
903
903
|
state.undefining = undefined
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
var fs = require('fs'),
|
|
2
2
|
util = require('util')
|
|
3
|
-
var unique_sockjs_string = '_connect_to_statebus_'
|
|
4
3
|
|
|
5
4
|
// Import braidify if it's available
|
|
6
5
|
try {
|
|
@@ -22,6 +21,7 @@ function default_options (bus) { return {
|
|
|
22
21
|
certificate: 'certs/certificate',
|
|
23
22
|
certificate_bundle: 'certs/certificate-bundle'},
|
|
24
23
|
connections: {include_users: true, edit_others: true},
|
|
24
|
+
websocket_path: '_connect_to_statebus_',
|
|
25
25
|
__secure: false
|
|
26
26
|
}}
|
|
27
27
|
|
|
@@ -106,9 +106,6 @@ function import_server (bus, options)
|
|
|
106
106
|
bus.sync_files(x.state_path, x.fs_path)
|
|
107
107
|
})
|
|
108
108
|
|
|
109
|
-
// User will put his routes in here
|
|
110
|
-
bus.express.use('/', bus.http)
|
|
111
|
-
|
|
112
109
|
// Free the CORS for Braid requests!
|
|
113
110
|
bus.express.use((req, res, next) => {
|
|
114
111
|
// If this requests knows about Braid then we presume the
|
|
@@ -122,6 +119,9 @@ function import_server (bus, options)
|
|
|
122
119
|
next()
|
|
123
120
|
})
|
|
124
121
|
|
|
122
|
+
// User will put his routes in here
|
|
123
|
+
bus.express.use(bus.http)
|
|
124
|
+
|
|
125
125
|
// Connect bus to the HTTP server
|
|
126
126
|
bus.express.use(bus.http_handlers)
|
|
127
127
|
|
|
@@ -175,7 +175,7 @@ function import_server (bus, options)
|
|
|
175
175
|
|
|
176
176
|
initialize_braid_http: function () {
|
|
177
177
|
// Set up default linked json converters
|
|
178
|
-
if (bus.options.
|
|
178
|
+
if (bus.options.braid_mode) {
|
|
179
179
|
bus.to_http_body = (o) => JSON.stringify(o.val)
|
|
180
180
|
bus.state = bus.braid_proxy()
|
|
181
181
|
} else
|
|
@@ -219,16 +219,17 @@ function import_server (bus, options)
|
|
|
219
219
|
cbus.fetch(key, cb = (o) => {
|
|
220
220
|
var body = bus.to_http_body(o)
|
|
221
221
|
|
|
222
|
-
// Return 404 if the value is undefined
|
|
223
|
-
if (!body) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
222
|
+
// // Return 404 if the value is undefined
|
|
223
|
+
// if (!body) {
|
|
224
|
+
// console.log('no body! time to 404')
|
|
225
|
+
// res.statusCode = 404
|
|
226
|
+
// res.end()
|
|
227
|
+
// }
|
|
228
228
|
|
|
229
229
|
// Or if we're braid, send via subscription
|
|
230
|
-
else
|
|
231
|
-
|
|
230
|
+
// else
|
|
231
|
+
if (braidify)
|
|
232
|
+
res.sendVersion({body: body || 'null'})
|
|
232
233
|
|
|
233
234
|
// Or just return the current version
|
|
234
235
|
else
|
|
@@ -385,7 +386,8 @@ function import_server (bus, options)
|
|
|
385
386
|
this.http_server.on('request', // Install express
|
|
386
387
|
function (request, response) {
|
|
387
388
|
// But express should ignore all sockjs requests
|
|
388
|
-
if (!request.url.startsWith(
|
|
389
|
+
if (!request.url.startsWith(
|
|
390
|
+
'/' + bus.options.websocket_path + '/'))
|
|
389
391
|
express_app(request, response)
|
|
390
392
|
})
|
|
391
393
|
|
|
@@ -585,7 +587,7 @@ function import_server (bus, options)
|
|
|
585
587
|
}
|
|
586
588
|
})
|
|
587
589
|
|
|
588
|
-
s.installHandlers(httpserver, {prefix:'/' +
|
|
590
|
+
s.installHandlers(httpserver, {prefix:'/' + bus.options.websocket_path})
|
|
589
591
|
},
|
|
590
592
|
|
|
591
593
|
make_websocket: function make_websocket (url) {
|
|
@@ -593,7 +595,7 @@ function import_server (bus, options)
|
|
|
593
595
|
url = url.replace(/^istate:\/\//, 'ws://')
|
|
594
596
|
url = url.replace(/^statei:\/\//, 'ws://')
|
|
595
597
|
WebSocket = require('websocket').w3cwebsocket
|
|
596
|
-
return new WebSocket(url+'/'+
|
|
598
|
+
return new WebSocket(url+'/'+bus.options.websocket_path+'/websocket')
|
|
597
599
|
},
|
|
598
600
|
client_creds: function client_creds (server_url) {
|
|
599
601
|
// Right now the server just creates a different random id each time
|
|
@@ -2387,11 +2389,14 @@ function import_server (bus, options)
|
|
|
2387
2389
|
serve_clientjs: function serve_clientjs (path) {
|
|
2388
2390
|
path = path || 'client.js'
|
|
2389
2391
|
bus.http.get('/' + path, (req, res) => {
|
|
2390
|
-
|
|
2392
|
+
var files =
|
|
2391
2393
|
['extras/coffee.js', 'extras/sockjs.js', 'extras/react.js',
|
|
2392
|
-
'statebus.js', 'client.js']
|
|
2393
|
-
|
|
2394
|
-
|
|
2394
|
+
'statebus.js', 'client.js'].map((f) => fs.readFileSync('node_modules/statebus/' + f))
|
|
2395
|
+
if (bus.options.braid_mode)
|
|
2396
|
+
files.unshift(fs.readFileSync(
|
|
2397
|
+
'node_modules/braidify/braidify-client.js'
|
|
2398
|
+
))
|
|
2399
|
+
res.send(files.join(';\n'))
|
|
2395
2400
|
})
|
|
2396
2401
|
},
|
|
2397
2402
|
|
package/statebus.js
CHANGED
|
@@ -1552,6 +1552,8 @@
|
|
|
1552
1552
|
else if (x[symbols.is_braid_proxy])
|
|
1553
1553
|
return ['span', {style: 'background-color: #fffbe5; padding: 3px;'},
|
|
1554
1554
|
JSON.stringify(x)]
|
|
1555
|
+
// For function proxies:
|
|
1556
|
+
// JSON.stringify(x(), null, 2)]
|
|
1555
1557
|
},
|
|
1556
1558
|
hasBody: function (x) {return false}
|
|
1557
1559
|
}]
|
|
@@ -1585,6 +1587,15 @@
|
|
|
1585
1587
|
return item_proxy(new_base, new_base.val)
|
|
1586
1588
|
}
|
|
1587
1589
|
|
|
1590
|
+
|
|
1591
|
+
// For function proxies:
|
|
1592
|
+
//
|
|
1593
|
+
// // Javascript won't let us function call a proxy unless the
|
|
1594
|
+
// // "target" is a function. So we make a dummy target, and
|
|
1595
|
+
// // don't use it.
|
|
1596
|
+
// var dummy = function () {}
|
|
1597
|
+
|
|
1598
|
+
|
|
1588
1599
|
return new Proxy(o, {
|
|
1589
1600
|
get: function get(o, k) {
|
|
1590
1601
|
if (k === 'inspect' || k === 'valueOf')
|
|
@@ -1606,20 +1617,24 @@
|
|
|
1606
1617
|
},
|
|
1607
1618
|
deleteProperty: function del (o, k) {
|
|
1608
1619
|
delete o[proxied_2_keyed(k)]
|
|
1609
|
-
},
|
|
1610
|
-
apply: function apply (o, This, args) {
|
|
1611
|
-
return o
|
|
1612
1620
|
}
|
|
1621
|
+
// For function proxies:
|
|
1622
|
+
//
|
|
1623
|
+
// apply: function apply (o, This, args) {
|
|
1624
|
+
// return translate_fields(o, keyed_2_proxied)
|
|
1625
|
+
// }
|
|
1613
1626
|
})}
|
|
1614
1627
|
|
|
1615
1628
|
|
|
1629
|
+
// For function proxies:
|
|
1630
|
+
// var dummy = function () {}
|
|
1631
|
+
|
|
1616
1632
|
// The top-level Proxy object holds HTTP resources
|
|
1617
1633
|
return new Proxy(cache, {
|
|
1618
1634
|
get: function get(o, k) {
|
|
1619
1635
|
if (k === 'inspect' || k === 'valueOf' || typeof k === 'symbol')
|
|
1620
1636
|
return undefined
|
|
1621
|
-
|
|
1622
|
-
return o[k]
|
|
1637
|
+
bogus_check(k)
|
|
1623
1638
|
var base = bus.fetch(k)
|
|
1624
1639
|
return item_proxy(base, base.val)
|
|
1625
1640
|
},
|
|
@@ -1630,7 +1645,11 @@
|
|
|
1630
1645
|
},
|
|
1631
1646
|
deleteProperty: function del (o, k) {
|
|
1632
1647
|
bus.delete(proxied_2_keyed(k))
|
|
1633
|
-
}
|
|
1648
|
+
},
|
|
1649
|
+
// For function proxies:
|
|
1650
|
+
// apply: function apply (o, This, args) {
|
|
1651
|
+
// return 'fluffy'
|
|
1652
|
+
// }
|
|
1634
1653
|
})
|
|
1635
1654
|
}
|
|
1636
1655
|
|
|
@@ -1640,8 +1659,8 @@
|
|
|
1640
1659
|
return result
|
|
1641
1660
|
}
|
|
1642
1661
|
|
|
1643
|
-
if ((nodejs && bus.options && bus.options.
|
|
1644
|
-
|| (!nodejs && window.Proxy && clientjs_option('
|
|
1662
|
+
if ((nodejs && bus.options && bus.options.braid_mode)
|
|
1663
|
+
|| (!nodejs && window.Proxy && clientjs_option('braid_mode')))
|
|
1645
1664
|
state = braid_proxy()
|
|
1646
1665
|
|
|
1647
1666
|
|