braid-text 0.2.33 → 0.2.34

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 +44 -25
  2. package/package.json +1 -1
  3. package/test/test.html +43 -0
package/index.js CHANGED
@@ -112,6 +112,7 @@ braid_text.serve = async (req, res, options = {}) => {
112
112
  version: req.version,
113
113
  parents: req.parents,
114
114
  merge_type,
115
+ transfer_encoding: req.headers['accept-transfer-encoding'],
115
116
  subscribe: x => res.sendVersion(x),
116
117
  write: (x) => res.write(x)
117
118
  }
@@ -244,39 +245,57 @@ braid_text.get = async (key, options) => {
244
245
  options.my_last_sent_version = x.version
245
246
  resource.simpleton_clients.add(options)
246
247
  } else {
247
- let updates = null
248
-
249
248
  if (resource.need_defrag) {
250
249
  if (braid_text.verbose) console.log(`doing defrag..`)
251
250
  resource.need_defrag = false
252
251
  resource.doc = defrag_dt(resource.doc)
253
252
  }
254
253
 
255
- if (!options.parents && !options.version) {
256
- options.subscribe({
257
- version: [],
258
- parents: [],
259
- body: "",
260
- })
261
-
262
- updates = dt_get_patches(resource.doc)
254
+ if (options.transfer_encoding === 'dt') {
255
+ var o = {
256
+ 'Transfer-Encoding': 'dt',
257
+ 'Current-Version': resource.doc.getRemoteVersion().
258
+ map(x => x.join("-")).
259
+ map(JSON.stringify).map(ascii_ify).join(", "),
260
+ }
261
+ var bytes = resource.doc.toBytes()
262
+ if (!options.parents && !options.version) o.body = bytes
263
+ else {
264
+ var doc = Doc.fromBytes(bytes)
265
+ o.body = doc.getPatchSince(
266
+ dt_get_local_version(bytes,
267
+ options.parents || options.version))
268
+ doc.free()
269
+ }
270
+ options.subscribe(o)
263
271
  } else {
264
- // Then start the subscription from the parents in options
265
- updates = dt_get_patches(resource.doc, options.parents || options.version)
266
- }
267
-
268
- for (let u of updates)
269
- options.subscribe({
270
- version: [u.version],
271
- parents: u.parents,
272
- patches: [{ unit: u.unit, range: u.range, content: u.content }],
273
- })
272
+ var updates = null
273
+ if (!options.parents && !options.version) {
274
+ options.subscribe({
275
+ version: [],
276
+ parents: [],
277
+ body: "",
278
+ })
279
+
280
+ updates = dt_get_patches(resource.doc)
281
+ } else {
282
+ // Then start the subscription from the parents in options
283
+ updates = dt_get_patches(resource.doc, options.parents || options.version)
284
+ }
274
285
 
275
- // Output at least *some* data, or else chrome gets confused and
276
- // thinks the connection failed. This isn't strictly necessary,
277
- // but it makes fewer scary errors get printed out in the JS
278
- // console.
279
- if (updates.length === 0) options.write?.("\r\n")
286
+ for (let u of updates)
287
+ options.subscribe({
288
+ version: [u.version],
289
+ parents: u.parents,
290
+ patches: [{ unit: u.unit, range: u.range, content: u.content }],
291
+ })
292
+
293
+ // Output at least *some* data, or else chrome gets confused and
294
+ // thinks the connection failed. This isn't strictly necessary,
295
+ // but it makes fewer scary errors get printed out in the JS
296
+ // console.
297
+ if (updates.length === 0) options.write?.("\r\n")
298
+ }
280
299
 
281
300
  resource.clients.add(options)
282
301
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.33",
3
+ "version": "0.2.34",
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
@@ -23,6 +23,13 @@
23
23
  <div id="testContainer"></div>
24
24
  <script type=module>
25
25
 
26
+ import {
27
+ default as init,
28
+ Doc,
29
+ OpLog,
30
+ } from "https://unpkg.com/diamond-types-web";
31
+ var dt_p = init()
32
+
26
33
  let delay = 0
27
34
 
28
35
  function createTestDiv(testName) {
@@ -61,6 +68,42 @@ async function runTest(testName, testFunction, expectedResult) {
61
68
  }
62
69
  }
63
70
 
71
+ runTest(
72
+ "test transfer-encoding dt",
73
+ async () => {
74
+ await dt_p
75
+ let key = 'test-' + Math.random().toString(36).slice(2)
76
+ var doc = new Doc('hi')
77
+ doc.ins(0, 'x')
78
+
79
+ let r = await braid_fetch(`/${key}`, {
80
+ method: 'PUT',
81
+ version: ['hi-1'],
82
+ parents: [],
83
+ body: 'xy'
84
+ })
85
+ if (!r.ok) throw 'got: ' + r.statusCode
86
+
87
+ let r2 = await braid_fetch(`/${key}`, {
88
+ version: ['hi-0'],
89
+ subscribe: true,
90
+ headers: {
91
+ 'Merge-Type': 'dt',
92
+ 'Accept-Transfer-Encoding': 'dt'
93
+ }
94
+ })
95
+ return await new Promise(async (done, fail) => {
96
+ r2.subscribe(async update => {
97
+ doc.mergeBytes(update.body)
98
+ done(update.extra_headers['current-version'] + ' ' +
99
+ update.extra_headers['transfer-encoding'] + ' ' +
100
+ doc.get())
101
+ }, fail)
102
+ })
103
+ },
104
+ '"hi-1" dt xy'
105
+ )
106
+
64
107
  runTest(
65
108
  "test error code when missing parents",
66
109
  async () => {