braid-text 0.2.24 → 0.2.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 CHANGED
@@ -76,6 +76,16 @@ braid_text.serve = async (req, res, options = {}) => {
76
76
  if (!req.subscribe) {
77
77
  res.setHeader("Accept-Subscribe", "true")
78
78
 
79
+ // special case for HEAD asking for version/parents,
80
+ // to be faster by not reconstructing body
81
+ if (req.method === "HEAD" && (req.version || req.parents)) {
82
+ if ((req.version || req.parents).every(event => {
83
+ var [actor, seq] = decode_version(event)
84
+ return resource.actor_seqs[actor]?.has(seq)
85
+ })) return my_end(200)
86
+ else return my_end(500, "Unknown Version")
87
+ }
88
+
79
89
  let x = null
80
90
  try {
81
91
  x = await braid_text.get(resource, { version: req.version, parents: req.parents })
@@ -478,6 +488,8 @@ braid_text.put = async (key, options) => {
478
488
 
479
489
  resource.need_defrag = true
480
490
 
491
+ await resource.db_delta(resource.doc.getPatchSince(v_before))
492
+
481
493
  if (options.merge_type != "dt") {
482
494
  patches = get_xf_patches(resource.doc, v_before)
483
495
  if (braid_text.verbose) console.log(JSON.stringify({ patches }))
@@ -578,8 +590,6 @@ braid_text.put = async (key, options) => {
578
590
  for (let client of resource.clients) {
579
591
  if (!peer || client.peer !== peer) client.subscribe(x)
580
592
  }
581
-
582
- await resource.db_delta(resource.doc.getPatchSince(v_before))
583
593
  }
584
594
 
585
595
  braid_text.list = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.24",
3
+ "version": "0.2.26",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
package/test/test.html CHANGED
@@ -266,4 +266,34 @@ runTest(
266
266
  'retried!'
267
267
  )
268
268
 
269
+ runTest(
270
+ "test asking for a version that should and shouldn't be there",
271
+ async () => {
272
+ var key = 'test-' + Math.random().toString(36).slice(2)
273
+
274
+ var r = await braid_fetch(`/${key}`, {
275
+ method: 'PUT',
276
+ version: ['hi-10'],
277
+ parents: [],
278
+ body: 'x'
279
+ })
280
+ if (!r.ok) throw 'got: ' + r.statusCode
281
+
282
+ var r = await braid_fetch(`/${key}`, {
283
+ method: 'HEAD',
284
+ version: ['hi-5']
285
+ })
286
+ if (r.ok) throw 'found version we should not have found'
287
+
288
+ var r = await braid_fetch(`/${key}`, {
289
+ method: 'HEAD',
290
+ version: ['hi-10']
291
+ })
292
+ if (!r.ok) throw 'could not find version we should have found'
293
+
294
+ return 'worked out!'
295
+ },
296
+ 'worked out!'
297
+ )
298
+
269
299
  </script>
package/test/server.js DELETED
@@ -1,50 +0,0 @@
1
-
2
- var port = process.argv[2] || 8889
3
-
4
- var braid_text = require("../index.js")
5
- braid_text.db_folder = null
6
-
7
- var server = require("http").createServer(async (req, res) => {
8
- console.log(`${req.method} ${req.url}`)
9
-
10
- // Free the CORS
11
- braid_text.free_cors(res)
12
- if (req.method === 'OPTIONS') return
13
-
14
- if (req.url.startsWith('/test.html')) {
15
- let parts = req.url.split(/[\?&=]/g)
16
-
17
- if (parts[1] === 'check') {
18
- res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
19
- return res.end(JSON.stringify({
20
- checking: parts[2],
21
- result: (await braid_text.get(parts[2])) != null
22
- }))
23
- } else if (parts[1] === 'dt_create_bytes_big_name') {
24
- try {
25
- braid_text.dt_create_bytes('x'.repeat(1000000) + '-0', [], 0, 0, 'hi')
26
- return res.end(JSON.stringify({ ok: true }))
27
- } catch (e) {
28
- return res.end(JSON.stringify({ ok: false, error: '' + e }))
29
- }
30
- } else if (parts[1] === 'dt_create_bytes_many_names') {
31
- try {
32
- braid_text.dt_create_bytes('hi-0', new Array(1000000).fill(0).map((x, i) => `x${i}-0`), 0, 0, 'hi')
33
- return res.end(JSON.stringify({ ok: true }))
34
- } catch (e) {
35
- return res.end(JSON.stringify({ ok: false, error: '' + e }))
36
- }
37
- }
38
-
39
- res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
40
- require("fs").createReadStream(`${__dirname}/test.html`).pipe(res)
41
- return
42
- }
43
-
44
- // Now serve the collaborative text!
45
- braid_text.serve(req, res)
46
- })
47
-
48
- server.listen(port, () => {
49
- console.log(`serving: http://localhost:${port}/test.html`)
50
- })