@automerge/automerge-repo 2.0.0-collectionsync-alpha.1 → 2.0.0

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.
Files changed (203) hide show
  1. package/README.md +8 -8
  2. package/dist/AutomergeUrl.d.ts +17 -5
  3. package/dist/AutomergeUrl.d.ts.map +1 -1
  4. package/dist/AutomergeUrl.js +71 -24
  5. package/dist/DocHandle.d.ts +33 -41
  6. package/dist/DocHandle.d.ts.map +1 -1
  7. package/dist/DocHandle.js +105 -66
  8. package/dist/FindProgress.d.ts +30 -0
  9. package/dist/FindProgress.d.ts.map +1 -0
  10. package/dist/FindProgress.js +1 -0
  11. package/dist/RemoteHeadsSubscriptions.d.ts +4 -5
  12. package/dist/RemoteHeadsSubscriptions.d.ts.map +1 -1
  13. package/dist/RemoteHeadsSubscriptions.js +4 -1
  14. package/dist/Repo.d.ts +24 -5
  15. package/dist/Repo.d.ts.map +1 -1
  16. package/dist/Repo.js +355 -169
  17. package/dist/helpers/abortable.d.ts +36 -0
  18. package/dist/helpers/abortable.d.ts.map +1 -0
  19. package/dist/helpers/abortable.js +47 -0
  20. package/dist/helpers/arraysAreEqual.d.ts.map +1 -1
  21. package/dist/helpers/bufferFromHex.d.ts +3 -0
  22. package/dist/helpers/bufferFromHex.d.ts.map +1 -0
  23. package/dist/helpers/bufferFromHex.js +13 -0
  24. package/dist/helpers/debounce.d.ts.map +1 -1
  25. package/dist/helpers/eventPromise.d.ts.map +1 -1
  26. package/dist/helpers/headsAreSame.d.ts +2 -2
  27. package/dist/helpers/headsAreSame.d.ts.map +1 -1
  28. package/dist/helpers/mergeArrays.d.ts +1 -1
  29. package/dist/helpers/mergeArrays.d.ts.map +1 -1
  30. package/dist/helpers/pause.d.ts.map +1 -1
  31. package/dist/helpers/tests/network-adapter-tests.d.ts.map +1 -1
  32. package/dist/helpers/tests/network-adapter-tests.js +13 -13
  33. package/dist/helpers/tests/storage-adapter-tests.d.ts.map +1 -1
  34. package/dist/helpers/tests/storage-adapter-tests.js +6 -9
  35. package/dist/helpers/throttle.d.ts.map +1 -1
  36. package/dist/helpers/withTimeout.d.ts.map +1 -1
  37. package/dist/index.d.ts +35 -7
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +37 -6
  40. package/dist/network/NetworkSubsystem.d.ts +0 -1
  41. package/dist/network/NetworkSubsystem.d.ts.map +1 -1
  42. package/dist/network/NetworkSubsystem.js +0 -3
  43. package/dist/network/messages.d.ts +1 -7
  44. package/dist/network/messages.d.ts.map +1 -1
  45. package/dist/network/messages.js +1 -2
  46. package/dist/storage/StorageAdapter.d.ts +0 -9
  47. package/dist/storage/StorageAdapter.d.ts.map +1 -1
  48. package/dist/storage/StorageAdapter.js +0 -33
  49. package/dist/storage/StorageSubsystem.d.ts +6 -2
  50. package/dist/storage/StorageSubsystem.d.ts.map +1 -1
  51. package/dist/storage/StorageSubsystem.js +131 -37
  52. package/dist/storage/keyHash.d.ts +1 -1
  53. package/dist/storage/keyHash.d.ts.map +1 -1
  54. package/dist/synchronizer/CollectionSynchronizer.d.ts +3 -4
  55. package/dist/synchronizer/CollectionSynchronizer.d.ts.map +1 -1
  56. package/dist/synchronizer/CollectionSynchronizer.js +32 -26
  57. package/dist/synchronizer/DocSynchronizer.d.ts +8 -8
  58. package/dist/synchronizer/DocSynchronizer.d.ts.map +1 -1
  59. package/dist/synchronizer/DocSynchronizer.js +205 -79
  60. package/dist/types.d.ts +4 -1
  61. package/dist/types.d.ts.map +1 -1
  62. package/fuzz/fuzz.ts +3 -3
  63. package/package.json +4 -5
  64. package/src/AutomergeUrl.ts +101 -26
  65. package/src/DocHandle.ts +158 -77
  66. package/src/FindProgress.ts +48 -0
  67. package/src/RemoteHeadsSubscriptions.ts +11 -9
  68. package/src/Repo.ts +465 -180
  69. package/src/helpers/abortable.ts +62 -0
  70. package/src/helpers/bufferFromHex.ts +14 -0
  71. package/src/helpers/headsAreSame.ts +2 -2
  72. package/src/helpers/tests/network-adapter-tests.ts +14 -13
  73. package/src/helpers/tests/storage-adapter-tests.ts +13 -24
  74. package/src/index.ts +57 -38
  75. package/src/network/NetworkSubsystem.ts +0 -4
  76. package/src/network/messages.ts +2 -11
  77. package/src/storage/StorageAdapter.ts +0 -42
  78. package/src/storage/StorageSubsystem.ts +155 -45
  79. package/src/storage/keyHash.ts +1 -1
  80. package/src/synchronizer/CollectionSynchronizer.ts +42 -29
  81. package/src/synchronizer/DocSynchronizer.ts +263 -89
  82. package/src/types.ts +4 -1
  83. package/test/AutomergeUrl.test.ts +130 -0
  84. package/test/CollectionSynchronizer.test.ts +6 -8
  85. package/test/DocHandle.test.ts +161 -77
  86. package/test/DocSynchronizer.test.ts +11 -9
  87. package/test/RemoteHeadsSubscriptions.test.ts +1 -1
  88. package/test/Repo.test.ts +406 -341
  89. package/test/StorageSubsystem.test.ts +95 -20
  90. package/test/remoteHeads.test.ts +28 -13
  91. package/dist/CollectionHandle.d.ts +0 -14
  92. package/dist/CollectionHandle.d.ts.map +0 -1
  93. package/dist/CollectionHandle.js +0 -37
  94. package/dist/DocUrl.d.ts +0 -47
  95. package/dist/DocUrl.d.ts.map +0 -1
  96. package/dist/DocUrl.js +0 -72
  97. package/dist/EphemeralData.d.ts +0 -20
  98. package/dist/EphemeralData.d.ts.map +0 -1
  99. package/dist/EphemeralData.js +0 -1
  100. package/dist/ferigan.d.ts +0 -51
  101. package/dist/ferigan.d.ts.map +0 -1
  102. package/dist/ferigan.js +0 -98
  103. package/dist/src/DocHandle.d.ts +0 -182
  104. package/dist/src/DocHandle.d.ts.map +0 -1
  105. package/dist/src/DocHandle.js +0 -405
  106. package/dist/src/DocUrl.d.ts +0 -49
  107. package/dist/src/DocUrl.d.ts.map +0 -1
  108. package/dist/src/DocUrl.js +0 -72
  109. package/dist/src/EphemeralData.d.ts +0 -19
  110. package/dist/src/EphemeralData.d.ts.map +0 -1
  111. package/dist/src/EphemeralData.js +0 -1
  112. package/dist/src/Repo.d.ts +0 -74
  113. package/dist/src/Repo.d.ts.map +0 -1
  114. package/dist/src/Repo.js +0 -208
  115. package/dist/src/helpers/arraysAreEqual.d.ts +0 -2
  116. package/dist/src/helpers/arraysAreEqual.d.ts.map +0 -1
  117. package/dist/src/helpers/arraysAreEqual.js +0 -2
  118. package/dist/src/helpers/cbor.d.ts +0 -4
  119. package/dist/src/helpers/cbor.d.ts.map +0 -1
  120. package/dist/src/helpers/cbor.js +0 -8
  121. package/dist/src/helpers/eventPromise.d.ts +0 -11
  122. package/dist/src/helpers/eventPromise.d.ts.map +0 -1
  123. package/dist/src/helpers/eventPromise.js +0 -7
  124. package/dist/src/helpers/headsAreSame.d.ts +0 -2
  125. package/dist/src/helpers/headsAreSame.d.ts.map +0 -1
  126. package/dist/src/helpers/headsAreSame.js +0 -4
  127. package/dist/src/helpers/mergeArrays.d.ts +0 -2
  128. package/dist/src/helpers/mergeArrays.d.ts.map +0 -1
  129. package/dist/src/helpers/mergeArrays.js +0 -15
  130. package/dist/src/helpers/pause.d.ts +0 -6
  131. package/dist/src/helpers/pause.d.ts.map +0 -1
  132. package/dist/src/helpers/pause.js +0 -10
  133. package/dist/src/helpers/tests/network-adapter-tests.d.ts +0 -21
  134. package/dist/src/helpers/tests/network-adapter-tests.d.ts.map +0 -1
  135. package/dist/src/helpers/tests/network-adapter-tests.js +0 -122
  136. package/dist/src/helpers/withTimeout.d.ts +0 -12
  137. package/dist/src/helpers/withTimeout.d.ts.map +0 -1
  138. package/dist/src/helpers/withTimeout.js +0 -24
  139. package/dist/src/index.d.ts +0 -53
  140. package/dist/src/index.d.ts.map +0 -1
  141. package/dist/src/index.js +0 -40
  142. package/dist/src/network/NetworkAdapter.d.ts +0 -26
  143. package/dist/src/network/NetworkAdapter.d.ts.map +0 -1
  144. package/dist/src/network/NetworkAdapter.js +0 -4
  145. package/dist/src/network/NetworkSubsystem.d.ts +0 -23
  146. package/dist/src/network/NetworkSubsystem.d.ts.map +0 -1
  147. package/dist/src/network/NetworkSubsystem.js +0 -120
  148. package/dist/src/network/messages.d.ts +0 -85
  149. package/dist/src/network/messages.d.ts.map +0 -1
  150. package/dist/src/network/messages.js +0 -23
  151. package/dist/src/storage/StorageAdapter.d.ts +0 -14
  152. package/dist/src/storage/StorageAdapter.d.ts.map +0 -1
  153. package/dist/src/storage/StorageAdapter.js +0 -1
  154. package/dist/src/storage/StorageSubsystem.d.ts +0 -12
  155. package/dist/src/storage/StorageSubsystem.d.ts.map +0 -1
  156. package/dist/src/storage/StorageSubsystem.js +0 -145
  157. package/dist/src/synchronizer/CollectionSynchronizer.d.ts +0 -25
  158. package/dist/src/synchronizer/CollectionSynchronizer.d.ts.map +0 -1
  159. package/dist/src/synchronizer/CollectionSynchronizer.js +0 -106
  160. package/dist/src/synchronizer/DocSynchronizer.d.ts +0 -29
  161. package/dist/src/synchronizer/DocSynchronizer.d.ts.map +0 -1
  162. package/dist/src/synchronizer/DocSynchronizer.js +0 -263
  163. package/dist/src/synchronizer/Synchronizer.d.ts +0 -9
  164. package/dist/src/synchronizer/Synchronizer.d.ts.map +0 -1
  165. package/dist/src/synchronizer/Synchronizer.js +0 -2
  166. package/dist/src/types.d.ts +0 -16
  167. package/dist/src/types.d.ts.map +0 -1
  168. package/dist/src/types.js +0 -1
  169. package/dist/test/CollectionSynchronizer.test.d.ts +0 -2
  170. package/dist/test/CollectionSynchronizer.test.d.ts.map +0 -1
  171. package/dist/test/CollectionSynchronizer.test.js +0 -57
  172. package/dist/test/DocHandle.test.d.ts +0 -2
  173. package/dist/test/DocHandle.test.d.ts.map +0 -1
  174. package/dist/test/DocHandle.test.js +0 -238
  175. package/dist/test/DocSynchronizer.test.d.ts +0 -2
  176. package/dist/test/DocSynchronizer.test.d.ts.map +0 -1
  177. package/dist/test/DocSynchronizer.test.js +0 -111
  178. package/dist/test/Network.test.d.ts +0 -2
  179. package/dist/test/Network.test.d.ts.map +0 -1
  180. package/dist/test/Network.test.js +0 -11
  181. package/dist/test/Repo.test.d.ts +0 -2
  182. package/dist/test/Repo.test.d.ts.map +0 -1
  183. package/dist/test/Repo.test.js +0 -568
  184. package/dist/test/StorageSubsystem.test.d.ts +0 -2
  185. package/dist/test/StorageSubsystem.test.d.ts.map +0 -1
  186. package/dist/test/StorageSubsystem.test.js +0 -56
  187. package/dist/test/helpers/DummyNetworkAdapter.d.ts +0 -9
  188. package/dist/test/helpers/DummyNetworkAdapter.d.ts.map +0 -1
  189. package/dist/test/helpers/DummyNetworkAdapter.js +0 -15
  190. package/dist/test/helpers/DummyStorageAdapter.d.ts +0 -16
  191. package/dist/test/helpers/DummyStorageAdapter.d.ts.map +0 -1
  192. package/dist/test/helpers/DummyStorageAdapter.js +0 -33
  193. package/dist/test/helpers/generate-large-object.d.ts +0 -5
  194. package/dist/test/helpers/generate-large-object.d.ts.map +0 -1
  195. package/dist/test/helpers/generate-large-object.js +0 -9
  196. package/dist/test/helpers/getRandomItem.d.ts +0 -2
  197. package/dist/test/helpers/getRandomItem.d.ts.map +0 -1
  198. package/dist/test/helpers/getRandomItem.js +0 -4
  199. package/dist/test/types.d.ts +0 -4
  200. package/dist/test/types.d.ts.map +0 -1
  201. package/dist/test/types.js +0 -1
  202. package/src/CollectionHandle.ts +0 -54
  203. package/src/ferigan.ts +0 -184
@@ -1,2 +0,0 @@
1
- export {}
2
- //# sourceMappingURL=DocHandle.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DocHandle.test.d.ts","sourceRoot":"","sources":["../../test/DocHandle.test.ts"],"names":[],"mappings":""}
@@ -1,238 +0,0 @@
1
- import * as A from "@automerge/automerge/next"
2
- import assert from "assert"
3
- import { it } from "mocha"
4
- import { DocHandle } from "../src/index.js"
5
- import { pause } from "../src/helpers/pause.js"
6
- import { generateAutomergeUrl, parseAutomergeUrl } from "../src/DocUrl.js"
7
- import { eventPromise } from "../src/helpers/eventPromise.js"
8
- import { decode } from "cbor-x"
9
- describe("DocHandle", () => {
10
- const TEST_ID = parseAutomergeUrl(generateAutomergeUrl()).documentId
11
- const BOGUS_ID = parseAutomergeUrl(generateAutomergeUrl()).documentId
12
- const docFromMockStorage = doc => {
13
- return A.change(doc, d => (d.foo = "bar"))
14
- }
15
- it("should take the UUID passed into it", () => {
16
- const handle = new DocHandle(TEST_ID)
17
- assert.equal(handle.documentId, TEST_ID)
18
- })
19
- it("should become ready when a document is loaded", async () => {
20
- const handle = new DocHandle(TEST_ID)
21
- assert.equal(handle.isReady(), false)
22
- // simulate loading from storage
23
- handle.update(doc => docFromMockStorage(doc))
24
- assert.equal(handle.isReady(), true)
25
- const doc = await handle.doc()
26
- assert.equal(doc?.foo, "bar")
27
- })
28
- it("should allow sync access to the doc", async () => {
29
- const handle = new DocHandle(TEST_ID)
30
- assert.equal(handle.isReady(), false)
31
- // simulate loading from storage
32
- handle.update(doc => docFromMockStorage(doc))
33
- assert.equal(handle.isReady(), true)
34
- const doc = await handle.doc()
35
- assert.deepEqual(doc, handle.docSync())
36
- })
37
- it("should return undefined if we accessing the doc before ready", async () => {
38
- const handle = new DocHandle(TEST_ID)
39
- assert.equal(handle.docSync(), undefined)
40
- })
41
- it("should not return a doc until ready", async () => {
42
- const handle = new DocHandle(TEST_ID)
43
- assert.equal(handle.isReady(), false)
44
- // simulate loading from storage
45
- handle.update(doc => docFromMockStorage(doc))
46
- const doc = await handle.doc()
47
- assert.equal(handle.isReady(), true)
48
- assert.equal(doc?.foo, "bar")
49
- })
50
- it("should block changes until ready()", async () => {
51
- const handle = new DocHandle(TEST_ID)
52
- // can't make changes in LOADING state
53
- assert.equal(handle.isReady(), false)
54
- assert.throws(() => handle.change(d => (d.foo = "baz")))
55
- // simulate loading from storage
56
- handle.update(doc => docFromMockStorage(doc))
57
- // now we're in READY state so we can make changes
58
- assert.equal(handle.isReady(), true)
59
- handle.change(d => (d.foo = "pizza"))
60
- const doc = await handle.doc()
61
- assert.equal(doc?.foo, "pizza")
62
- })
63
- it("should not be ready while requesting from the network", async () => {
64
- const handle = new DocHandle(TEST_ID)
65
- // we don't have it in storage, so we request it from the network
66
- handle.request()
67
- assert.equal(handle.docSync(), undefined)
68
- assert.equal(handle.isReady(), false)
69
- assert.throws(() => handle.change(_ => {}))
70
- })
71
- it("should become ready if the document is updated by the network", async () => {
72
- const handle = new DocHandle(TEST_ID)
73
- // we don't have it in storage, so we request it from the network
74
- handle.request()
75
- // simulate updating from the network
76
- handle.update(doc => {
77
- return A.change(doc, d => (d.foo = "bar"))
78
- })
79
- const doc = await handle.doc()
80
- assert.equal(handle.isReady(), true)
81
- assert.equal(doc?.foo, "bar")
82
- })
83
- it("should emit a change message when changes happen", async () => {
84
- const handle = new DocHandle(TEST_ID, { isNew: true })
85
- const p = new Promise(resolve => handle.once("change", d => resolve(d)))
86
- handle.change(doc => {
87
- doc.foo = "bar"
88
- })
89
- const doc = await handle.doc()
90
- assert.equal(doc?.foo, "bar")
91
- const changePayload = await p
92
- assert.deepStrictEqual(changePayload.doc, doc)
93
- assert.deepStrictEqual(changePayload.handle, handle)
94
- })
95
- it("should not emit a change message if no change happens via update", done => {
96
- const handle = new DocHandle(TEST_ID, { isNew: true })
97
- handle.once("change", () => {
98
- done(new Error("shouldn't have changed"))
99
- })
100
- handle.update(d => {
101
- setTimeout(done, 0)
102
- return d
103
- })
104
- })
105
- it("should update the internal doc prior to emitting the change message", async () => {
106
- const handle = new DocHandle(TEST_ID, { isNew: true })
107
- const p = new Promise(resolve =>
108
- handle.once("change", ({ handle, doc }) => {
109
- assert.equal(handle.docSync()?.foo, doc.foo)
110
- resolve()
111
- })
112
- )
113
- handle.change(doc => {
114
- doc.foo = "baz"
115
- })
116
- return p
117
- })
118
- it("should emit distinct change messages when consecutive changes happen", async () => {
119
- const handle = new DocHandle(TEST_ID, { isNew: true })
120
- let calls = 0
121
- const p = new Promise(resolve =>
122
- handle.on("change", async ({ doc: d }) => {
123
- if (calls === 0) {
124
- assert.equal(d.foo, "bar")
125
- calls++
126
- return
127
- }
128
- assert.equal(d.foo, "baz")
129
- resolve(d)
130
- })
131
- )
132
- handle.change(doc => {
133
- doc.foo = "bar"
134
- })
135
- handle.change(doc => {
136
- doc.foo = "baz"
137
- })
138
- const doc = await handle.doc()
139
- assert.equal(doc?.foo, "baz")
140
- return p
141
- })
142
- it("should emit a change message when changes happen", async () => {
143
- const handle = new DocHandle(TEST_ID, { isNew: true })
144
- const p = new Promise(resolve => handle.once("change", d => resolve(d)))
145
- handle.change(doc => {
146
- doc.foo = "bar"
147
- })
148
- await p
149
- const doc = await handle.doc()
150
- assert.equal(doc?.foo, "bar")
151
- })
152
- it("should not emit a patch message if no change happens", done => {
153
- const handle = new DocHandle(TEST_ID, { isNew: true })
154
- handle.on("change", () => {
155
- done(new Error("shouldn't have changed"))
156
- })
157
- handle.change(_doc => {
158
- // do nothing
159
- setTimeout(done, 0)
160
- })
161
- })
162
- it("should time out if the document is not loaded", async () => {
163
- // set docHandle time out after 5 ms
164
- const handle = new DocHandle(TEST_ID, { timeoutDelay: 5 })
165
- // we're not going to load
166
- await pause(10)
167
- assert.equal(handle.state, "failed")
168
- // so it should time out
169
- return assert.rejects(handle.doc, "DocHandle timed out")
170
- })
171
- it("should not time out if the document is loaded in time", async () => {
172
- // set docHandle time out after 5 ms
173
- const handle = new DocHandle(TEST_ID, { timeoutDelay: 5 })
174
- // simulate loading from storage before the timeout expires
175
- handle.update(doc => docFromMockStorage(doc))
176
- // now it should not time out
177
- const doc = await handle.doc()
178
- assert.equal(doc?.foo, "bar")
179
- })
180
- it("should time out if the document is not updated from the network", async () => {
181
- // set docHandle time out after 5 ms
182
- const handle = new DocHandle(TEST_ID, { timeoutDelay: 5 })
183
- // simulate requesting from the network
184
- handle.request()
185
- // there's no update
186
- await pause(10)
187
- // so it should time out
188
- return assert.rejects(handle.doc, "DocHandle timed out")
189
- })
190
- it("should not time out if the document is updated in time", async () => {
191
- // set docHandle time out after 5 ms
192
- const handle = new DocHandle(TEST_ID, { timeoutDelay: 1 })
193
- // simulate requesting from the network
194
- handle.request()
195
- // simulate updating from the network before the timeout expires
196
- handle.update(doc => {
197
- return A.change(doc, d => (d.foo = "bar"))
198
- })
199
- // now it should not time out
200
- await pause(5)
201
- const doc = await handle.doc()
202
- assert.equal(doc?.foo, "bar")
203
- })
204
- it("should emit a delete event when deleted", async () => {
205
- const handle = new DocHandle(TEST_ID, { isNew: true })
206
- const p = new Promise(resolve => handle.once("delete", () => resolve()))
207
- handle.delete()
208
- await p
209
- assert.equal(handle.isDeleted(), true)
210
- })
211
- it("should allow changing at old heads", async () => {
212
- const handle = new DocHandle(TEST_ID, { isNew: true })
213
- handle.change(doc => {
214
- doc.foo = "bar"
215
- })
216
- const headsBefore = A.getHeads(handle.docSync())
217
- handle.change(doc => {
218
- doc.foo = "rab"
219
- })
220
- let wasBar = false
221
- let newHeads = handle.changeAt(headsBefore, doc => {
222
- wasBar = doc.foo === "bar"
223
- doc.foo = "baz"
224
- })
225
- assert(newHeads && newHeads.length > 0, "should have new heads")
226
- assert(wasBar, "foo should have been bar as we changed at the old heads")
227
- })
228
- describe("ephemeral messaging", () => {
229
- it("can broadcast a message for the network to send out", async () => {
230
- const handle = new DocHandle(TEST_ID, { isNew: true })
231
- const message = { foo: "bar" }
232
- const promise = eventPromise(handle, "ephemeral-message-outbound")
233
- handle.broadcast(message)
234
- const { data } = await promise
235
- assert.deepStrictEqual(decode(data), message)
236
- })
237
- })
238
- })
@@ -1,2 +0,0 @@
1
- export {}
2
- //# sourceMappingURL=DocSynchronizer.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DocSynchronizer.test.d.ts","sourceRoot":"","sources":["../../test/DocSynchronizer.test.ts"],"names":[],"mappings":""}
@@ -1,111 +0,0 @@
1
- import assert from "assert"
2
- import { DocHandle } from "../src/DocHandle.js"
3
- import { DocSynchronizer } from "../src/synchronizer/DocSynchronizer.js"
4
- import { eventPromise } from "../src/helpers/eventPromise.js"
5
- import { parseAutomergeUrl, generateAutomergeUrl } from "../src/DocUrl.js"
6
- const alice = "alice"
7
- const bob = "bob"
8
- const charlie = "charlie"
9
- describe("DocSynchronizer", () => {
10
- let handle
11
- let docSynchronizer
12
- const setup = () => {
13
- const docId = parseAutomergeUrl(generateAutomergeUrl()).documentId
14
- handle = new DocHandle(docId, { isNew: true })
15
- docSynchronizer = new DocSynchronizer(handle)
16
- return { handle, docSynchronizer }
17
- }
18
- it("takes the handle passed into it", () => {
19
- const { handle, docSynchronizer } = setup()
20
- assert(docSynchronizer.documentId === handle.documentId)
21
- })
22
- it("emits a syncMessage when beginSync is called", async () => {
23
- const { docSynchronizer } = setup()
24
- docSynchronizer.beginSync([alice])
25
- const { targetId, type } = await eventPromise(docSynchronizer, "message")
26
- assert.equal(type, "sync")
27
- assert.equal(targetId, "alice")
28
- })
29
- it("emits a syncMessage to peers when the handle is updated", async () => {
30
- const { handle, docSynchronizer } = setup()
31
- docSynchronizer.beginSync([alice])
32
- handle.change(doc => {
33
- doc.foo = "bar"
34
- })
35
- const { targetId, type } = await eventPromise(docSynchronizer, "message")
36
- assert.equal(targetId, "alice")
37
- assert.equal(type, "sync")
38
- })
39
- it("still syncs with a peer after it disconnects and reconnects", async () => {
40
- const { handle, docSynchronizer } = setup()
41
- // first connection
42
- {
43
- docSynchronizer.beginSync([bob])
44
- handle.change(doc => {
45
- doc.foo = "a change"
46
- })
47
- const { targetId, type } = await eventPromise(docSynchronizer, "message")
48
- assert.equal(targetId, "bob")
49
- assert.equal(type, "sync")
50
- docSynchronizer.endSync(bob)
51
- }
52
- // second connection
53
- {
54
- docSynchronizer.beginSync([bob])
55
- handle.change(doc => {
56
- doc.foo = "another change"
57
- })
58
- const { targetId, type } = await eventPromise(docSynchronizer, "message")
59
- assert.equal(targetId, "bob")
60
- assert.equal(type, "sync")
61
- }
62
- })
63
- it("emits a requestMessage if the local handle is being requested", async () => {
64
- const docId = parseAutomergeUrl(generateAutomergeUrl()).documentId
65
- const handle = new DocHandle(docId, { isNew: false })
66
- docSynchronizer = new DocSynchronizer(handle)
67
- docSynchronizer.beginSync([alice])
68
- handle.request()
69
- const message = await eventPromise(docSynchronizer, "message")
70
- assert.equal(message.targetId, "alice")
71
- assert.equal(message.type, "request")
72
- })
73
- it("emits the correct sequence of messages when a document is not found then not available", async () => {
74
- const docId = parseAutomergeUrl(generateAutomergeUrl()).documentId
75
- const bobHandle = new DocHandle(docId, { isNew: false })
76
- const bobDocSynchronizer = new DocSynchronizer(bobHandle)
77
- bobDocSynchronizer.beginSync([alice])
78
- bobHandle.request()
79
- const message = await eventPromise(bobDocSynchronizer, "message")
80
- const aliceHandle = new DocHandle(docId, { isNew: false })
81
- const aliceDocSynchronizer = new DocSynchronizer(aliceHandle)
82
- aliceHandle.request()
83
- aliceDocSynchronizer.receiveSyncMessage({ ...message, senderId: bob })
84
- aliceDocSynchronizer.beginSync([charlie, bob])
85
- const [charlieMessage, bobMessage] = await new Promise(resolve => {
86
- const messages = []
87
- aliceDocSynchronizer.on("message", message => {
88
- messages.push(message)
89
- if (messages.length === 2) {
90
- resolve(messages)
91
- }
92
- })
93
- })
94
- // the response should be a sync message, not a request message
95
- assert.equal(charlieMessage.targetId, "charlie")
96
- assert.equal(charlieMessage.type, "request")
97
- assert.equal(bobMessage.targetId, "bob")
98
- assert.equal(bobMessage.type, "sync")
99
- const docUnavailableMessage = {
100
- type: "doc-unavailable",
101
- targetId: alice,
102
- senderId: charlie,
103
- documentId: docId,
104
- }
105
- const p = eventPromise(aliceDocSynchronizer, "message")
106
- aliceDocSynchronizer.receiveMessage(docUnavailableMessage)
107
- const message2 = await p
108
- assert.equal(message2.targetId, "bob")
109
- assert.equal(message2.type, "doc-unavailable")
110
- })
111
- })
@@ -1,2 +0,0 @@
1
- export {}
2
- //# sourceMappingURL=Network.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Network.test.d.ts","sourceRoot":"","sources":["../../test/Network.test.ts"],"names":[],"mappings":""}
@@ -1,11 +0,0 @@
1
- import assert from "assert"
2
- import { NetworkSubsystem } from "../src/network/NetworkSubsystem.js"
3
- // Note: The sync tests in `Repo.test.ts` exercise the network subsystem, and the suite in
4
- // `automerge-repo` provides test utilities that individual adapters can
5
- // use to ensure that they work correctly.
6
- describe("Network subsystem", () => {
7
- it("Can be instantiated", () => {
8
- const network = new NetworkSubsystem([])
9
- assert(network !== null)
10
- })
11
- })
@@ -1,2 +0,0 @@
1
- export {}
2
- //# sourceMappingURL=Repo.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Repo.test.d.ts","sourceRoot":"","sources":["../../test/Repo.test.ts"],"names":[],"mappings":""}