braid-text 0.0.24 → 0.0.26
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 +63 -57
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -210,12 +210,12 @@ braid_text.get = async (key, options) => {
|
|
|
210
210
|
if (options.version || options.parents) doc = dt_get(doc, options.version || options.parents)
|
|
211
211
|
|
|
212
212
|
return {
|
|
213
|
-
version: doc.getRemoteVersion().map((x) => x.join("-")),
|
|
213
|
+
version: doc.getRemoteVersion().map((x) => x.join("-")).sort(),
|
|
214
214
|
body: doc.get()
|
|
215
215
|
}
|
|
216
216
|
} else {
|
|
217
217
|
if (options.merge_type != "dt") {
|
|
218
|
-
let version = resource.doc.getRemoteVersion().map((x) => x.join("-"))
|
|
218
|
+
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
219
219
|
let x = { version }
|
|
220
220
|
|
|
221
221
|
if (!options.parents && !options.version) {
|
|
@@ -300,7 +300,7 @@ braid_text.put = async (key, options) => {
|
|
|
300
300
|
|
|
301
301
|
let resource = (typeof key == 'string') ? await get_resource(key) : key
|
|
302
302
|
|
|
303
|
-
let parents = resource.doc.getRemoteVersion().map((x) => x.join("-"))
|
|
303
|
+
let parents = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
304
304
|
let og_parents = options_parents || parents
|
|
305
305
|
|
|
306
306
|
let max_pos = count_code_points(v_eq(parents, og_parents) ?
|
|
@@ -440,7 +440,7 @@ braid_text.put = async (key, options) => {
|
|
|
440
440
|
patches = get_xf_patches(resource.doc, v_before)
|
|
441
441
|
console.log(JSON.stringify({ patches }))
|
|
442
442
|
|
|
443
|
-
let version = resource.doc.getRemoteVersion().map((x) => x.join("-"))
|
|
443
|
+
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
444
444
|
|
|
445
445
|
for (let client of resource.simpleton_clients) {
|
|
446
446
|
if (client.peer == peer) {
|
|
@@ -450,7 +450,7 @@ braid_text.put = async (key, options) => {
|
|
|
450
450
|
function set_timeout(time_override) {
|
|
451
451
|
if (client.my_timeout) clearTimeout(client.my_timeout)
|
|
452
452
|
client.my_timeout = setTimeout(() => {
|
|
453
|
-
let version = resource.doc.getRemoteVersion().map((x) => x.join("-"))
|
|
453
|
+
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
454
454
|
let x = { version }
|
|
455
455
|
x.parents = client.my_last_seen_version
|
|
456
456
|
|
|
@@ -513,7 +513,7 @@ braid_text.put = async (key, options) => {
|
|
|
513
513
|
}
|
|
514
514
|
} else {
|
|
515
515
|
if (resource.simpleton_clients.size) {
|
|
516
|
-
let version = resource.doc.getRemoteVersion().map((x) => x.join("-"))
|
|
516
|
+
let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
517
517
|
patches = get_xf_patches(resource.doc, v_before)
|
|
518
518
|
let x = { version, parents, patches }
|
|
519
519
|
console.log(`sending: ${JSON.stringify(x)}`)
|
|
@@ -695,62 +695,67 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
695
695
|
}
|
|
696
696
|
}
|
|
697
697
|
|
|
698
|
+
let chain = Promise.resolve()
|
|
698
699
|
return {
|
|
699
700
|
change: async (bytes) => {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
console.log(`starting new db..`)
|
|
714
|
-
|
|
715
|
-
currentNumber++
|
|
716
|
-
const init = get_init()
|
|
717
|
-
const buffer = Buffer.allocUnsafe(4)
|
|
718
|
-
buffer.writeUInt32LE(init.length, 0)
|
|
719
|
-
|
|
720
|
-
const newFilename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
721
|
-
await fs.promises.writeFile(newFilename, buffer)
|
|
722
|
-
await fs.promises.appendFile(newFilename, init)
|
|
723
|
-
|
|
724
|
-
console.log("wrote to : " + newFilename)
|
|
725
|
-
|
|
726
|
-
currentSize = 4 + init.length
|
|
727
|
-
threshold = currentSize * 10
|
|
701
|
+
await (chain = chain.then(async () => {
|
|
702
|
+
currentSize += bytes.length + 4 // we account for the extra 4 bytes for uint32
|
|
703
|
+
const filename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
704
|
+
if (currentSize < threshold) {
|
|
705
|
+
console.log(`appending to db..`)
|
|
706
|
+
|
|
707
|
+
let buffer = Buffer.allocUnsafe(4)
|
|
708
|
+
buffer.writeUInt32LE(bytes.length, 0)
|
|
709
|
+
await fs.promises.appendFile(filename, buffer)
|
|
710
|
+
await fs.promises.appendFile(filename, bytes)
|
|
711
|
+
|
|
712
|
+
console.log("wrote to : " + filename)
|
|
713
|
+
} else {
|
|
728
714
|
try {
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
715
|
+
console.log(`starting new db..`)
|
|
716
|
+
|
|
717
|
+
currentNumber++
|
|
718
|
+
const init = get_init()
|
|
719
|
+
const buffer = Buffer.allocUnsafe(4)
|
|
720
|
+
buffer.writeUInt32LE(init.length, 0)
|
|
721
|
+
|
|
722
|
+
const newFilename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
723
|
+
await fs.promises.writeFile(newFilename, buffer)
|
|
724
|
+
await fs.promises.appendFile(newFilename, init)
|
|
725
|
+
|
|
726
|
+
console.log("wrote to : " + newFilename)
|
|
727
|
+
|
|
728
|
+
currentSize = 4 + init.length
|
|
729
|
+
threshold = currentSize * 10
|
|
730
|
+
try {
|
|
731
|
+
await fs.promises.unlink(filename)
|
|
732
|
+
} catch (e) { }
|
|
733
|
+
} catch (e) {
|
|
734
|
+
console.log(`e = ${e.stack}`)
|
|
735
|
+
}
|
|
733
736
|
}
|
|
734
|
-
}
|
|
737
|
+
}))
|
|
735
738
|
},
|
|
736
739
|
delete_me: async () => {
|
|
737
|
-
await
|
|
738
|
-
(
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
740
|
+
await (chain = chain.then(async () => {
|
|
741
|
+
await Promise.all(
|
|
742
|
+
(
|
|
743
|
+
await get_files_for_key(key)
|
|
744
|
+
).map((file) => {
|
|
745
|
+
return new Promise((resolve, reject) => {
|
|
746
|
+
fs.unlink(file, (err) => {
|
|
747
|
+
if (err) {
|
|
748
|
+
console.error(`Error deleting file: ${file}`)
|
|
749
|
+
reject(err)
|
|
750
|
+
} else {
|
|
751
|
+
console.log(`Deleted file: ${file}`)
|
|
752
|
+
resolve()
|
|
753
|
+
}
|
|
754
|
+
})
|
|
750
755
|
})
|
|
751
756
|
})
|
|
752
|
-
|
|
753
|
-
)
|
|
757
|
+
)
|
|
758
|
+
}))
|
|
754
759
|
},
|
|
755
760
|
}
|
|
756
761
|
}
|
|
@@ -836,7 +841,7 @@ function OpLog_get_patches(bytes, op_runs) {
|
|
|
836
841
|
let patches = []
|
|
837
842
|
op_runs.forEach((op_run) => {
|
|
838
843
|
let version = versions[i].join("-")
|
|
839
|
-
let parents = parentss[i].map((x) => x.join("-"))
|
|
844
|
+
let parents = parentss[i].map((x) => x.join("-")).sort()
|
|
840
845
|
let start = op_run.start
|
|
841
846
|
let end = start + 1
|
|
842
847
|
if (op_run.content) op_run.content = [...op_run.content]
|
|
@@ -863,7 +868,7 @@ function OpLog_get_patches(bytes, op_runs) {
|
|
|
863
868
|
})
|
|
864
869
|
if (j == len) break
|
|
865
870
|
version = versions[I].join("-")
|
|
866
|
-
parents = parentss[I].map((x) => x.join("-"))
|
|
871
|
+
parents = parentss[I].map((x) => x.join("-")).sort()
|
|
867
872
|
start = op_run.start + j
|
|
868
873
|
content = ""
|
|
869
874
|
}
|
|
@@ -1450,6 +1455,7 @@ function decode_filename(encodedFilename) {
|
|
|
1450
1455
|
|
|
1451
1456
|
function validate_version_array(x) {
|
|
1452
1457
|
if (!Array.isArray(x)) throw new Error(`invalid version array: not an array`)
|
|
1458
|
+
x.sort()
|
|
1453
1459
|
for (xx of x) validate_actor_seq(xx)
|
|
1454
1460
|
}
|
|
1455
1461
|
|
|
@@ -1468,7 +1474,7 @@ function is_valid_actor(x) {
|
|
|
1468
1474
|
try {
|
|
1469
1475
|
validate_actor(x)
|
|
1470
1476
|
return true
|
|
1471
|
-
} catch (e) {}
|
|
1477
|
+
} catch (e) { }
|
|
1472
1478
|
}
|
|
1473
1479
|
|
|
1474
1480
|
function decode_version(v) {
|