braid-http 1.3.57 → 1.3.59

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/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Braid-HTTP
2
2
 
3
- This polyfill library implements the [Braid-HTTP v04 protocol](https://github.com/braid-org/braid-spec/blob/master/draft-toomim-httpbis-braid-http-04.txt), modified slightly to follow the HTTP Multiresponse concept discussed at [braid.org/meeting-89](https://braid.org/meeting-89). It provides browsers with a `braid_fetch()` drop-in replacement for the `fetch()` API, and offers nodejs an `http` plugin, enabling simple Braid communication.
3
+ This polyfill library implements the [Braid-HTTP v04
4
+ protocol](https://github.com/braid-org/braid-spec/blob/master/draft-toomim-httpbis-braid-http-04.txt),
5
+ with [HTTP Multiresponse](https://braid.org/meeting-89), and [Multiplexing
6
+ v1.0](https://braid.org/protocol/multiplexing). It provides browsers with a
7
+ `braid_fetch()` drop-in replacement for the `fetch()` API, and offers nodejs
8
+ an `http` plugin, enabling simple Braid communication.
4
9
 
5
10
  Developed in [braid.org](https://braid.org).
6
11
 
@@ -143,18 +148,17 @@ var braidify = require('braid-http').http_server
143
148
  import {http_server as braidify} from 'braid-http'
144
149
 
145
150
  require('http').createServer(
146
- (req, res) => {
147
- // Add braid stuff to req and res
148
- braidify(req, res)
151
+ braidify((req, res) => {
152
+ // Now braid stuff is available on req and res
149
153
 
150
- // Now use it
154
+ // So you can easily handle subscriptions
151
155
  if (req.subscribe)
152
156
  res.startSubscription({ onClose: _=> null })
153
157
  // startSubscription automatically sets statusCode = 209
154
158
  else
155
159
  res.statusCode = 200
156
160
 
157
- // Send the current version
161
+ // And send updates over a subscription
158
162
  res.sendUpdate({
159
163
  version: ['greg'],
160
164
  body: JSON.stringify({greg: 'greg'})
@@ -165,8 +169,8 @@ require('http').createServer(
165
169
 
166
170
  ### Example Nodejs server with `require('express')`
167
171
 
168
- With `express`, you can simply call `app.use(braidify)` to get braid features
169
- added to every request and response.
172
+ Or if you're using `express`, you can just call `app.use(braidify)` to get
173
+ braid features added to every request and response.
170
174
 
171
175
  ```javascript
172
176
  var braidify = require('braid-http').http_server
@@ -900,14 +900,16 @@ async function multiplex_fetch(url, params, mux_params) {
900
900
  var mux_key = params.headers.get('multiplex-through')?.split('/')[3] ?? origin
901
901
 
902
902
  // create a new multiplexer if it doesn't exist for this origin
903
- if (!multiplex_fetch.multiplexers) multiplex_fetch.multiplexers = {}
903
+ if (!multiplex_fetch.multiplexers)
904
+ multiplex_fetch.multiplexers = {}
904
905
 
905
906
  // this for-loop allows us to retry right away,
906
907
  // in case of duplicate ids
907
908
  for (let attempt = 1; ; attempt++) {
908
909
  await new Promise(done => setTimeout(done, attempt >= 3 ? 1000 : 0))
909
910
 
910
- if (!multiplex_fetch.multiplexers[mux_key]) multiplex_fetch.multiplexers[mux_key] =
911
+ // Create a multiplexer if it does not exist yet
912
+ multiplex_fetch.multiplexers[mux_key] ||=
911
913
  create_multiplexer(origin, mux_key, params, mux_params, attempt)
912
914
 
913
915
  // call the special fetch function for the multiplexer
@@ -251,16 +251,23 @@ function braidify (req, res, next) {
251
251
  // Multiplexer stuff
252
252
  var multiplex_version = '1.0'
253
253
  if ((braidify.enable_multiplex ?? true) &&
254
- (req.method === 'MULTIPLEX' || req.url.startsWith('/.well-known/multiplexer/')) &&
255
- req.headers['multiplex-version'] === multiplex_version) {
256
-
257
- // let the caller know we're handling things
258
- req.is_multiplexer = res.is_multiplexer = true
254
+ (req.method === 'MULTIPLEX' || req.url.startsWith('/.well-known/multiplexer/'))) {
259
255
 
260
256
  // free the cors
261
257
  res.setHeader("Access-Control-Allow-Origin", "*")
262
258
  res.setHeader("Access-Control-Allow-Methods", "*")
263
259
  res.setHeader("Access-Control-Allow-Headers", "*")
260
+ res.setHeader("Access-Control-Expose-Headers", "*")
261
+ if (req.method === 'OPTIONS') return res.end()
262
+
263
+ // check the multiplexing protocol version
264
+ if (req.headers['multiplex-version'] !== multiplex_version) {
265
+ res.writeHead(400, 'Bad Multiplexer Version')
266
+ return res.end()
267
+ }
268
+
269
+ // let the caller know we're handling things
270
+ req.is_multiplexer = res.is_multiplexer = true
264
271
 
265
272
  // parse the multiplexer id and request id from the url
266
273
  var [multiplexer, request] = req.url.split('/').slice(req.method === 'MULTIPLEX' ? 1 : 3)
@@ -293,7 +300,7 @@ function braidify (req, res, next) {
293
300
  res.writeHead(200, 'OK', {
294
301
  'Multiplex-Version': multiplex_version,
295
302
  'Incremental': '?1',
296
- 'Cache-Control': 'no-cache',
303
+ 'Cache-Control': 'no-store',
297
304
  'X-Accel-Buffering': 'no',
298
305
  ...req.httpVersion !== '2.0' && {'Connection': 'keep-alive'}
299
306
  })
@@ -373,7 +380,7 @@ function braidify (req, res, next) {
373
380
 
374
381
  // copy any CORS headers from the user
375
382
  var cors_headers = Object.entries(res2.getHeaders()).
376
- filter(x => braidify.cors_headers.has(x.key))
383
+ filter(x => braidify.cors_headers.has(x[0]))
377
384
 
378
385
  if (og_stream) {
379
386
  og_stream.respond({
@@ -440,6 +447,10 @@ function braidify (req, res, next) {
440
447
  // we want to send a special message to the multiplexer saying so
441
448
  res2.on('finish', () => m.res.write(`close response ${request}\r\n`))
442
449
 
450
+ // copy over any headers which have already been set on res to res2
451
+ for (let x of Object.entries(res.getHeaders()))
452
+ res2.setHeader(...x)
453
+
443
454
  // we want access to "res" to be forwarded to our fake "res2",
444
455
  // so that it goes into the multiplexer
445
456
  function* get_props(obj) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.57",
3
+ "version": "1.3.59",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"