braid-text 0.2.32 → 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 +52 -26
- package/package.json +1 -1
- package/test/test.html +46 -3
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
|
}
|
|
@@ -180,7 +181,10 @@ braid_text.serve = async (req, res, options = {}) => {
|
|
|
180
181
|
// we want to send some kind of error that gives the client faith,
|
|
181
182
|
// that resending this request later may work,
|
|
182
183
|
// hopefully after we've received the necessary parents.
|
|
183
|
-
return done_my_turn(309, e.message, '
|
|
184
|
+
return done_my_turn(309, e.message, 'Version Unknown', {
|
|
185
|
+
Parents: req.headers.parents,
|
|
186
|
+
'Retry-After': '1'
|
|
187
|
+
})
|
|
184
188
|
} else {
|
|
185
189
|
return done_my_turn(500, "The server failed to apply this version. The error generated was: " + e)
|
|
186
190
|
}
|
|
@@ -241,39 +245,57 @@ braid_text.get = async (key, options) => {
|
|
|
241
245
|
options.my_last_sent_version = x.version
|
|
242
246
|
resource.simpleton_clients.add(options)
|
|
243
247
|
} else {
|
|
244
|
-
let updates = null
|
|
245
|
-
|
|
246
248
|
if (resource.need_defrag) {
|
|
247
249
|
if (braid_text.verbose) console.log(`doing defrag..`)
|
|
248
250
|
resource.need_defrag = false
|
|
249
251
|
resource.doc = defrag_dt(resource.doc)
|
|
250
252
|
}
|
|
251
253
|
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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)
|
|
260
271
|
} else {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
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
|
+
}
|
|
271
285
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
+
}
|
|
277
299
|
|
|
278
300
|
resource.clients.add(options)
|
|
279
301
|
}
|
|
@@ -1863,6 +1885,10 @@ class RangeSet {
|
|
|
1863
1885
|
}
|
|
1864
1886
|
}
|
|
1865
1887
|
|
|
1888
|
+
function ascii_ify(s) {
|
|
1889
|
+
return s.replace(/[^\x20-\x7E]/g, c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'))
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1866
1892
|
braid_text.get_resource = get_resource
|
|
1867
1893
|
|
|
1868
1894
|
braid_text.encode_filename = encode_filename
|
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 () => {
|
|
@@ -68,12 +111,12 @@ runTest(
|
|
|
68
111
|
let r = await braid_fetch(`/${key}`, {
|
|
69
112
|
method: 'PUT',
|
|
70
113
|
version: ['hi-1'],
|
|
71
|
-
parents: ['missing-0'],
|
|
114
|
+
parents: ['missing-0', 'y😀-0'],
|
|
72
115
|
body: 'xx'
|
|
73
116
|
})
|
|
74
|
-
return r.status + ' ' + r.ok
|
|
117
|
+
return r.status + ' ' + r.ok + ' ' + r.statusText + ' ' + r.headers.get('Parents')
|
|
75
118
|
},
|
|
76
|
-
'309 false'
|
|
119
|
+
'309 false Version Unknown "missing-0", "y\\ud83d\\ude00-0"'
|
|
77
120
|
)
|
|
78
121
|
|
|
79
122
|
runTest(
|