braid-http 1.3.46 → 1.3.48

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.
@@ -851,14 +851,38 @@ async function multiplex_fetch(url, params, mux_params) {
851
851
 
852
852
  var requests = new Map()
853
853
  var mux_error = null
854
+ var try_deleting = new Set()
854
855
 
855
- function cleanup(e) {
856
+ function cleanup(e, stay_dead) {
856
857
  // the multiplexer stream has died.. let everyone know..
857
858
  mux_error = e
858
- delete multiplex_fetch.multiplexers[mux_key]
859
+ if (!stay_dead) delete multiplex_fetch.multiplexers[mux_key]
859
860
  for (var f of requests.values()) f()
860
861
  }
861
862
 
863
+ async function try_deleting_request(request) {
864
+ if (!try_deleting.has(request)) {
865
+ try_deleting.add(request)
866
+ try {
867
+ var r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
868
+ method: 'DELETE',
869
+ headers: { 'Multiplex-Version': multiplex_version },
870
+ retry: true
871
+ })
872
+
873
+ if (!r.ok) throw new Error('status not ok: ' + r.status)
874
+ if (r.headers.get('Multiplex-Version') !== multiplex_version)
875
+ throw new Error('wrong multiplex version: '
876
+ + r.headers.get('Multiplex-Version')
877
+ + ', expected ' + multiplex_version)
878
+ } catch (e) {
879
+ e = new Error(`Could not cancel multiplexed request: ${e}`)
880
+ console.error('' + e)
881
+ throw e
882
+ } finally { try_deleting.delete(request) }
883
+ }
884
+ }
885
+
862
886
  var mux_promise = (async () => {
863
887
  // attempt to establish a multiplexed connection
864
888
  try {
@@ -868,17 +892,24 @@ async function multiplex_fetch(url, params, mux_params) {
868
892
  headers: {'Multiplex-Version': multiplex_version},
869
893
  retry: true
870
894
  })
895
+ if (r.status === 409) {
896
+ var e = await r.json()
897
+ if (e.error === 'Multiplexer already exists') return cleanup(new Error(e.error))
898
+ }
871
899
  if (!r.ok || r.headers.get('Multiplex-Version') !== multiplex_version)
872
900
  throw 'bad'
873
901
  } catch (e) {
874
902
  // some servers don't like custom methods,
875
- // so let's try with a custom header
903
+ // so let's try with a well-known url
876
904
  try {
877
905
  r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}`,
878
906
  {method: 'POST',
879
907
  headers: {'Multiplex-Version': multiplex_version},
880
908
  retry: true})
881
-
909
+ if (r.status === 409) {
910
+ var e = await r.json()
911
+ if (e.error === 'Multiplexer already exists') return cleanup(new Error(e.error))
912
+ }
882
913
  if (!r.ok) throw new Error('status not ok: ' + r.status)
883
914
  if (r.headers.get('Multiplex-Version') !== multiplex_version)
884
915
  throw new Error('wrong multiplex version: '
@@ -888,7 +919,7 @@ async function multiplex_fetch(url, params, mux_params) {
888
919
  // fallback to normal fetch if multiplexed connection fails
889
920
  console.error(`Could not establish multiplexer.\n`
890
921
  + `Got error: ${e}.\nFalling back to normal connection.`)
891
- return false
922
+ return cleanup(e, true)
892
923
  }
893
924
  }
894
925
 
@@ -896,19 +927,9 @@ async function multiplex_fetch(url, params, mux_params) {
896
927
  // and send messages to the appropriate requests
897
928
  var try_deleting = new Set()
898
929
  parse_multiplex_stream(r.body.getReader(), async (request, bytes) => {
899
- if (requests.has(request)) {
900
- requests.get(request)(bytes)
901
- } else if (!try_deleting.has(request)) {
902
- try_deleting.add(request)
903
- try {
904
- await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
905
- method: 'DELETE',
906
- headers: { 'Multiplex-Version': multiplex_version },
907
- retry: true
908
- })
909
- } finally { try_deleting.delete(request) }
910
- }
911
- }, cleanup)
930
+ if (requests.has(request)) requests.get(request)(bytes)
931
+ else try_deleting_request(request)
932
+ }, e => cleanup(e))
912
933
  })()
913
934
 
914
935
  // return a "fetch" for this multiplexer
@@ -956,22 +977,7 @@ async function multiplex_fetch(url, params, mux_params) {
956
977
  requests.delete(request)
957
978
  request_error = e
958
979
  bytes_available()
959
- try {
960
- var r = await braid_fetch(`${origin}${params.headers.get('multiplex-through')}`, {
961
- method: 'DELETE',
962
- headers: { 'Multiplex-Version': multiplex_version }, retry: true
963
- })
964
-
965
- if (!r.ok) throw new Error('status not ok: ' + r.status)
966
- if (r.headers.get('Multiplex-Version') !== multiplex_version)
967
- throw new Error('wrong multiplex version: '
968
- + r.headers.get('Multiplex-Version')
969
- + ', expected ' + multiplex_version)
970
- } catch (e) {
971
- e = new Error(`Could not cancel multiplexed request: ${e}`)
972
- console.error('' + e)
973
- throw e
974
- }
980
+ await try_deleting_request(request)
975
981
  }
976
982
 
977
983
  // do the underlying fetch
@@ -980,6 +986,11 @@ async function multiplex_fetch(url, params, mux_params) {
980
986
 
981
987
  var res = await normal_fetch(url, params)
982
988
 
989
+ if (res.status === 409) {
990
+ var e = await r.json()
991
+ if (e.error === 'Request already multiplexed') throw new Error(e.error)
992
+ }
993
+
983
994
  if (res.status === 424 && !mux_was_done) {
984
995
  // this error will trigger a retry if the user is using that option
985
996
  throw new Error('multiplexer not yet connected')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.46",
3
+ "version": "1.3.48",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"