@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.
- package/README.md +8 -8
- package/dist/AutomergeUrl.d.ts +17 -5
- package/dist/AutomergeUrl.d.ts.map +1 -1
- package/dist/AutomergeUrl.js +71 -24
- package/dist/DocHandle.d.ts +33 -41
- package/dist/DocHandle.d.ts.map +1 -1
- package/dist/DocHandle.js +105 -66
- package/dist/FindProgress.d.ts +30 -0
- package/dist/FindProgress.d.ts.map +1 -0
- package/dist/FindProgress.js +1 -0
- package/dist/RemoteHeadsSubscriptions.d.ts +4 -5
- package/dist/RemoteHeadsSubscriptions.d.ts.map +1 -1
- package/dist/RemoteHeadsSubscriptions.js +4 -1
- package/dist/Repo.d.ts +24 -5
- package/dist/Repo.d.ts.map +1 -1
- package/dist/Repo.js +355 -169
- package/dist/helpers/abortable.d.ts +36 -0
- package/dist/helpers/abortable.d.ts.map +1 -0
- package/dist/helpers/abortable.js +47 -0
- package/dist/helpers/arraysAreEqual.d.ts.map +1 -1
- package/dist/helpers/bufferFromHex.d.ts +3 -0
- package/dist/helpers/bufferFromHex.d.ts.map +1 -0
- package/dist/helpers/bufferFromHex.js +13 -0
- package/dist/helpers/debounce.d.ts.map +1 -1
- package/dist/helpers/eventPromise.d.ts.map +1 -1
- package/dist/helpers/headsAreSame.d.ts +2 -2
- package/dist/helpers/headsAreSame.d.ts.map +1 -1
- package/dist/helpers/mergeArrays.d.ts +1 -1
- package/dist/helpers/mergeArrays.d.ts.map +1 -1
- package/dist/helpers/pause.d.ts.map +1 -1
- package/dist/helpers/tests/network-adapter-tests.d.ts.map +1 -1
- package/dist/helpers/tests/network-adapter-tests.js +13 -13
- package/dist/helpers/tests/storage-adapter-tests.d.ts.map +1 -1
- package/dist/helpers/tests/storage-adapter-tests.js +6 -9
- package/dist/helpers/throttle.d.ts.map +1 -1
- package/dist/helpers/withTimeout.d.ts.map +1 -1
- package/dist/index.d.ts +35 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +37 -6
- package/dist/network/NetworkSubsystem.d.ts +0 -1
- package/dist/network/NetworkSubsystem.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.js +0 -3
- package/dist/network/messages.d.ts +1 -7
- package/dist/network/messages.d.ts.map +1 -1
- package/dist/network/messages.js +1 -2
- package/dist/storage/StorageAdapter.d.ts +0 -9
- package/dist/storage/StorageAdapter.d.ts.map +1 -1
- package/dist/storage/StorageAdapter.js +0 -33
- package/dist/storage/StorageSubsystem.d.ts +6 -2
- package/dist/storage/StorageSubsystem.d.ts.map +1 -1
- package/dist/storage/StorageSubsystem.js +131 -37
- package/dist/storage/keyHash.d.ts +1 -1
- package/dist/storage/keyHash.d.ts.map +1 -1
- package/dist/synchronizer/CollectionSynchronizer.d.ts +3 -4
- package/dist/synchronizer/CollectionSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/CollectionSynchronizer.js +32 -26
- package/dist/synchronizer/DocSynchronizer.d.ts +8 -8
- package/dist/synchronizer/DocSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/DocSynchronizer.js +205 -79
- package/dist/types.d.ts +4 -1
- package/dist/types.d.ts.map +1 -1
- package/fuzz/fuzz.ts +3 -3
- package/package.json +4 -5
- package/src/AutomergeUrl.ts +101 -26
- package/src/DocHandle.ts +158 -77
- package/src/FindProgress.ts +48 -0
- package/src/RemoteHeadsSubscriptions.ts +11 -9
- package/src/Repo.ts +465 -180
- package/src/helpers/abortable.ts +62 -0
- package/src/helpers/bufferFromHex.ts +14 -0
- package/src/helpers/headsAreSame.ts +2 -2
- package/src/helpers/tests/network-adapter-tests.ts +14 -13
- package/src/helpers/tests/storage-adapter-tests.ts +13 -24
- package/src/index.ts +57 -38
- package/src/network/NetworkSubsystem.ts +0 -4
- package/src/network/messages.ts +2 -11
- package/src/storage/StorageAdapter.ts +0 -42
- package/src/storage/StorageSubsystem.ts +155 -45
- package/src/storage/keyHash.ts +1 -1
- package/src/synchronizer/CollectionSynchronizer.ts +42 -29
- package/src/synchronizer/DocSynchronizer.ts +263 -89
- package/src/types.ts +4 -1
- package/test/AutomergeUrl.test.ts +130 -0
- package/test/CollectionSynchronizer.test.ts +6 -8
- package/test/DocHandle.test.ts +161 -77
- package/test/DocSynchronizer.test.ts +11 -9
- package/test/RemoteHeadsSubscriptions.test.ts +1 -1
- package/test/Repo.test.ts +406 -341
- package/test/StorageSubsystem.test.ts +95 -20
- package/test/remoteHeads.test.ts +28 -13
- package/dist/CollectionHandle.d.ts +0 -14
- package/dist/CollectionHandle.d.ts.map +0 -1
- package/dist/CollectionHandle.js +0 -37
- package/dist/DocUrl.d.ts +0 -47
- package/dist/DocUrl.d.ts.map +0 -1
- package/dist/DocUrl.js +0 -72
- package/dist/EphemeralData.d.ts +0 -20
- package/dist/EphemeralData.d.ts.map +0 -1
- package/dist/EphemeralData.js +0 -1
- package/dist/ferigan.d.ts +0 -51
- package/dist/ferigan.d.ts.map +0 -1
- package/dist/ferigan.js +0 -98
- package/dist/src/DocHandle.d.ts +0 -182
- package/dist/src/DocHandle.d.ts.map +0 -1
- package/dist/src/DocHandle.js +0 -405
- package/dist/src/DocUrl.d.ts +0 -49
- package/dist/src/DocUrl.d.ts.map +0 -1
- package/dist/src/DocUrl.js +0 -72
- package/dist/src/EphemeralData.d.ts +0 -19
- package/dist/src/EphemeralData.d.ts.map +0 -1
- package/dist/src/EphemeralData.js +0 -1
- package/dist/src/Repo.d.ts +0 -74
- package/dist/src/Repo.d.ts.map +0 -1
- package/dist/src/Repo.js +0 -208
- package/dist/src/helpers/arraysAreEqual.d.ts +0 -2
- package/dist/src/helpers/arraysAreEqual.d.ts.map +0 -1
- package/dist/src/helpers/arraysAreEqual.js +0 -2
- package/dist/src/helpers/cbor.d.ts +0 -4
- package/dist/src/helpers/cbor.d.ts.map +0 -1
- package/dist/src/helpers/cbor.js +0 -8
- package/dist/src/helpers/eventPromise.d.ts +0 -11
- package/dist/src/helpers/eventPromise.d.ts.map +0 -1
- package/dist/src/helpers/eventPromise.js +0 -7
- package/dist/src/helpers/headsAreSame.d.ts +0 -2
- package/dist/src/helpers/headsAreSame.d.ts.map +0 -1
- package/dist/src/helpers/headsAreSame.js +0 -4
- package/dist/src/helpers/mergeArrays.d.ts +0 -2
- package/dist/src/helpers/mergeArrays.d.ts.map +0 -1
- package/dist/src/helpers/mergeArrays.js +0 -15
- package/dist/src/helpers/pause.d.ts +0 -6
- package/dist/src/helpers/pause.d.ts.map +0 -1
- package/dist/src/helpers/pause.js +0 -10
- package/dist/src/helpers/tests/network-adapter-tests.d.ts +0 -21
- package/dist/src/helpers/tests/network-adapter-tests.d.ts.map +0 -1
- package/dist/src/helpers/tests/network-adapter-tests.js +0 -122
- package/dist/src/helpers/withTimeout.d.ts +0 -12
- package/dist/src/helpers/withTimeout.d.ts.map +0 -1
- package/dist/src/helpers/withTimeout.js +0 -24
- package/dist/src/index.d.ts +0 -53
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -40
- package/dist/src/network/NetworkAdapter.d.ts +0 -26
- package/dist/src/network/NetworkAdapter.d.ts.map +0 -1
- package/dist/src/network/NetworkAdapter.js +0 -4
- package/dist/src/network/NetworkSubsystem.d.ts +0 -23
- package/dist/src/network/NetworkSubsystem.d.ts.map +0 -1
- package/dist/src/network/NetworkSubsystem.js +0 -120
- package/dist/src/network/messages.d.ts +0 -85
- package/dist/src/network/messages.d.ts.map +0 -1
- package/dist/src/network/messages.js +0 -23
- package/dist/src/storage/StorageAdapter.d.ts +0 -14
- package/dist/src/storage/StorageAdapter.d.ts.map +0 -1
- package/dist/src/storage/StorageAdapter.js +0 -1
- package/dist/src/storage/StorageSubsystem.d.ts +0 -12
- package/dist/src/storage/StorageSubsystem.d.ts.map +0 -1
- package/dist/src/storage/StorageSubsystem.js +0 -145
- package/dist/src/synchronizer/CollectionSynchronizer.d.ts +0 -25
- package/dist/src/synchronizer/CollectionSynchronizer.d.ts.map +0 -1
- package/dist/src/synchronizer/CollectionSynchronizer.js +0 -106
- package/dist/src/synchronizer/DocSynchronizer.d.ts +0 -29
- package/dist/src/synchronizer/DocSynchronizer.d.ts.map +0 -1
- package/dist/src/synchronizer/DocSynchronizer.js +0 -263
- package/dist/src/synchronizer/Synchronizer.d.ts +0 -9
- package/dist/src/synchronizer/Synchronizer.d.ts.map +0 -1
- package/dist/src/synchronizer/Synchronizer.js +0 -2
- package/dist/src/types.d.ts +0 -16
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/types.js +0 -1
- package/dist/test/CollectionSynchronizer.test.d.ts +0 -2
- package/dist/test/CollectionSynchronizer.test.d.ts.map +0 -1
- package/dist/test/CollectionSynchronizer.test.js +0 -57
- package/dist/test/DocHandle.test.d.ts +0 -2
- package/dist/test/DocHandle.test.d.ts.map +0 -1
- package/dist/test/DocHandle.test.js +0 -238
- package/dist/test/DocSynchronizer.test.d.ts +0 -2
- package/dist/test/DocSynchronizer.test.d.ts.map +0 -1
- package/dist/test/DocSynchronizer.test.js +0 -111
- package/dist/test/Network.test.d.ts +0 -2
- package/dist/test/Network.test.d.ts.map +0 -1
- package/dist/test/Network.test.js +0 -11
- package/dist/test/Repo.test.d.ts +0 -2
- package/dist/test/Repo.test.d.ts.map +0 -1
- package/dist/test/Repo.test.js +0 -568
- package/dist/test/StorageSubsystem.test.d.ts +0 -2
- package/dist/test/StorageSubsystem.test.d.ts.map +0 -1
- package/dist/test/StorageSubsystem.test.js +0 -56
- package/dist/test/helpers/DummyNetworkAdapter.d.ts +0 -9
- package/dist/test/helpers/DummyNetworkAdapter.d.ts.map +0 -1
- package/dist/test/helpers/DummyNetworkAdapter.js +0 -15
- package/dist/test/helpers/DummyStorageAdapter.d.ts +0 -16
- package/dist/test/helpers/DummyStorageAdapter.d.ts.map +0 -1
- package/dist/test/helpers/DummyStorageAdapter.js +0 -33
- package/dist/test/helpers/generate-large-object.d.ts +0 -5
- package/dist/test/helpers/generate-large-object.d.ts.map +0 -1
- package/dist/test/helpers/generate-large-object.js +0 -9
- package/dist/test/helpers/getRandomItem.d.ts +0 -2
- package/dist/test/helpers/getRandomItem.d.ts.map +0 -1
- package/dist/test/helpers/getRandomItem.js +0 -4
- package/dist/test/types.d.ts +0 -4
- package/dist/test/types.d.ts.map +0 -1
- package/dist/test/types.js +0 -1
- package/src/CollectionHandle.ts +0 -54
- package/src/ferigan.ts +0 -184
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a Promise and causes it to reject when the signal is aborted.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This utility wraps a Promise and rejects when the provided AbortSignal is aborted.
|
|
6
|
+
* It's designed to make Promise awaits abortable.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const controller = new AbortController();
|
|
11
|
+
*
|
|
12
|
+
* try {
|
|
13
|
+
* const result = await abortable(fetch('https://api.example.com/data'), controller.signal);
|
|
14
|
+
* // Meanwhile, to abort in concurrent code before the above line returns: controller.abort();
|
|
15
|
+
* } catch (err) {
|
|
16
|
+
* if (err.name === 'AbortError') {
|
|
17
|
+
* console.log('The operation was aborted');
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param p - A Promise to wrap
|
|
24
|
+
* @param signal - An AbortSignal that can be used to abort the operation
|
|
25
|
+
* @returns A wrapper Promise that rejects with AbortError if the signal is aborted
|
|
26
|
+
* before the promise p settles, and settles as p settles otherwise
|
|
27
|
+
* @throws {DOMException} With name "AbortError" if aborted before p settles
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export function abortable<T>(
|
|
31
|
+
p: Promise<T>,
|
|
32
|
+
signal: AbortSignal | undefined
|
|
33
|
+
): Promise<T> {
|
|
34
|
+
let settled = false
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
signal?.addEventListener(
|
|
37
|
+
"abort",
|
|
38
|
+
() => {
|
|
39
|
+
if (!settled) {
|
|
40
|
+
reject(new DOMException("Operation aborted", "AbortError"))
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{ once: true }
|
|
44
|
+
)
|
|
45
|
+
p.then(result => {
|
|
46
|
+
resolve(result)
|
|
47
|
+
})
|
|
48
|
+
.catch(error => {
|
|
49
|
+
reject(error)
|
|
50
|
+
})
|
|
51
|
+
.finally(() => {
|
|
52
|
+
settled = true
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Include this type in an options object to pass an AbortSignal to a function.
|
|
59
|
+
*/
|
|
60
|
+
export interface AbortOptions {
|
|
61
|
+
signal?: AbortSignal
|
|
62
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const uint8ArrayFromHexString = (hexString: string): Uint8Array => {
|
|
2
|
+
if (hexString.length % 2 !== 0) {
|
|
3
|
+
throw new Error("Hex string must have an even length")
|
|
4
|
+
}
|
|
5
|
+
const bytes = new Uint8Array(hexString.length / 2)
|
|
6
|
+
for (let i = 0; i < hexString.length; i += 2) {
|
|
7
|
+
bytes[i >> 1] = parseInt(hexString.slice(i, i + 2), 16)
|
|
8
|
+
}
|
|
9
|
+
return bytes
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const uint8ArrayToHexString = (data: Uint8Array): string => {
|
|
13
|
+
return Array.from(data, byte => byte.toString(16).padStart(2, "0")).join("")
|
|
14
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Heads } from "@automerge/automerge/slim/next"
|
|
2
1
|
import { arraysAreEqual } from "./arraysAreEqual.js"
|
|
2
|
+
import type { UrlHeads } from "../types.js"
|
|
3
3
|
|
|
4
|
-
export const headsAreSame = (a:
|
|
4
|
+
export const headsAreSame = (a: UrlHeads, b: UrlHeads) => {
|
|
5
5
|
return arraysAreEqual(a, b)
|
|
6
6
|
}
|
|
@@ -49,9 +49,10 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
49
49
|
// Alice creates a document
|
|
50
50
|
const aliceHandle = aliceRepo.create<TestDoc>()
|
|
51
51
|
|
|
52
|
-
//
|
|
53
|
-
await
|
|
54
|
-
|
|
52
|
+
// TODO: ... let connections complete. this shouldn't be necessary.
|
|
53
|
+
await pause(50)
|
|
54
|
+
|
|
55
|
+
const bobHandle = await bobRepo.find<TestDoc>(aliceHandle.url)
|
|
55
56
|
|
|
56
57
|
// Alice changes the document
|
|
57
58
|
aliceHandle.change(d => {
|
|
@@ -60,7 +61,7 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
60
61
|
|
|
61
62
|
// Bob receives the change
|
|
62
63
|
await eventPromise(bobHandle, "change")
|
|
63
|
-
assert.equal((await bobHandle.doc()
|
|
64
|
+
assert.equal((await bobHandle).doc()?.foo, "bar")
|
|
64
65
|
|
|
65
66
|
// Bob changes the document
|
|
66
67
|
bobHandle.change(d => {
|
|
@@ -69,7 +70,7 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
69
70
|
|
|
70
71
|
// Alice receives the change
|
|
71
72
|
await eventPromise(aliceHandle, "change")
|
|
72
|
-
assert.equal(
|
|
73
|
+
assert.equal(aliceHandle.doc().foo, "baz")
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
// Run the test in both directions, in case they're different types of adapters
|
|
@@ -100,9 +101,9 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
100
101
|
const docUrl = aliceHandle.url
|
|
101
102
|
|
|
102
103
|
// Bob and Charlie receive the document
|
|
103
|
-
await
|
|
104
|
-
const bobHandle = bobRepo.find<TestDoc>(docUrl)
|
|
105
|
-
const charlieHandle = charlieRepo.find<TestDoc>(docUrl)
|
|
104
|
+
await pause(50)
|
|
105
|
+
const bobHandle = await bobRepo.find<TestDoc>(docUrl)
|
|
106
|
+
const charlieHandle = await charlieRepo.find<TestDoc>(docUrl)
|
|
106
107
|
|
|
107
108
|
// Alice changes the document
|
|
108
109
|
aliceHandle.change(d => {
|
|
@@ -111,8 +112,8 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
111
112
|
|
|
112
113
|
// Bob and Charlie receive the change
|
|
113
114
|
await eventPromises([bobHandle, charlieHandle], "change")
|
|
114
|
-
assert.equal(
|
|
115
|
-
assert.equal(
|
|
115
|
+
assert.equal(bobHandle.doc().foo, "bar")
|
|
116
|
+
assert.equal(charlieHandle.doc().foo, "bar")
|
|
116
117
|
|
|
117
118
|
// Charlie changes the document
|
|
118
119
|
charlieHandle.change(d => {
|
|
@@ -121,8 +122,8 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
121
122
|
|
|
122
123
|
// Alice and Bob receive the change
|
|
123
124
|
await eventPromises([aliceHandle, bobHandle], "change")
|
|
124
|
-
assert.equal(
|
|
125
|
-
assert.equal(
|
|
125
|
+
assert.equal(bobHandle.doc().foo, "baz")
|
|
126
|
+
assert.equal(charlieHandle.doc().foo, "baz")
|
|
126
127
|
|
|
127
128
|
teardown()
|
|
128
129
|
})
|
|
@@ -141,7 +142,7 @@ export function runNetworkAdapterTests(_setup: SetupFn, title?: string): void {
|
|
|
141
142
|
)
|
|
142
143
|
|
|
143
144
|
const aliceHandle = aliceRepo.create<TestDoc>()
|
|
144
|
-
const charlieHandle = charlieRepo.find(aliceHandle.url)
|
|
145
|
+
const charlieHandle = await charlieRepo.find(aliceHandle.url)
|
|
145
146
|
|
|
146
147
|
// pause to give charlie a chance to let alice know it wants the doc
|
|
147
148
|
await pause(100)
|
|
@@ -67,20 +67,16 @@ export function runStorageAdapterTests(setup: SetupFn, title?: string): void {
|
|
|
67
67
|
await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B())
|
|
68
68
|
await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C())
|
|
69
69
|
|
|
70
|
-
expect(await adapter.loadRange(["AAAAA"])).toStrictEqual(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() },
|
|
81
|
-
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() },
|
|
82
|
-
])
|
|
83
|
-
)
|
|
70
|
+
expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([
|
|
71
|
+
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() },
|
|
72
|
+
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B() },
|
|
73
|
+
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() },
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
expect(await adapter.loadRange(["AAAAA", "sync-state"])).toStrictEqual([
|
|
77
|
+
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() },
|
|
78
|
+
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() },
|
|
79
|
+
])
|
|
84
80
|
})
|
|
85
81
|
|
|
86
82
|
it("should only load values that match they key", async ({ adapter }) => {
|
|
@@ -88,16 +84,9 @@ export function runStorageAdapterTests(setup: SetupFn, title?: string): void {
|
|
|
88
84
|
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C())
|
|
89
85
|
|
|
90
86
|
const actual = await adapter.loadRange(["AAAAA"])
|
|
91
|
-
expect(actual).toStrictEqual(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
])
|
|
95
|
-
)
|
|
96
|
-
expect(actual).toStrictEqual(
|
|
97
|
-
expect.not.arrayContaining([
|
|
98
|
-
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C() },
|
|
99
|
-
])
|
|
100
|
-
)
|
|
87
|
+
expect(actual).toStrictEqual([
|
|
88
|
+
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() },
|
|
89
|
+
])
|
|
101
90
|
})
|
|
102
91
|
})
|
|
103
92
|
|
package/src/index.ts
CHANGED
|
@@ -34,6 +34,8 @@ export {
|
|
|
34
34
|
stringifyAutomergeUrl,
|
|
35
35
|
interpretAsDocumentId,
|
|
36
36
|
generateAutomergeUrl,
|
|
37
|
+
encodeHeads,
|
|
38
|
+
decodeHeads,
|
|
37
39
|
} from "./AutomergeUrl.js"
|
|
38
40
|
export { Repo } from "./Repo.js"
|
|
39
41
|
export { NetworkAdapter } from "./network/NetworkAdapter.js"
|
|
@@ -41,8 +43,7 @@ export type { NetworkAdapterInterface } from "./network/NetworkAdapterInterface.
|
|
|
41
43
|
export { isRepoMessage } from "./network/messages.js"
|
|
42
44
|
export { StorageAdapter } from "./storage/StorageAdapter.js"
|
|
43
45
|
export type { StorageAdapterInterface } from "./storage/StorageAdapterInterface.js"
|
|
44
|
-
|
|
45
|
-
export { CollectionHandle } from "./CollectionHandle.js"
|
|
46
|
+
import { next as Automerge } from "@automerge/automerge/slim"
|
|
46
47
|
|
|
47
48
|
/** @hidden **/
|
|
48
49
|
export * as cbor from "./helpers/cbor.js"
|
|
@@ -77,6 +78,11 @@ export type {
|
|
|
77
78
|
PeerMetadata,
|
|
78
79
|
} from "./network/NetworkAdapterInterface.js"
|
|
79
80
|
|
|
81
|
+
export type {
|
|
82
|
+
NetworkSubsystemEvents,
|
|
83
|
+
PeerPayload,
|
|
84
|
+
} from "./network/NetworkSubsystem.js"
|
|
85
|
+
|
|
80
86
|
export type {
|
|
81
87
|
DocumentUnavailableMessage,
|
|
82
88
|
EphemeralMessage,
|
|
@@ -96,46 +102,59 @@ export type {
|
|
|
96
102
|
|
|
97
103
|
export * from "./types.js"
|
|
98
104
|
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
105
|
+
// Automerge re-exports
|
|
106
|
+
//
|
|
107
|
+
// Note that we can't use export type { .. } from "@automerge/automerge" because we are
|
|
108
|
+
// importing automerge like this:
|
|
109
|
+
//
|
|
110
|
+
// import { next as Automerge } from "@automerge/automerge"
|
|
111
|
+
//
|
|
112
|
+
// I.e. we are using the `next` export from Automerge. Not the module itself. This is
|
|
113
|
+
// to maintain compatiblity with Automerge 3.0 and 2.0. In 2.0 we used to have a
|
|
114
|
+
// subpath export at `/next` so the re-exports looked like this:
|
|
115
|
+
//
|
|
116
|
+
// export { type .. } from "@automerge/automerge/slim/next"
|
|
117
|
+
//
|
|
118
|
+
// However, we have now removed the subpath export (and deprecated next generally)
|
|
119
|
+
// so we need to explicitly name each type we are re-exporting here.
|
|
120
|
+
export const Counter = Automerge.Counter
|
|
121
|
+
export const RawString = Automerge.RawString
|
|
122
|
+
// In automerge 3.0 RawString is renamed to ImmutableString
|
|
123
|
+
export const ImmutableString = Automerge.RawString
|
|
124
|
+
export type Counter = Automerge.Counter
|
|
125
|
+
export type Doc<T> = Automerge.Doc<T>
|
|
126
|
+
export type Heads = Automerge.Heads
|
|
127
|
+
export type Patch = Automerge.Patch
|
|
128
|
+
export type PatchCallback<T> = Automerge.PatchCallback<T>
|
|
129
|
+
export type Prop = Automerge.Prop
|
|
130
|
+
export type ActorId = Automerge.ActorId
|
|
131
|
+
export type Change = Automerge.Change
|
|
132
|
+
export type ChangeFn<T> = Automerge.ChangeFn<T>
|
|
133
|
+
export type Mark = Automerge.Mark
|
|
134
|
+
export type MarkSet = Automerge.MarkSet
|
|
135
|
+
export type MarkRange = Automerge.MarkRange
|
|
136
|
+
export type MarkValue = Automerge.MarkValue
|
|
137
|
+
export type Cursor = Automerge.Cursor
|
|
118
138
|
|
|
119
139
|
// export a few utility functions that aren't in automerge-repo
|
|
120
140
|
// NB that these should probably all just be available via the dochandle
|
|
121
|
-
export
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
getConflicts,
|
|
127
|
-
} from "@automerge/automerge/slim/next"
|
|
141
|
+
export const getChanges = Automerge.getChanges
|
|
142
|
+
export const getAllChanges = Automerge.getAllChanges
|
|
143
|
+
export const applyChanges = Automerge.applyChanges
|
|
144
|
+
export const view = Automerge.view
|
|
145
|
+
export const getConflicts = Automerge.getConflicts
|
|
128
146
|
|
|
129
147
|
// export type-specific utility functions
|
|
130
148
|
// these mostly can't be on the data-type in question because
|
|
131
149
|
// JS strings can't have methods added to them
|
|
132
|
-
export
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
export const getCursor = Automerge.getCursor
|
|
151
|
+
export const getCursorPosition = Automerge.getCursorPosition
|
|
152
|
+
export const splice = Automerge.splice
|
|
153
|
+
export const updateText = Automerge.updateText
|
|
154
|
+
export const insertAt = Automerge.insertAt
|
|
155
|
+
export const deleteAt = Automerge.deleteAt
|
|
156
|
+
export const mark = Automerge.mark
|
|
157
|
+
export const unmark = Automerge.unmark
|
|
158
|
+
export const isRawString = Automerge.isRawString
|
|
159
|
+
// In Automerge 3.0 raw string is renamed to immutable string
|
|
160
|
+
export const isImmutableString = Automerge.isRawString
|
|
@@ -38,10 +38,6 @@ export class NetworkSubsystem extends EventEmitter<NetworkSubsystemEvents> {
|
|
|
38
38
|
adapters.forEach(a => this.addNetworkAdapter(a))
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
get peers(): PeerId[] {
|
|
42
|
-
return Array.from(Object.keys(this.#adaptersByPeer)) as PeerId[]
|
|
43
|
-
}
|
|
44
|
-
|
|
45
41
|
disconnect() {
|
|
46
42
|
this.adapters.forEach(a => a.disconnect())
|
|
47
43
|
}
|
package/src/network/messages.ts
CHANGED
|
@@ -130,14 +130,6 @@ export type RepoMessage =
|
|
|
130
130
|
| DocumentUnavailableMessage
|
|
131
131
|
| RemoteSubscriptionControlMessage
|
|
132
132
|
| RemoteHeadsChanged
|
|
133
|
-
| BeelayMessage
|
|
134
|
-
|
|
135
|
-
export type BeelayMessage = {
|
|
136
|
-
type: "beelay"
|
|
137
|
-
senderId: PeerId
|
|
138
|
-
targetId: PeerId
|
|
139
|
-
message: Uint8Array
|
|
140
|
-
}
|
|
141
133
|
|
|
142
134
|
/** These are message types that are handled by the {@link CollectionSynchronizer}.*/
|
|
143
135
|
export type DocMessage =
|
|
@@ -175,11 +167,10 @@ export const isRepoMessage = (message: Message): message is RepoMessage =>
|
|
|
175
167
|
isRequestMessage(message) ||
|
|
176
168
|
isDocumentUnavailableMessage(message) ||
|
|
177
169
|
isRemoteSubscriptionControlMessage(message) ||
|
|
178
|
-
isRemoteHeadsChanged(message)
|
|
179
|
-
true
|
|
170
|
+
isRemoteHeadsChanged(message)
|
|
180
171
|
|
|
181
172
|
// prettier-ignore
|
|
182
|
-
export const isDocumentUnavailableMessage = (msg: Message): msg is DocumentUnavailableMessage =>
|
|
173
|
+
export const isDocumentUnavailableMessage = (msg: Message): msg is DocumentUnavailableMessage =>
|
|
183
174
|
msg.type === "doc-unavailable"
|
|
184
175
|
|
|
185
176
|
export const isRequestMessage = (msg: Message): msg is RequestMessage =>
|
|
@@ -34,45 +34,3 @@ export abstract class StorageAdapter implements StorageAdapterInterface {
|
|
|
34
34
|
/** Remove all values with keys that start with `keyPrefix` */
|
|
35
35
|
abstract removeRange(keyPrefix: StorageKey): Promise<void>
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
export class InMemoryStorageAdapter extends StorageAdapter {
|
|
39
|
-
#data: Record<string, Uint8Array> = {}
|
|
40
|
-
|
|
41
|
-
#keyToString(key: string[]): string {
|
|
42
|
-
return key.join(".")
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
#stringToKey(key: string): string[] {
|
|
46
|
-
return key.split(".")
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async loadRange(keyPrefix: StorageKey): Promise<Chunk[]> {
|
|
50
|
-
const range = Object.entries(this.#data)
|
|
51
|
-
.filter(([key, _]) => key.startsWith(this.#keyToString(keyPrefix)))
|
|
52
|
-
.map(([key, data]) => ({ key: this.#stringToKey(key), data }))
|
|
53
|
-
return Promise.resolve(range)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async removeRange(keyPrefix: string[]): Promise<void> {
|
|
57
|
-
Object.entries(this.#data)
|
|
58
|
-
.filter(([key, _]) => key.startsWith(this.#keyToString(keyPrefix)))
|
|
59
|
-
.forEach(([key, _]) => delete this.#data[key])
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async load(key: string[]): Promise<Uint8Array | undefined> {
|
|
63
|
-
return new Promise(resolve => resolve(this.#data[this.#keyToString(key)]))
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async save(key: string[], binary: Uint8Array) {
|
|
67
|
-
this.#data[this.#keyToString(key)] = binary
|
|
68
|
-
return Promise.resolve()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async remove(key: string[]) {
|
|
72
|
-
delete this.#data[this.#keyToString(key)]
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
keys() {
|
|
76
|
-
return Object.keys(this.#data)
|
|
77
|
-
}
|
|
78
|
-
}
|