braid-http 1.0.6 → 1.1.0
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 -32
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -283,9 +283,10 @@ async function braid_fetch (url, params = {}) {
|
|
|
283
283
|
let timeout = null
|
|
284
284
|
on_heartbeat = () => {
|
|
285
285
|
clearTimeout(timeout)
|
|
286
|
+
let wait_seconds = 1.2 * heartbeats + 3
|
|
286
287
|
timeout = setTimeout(() => {
|
|
287
|
-
on_error(new Error(`heartbeat not seen in ${
|
|
288
|
-
},
|
|
288
|
+
on_error(new Error(`heartbeat not seen in ${wait_seconds.toFixed(2)}s`))
|
|
289
|
+
}, wait_seconds * 1000)
|
|
289
290
|
}
|
|
290
291
|
on_heartbeat()
|
|
291
292
|
}
|
|
@@ -314,7 +315,6 @@ async function braid_fetch (url, params = {}) {
|
|
|
314
315
|
// streamed response.
|
|
315
316
|
on_error(err)
|
|
316
317
|
},
|
|
317
|
-
!isTextContentType(res.headers.get('content-type')),
|
|
318
318
|
(...args) => {
|
|
319
319
|
on_heartbeat()
|
|
320
320
|
params.onBytes?.(...args)
|
|
@@ -401,13 +401,13 @@ async function braid_fetch (url, params = {}) {
|
|
|
401
401
|
}
|
|
402
402
|
|
|
403
403
|
// Parse a stream of versions from the incoming bytes
|
|
404
|
-
async function handle_fetch_stream (stream, cb,
|
|
404
|
+
async function handle_fetch_stream (stream, cb, on_bytes) {
|
|
405
405
|
if (is_nodejs)
|
|
406
406
|
stream = to_whatwg_stream(stream)
|
|
407
407
|
|
|
408
408
|
// Set up a reader
|
|
409
409
|
var reader = stream.getReader(),
|
|
410
|
-
parser = subscription_parser(cb
|
|
410
|
+
parser = subscription_parser(cb)
|
|
411
411
|
|
|
412
412
|
while (true) {
|
|
413
413
|
var versions = []
|
|
@@ -441,7 +441,7 @@ async function handle_fetch_stream (stream, cb, binary, on_bytes) {
|
|
|
441
441
|
// Braid-HTTP Subscription Parser
|
|
442
442
|
// ****************************
|
|
443
443
|
|
|
444
|
-
var subscription_parser = (cb
|
|
444
|
+
var subscription_parser = (cb) => ({
|
|
445
445
|
// A parser keeps some parse state
|
|
446
446
|
state: {input: []},
|
|
447
447
|
|
|
@@ -461,9 +461,6 @@ var subscription_parser = (cb, binary) => ({
|
|
|
461
461
|
// Try to parse an update
|
|
462
462
|
try {
|
|
463
463
|
this.state = parse_update (this.state)
|
|
464
|
-
|
|
465
|
-
// Parse UTF-8 if it isn't binary
|
|
466
|
-
if (!binary && this.state.body) this.state.body = (new TextDecoder('utf-8')).decode(this.state.body)
|
|
467
464
|
} catch (e) {
|
|
468
465
|
this.cb(null, e)
|
|
469
466
|
return
|
|
@@ -482,6 +479,13 @@ var subscription_parser = (cb, binary) => ({
|
|
|
482
479
|
}
|
|
483
480
|
for (var k in update)
|
|
484
481
|
if (update[k] === undefined) delete update[k]
|
|
482
|
+
|
|
483
|
+
Object.defineProperty(update, 'body_text', {
|
|
484
|
+
get: function () {
|
|
485
|
+
return new TextDecoder('utf-8').decode(this.body.buffer)
|
|
486
|
+
}
|
|
487
|
+
})
|
|
488
|
+
|
|
485
489
|
this.cb(update)
|
|
486
490
|
|
|
487
491
|
// Reset the parser for the next version!
|
|
@@ -807,29 +811,6 @@ function extractHeader(input) {
|
|
|
807
811
|
};
|
|
808
812
|
}
|
|
809
813
|
|
|
810
|
-
function isTextContentType(contentType) {
|
|
811
|
-
if (!contentType) return false
|
|
812
|
-
|
|
813
|
-
contentType = contentType.toLowerCase().trim()
|
|
814
|
-
|
|
815
|
-
// Check if it starts with "text/"
|
|
816
|
-
if (contentType.startsWith("text/")) return true
|
|
817
|
-
|
|
818
|
-
// Initialize the Map if it doesn't exist yet
|
|
819
|
-
if (!isTextContentType.textApplicationTypes) {
|
|
820
|
-
isTextContentType.textApplicationTypes = new Map([
|
|
821
|
-
["application/json", true],
|
|
822
|
-
["application/xml", true],
|
|
823
|
-
["application/javascript", true],
|
|
824
|
-
["application/ecmascript", true],
|
|
825
|
-
["application/x-www-form-urlencoded", true],
|
|
826
|
-
])
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
// Use the cached map of text-based application types
|
|
830
|
-
return isTextContentType.textApplicationTypes.has(contentType)
|
|
831
|
-
}
|
|
832
|
-
|
|
833
814
|
// ****************************
|
|
834
815
|
// Exports
|
|
835
816
|
// ****************************
|