braid-text 0.1.1 → 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 +41 -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',
@@ -164,32 +166,36 @@ braid_text.serve = async (req, res, options = {}) => {
164
166
 
165
167
  options.put_cb(options.key, resource.val)
166
168
  } catch (e) {
167
- console.log(`EEE= ${e}:${e.stack}`)
168
- // we couldn't apply the version, possibly because we're missing its parents,
169
- // we want to send a 4XX error, so the client will resend this request later,
170
- // hopefully after we've received the necessary parents.
171
-
172
- // here are some 4XX error code options..
173
- //
174
- // - 425 Too Early
175
- // - pros: our message is too early
176
- // - cons: associated with some "Early-Data" http thing, which we're not using
177
- // - 400 Bad Request
178
- // - pros: pretty generic
179
- // - cons: implies client shouldn't resend as-is
180
- // - 409 Conflict
181
- // - pros: doesn't imply modifications needed
182
- // - cons: the message is not conflicting with anything
183
- // - 412 Precondition Failed
184
- // - pros: kindof true.. the precondition of having another version has failed..
185
- // - cons: not strictly true, as this code is associated with http's If-Unmodified-Since stuff
186
- // - 422 Unprocessable Content
187
- // - pros: it's true
188
- // - 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)
189
- // - 428 Precondition Required
190
- // - pros: the name sounds right
191
- // - 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
192
- 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
+ }
193
199
  }
194
200
 
195
201
  return done_my_turn(200)
@@ -303,6 +309,14 @@ braid_text.put = async (key, options) => {
303
309
 
304
310
  let resource = (typeof key == 'string') ? await get_resource(key) : key
305
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
+
306
320
  let parents = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
307
321
  let og_parents = options_parents || parents
308
322
 
@@ -577,7 +591,7 @@ async function get_resource(key) {
577
591
  resource.need_defrag = false
578
592
 
579
593
  resource.actor_seqs = {}
580
- let max_version = resource.doc.getLocalVersion()[0] ?? -1
594
+ let max_version = Math.max(...resource.doc.getLocalVersion()) ?? -1
581
595
  for (let i = 0; i <= max_version; i++) {
582
596
  let v = resource.doc.localToRemoteVersion([i])[0]
583
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.1",
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
  }