braid-http 1.3.76 → 1.3.78
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 +48 -4
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -439,7 +439,10 @@ async function braid_fetch (url, params = {}) {
|
|
|
439
439
|
|
|
440
440
|
if (params.retry && !res.ok) {
|
|
441
441
|
var give_up
|
|
442
|
-
if (params.retry
|
|
442
|
+
if (typeof params.retry === 'function') {
|
|
443
|
+
give_up = !params.retry(res)
|
|
444
|
+
} else if (params.retry.retryRes) {
|
|
445
|
+
// deprecated in favor of setting retry to a function
|
|
443
446
|
give_up = !params.retry.retryRes(res)
|
|
444
447
|
} else {
|
|
445
448
|
give_up = res.status >= 400 && res.status < 600
|
|
@@ -609,7 +612,7 @@ var subscription_parser = (cb) => ({
|
|
|
609
612
|
function parse_update (state) {
|
|
610
613
|
// If we don't have headers yet, let's try to parse some
|
|
611
614
|
if (!state.headers) {
|
|
612
|
-
var parsed = parse_headers(state.input)
|
|
615
|
+
var parsed = parse_headers(state.input, true)
|
|
613
616
|
|
|
614
617
|
// If header-parsing fails, send the error upstream
|
|
615
618
|
if (parsed.result === 'error')
|
|
@@ -633,13 +636,53 @@ function parse_update (state) {
|
|
|
633
636
|
}
|
|
634
637
|
|
|
635
638
|
// Parsing helpers
|
|
636
|
-
function parse_headers (input) {
|
|
639
|
+
function parse_headers (input, check_for_encoding_blocks) {
|
|
637
640
|
|
|
638
641
|
// Find the start of the headers
|
|
639
642
|
var start = 0
|
|
640
643
|
while (input[start] === 13 || input[start] === 10) start++
|
|
641
644
|
if (start === input.length) return {result: 'waiting'}
|
|
642
645
|
|
|
646
|
+
// Check for an "Encoding" block like this:
|
|
647
|
+
//
|
|
648
|
+
// Encoding: dt
|
|
649
|
+
// Length: 411813
|
|
650
|
+
// <binary dt file>
|
|
651
|
+
//
|
|
652
|
+
// Such a block will start with an "e", not an "h"
|
|
653
|
+
if (check_for_encoding_blocks &&
|
|
654
|
+
(input[start] === 101 || input[start] === 69)) {
|
|
655
|
+
|
|
656
|
+
// Look for two newlines
|
|
657
|
+
var end = start
|
|
658
|
+
var count = 0
|
|
659
|
+
while (++end) {
|
|
660
|
+
if (end > input.length) return {result: 'waiting'}
|
|
661
|
+
if (input[end - 1] === 10) count++
|
|
662
|
+
if (count === 2) break
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// Extract the header string
|
|
666
|
+
var headers_source = input.slice(start, end).map(x => String.fromCharCode(x)).join('')
|
|
667
|
+
|
|
668
|
+
// Parse
|
|
669
|
+
var m = headers_source.match(/Encoding:\s*(\w+)\r?\nLength:\s*(\d+)\r?\n/i)
|
|
670
|
+
if (!m) return {
|
|
671
|
+
result: 'error',
|
|
672
|
+
message: `Parse error in encoding block: ${JSON.stringify(headers_source)}`
|
|
673
|
+
}
|
|
674
|
+
var headers = {
|
|
675
|
+
encoding: m[1],
|
|
676
|
+
length: m[2]
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Update the input
|
|
680
|
+
input = input.slice(end)
|
|
681
|
+
|
|
682
|
+
// And return the parsed result
|
|
683
|
+
return { result: 'success', headers, input }
|
|
684
|
+
}
|
|
685
|
+
|
|
643
686
|
// Look for the double-newline at the end of the headers.
|
|
644
687
|
var end = start
|
|
645
688
|
while (++end) {
|
|
@@ -711,7 +754,8 @@ function parse_body (state) {
|
|
|
711
754
|
|
|
712
755
|
// Parse Body Snapshot
|
|
713
756
|
|
|
714
|
-
var content_length = parseInt(state.headers['content-length']
|
|
757
|
+
var content_length = parseInt(state.headers['content-length'] ??
|
|
758
|
+
(state.headers.patches === undefined && state.headers['length']))
|
|
715
759
|
if (!isNaN(content_length)) {
|
|
716
760
|
|
|
717
761
|
// We've read a Content-Length, so we have a block to parse
|