braid-text 0.0.13 → 0.0.14
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 +35 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -10,6 +10,8 @@ let braid_text = {
|
|
|
10
10
|
let waiting_puts = 0
|
|
11
11
|
let prev_put_p = null
|
|
12
12
|
|
|
13
|
+
let max_encoded_key_size = 170
|
|
14
|
+
|
|
13
15
|
braid_text.serve = async (req, res, options = {}) => {
|
|
14
16
|
options = {
|
|
15
17
|
key: req.url.split('?')[0], // Default key
|
|
@@ -499,8 +501,9 @@ braid_text.list = async () => {
|
|
|
499
501
|
try {
|
|
500
502
|
var pages = new Set()
|
|
501
503
|
for (let x of await require('fs').promises.readdir(braid_text.db_folder)) {
|
|
502
|
-
let
|
|
503
|
-
if (
|
|
504
|
+
let k = x.replace(/\.\w+$/, '')
|
|
505
|
+
if (k.length <= max_encoded_key_size) pages.add(decode_filename(k))
|
|
506
|
+
else if (x.endsWith('.name')) pages.add(await require('fs').promises.readFile(`${braid_text.db_folder}/${x}`, { encoding: 'utf8' }))
|
|
504
507
|
}
|
|
505
508
|
return [...pages.keys()]
|
|
506
509
|
} catch (e) { return [] }
|
|
@@ -537,7 +540,7 @@ async function get_resource(key) {
|
|
|
537
540
|
|
|
538
541
|
async function get_files_for_key(key) {
|
|
539
542
|
try {
|
|
540
|
-
let re = new RegExp("^" + encode_filename(key).replace(/[^a-zA-Z0-9]/g, "\\$&") + "\\.\\
|
|
543
|
+
let re = new RegExp("^" + encode_filename(key).replace(/[^a-zA-Z0-9]/g, "\\$&") + "\\.\\w+$")
|
|
541
544
|
return (await fs.promises.readdir(braid_text.db_folder))
|
|
542
545
|
.filter((a) => re.test(a))
|
|
543
546
|
.map((a) => `${braid_text.db_folder}/${a}`)
|
|
@@ -550,10 +553,29 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
550
553
|
let threshold = 0
|
|
551
554
|
|
|
552
555
|
// Ensure the existence of db_folder
|
|
553
|
-
|
|
556
|
+
if (!file_sync.init_p) file_sync.init_p = new Promise(async done => {
|
|
557
|
+
await fs.promises.mkdir(braid_text.db_folder, { recursive: true });
|
|
558
|
+
|
|
559
|
+
// 0.0.13 -> 0.0.14
|
|
560
|
+
// look for files with key-encodings over max_encoded_key_size,
|
|
561
|
+
// and convert them using the new method
|
|
562
|
+
for (let x of await fs.promises.readdir(braid_text.db_folder)) {
|
|
563
|
+
let k = x.replace(/(_[0-9a-f]{64})?\.\w+$/, '')
|
|
564
|
+
if (k.length > max_encoded_key_size) {
|
|
565
|
+
k = decode_filename(k)
|
|
566
|
+
|
|
567
|
+
await fs.promises.rename(`${braid_text.db_folder}/${x}`, `${braid_text.db_folder}/${encode_filename(k)}${x.match(/\.\w+$/)[0]}`)
|
|
568
|
+
await fs.promises.writeFile(`${braid_text.db_folder}/${encode_filename(k)}.name`, k)
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
done()
|
|
573
|
+
})
|
|
574
|
+
await file_sync.init_p
|
|
554
575
|
|
|
555
576
|
// Read existing files and sort by numbers.
|
|
556
577
|
const files = (await get_files_for_key(key))
|
|
578
|
+
.filter(x => x.match(/\.\d+$/))
|
|
557
579
|
.sort((a, b) => parseInt(a.match(/\d+$/)[0]) - parseInt(b.match(/\d+$/)[0]))
|
|
558
580
|
|
|
559
581
|
// Try to process files starting from the highest number.
|
|
@@ -609,12 +631,15 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
609
631
|
try {
|
|
610
632
|
console.log(`starting new db..`)
|
|
611
633
|
|
|
634
|
+
let encoded = encode_filename(key)
|
|
635
|
+
if (encoded.length > max_encoded_key_size) await fs.promises.writeFile(`${braid_text.db_folder}/${encoded}.name`, key)
|
|
636
|
+
|
|
612
637
|
currentNumber++
|
|
613
638
|
const init = get_init()
|
|
614
639
|
const buffer = Buffer.allocUnsafe(4)
|
|
615
640
|
buffer.writeUInt32LE(init.length, 0)
|
|
616
641
|
|
|
617
|
-
const newFilename = `${braid_text.db_folder}/${
|
|
642
|
+
const newFilename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
618
643
|
await fs.promises.writeFile(newFilename, buffer)
|
|
619
644
|
await fs.promises.appendFile(newFilename, init)
|
|
620
645
|
|
|
@@ -1281,6 +1306,11 @@ function encode_filename(filename) {
|
|
|
1281
1306
|
// Encode the filename using encodeURIComponent()
|
|
1282
1307
|
let encoded = encodeURIComponent(swapped)
|
|
1283
1308
|
|
|
1309
|
+
// Do something special with a hash if the encoding is too long
|
|
1310
|
+
if (encoded.length > max_encoded_key_size) {
|
|
1311
|
+
encoded = `${swapped.slice(0, max_encoded_key_size)}_${require('crypto').createHash('sha256').update(filename).digest('hex')}`
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1284
1314
|
return encoded
|
|
1285
1315
|
}
|
|
1286
1316
|
|