braid-text 0.0.4 → 0.0.5
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/README.md +5 -1
- package/index.js +36 -1
- package/package.json +1 -1
- package/server-demo.js +2 -6
package/README.md
CHANGED
|
@@ -105,7 +105,11 @@ server.on("request", (req, res) => {
|
|
|
105
105
|
},
|
|
106
106
|
generate_local_diff_update: (prev_state) => {
|
|
107
107
|
|
|
108
|
-
// Compute diff between prev_state ^ and the current textarea string
|
|
108
|
+
// Compute diff between prev_state ^ and the current textarea string,
|
|
109
|
+
// which might look like [{
|
|
110
|
+
// range: [5:5],
|
|
111
|
+
// content: " World"
|
|
112
|
+
// }] to insert something after a prev_state of "Hello"
|
|
109
113
|
|
|
110
114
|
// Then return the new state (as a string) and the diff (as `patches`)
|
|
111
115
|
return {new_state, patches}
|
package/index.js
CHANGED
|
@@ -483,6 +483,15 @@ braid_text.put = async (key, options) => {
|
|
|
483
483
|
await resource.db_delta(resource.doc.getPatchSince(v_before))
|
|
484
484
|
}
|
|
485
485
|
|
|
486
|
+
braid_text.list = async () => {
|
|
487
|
+
var pages = new Set()
|
|
488
|
+
for (let x of await require('fs').promises.readdir(braid_text.db_folder)) {
|
|
489
|
+
let m = x.match(/^(.*)\.\d+$/)
|
|
490
|
+
if (m) pages.add(decode_filename(m[1]))
|
|
491
|
+
}
|
|
492
|
+
return [...pages.keys()]
|
|
493
|
+
}
|
|
494
|
+
|
|
486
495
|
async function get_resource(key) {
|
|
487
496
|
let cache = get_resource.cache || (get_resource.cache = {})
|
|
488
497
|
if (cache[key]) return cache[key]
|
|
@@ -496,7 +505,7 @@ async function get_resource(key) {
|
|
|
496
505
|
let { change, delete_me } = braid_text.db_folder
|
|
497
506
|
? await file_sync(
|
|
498
507
|
braid_text.db_folder,
|
|
499
|
-
|
|
508
|
+
encode_filename(key),
|
|
500
509
|
(bytes) => resource.doc.mergeBytes(bytes),
|
|
501
510
|
() => resource.doc.toBytes()
|
|
502
511
|
)
|
|
@@ -1260,4 +1269,30 @@ function codePoints_to_index(str, codePoints) {
|
|
|
1260
1269
|
return i
|
|
1261
1270
|
}
|
|
1262
1271
|
|
|
1272
|
+
function encode_filename(filename) {
|
|
1273
|
+
// Replace all '!' with '%21'
|
|
1274
|
+
let encoded = filename.replace(/!/g, '%21');
|
|
1275
|
+
|
|
1276
|
+
// Encode the filename using encodeURIComponent()
|
|
1277
|
+
encoded = encodeURIComponent(encoded);
|
|
1278
|
+
|
|
1279
|
+
// Replace all '%2F' (for '/') with '!'
|
|
1280
|
+
encoded = encoded.replace(/%2F/g, '!');
|
|
1281
|
+
|
|
1282
|
+
return encoded;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function decode_filename(encodedFilename) {
|
|
1286
|
+
// Replace all '!' with '%2F'
|
|
1287
|
+
let decoded = encodedFilename.replace(/!/g, '%2F');
|
|
1288
|
+
|
|
1289
|
+
// Decode the filename using decodeURIComponent()
|
|
1290
|
+
decoded = decodeURIComponent(decoded);
|
|
1291
|
+
|
|
1292
|
+
// Replace all '%21' with '!'
|
|
1293
|
+
decoded = decoded.replace(/%21/g, '!');
|
|
1294
|
+
|
|
1295
|
+
return decoded;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1263
1298
|
module.exports = braid_text
|
package/package.json
CHANGED
package/server-demo.js
CHANGED
|
@@ -27,11 +27,7 @@ var server = require("http").createServer(async (req, res) => {
|
|
|
27
27
|
// which displays all the currently used keys
|
|
28
28
|
//
|
|
29
29
|
// if (req.url === '/pages') {
|
|
30
|
-
// var pages =
|
|
31
|
-
// for (let x of await require('fs').promises.readdir(db_folder)) {
|
|
32
|
-
// let m = x.match(/^(.*)\.\d+$/)
|
|
33
|
-
// if (m) pages.add(decodeURIComponent(m[1]))
|
|
34
|
-
// }
|
|
30
|
+
// var pages = await braid_text.list()
|
|
35
31
|
// res.writeHead(200, {
|
|
36
32
|
// "Content-Type": "application/json",
|
|
37
33
|
// "Access-Control-Allow-Origin": "*",
|
|
@@ -39,7 +35,7 @@ var server = require("http").createServer(async (req, res) => {
|
|
|
39
35
|
// "Access-Control-Allow-Headers": "*",
|
|
40
36
|
// "Access-Control-Expose-Headers": "*"
|
|
41
37
|
// })
|
|
42
|
-
// res.end(JSON.stringify(
|
|
38
|
+
// res.end(JSON.stringify(pages))
|
|
43
39
|
// return
|
|
44
40
|
// }
|
|
45
41
|
|