braid-http 1.3.45 → 1.3.46
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 +17 -20
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// var peer = Math.random().toString(36).substr(2)
|
|
2
1
|
|
|
3
2
|
// ***************************
|
|
4
3
|
// http
|
|
@@ -316,7 +315,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
316
315
|
(params.headers.has('subscribe') &&
|
|
317
316
|
braid_fetch.subscription_counts?.[origin] >
|
|
318
317
|
(!mux_params ? 1 : (mux_params.after ?? 0))))) {
|
|
319
|
-
res = await multiplex_fetch(url, params, mux_params
|
|
318
|
+
res = await multiplex_fetch(url, params, mux_params)
|
|
320
319
|
} else
|
|
321
320
|
res = await normal_fetch(url, params)
|
|
322
321
|
|
|
@@ -833,7 +832,7 @@ function parse_body (state) {
|
|
|
833
832
|
|
|
834
833
|
// multiplex_fetch provides a fetch-like experience for HTTP requests
|
|
835
834
|
// where the result is actually being sent over a separate multiplexed connection.
|
|
836
|
-
async function multiplex_fetch(url, params,
|
|
835
|
+
async function multiplex_fetch(url, params, mux_params) {
|
|
837
836
|
var multiplex_version = '1.0'
|
|
838
837
|
|
|
839
838
|
var origin = new URL(url, typeof document !== 'undefined' ? document.baseURI : undefined).origin
|
|
@@ -848,15 +847,22 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
848
847
|
async () => {
|
|
849
848
|
// make up a new multiplexer id (unless it is being overriden)
|
|
850
849
|
var multiplexer = params.headers.get('multiplex-through')?.split('/')[3]
|
|
851
|
-
?? Math.
|
|
850
|
+
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
852
851
|
|
|
853
852
|
var requests = new Map()
|
|
854
853
|
var mux_error = null
|
|
855
854
|
|
|
855
|
+
function cleanup(e) {
|
|
856
|
+
// the multiplexer stream has died.. let everyone know..
|
|
857
|
+
mux_error = e
|
|
858
|
+
delete multiplex_fetch.multiplexers[mux_key]
|
|
859
|
+
for (var f of requests.values()) f()
|
|
860
|
+
}
|
|
861
|
+
|
|
856
862
|
var mux_promise = (async () => {
|
|
857
863
|
// attempt to establish a multiplexed connection
|
|
858
864
|
try {
|
|
859
|
-
if (
|
|
865
|
+
if (mux_params?.via === 'POST') throw 'skip multiplex method'
|
|
860
866
|
var r = await braid_fetch(`${origin}/${multiplexer}`, {
|
|
861
867
|
method: 'MULTIPLEX',
|
|
862
868
|
headers: {'Multiplex-Version': multiplex_version},
|
|
@@ -902,28 +908,15 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
902
908
|
})
|
|
903
909
|
} finally { try_deleting.delete(request) }
|
|
904
910
|
}
|
|
905
|
-
},
|
|
906
|
-
// the multiplexer stream has died.. let everyone know..
|
|
907
|
-
mux_error = e
|
|
908
|
-
for (var f of requests.values()) f()
|
|
909
|
-
delete multiplex_fetch.multiplexers[mux_key]
|
|
910
|
-
})
|
|
911
|
+
}, cleanup)
|
|
911
912
|
})()
|
|
912
913
|
|
|
913
914
|
// return a "fetch" for this multiplexer
|
|
914
915
|
return async (url, params) => {
|
|
915
916
|
|
|
916
|
-
// if we already know the multiplexer is not working,
|
|
917
|
-
// then fallback to normal fetch
|
|
918
|
-
// (unless the user is specifically asking for multiplexing)
|
|
919
|
-
if ((await promise_done(mux_promise))
|
|
920
|
-
&& (await mux_promise) === false
|
|
921
|
-
&& !params.headers.get('multiplex-through'))
|
|
922
|
-
return await normal_fetch(url, params)
|
|
923
|
-
|
|
924
917
|
// make up a new request id (unless it is being overriden)
|
|
925
918
|
var request = params.headers.get('multiplex-through')?.split('/')[4]
|
|
926
|
-
?? Math.
|
|
919
|
+
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
927
920
|
|
|
928
921
|
// add the Multiplex-Through header without affecting the underlying params
|
|
929
922
|
var mux_headers = new Headers(params.headers)
|
|
@@ -1218,6 +1211,10 @@ async function promise_done(promise) {
|
|
|
1218
1211
|
return ret !== pending
|
|
1219
1212
|
}
|
|
1220
1213
|
|
|
1214
|
+
function random_base64url(n) {
|
|
1215
|
+
return [...crypto.getRandomValues(new Uint8Array(n))].map(x => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'[x % 64]).join('')
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1221
1218
|
// ****************************
|
|
1222
1219
|
// Exports
|
|
1223
1220
|
// ****************************
|