braid-http 1.3.42 → 1.3.44
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 +13 -13
- package/braid-http-server.js +19 -15
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -309,7 +309,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
309
309
|
// try multiplexing if the multiplex flag is set, and conditions are met
|
|
310
310
|
var mux_params = params.multiplex ?? braid_fetch.enable_multiplex
|
|
311
311
|
if (mux_params !== false &&
|
|
312
|
-
(params.headers.has('multiplex-
|
|
312
|
+
(params.headers.has('multiplex-through') ||
|
|
313
313
|
(params.headers.has('subscribe') &&
|
|
314
314
|
braid_fetch.subscription_counts?.[origin] >
|
|
315
315
|
(!mux_params ? 1 : (mux_params.after ?? 0))))) {
|
|
@@ -834,13 +834,13 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
834
834
|
|
|
835
835
|
// the mux_key is the same as the origin, unless it is being overriden
|
|
836
836
|
// (the overriding is done by the tests)
|
|
837
|
-
var mux_key = params.headers.get('multiplex-
|
|
837
|
+
var mux_key = params.headers.get('multiplex-through')?.split('/')[3] ?? origin
|
|
838
838
|
|
|
839
839
|
// create a new multiplexer if it doesn't exist for this origin
|
|
840
840
|
if (!multiplex_fetch.multiplexers) multiplex_fetch.multiplexers = {}
|
|
841
841
|
if (!multiplex_fetch.multiplexers[mux_key]) multiplex_fetch.multiplexers[mux_key] = (async () => {
|
|
842
842
|
// make up a new multiplexer id (unless it is being overriden)
|
|
843
|
-
var multiplexer = params.headers.get('multiplex-
|
|
843
|
+
var multiplexer = params.headers.get('multiplex-through')?.split('/')[3] ?? Math.random().toString(36).slice(2)
|
|
844
844
|
|
|
845
845
|
var requests = new Map()
|
|
846
846
|
var mux_error = null
|
|
@@ -855,7 +855,7 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
855
855
|
// some servers don't like custom methods,
|
|
856
856
|
// so let's try with a custom header
|
|
857
857
|
try {
|
|
858
|
-
r = await braid_fetch(`${origin}/.well-known/
|
|
858
|
+
r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}`, {method: 'POST', headers: {'Multiplex-Version': multiplex_version}, retry: true})
|
|
859
859
|
|
|
860
860
|
if (!r.ok) throw new Error('status not ok: ' + r.status)
|
|
861
861
|
if (r.headers.get('Multiplex-Version') !== multiplex_version) throw new Error('wrong multiplex version: ' + r.headers.get('Multiplex-Version') + ', expected ' + multiplex_version)
|
|
@@ -875,7 +875,7 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
875
875
|
} else if (!try_deleting.has(request)) {
|
|
876
876
|
try_deleting.add(request)
|
|
877
877
|
try {
|
|
878
|
-
await braid_fetch(`${origin}/.well-known/
|
|
878
|
+
await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
|
|
879
879
|
method: 'DELETE',
|
|
880
880
|
headers: { 'Multiplex-Version': multiplex_version }, retry: true
|
|
881
881
|
})
|
|
@@ -895,15 +895,15 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
895
895
|
// if we already know the multiplexer is not working,
|
|
896
896
|
// then fallback to normal fetch
|
|
897
897
|
// (unless the user is specifically asking for multiplexing)
|
|
898
|
-
if ((await promise_done(mux_promise)) && (await mux_promise) === false && !params.headers.get('multiplex-
|
|
898
|
+
if ((await promise_done(mux_promise)) && (await mux_promise) === false && !params.headers.get('multiplex-through'))
|
|
899
899
|
return await normal_fetch(url, params)
|
|
900
900
|
|
|
901
901
|
// make up a new request id (unless it is being overriden)
|
|
902
|
-
var request = params.headers.get('multiplex-
|
|
902
|
+
var request = params.headers.get('multiplex-through')?.split('/')[4] ?? Math.random().toString(36).slice(2)
|
|
903
903
|
|
|
904
|
-
// add the Multiplex-
|
|
904
|
+
// add the Multiplex-Through header without affecting the underlying params
|
|
905
905
|
var mux_headers = new Headers(params.headers)
|
|
906
|
-
mux_headers.set('Multiplex-
|
|
906
|
+
mux_headers.set('Multiplex-Through', `/.well-known/multiplexer/${multiplexer}/${request}`)
|
|
907
907
|
mux_headers.set('Multiplex-Version', multiplex_version)
|
|
908
908
|
params = {...params, headers: mux_headers}
|
|
909
909
|
|
|
@@ -940,7 +940,7 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
940
940
|
request_error = e
|
|
941
941
|
bytes_available()
|
|
942
942
|
try {
|
|
943
|
-
var r = await braid_fetch(`${origin}${params.headers.get('multiplex-
|
|
943
|
+
var r = await braid_fetch(`${origin}${params.headers.get('multiplex-through')}`, {
|
|
944
944
|
method: 'DELETE',
|
|
945
945
|
headers: { 'Multiplex-Version': multiplex_version }, retry: true
|
|
946
946
|
})
|
|
@@ -970,9 +970,9 @@ async function multiplex_fetch(url, params, skip_multiplex_method) {
|
|
|
970
970
|
// fall back to as if it was a normal fetch
|
|
971
971
|
if (res.ok && res.status !== 293) return res
|
|
972
972
|
|
|
973
|
-
if (res.status !== 293) throw new Error('Could not establish multiplexed request ' + params.headers.get('multiplex-
|
|
973
|
+
if (res.status !== 293) throw new Error('Could not establish multiplexed request ' + params.headers.get('multiplex-through') + ', got status: ' + res.status)
|
|
974
974
|
|
|
975
|
-
if (res.headers.get('Multiplex-Version') !== multiplex_version) throw new Error('Could not establish multiplexed request ' + params.headers.get('multiplex-
|
|
975
|
+
if (res.headers.get('Multiplex-Version') !== multiplex_version) throw new Error('Could not establish multiplexed request ' + params.headers.get('multiplex-through') + ', got unknown version: ' + res.headers.get('Multiplex-Version'))
|
|
976
976
|
|
|
977
977
|
// we want to present the illusion that the connection is still open,
|
|
978
978
|
// and therefor closable with "abort",
|
|
@@ -1085,7 +1085,7 @@ async function parse_multiplex_stream(reader, cb, on_error) {
|
|
|
1085
1085
|
}
|
|
1086
1086
|
if (headerComplete) {
|
|
1087
1087
|
var headerStr = new TextDecoder().decode(buffers[0].slice(0, header_length))
|
|
1088
|
-
var m = headerStr.match(/^[\r\n]*((\d+) bytes for|close|start)
|
|
1088
|
+
var m = headerStr.match(/^[\r\n]*((\d+) bytes for|close|start) response ([A-Za-z0-9_-]+)\r\n$/)
|
|
1089
1089
|
|
|
1090
1090
|
if (!m) throw new Error('invalid multiplex header')
|
|
1091
1091
|
request_id = m[3]
|
package/braid-http-server.js
CHANGED
|
@@ -245,7 +245,7 @@ function braidify (req, res, next) {
|
|
|
245
245
|
// Multiplexer stuff
|
|
246
246
|
var multiplex_version = '1.0'
|
|
247
247
|
if ((braidify.enable_multiplex ?? true) &&
|
|
248
|
-
(req.method === 'MULTIPLEX' || req.url.startsWith('/.well-known/
|
|
248
|
+
(req.method === 'MULTIPLEX' || req.url.startsWith('/.well-known/multiplexer/')) &&
|
|
249
249
|
req.headers['multiplex-version'] === multiplex_version) {
|
|
250
250
|
|
|
251
251
|
// let the caller know we're handling things
|
|
@@ -323,15 +323,15 @@ function braidify (req, res, next) {
|
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
// a Multiplex-
|
|
326
|
+
// a Multiplex-Through header means the user wants to send the
|
|
327
327
|
// results of this request to the provided multiplexer,
|
|
328
328
|
// tagged with the given request id
|
|
329
329
|
if ((braidify.enable_multiplex ?? true) &&
|
|
330
|
-
req.headers['multiplex-
|
|
330
|
+
req.headers['multiplex-through'] &&
|
|
331
331
|
req.headers['multiplex-version'] === multiplex_version) {
|
|
332
332
|
|
|
333
333
|
// parse the multiplexer id and request id from the header
|
|
334
|
-
var [multiplexer, request] = req.headers['multiplex-
|
|
334
|
+
var [multiplexer, request] = req.headers['multiplex-through'].split('/').slice(3)
|
|
335
335
|
|
|
336
336
|
// find the multiplexer object (contains a response object)
|
|
337
337
|
var m = braidify.multiplexers?.get(multiplexer)
|
|
@@ -340,7 +340,17 @@ function braidify (req, res, next) {
|
|
|
340
340
|
return res.end(`multiplexer ${multiplexer} does not exist`)
|
|
341
341
|
}
|
|
342
342
|
|
|
343
|
-
|
|
343
|
+
// if this request-id already exists, respond with an error
|
|
344
|
+
if (m.requests.has(request)) {
|
|
345
|
+
res.writeHead(409, 'Conflict', {'Content-Type': 'application/json'})
|
|
346
|
+
return res.end(JSON.stringify({
|
|
347
|
+
error: 'Request already exists',
|
|
348
|
+
message: `Cannot create duplicate request with ID '${request}'`,
|
|
349
|
+
details: 'This request ID must be unique'
|
|
350
|
+
}))
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
m.res.write(`start response ${request}\r\n`)
|
|
344
354
|
|
|
345
355
|
// let the requester know we've multiplexed their response
|
|
346
356
|
var og_stream = res.stream
|
|
@@ -363,7 +373,7 @@ function braidify (req, res, next) {
|
|
|
363
373
|
if (og_stream) {
|
|
364
374
|
og_stream.respond({
|
|
365
375
|
':status': 293,
|
|
366
|
-
'Multiplex-
|
|
376
|
+
'Multiplex-Through': req.headers['multiplex-through'],
|
|
367
377
|
'Multiplex-Version': multiplex_version,
|
|
368
378
|
...Object.fromEntries(cors_headers)
|
|
369
379
|
})
|
|
@@ -371,7 +381,7 @@ function braidify (req, res, next) {
|
|
|
371
381
|
og_stream.end()
|
|
372
382
|
} else {
|
|
373
383
|
og_socket.write('HTTP/1.1 293 Responded via multiplexer\r\n')
|
|
374
|
-
og_socket.write(`Multiplex-
|
|
384
|
+
og_socket.write(`Multiplex-Through: ${req.headers['multiplex-through']}\r\n`)
|
|
375
385
|
og_socket.write(`Multiplex-Version: ${multiplex_version}\r\n`)
|
|
376
386
|
cors_headers.forEach(([key, value]) =>
|
|
377
387
|
og_socket.write(`${key}: ${value}\r\n`))
|
|
@@ -397,14 +407,8 @@ function braidify (req, res, next) {
|
|
|
397
407
|
|
|
398
408
|
try {
|
|
399
409
|
var len = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk, encoding)
|
|
400
|
-
this.multiplexer.res.write(`${len} bytes for
|
|
410
|
+
this.multiplexer.res.write(`${len} bytes for response ${this.request}\r\n`)
|
|
401
411
|
this.multiplexer.res.write(chunk, encoding, callback)
|
|
402
|
-
|
|
403
|
-
// console.log(`wrote:`)
|
|
404
|
-
// console.log(`${len} bytes for request /${this.request}\r\n`)
|
|
405
|
-
// if (Buffer.isBuffer(chunk)) console.log(new TextDecoder().decode(chunk))
|
|
406
|
-
// else console.log('STRING?: ' + chunk)
|
|
407
|
-
|
|
408
412
|
} catch (e) {
|
|
409
413
|
callback(e)
|
|
410
414
|
}
|
|
@@ -427,7 +431,7 @@ function braidify (req, res, next) {
|
|
|
427
431
|
|
|
428
432
|
// when our fake response is done,
|
|
429
433
|
// we want to send a special message to the multiplexer saying so
|
|
430
|
-
res2.on('finish', () => m.res.write(`close
|
|
434
|
+
res2.on('finish', () => m.res.write(`close response ${request}\r\n`))
|
|
431
435
|
|
|
432
436
|
// we want access to "res" to be forwarded to our fake "res2",
|
|
433
437
|
// so that it goes into the multiplexer
|