braid-http 1.3.60 → 1.3.62

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
@@ -37,7 +37,7 @@ npm install braid-http
37
37
 
38
38
  ```javascript
39
39
  // Import with require()
40
- require('braid-http').fetch // A polyfill for require('node-fetch')
40
+ require('braid-http').fetch // A polyfill for fetch
41
41
  require('braid-http').http_client // A polyfill for require('http') clients
42
42
  require('braid-http').http_server // A polyfill for require('http') servers
43
43
 
@@ -285,10 +285,3 @@ fetch('https://localhost:3009/chat',
285
285
  x => console.log('Got ', x)
286
286
  )
287
287
  ```
288
-
289
- Note: the current version of `node-fetch` doesn't properly throw errors when a
290
- response connection dies, and thus you cannot attach a `.catch()` handler to
291
- automatically reconnect. (See
292
- [issue #980](https://github.com/node-fetch/node-fetch/issues/980) and
293
- [#753](https://github.com/node-fetch/node-fetch/issues/753).) We recommend
294
- using the `http` client on nodejs instead.
@@ -122,7 +122,7 @@ var normal_fetch,
122
122
 
123
123
  if (is_nodejs) {
124
124
  // Nodejs
125
- normal_fetch = fetch
125
+ normal_fetch = typeof fetch !== 'undefined' && fetch
126
126
  } else {
127
127
  // Web Browser
128
128
  normal_fetch = window.fetch
@@ -860,7 +860,7 @@ function get_binary_length(x) {
860
860
  function deep_copy(x) {
861
861
  if (x === null || typeof x !== 'object') return x
862
862
  if (Array.isArray(x)) return x.map(x => deep_copy(x))
863
- if (Object.prototype.toString.call(x) === '[object Object]')
863
+ if (x.constructor === Object)
864
864
  return Object.fromEntries(Object.entries(x).map(([k, x]) => [k, deep_copy(x)]))
865
865
  return x
866
866
  }
@@ -882,7 +882,11 @@ async function promise_done(promise) {
882
882
  }
883
883
 
884
884
  function random_base64url(n) {
885
- return [...crypto.getRandomValues(new Uint8Array(n))].map(x => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'[x % 64]).join('')
885
+ var buf = new Uint8Array(n)
886
+ var crypt = (typeof crypto !== 'undefined') ? crypto : require('crypto')
887
+ var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
888
+ return [...(crypt.webcrypto ?? crypt).getRandomValues(buf)].
889
+ map(x => chars[x % 64]).join('')
886
890
  }
887
891
 
888
892
 
@@ -949,7 +953,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
949
953
  if (!try_deleting.has(request)) {
950
954
  try_deleting.add(request)
951
955
  try {
952
- var mux_was_done = await promise_done(mux_promise)
956
+ var mux_was_done = await promise_done(mux_created_promise)
953
957
 
954
958
  var r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
955
959
  method: 'DELETE',
@@ -979,7 +983,10 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
979
983
  }
980
984
  }
981
985
 
982
- var mux_promise = (async () => {
986
+ // This promise resolves when the create_multiplexer request responds.
987
+ // - its value is undefined if successfully created
988
+ // - its value is false if creation failed
989
+ var mux_created_promise = (async () => {
983
990
  // attempt to establish a multiplexed connection
984
991
  try {
985
992
  if (mux_params?.via === 'POST') throw 'skip multiplex method'
@@ -1039,7 +1046,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
1039
1046
 
1040
1047
  // if we already know the multiplexer is not working,
1041
1048
  // then fallback to normal fetch
1042
- if ((await promise_done(mux_promise)) && (await mux_promise) === false) {
1049
+ if ((await promise_done(mux_created_promise)) && (await mux_created_promise) === false) {
1043
1050
  // if the user is specifically asking for multiplexing,
1044
1051
  // throw an error instead
1045
1052
  if (params.headers.get('multiplex-through')) throw new Error('multiplexer failed')
@@ -1105,7 +1112,7 @@ async function create_multiplexer(origin, mux_key, params, mux_params, attempt)
1105
1112
 
1106
1113
  // do the underlying fetch
1107
1114
  try {
1108
- var mux_was_done = await promise_done(mux_promise)
1115
+ var mux_was_done = await promise_done(mux_created_promise)
1109
1116
 
1110
1117
  // callback for testing
1111
1118
  mux_params?.onFetch?.(url, params)
@@ -348,12 +348,14 @@ function braidify (req, res, next) {
348
348
  // find the multiplexer object (contains a response object)
349
349
  var m = braidify.multiplexers?.get(multiplexer)
350
350
  if (!m) {
351
+ req.is_multiplexer = res.is_multiplexer = true
351
352
  res.writeHead(424, 'Multiplexer no exist', {'Bad-Multiplexer': multiplexer})
352
353
  return res.end(`multiplexer ${multiplexer} does not exist`)
353
354
  }
354
355
 
355
356
  // if this request-id already exists, respond with an error
356
357
  if (m.requests.has(request)) {
358
+ req.is_multiplexer = res.is_multiplexer = true
357
359
  res.writeHead(409, 'Conflict', {'Content-Type': 'application/json'})
358
360
  return res.end(JSON.stringify({
359
361
  error: 'Request already multiplexed',
@@ -621,7 +623,7 @@ async function send_update(res, data, url, peer) {
621
623
  // Validate body format
622
624
  if (body !== undefined) {
623
625
  assert(typeof body === 'string' || get_binary_length(body) != null)
624
- if (body instanceof Blob) body = await body.arrayBuffer()
626
+ if (typeof Blob !== 'undefined' && body instanceof Blob) body = await body.arrayBuffer()
625
627
  }
626
628
 
627
629
  // Validate patches format
@@ -638,7 +640,8 @@ async function send_update(res, data, url, peer) {
638
640
  assert('content' in p)
639
641
  assert(typeof p.content === 'string'
640
642
  || get_binary_length(p.content) != null)
641
- if (p.content instanceof Blob) p.content = await p.content.arrayBuffer()
643
+ if (typeof Blob !== 'undefined' && p.content instanceof Blob)
644
+ p.content = await p.content.arrayBuffer()
642
645
  }
643
646
  }
644
647
 
@@ -708,7 +711,7 @@ async function send_update(res, data, url, peer) {
708
711
  function get_binary_length(x) {
709
712
  return x instanceof ArrayBuffer ? x.byteLength :
710
713
  x instanceof Uint8Array ? x.length :
711
- x instanceof Blob ? x.size :
714
+ typeof Blob !== 'undefined' && x instanceof Blob ? x.size :
712
715
  x instanceof Buffer ? x.length : undefined
713
716
  }
714
717
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.60",
3
+ "version": "1.3.62",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"
@@ -19,15 +19,10 @@
19
19
  "require": "./index.js",
20
20
  "import": "./index.mjs"
21
21
  },
22
- "browser": {
23
- "node-web-streams": false,
24
- "node-fetch": false,
25
- "abort-controller": false
26
- },
27
22
  "dependencies": {
28
- "abort-controller": "^3.0.0",
29
- "node-fetch": "^2.6.1",
30
- "parse-headers": "^2.0.3",
31
- "web-streams-node": "^0.4.0"
23
+ "parse-headers": "^2.0.3"
24
+ },
25
+ "devDependencies": {
26
+ "express": "^4.21.2"
32
27
  }
33
28
  }