braid-http 1.3.22 → 1.3.24

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,44 +845,62 @@ 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', headers: {'Multiplex-Version': '0.0.1'}, retry: true})
857
+ if (!r.ok || r.headers.get('Multiplex-Version') !== '0.0.1') throw 'bad'
858
858
  } 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)
859
+ // some servers don't like custom methods,
860
+ // so let's try with a custom header
861
+ try {
862
+ using_multiplex_header = true
863
+ r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {Multiplex: true, 'Multiplex-Version': '0.0.1'}, retry: true})
864
+
865
+ if (!r.ok) throw new Error('status not ok: ' + r.status)
866
+ if (r.headers.get('Multiplex-Version') !== '0.0.1') throw new Error('wrong multiplex version: ' + r.headers.get('Multiplex-Version') + ', expected 0.0.1')
867
+ } catch (e) {
868
+ // fallback to normal fetch if multiplexed connection fails
869
+ console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
870
+ return false
871
+ }
862
872
  }
863
- }
864
873
 
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
- })
874
+ // parse the multiplexed stream,
875
+ // and send messages to the appropriate streams
876
+ parse_multiplex_stream(r.body.getReader(), (stream, bytes) => {
877
+ streams.get(stream)?.(bytes)
878
+ }, e => {
879
+ // the multiplexer stream has died.. let everyone know..
880
+ mux_error = e
881
+ for (var f of streams.values()) f()
882
+ delete multiplex_fetch.multiplexers[mux_key]
883
+ })
884
+ })()
877
885
 
878
886
  // return a "fetch" for this multiplexer
879
887
  return async (url, params) => {
888
+ // maybe wait for multiplexer to be connected..
889
+ if (!params.experimental_do_not_wait_for_multiplexer) {
890
+ if ((await mux_promise) === false) {
891
+ // it failed to connect the multiplexer,
892
+ // so fallback to normal fetch
893
+ return await normal_fetch(url, params)
894
+ }
895
+ }
896
+
880
897
  // make up a new stream id (unless it is being overriden)
881
898
  var stream = params.headers.get('multiplexer')?.split('/')[2] ?? Math.random().toString(36).slice(2)
882
899
 
883
900
  // add the multiplexer header without affecting the underlying params
884
901
  var mux_headers = new Headers(params.headers)
885
- mux_headers.set('multiplexer', `/${multiplexer}/${stream}`)
902
+ mux_headers.set('Multiplexer', `/${multiplexer}/${stream}`)
903
+ mux_headers.set('Multiplex-Version', '0.0.1')
886
904
  params = {...params, headers: mux_headers}
887
905
 
888
906
  // setup a way to receive incoming data from the multiplexer
@@ -920,9 +938,18 @@ async function multiplex_fetch(url, params) {
920
938
  stream_error = e
921
939
  bytes_available()
922
940
  try {
923
- await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {...using_multiplex_header ? {headers: {MULTIPLEX: true}} : {method: 'MULTIPLEX'}, retry: true})
941
+ var r = await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {
942
+ ...!using_multiplex_header && {method: 'MULTIPLEX'},
943
+ headers: {
944
+ ...using_multiplex_header && {Multiplex: true},
945
+ 'Multiplex-Version': '0.0.1'
946
+ }, retry: true})
947
+
948
+ if (!r.ok) throw new Error('status not ok: ' + r.status)
949
+ if (r.headers.get('Multiplex-Version') !== '0.0.1') throw new Error('wrong multiplex version: ' + r.headers.get('Multiplex-Version') + ', expected 0.0.1')
924
950
  } catch (e) {
925
- console.error(`Could not cancel multiplexed connection:`, e)
951
+ e = new Error(`Could not cancel multiplexed connection: ${e}`)
952
+ console.error('' + e)
926
953
  throw e
927
954
  }
928
955
  }
@@ -930,7 +957,17 @@ async function multiplex_fetch(url, params) {
930
957
  // do the underlying fetch
931
958
  try {
932
959
  var res = await normal_fetch(url, params)
933
- if (res.status !== 293) throw new Error('Could not establish multiplexed stream ' + params.headers.get('multiplexer') + ' got status: ' + res.status)
960
+
961
+ if (params.experimental_do_not_wait_for_multiplexer &&
962
+ res.status === 422 &&
963
+ !(await promise_done(mux_promise))) {
964
+ // this error will trigger a retry if the user is using that
965
+ throw new Error('multiplexer not yet connected')
966
+ }
967
+
968
+ if (res.status !== 293) throw new Error('Could not establish multiplexed stream ' + params.headers.get('multiplexer') + ', got status: ' + res.status)
969
+
970
+ if (res.headers.get('Multiplex-Version') !== '0.0.1') throw new Error('Could not establish multiplexed stream ' + params.headers.get('multiplexer') + ', got unknown version: ' + res.headers.get('Multiplex-Version'))
934
971
 
935
972
  // we want to present the illusion that the connection is still open,
936
973
  // and therefor closable with "abort",
@@ -1134,6 +1171,12 @@ function create_abort_error(msg) {
1134
1171
  return e
1135
1172
  }
1136
1173
 
1174
+ async function promise_done(promise) {
1175
+ var pending = {}
1176
+ var ret = await Promise.race([promise, Promise.resolve(pending)])
1177
+ return ret !== pending
1178
+ }
1179
+
1137
1180
  // ****************************
1138
1181
  // Exports
1139
1182
  // ****************************
@@ -243,7 +243,10 @@ function braidify (req, res, next) {
243
243
  req.subscribe = subscribe
244
244
 
245
245
  // Multiplexer stuff
246
- if (braidify.use_multiplexing && (req.method === 'MULTIPLEX' || req.headers.multiplex)) {
246
+ if (braidify.use_multiplexing &&
247
+ (req.method === 'MULTIPLEX' || req.headers.multiplex) &&
248
+ req.headers['multiplex-version'] === '0.0.1') {
249
+
247
250
  // parse the multiplexer id and stream id from the url
248
251
  var [multiplexer, stream] = req.url.slice(1).split('/')
249
252
 
@@ -263,6 +266,7 @@ function braidify (req, res, next) {
263
266
  // keep the connection open,
264
267
  // so people can send multiplexed data to it
265
268
  res.writeHead(200, {
269
+ 'Multiplex-Version': '0.0.1',
266
270
  'Cache-Control': 'no-cache',
267
271
  'X-Accel-Buffering': 'no',
268
272
  ...req.httpVersion !== '2.0' && {'Connection': 'keep-alive'}
@@ -290,8 +294,11 @@ function braidify (req, res, next) {
290
294
  m.streams.delete(stream)
291
295
  } else m.streams.set(stream, 'abort')
292
296
 
297
+ console.log('got here....!!!!')
298
+
299
+
293
300
  // let the requester know we succeeded
294
- res.writeHead(200, {})
301
+ res.writeHead(200, { 'Multiplex-Version': '0.0.1' })
295
302
  return res.end(``)
296
303
  }
297
304
  }
@@ -299,12 +306,15 @@ function braidify (req, res, next) {
299
306
  // a multiplexer header means the user wants to send the
300
307
  // results of this request to the provided multiplexer,
301
308
  // tagged with the given stream id
302
- if (braidify.use_multiplexing && req.headers.multiplexer) {
309
+ if (braidify.use_multiplexing &&
310
+ req.headers.multiplexer &&
311
+ req.headers['multiplex-version'] === '0.0.1') {
312
+
303
313
  // parse the multiplexer id and stream id from the url
304
314
  var [multiplexer, stream] = req.headers.multiplexer.slice(1).split('/')
305
315
 
306
316
  var end_things = (msg) => {
307
- res.statusCode = 400
317
+ res.statusCode = 422 // Unprocessable Entity (but good syntax!)
308
318
  res.end(msg)
309
319
  }
310
320
 
@@ -319,7 +329,10 @@ function braidify (req, res, next) {
319
329
  }
320
330
 
321
331
  // let the requester know we've multiplexed their response
322
- res.writeHead(293, {multiplexer: req.headers.multiplexer})
332
+ res.writeHead(293, {
333
+ multiplexer: req.headers.multiplexer,
334
+ 'Multiplex-Version': '0.0.1'
335
+ })
323
336
  res.end('Ok.')
324
337
 
325
338
  // and now set things up so that future use of the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.3.22",
3
+ "version": "1.3.24",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"