statebus 6.1.32 → 6.2.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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/server.js +51 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "6.1.32",
3
+ "version": "6.2.0",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
@@ -18,10 +18,11 @@
18
18
  "dependencies": {
19
19
  "bcrypt-nodejs": "0.0.3",
20
20
  "braidify": "^0.1.18",
21
- "chokidar": "^3.4.0",
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
@@ -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
  }
@@ -262,10 +275,11 @@ function import_server (bus, options)
262
275
  if (typeof req.headers['content-type'] !== 'string'
263
276
  || req.headers['content-type'].toLowerCase() !== 'application/json')
264
277
  console.error('Error: PUT content-type is not application/json')
265
- var body = ''
266
- req.on('data', chunk => {body += chunk.toString()})
278
+ var chunks = []
279
+ req.on('data', chunk => {chunks.push(chunk)})
267
280
  req.on('end', () => {
268
281
  try {
282
+ var body = Buffer.concat(chunks).toString()
269
283
  console.log('gonna parse', body)
270
284
  var path = req.url.substr(1)
271
285
  var obj = bus.from_http_body(path, body)
@@ -308,15 +322,7 @@ function import_server (bus, options)
308
322
  if (options.use_ssl) {
309
323
  // Load with TLS/SSL
310
324
  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
-
325
+ var http1 = require('https')
320
326
  var protocol = 'https'
321
327
  var ssl_options = {
322
328
  ca: (fs.existsSync(this.options.certs.certificate_bundle)
@@ -332,11 +338,26 @@ function import_server (bus, options)
332
338
  else {
333
339
  // Load unencrypted server
334
340
  console.log('Encryption OFF')
335
- var http = require('http')
341
+ var http1 = require('http')
336
342
  var protocol = 'http'
337
343
  var ssl_options = undefined
338
344
  }
339
345
 
346
+ // Create the http server
347
+ var http_server
348
+ if (options.use_http2) {
349
+ console.log('Using HTTP/2')
350
+ // The HTTP2 way
351
+ http2 = require('http2')
352
+ http_server = http2[options.use_ssl ? 'createSecureServer' : 'createServer'](
353
+ {...ssl_options, allowHTTP1: true}
354
+ )
355
+ } else
356
+ // Or the HTTP1 way
357
+ http_server = http1.createServer(ssl_options)
358
+
359
+ // Choose the port to listen on
360
+ var port
340
361
  if (options.port === 'auto') {
341
362
  var bind = require('./extras/tcp-bind')
342
363
  function find_a_port () {
@@ -360,18 +381,16 @@ function import_server (bus, options)
360
381
  bus.redirect_port_80()
361
382
  } catch (e) {fd = find_a_port()}
362
383
  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
- })
384
+
385
+ port = {fd: fd}
374
386
  }
387
+ else
388
+ port = bus.port = bus.options.port
389
+
390
+ // And start the server listening on the port!
391
+ http_server.listen(port, () => {
392
+ console.log('Listening on '+protocol+'://<host>:'+bus.port)
393
+ })
375
394
 
376
395
  bus[options.name || 'http_server'] = http_server
377
396
  },