braid-text 0.0.29 → 0.0.31
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/index.js +23 -21
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ let braidify = require("braid-http").http_server
|
|
|
4
4
|
let fs = require("fs")
|
|
5
5
|
|
|
6
6
|
let braid_text = {
|
|
7
|
+
verbose: false,
|
|
7
8
|
db_folder: './braid-text-db',
|
|
8
9
|
length_cache_size: 10,
|
|
9
10
|
cache: {}
|
|
@@ -86,6 +87,7 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
86
87
|
res.setHeader("Version", x.version.map((x) => JSON.stringify(x)).join(", "))
|
|
87
88
|
|
|
88
89
|
const buffer = Buffer.from(x.body, "utf8")
|
|
90
|
+
res.setHeader("Repr-Digest", `sha-256=:${require('crypto').createHash('sha256').update(buffer).digest('base64')}:`)
|
|
89
91
|
res.setHeader("Content-Length", buffer.length)
|
|
90
92
|
|
|
91
93
|
if (req.method === "HEAD") return my_end(200)
|
|
@@ -127,7 +129,7 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
waiting_puts++
|
|
130
|
-
console.log(`waiting_puts(after++) = ${waiting_puts}`)
|
|
132
|
+
if (braid_text.verbose) console.log(`waiting_puts(after++) = ${waiting_puts}`)
|
|
131
133
|
|
|
132
134
|
let my_prev_put_p = prev_put_p
|
|
133
135
|
let done_my_turn = null
|
|
@@ -135,7 +137,7 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
135
137
|
(done) =>
|
|
136
138
|
(done_my_turn = (statusCode, x) => {
|
|
137
139
|
waiting_puts--
|
|
138
|
-
console.log(`waiting_puts(after--) = ${waiting_puts}`)
|
|
140
|
+
if (braid_text.verbose) console.log(`waiting_puts(after--) = ${waiting_puts}`)
|
|
139
141
|
my_end(statusCode, x)
|
|
140
142
|
done()
|
|
141
143
|
})
|
|
@@ -241,7 +243,7 @@ braid_text.get = async (key, options) => {
|
|
|
241
243
|
let updates = null
|
|
242
244
|
|
|
243
245
|
if (resource.need_defrag) {
|
|
244
|
-
console.log(`doing defrag..`)
|
|
246
|
+
if (braid_text.verbose) console.log(`doing defrag..`)
|
|
245
247
|
resource.need_defrag = false
|
|
246
248
|
resource.doc = defrag_dt(resource.doc)
|
|
247
249
|
}
|
|
@@ -440,7 +442,7 @@ braid_text.put = async (key, options) => {
|
|
|
440
442
|
|
|
441
443
|
if (options.merge_type != "dt") {
|
|
442
444
|
patches = get_xf_patches(resource.doc, v_before)
|
|
443
|
-
console.log(JSON.stringify({ patches }))
|
|
445
|
+
if (braid_text.verbose) console.log(JSON.stringify({ patches }))
|
|
444
446
|
|
|
445
447
|
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
446
448
|
|
|
@@ -456,11 +458,11 @@ braid_text.put = async (key, options) => {
|
|
|
456
458
|
let x = { version }
|
|
457
459
|
x.parents = client.my_last_seen_version
|
|
458
460
|
|
|
459
|
-
console.log("rebasing after timeout.. ")
|
|
460
|
-
console.log(" client.my_unused_version_count = " + client.my_unused_version_count)
|
|
461
|
+
if (braid_text.verbose) console.log("rebasing after timeout.. ")
|
|
462
|
+
if (braid_text.verbose) console.log(" client.my_unused_version_count = " + client.my_unused_version_count)
|
|
461
463
|
x.patches = get_xf_patches(resource.doc, OpLog_remote_to_local(resource.doc, client.my_last_seen_version))
|
|
462
464
|
|
|
463
|
-
console.log(`sending from rebase: ${JSON.stringify(x)}`)
|
|
465
|
+
if (braid_text.verbose) console.log(`sending from rebase: ${JSON.stringify(x)}`)
|
|
464
466
|
client.subscribe(x)
|
|
465
467
|
client.my_last_sent_version = x.version
|
|
466
468
|
|
|
@@ -496,12 +498,12 @@ braid_text.put = async (key, options) => {
|
|
|
496
498
|
|
|
497
499
|
x.parents = options.version
|
|
498
500
|
if (!v_eq(version, options.version)) {
|
|
499
|
-
console.log("rebasing..")
|
|
501
|
+
if (braid_text.verbose) console.log("rebasing..")
|
|
500
502
|
x.patches = get_xf_patches(resource.doc, OpLog_remote_to_local(resource.doc, [og_v]))
|
|
501
503
|
} else {
|
|
502
504
|
// this client already has this version,
|
|
503
505
|
// so let's pretend to send it back, but not
|
|
504
|
-
console.log(`not reflecting back to simpleton`)
|
|
506
|
+
if (braid_text.verbose) console.log(`not reflecting back to simpleton`)
|
|
505
507
|
client.my_last_sent_version = x.version
|
|
506
508
|
continue
|
|
507
509
|
}
|
|
@@ -509,7 +511,7 @@ braid_text.put = async (key, options) => {
|
|
|
509
511
|
x.parents = parents
|
|
510
512
|
x.patches = patches
|
|
511
513
|
}
|
|
512
|
-
console.log(`sending: ${JSON.stringify(x)}`)
|
|
514
|
+
if (braid_text.verbose) console.log(`sending: ${JSON.stringify(x)}`)
|
|
513
515
|
client.subscribe(x)
|
|
514
516
|
client.my_last_sent_version = x.version
|
|
515
517
|
}
|
|
@@ -518,7 +520,7 @@ braid_text.put = async (key, options) => {
|
|
|
518
520
|
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
519
521
|
patches = get_xf_patches(resource.doc, v_before)
|
|
520
522
|
let x = { version, parents, patches }
|
|
521
|
-
console.log(`sending: ${JSON.stringify(x)}`)
|
|
523
|
+
if (braid_text.verbose) console.log(`sending: ${JSON.stringify(x)}`)
|
|
522
524
|
for (let client of resource.simpleton_clients) {
|
|
523
525
|
if (client.my_timeout) continue
|
|
524
526
|
client.subscribe(x)
|
|
@@ -594,7 +596,7 @@ async function get_resource(key) {
|
|
|
594
596
|
}
|
|
595
597
|
|
|
596
598
|
async function db_folder_init() {
|
|
597
|
-
console.log('__!')
|
|
599
|
+
if (braid_text.verbose) console.log('__!')
|
|
598
600
|
if (!db_folder_init.p) db_folder_init.p = new Promise(async done => {
|
|
599
601
|
await fs.promises.mkdir(braid_text.db_folder, { recursive: true });
|
|
600
602
|
|
|
@@ -621,7 +623,7 @@ async function db_folder_init() {
|
|
|
621
623
|
console.log(`trying to convert file to new format, but the key is too big: ${braid_text.db_folder}/${x}`)
|
|
622
624
|
process.exit()
|
|
623
625
|
}
|
|
624
|
-
console.log(`deleting: ${braid_text.db_folder}/${x}`)
|
|
626
|
+
if (braid_text.verbose) console.log(`deleting: ${braid_text.db_folder}/${x}`)
|
|
625
627
|
await fs.promises.unlink(`${braid_text.db_folder}/${x}`)
|
|
626
628
|
}
|
|
627
629
|
}
|
|
@@ -629,7 +631,7 @@ async function db_folder_init() {
|
|
|
629
631
|
for (let x of await fs.promises.readdir(braid_text.db_folder)) {
|
|
630
632
|
let [_, k, num] = x.match(/^(.*)\.(\d+)$/s)
|
|
631
633
|
if (!convert_us[k]) continue
|
|
632
|
-
console.log(`renaming: ${braid_text.db_folder}/${x} -> ${braid_text.db_folder}/${convert_us[k]}.${num}`)
|
|
634
|
+
if (braid_text.verbose) console.log(`renaming: ${braid_text.db_folder}/${x} -> ${braid_text.db_folder}/${convert_us[k]}.${num}`)
|
|
633
635
|
if (convert_us[k]) await fs.promises.rename(`${braid_text.db_folder}/${x}`, `${braid_text.db_folder}/${convert_us[k]}.${num}`)
|
|
634
636
|
}
|
|
635
637
|
}
|
|
@@ -672,7 +674,7 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
672
674
|
}
|
|
673
675
|
try {
|
|
674
676
|
const filename = files[i]
|
|
675
|
-
console.log(`trying to process file: ${filename}`)
|
|
677
|
+
if (braid_text.verbose) console.log(`trying to process file: ${filename}`)
|
|
676
678
|
const data = await fs.promises.readFile(filename)
|
|
677
679
|
|
|
678
680
|
let cursor = 0
|
|
@@ -706,17 +708,17 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
706
708
|
currentSize += bytes.length + 4 // we account for the extra 4 bytes for uint32
|
|
707
709
|
const filename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
708
710
|
if (currentSize < threshold) {
|
|
709
|
-
console.log(`appending to db..`)
|
|
711
|
+
if (braid_text.verbose) console.log(`appending to db..`)
|
|
710
712
|
|
|
711
713
|
let buffer = Buffer.allocUnsafe(4)
|
|
712
714
|
buffer.writeUInt32LE(bytes.length, 0)
|
|
713
715
|
await fs.promises.appendFile(filename, buffer)
|
|
714
716
|
await fs.promises.appendFile(filename, bytes)
|
|
715
717
|
|
|
716
|
-
console.log("wrote to : " + filename)
|
|
718
|
+
if (braid_text.verbose) console.log("wrote to : " + filename)
|
|
717
719
|
} else {
|
|
718
720
|
try {
|
|
719
|
-
console.log(`starting new db..`)
|
|
721
|
+
if (braid_text.verbose) console.log(`starting new db..`)
|
|
720
722
|
|
|
721
723
|
currentNumber++
|
|
722
724
|
const init = get_init()
|
|
@@ -727,7 +729,7 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
727
729
|
await fs.promises.writeFile(newFilename, buffer)
|
|
728
730
|
await fs.promises.appendFile(newFilename, init)
|
|
729
731
|
|
|
730
|
-
console.log("wrote to : " + newFilename)
|
|
732
|
+
if (braid_text.verbose) console.log("wrote to : " + newFilename)
|
|
731
733
|
|
|
732
734
|
currentSize = 4 + init.length
|
|
733
735
|
threshold = currentSize * 10
|
|
@@ -735,7 +737,7 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
735
737
|
await fs.promises.unlink(filename)
|
|
736
738
|
} catch (e) { }
|
|
737
739
|
} catch (e) {
|
|
738
|
-
console.log(`e = ${e.stack}`)
|
|
740
|
+
if (braid_text.verbose) console.log(`e = ${e.stack}`)
|
|
739
741
|
}
|
|
740
742
|
}
|
|
741
743
|
}))
|
|
@@ -752,7 +754,7 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
752
754
|
console.error(`Error deleting file: ${file}`)
|
|
753
755
|
reject(err)
|
|
754
756
|
} else {
|
|
755
|
-
console.log(`Deleted file: ${file}`)
|
|
757
|
+
if (braid_text.verbose) console.log(`Deleted file: ${file}`)
|
|
756
758
|
resolve()
|
|
757
759
|
}
|
|
758
760
|
})
|