braid-http 1.3.18 → 1.3.20
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 +21 -37
- package/braid-http-server.js +2 -3
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -312,22 +312,6 @@ async function braid_fetch (url, params = {}) {
|
|
|
312
312
|
(params.headers.has('multiplexer') ||
|
|
313
313
|
(params.headers.has('subscribe') &&
|
|
314
314
|
braid_fetch.subscription_counts?.[origin] > 1))) {
|
|
315
|
-
|
|
316
|
-
// invent a new multiplexer and stream id
|
|
317
|
-
// if not provided
|
|
318
|
-
if (!params.headers.has('multiplexer')) {
|
|
319
|
-
// we want to keep the same multiplexer id for each origin
|
|
320
|
-
if (!braid_fetch.multiplexers)
|
|
321
|
-
braid_fetch.multiplexers = {}
|
|
322
|
-
if (!braid_fetch.multiplexers[origin])
|
|
323
|
-
braid_fetch.multiplexers[origin] =
|
|
324
|
-
Math.random().toString(36).slice(2)
|
|
325
|
-
|
|
326
|
-
// the stream id is different each time
|
|
327
|
-
var stream = Math.random().toString(36).slice(2)
|
|
328
|
-
params.headers.set('multiplexer',
|
|
329
|
-
`/${braid_fetch.multiplexers[origin]}/${stream}`)
|
|
330
|
-
}
|
|
331
315
|
res = await multiplex_fetch(url, params)
|
|
332
316
|
} else {
|
|
333
317
|
res = await normal_fetch(url, params)
|
|
@@ -849,27 +833,25 @@ function parse_body (state) {
|
|
|
849
833
|
// This tells the server to send the results to the given multiplexer.
|
|
850
834
|
//
|
|
851
835
|
async function multiplex_fetch(url, params) {
|
|
852
|
-
|
|
853
|
-
|
|
836
|
+
var origin = new URL(url, typeof document !== 'undefined' ? document.baseURI : undefined).origin
|
|
837
|
+
|
|
838
|
+
// the mux_key is the same as the origin, unless it is being overriden
|
|
839
|
+
// (the overriding is done by the tests)
|
|
840
|
+
var mux_key = params.headers.get('multiplexer')?.split('/')[1] ?? origin
|
|
854
841
|
|
|
855
|
-
// create a new multiplexer if it doesn't exist
|
|
842
|
+
// create a new multiplexer if it doesn't exist for this origin
|
|
856
843
|
if (!multiplex_fetch.multiplexers) multiplex_fetch.multiplexers = {}
|
|
857
|
-
if (!multiplex_fetch.multiplexers[
|
|
858
|
-
|
|
844
|
+
if (!multiplex_fetch.multiplexers[mux_key]) multiplex_fetch.multiplexers[mux_key] = (async () => {
|
|
845
|
+
// make up a new multiplexer id (unless it is being overriden)
|
|
846
|
+
var multiplexer = params.headers.get('multiplexer')?.split('/')[1] ?? Math.random().toString(36).slice(2)
|
|
859
847
|
|
|
860
848
|
// attempt to establish a multiplexed connection
|
|
861
849
|
try {
|
|
862
|
-
|
|
863
|
-
var r = await braid_fetch(`${origin}/MULTIPLEX/${multiplexer}`, {retry: true})
|
|
864
|
-
else
|
|
865
|
-
var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
|
|
850
|
+
var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
|
|
866
851
|
} catch (e) {
|
|
867
852
|
// fallback to normal fetch if multiplexed connection fails
|
|
868
853
|
console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
|
|
869
|
-
return (url, params) =>
|
|
870
|
-
params.headers.delete('multiplexer')
|
|
871
|
-
return normal_fetch(url, params)
|
|
872
|
-
}
|
|
854
|
+
return (url, params) => normal_fetch(url, params)
|
|
873
855
|
}
|
|
874
856
|
|
|
875
857
|
// parse the multiplexed stream,
|
|
@@ -882,13 +864,18 @@ async function multiplex_fetch(url, params) {
|
|
|
882
864
|
// the multiplexer stream has died.. let everyone know..
|
|
883
865
|
mux_error = e
|
|
884
866
|
for (var f of streams.values()) f()
|
|
885
|
-
delete multiplex_fetch.multiplexers[
|
|
867
|
+
delete multiplex_fetch.multiplexers[mux_key]
|
|
886
868
|
})
|
|
887
869
|
|
|
888
870
|
// return a "fetch" for this multiplexer
|
|
889
871
|
return async (url, params) => {
|
|
890
|
-
//
|
|
891
|
-
var stream = params.headers.get('multiplexer')
|
|
872
|
+
// make up a new stream id (unless it is being overriden)
|
|
873
|
+
var stream = params.headers.get('multiplexer')?.split('/')[2] ?? Math.random().toString(36).slice(2)
|
|
874
|
+
|
|
875
|
+
// add the multiplexer header without affecting the underlying params
|
|
876
|
+
var mux_headers = new Headers(params.headers)
|
|
877
|
+
mux_headers.set('multiplexer', `/${multiplexer}/${stream}`)
|
|
878
|
+
params = {...params, headers: mux_headers}
|
|
892
879
|
|
|
893
880
|
// setup a way to receive incoming data from the multiplexer
|
|
894
881
|
var buffers = []
|
|
@@ -925,10 +912,7 @@ async function multiplex_fetch(url, params) {
|
|
|
925
912
|
stream_error = e
|
|
926
913
|
bytes_available()
|
|
927
914
|
try {
|
|
928
|
-
|
|
929
|
-
await braid_fetch(`${origin}/MULTIPLEX${params.headers.get('multiplexer')}`, {retry: true})
|
|
930
|
-
else
|
|
931
|
-
await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {method: 'MULTIPLEX', retry: true})
|
|
915
|
+
await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {method: 'MULTIPLEX', retry: true})
|
|
932
916
|
} catch (e) {
|
|
933
917
|
console.error(`Could not cancel multiplexed connection:`, e)
|
|
934
918
|
throw e
|
|
@@ -1014,7 +998,7 @@ async function multiplex_fetch(url, params) {
|
|
|
1014
998
|
})()
|
|
1015
999
|
|
|
1016
1000
|
// call the special fetch function for the multiplexer
|
|
1017
|
-
return await (await multiplex_fetch.multiplexers[
|
|
1001
|
+
return await (await multiplex_fetch.multiplexers[mux_key])(url, params)
|
|
1018
1002
|
}
|
|
1019
1003
|
|
|
1020
1004
|
// waits on reader for chunks like: 123 bytes for stream ABC\r\n..123 bytes..
|
package/braid-http-server.js
CHANGED
|
@@ -243,10 +243,9 @@ function braidify (req, res, next) {
|
|
|
243
243
|
req.subscribe = subscribe
|
|
244
244
|
|
|
245
245
|
// Multiplexer stuff
|
|
246
|
-
if (
|
|
247
|
-
(braidify.use_multiplexing === 'USE GET' && req.url.startsWith('/MULTIPLEX/'))) {
|
|
246
|
+
if (braidify.use_multiplexing && req.method === 'MULTIPLEX') {
|
|
248
247
|
// parse the multiplexer id and stream id from the url
|
|
249
|
-
var [multiplexer, stream] = req.url.slice(1).
|
|
248
|
+
var [multiplexer, stream] = req.url.slice(1).split('/')
|
|
250
249
|
|
|
251
250
|
// if there's just a multiplexer, then we're creating a multiplexer..
|
|
252
251
|
if (!stream) {
|