braid-http 1.3.62 → 1.3.64

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.
@@ -882,11 +882,11 @@ async function promise_done(promise) {
882
882
  }
883
883
 
884
884
  function random_base64url(n) {
885
- var buf = new Uint8Array(n)
886
- var crypt = (typeof crypto !== 'undefined') ? crypto : require('crypto')
887
885
  var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
888
- return [...(crypt.webcrypto ?? crypt).getRandomValues(buf)].
889
- map(x => chars[x % 64]).join('')
886
+ var result = ''
887
+ for (let i = 0; i < n; i++)
888
+ result += chars[Math.floor(Math.random() * 64)]
889
+ return result
890
890
  }
891
891
 
892
892
 
@@ -561,7 +561,12 @@ function braidify (req, res, next) {
561
561
  res.on('finish', x => disconnected('finish'))
562
562
  req.on('abort', x => disconnected('abort'))
563
563
 
564
- // Heartbeats
564
+ // Start sending heartbeats to the client every N seconds if
565
+ // they've been requested. Heartbeats help a client know if a
566
+ // connection is still alive, and can also signal to
567
+ // intermediaries to keep a connection open, because sometimes
568
+ // intermediaries will time-out a connection after a period of no
569
+ // activity.
565
570
  if (req.headers['heartbeats']) {
566
571
  let heartbeats = parseFloat(req.headers['heartbeats'])
567
572
  if (isFinite(heartbeats)) {
@@ -570,8 +575,12 @@ function braidify (req, res, next) {
570
575
  res.on('close', () => closed = true)
571
576
  loop()
572
577
  function loop() {
573
- if (res.writableEnded || closed) return
574
- res.write("\r\n")
578
+ // We only send heartbeats:
579
+ // - After the headers have been sent
580
+ // - Before the stream has closed
581
+ if (res.headersSent && !res.writableEnded && !closed)
582
+ res.write("\r\n")
583
+
575
584
  setTimeout(loop, 1000 * heartbeats)
576
585
  }
577
586
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.62",
3
+ "version": "1.3.64",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"