statebus 6.1.31 → 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.
@@ -0,0 +1,3 @@
1
+ import statebus from 'statebus'
2
+
3
+ console.log(statebus)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "6.1.31",
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)
@@ -297,6 +311,7 @@ function import_server (bus, options)
297
311
  cbus.serves_auth({remoteAddress: req.connection.remoteAddress}, bus)
298
312
  bus.options.client(cbus)
299
313
  cbus.save({key: 'current_user', client: req.client})
314
+ req.client_bus = cbus
300
315
  return cbus
301
316
  },
302
317
 
@@ -307,15 +322,7 @@ function import_server (bus, options)
307
322
  if (options.use_ssl) {
308
323
  // Load with TLS/SSL
309
324
  console.log('Encryption ON')
310
-
311
- // use http2 compatible library if available
312
- try {
313
- var http = require('spdy')
314
- console.log('Found spdy library. HTTP/2 enabled!')
315
- } catch (e) {
316
- var http = require('https')
317
- }
318
-
325
+ var http1 = require('https')
319
326
  var protocol = 'https'
320
327
  var ssl_options = {
321
328
  ca: (fs.existsSync(this.options.certs.certificate_bundle)
@@ -331,11 +338,26 @@ function import_server (bus, options)
331
338
  else {
332
339
  // Load unencrypted server
333
340
  console.log('Encryption OFF')
334
- var http = require('http')
341
+ var http1 = require('http')
335
342
  var protocol = 'http'
336
343
  var ssl_options = undefined
337
344
  }
338
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
339
361
  if (options.port === 'auto') {
340
362
  var bind = require('./extras/tcp-bind')
341
363
  function find_a_port () {
@@ -359,18 +381,16 @@ function import_server (bus, options)
359
381
  bus.redirect_port_80()
360
382
  } catch (e) {fd = find_a_port()}
361
383
  else fd = find_a_port()
362
- var http_server = http.createServer(ssl_options)
363
- http_server.listen({fd: fd}, () => {
364
- console.log('Listening on '+protocol+'://<host>:'+bus.port)
365
- })
366
- }
367
- else {
368
- bus.port = bus.options.port
369
- var http_server = http.createServer(ssl_options)
370
- http_server.listen(bus.options.port, () => {
371
- console.log('Listening on '+protocol+'://<host>:'+bus.port)
372
- })
384
+
385
+ port = {fd: fd}
373
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
+ })
374
394
 
375
395
  bus[options.name || 'http_server'] = http_server
376
396
  },