braid-http 1.3.23 → 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.
@@ -853,13 +853,17 @@ async function multiplex_fetch(url, params) {
853
853
  // attempt to establish a multiplexed connection
854
854
  try {
855
855
  if (params.use_multiplex_header) throw 'skip to trying header'
856
- var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
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'
857
858
  } catch (e) {
858
859
  // some servers don't like custom methods,
859
860
  // so let's try with a custom header
860
861
  try {
861
862
  using_multiplex_header = true
862
- r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {MULTIPLEX: true}, retry: 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')
863
867
  } catch (e) {
864
868
  // fallback to normal fetch if multiplexed connection fails
865
869
  console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
@@ -895,7 +899,8 @@ async function multiplex_fetch(url, params) {
895
899
 
896
900
  // add the multiplexer header without affecting the underlying params
897
901
  var mux_headers = new Headers(params.headers)
898
- mux_headers.set('multiplexer', `/${multiplexer}/${stream}`)
902
+ mux_headers.set('Multiplexer', `/${multiplexer}/${stream}`)
903
+ mux_headers.set('Multiplex-Version', '0.0.1')
899
904
  params = {...params, headers: mux_headers}
900
905
 
901
906
  // setup a way to receive incoming data from the multiplexer
@@ -933,9 +938,18 @@ async function multiplex_fetch(url, params) {
933
938
  stream_error = e
934
939
  bytes_available()
935
940
  try {
936
- 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')
937
950
  } catch (e) {
938
- console.error(`Could not cancel multiplexed connection:`, e)
951
+ e = new Error(`Could not cancel multiplexed connection: ${e}`)
952
+ console.error('' + e)
939
953
  throw e
940
954
  }
941
955
  }
@@ -951,7 +965,9 @@ async function multiplex_fetch(url, params) {
951
965
  throw new Error('multiplexer not yet connected')
952
966
  }
953
967
 
954
- if (res.status !== 293) throw new Error('Could not establish multiplexed stream ' + params.headers.get('multiplexer') + ' got status: ' + res.status)
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'))
955
971
 
956
972
  // we want to present the illusion that the connection is still open,
957
973
  // and therefor closable with "abort",
@@ -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,7 +306,10 @@ 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
 
@@ -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.23",
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"