braid-http 1.3.83 → 1.3.84

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
@@ -48,10 +48,11 @@ import {fetch, http_client, http_server} from 'braid-http'
48
48
  ## Using it in Browsers
49
49
 
50
50
  This library adds a `{subscribe: true}` option to `fetch()`, and lets you
51
- access the result of a subscription with two new fields on the fetch response:
51
+ access the result of a subscription with these new fields on the fetch response:
52
52
 
53
53
  - `response.subscribe( update => ... )`
54
54
  - `response.subscription`: an iterator that can be used with `for await`
55
+ - `response.version`: the parsed version from the response headers (if present)
55
56
 
56
57
  ### Example Subscription with Promises
57
58
 
@@ -363,23 +364,43 @@ nbraidify.enable_multiplex = true // or false
363
364
 
364
365
  ## Test Procedure
365
366
 
366
- Using 3 terminals, in first terminal start the demo chat server:
367
+ Run tests from the command line:
368
+
369
+ ```
370
+ node test/test.js
371
+ ```
372
+
373
+ Or run tests in a browser by starting the test server:
374
+
375
+ ```
376
+ node test/test.js --browser
377
+ ```
378
+
379
+ Then open https://localhost:9000 and make sure all the boxes turn green.
380
+
381
+ You can also filter tests by name:
382
+
383
+ ```
384
+ node test/test.js --filter="version"
385
+ ```
386
+
387
+ For the complete browser test (including demos), use 3 terminals. In the first terminal start the demo chat server:
367
388
 
368
389
  ```
369
390
  cd demos/chat
370
391
  node server.js
371
392
  ```
372
393
 
373
- in second terminal start the demo blog server:
394
+ In the second terminal start the demo blog server:
374
395
  ```
375
396
  cd demos/blog
376
397
  node server.js
377
398
  ```
378
399
 
379
- and in third and final terminal, start the test server:
400
+ And in the third terminal, start the test server:
380
401
  ```
381
- node test/server.js
402
+ node test/test.js --browser
382
403
  ```
383
404
 
384
- now open https://localhost:9000, and make sure all the boxes turn green, and try out the demo chat and blog, sending a message in each.
405
+ Now open https://localhost:9000, make sure all the boxes turn green, and try out the demo chat and blog, sending a message in each.
385
406
 
@@ -471,6 +471,11 @@ async function braid_fetch (url, params = {}) {
471
471
  params?.retry?.onRes?.(res)
472
472
  waitTime = 1
473
473
 
474
+ // parse version if it exists
475
+ var version_header = res.headers.get('version') || res.headers.get('current-version')
476
+ if (version_header)
477
+ try { res.version = JSON.parse('[' + version_header + ']') } catch (e) { console.log('error parsing version: ' + version_header) }
478
+
474
479
  done(res)
475
480
  } catch (e) { on_error(e) }
476
481
  }
@@ -643,7 +648,7 @@ function parse_update (state) {
643
648
  }
644
649
 
645
650
  // Parsing helpers
646
- function parse_headers (input, check_for_encoding_blocks) {
651
+ function parse_headers (input, check_for_encoding_blocks, dont_parse_special_headers) {
647
652
 
648
653
  // Find the start of the headers
649
654
  var start = 0
@@ -738,12 +743,14 @@ function parse_headers (input, check_for_encoding_blocks) {
738
743
  }
739
744
 
740
745
  // Success! Let's parse special headers
741
- if ('version' in headers)
742
- headers.version = JSON.parse('['+headers.version+']')
743
- if ('parents' in headers)
744
- headers.parents = JSON.parse('['+headers.parents+']')
745
- if ('patches' in headers)
746
- headers.patches = JSON.parse(headers.patches)
746
+ if (!dont_parse_special_headers) {
747
+ if ('version' in headers)
748
+ headers.version = JSON.parse('['+headers.version+']')
749
+ if ('parents' in headers)
750
+ headers.parents = JSON.parse('['+headers.parents+']')
751
+ if ('patches' in headers)
752
+ headers.patches = JSON.parse(headers.patches)
753
+ }
747
754
 
748
755
  // Update the input
749
756
  input = input.slice(end)
@@ -1126,6 +1133,9 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
1126
1133
  var request = (attempt === 1
1127
1134
  && params.headers.get('multiplex-through')?.split('/')[4])
1128
1135
  || random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
1136
+
1137
+ // make sure this request id is not already in use
1138
+ if (requests.has(request)) throw "retry"
1129
1139
 
1130
1140
  // add the Multiplex-Through header without affecting the underlying params
1131
1141
  var mux_headers = new Headers(params.headers)
@@ -1178,6 +1188,8 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
1178
1188
 
1179
1189
  // do the underlying fetch
1180
1190
  try {
1191
+ if (attempt > 1) await mux_created_promise
1192
+
1181
1193
  var mux_was_done = await promise_done(mux_created_promise)
1182
1194
 
1183
1195
  // callback for testing
@@ -1252,7 +1264,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
1252
1264
  if (request_ended) buffers.push(null)
1253
1265
 
1254
1266
  // try parsing what we got so far as headers..
1255
- var x = parse_headers(headers_buffer)
1267
+ var x = parse_headers(headers_buffer, false, true)
1256
1268
 
1257
1269
  // how did it go?
1258
1270
  if (x.result === 'error') {
@@ -1394,6 +1406,7 @@ function concat_buffers(buffers) {
1394
1406
  if (typeof module !== 'undefined' && module.exports)
1395
1407
  module.exports = {
1396
1408
  fetch: braid_fetch,
1409
+ multiplex_fetch,
1397
1410
  http: braidify_http,
1398
1411
  subscription_parser,
1399
1412
  parse_update,
@@ -253,6 +253,7 @@ function braidify (req, res, next) {
253
253
  var multiplex_version = '1.0'
254
254
  if ((braidify.enable_multiplex ?? true) &&
255
255
  (req.method === 'MULTIPLEX' || req.url.startsWith('/.well-known/multiplexer/'))) {
256
+ req.is_multiplexer = res.is_multiplexer = true
256
257
 
257
258
  free_cors(res)
258
259
  if (req.method === 'OPTIONS') return res.end()
@@ -263,9 +264,6 @@ function braidify (req, res, next) {
263
264
  return res.end()
264
265
  }
265
266
 
266
- // let the caller know we're handling things
267
- req.is_multiplexer = res.is_multiplexer = true
268
-
269
267
  // parse the multiplexer id and request id from the url
270
268
  var [multiplexer, request] = req.url.split('/').slice(req.method === 'MULTIPLEX' ? 1 : 3)
271
269
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.83",
3
+ "version": "1.3.84",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"