braid-text 0.2.45 → 0.2.47
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 +15 -9
- package/package.json +1 -1
- package/test/test.html +131 -0
package/index.js
CHANGED
|
@@ -293,14 +293,20 @@ braid_text.get = async (key, options) => {
|
|
|
293
293
|
} else {
|
|
294
294
|
|
|
295
295
|
if (options.accept_encoding?.match(/updates\s*\((.*)\)/)?.[1].split(',').map(x=>x.trim()).includes('dt')) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
doc.
|
|
296
|
+
// optimization: if client wants past current version,
|
|
297
|
+
// send empty dt
|
|
298
|
+
if (options.parents && v_eq(options.parents, version)) {
|
|
299
|
+
options.subscribe({ encoding: 'dt', body: new Doc().toBytes() })
|
|
300
|
+
} else {
|
|
301
|
+
var bytes = resource.doc.toBytes()
|
|
302
|
+
if (options.parents) {
|
|
303
|
+
var doc = Doc.fromBytes(bytes)
|
|
304
|
+
bytes = doc.getPatchSince(
|
|
305
|
+
dt_get_local_version(bytes, options.parents))
|
|
306
|
+
doc.free()
|
|
307
|
+
}
|
|
308
|
+
options.subscribe({ encoding: 'dt', body: bytes })
|
|
302
309
|
}
|
|
303
|
-
options.subscribe({ encoding: 'dt', body: bytes })
|
|
304
310
|
} else {
|
|
305
311
|
var updates = null
|
|
306
312
|
if (!options.parents && !options.version) {
|
|
@@ -426,7 +432,7 @@ braid_text.put = async (key, options) => {
|
|
|
426
432
|
// validate version: make sure we haven't seen it already
|
|
427
433
|
if (resource.actor_seqs[v[0]]?.has(v[1])) {
|
|
428
434
|
|
|
429
|
-
if (!options.validate_already_seen_versions) return
|
|
435
|
+
if (!options.validate_already_seen_versions) return { change_count }
|
|
430
436
|
|
|
431
437
|
// if we have seen it already, make sure it's the same as before
|
|
432
438
|
let updates = dt_get_patches(resource.doc, og_parents)
|
|
@@ -483,7 +489,7 @@ braid_text.put = async (key, options) => {
|
|
|
483
489
|
}
|
|
484
490
|
|
|
485
491
|
// we already have this version, so nothing left to do
|
|
486
|
-
return
|
|
492
|
+
return { change_count: change_count }
|
|
487
493
|
}
|
|
488
494
|
if (!resource.actor_seqs[v[0]]) resource.actor_seqs[v[0]] = new RangeSet()
|
|
489
495
|
resource.actor_seqs[v[0]].add_range(v[1] + 1 - change_count, v[1])
|
package/package.json
CHANGED
package/test/test.html
CHANGED
|
@@ -96,6 +96,98 @@ async function runTest(testName, testFunction, expectedResult) {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
runTest(
|
|
100
|
+
"test PUTing a version that the server already has",
|
|
101
|
+
async () => {
|
|
102
|
+
var key = 'test-' + Math.random().toString(36).slice(2)
|
|
103
|
+
|
|
104
|
+
var r1 = await braid_fetch(`/${key}`, {
|
|
105
|
+
method: 'PUT',
|
|
106
|
+
version: ['hi-0'],
|
|
107
|
+
parents: [],
|
|
108
|
+
body: 'x'
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
var r2 = await braid_fetch(`/${key}`, {
|
|
112
|
+
method: 'PUT',
|
|
113
|
+
version: ['hi-0'],
|
|
114
|
+
parents: [],
|
|
115
|
+
body: 'x'
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
return r1.status + " " + r2.status
|
|
119
|
+
},
|
|
120
|
+
'200 200'
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
runTest(
|
|
124
|
+
"test validate_already_seen_versions with same version",
|
|
125
|
+
async () => {
|
|
126
|
+
var key = 'test-' + Math.random().toString(36).slice(2)
|
|
127
|
+
|
|
128
|
+
var r1 = await braid_fetch(`/eval`, {
|
|
129
|
+
method: 'PUT',
|
|
130
|
+
body: `void (async () => {
|
|
131
|
+
var resource = await braid_text.get_resource('/${key}')
|
|
132
|
+
|
|
133
|
+
var {change_count} = await braid_text.put(resource, { peer: "abc", version: ["hi-2"], parents: [], patches: [{unit: "text", range: "[0:0]", content: "XYZ"}], merge_type: "dt" })
|
|
134
|
+
|
|
135
|
+
res.end('' + change_count)
|
|
136
|
+
})()`
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
var r2 = await braid_fetch(`/eval`, {
|
|
140
|
+
method: 'PUT',
|
|
141
|
+
body: `void (async () => {
|
|
142
|
+
var resource = await braid_text.get_resource('/${key}')
|
|
143
|
+
|
|
144
|
+
var {change_count} = await braid_text.put(resource, { peer: "abc", version: ["hi-2"], parents: [], patches: [{unit: "text", range: "[0:0]", content: "XYZ"}], merge_type: "dt", validate_already_seen_versions: true })
|
|
145
|
+
|
|
146
|
+
res.end('' + change_count)
|
|
147
|
+
})()`
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
return (await r1.text()) + " " + (await r2.text())
|
|
151
|
+
},
|
|
152
|
+
'3 3'
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
runTest(
|
|
156
|
+
"test validate_already_seen_versions with modified version",
|
|
157
|
+
async () => {
|
|
158
|
+
var key = 'test-' + Math.random().toString(36).slice(2)
|
|
159
|
+
|
|
160
|
+
var r1 = await braid_fetch(`/eval`, {
|
|
161
|
+
method: 'PUT',
|
|
162
|
+
body: `void (async () => {
|
|
163
|
+
var resource = await braid_text.get_resource('/${key}')
|
|
164
|
+
|
|
165
|
+
var {change_count} = await braid_text.put(resource, { peer: "abc", version: ["hi-2"], parents: [], patches: [{unit: "text", range: "[0:0]", content: "XYZ"}], merge_type: "dt" })
|
|
166
|
+
|
|
167
|
+
res.end('' + change_count)
|
|
168
|
+
})()`
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
var r2 = await braid_fetch(`/eval`, {
|
|
172
|
+
method: 'PUT',
|
|
173
|
+
body: `void (async () => {
|
|
174
|
+
var resource = await braid_text.get_resource('/${key}')
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
var {change_count} = await braid_text.put(resource, { peer: "abc", version: ["hi-2"], parents: [], patches: [{unit: "text", range: "[0:0]", content: "ABC"}], merge_type: "dt", validate_already_seen_versions: true })
|
|
178
|
+
|
|
179
|
+
res.end('' + change_count)
|
|
180
|
+
} catch (e) {
|
|
181
|
+
res.end(e.message)
|
|
182
|
+
}
|
|
183
|
+
})()`
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
return await r2.text()
|
|
187
|
+
},
|
|
188
|
+
'invalid update: different from previous update with same version'
|
|
189
|
+
)
|
|
190
|
+
|
|
99
191
|
runTest(
|
|
100
192
|
"test loading a previously saved resource",
|
|
101
193
|
async () => {
|
|
@@ -582,6 +674,45 @@ runTest(
|
|
|
582
674
|
'xy'
|
|
583
675
|
)
|
|
584
676
|
|
|
677
|
+
runTest(
|
|
678
|
+
"test accept-encoding updates(dt) (with parents which are current version)",
|
|
679
|
+
async () => {
|
|
680
|
+
await dt_p
|
|
681
|
+
let key = 'test-' + Math.random().toString(36).slice(2)
|
|
682
|
+
var doc = new Doc('hi')
|
|
683
|
+
doc.ins(0, 'xy')
|
|
684
|
+
|
|
685
|
+
let r = await braid_fetch(`/${key}`, {
|
|
686
|
+
method: 'PUT',
|
|
687
|
+
version: ['hi-1'],
|
|
688
|
+
parents: [],
|
|
689
|
+
body: 'xy'
|
|
690
|
+
})
|
|
691
|
+
if (!r.ok) throw 'got: ' + r.statusCode
|
|
692
|
+
|
|
693
|
+
var a = new AbortController()
|
|
694
|
+
let r2 = await braid_fetch(`/${key}`, {
|
|
695
|
+
signal: a.signal,
|
|
696
|
+
parents: ['hi-1'],
|
|
697
|
+
subscribe: true,
|
|
698
|
+
headers: {
|
|
699
|
+
'merge-type': 'dt',
|
|
700
|
+
'X-Accept-Encoding': 'updates(dt)'
|
|
701
|
+
}
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
return await new Promise(done => {
|
|
705
|
+
r2.subscribe(u => {
|
|
706
|
+
doc.mergeBytes(u.body)
|
|
707
|
+
done(doc.get())
|
|
708
|
+
doc.free()
|
|
709
|
+
a.abort()
|
|
710
|
+
})
|
|
711
|
+
})
|
|
712
|
+
},
|
|
713
|
+
'xy'
|
|
714
|
+
)
|
|
715
|
+
|
|
585
716
|
runTest(
|
|
586
717
|
"test accept-encoding updates(dt)",
|
|
587
718
|
async () => {
|