@automerge/automerge-repo 1.0.1 → 1.0.2
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/dist/DocUrl.d.ts +1 -0
- package/dist/DocUrl.d.ts.map +1 -1
- package/dist/DocUrl.js +9 -2
- package/dist/Repo.d.ts.map +1 -1
- package/dist/Repo.js +14 -4
- package/package.json +2 -2
- package/src/DocUrl.ts +10 -2
- package/src/Repo.ts +25 -8
- package/test/Repo.test.ts +31 -0
package/dist/DocUrl.d.ts
CHANGED
|
@@ -36,5 +36,6 @@ export declare const isValidAutomergeUrl: (str: string) => str is AutomergeUrl;
|
|
|
36
36
|
export declare const generateAutomergeUrl: () => AutomergeUrl;
|
|
37
37
|
export declare const documentIdToBinary: (docId: DocumentId) => BinaryDocumentId | undefined;
|
|
38
38
|
export declare const binaryToDocumentId: (docId: BinaryDocumentId) => DocumentId;
|
|
39
|
+
export declare const parseLegacyUUID: (str: string) => AutomergeUrl | undefined;
|
|
39
40
|
export {};
|
|
40
41
|
//# sourceMappingURL=DocUrl.d.ts.map
|
package/dist/DocUrl.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DocUrl.d.ts","sourceRoot":"","sources":["../src/DocUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,YAAY,CAAA;AAInB,eAAO,MAAM,SAAS,eAAe,CAAA;AAErC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAAS,YAAY;;;CAIlD,CAAA;AAED,UAAU,4BAA4B;IACpC,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAAA;CAC1C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,oBAE/B,4BAA4B,KAAG,YAQjC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,QAAS,MAAM,wBAK9C,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,QAAO,YAGpC,CAAA;AAEJ,eAAO,MAAM,kBAAkB,UACtB,UAAU,KAChB,gBAAgB,GAAG,SACyC,CAAA;AAE/D,eAAO,MAAM,kBAAkB,UAAW,gBAAgB,KAAG,UACtB,CAAA"}
|
|
1
|
+
{"version":3,"file":"DocUrl.d.ts","sourceRoot":"","sources":["../src/DocUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,YAAY,CAAA;AAInB,eAAO,MAAM,SAAS,eAAe,CAAA;AAErC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAAS,YAAY;;;CAIlD,CAAA;AAED,UAAU,4BAA4B;IACpC,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAAA;CAC1C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,oBAE/B,4BAA4B,KAAG,YAQjC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,QAAS,MAAM,wBAK9C,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,QAAO,YAGpC,CAAA;AAEJ,eAAO,MAAM,kBAAkB,UACtB,UAAU,KAChB,gBAAgB,GAAG,SACyC,CAAA;AAE/D,eAAO,MAAM,kBAAkB,UAAW,gBAAgB,KAAG,UACtB,CAAA;AAEvC,eAAO,MAAM,eAAe,QAAS,MAAM,KAAG,YAAY,GAAG,SAM5D,CAAA"}
|
package/dist/DocUrl.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Uuid from "uuid";
|
|
2
2
|
import bs58check from "bs58check";
|
|
3
3
|
export const urlPrefix = "automerge:";
|
|
4
4
|
/**
|
|
@@ -47,10 +47,17 @@ export const isValidAutomergeUrl = (str) => {
|
|
|
47
47
|
* @returns a new Automerge URL with a random UUID documentId
|
|
48
48
|
*/
|
|
49
49
|
export const generateAutomergeUrl = () => stringifyAutomergeUrl({
|
|
50
|
-
documentId:
|
|
50
|
+
documentId: Uuid.v4(null, new Uint8Array(16)),
|
|
51
51
|
});
|
|
52
52
|
export const documentIdToBinary = (docId) => bs58check.decodeUnsafe(docId);
|
|
53
53
|
export const binaryToDocumentId = (docId) => bs58check.encode(docId);
|
|
54
|
+
export const parseLegacyUUID = (str) => {
|
|
55
|
+
if (Uuid.validate(str)) {
|
|
56
|
+
const uuid = Uuid.parse(str);
|
|
57
|
+
return stringifyAutomergeUrl({ documentId: uuid });
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
};
|
|
54
61
|
/**
|
|
55
62
|
* parts breaks up the URL into constituent pieces,
|
|
56
63
|
* eventually this could include things like heads, so we use this structure
|
package/dist/Repo.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Repo.d.ts","sourceRoot":"","sources":["../src/Repo.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAAE,KAAK,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"Repo.d.ts","sourceRoot":"","sources":["../src/Repo.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAAE,KAAK,YAAY,EAAE,UAAU,EAAE,MAAM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,KAAK,IAAI,CAAA;CAC7D;AAED,UAAU,eAAe;IACvB,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAA;IACtB,KAAK,EAAE,OAAO,CAAA;CACf;AAED,UAAU,qBAAqB;IAC7B,UAAU,EAAE,UAAU,CAAA;CACvB"}
|
package/dist/Repo.js
CHANGED
|
@@ -2,7 +2,7 @@ import debug from "debug";
|
|
|
2
2
|
import { NetworkSubsystem } from "./network/NetworkSubsystem.js";
|
|
3
3
|
import { StorageSubsystem } from "./storage/StorageSubsystem.js";
|
|
4
4
|
import { CollectionSynchronizer } from "./synchronizer/CollectionSynchronizer.js";
|
|
5
|
-
import { parseAutomergeUrl, generateAutomergeUrl, isValidAutomergeUrl } from "./DocUrl.js";
|
|
5
|
+
import { parseAutomergeUrl, generateAutomergeUrl, isValidAutomergeUrl, parseLegacyUUID, } from "./DocUrl.js";
|
|
6
6
|
import { DocHandle } from "./DocHandle.js";
|
|
7
7
|
import { EventEmitter } from "eventemitter3";
|
|
8
8
|
/** A Repo is a collection of documents with networking, syncing, and storage capabilities. */
|
|
@@ -49,9 +49,12 @@ export class Repo extends EventEmitter {
|
|
|
49
49
|
}
|
|
50
50
|
else {
|
|
51
51
|
handle.awaitNetwork();
|
|
52
|
-
this.networkSubsystem
|
|
52
|
+
this.networkSubsystem
|
|
53
|
+
.whenReady()
|
|
54
|
+
.then(() => {
|
|
53
55
|
handle.networkReady();
|
|
54
|
-
})
|
|
56
|
+
})
|
|
57
|
+
.catch(err => {
|
|
55
58
|
this.#log("error waiting for network", { err });
|
|
56
59
|
});
|
|
57
60
|
}
|
|
@@ -151,7 +154,14 @@ export class Repo extends EventEmitter {
|
|
|
151
154
|
/** The documentId of the handle to retrieve */
|
|
152
155
|
automergeUrl) {
|
|
153
156
|
if (!isValidAutomergeUrl(automergeUrl)) {
|
|
154
|
-
|
|
157
|
+
let maybeAutomergeUrl = parseLegacyUUID(automergeUrl);
|
|
158
|
+
if (maybeAutomergeUrl) {
|
|
159
|
+
console.warn("Legacy UUID document ID detected, converting to AutomergeUrl. This will be removed in a future version.");
|
|
160
|
+
automergeUrl = maybeAutomergeUrl;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
throw new Error(`Invalid AutomergeUrl: '${automergeUrl}'`);
|
|
164
|
+
}
|
|
155
165
|
}
|
|
156
166
|
const { documentId } = parseAutomergeUrl(automergeUrl);
|
|
157
167
|
// If we have the handle cached, return it
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A repository object to manage a collection of automerge documents",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo",
|
|
6
6
|
"author": "Peter van Hardenberg <pvh@pvh.ca>",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"publishConfig": {
|
|
66
66
|
"access": "public"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "c72b9ec33c3a1f7f39ad20cb4fa0d766afe42158"
|
|
69
69
|
}
|
package/src/DocUrl.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
type BinaryDocumentId,
|
|
4
4
|
type DocumentId,
|
|
5
5
|
} from "./types.js"
|
|
6
|
-
import
|
|
6
|
+
import * as Uuid from "uuid"
|
|
7
7
|
import bs58check from "bs58check"
|
|
8
8
|
|
|
9
9
|
export const urlPrefix = "automerge:"
|
|
@@ -63,7 +63,7 @@ export const isValidAutomergeUrl = (str: string): str is AutomergeUrl => {
|
|
|
63
63
|
*/
|
|
64
64
|
export const generateAutomergeUrl = (): AutomergeUrl =>
|
|
65
65
|
stringifyAutomergeUrl({
|
|
66
|
-
documentId:
|
|
66
|
+
documentId: Uuid.v4(null, new Uint8Array(16)) as BinaryDocumentId,
|
|
67
67
|
})
|
|
68
68
|
|
|
69
69
|
export const documentIdToBinary = (
|
|
@@ -74,6 +74,14 @@ export const documentIdToBinary = (
|
|
|
74
74
|
export const binaryToDocumentId = (docId: BinaryDocumentId): DocumentId =>
|
|
75
75
|
bs58check.encode(docId) as DocumentId
|
|
76
76
|
|
|
77
|
+
export const parseLegacyUUID = (str: string): AutomergeUrl | undefined => {
|
|
78
|
+
if (Uuid.validate(str)) {
|
|
79
|
+
const uuid = Uuid.parse(str) as BinaryDocumentId
|
|
80
|
+
return stringifyAutomergeUrl({ documentId: uuid })
|
|
81
|
+
}
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
77
85
|
/**
|
|
78
86
|
* parts breaks up the URL into constituent pieces,
|
|
79
87
|
* eventually this could include things like heads, so we use this structure
|
package/src/Repo.ts
CHANGED
|
@@ -6,10 +6,16 @@ import { StorageSubsystem } from "./storage/StorageSubsystem.js"
|
|
|
6
6
|
import { CollectionSynchronizer } from "./synchronizer/CollectionSynchronizer.js"
|
|
7
7
|
import { type AutomergeUrl, DocumentId, PeerId } from "./types.js"
|
|
8
8
|
import { v4 as uuid } from "uuid"
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
parseAutomergeUrl,
|
|
11
|
+
generateAutomergeUrl,
|
|
12
|
+
isValidAutomergeUrl,
|
|
13
|
+
parseLegacyUUID,
|
|
14
|
+
} from "./DocUrl.js"
|
|
10
15
|
|
|
11
16
|
import { DocHandle } from "./DocHandle.js"
|
|
12
17
|
import { EventEmitter } from "eventemitter3"
|
|
18
|
+
import bs58check from "bs58check"
|
|
13
19
|
|
|
14
20
|
/** A Repo is a collection of documents with networking, syncing, and storage capabilities. */
|
|
15
21
|
export class Repo extends EventEmitter<DocCollectionEvents> {
|
|
@@ -61,11 +67,14 @@ export class Repo extends EventEmitter<DocCollectionEvents> {
|
|
|
61
67
|
handle.request()
|
|
62
68
|
} else {
|
|
63
69
|
handle.awaitNetwork()
|
|
64
|
-
this.networkSubsystem
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
this.networkSubsystem
|
|
71
|
+
.whenReady()
|
|
72
|
+
.then(() => {
|
|
73
|
+
handle.networkReady()
|
|
74
|
+
})
|
|
75
|
+
.catch(err => {
|
|
76
|
+
this.#log("error waiting for network", { err })
|
|
77
|
+
})
|
|
69
78
|
}
|
|
70
79
|
|
|
71
80
|
// Register the document with the synchronizer. This advertises our interest in the document.
|
|
@@ -120,7 +129,7 @@ export class Repo extends EventEmitter<DocCollectionEvents> {
|
|
|
120
129
|
})
|
|
121
130
|
}
|
|
122
131
|
|
|
123
|
-
|
|
132
|
+
/** Returns an existing handle if we have it; creates one otherwise. */
|
|
124
133
|
#getHandle<T>(
|
|
125
134
|
/** The documentId of the handle to look up or create */
|
|
126
135
|
documentId: DocumentId,
|
|
@@ -183,7 +192,15 @@ export class Repo extends EventEmitter<DocCollectionEvents> {
|
|
|
183
192
|
automergeUrl: AutomergeUrl
|
|
184
193
|
): DocHandle<T> {
|
|
185
194
|
if (!isValidAutomergeUrl(automergeUrl)) {
|
|
186
|
-
|
|
195
|
+
let maybeAutomergeUrl = parseLegacyUUID(automergeUrl)
|
|
196
|
+
if (maybeAutomergeUrl) {
|
|
197
|
+
console.warn(
|
|
198
|
+
"Legacy UUID document ID detected, converting to AutomergeUrl. This will be removed in a future version."
|
|
199
|
+
)
|
|
200
|
+
automergeUrl = maybeAutomergeUrl
|
|
201
|
+
} else {
|
|
202
|
+
throw new Error(`Invalid AutomergeUrl: '${automergeUrl}'`)
|
|
203
|
+
}
|
|
187
204
|
}
|
|
188
205
|
|
|
189
206
|
const { documentId } = parseAutomergeUrl(automergeUrl)
|
package/test/Repo.test.ts
CHANGED
|
@@ -22,6 +22,9 @@ import {
|
|
|
22
22
|
generateLargeObject,
|
|
23
23
|
LargeObject,
|
|
24
24
|
} from "./helpers/generate-large-object.js"
|
|
25
|
+
import { parseAutomergeUrl } from "../dist/DocUrl.js"
|
|
26
|
+
|
|
27
|
+
import * as Uuid from "uuid"
|
|
25
28
|
|
|
26
29
|
describe("Repo", () => {
|
|
27
30
|
describe("single repo", () => {
|
|
@@ -50,6 +53,34 @@ describe("Repo", () => {
|
|
|
50
53
|
assert.equal(handle.isReady(), true)
|
|
51
54
|
})
|
|
52
55
|
|
|
56
|
+
it("can find a document once it's created", () => {
|
|
57
|
+
const { repo } = setup()
|
|
58
|
+
const handle = repo.create<TestDoc>()
|
|
59
|
+
handle.change((d: TestDoc) => {
|
|
60
|
+
d.foo = "bar"
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const handle2 = repo.find(handle.url)
|
|
64
|
+
assert.equal(handle, handle2)
|
|
65
|
+
assert.deepEqual(handle2.docSync(), { foo: "bar" })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it("can find a document using a legacy UUID (for now)", () => {
|
|
69
|
+
const { repo } = setup()
|
|
70
|
+
const handle = repo.create<TestDoc>()
|
|
71
|
+
handle.change((d: TestDoc) => {
|
|
72
|
+
d.foo = "bar"
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const url = handle.url
|
|
76
|
+
const { binaryDocumentId } = parseAutomergeUrl(url)
|
|
77
|
+
const legacyDocumentId = Uuid.stringify(binaryDocumentId) as AutomergeUrl // a white lie
|
|
78
|
+
|
|
79
|
+
const handle2 = repo.find(legacyDocumentId)
|
|
80
|
+
assert.equal(handle, handle2)
|
|
81
|
+
assert.deepEqual(handle2.docSync(), { foo: "bar" })
|
|
82
|
+
})
|
|
83
|
+
|
|
53
84
|
it("can change a document", async () => {
|
|
54
85
|
const { repo } = setup()
|
|
55
86
|
const handle = repo.create<TestDoc>()
|