braid-http 1.3.22 → 1.3.23

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.
@@ -845,38 +845,51 @@ async function multiplex_fetch(url, params) {
845
845
  // make up a new multiplexer id (unless it is being overriden)
846
846
  var multiplexer = params.headers.get('multiplexer')?.split('/')[1] ?? Math.random().toString(36).slice(2)
847
847
 
848
- // attempt to establish a multiplexed connection
849
- try {
850
- if (params.use_multiplex_header) throw 'skip to trying header'
851
- var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
852
- } catch (e) {
853
- // some servers don't like custom methods,
854
- // so let's try with a custom header
848
+ var streams = new Map()
849
+ var mux_error = null
850
+ var using_multiplex_header = false
851
+
852
+ var mux_promise = (async () => {
853
+ // attempt to establish a multiplexed connection
855
854
  try {
856
- var using_multiplex_header = true
857
- r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {MULTIPLEX: true}, retry: true})
855
+ if (params.use_multiplex_header) throw 'skip to trying header'
856
+ var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
858
857
  } catch (e) {
859
- // fallback to normal fetch if multiplexed connection fails
860
- console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
861
- return (url, params) => normal_fetch(url, params)
858
+ // some servers don't like custom methods,
859
+ // so let's try with a custom header
860
+ try {
861
+ using_multiplex_header = true
862
+ r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {MULTIPLEX: true}, retry: true})
863
+ } catch (e) {
864
+ // fallback to normal fetch if multiplexed connection fails
865
+ console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
866
+ return false
867
+ }
862
868
  }
863
- }
864
869
 
865
- // parse the multiplexed stream,
866
- // and send messages to the appropriate streams
867
- var streams = new Map()
868
- var mux_error = null
869
- parse_multiplex_stream(r.body.getReader(), (stream, bytes) => {
870
- streams.get(stream)?.(bytes)
871
- }, e => {
872
- // the multiplexer stream has died.. let everyone know..
873
- mux_error = e
874
- for (var f of streams.values()) f()
875
- delete multiplex_fetch.multiplexers[mux_key]
876
- })
870
+ // parse the multiplexed stream,
871
+ // and send messages to the appropriate streams
872
+ parse_multiplex_stream(r.body.getReader(), (stream, bytes) => {
873
+ streams.get(stream)?.(bytes)
874
+ }, e => {
875
+ // the multiplexer stream has died.. let everyone know..
876
+ mux_error = e
877
+ for (var f of streams.values()) f()
878
+ delete multiplex_fetch.multiplexers[mux_key]
879
+ })
880
+ })()
877
881
 
878
882
  // return a "fetch" for this multiplexer
879
883
  return async (url, params) => {
884
+ // maybe wait for multiplexer to be connected..
885
+ if (!params.experimental_do_not_wait_for_multiplexer) {
886
+ if ((await mux_promise) === false) {
887
+ // it failed to connect the multiplexer,
888
+ // so fallback to normal fetch
889
+ return await normal_fetch(url, params)
890
+ }
891
+ }
892
+
880
893
  // make up a new stream id (unless it is being overriden)
881
894
  var stream = params.headers.get('multiplexer')?.split('/')[2] ?? Math.random().toString(36).slice(2)
882
895
 
@@ -930,6 +943,14 @@ async function multiplex_fetch(url, params) {
930
943
  // do the underlying fetch
931
944
  try {
932
945
  var res = await normal_fetch(url, params)
946
+
947
+ if (params.experimental_do_not_wait_for_multiplexer &&
948
+ res.status === 422 &&
949
+ !(await promise_done(mux_promise))) {
950
+ // this error will trigger a retry if the user is using that
951
+ throw new Error('multiplexer not yet connected')
952
+ }
953
+
933
954
  if (res.status !== 293) throw new Error('Could not establish multiplexed stream ' + params.headers.get('multiplexer') + ' got status: ' + res.status)
934
955
 
935
956
  // we want to present the illusion that the connection is still open,
@@ -1134,6 +1155,12 @@ function create_abort_error(msg) {
1134
1155
  return e
1135
1156
  }
1136
1157
 
1158
+ async function promise_done(promise) {
1159
+ var pending = {}
1160
+ var ret = await Promise.race([promise, Promise.resolve(pending)])
1161
+ return ret !== pending
1162
+ }
1163
+
1137
1164
  // ****************************
1138
1165
  // Exports
1139
1166
  // ****************************
@@ -304,7 +304,7 @@ function braidify (req, res, next) {
304
304
  var [multiplexer, stream] = req.headers.multiplexer.slice(1).split('/')
305
305
 
306
306
  var end_things = (msg) => {
307
- res.statusCode = 400
307
+ res.statusCode = 422 // Unprocessable Entity (but good syntax!)
308
308
  res.end(msg)
309
309
  }
310
310
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.22",
3
+ "version": "1.3.23",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"