braid-http 1.3.8 → 1.3.10
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 +25 -16
- package/braid-http-server.js +15 -4
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -162,9 +162,9 @@ async function braid_fetch (url, params = {}) {
|
|
|
162
162
|
|
|
163
163
|
// We provide some shortcuts for Braid params
|
|
164
164
|
if (params.version)
|
|
165
|
-
params.headers.set('version', params.version.map(JSON.stringify).join(', '))
|
|
165
|
+
params.headers.set('version', params.version.map(JSON.stringify).map(ascii_ify).join(', '))
|
|
166
166
|
if (Array.isArray(params.parents))
|
|
167
|
-
params.headers.set('parents', params.parents.map(JSON.stringify).join(', '))
|
|
167
|
+
params.headers.set('parents', params.parents.map(JSON.stringify).map(ascii_ify).join(', '))
|
|
168
168
|
if (params.subscribe)
|
|
169
169
|
params.headers.set('subscribe', 'true')
|
|
170
170
|
if (params.peer)
|
|
@@ -395,7 +395,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
395
395
|
|
|
396
396
|
case 502: // Bad Gateway
|
|
397
397
|
case 504: // Gateway Timeout
|
|
398
|
-
give_up = false
|
|
398
|
+
give_up = false
|
|
399
399
|
}
|
|
400
400
|
if (give_up) {
|
|
401
401
|
let e = new Error(`giving up because of http status: ${res.status}${(res.status === 401 || res.status === 403) ? ` (access denied)` : ''}`)
|
|
@@ -489,6 +489,7 @@ var subscription_parser = (cb) => ({
|
|
|
489
489
|
parents: this.state.parents,
|
|
490
490
|
body: this.state.body,
|
|
491
491
|
patches: this.state.patches,
|
|
492
|
+
status: this.state.status,
|
|
492
493
|
|
|
493
494
|
// Output extra_headers if there are some
|
|
494
495
|
extra_headers: extra_headers(this.state.headers)
|
|
@@ -563,6 +564,7 @@ function parse_update (state) {
|
|
|
563
564
|
state.headers = parsed.headers
|
|
564
565
|
state.version = state.headers.version
|
|
565
566
|
state.parents = state.headers.parents
|
|
567
|
+
state.status = state.headers[':status']
|
|
566
568
|
|
|
567
569
|
// Take the parsed headers out of the buffer
|
|
568
570
|
state.input = parsed.input
|
|
@@ -576,22 +578,24 @@ function parse_update (state) {
|
|
|
576
578
|
function parse_headers (input) {
|
|
577
579
|
|
|
578
580
|
// Find the start of the headers
|
|
579
|
-
|
|
580
|
-
while (input[
|
|
581
|
-
if (
|
|
581
|
+
var start = 0
|
|
582
|
+
while (input[start] === 13 || input[start] === 10) start++
|
|
583
|
+
if (start === input.length) return {result: 'waiting'}
|
|
582
584
|
|
|
583
585
|
// Look for the double-newline at the end of the headers.
|
|
584
|
-
|
|
585
|
-
while (++
|
|
586
|
-
if (
|
|
587
|
-
if (input[
|
|
586
|
+
var end = start
|
|
587
|
+
while (++end) {
|
|
588
|
+
if (end > input.length) return {result: 'waiting'}
|
|
589
|
+
if ( input[end - 1] === 10
|
|
590
|
+
&& (input[end - 2] === 10 || (input[end - 2] === 13 && input[end - 3] === 10)))
|
|
591
|
+
break
|
|
588
592
|
}
|
|
589
593
|
|
|
590
594
|
// Extract the header string
|
|
591
|
-
var headers_source = new TextDecoder('utf-8').decode(new Uint8Array(input.slice(
|
|
595
|
+
var headers_source = new TextDecoder('utf-8').decode(new Uint8Array(input.slice(start, end)))
|
|
592
596
|
|
|
593
|
-
//
|
|
594
|
-
headers_source = headers_source.replace(/^HTTP
|
|
597
|
+
// Convert "HTTP 200 OK" to a :status: 200 header
|
|
598
|
+
headers_source = headers_source.replace(/^HTTP\/?\d*\.?\d* (\d\d\d).*\r?\n/, ':status: $1\r\n')
|
|
595
599
|
|
|
596
600
|
var headers_length = headers_source.length
|
|
597
601
|
|
|
@@ -630,7 +634,7 @@ function parse_headers (input) {
|
|
|
630
634
|
headers.patches = JSON.parse(headers.patches)
|
|
631
635
|
|
|
632
636
|
// Update the input
|
|
633
|
-
input = input.slice(
|
|
637
|
+
input = input.slice(end)
|
|
634
638
|
|
|
635
639
|
// And return the parsed result
|
|
636
640
|
return { result: 'success', headers, input }
|
|
@@ -787,7 +791,7 @@ function extra_headers (headers) {
|
|
|
787
791
|
|
|
788
792
|
// Remove the non-extra parts
|
|
789
793
|
var known_headers = ['version', 'parents', 'patches',
|
|
790
|
-
'content-length', 'content-range']
|
|
794
|
+
'content-length', 'content-range', ':status']
|
|
791
795
|
for (var i = 0; i < known_headers.length; i++)
|
|
792
796
|
delete result[known_headers[i]]
|
|
793
797
|
|
|
@@ -807,10 +811,15 @@ function get_binary_length(x) {
|
|
|
807
811
|
function deep_copy(x) {
|
|
808
812
|
if (x === null || typeof x !== 'object') return x
|
|
809
813
|
if (Array.isArray(x)) return x.map(x => deep_copy(x))
|
|
810
|
-
if (Object.prototype.toString.call(x) === '[object Object]')
|
|
814
|
+
if (Object.prototype.toString.call(x) === '[object Object]')
|
|
815
|
+
return Object.fromEntries(Object.entries(x).map(([k, x]) => [k, deep_copy(x)]))
|
|
811
816
|
return x
|
|
812
817
|
}
|
|
813
818
|
|
|
819
|
+
function ascii_ify(s) {
|
|
820
|
+
return s.replace(/[^\x20-\x7E]/g, c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'))
|
|
821
|
+
}
|
|
822
|
+
|
|
814
823
|
// ****************************
|
|
815
824
|
// Exports
|
|
816
825
|
// ****************************
|
package/braid-http-server.js
CHANGED
|
@@ -325,7 +325,14 @@ function braidify (req, res, next) {
|
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
async function send_update(res, data, url, peer) {
|
|
328
|
-
var {version, parents, patches, patch, body} = data
|
|
328
|
+
var {version, parents, patches, patch, body, status} = data
|
|
329
|
+
|
|
330
|
+
if (status) {
|
|
331
|
+
assert(typeof status === 'number', 'sendUpdate: status must be a number')
|
|
332
|
+
assert(status > 100 && status < 600, 'sendUpdate: status must be a number between 100 and 600')
|
|
333
|
+
}
|
|
334
|
+
else
|
|
335
|
+
status = 200
|
|
329
336
|
|
|
330
337
|
function set_header (key, val) {
|
|
331
338
|
if (res.isSubscription)
|
|
@@ -379,7 +386,7 @@ async function send_update(res, data, url, peer) {
|
|
|
379
386
|
assert(body_exists || patches, 'Missing body or patches')
|
|
380
387
|
assert(!(body_exists && patches), 'Cannot send both body and patches')
|
|
381
388
|
|
|
382
|
-
if (res.isSubscription) res.write(`HTTP
|
|
389
|
+
if (res.isSubscription) res.write(`HTTP ${status} OK\r\n`)
|
|
383
390
|
|
|
384
391
|
// Write the headers or virtual headers
|
|
385
392
|
for (var [header, value] of Object.entries(data)) {
|
|
@@ -393,10 +400,10 @@ async function send_update(res, data, url, peer) {
|
|
|
393
400
|
// so we convert `value` from array to comma-separated strings.
|
|
394
401
|
if (header === 'version') {
|
|
395
402
|
header = 'Version' // Capitalize for prettiness
|
|
396
|
-
value = value.map(JSON.stringify).join(", ")
|
|
403
|
+
value = value.map(JSON.stringify).map(ascii_ify).join(", ")
|
|
397
404
|
} else if (header === 'parents') {
|
|
398
405
|
header = 'Parents' // Capitalize for prettiness
|
|
399
|
-
value = value.map(JSON.stringify).join(", ")
|
|
406
|
+
value = value.map(JSON.stringify).map(ascii_ify).join(", ")
|
|
400
407
|
}
|
|
401
408
|
|
|
402
409
|
// We don't output patches or body yet
|
|
@@ -440,4 +447,8 @@ function write_binary(res, body) {
|
|
|
440
447
|
res.write(body)
|
|
441
448
|
}
|
|
442
449
|
|
|
450
|
+
function ascii_ify(s) {
|
|
451
|
+
return s.replace(/[^\x20-\x7E]/g, c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'))
|
|
452
|
+
}
|
|
453
|
+
|
|
443
454
|
module.exports = braidify
|