braid-http 1.3.82 → 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 +27 -6
- package/braid-http-client.js +31 -11
- 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
|
@@ -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
|
}
|
|
@@ -557,16 +562,23 @@ var subscription_parser = (cb) => ({
|
|
|
557
562
|
for (var k in update)
|
|
558
563
|
if (update[k] === undefined) delete update[k]
|
|
559
564
|
|
|
565
|
+
var body_text_cache = null
|
|
560
566
|
Object.defineProperty(update, 'body_text', {
|
|
561
567
|
get: function () {
|
|
562
|
-
if (
|
|
563
|
-
|
|
568
|
+
if (body_text_cache !== null) return body_text_cache
|
|
569
|
+
return body_text_cache = this.body != null ?
|
|
570
|
+
new TextDecoder('utf-8').decode(this.body.buffer) : undefined
|
|
564
571
|
}
|
|
565
572
|
})
|
|
566
573
|
|
|
567
574
|
for (let p of update.patches ?? []) {
|
|
575
|
+
let content_text_cache = null
|
|
568
576
|
Object.defineProperty(p, 'content_text', {
|
|
569
|
-
get: () =>
|
|
577
|
+
get: () => {
|
|
578
|
+
if (content_text_cache !== null) return content_text_cache
|
|
579
|
+
return content_text_cache =
|
|
580
|
+
new TextDecoder('utf-8').decode(p.content)
|
|
581
|
+
}
|
|
570
582
|
})
|
|
571
583
|
}
|
|
572
584
|
|
|
@@ -636,7 +648,7 @@ function parse_update (state) {
|
|
|
636
648
|
}
|
|
637
649
|
|
|
638
650
|
// Parsing helpers
|
|
639
|
-
function parse_headers (input, check_for_encoding_blocks) {
|
|
651
|
+
function parse_headers (input, check_for_encoding_blocks, dont_parse_special_headers) {
|
|
640
652
|
|
|
641
653
|
// Find the start of the headers
|
|
642
654
|
var start = 0
|
|
@@ -731,12 +743,14 @@ function parse_headers (input, check_for_encoding_blocks) {
|
|
|
731
743
|
}
|
|
732
744
|
|
|
733
745
|
// Success! Let's parse special headers
|
|
734
|
-
if (
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
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
|
+
}
|
|
740
754
|
|
|
741
755
|
// Update the input
|
|
742
756
|
input = input.slice(end)
|
|
@@ -1119,6 +1133,9 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1119
1133
|
var request = (attempt === 1
|
|
1120
1134
|
&& params.headers.get('multiplex-through')?.split('/')[4])
|
|
1121
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"
|
|
1122
1139
|
|
|
1123
1140
|
// add the Multiplex-Through header without affecting the underlying params
|
|
1124
1141
|
var mux_headers = new Headers(params.headers)
|
|
@@ -1171,6 +1188,8 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1171
1188
|
|
|
1172
1189
|
// do the underlying fetch
|
|
1173
1190
|
try {
|
|
1191
|
+
if (attempt > 1) await mux_created_promise
|
|
1192
|
+
|
|
1174
1193
|
var mux_was_done = await promise_done(mux_created_promise)
|
|
1175
1194
|
|
|
1176
1195
|
// callback for testing
|
|
@@ -1245,7 +1264,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
|
|
|
1245
1264
|
if (request_ended) buffers.push(null)
|
|
1246
1265
|
|
|
1247
1266
|
// try parsing what we got so far as headers..
|
|
1248
|
-
var x = parse_headers(headers_buffer)
|
|
1267
|
+
var x = parse_headers(headers_buffer, false, true)
|
|
1249
1268
|
|
|
1250
1269
|
// how did it go?
|
|
1251
1270
|
if (x.result === 'error') {
|
|
@@ -1387,6 +1406,7 @@ function concat_buffers(buffers) {
|
|
|
1387
1406
|
if (typeof module !== 'undefined' && module.exports)
|
|
1388
1407
|
module.exports = {
|
|
1389
1408
|
fetch: braid_fetch,
|
|
1409
|
+
multiplex_fetch,
|
|
1390
1410
|
http: braidify_http,
|
|
1391
1411
|
subscription_parser,
|
|
1392
1412
|
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
|
|