braid-text 0.2.67 → 0.2.68

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/index.js +25 -4
  2. package/package.json +2 -2
  3. package/test/test.html +270 -100
package/index.js CHANGED
@@ -16,6 +16,15 @@ function create_braid_text() {
16
16
 
17
17
  let max_encoded_key_size = 240
18
18
 
19
+ braid_text.sync = async (a, b, options) => {
20
+ braid_text.get(a, {
21
+ subscribe: update => braid_text.put(b, update)
22
+ })
23
+ braid_text.get(b, {
24
+ subscribe: update => braid_text.put(a, update)
25
+ })
26
+ }
27
+
19
28
  braid_text.serve = async (req, res, options = {}) => {
20
29
  options = {
21
30
  key: req.url.split('?')[0], // Default key
@@ -237,7 +246,7 @@ function create_braid_text() {
237
246
  req.parents,
238
247
  resource.actor_seqs,
239
248
  // approximation of memory usage for this update
240
- body ? body.length :
249
+ body != null ? body.length :
241
250
  patches.reduce((a, b) => a + b.range.length + b.content.length, 0),
242
251
  options.recv_buffer_max_time,
243
252
  options.recv_buffer_max_space)
@@ -270,7 +279,8 @@ function create_braid_text() {
270
279
  return done_my_turn(550, "repr-digest mismatch!")
271
280
  }
272
281
 
273
- if (req.version) got_event(options.key, req.version[0], change_count)
282
+ if (req.version?.length)
283
+ got_event(options.key, req.version[0], change_count)
274
284
 
275
285
  res.setHeader("Version", get_current_version())
276
286
 
@@ -308,7 +318,12 @@ function create_braid_text() {
308
318
  var res = await braid_fetch(key.href, params)
309
319
 
310
320
  if (options.subscribe) {
311
- res.subscribe(options.subscribe)
321
+ res.subscribe(update => {
322
+ update.body = update.body_text
323
+ if (update.patches)
324
+ for (var p of update.patches) p.content = p.content_text
325
+ options.subscribe(update)
326
+ })
312
327
  return res
313
328
  } else return await res.text()
314
329
  }
@@ -452,7 +467,7 @@ function create_braid_text() {
452
467
  signal: options.my_abort.signal,
453
468
  retry: () => true,
454
469
  }
455
- for (var x of ['headers', 'parents', 'version', 'peer', 'body'])
470
+ for (var x of ['headers', 'parents', 'version', 'peer', 'body', 'patches'])
456
471
  if (options[x] != null) params[x] = options[x]
457
472
 
458
473
  return await braid_fetch(key.href, params)
@@ -494,6 +509,12 @@ function create_braid_text() {
494
509
  }
495
510
 
496
511
  if (version) validate_version_array(version)
512
+ if (version && !version.length) {
513
+ console.log(`warning: ignoring put with empty version`)
514
+ return { change_count: 0 }
515
+ }
516
+ if (version && version.length > 1)
517
+ throw new Error(`cannot put a version with multiple ids`)
497
518
 
498
519
  // translate a single parent of "root" to the empty array (same meaning)
499
520
  let options_parents = options.parents
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.67",
3
+ "version": "0.2.68",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
7
7
  "homepage": "https://braid.org",
8
8
  "dependencies": {
9
9
  "@braid.org/diamond-types-node": "^2.0.0",
10
- "braid-http": "~1.3.80"
10
+ "braid-http": "~1.3.83"
11
11
  }
12
12
  }
package/test/test.html CHANGED
@@ -96,105 +96,275 @@ async function runTest(testName, testFunction, expectedResult) {
96
96
  }
97
97
  }
98
98
 
99
- // runTest(
100
- // "test braid_text.get(url), with no options",
101
- // async () => {
102
- // var key = 'test-' + Math.random().toString(36).slice(2)
103
- // let r = await braid_fetch(`/${key}`, {
104
- // method: 'PUT',
105
- // body: 'hi'
106
- // })
107
- // if (!r.ok) return 'got: ' + r.status
108
-
109
- // var r1 = await braid_fetch(`/eval`, {
110
- // method: 'PUT',
111
- // body: `void (async () => {
112
- // res.end(await braid_text.get(new URL('http://localhost:8889/${key}')))
113
- // })()`
114
- // })
115
-
116
- // return 'got: ' + (await r1.text())
117
- // },
118
- // 'got: hi'
119
- // )
120
-
121
- // runTest(
122
- // "test braid_text.get(url), with headers",
123
- // async () => {
124
- // var key = 'test-' + Math.random().toString(36).slice(2)
125
- // let r = await braid_fetch(`/${key}`, {
126
- // method: 'PUT',
127
- // version: ['xyz-1'],
128
- // body: 'hi'
129
- // })
130
- // if (!r.ok) return 'got: ' + r.status
131
-
132
- // var r1 = await braid_fetch(`/eval`, {
133
- // method: 'PUT',
134
- // body: `void (async () => {
135
- // res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
136
- // headers: {
137
- // version: '"xyz-0"'
138
- // }
139
- // }))
140
- // })()`
141
- // })
142
-
143
- // return 'got: ' + (await r1.text())
144
- // },
145
- // 'got: h'
146
- // )
147
-
148
- // runTest(
149
- // "test braid_text.get(url), with parents",
150
- // async () => {
151
- // var key = 'test-' + Math.random().toString(36).slice(2)
152
- // let r = await braid_fetch(`/${key}`, {
153
- // method: 'PUT',
154
- // version: ['xyz-1'],
155
- // body: 'hi'
156
- // })
157
- // if (!r.ok) return 'got: ' + r.status
158
-
159
- // var r1 = await braid_fetch(`/eval`, {
160
- // method: 'PUT',
161
- // body: `void (async () => {
162
- // res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
163
- // parents: ['xyz-0']
164
- // }))
165
- // })()`
166
- // })
167
-
168
- // return 'got: ' + (await r1.text())
169
- // },
170
- // 'got: h'
171
- // )
172
-
173
- // runTest(
174
- // "test braid_text.get(url), with version and peer",
175
- // async () => {
176
- // var key = 'test-' + Math.random().toString(36).slice(2)
177
- // let r = await braid_fetch(`/${key}`, {
178
- // method: 'PUT',
179
- // version: ['xyz-1'],
180
- // body: 'hi'
181
- // })
182
- // if (!r.ok) return 'got: ' + r.status
183
-
184
- // var r1 = await braid_fetch(`/eval`, {
185
- // method: 'PUT',
186
- // body: `void (async () => {
187
- // res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
188
- // version: ['xyz-0'],
189
- // peer: 'xyz'
190
- // }))
191
- // })()`
192
- // })
193
-
194
- // return 'got: ' + (await r1.text())
195
- // },
196
- // 'got: h'
197
- // )
99
+ runTest(
100
+ "test subscribe update with body_text",
101
+ async () => {
102
+ var key = 'test' + Math.random().toString(36).slice(2)
103
+
104
+ var r = await braid_fetch(`/${key}`, {
105
+ method: 'PUT',
106
+ body: 'hi'
107
+ })
108
+ if (!r.ok) return 'got: ' + r.status
109
+
110
+ var r1p = braid_fetch(`/eval`, {
111
+ method: 'PUT',
112
+ body: `void (async () => {
113
+ var x = await new Promise(done => {
114
+ braid_text.get(new URL('http://localhost:8889/${key}'), {
115
+ subscribe: update => {
116
+ if (update.body_text) done(update.body_text)
117
+ }
118
+ })
119
+ })
120
+ res.end(x)
121
+ })()`
122
+ })
123
+
124
+ var r2 = await braid_fetch(`/${key}`, {
125
+ method: 'PUT',
126
+ body: 'yo'
127
+ })
128
+
129
+ var r1 = await r1p
130
+ if (!r1.ok) return 'got: ' + r.status
131
+
132
+ return await r1.text()
133
+ },
134
+ 'yo'
135
+ )
136
+
137
+ runTest(
138
+ "test braid_text.sync, url to key",
139
+ async () => {
140
+ var key_a = 'test-a-' + Math.random().toString(36).slice(2)
141
+ var key_b = 'test-b-' + Math.random().toString(36).slice(2)
142
+
143
+ var r = await braid_fetch(`/${key_a}`, {
144
+ method: 'PUT',
145
+ body: 'hi'
146
+ })
147
+ if (!r.ok) return 'got: ' + r.status
148
+
149
+ var r = await braid_fetch(`/eval`, {
150
+ method: 'PUT',
151
+ body: `void (async () => {
152
+ braid_text.sync(new URL('http://localhost:8889/${key_a}'), '/${key_b}')
153
+ res.end('')
154
+ })()`
155
+ })
156
+ if (!r.ok) return 'got: ' + r.status
157
+
158
+ await new Promise(done => setTimeout(done, 100))
159
+
160
+ var r = await braid_fetch(`/${key_b}`)
161
+ return 'got: ' + (await r.text())
162
+ },
163
+ 'got: hi'
164
+ )
165
+
166
+ runTest(
167
+ "test braid_text.sync, with two urls",
168
+ async () => {
169
+ var key_a = 'test-a-' + Math.random().toString(36).slice(2)
170
+ var key_b = 'test-b-' + Math.random().toString(36).slice(2)
171
+
172
+ var r = await braid_fetch(`/${key_a}`, {
173
+ method: 'PUT',
174
+ body: 'hi'
175
+ })
176
+ if (!r.ok) return 'got: ' + r.status
177
+
178
+ var r = await braid_fetch(`/eval`, {
179
+ method: 'PUT',
180
+ body: `void (async () => {
181
+ braid_text.sync(new URL('http://localhost:8889/${key_a}'),
182
+ new URL('http://localhost:8889/${key_b}'))
183
+ res.end('')
184
+ })()`
185
+ })
186
+ if (!r.ok) return 'got: ' + r.status
187
+
188
+ await new Promise(done => setTimeout(done, 100))
189
+
190
+ var r = await braid_fetch(`/${key_b}`)
191
+ return 'got: ' + (await r.text())
192
+ },
193
+ 'got: hi'
194
+ )
195
+
196
+ runTest(
197
+ "test braid_text.sync, key to url",
198
+ async () => {
199
+ var key_a = 'test-a-' + Math.random().toString(36).slice(2)
200
+ var key_b = 'test-b-' + Math.random().toString(36).slice(2)
201
+
202
+ var r = await braid_fetch(`/${key_a}`, {
203
+ method: 'PUT',
204
+ body: 'hi'
205
+ })
206
+ if (!r.ok) return 'got: ' + r.status
207
+
208
+ var r = await braid_fetch(`/eval`, {
209
+ method: 'PUT',
210
+ body: `void (async () => {
211
+ braid_text.sync('/${key_a}', new URL('http://localhost:8889/${key_b}'))
212
+ res.end('')
213
+ })()`
214
+ })
215
+ if (!r.ok) return 'got: ' + r.status
216
+
217
+ await new Promise(done => setTimeout(done, 100))
218
+
219
+ var r = await braid_fetch(`/${key_b}`)
220
+ return 'got: ' + (await r.text())
221
+ },
222
+ 'got: hi'
223
+ )
224
+
225
+ runTest(
226
+ "test braid_text.sync, with two keys",
227
+ async () => {
228
+ var key_a = 'test-a-' + Math.random().toString(36).slice(2)
229
+ var key_b = 'test-b-' + Math.random().toString(36).slice(2)
230
+
231
+ var r = await braid_fetch(`/${key_a}`, {
232
+ method: 'PUT',
233
+ body: 'hi'
234
+ })
235
+ if (!r.ok) return 'got: ' + r.status
236
+
237
+ var r = await braid_fetch(`/eval`, {
238
+ method: 'PUT',
239
+ body: `void (async () => {
240
+ braid_text.sync('/${key_a}', '/${key_b}')
241
+ res.end('')
242
+ })()`
243
+ })
244
+ if (!r.ok) return 'got: ' + r.status
245
+
246
+ await new Promise(done => setTimeout(done, 100))
247
+
248
+ var r = await braid_fetch(`/${key_b}`)
249
+ return 'got: ' + (await r.text())
250
+ },
251
+ 'got: hi'
252
+ )
253
+
254
+ runTest(
255
+ "test putting version with multiple event ids, should have error",
256
+ async () => {
257
+ var key_a = 'test-a-' + Math.random().toString(36).slice(2)
258
+
259
+ var r = await braid_fetch(`/${key_a}`, {
260
+ method: 'PUT',
261
+ version: ['abc-1', 'xyz-2'],
262
+ body: 'hi'
263
+ })
264
+ return '' + (await r.text()).includes('cannot put a version with multiple ids')
265
+ },
266
+ 'true'
267
+ )
268
+
269
+ runTest(
270
+ "test braid_text.get(url), with no options",
271
+ async () => {
272
+ var key = 'test-' + Math.random().toString(36).slice(2)
273
+ let r = await braid_fetch(`/${key}`, {
274
+ method: 'PUT',
275
+ body: 'hi'
276
+ })
277
+ if (!r.ok) return 'got: ' + r.status
278
+
279
+ var r1 = await braid_fetch(`/eval`, {
280
+ method: 'PUT',
281
+ body: `void (async () => {
282
+ res.end(await braid_text.get(new URL('http://localhost:8889/${key}')))
283
+ })()`
284
+ })
285
+
286
+ return 'got: ' + (await r1.text())
287
+ },
288
+ 'got: hi'
289
+ )
290
+
291
+ runTest(
292
+ "test braid_text.get(url), with headers",
293
+ async () => {
294
+ var key = 'test-' + Math.random().toString(36).slice(2)
295
+ let r = await braid_fetch(`/${key}`, {
296
+ method: 'PUT',
297
+ version: ['xyz-1'],
298
+ body: 'hi'
299
+ })
300
+ if (!r.ok) return 'got: ' + r.status
301
+
302
+ var r1 = await braid_fetch(`/eval`, {
303
+ method: 'PUT',
304
+ body: `void (async () => {
305
+ res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
306
+ headers: {
307
+ version: '"xyz-0"'
308
+ }
309
+ }))
310
+ })()`
311
+ })
312
+
313
+ return 'got: ' + (await r1.text())
314
+ },
315
+ 'got: h'
316
+ )
317
+
318
+ runTest(
319
+ "test braid_text.get(url), with parents",
320
+ async () => {
321
+ var key = 'test-' + Math.random().toString(36).slice(2)
322
+ let r = await braid_fetch(`/${key}`, {
323
+ method: 'PUT',
324
+ version: ['xyz-1'],
325
+ body: 'hi'
326
+ })
327
+ if (!r.ok) return 'got: ' + r.status
328
+
329
+ var r1 = await braid_fetch(`/eval`, {
330
+ method: 'PUT',
331
+ body: `void (async () => {
332
+ res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
333
+ parents: ['xyz-0']
334
+ }))
335
+ })()`
336
+ })
337
+
338
+ return 'got: ' + (await r1.text())
339
+ },
340
+ 'got: h'
341
+ )
342
+
343
+ runTest(
344
+ "test braid_text.get(url), with version and peer",
345
+ async () => {
346
+ var key = 'test-' + Math.random().toString(36).slice(2)
347
+ let r = await braid_fetch(`/${key}`, {
348
+ method: 'PUT',
349
+ version: ['xyz-1'],
350
+ body: 'hi'
351
+ })
352
+ if (!r.ok) return 'got: ' + r.status
353
+
354
+ var r1 = await braid_fetch(`/eval`, {
355
+ method: 'PUT',
356
+ body: `void (async () => {
357
+ res.end(await braid_text.get(new URL('http://localhost:8889/${key}'), {
358
+ version: ['xyz-0'],
359
+ peer: 'xyz'
360
+ }))
361
+ })()`
362
+ })
363
+
364
+ return 'got: ' + (await r1.text())
365
+ },
366
+ 'got: h'
367
+ )
198
368
 
199
369
  runTest(
200
370
  "test braid_text.get(url) with subscription",
@@ -220,7 +390,7 @@ runTest(
220
390
  }
221
391
  braid_text.get(url, o)
222
392
  })
223
- res.end(update.body_text)
393
+ res.end(update.body)
224
394
  })()`
225
395
  })
226
396