braid-http 1.3.22 → 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.
- package/braid-http-client.js +72 -29
- package/braid-http-server.js +18 -5
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -845,44 +845,62 @@ async function multiplex_fetch(url, params) {
|
|
|
845
845
|
// make up a new multiplexer id (unless it is being overriden)
|
|
846
846
|
var multiplexer = params.headers.get('multiplexer')?.split('/')[1] ?? Math.random().toString(36).slice(2)
|
|
847
847
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
//
|
|
854
|
-
// so let's try with a custom header
|
|
848
|
+
var streams = new Map()
|
|
849
|
+
var mux_error = null
|
|
850
|
+
var using_multiplex_header = false
|
|
851
|
+
|
|
852
|
+
var mux_promise = (async () => {
|
|
853
|
+
// attempt to establish a multiplexed connection
|
|
855
854
|
try {
|
|
856
|
-
|
|
857
|
-
r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {
|
|
855
|
+
if (params.use_multiplex_header) throw 'skip to trying header'
|
|
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'
|
|
858
858
|
} catch (e) {
|
|
859
|
-
//
|
|
860
|
-
|
|
861
|
-
|
|
859
|
+
// some servers don't like custom methods,
|
|
860
|
+
// so let's try with a custom header
|
|
861
|
+
try {
|
|
862
|
+
using_multiplex_header = 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')
|
|
867
|
+
} catch (e) {
|
|
868
|
+
// fallback to normal fetch if multiplexed connection fails
|
|
869
|
+
console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
|
|
870
|
+
return false
|
|
871
|
+
}
|
|
862
872
|
}
|
|
863
|
-
}
|
|
864
873
|
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
})
|
|
874
|
+
// parse the multiplexed stream,
|
|
875
|
+
// and send messages to the appropriate streams
|
|
876
|
+
parse_multiplex_stream(r.body.getReader(), (stream, bytes) => {
|
|
877
|
+
streams.get(stream)?.(bytes)
|
|
878
|
+
}, e => {
|
|
879
|
+
// the multiplexer stream has died.. let everyone know..
|
|
880
|
+
mux_error = e
|
|
881
|
+
for (var f of streams.values()) f()
|
|
882
|
+
delete multiplex_fetch.multiplexers[mux_key]
|
|
883
|
+
})
|
|
884
|
+
})()
|
|
877
885
|
|
|
878
886
|
// return a "fetch" for this multiplexer
|
|
879
887
|
return async (url, params) => {
|
|
888
|
+
// maybe wait for multiplexer to be connected..
|
|
889
|
+
if (!params.experimental_do_not_wait_for_multiplexer) {
|
|
890
|
+
if ((await mux_promise) === false) {
|
|
891
|
+
// it failed to connect the multiplexer,
|
|
892
|
+
// so fallback to normal fetch
|
|
893
|
+
return await normal_fetch(url, params)
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
880
897
|
// make up a new stream id (unless it is being overriden)
|
|
881
898
|
var stream = params.headers.get('multiplexer')?.split('/')[2] ?? Math.random().toString(36).slice(2)
|
|
882
899
|
|
|
883
900
|
// add the multiplexer header without affecting the underlying params
|
|
884
901
|
var mux_headers = new Headers(params.headers)
|
|
885
|
-
mux_headers.set('
|
|
902
|
+
mux_headers.set('Multiplexer', `/${multiplexer}/${stream}`)
|
|
903
|
+
mux_headers.set('Multiplex-Version', '0.0.1')
|
|
886
904
|
params = {...params, headers: mux_headers}
|
|
887
905
|
|
|
888
906
|
// setup a way to receive incoming data from the multiplexer
|
|
@@ -920,9 +938,18 @@ async function multiplex_fetch(url, params) {
|
|
|
920
938
|
stream_error = e
|
|
921
939
|
bytes_available()
|
|
922
940
|
try {
|
|
923
|
-
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')
|
|
924
950
|
} catch (e) {
|
|
925
|
-
|
|
951
|
+
e = new Error(`Could not cancel multiplexed connection: ${e}`)
|
|
952
|
+
console.error('' + e)
|
|
926
953
|
throw e
|
|
927
954
|
}
|
|
928
955
|
}
|
|
@@ -930,7 +957,17 @@ async function multiplex_fetch(url, params) {
|
|
|
930
957
|
// do the underlying fetch
|
|
931
958
|
try {
|
|
932
959
|
var res = await normal_fetch(url, params)
|
|
933
|
-
|
|
960
|
+
|
|
961
|
+
if (params.experimental_do_not_wait_for_multiplexer &&
|
|
962
|
+
res.status === 422 &&
|
|
963
|
+
!(await promise_done(mux_promise))) {
|
|
964
|
+
// this error will trigger a retry if the user is using that
|
|
965
|
+
throw new Error('multiplexer not yet connected')
|
|
966
|
+
}
|
|
967
|
+
|
|
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'))
|
|
934
971
|
|
|
935
972
|
// we want to present the illusion that the connection is still open,
|
|
936
973
|
// and therefor closable with "abort",
|
|
@@ -1134,6 +1171,12 @@ function create_abort_error(msg) {
|
|
|
1134
1171
|
return e
|
|
1135
1172
|
}
|
|
1136
1173
|
|
|
1174
|
+
async function promise_done(promise) {
|
|
1175
|
+
var pending = {}
|
|
1176
|
+
var ret = await Promise.race([promise, Promise.resolve(pending)])
|
|
1177
|
+
return ret !== pending
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1137
1180
|
// ****************************
|
|
1138
1181
|
// Exports
|
|
1139
1182
|
// ****************************
|
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'}
|
|
@@ -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,12 +306,15 @@ 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 &&
|
|
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
|
|
|
306
316
|
var end_things = (msg) => {
|
|
307
|
-
res.statusCode =
|
|
317
|
+
res.statusCode = 422 // Unprocessable Entity (but good syntax!)
|
|
308
318
|
res.end(msg)
|
|
309
319
|
}
|
|
310
320
|
|
|
@@ -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, {
|
|
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
|