braid-http 1.3.83 → 1.3.85
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 +27 -6
- package/braid-http-client.js +22 -8
- package/braid-http-server.js +1 -3
- package/package.json +1 -1
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
400
|
+
And in the third terminal, start the test server:
|
|
380
401
|
```
|
|
381
|
-
node test/
|
|
402
|
+
node test/test.js --browser
|
|
382
403
|
```
|
|
383
404
|
|
|
384
|
-
|
|
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
|
|
package/braid-http-client.js
CHANGED
|
@@ -336,6 +336,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
336
336
|
if (isFinite(heartbeats)) {
|
|
337
337
|
let timeout = null
|
|
338
338
|
on_heartbeat = () => {
|
|
339
|
+
params.heartbeat_cb?.()
|
|
339
340
|
clearTimeout(timeout)
|
|
340
341
|
let wait_seconds = 1.2 * heartbeats + 3
|
|
341
342
|
timeout = setTimeout(() => {
|
|
@@ -471,6 +472,11 @@ async function braid_fetch (url, params = {}) {
|
|
|
471
472
|
params?.retry?.onRes?.(res)
|
|
472
473
|
waitTime = 1
|
|
473
474
|
|
|
475
|
+
// parse version if it exists
|
|
476
|
+
var version_header = res.headers.get('version') || res.headers.get('current-version')
|
|
477
|
+
if (version_header)
|
|
478
|
+
try { res.version = JSON.parse('[' + version_header + ']') } catch (e) { console.log('error parsing version: ' + version_header) }
|
|
479
|
+
|
|
474
480
|
done(res)
|
|
475
481
|
} catch (e) { on_error(e) }
|
|
476
482
|
}
|
|
@@ -643,7 +649,7 @@ function parse_update (state) {
|
|
|
643
649
|
}
|
|
644
650
|
|
|
645
651
|
// Parsing helpers
|
|
646
|
-
function parse_headers (input, check_for_encoding_blocks) {
|
|
652
|
+
function parse_headers (input, check_for_encoding_blocks, dont_parse_special_headers) {
|
|
647
653
|
|
|
648
654
|
// Find the start of the headers
|
|
649
655
|
var start = 0
|
|
@@ -738,12 +744,14 @@ function parse_headers (input, check_for_encoding_blocks) {
|
|
|
738
744
|
}
|
|
739
745
|
|
|
740
746
|
// Success! Let's parse special headers
|
|
741
|
-
if (
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
+
if (!dont_parse_special_headers) {
|
|
748
|
+
if ('version' in headers)
|
|
749
|
+
headers.version = JSON.parse('['+headers.version+']')
|
|
750
|
+
if ('parents' in headers)
|
|
751
|
+
headers.parents = JSON.parse('['+headers.parents+']')
|
|
752
|
+
if ('patches' in headers)
|
|
753
|
+
headers.patches = JSON.parse(headers.patches)
|
|
754
|
+
}
|
|
747
755
|
|
|
748
756
|
// Update the input
|
|
749
757
|
input = input.slice(end)
|
|
@@ -1126,6 +1134,9 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1126
1134
|
var request = (attempt === 1
|
|
1127
1135
|
&& params.headers.get('multiplex-through')?.split('/')[4])
|
|
1128
1136
|
|| random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
1137
|
+
|
|
1138
|
+
// make sure this request id is not already in use
|
|
1139
|
+
if (requests.has(request)) throw "retry"
|
|
1129
1140
|
|
|
1130
1141
|
// add the Multiplex-Through header without affecting the underlying params
|
|
1131
1142
|
var mux_headers = new Headers(params.headers)
|
|
@@ -1178,6 +1189,8 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1178
1189
|
|
|
1179
1190
|
// do the underlying fetch
|
|
1180
1191
|
try {
|
|
1192
|
+
if (attempt > 1) await mux_created_promise
|
|
1193
|
+
|
|
1181
1194
|
var mux_was_done = await promise_done(mux_created_promise)
|
|
1182
1195
|
|
|
1183
1196
|
// callback for testing
|
|
@@ -1252,7 +1265,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1252
1265
|
if (request_ended) buffers.push(null)
|
|
1253
1266
|
|
|
1254
1267
|
// try parsing what we got so far as headers..
|
|
1255
|
-
var x = parse_headers(headers_buffer)
|
|
1268
|
+
var x = parse_headers(headers_buffer, false, true)
|
|
1256
1269
|
|
|
1257
1270
|
// how did it go?
|
|
1258
1271
|
if (x.result === 'error') {
|
|
@@ -1394,6 +1407,7 @@ function concat_buffers(buffers) {
|
|
|
1394
1407
|
if (typeof module !== 'undefined' && module.exports)
|
|
1395
1408
|
module.exports = {
|
|
1396
1409
|
fetch: braid_fetch,
|
|
1410
|
+
multiplex_fetch,
|
|
1397
1411
|
http: braidify_http,
|
|
1398
1412
|
subscription_parser,
|
|
1399
1413
|
parse_update,
|
package/braid-http-server.js
CHANGED
|
@@ -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
|
|