statebus 6.1.32 → 6.2.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.
Files changed (3) hide show
  1. package/client.js +10 -5
  2. package/package.json +5 -4
  3. package/server.js +59 -37
package/client.js CHANGED
@@ -59,6 +59,8 @@
59
59
  }
60
60
 
61
61
  function braid_http_mount (prefix, url) {
62
+ throw "Sorry, braid_http_mount is disabled! Tell mike you're using it, and he'll re-enable it."
63
+
62
64
  var preprefix = prefix.slice(0,-1)
63
65
  var has_prefix = new RegExp('^' + preprefix)
64
66
  var is_absolute = /^https?:\/\//
@@ -553,12 +555,15 @@
553
555
  improve_react()
554
556
  window.ignore_flashbacks = false
555
557
  if (statebus_server !== 'none') {
556
- if (clientjs_option('braid_mode')) {
557
- console.log('Using Braid-HTTP!')
558
- braid_http_mount ('/*', statebus_server)
559
- } else {
558
+ // Disable "braid_mode" for now
559
+ if (clientjs_option('braid_mode'))
560
+ console.log('braid_mode is disabled for now. Tell mike that you\'re using it, and he\'ll re-enable it!')
561
+ // if (clientjs_option('braid_mode')) {
562
+ // console.log('Using Braid-HTTP!')
563
+ // braid_http_mount ('/*', statebus_server)
564
+ // } else {
560
565
  bus.net_mount ('/*', statebus_server)
561
- }
566
+ // }
562
567
  }
563
568
 
564
569
  if (window.statebus_backdoor) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "6.1.32",
3
+ "version": "6.2.1",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
@@ -17,11 +17,12 @@
17
17
  "repository": "invisible-college/statebus",
18
18
  "dependencies": {
19
19
  "bcrypt-nodejs": "0.0.3",
20
- "braidify": "^0.1.18",
21
- "chokidar": "^3.4.0",
20
+ "braid-http": "^1.3.82",
21
+ "chokidar": "^4.0.3",
22
22
  "coffeescript": "^2.3.1",
23
23
  "cookie": "^0.3.1",
24
- "express": "^4.15.3",
24
+ "express": "^4.21.2",
25
+ "http2-express-bridge": "^1.0.7",
25
26
  "sockjs": "^0.3.18",
26
27
  "websocket": "^1.0.24"
27
28
  }
package/server.js CHANGED
@@ -3,8 +3,8 @@ var fs = require('fs'),
3
3
 
4
4
  // Import braidify if it's available
5
5
  try {
6
- var braidify = require('braidify').http_server
7
- console.log('Found braidify library. Braid-HTTP enabled!')
6
+ var braidify = require('braid-http').http_server
7
+ console.log('Found braid-http library. Braid-HTTP enabled!')
8
8
  } catch (e) {
9
9
  var braidify = undefined
10
10
  }
@@ -22,7 +22,8 @@ function default_options (bus) { return {
22
22
  certificate_bundle: 'certs/certificate-bundle'},
23
23
  connections: {include_users: true, edit_others: true},
24
24
  websocket_path: '_connect_to_statebus_',
25
- __secure: false
25
+ __secure: false,
26
+ use_http2: true
26
27
  }}
27
28
 
28
29
  function set_options (bus, options) {
@@ -46,10 +47,10 @@ function set_options (bus, options) {
46
47
  function import_server (bus, options)
47
48
  { var extra_methods = {
48
49
 
49
- serve: function serve (options) {
50
+ serve: function serve (options={}) {
50
51
  var master = bus
51
- bus.honk = options.honk ?? 'statelog'
52
52
  master.label = 'master'
53
+ bus.honk = options.honk ?? 'statelog'
53
54
 
54
55
  // Initialize Options
55
56
  set_options(bus, options)
@@ -59,6 +60,12 @@ function import_server (bus, options)
59
60
  || require('fs').existsSync(bus.options.certs.certificate)
60
61
  || require('fs').existsSync(bus.options.certs.certificate_bundle))
61
62
 
63
+ // Only allow H2 with SSL/TLS for now
64
+ if (bus.options.use_http2 && !use_ssl) {
65
+ console.log('HTTP/2 disabled because we have no TLS/SSL support.')
66
+ bus.options.use_http2 = false
67
+ }
68
+
62
69
  function c (client, conn) {
63
70
  client.honk = bus.honk
64
71
  client.serves_auth(conn, master)
@@ -71,17 +78,22 @@ function import_server (bus, options)
71
78
 
72
79
  // ******************************************
73
80
  // ***** Create our own http server *********
74
- bus.make_http_server({port: bus.options.port, use_ssl})
81
+ bus.make_http_server({port: bus.options.port,
82
+ use_ssl,
83
+ use_http2: bus.options.use_http2})
75
84
  bus.sockjs_server(this.http_server, c) // Serve via sockjs on it
76
85
  var express = require('express')
77
- bus.express = express()
86
+ if (bus.options.use_http2)
87
+ bus.express = require('http2-express-bridge')(express)
88
+ else
89
+ bus.express = express()
78
90
  bus.http = express.Router()
79
91
  bus.install_express(bus.express)
80
92
  bus.initialize_braid_http()
81
93
 
82
- // use gzip compression if available
83
- try { bus.http.use(require('compression')())
84
- console.log('Enabled http compression!') } catch (e) {}
94
+ // // use gzip compression if available
95
+ // try { bus.http.use(require('compression')())
96
+ // console.log('Enabled http compression!') } catch (e) {}
85
97
 
86
98
 
87
99
  // Initialize new clients with an id. We put the client id on
@@ -167,7 +179,8 @@ function import_server (bus, options)
167
179
  bus.make_http_server({
168
180
  port: bus.options.backdoor,
169
181
  name: 'backdoor_http_server',
170
- use_ssl: use_ssl
182
+ use_ssl: use_ssl,
183
+ use_http2: bus.options.use_http2
171
184
  })
172
185
  bus.sockjs_server(this.backdoor_http_server)
173
186
  }
@@ -206,7 +219,10 @@ function import_server (bus, options)
206
219
  res.end()
207
220
  }
208
221
 
209
- braidify && braidify(req, res)
222
+ // Braidify the request
223
+ braidify && braidify(req, res); if (req.is_multiplexer) return
224
+
225
+ // Handle subscription
210
226
  if (req.subscribe) {
211
227
  res.startSubscription({ onClose: end_it_all })
212
228
  console.log('yay subscription!')
@@ -224,7 +240,7 @@ function import_server (bus, options)
224
240
  // Note: if body === undefined, we need to send the
225
241
  // equivalent of a 404. This is missing in braid spec:
226
242
  // https://github.com/braid-org/braid-spec/issues/110
227
- res.sendVersion({body: body || 'null'})
243
+ res.sendUpdate({body: body || 'null'})
228
244
 
229
245
  // Or just return the current version
230
246
  else {
@@ -262,10 +278,11 @@ function import_server (bus, options)
262
278
  if (typeof req.headers['content-type'] !== 'string'
263
279
  || req.headers['content-type'].toLowerCase() !== 'application/json')
264
280
  console.error('Error: PUT content-type is not application/json')
265
- var body = ''
266
- req.on('data', chunk => {body += chunk.toString()})
281
+ var chunks = []
282
+ req.on('data', chunk => {chunks.push(chunk)})
267
283
  req.on('end', () => {
268
284
  try {
285
+ var body = Buffer.concat(chunks).toString()
269
286
  console.log('gonna parse', body)
270
287
  var path = req.url.substr(1)
271
288
  var obj = bus.from_http_body(path, body)
@@ -308,15 +325,7 @@ function import_server (bus, options)
308
325
  if (options.use_ssl) {
309
326
  // Load with TLS/SSL
310
327
  console.log('Encryption ON')
311
-
312
- // use http2 compatible library if available
313
- try {
314
- var http = require('spdy')
315
- console.log('Found spdy library. HTTP/2 enabled!')
316
- } catch (e) {
317
- var http = require('https')
318
- }
319
-
328
+ var http1 = require('https')
320
329
  var protocol = 'https'
321
330
  var ssl_options = {
322
331
  ca: (fs.existsSync(this.options.certs.certificate_bundle)
@@ -332,11 +341,26 @@ function import_server (bus, options)
332
341
  else {
333
342
  // Load unencrypted server
334
343
  console.log('Encryption OFF')
335
- var http = require('http')
344
+ var http1 = require('http')
336
345
  var protocol = 'http'
337
346
  var ssl_options = undefined
338
347
  }
339
348
 
349
+ // Create the http server
350
+ var http_server
351
+ if (options.use_http2) {
352
+ console.log('Using HTTP/2')
353
+ // The HTTP2 way
354
+ http2 = require('http2')
355
+ http_server = http2[options.use_ssl ? 'createSecureServer' : 'createServer'](
356
+ {...ssl_options, allowHTTP1: true}
357
+ )
358
+ } else
359
+ // Or the HTTP1 way
360
+ http_server = http1.createServer(ssl_options)
361
+
362
+ // Choose the port to listen on
363
+ var port
340
364
  if (options.port === 'auto') {
341
365
  var bind = require('./extras/tcp-bind')
342
366
  function find_a_port () {
@@ -360,18 +384,16 @@ function import_server (bus, options)
360
384
  bus.redirect_port_80()
361
385
  } catch (e) {fd = find_a_port()}
362
386
  else fd = find_a_port()
363
- var http_server = http.createServer(ssl_options)
364
- http_server.listen({fd: fd}, () => {
365
- console.log('Listening on '+protocol+'://<host>:'+bus.port)
366
- })
367
- }
368
- else {
369
- bus.port = bus.options.port
370
- var http_server = http.createServer(ssl_options)
371
- http_server.listen(bus.options.port, () => {
372
- console.log('Listening on '+protocol+'://<host>:'+bus.port)
373
- })
387
+
388
+ port = {fd: fd}
374
389
  }
390
+ else
391
+ port = bus.port = bus.options.port
392
+
393
+ // And start the server listening on the port!
394
+ http_server.listen(port, () => {
395
+ console.log('Listening on '+protocol+'://<host>:'+bus.port)
396
+ })
375
397
 
376
398
  bus[options.name || 'http_server'] = http_server
377
399
  },
@@ -2397,7 +2419,7 @@ function import_server (bus, options)
2397
2419
  'statebus.js', 'client.js'].map((f) => fs.readFileSync('node_modules/statebus/' + f))
2398
2420
  if (bus.options.braid_mode)
2399
2421
  files.unshift(fs.readFileSync(
2400
- 'node_modules/braidify/braidify-client.js'
2422
+ 'node_modules/braid-http/braid-http-client.js'
2401
2423
  ))
2402
2424
  res.send(files.join(';\n'))
2403
2425
  })