braid-text 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/index.js +43 -27
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -3,6 +3,8 @@ let { Doc } = require("diamond-types-node")
3
3
  let braidify = require("braid-http").http_server
4
4
  let fs = require("fs")
5
5
 
6
+ let MISSING_PARENT_VERSION = 'missing parent version'
7
+
6
8
  let braid_text = {
7
9
  verbose: false,
8
10
  db_folder: './braid-text-db',
@@ -159,35 +161,41 @@ braid_text.serve = async (req, res, options = {}) => {
159
161
  }
160
162
 
161
163
  await braid_text.put(resource, { peer, version: req.version, parents: req.parents, patches, body, merge_type })
164
+
165
+ res.setHeader("Version", resource.doc.getRemoteVersion().map((x) => x.join("-")).sort())
162
166
 
163
167
  options.put_cb(options.key, resource.val)
164
168
  } catch (e) {
165
- console.log(`EEE= ${e}:${e.stack}`)
166
- // we couldn't apply the version, possibly because we're missing its parents,
167
- // we want to send a 4XX error, so the client will resend this request later,
168
- // hopefully after we've received the necessary parents.
169
-
170
- // here are some 4XX error code options..
171
- //
172
- // - 425 Too Early
173
- // - pros: our message is too early
174
- // - cons: associated with some "Early-Data" http thing, which we're not using
175
- // - 400 Bad Request
176
- // - pros: pretty generic
177
- // - cons: implies client shouldn't resend as-is
178
- // - 409 Conflict
179
- // - pros: doesn't imply modifications needed
180
- // - cons: the message is not conflicting with anything
181
- // - 412 Precondition Failed
182
- // - pros: kindof true.. the precondition of having another version has failed..
183
- // - cons: not strictly true, as this code is associated with http's If-Unmodified-Since stuff
184
- // - 422 Unprocessable Content
185
- // - pros: it's true
186
- // - cons: implies client shouldn't resend as-is (at least, it says that here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422)
187
- // - 428 Precondition Required
188
- // - pros: the name sounds right
189
- // - cons: typically implies that the request was missing an http conditional field like If-Match. that is to say, it implies that the request is missing a precondition, not that the server is missing a precondition
190
- return done_my_turn(425, "The server failed to apply this version. The error generated was: " + e)
169
+ console.log(`${req.method} ERROR: ${e.stack}`)
170
+ if (e.message?.startsWith(MISSING_PARENT_VERSION)) {
171
+ // we couldn't apply the version, because we're missing its parents,
172
+ // we want to send a 4XX error, so the client will resend this request later,
173
+ // hopefully after we've received the necessary parents.
174
+
175
+ // here are some 4XX error code options..
176
+ //
177
+ // - 425 Too Early
178
+ // - pros: our message is too early
179
+ // - cons: associated with some "Early-Data" http thing, which we're not using
180
+ // - 400 Bad Request
181
+ // - pros: pretty generic
182
+ // - cons: implies client shouldn't resend as-is
183
+ // - 409 Conflict
184
+ // - pros: doesn't imply modifications needed
185
+ // - cons: the message is not conflicting with anything
186
+ // - 412 Precondition Failed
187
+ // - pros: kindof true.. the precondition of having another version has failed..
188
+ // - cons: not strictly true, as this code is associated with http's If-Unmodified-Since stuff
189
+ // - 422 Unprocessable Content
190
+ // - pros: it's true
191
+ // - cons: implies client shouldn't resend as-is (at least, it says that here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422)
192
+ // - 428 Precondition Required
193
+ // - pros: the name sounds right
194
+ // - cons: typically implies that the request was missing an http conditional field like If-Match. that is to say, it implies that the request is missing a precondition, not that the server is missing a precondition
195
+ return done_my_turn(425, e.message)
196
+ } else {
197
+ return done_my_turn(400, "The server failed to apply this version. The error generated was: " + e)
198
+ }
191
199
  }
192
200
 
193
201
  return done_my_turn(200)
@@ -301,6 +309,14 @@ braid_text.put = async (key, options) => {
301
309
 
302
310
  let resource = (typeof key == 'string') ? await get_resource(key) : key
303
311
 
312
+ if (options_parents) {
313
+ // make sure we have all these parents
314
+ for (let p of options_parents) {
315
+ let P = decode_version(p)
316
+ if (P[1] > (resource.actor_seqs[P[0]] ?? -1)) throw new Error(`${MISSING_PARENT_VERSION}: ${p}`)
317
+ }
318
+ }
319
+
304
320
  let parents = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
305
321
  let og_parents = options_parents || parents
306
322
 
@@ -575,7 +591,7 @@ async function get_resource(key) {
575
591
  resource.need_defrag = false
576
592
 
577
593
  resource.actor_seqs = {}
578
- let max_version = resource.doc.getLocalVersion()[0] ?? -1
594
+ let max_version = Math.max(...resource.doc.getLocalVersion()) ?? -1
579
595
  for (let i = 0; i <= max_version; i++) {
580
596
  let v = resource.doc.localToRemoteVersion([i])[0]
581
597
  resource.actor_seqs[v[0]] = Math.max(v[1], resource.actor_seqs[v[0]] ?? -1)
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidjs",
7
7
  "homepage": "https://braid.org",
8
8
  "dependencies": {
9
9
  "diamond-types-node": "^1.0.2",
10
- "braid-http": "^0.3.18"
10
+ "braid-http": "^1.0.7"
11
11
  }
12
12
  }