@automerge/automerge-repo 1.0.6 → 1.0.8
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/.eslintrc +1 -1
- package/dist/DocHandle.d.ts +8 -8
- package/dist/DocHandle.d.ts.map +1 -1
- package/dist/DocHandle.js +4 -8
- package/dist/DocUrl.d.ts.map +1 -1
- package/dist/Repo.d.ts +6 -3
- package/dist/Repo.d.ts.map +1 -1
- package/dist/Repo.js +20 -19
- package/dist/helpers/cbor.d.ts +2 -2
- package/dist/helpers/cbor.d.ts.map +1 -1
- package/dist/helpers/cbor.js +1 -1
- package/dist/helpers/debounce.d.ts +14 -0
- package/dist/helpers/debounce.d.ts.map +1 -0
- package/dist/helpers/debounce.js +21 -0
- package/dist/helpers/pause.d.ts.map +1 -1
- package/dist/helpers/pause.js +3 -1
- package/dist/helpers/tests/network-adapter-tests.d.ts.map +1 -1
- package/dist/helpers/tests/network-adapter-tests.js +2 -2
- package/dist/helpers/throttle.d.ts +28 -0
- package/dist/helpers/throttle.d.ts.map +1 -0
- package/dist/helpers/throttle.js +39 -0
- package/dist/index.d.ts +11 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/network/NetworkAdapter.d.ts +3 -3
- package/dist/network/NetworkAdapter.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.d.ts +2 -2
- package/dist/network/NetworkSubsystem.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.js +30 -18
- package/dist/network/messages.d.ts +38 -68
- package/dist/network/messages.d.ts.map +1 -1
- package/dist/network/messages.js +13 -21
- package/dist/storage/StorageSubsystem.d.ts +1 -1
- package/dist/storage/StorageSubsystem.d.ts.map +1 -1
- package/dist/storage/StorageSubsystem.js +9 -9
- package/dist/synchronizer/CollectionSynchronizer.d.ts +3 -3
- package/dist/synchronizer/CollectionSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/CollectionSynchronizer.js +3 -3
- package/dist/synchronizer/DocSynchronizer.d.ts +4 -3
- package/dist/synchronizer/DocSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/DocSynchronizer.js +25 -34
- package/dist/synchronizer/Synchronizer.d.ts +2 -2
- package/dist/synchronizer/Synchronizer.d.ts.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.d.ts.map +1 -1
- package/fuzz/fuzz.ts +8 -6
- package/fuzz/tsconfig.json +8 -0
- package/package.json +5 -13
- package/src/DocHandle.ts +12 -15
- package/src/DocUrl.ts +3 -1
- package/src/Repo.ts +36 -29
- package/src/helpers/cbor.ts +4 -4
- package/src/helpers/debounce.ts +25 -0
- package/src/helpers/headsAreSame.ts +1 -1
- package/src/helpers/pause.ts +7 -2
- package/src/helpers/tests/network-adapter-tests.ts +3 -3
- package/src/helpers/throttle.ts +43 -0
- package/src/helpers/withTimeout.ts +2 -2
- package/src/index.ts +36 -29
- package/src/network/NetworkAdapter.ts +7 -3
- package/src/network/NetworkSubsystem.ts +31 -23
- package/src/network/messages.ts +88 -151
- package/src/storage/StorageSubsystem.ts +12 -12
- package/src/synchronizer/CollectionSynchronizer.ts +7 -16
- package/src/synchronizer/DocSynchronizer.ts +42 -53
- package/src/synchronizer/Synchronizer.ts +2 -2
- package/src/types.ts +8 -3
- package/test/CollectionSynchronizer.test.ts +58 -53
- package/test/DocHandle.test.ts +35 -36
- package/test/DocSynchronizer.test.ts +5 -8
- package/test/Network.test.ts +1 -0
- package/test/Repo.test.ts +229 -158
- package/test/StorageSubsystem.test.ts +6 -9
- package/test/helpers/DummyNetworkAdapter.ts +9 -4
- package/test/helpers/DummyStorageAdapter.ts +4 -2
- package/test/tsconfig.json +8 -0
- package/typedoc.json +3 -3
- package/.mocharc.json +0 -5
- package/dist/EphemeralData.d.ts +0 -20
- package/dist/EphemeralData.d.ts.map +0 -1
- package/dist/EphemeralData.js +0 -1
- package/src/EphemeralData.ts +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import assert from "assert"
|
|
2
|
-
import { beforeEach } from "
|
|
2
|
+
import { beforeEach, describe, it } from "vitest"
|
|
3
3
|
import { PeerId, Repo } from "../src/index.js"
|
|
4
4
|
import { CollectionSynchronizer } from "../src/synchronizer/CollectionSynchronizer.js"
|
|
5
5
|
|
|
@@ -18,56 +18,61 @@ describe("CollectionSynchronizer", () => {
|
|
|
18
18
|
assert(synchronizer !== null)
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
it("starts synchronizing a document to peers when added",
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
21
|
+
it("starts synchronizing a document to peers when added", () =>
|
|
22
|
+
new Promise<void>(done => {
|
|
23
|
+
const handle = repo.create()
|
|
24
|
+
synchronizer.addPeer("peer1" as PeerId)
|
|
25
|
+
|
|
26
|
+
synchronizer.once("message", event => {
|
|
27
|
+
assert(event.targetId === "peer1")
|
|
28
|
+
assert(event.documentId === handle.documentId)
|
|
29
|
+
done()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
synchronizer.addDocument(handle.documentId)
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
it("starts synchronizing existing documents when a peer is added", () =>
|
|
36
|
+
new Promise<void>(done => {
|
|
37
|
+
const handle = repo.create()
|
|
38
|
+
synchronizer.addDocument(handle.documentId)
|
|
39
|
+
synchronizer.once("message", event => {
|
|
40
|
+
assert(event.targetId === "peer1")
|
|
41
|
+
assert(event.documentId === handle.documentId)
|
|
42
|
+
done()
|
|
43
|
+
})
|
|
44
|
+
synchronizer.addPeer("peer1" as PeerId)
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
it("should not synchronize to a peer which is excluded from the share policy", () =>
|
|
48
|
+
new Promise<void>((done, reject) => {
|
|
49
|
+
const handle = repo.create()
|
|
50
|
+
|
|
51
|
+
repo.sharePolicy = async (peerId: PeerId) => peerId !== "peer1"
|
|
52
|
+
|
|
53
|
+
synchronizer.addDocument(handle.documentId)
|
|
54
|
+
synchronizer.once("message", () => {
|
|
55
|
+
reject(new Error("Should not have sent a message"))
|
|
56
|
+
})
|
|
57
|
+
synchronizer.addPeer("peer1" as PeerId)
|
|
58
|
+
|
|
59
|
+
setTimeout(done)
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
it("should not synchronize a document which is excluded from the share policy", () =>
|
|
63
|
+
new Promise<void>((done, reject) => {
|
|
64
|
+
const handle = repo.create()
|
|
65
|
+
repo.sharePolicy = async (_, documentId) =>
|
|
66
|
+
documentId !== handle.documentId
|
|
67
|
+
|
|
68
|
+
synchronizer.addPeer("peer2" as PeerId)
|
|
69
|
+
|
|
70
|
+
synchronizer.once("message", () => {
|
|
71
|
+
reject(new Error("Should not have sent a message"))
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
synchronizer.addDocument(handle.documentId)
|
|
75
|
+
|
|
76
|
+
setTimeout(done)
|
|
77
|
+
}))
|
|
73
78
|
})
|
package/test/DocHandle.test.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import * as A from "@automerge/automerge/next"
|
|
2
2
|
import assert from "assert"
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { pause } from "../src/helpers/pause.js"
|
|
6
|
-
import { TestDoc } from "./types.js"
|
|
3
|
+
import { decode } from "cbor-x"
|
|
4
|
+
import { describe, it } from "vitest"
|
|
7
5
|
import { generateAutomergeUrl, parseAutomergeUrl } from "../src/DocUrl.js"
|
|
8
6
|
import { eventPromise } from "../src/helpers/eventPromise.js"
|
|
9
|
-
import {
|
|
7
|
+
import { pause } from "../src/helpers/pause.js"
|
|
8
|
+
import { DocHandle, DocHandleChangePayload } from "../src/index.js"
|
|
9
|
+
import { TestDoc } from "./types.js"
|
|
10
10
|
|
|
11
11
|
describe("DocHandle", () => {
|
|
12
12
|
const TEST_ID = parseAutomergeUrl(generateAutomergeUrl()).documentId
|
|
13
|
-
const BOGUS_ID = parseAutomergeUrl(generateAutomergeUrl()).documentId
|
|
14
13
|
|
|
15
14
|
const docFromMockStorage = (doc: A.Doc<{ foo: string }>) => {
|
|
16
15
|
return A.change<{ foo: string }>(doc, d => (d.foo = "bar"))
|
|
@@ -90,7 +89,7 @@ describe("DocHandle", () => {
|
|
|
90
89
|
|
|
91
90
|
assert.equal(handle.docSync(), undefined)
|
|
92
91
|
assert.equal(handle.isReady(), false)
|
|
93
|
-
assert.throws(() => handle.change(_ => {
|
|
92
|
+
assert.throws(() => handle.change(_ => {}))
|
|
94
93
|
})
|
|
95
94
|
|
|
96
95
|
it("should become ready if the document is updated by the network", async () => {
|
|
@@ -128,16 +127,17 @@ describe("DocHandle", () => {
|
|
|
128
127
|
assert.deepStrictEqual(changePayload.handle, handle)
|
|
129
128
|
})
|
|
130
129
|
|
|
131
|
-
it("should not emit a change message if no change happens via update",
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
130
|
+
it("should not emit a change message if no change happens via update", () =>
|
|
131
|
+
new Promise<void>((done, reject) => {
|
|
132
|
+
const handle = new DocHandle<TestDoc>(TEST_ID, { isNew: true })
|
|
133
|
+
handle.once("change", () => {
|
|
134
|
+
reject(new Error("shouldn't have changed"))
|
|
135
|
+
})
|
|
136
|
+
handle.update(d => {
|
|
137
|
+
setTimeout(done, 0)
|
|
138
|
+
return d
|
|
139
|
+
})
|
|
140
|
+
}))
|
|
141
141
|
|
|
142
142
|
it("should update the internal doc prior to emitting the change message", async () => {
|
|
143
143
|
const handle = new DocHandle<TestDoc>(TEST_ID, { isNew: true })
|
|
@@ -200,28 +200,27 @@ describe("DocHandle", () => {
|
|
|
200
200
|
assert.equal(doc?.foo, "bar")
|
|
201
201
|
})
|
|
202
202
|
|
|
203
|
-
it("should not emit a patch message if no change happens",
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
203
|
+
it("should not emit a patch message if no change happens", () =>
|
|
204
|
+
new Promise<void>((done, reject) => {
|
|
205
|
+
const handle = new DocHandle<TestDoc>(TEST_ID, { isNew: true })
|
|
206
|
+
handle.on("change", () => {
|
|
207
|
+
reject(new Error("shouldn't have changed"))
|
|
208
|
+
})
|
|
209
|
+
handle.change(_doc => {
|
|
210
|
+
// do nothing
|
|
211
|
+
setTimeout(done, 0)
|
|
212
|
+
})
|
|
213
|
+
}))
|
|
213
214
|
|
|
214
|
-
it("should
|
|
215
|
+
it("should be undefined if loading the document times out", async () => {
|
|
215
216
|
// set docHandle time out after 5 ms
|
|
216
217
|
const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 5 })
|
|
217
218
|
|
|
218
|
-
|
|
219
|
-
await pause(10)
|
|
219
|
+
const doc = await handle.doc()
|
|
220
220
|
|
|
221
|
-
assert.equal(
|
|
221
|
+
assert.equal(doc, undefined)
|
|
222
222
|
|
|
223
|
-
|
|
224
|
-
return assert.rejects(handle.doc, "DocHandle timed out")
|
|
223
|
+
assert.equal(handle.state, "failed")
|
|
225
224
|
})
|
|
226
225
|
|
|
227
226
|
it("should not time out if the document is loaded in time", async () => {
|
|
@@ -236,7 +235,7 @@ describe("DocHandle", () => {
|
|
|
236
235
|
assert.equal(doc?.foo, "bar")
|
|
237
236
|
})
|
|
238
237
|
|
|
239
|
-
it("should
|
|
238
|
+
it("should be undefined if loading from the network times out", async () => {
|
|
240
239
|
// set docHandle time out after 5 ms
|
|
241
240
|
const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 5 })
|
|
242
241
|
|
|
@@ -246,8 +245,8 @@ describe("DocHandle", () => {
|
|
|
246
245
|
// there's no update
|
|
247
246
|
await pause(10)
|
|
248
247
|
|
|
249
|
-
|
|
250
|
-
|
|
248
|
+
const doc = await handle.doc()
|
|
249
|
+
assert.equal(doc, undefined)
|
|
251
250
|
})
|
|
252
251
|
|
|
253
252
|
it("should not time out if the document is updated in time", async () => {
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import assert from "assert"
|
|
2
|
-
import {
|
|
2
|
+
import { describe, it } from "vitest"
|
|
3
3
|
import { DocHandle } from "../src/DocHandle.js"
|
|
4
|
-
import {
|
|
4
|
+
import { generateAutomergeUrl, parseAutomergeUrl } from "../src/DocUrl.js"
|
|
5
5
|
import { eventPromise } from "../src/helpers/eventPromise.js"
|
|
6
|
-
import { TestDoc } from "./types.js"
|
|
7
|
-
import { parseAutomergeUrl, generateAutomergeUrl } from "../src/DocUrl.js"
|
|
8
|
-
import { SyncMessage } from "../src/index.js"
|
|
9
6
|
import {
|
|
10
7
|
DocumentUnavailableMessage,
|
|
11
|
-
DocumentUnavailableMessageContents,
|
|
12
8
|
MessageContents,
|
|
13
|
-
RequestMessageContents,
|
|
14
|
-
SyncMessageContents,
|
|
15
9
|
} from "../src/network/messages.js"
|
|
10
|
+
import { DocSynchronizer } from "../src/synchronizer/DocSynchronizer.js"
|
|
11
|
+
import { PeerId } from "../src/types.js"
|
|
12
|
+
import { TestDoc } from "./types.js"
|
|
16
13
|
|
|
17
14
|
const alice = "alice" as PeerId
|
|
18
15
|
const bob = "bob" as PeerId
|