braid-http 1.3.14 → 1.3.16
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 +24 -14
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -64,16 +64,19 @@ function braidify_http (http) {
|
|
|
64
64
|
on_update = f
|
|
65
65
|
|
|
66
66
|
// And set up a subscription parser
|
|
67
|
-
var parser = subscription_parser((update, error) => {
|
|
67
|
+
var parser = subscription_parser(async (update, error) => {
|
|
68
68
|
if (!error)
|
|
69
|
-
on_update && on_update(update)
|
|
69
|
+
on_update && (await on_update(update))
|
|
70
70
|
else
|
|
71
71
|
on_error && on_error(error)
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
// That will run each time we get new data
|
|
75
|
+
var chain = Promise.resolve()
|
|
75
76
|
res.orig_on('data', (chunk) => {
|
|
76
|
-
|
|
77
|
+
chain = chain.then(async () => {
|
|
78
|
+
await parser.read(chunk)
|
|
79
|
+
})
|
|
77
80
|
})
|
|
78
81
|
}
|
|
79
82
|
|
|
@@ -226,6 +229,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
226
229
|
var res = null
|
|
227
230
|
var subscription_cb = null
|
|
228
231
|
var subscription_error = null
|
|
232
|
+
var cb_running = false
|
|
229
233
|
|
|
230
234
|
return await new Promise((done, fail) => {
|
|
231
235
|
connect()
|
|
@@ -233,7 +237,11 @@ async function braid_fetch (url, params = {}) {
|
|
|
233
237
|
let on_error = e => {
|
|
234
238
|
on_error = () => {}
|
|
235
239
|
|
|
236
|
-
if (!params.retry ||
|
|
240
|
+
if (!params.retry ||
|
|
241
|
+
e.name === "AbortError" ||
|
|
242
|
+
e.startsWith?.('Parse error in headers') ||
|
|
243
|
+
cb_running) {
|
|
244
|
+
|
|
237
245
|
subscription_error?.(e)
|
|
238
246
|
return fail(e)
|
|
239
247
|
}
|
|
@@ -307,13 +315,15 @@ async function braid_fetch (url, params = {}) {
|
|
|
307
315
|
|
|
308
316
|
// Each time something happens, we'll either get a new
|
|
309
317
|
// version back, or an error.
|
|
310
|
-
(result, err) => {
|
|
318
|
+
async (result, err) => {
|
|
311
319
|
if (!err) {
|
|
312
320
|
// check whether we aborted
|
|
313
321
|
if (original_signal?.aborted) throw new DOMException('already aborted', 'AbortError')
|
|
314
322
|
|
|
315
323
|
// Yay! We got a new version! Tell the callback!
|
|
316
|
-
|
|
324
|
+
cb_running = true
|
|
325
|
+
await cb(result)
|
|
326
|
+
cb_running = false
|
|
317
327
|
} else
|
|
318
328
|
// This error handling code runs if the connection
|
|
319
329
|
// closes, or if there is unparseable stuff in the
|
|
@@ -419,21 +429,21 @@ async function handle_fetch_stream (stream, cb, on_bytes) {
|
|
|
419
429
|
var {done, value} = await reader.read()
|
|
420
430
|
}
|
|
421
431
|
catch (e) {
|
|
422
|
-
cb(null, e)
|
|
432
|
+
await cb(null, e)
|
|
423
433
|
return
|
|
424
434
|
}
|
|
425
435
|
|
|
426
436
|
// Check if this connection has been closed!
|
|
427
437
|
if (done) {
|
|
428
438
|
console.debug("Connection closed.")
|
|
429
|
-
cb(null, 'Connection closed')
|
|
439
|
+
await cb(null, 'Connection closed')
|
|
430
440
|
return
|
|
431
441
|
}
|
|
432
442
|
|
|
433
443
|
on_bytes?.(value)
|
|
434
444
|
|
|
435
445
|
// Tell the parser to process some more stream
|
|
436
|
-
parser.read(value)
|
|
446
|
+
await parser.read(value)
|
|
437
447
|
}
|
|
438
448
|
}
|
|
439
449
|
|
|
@@ -452,7 +462,7 @@ var subscription_parser = (cb) => ({
|
|
|
452
462
|
|
|
453
463
|
// You give it new input information as soon as you get it, and it will
|
|
454
464
|
// report back with new versions as soon as it finds them.
|
|
455
|
-
read (input) {
|
|
465
|
+
async read (input) {
|
|
456
466
|
|
|
457
467
|
// Store the new input!
|
|
458
468
|
for (let x of input) this.state.input.push(x)
|
|
@@ -464,7 +474,7 @@ var subscription_parser = (cb) => ({
|
|
|
464
474
|
try {
|
|
465
475
|
this.state = parse_update (this.state)
|
|
466
476
|
} catch (e) {
|
|
467
|
-
this.cb(null, e)
|
|
477
|
+
await this.cb(null, e)
|
|
468
478
|
return
|
|
469
479
|
}
|
|
470
480
|
|
|
@@ -496,9 +506,9 @@ var subscription_parser = (cb) => ({
|
|
|
496
506
|
}
|
|
497
507
|
|
|
498
508
|
try {
|
|
499
|
-
this.cb(update)
|
|
509
|
+
await this.cb(update)
|
|
500
510
|
} catch (e) {
|
|
501
|
-
this.cb(null, e)
|
|
511
|
+
await this.cb(null, e)
|
|
502
512
|
return
|
|
503
513
|
}
|
|
504
514
|
|
|
@@ -508,7 +518,7 @@ var subscription_parser = (cb) => ({
|
|
|
508
518
|
|
|
509
519
|
// Or maybe there's an error to report upstream
|
|
510
520
|
else if (this.state.result === 'error') {
|
|
511
|
-
this.cb(null, this.state.message)
|
|
521
|
+
await this.cb(null, this.state.message)
|
|
512
522
|
return
|
|
513
523
|
}
|
|
514
524
|
|