braid-text 0.2.1 → 0.2.2

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 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 resource.delete_me()
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
@@ -599,9 +605,11 @@ braid_text.list = async () => {
599
605
  }
600
606
 
601
607
  async function get_resource(key) {
608
+ if (braid_text.delete_cache[key]) await braid_text.delete_cache[key]
609
+
602
610
  let cache = braid_text.cache
603
611
  if (!cache[key]) cache[key] = new Promise(async done => {
604
- let resource = {}
612
+ let resource = {key}
605
613
  resource.clients = new Set()
606
614
  resource.simpleton_clients = new Set()
607
615
 
@@ -625,9 +633,9 @@ async function get_resource(key) {
625
633
  resource.actor_seqs[v[0]] = Math.max(v[1], resource.actor_seqs[v[0]] ?? -1)
626
634
  }
627
635
 
628
- resource.delete_me = () => {
629
- delete_me()
636
+ resource.delete_me = async () => {
630
637
  delete cache[key]
638
+ await (braid_text.delete_cache[key] = delete_me())
631
639
  }
632
640
 
633
641
  resource.val = resource.doc.get()
@@ -745,9 +753,11 @@ async function file_sync(key, process_delta, get_init) {
745
753
  }
746
754
  }
747
755
 
756
+ let deleted = false
748
757
  let chain = Promise.resolve()
749
758
  return {
750
759
  change: async (bytes) => {
760
+ if (deleted) return
751
761
  await (chain = chain.then(async () => {
752
762
  currentSize += bytes.length + 4 // we account for the extra 4 bytes for uint32
753
763
  const filename = `${braid_text.db_folder}/${encoded}.${currentNumber}`
@@ -787,6 +797,7 @@ async function file_sync(key, process_delta, get_init) {
787
797
  }))
788
798
  },
789
799
  delete_me: async () => {
800
+ deleted = true
790
801
  await (chain = chain.then(async () => {
791
802
  await Promise.all(
792
803
  (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidjs",
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 == '/test.html') {
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 () => {