braid-text 0.2.1 → 0.2.3
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 +17 -7
- package/package.json +1 -1
- package/server-demo.js +11 -1
- package/test.html +32 -0
package/index.js
CHANGED
|
@@ -9,7 +9,8 @@ let braid_text = {
|
|
|
9
9
|
verbose: false,
|
|
10
10
|
db_folder: './braid-text-db',
|
|
11
11
|
length_cache_size: 10,
|
|
12
|
-
cache: {}
|
|
12
|
+
cache: {},
|
|
13
|
+
delete_cache: {}
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
let waiting_puts = 0
|
|
@@ -71,7 +72,7 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
71
72
|
if (req.method == "OPTIONS") return my_end(200)
|
|
72
73
|
|
|
73
74
|
if (req.method == "DELETE") {
|
|
74
|
-
await
|
|
75
|
+
await braid_text.delete(resource)
|
|
75
76
|
return my_end(200)
|
|
76
77
|
}
|
|
77
78
|
|
|
@@ -205,6 +206,11 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
205
206
|
throw new Error("unknown")
|
|
206
207
|
}
|
|
207
208
|
|
|
209
|
+
braid_text.delete = async (key) => {
|
|
210
|
+
let resource = (typeof key == 'string') ? await get_resource(key) : key
|
|
211
|
+
await resource.delete_me()
|
|
212
|
+
}
|
|
213
|
+
|
|
208
214
|
braid_text.get = async (key, options) => {
|
|
209
215
|
if (!options) {
|
|
210
216
|
// if it doesn't exist already, don't create it in this case
|
|
@@ -481,6 +487,7 @@ braid_text.put = async (key, options) => {
|
|
|
481
487
|
}
|
|
482
488
|
|
|
483
489
|
for (let b of bytes) resource.doc.mergeBytes(b)
|
|
490
|
+
resource.val = resource.doc.get()
|
|
484
491
|
|
|
485
492
|
resource.need_defrag = true
|
|
486
493
|
|
|
@@ -583,8 +590,6 @@ braid_text.put = async (key, options) => {
|
|
|
583
590
|
}
|
|
584
591
|
|
|
585
592
|
await resource.db_delta(resource.doc.getPatchSince(v_before))
|
|
586
|
-
|
|
587
|
-
resource.val = resource.doc.get()
|
|
588
593
|
}
|
|
589
594
|
|
|
590
595
|
braid_text.list = async () => {
|
|
@@ -599,9 +604,11 @@ braid_text.list = async () => {
|
|
|
599
604
|
}
|
|
600
605
|
|
|
601
606
|
async function get_resource(key) {
|
|
607
|
+
if (braid_text.delete_cache[key]) await braid_text.delete_cache[key]
|
|
608
|
+
|
|
602
609
|
let cache = braid_text.cache
|
|
603
610
|
if (!cache[key]) cache[key] = new Promise(async done => {
|
|
604
|
-
let resource = {}
|
|
611
|
+
let resource = {key}
|
|
605
612
|
resource.clients = new Set()
|
|
606
613
|
resource.simpleton_clients = new Set()
|
|
607
614
|
|
|
@@ -625,9 +632,9 @@ async function get_resource(key) {
|
|
|
625
632
|
resource.actor_seqs[v[0]] = Math.max(v[1], resource.actor_seqs[v[0]] ?? -1)
|
|
626
633
|
}
|
|
627
634
|
|
|
628
|
-
resource.delete_me = () => {
|
|
629
|
-
delete_me()
|
|
635
|
+
resource.delete_me = async () => {
|
|
630
636
|
delete cache[key]
|
|
637
|
+
await (braid_text.delete_cache[key] = delete_me())
|
|
631
638
|
}
|
|
632
639
|
|
|
633
640
|
resource.val = resource.doc.get()
|
|
@@ -745,9 +752,11 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
745
752
|
}
|
|
746
753
|
}
|
|
747
754
|
|
|
755
|
+
let deleted = false
|
|
748
756
|
let chain = Promise.resolve()
|
|
749
757
|
return {
|
|
750
758
|
change: async (bytes) => {
|
|
759
|
+
if (deleted) return
|
|
751
760
|
await (chain = chain.then(async () => {
|
|
752
761
|
currentSize += bytes.length + 4 // we account for the extra 4 bytes for uint32
|
|
753
762
|
const filename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
|
|
@@ -787,6 +796,7 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
787
796
|
}))
|
|
788
797
|
},
|
|
789
798
|
delete_me: async () => {
|
|
799
|
+
deleted = true
|
|
790
800
|
await (chain = chain.then(async () => {
|
|
791
801
|
await Promise.all(
|
|
792
802
|
(
|
package/package.json
CHANGED
package/server-demo.js
CHANGED
|
@@ -34,7 +34,17 @@ var server = require("http").createServer(async (req, res) => {
|
|
|
34
34
|
return
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
if (req.url
|
|
37
|
+
if (req.url.startsWith('/test.html')) {
|
|
38
|
+
let parts = req.url.split(/[\?&=]/g)
|
|
39
|
+
|
|
40
|
+
if (parts[1] == 'check') {
|
|
41
|
+
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
|
|
42
|
+
return res.end(JSON.stringify({
|
|
43
|
+
checking: parts[2],
|
|
44
|
+
result: (await braid_text.get(parts[2])) != null
|
|
45
|
+
}))
|
|
46
|
+
}
|
|
47
|
+
|
|
38
48
|
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
|
|
39
49
|
require("fs").createReadStream("./test.html").pipe(res)
|
|
40
50
|
return
|
package/test.html
CHANGED
|
@@ -61,6 +61,38 @@ async function runTest(testName, testFunction, expectedResult) {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
runTest(
|
|
65
|
+
"test deleting a resource",
|
|
66
|
+
async () => {
|
|
67
|
+
let key = 'test-' + Math.random().toString(36).slice(2)
|
|
68
|
+
|
|
69
|
+
let r0 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
|
|
70
|
+
|
|
71
|
+
await fetch(`/${key}`, {
|
|
72
|
+
method: 'PUT',
|
|
73
|
+
body: JSON.stringify({a: 5, b: 6}, null, 4)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
let r1 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
|
|
77
|
+
|
|
78
|
+
await fetch(`/${key}`, {
|
|
79
|
+
method: 'DELETE'
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
let r2 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
|
|
83
|
+
|
|
84
|
+
await fetch(`/${key}`, {
|
|
85
|
+
method: 'PUT',
|
|
86
|
+
body: JSON.stringify({a: 5, b: 7}, null, 4)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
let r3 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
|
|
90
|
+
|
|
91
|
+
return JSON.stringify([r0, r1, r2, r3])
|
|
92
|
+
},
|
|
93
|
+
JSON.stringify([false, true, false, true])
|
|
94
|
+
)
|
|
95
|
+
|
|
64
96
|
runTest(
|
|
65
97
|
"test getting a binary update from a subscription",
|
|
66
98
|
async () => {
|