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