braid-http 1.3.23 → 1.3.25
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/braid-http-client.js +22 -6
- package/braid-http-server.js +14 -4
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -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: {
|
|
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('
|
|
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')}`, {
|
|
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
|
-
|
|
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",
|
package/braid-http-server.js
CHANGED
|
@@ -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 &&
|
|
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'}
|
|
@@ -291,7 +295,7 @@ function braidify (req, res, next) {
|
|
|
291
295
|
} else m.streams.set(stream, 'abort')
|
|
292
296
|
|
|
293
297
|
// let the requester know we succeeded
|
|
294
|
-
res.writeHead(200, {})
|
|
298
|
+
res.writeHead(200, { 'Multiplex-Version': '0.0.1' })
|
|
295
299
|
return res.end(``)
|
|
296
300
|
}
|
|
297
301
|
}
|
|
@@ -299,7 +303,10 @@ function braidify (req, res, next) {
|
|
|
299
303
|
// a multiplexer header means the user wants to send the
|
|
300
304
|
// results of this request to the provided multiplexer,
|
|
301
305
|
// tagged with the given stream id
|
|
302
|
-
if (braidify.use_multiplexing &&
|
|
306
|
+
if (braidify.use_multiplexing &&
|
|
307
|
+
req.headers.multiplexer &&
|
|
308
|
+
req.headers['multiplex-version'] === '0.0.1') {
|
|
309
|
+
|
|
303
310
|
// parse the multiplexer id and stream id from the url
|
|
304
311
|
var [multiplexer, stream] = req.headers.multiplexer.slice(1).split('/')
|
|
305
312
|
|
|
@@ -319,7 +326,10 @@ function braidify (req, res, next) {
|
|
|
319
326
|
}
|
|
320
327
|
|
|
321
328
|
// let the requester know we've multiplexed their response
|
|
322
|
-
res.writeHead(293, {
|
|
329
|
+
res.writeHead(293, {
|
|
330
|
+
multiplexer: req.headers.multiplexer,
|
|
331
|
+
'Multiplex-Version': '0.0.1'
|
|
332
|
+
})
|
|
323
333
|
res.end('Ok.')
|
|
324
334
|
|
|
325
335
|
// and now set things up so that future use of the
|