braid-http 1.3.19 → 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 +19 -29
- 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,13 +833,17 @@ 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 {
|
|
@@ -863,10 +851,7 @@ async function multiplex_fetch(url, params) {
|
|
|
863
851
|
} catch (e) {
|
|
864
852
|
// fallback to normal fetch if multiplexed connection fails
|
|
865
853
|
console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
|
|
866
|
-
return (url, params) =>
|
|
867
|
-
params.headers.delete('multiplexer')
|
|
868
|
-
return normal_fetch(url, params)
|
|
869
|
-
}
|
|
854
|
+
return (url, params) => normal_fetch(url, params)
|
|
870
855
|
}
|
|
871
856
|
|
|
872
857
|
// parse the multiplexed stream,
|
|
@@ -879,13 +864,18 @@ async function multiplex_fetch(url, params) {
|
|
|
879
864
|
// the multiplexer stream has died.. let everyone know..
|
|
880
865
|
mux_error = e
|
|
881
866
|
for (var f of streams.values()) f()
|
|
882
|
-
delete multiplex_fetch.multiplexers[
|
|
867
|
+
delete multiplex_fetch.multiplexers[mux_key]
|
|
883
868
|
})
|
|
884
869
|
|
|
885
870
|
// return a "fetch" for this multiplexer
|
|
886
871
|
return async (url, params) => {
|
|
887
|
-
//
|
|
888
|
-
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}
|
|
889
879
|
|
|
890
880
|
// setup a way to receive incoming data from the multiplexer
|
|
891
881
|
var buffers = []
|
|
@@ -1008,7 +998,7 @@ async function multiplex_fetch(url, params) {
|
|
|
1008
998
|
})()
|
|
1009
999
|
|
|
1010
1000
|
// call the special fetch function for the multiplexer
|
|
1011
|
-
return await (await multiplex_fetch.multiplexers[
|
|
1001
|
+
return await (await multiplex_fetch.multiplexers[mux_key])(url, params)
|
|
1012
1002
|
}
|
|
1013
1003
|
|
|
1014
1004
|
// waits on reader for chunks like: 123 bytes for stream ABC\r\n..123 bytes..
|