braid-text 0.2.86 → 0.2.87
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/.claude/settings.local.json +9 -0
- package/index.js +30 -8
- package/package.json +1 -1
- package/test/tests.js +33 -0
package/index.js
CHANGED
|
@@ -185,12 +185,30 @@ function create_braid_text() {
|
|
|
185
185
|
braid_text.get(a, a_ops)
|
|
186
186
|
|
|
187
187
|
// remote -> local
|
|
188
|
+
var remote_res_done
|
|
189
|
+
var remote_res_promise = new Promise(done => remote_res_done = done)
|
|
190
|
+
var remote_res = null
|
|
191
|
+
|
|
188
192
|
var b_ops = {
|
|
189
193
|
signal: ac.signal,
|
|
190
194
|
dont_retry: true,
|
|
195
|
+
headers: { 'Merge-Type': 'dt', 'accept-encoding': 'updates(dt)' },
|
|
191
196
|
subscribe: async update => {
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
// Wait for remote_res to be available
|
|
198
|
+
await remote_res_promise
|
|
199
|
+
|
|
200
|
+
// Check if this is a dt-encoded update (initial body without status)
|
|
201
|
+
if (!update.status) {
|
|
202
|
+
var cv = remote_res.headers.get('current-version')
|
|
203
|
+
await braid_text.put(a, {
|
|
204
|
+
body: update.body,
|
|
205
|
+
transfer_encoding: 'dt'
|
|
206
|
+
})
|
|
207
|
+
if (cv) extend_fork_point({ version: JSON.parse(`[${cv}]`), parents: resource.meta.fork_point || [] })
|
|
208
|
+
} else {
|
|
209
|
+
await braid_text.put(a, update)
|
|
210
|
+
if (update.version) extend_fork_point(update)
|
|
211
|
+
}
|
|
194
212
|
},
|
|
195
213
|
on_error: e => {
|
|
196
214
|
options.on_disconnect?.()
|
|
@@ -198,15 +216,16 @@ function create_braid_text() {
|
|
|
198
216
|
}
|
|
199
217
|
}
|
|
200
218
|
// Handle case where remote doesn't exist yet - wait for local to create it
|
|
201
|
-
|
|
202
|
-
|
|
219
|
+
remote_res = await braid_text.get(b, b_ops)
|
|
220
|
+
remote_res_done()
|
|
221
|
+
if (remote_res === null) {
|
|
203
222
|
// Remote doesn't exist yet, wait for local to put something
|
|
204
223
|
await local_first_put_promise
|
|
205
224
|
disconnect()
|
|
206
225
|
connect()
|
|
207
226
|
return
|
|
208
227
|
}
|
|
209
|
-
options.on_res?.(
|
|
228
|
+
options.on_res?.(remote_res)
|
|
210
229
|
// on_error will call handle_error when connection drops
|
|
211
230
|
} catch (e) {
|
|
212
231
|
handle_error(e)
|
|
@@ -548,9 +567,12 @@ function create_braid_text() {
|
|
|
548
567
|
|
|
549
568
|
if (options.subscribe) {
|
|
550
569
|
res.subscribe(async update => {
|
|
551
|
-
|
|
552
|
-
if (update.
|
|
553
|
-
|
|
570
|
+
// Don't convert to text for initial dt-encoded body (no status)
|
|
571
|
+
if (update.status) {
|
|
572
|
+
update.body = update.body_text
|
|
573
|
+
if (update.patches)
|
|
574
|
+
for (var p of update.patches) p.content = p.content_text
|
|
575
|
+
}
|
|
554
576
|
await options.subscribe(update)
|
|
555
577
|
}, e => options.on_error?.(e))
|
|
556
578
|
|
package/package.json
CHANGED
package/test/tests.js
CHANGED
|
@@ -2066,6 +2066,39 @@ runTest(
|
|
|
2066
2066
|
'on_res called'
|
|
2067
2067
|
)
|
|
2068
2068
|
|
|
2069
|
+
runTest(
|
|
2070
|
+
"test braid_text.sync uses accept-encoding updates(dt)",
|
|
2071
|
+
async () => {
|
|
2072
|
+
var remote_key = 'test-remote-' + Math.random().toString(36).slice(2)
|
|
2073
|
+
var local_key = 'test-local-' + Math.random().toString(36).slice(2)
|
|
2074
|
+
|
|
2075
|
+
// Create the remote resource with some content
|
|
2076
|
+
var r = await braid_fetch(`/${remote_key}`, {
|
|
2077
|
+
method: 'PUT',
|
|
2078
|
+
body: 'remote content here'
|
|
2079
|
+
})
|
|
2080
|
+
if (!r.ok) return 'put failed: ' + r.status
|
|
2081
|
+
|
|
2082
|
+
// Start sync with URL first (like the passing "url to key" test)
|
|
2083
|
+
var r = await braid_fetch(`/eval`, {
|
|
2084
|
+
method: 'PUT',
|
|
2085
|
+
body: `void (async () => {
|
|
2086
|
+
braid_text.sync(new URL('http://localhost:8889/${remote_key}'), '/${local_key}')
|
|
2087
|
+
res.end('')
|
|
2088
|
+
})()`
|
|
2089
|
+
})
|
|
2090
|
+
if (!r.ok) return 'eval failed: ' + r.status
|
|
2091
|
+
|
|
2092
|
+
// Wait for sync to complete
|
|
2093
|
+
await new Promise(done => setTimeout(done, 100))
|
|
2094
|
+
|
|
2095
|
+
// Check local has remote content
|
|
2096
|
+
var r = await braid_fetch(`/${local_key}`)
|
|
2097
|
+
return await r.text()
|
|
2098
|
+
},
|
|
2099
|
+
'remote content here'
|
|
2100
|
+
)
|
|
2101
|
+
|
|
2069
2102
|
runTest(
|
|
2070
2103
|
"test braid_text.sync reconnects when inner put fails with non-200 status",
|
|
2071
2104
|
async () => {
|