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.
- package/index.js +44 -25
- package/package.json +1 -1
- 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 (
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
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 () => {
|