braid-text 0.2.30 → 0.2.32

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 CHANGED
@@ -105,7 +105,11 @@ http_server.on("request", (req, res) => {
105
105
  apply_remote_update: ({ state, patches }) => {
106
106
 
107
107
  // Apply the incoming state or patches to local text here.
108
-
108
+ //
109
+ // Example data:
110
+ // state: "Hello World" // The new text
111
+ // patches: [{ range: [5, 5], content: " World" }] // Patches that create the new text
112
+ //
109
113
  // Then return the new state of textarea as a string:
110
114
  return new_state
111
115
  },
@@ -114,8 +118,8 @@ http_server.on("request", (req, res) => {
114
118
  // Compute diff between prev_state ^ and the current textarea string, such as:
115
119
  //
116
120
  // var patches = [{
117
- // range: [5:5],
118
- // content: " World"
121
+ // range: [5, 5], // The range from position 5 to position 5
122
+ // content: " World" // is replaced with the string " World"
119
123
  // }]
120
124
  //
121
125
  // ...to insert something after a prev_state of "Hello".
package/index.js CHANGED
@@ -176,31 +176,11 @@ braid_text.serve = async (req, res, options = {}) => {
176
176
  } catch (e) {
177
177
  console.log(`${req.method} ERROR: ${e.stack}`)
178
178
  if (e.message?.startsWith(MISSING_PARENT_VERSION)) {
179
- // we couldn't apply the version, because we're missing its parents,
180
- // we want to send a 4XX error, so the client will resend this request later,
179
+ // we couldn't apply the version, because we're missing its parents;
180
+ // we want to send some kind of error that gives the client faith,
181
+ // that resending this request later may work,
181
182
  // hopefully after we've received the necessary parents.
182
-
183
- // here are some 4XX error code options..
184
- //
185
- // - 425 Too Early
186
- // - pros: our message is too early
187
- // - cons: associated with some "Early-Data" http thing, which we're not using
188
- // - 400 Bad Request
189
- // - pros: pretty generic
190
- // - cons: implies client shouldn't resend as-is
191
- // - 409 Conflict
192
- // - pros: doesn't imply modifications needed
193
- // - cons: the message is not conflicting with anything
194
- // - 412 Precondition Failed
195
- // - pros: kindof true.. the precondition of having another version has failed..
196
- // - cons: not strictly true, as this code is associated with http's If-Unmodified-Since stuff
197
- // - 422 Unprocessable Content
198
- // - pros: it's true
199
- // - 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)
200
- // - 428 Precondition Required
201
- // - pros: the name sounds right
202
- // - 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
203
- return done_my_turn(400, e.message, 'Missing Parents', { 'Retry-After': '1' })
183
+ return done_my_turn(309, e.message, 'Missing Parents', { 'Retry-After': '1' })
204
184
  } else {
205
185
  return done_my_turn(500, "The server failed to apply this version. The error generated was: " + e)
206
186
  }
@@ -931,23 +911,18 @@ function dt_get_patches(doc, version = null) {
931
911
  if (version && v_eq(version,
932
912
  doc.getRemoteVersion().map((x) => x.join("-")).sort())) {
933
913
  // they want everything past the end, which is nothing
934
- } else if (version) {
914
+ } else if (version?.length) {
935
915
  let frontier = {}
936
916
  version.forEach((x) => frontier[x] = true)
937
917
  let local_version = []
938
918
  for (let i = 0; i < versions.length; i++)
939
919
  if (frontier[versions[i].join("-")]) local_version.push(i)
940
- let after_bytes = doc.getPatchSince(new Uint32Array(local_version))
941
-
942
- ;[_agents, versions, parentss] = dt_parse([...after_bytes])
943
920
 
944
- let before_doc = dt_get(doc, version)
945
- let before_doc_frontier = before_doc.getLocalVersion()
921
+ local_version = new Uint32Array(local_version)
946
922
 
947
- before_doc.mergeBytes(after_bytes)
948
- op_runs = before_doc.getOpsSince(before_doc_frontier)
949
-
950
- before_doc.free()
923
+ let after_bytes = doc.getPatchSince(local_version)
924
+ ;[_agents, versions, parentss] = dt_parse([...after_bytes])
925
+ op_runs = doc.getOpsSince(local_version)
951
926
  } else op_runs = doc.getOpsSince([])
952
927
 
953
928
  doc.free()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.30",
3
+ "version": "0.2.32",
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
@@ -61,6 +61,21 @@ async function runTest(testName, testFunction, expectedResult) {
61
61
  }
62
62
  }
63
63
 
64
+ runTest(
65
+ "test error code when missing parents",
66
+ async () => {
67
+ let key = 'test-' + Math.random().toString(36).slice(2)
68
+ let r = await braid_fetch(`/${key}`, {
69
+ method: 'PUT',
70
+ version: ['hi-1'],
71
+ parents: ['missing-0'],
72
+ body: 'xx'
73
+ })
74
+ return r.status + ' ' + r.ok
75
+ },
76
+ '309 false'
77
+ )
78
+
64
79
  runTest(
65
80
  "test subscribing starting at a version using simpleton",
66
81
  async () => {