braid-http 1.3.21 → 1.3.22
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 -5
- package/braid-http-server.js +52 -10
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -236,7 +236,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
236
236
|
// then we want to multiplex
|
|
237
237
|
var subscription_counts_on_close = null
|
|
238
238
|
if (params.headers.has('subscribe')) {
|
|
239
|
-
var origin = url
|
|
239
|
+
var origin = new URL(url, typeof document !== 'undefined' ? document.baseURI : undefined).origin
|
|
240
240
|
if (!braid_fetch.subscription_counts)
|
|
241
241
|
braid_fetch.subscription_counts = {}
|
|
242
242
|
braid_fetch.subscription_counts[origin] =
|
|
@@ -847,11 +847,19 @@ async function multiplex_fetch(url, params) {
|
|
|
847
847
|
|
|
848
848
|
// attempt to establish a multiplexed connection
|
|
849
849
|
try {
|
|
850
|
+
if (params.use_multiplex_header) throw 'skip to trying header'
|
|
850
851
|
var r = await braid_fetch(`${origin}/${multiplexer}`, {method: 'MULTIPLEX', retry: true})
|
|
851
852
|
} catch (e) {
|
|
852
|
-
//
|
|
853
|
-
|
|
854
|
-
|
|
853
|
+
// some servers don't like custom methods,
|
|
854
|
+
// so let's try with a custom header
|
|
855
|
+
try {
|
|
856
|
+
var using_multiplex_header = true
|
|
857
|
+
r = await braid_fetch(`${origin}/${multiplexer}`, {headers: {MULTIPLEX: true}, retry: true})
|
|
858
|
+
} catch (e) {
|
|
859
|
+
// fallback to normal fetch if multiplexed connection fails
|
|
860
|
+
console.error(`Could not establish multiplexed connection.\nGot error: ${e}.\nFalling back to normal connection.`)
|
|
861
|
+
return (url, params) => normal_fetch(url, params)
|
|
862
|
+
}
|
|
855
863
|
}
|
|
856
864
|
|
|
857
865
|
// parse the multiplexed stream,
|
|
@@ -912,7 +920,7 @@ async function multiplex_fetch(url, params) {
|
|
|
912
920
|
stream_error = e
|
|
913
921
|
bytes_available()
|
|
914
922
|
try {
|
|
915
|
-
await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {method: 'MULTIPLEX', retry: true})
|
|
923
|
+
await braid_fetch(`${origin}${params.headers.get('multiplexer')}`, {...using_multiplex_header ? {headers: {MULTIPLEX: true}} : {method: 'MULTIPLEX'}, retry: true})
|
|
916
924
|
} catch (e) {
|
|
917
925
|
console.error(`Could not cancel multiplexed connection:`, e)
|
|
918
926
|
throw e
|
package/braid-http-server.js
CHANGED
|
@@ -243,7 +243,7 @@ function braidify (req, res, next) {
|
|
|
243
243
|
req.subscribe = subscribe
|
|
244
244
|
|
|
245
245
|
// Multiplexer stuff
|
|
246
|
-
if (braidify.use_multiplexing && req.method === 'MULTIPLEX') {
|
|
246
|
+
if (braidify.use_multiplexing && (req.method === 'MULTIPLEX' || req.headers.multiplex)) {
|
|
247
247
|
// parse the multiplexer id and stream id from the url
|
|
248
248
|
var [multiplexer, stream] = req.url.slice(1).split('/')
|
|
249
249
|
|
|
@@ -334,14 +334,19 @@ function braidify (req, res, next) {
|
|
|
334
334
|
}
|
|
335
335
|
|
|
336
336
|
_write(chunk, encoding, callback) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
337
|
+
try {
|
|
338
|
+
var len = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk, encoding)
|
|
339
|
+
this.multiplexer.res.write(`${len} bytes for stream ${this.stream}\r\n`)
|
|
340
|
+
this.multiplexer.res.write(chunk, encoding, callback)
|
|
341
|
+
|
|
342
|
+
// console.log(`wrote:`)
|
|
343
|
+
// console.log(`${len} bytes for stream /${this.stream}\r\n`)
|
|
344
|
+
// if (Buffer.isBuffer(chunk)) console.log(new TextDecoder().decode(chunk))
|
|
345
|
+
// else console.log('STRING?: ' + chunk)
|
|
346
|
+
|
|
347
|
+
} catch (e) {
|
|
348
|
+
callback(e)
|
|
349
|
+
}
|
|
345
350
|
}
|
|
346
351
|
}
|
|
347
352
|
var mw = new MultiplexedWritable(m, stream)
|
|
@@ -368,7 +373,31 @@ function braidify (req, res, next) {
|
|
|
368
373
|
} while (obj = Object.getPrototypeOf(obj))
|
|
369
374
|
}
|
|
370
375
|
for (let key of get_props(res)) {
|
|
371
|
-
|
|
376
|
+
// skip keys that break stuff for some reason
|
|
377
|
+
if (
|
|
378
|
+
// just touching these seems to cause issues
|
|
379
|
+
key === '_events' || key === 'emit'
|
|
380
|
+
|
|
381
|
+
// because we called res.end above,
|
|
382
|
+
// in http 1, node is going to wait
|
|
383
|
+
// for the event loop to fire again,
|
|
384
|
+
// and then call these keys:
|
|
385
|
+
// "socket" to set it to null,
|
|
386
|
+
// "detachSocket" to do that,
|
|
387
|
+
// "_closed" to determine if the socket is closed;
|
|
388
|
+
// we're going to override that to say true,
|
|
389
|
+
// we do that below..
|
|
390
|
+
// we do it so we don't need to add "destroyed" here,
|
|
391
|
+
// because if _closed was false,
|
|
392
|
+
// it would try to set destroyed to true
|
|
393
|
+
|| key === 'socket'
|
|
394
|
+
|| key === 'detachSocket'
|
|
395
|
+
|| key === '_closed'
|
|
396
|
+
|
|
397
|
+
// adding these lines gets rid of some deprecation warnings.. keep?
|
|
398
|
+
|| key === '_headers'
|
|
399
|
+
|| key === '_headerNames') continue
|
|
400
|
+
|
|
372
401
|
if (res2[key] === undefined) continue
|
|
373
402
|
var value = res[key]
|
|
374
403
|
if (typeof value === 'function') {
|
|
@@ -383,6 +412,19 @@ function braidify (req, res, next) {
|
|
|
383
412
|
}
|
|
384
413
|
}
|
|
385
414
|
|
|
415
|
+
// node http 1 has the issue that when we call res.end,
|
|
416
|
+
// which we do above, not everything happens right away..
|
|
417
|
+
// in the next js event loop tick, it is going to
|
|
418
|
+
// try to tear down the stream,
|
|
419
|
+
// but we have proxied all the properties,
|
|
420
|
+
// so it would tear down our new res2 stream..
|
|
421
|
+
// to prevent that, we sacrafice this property
|
|
422
|
+
// (which the end-user hopefully won't be accessing anyway)
|
|
423
|
+
// to make the http 1 tear down code think its job is complete already,
|
|
424
|
+
// otherwise it would want to set "destroyed",
|
|
425
|
+
// and we would need to not proxy that key as well
|
|
426
|
+
res._closed = true
|
|
427
|
+
|
|
386
428
|
// this is provided so code can know if the response has been multiplexed
|
|
387
429
|
res.multiplexer = res2
|
|
388
430
|
}
|