braid-http 1.3.105 → 1.3.106
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 +22 -16
- package/braid-http-server.js +13 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -165,7 +165,7 @@ for await (var update of subscription_iterator) {
|
|
|
165
165
|
|
|
166
166
|
You can braidify your nodejs server with:
|
|
167
167
|
|
|
168
|
-
```
|
|
168
|
+
```javascript
|
|
169
169
|
var braidify = require('braid-http').http_server
|
|
170
170
|
```
|
|
171
171
|
|
|
@@ -344,19 +344,19 @@ fetch('https://localhost:3009/chat',
|
|
|
344
344
|
|
|
345
345
|
Run all tests from the command line:
|
|
346
346
|
|
|
347
|
-
```
|
|
347
|
+
```bash
|
|
348
348
|
npm test
|
|
349
349
|
```
|
|
350
350
|
|
|
351
351
|
Run tests in a browser (auto-opens):
|
|
352
352
|
|
|
353
|
-
```
|
|
353
|
+
```bash
|
|
354
354
|
npm run test:browser
|
|
355
355
|
```
|
|
356
356
|
|
|
357
357
|
You can also filter tests by name:
|
|
358
358
|
|
|
359
|
-
```
|
|
359
|
+
```bash
|
|
360
360
|
node test/test.js --filter="version"
|
|
361
361
|
```
|
|
362
362
|
|
|
@@ -369,7 +369,7 @@ the scenes to overcome web browsers' 6-connection limit (with HTTP/1) and
|
|
|
369
369
|
the recommended ways, you don't need to know it's happening — the abstraction
|
|
370
370
|
is completely transparent.
|
|
371
371
|
|
|
372
|
-
```
|
|
372
|
+
```javascript
|
|
373
373
|
// Recommendation #1: Wrapping the entire request handler
|
|
374
374
|
require('http').createServer(
|
|
375
375
|
braidify((req, res) => {
|
|
@@ -378,18 +378,22 @@ require('http').createServer(
|
|
|
378
378
|
)
|
|
379
379
|
```
|
|
380
380
|
|
|
381
|
-
```
|
|
381
|
+
```javascript
|
|
382
382
|
// Recommendation #2: As middleware
|
|
383
383
|
var app = require('express')()
|
|
384
384
|
app.use(braidify)
|
|
385
385
|
```
|
|
386
386
|
|
|
387
|
-
```
|
|
387
|
+
```javascript
|
|
388
388
|
// Recommendation #3: With braidify(req, res, next)
|
|
389
389
|
// (Equivalent to the middleware form.)
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
390
|
+
app.use(
|
|
391
|
+
(req, res, next) => {
|
|
392
|
+
...
|
|
393
|
+
braidify(req, res, next)
|
|
394
|
+
...
|
|
395
|
+
}
|
|
396
|
+
)
|
|
393
397
|
```
|
|
394
398
|
|
|
395
399
|
|
|
@@ -397,14 +401,16 @@ app.use(braidify)
|
|
|
397
401
|
|
|
398
402
|
If you are using braidify from within a library, or in another context without
|
|
399
403
|
access to the entire request handler, or a `next()` method, then you can use
|
|
400
|
-
the inline form:
|
|
404
|
+
the inline `braidify(req, res)` form:
|
|
401
405
|
|
|
402
|
-
```
|
|
406
|
+
```javascript
|
|
407
|
+
require('http').createServer(
|
|
403
408
|
(req, res) => {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
}
|
|
409
|
+
...
|
|
410
|
+
braidify(req, res); if (req.is_multiplexer) return
|
|
411
|
+
...
|
|
412
|
+
}
|
|
413
|
+
)
|
|
408
414
|
```
|
|
409
415
|
|
|
410
416
|
Just know that there are three abstraction leaks when using this form:
|
package/braid-http-server.js
CHANGED
|
@@ -649,9 +649,19 @@ function braidify (req, res, next) {
|
|
|
649
649
|
res.isSubscription = true
|
|
650
650
|
|
|
651
651
|
// Let's disable the timeouts (if it exists)
|
|
652
|
-
if (req.socket.server)
|
|
652
|
+
if (req.socket.server) {
|
|
653
653
|
req.socket.server.timeout = 0.0
|
|
654
654
|
|
|
655
|
+
// Node 18+ added requestTimeout (default 300s) and
|
|
656
|
+
// headersTimeout (default 60s) which will kill idle
|
|
657
|
+
// long-lived connections — our bread and butter. We disable
|
|
658
|
+
// the requestTimeout, but the headersTimeout is probably
|
|
659
|
+
// fine.
|
|
660
|
+
//
|
|
661
|
+
req.socket.server.requestTimeout = 0
|
|
662
|
+
// req.socket.server.headersTimeout = 0
|
|
663
|
+
}
|
|
664
|
+
|
|
655
665
|
// We have a subscription!
|
|
656
666
|
res.statusCode = 209
|
|
657
667
|
res.statusMessage = 'Multiresponse'
|
|
@@ -753,12 +763,12 @@ async function send_update(res, data, url, peer) {
|
|
|
753
763
|
// Validate the body and patches
|
|
754
764
|
assert(!(patch && patches),
|
|
755
765
|
'sendUpdate: cannot have both `update.patch` and `update.patches` set')
|
|
756
|
-
if (patch)
|
|
757
|
-
patches = [patch]
|
|
758
766
|
assert(!(body && patches),
|
|
759
767
|
'sendUpdate: cannot have both `update.body` and `update.patch(es)')
|
|
760
768
|
assert(!patches || Array.isArray(patches),
|
|
761
769
|
'sendUpdate: `patches` provided is not array')
|
|
770
|
+
if (patch)
|
|
771
|
+
patches = patch
|
|
762
772
|
|
|
763
773
|
// Validate body format
|
|
764
774
|
if (body !== undefined) {
|