braid-text 0.3.20 → 0.3.21

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.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/package.json +1 -1
  3. package/server.js +10 -4
package/README.md CHANGED
@@ -69,6 +69,11 @@ http_server.on("request", (req, res) => {
69
69
  - `res`: Outgoing HTTP response object.
70
70
  - `options`: <small style="color:lightgrey">[optional]</small> An object containing additional options:
71
71
  - `key`: <small style="color:lightgrey">[optional]</small> ID of text resource to sync with. Defaults to `req.url`.
72
+ - `put_cb`: <small style="color:lightgrey">[optional]</small> Callback invoked after a PUT changes a resource. Signature: `(key, val, old_val, patches)` where:
73
+ - `key` - The resource key
74
+ - `val` - The new document text after the PUT
75
+ - `old_val` - The document text before the PUT
76
+ - `patches` - Array of patches applied (each `{unit, range, content}`), or `null` for full-body replacements
72
77
  - This is the main method of this library, and does all the work to handle Braid-HTTP `GET` and `PUT` requests concerned with a specific text resource.
73
78
 
74
79
  `await braid_text.get(key)`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.3.20",
3
+ "version": "0.3.21",
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/server.js CHANGED
@@ -345,11 +345,11 @@ function create_braid_text() {
345
345
  braid_text.serve = async (req, res, options = {}) => {
346
346
  options = {
347
347
  key: req.url.split('?')[0], // Default key
348
- put_cb: (key, val) => { }, // Default callback when a PUT changes a key
348
+ put_cb: (key, val, old_val, patches) => { }, // Default callback when a PUT changes a key
349
349
  ...options // Override with all options passed in
350
350
  }
351
351
 
352
- braid_text.free_cors(res)
352
+ if (braid_text.cors !== false) braid_text.free_cors(res)
353
353
 
354
354
  function my_end(statusCode, x, statusText, headers) {
355
355
  res.writeHead(statusCode, statusText, headers)
@@ -555,6 +555,8 @@ function create_braid_text() {
555
555
  })
556
556
  }
557
557
 
558
+ var old_val = resource.val
559
+ var put_patches = patches ? patches.map(p => ({unit: p.unit, range: p.range, content: p.content})) : null
558
560
  var {change_count} = await braid_text.put(resource, { peer, version: req.version, parents: req.parents, patches, body, merge_type })
559
561
 
560
562
  // if Repr-Digest is set,
@@ -575,7 +577,7 @@ function create_braid_text() {
575
577
 
576
578
  res.setHeader("Version", get_current_version())
577
579
 
578
- options.put_cb(options.key, resource.val)
580
+ options.put_cb(options.key, resource.val, old_val, put_patches)
579
581
  } catch (e) {
580
582
  console.log(`${req.method} ERROR: ${e.stack}`)
581
583
  return done_my_turn(500, "The server failed to apply this version. The error generated was: " + e)
@@ -653,7 +655,7 @@ function create_braid_text() {
653
655
 
654
656
  if (!options) {
655
657
  // if it doesn't exist already, don't create it in this case
656
- if (!braid_text.cache[key]) return
658
+ if (!braid_text.cache[key]) return null
657
659
  return (await get_resource(key)).val
658
660
  }
659
661
 
@@ -897,6 +899,10 @@ function create_braid_text() {
897
899
 
898
900
  let change_count = patches.reduce((a, b) => a + b.content_codepoints.length + (b.range[1] - b.range[0]), 0)
899
901
 
902
+ // Nothing to do: e.g. PUT with empty body on an already-empty doc,
903
+ // or patches that delete and insert zero characters.
904
+ if (change_count === 0) return { change_count }
905
+
900
906
  version = version?.[0] || `${(is_valid_actor(peer) && peer) || Math.random().toString(36).slice(2, 7)}-${change_count - 1}`
901
907
 
902
908
  let v = decode_version(version)