braid-text 0.3.20 → 0.3.22
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 -0
- package/client/simpleton-sync.js +5 -2
- package/package.json +1 -1
- 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/client/simpleton-sync.js
CHANGED
|
@@ -104,7 +104,8 @@ function simpleton_client(url, {
|
|
|
104
104
|
get_patches,
|
|
105
105
|
get_state,
|
|
106
106
|
content_type,
|
|
107
|
-
|
|
107
|
+
headers: user_headers, // The user can pass in custom headers
|
|
108
|
+
// that are forwared into the fetch
|
|
108
109
|
on_error,
|
|
109
110
|
on_res,
|
|
110
111
|
on_online,
|
|
@@ -141,7 +142,8 @@ function simpleton_client(url, {
|
|
|
141
142
|
retry: () => true,
|
|
142
143
|
parents: () => client_version.length ? client_version : null,
|
|
143
144
|
onSubscriptionStatus: status => on_online && on_online(status.online),
|
|
144
|
-
headers: {
|
|
145
|
+
headers: { ...user_headers,
|
|
146
|
+
"Merge-Type": "simpleton",
|
|
145
147
|
...content_type && {Accept: content_type} },
|
|
146
148
|
}).then(res => {
|
|
147
149
|
if (on_res) on_res(res)
|
|
@@ -328,6 +330,7 @@ function simpleton_client(url, {
|
|
|
328
330
|
peer, version, parents, patches,
|
|
329
331
|
retry: (res) => res.status !== 550,
|
|
330
332
|
headers: {
|
|
333
|
+
...user_headers,
|
|
331
334
|
"Merge-Type": "simpleton",
|
|
332
335
|
...send_digests && {
|
|
333
336
|
"Repr-Digest": await get_digest(client_state) },
|
package/package.json
CHANGED
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)
|