diff --git a/packages/automerge-repo/src/Repo.ts b/packages/automerge-repo/src/Repo.ts index 4b068616e..623bc84d3 100644 --- a/packages/automerge-repo/src/Repo.ts +++ b/packages/automerge-repo/src/Repo.ts @@ -1,20 +1,19 @@ import { next as Automerge } from "@automerge/automerge" import debug from "debug" import { EventEmitter } from "eventemitter3" -import { DocHandle, DocHandleEncodedChangePayload } from "./DocHandle.js" import { generateAutomergeUrl, - isValidAutomergeUrl, + interpretAsDocumentId, parseAutomergeUrl, - parseLegacyUUID, } from "./AutomergeUrl.js" +import { DocHandle, DocHandleEncodedChangePayload } from "./DocHandle.js" import { throttle } from "./helpers/throttle.js" import { NetworkAdapter } from "./network/NetworkAdapter.js" import { NetworkSubsystem } from "./network/NetworkSubsystem.js" import { StorageAdapter } from "./storage/StorageAdapter.js" import { StorageSubsystem } from "./storage/StorageSubsystem.js" import { CollectionSynchronizer } from "./synchronizer/CollectionSynchronizer.js" -import { DocumentId, PeerId, type AutomergeUrl } from "./types.js" +import type { AnyDocumentId, DocumentId, PeerId } from "./types.js" /** A Repo is a collection of documents with networking, syncing, and storage capabilities. */ /** The `Repo` is the main entry point of this library @@ -247,22 +246,11 @@ export class Repo extends EventEmitter { * event to advertise interest in the document. */ find( - /** The documentId of the handle to retrieve */ - automergeUrl: AutomergeUrl + /** The url or documentId of the handle to retrieve */ + id: AnyDocumentId ): DocHandle { - if (!isValidAutomergeUrl(automergeUrl)) { - const maybeAutomergeUrl = parseLegacyUUID(automergeUrl) - if (maybeAutomergeUrl) { - console.warn( - "Legacy UUID document ID detected, converting to AutomergeUrl. This will be removed in a future version." - ) - automergeUrl = maybeAutomergeUrl - } else { - throw new Error(`Invalid AutomergeUrl: '${automergeUrl}'`) - } - } + const documentId = interpretAsDocumentId(id) - const { documentId } = parseAutomergeUrl(automergeUrl) // If we have the handle cached, return it if (this.#handleCache[documentId]) { if (this.#handleCache[documentId].isUnavailable()) { @@ -282,16 +270,16 @@ export class Repo extends EventEmitter { } delete( - /** The documentId of the handle to delete */ - id: DocumentId | AutomergeUrl + /** The url or documentId of the handle to delete */ + id: AnyDocumentId ) { - if (isValidAutomergeUrl(id)) id = parseAutomergeUrl(id).documentId + const documentId = interpretAsDocumentId(id) - const handle = this.#getHandle(id, false) + const handle = this.#getHandle(documentId, false) handle.delete() - delete this.#handleCache[id] - this.emit("delete-document", { documentId: id }) + delete this.#handleCache[documentId] + this.emit("delete-document", { documentId }) } } diff --git a/packages/automerge-repo/test/Repo.test.ts b/packages/automerge-repo/test/Repo.test.ts index d8e82e521..470827eff 100644 --- a/packages/automerge-repo/test/Repo.test.ts +++ b/packages/automerge-repo/test/Repo.test.ts @@ -15,6 +15,7 @@ import { AutomergeUrl, DocHandle, DocumentId, + LegacyDocumentId, PeerId, SharePolicy, } from "../src/index.js" @@ -54,7 +55,7 @@ describe("Repo", () => { assert.equal(handle.isReady(), true) }) - it("can find a document once it's created", () => { + it("can find a document by url", () => { const { repo } = setup() const handle = repo.create() handle.change((d: TestDoc) => { @@ -66,7 +67,19 @@ describe("Repo", () => { assert.deepEqual(handle2.docSync(), { foo: "bar" }) }) - it("can find a document using a legacy UUID (for now)", () => { + it("can find a document by its unprefixed document ID", () => { + const { repo } = setup() + const handle = repo.create() + handle.change((d: TestDoc) => { + d.foo = "bar" + }) + + const handle2 = repo.find(handle.documentId) + assert.equal(handle, handle2) + assert.deepEqual(handle2.docSync(), { foo: "bar" }) + }) + + it("can find a document by legacy UUID (for now)", () => { disableConsoleWarn() const { repo } = setup() @@ -77,9 +90,9 @@ describe("Repo", () => { const url = handle.url const { binaryDocumentId } = parseAutomergeUrl(url) - const legacyDocumentId = Uuid.stringify(binaryDocumentId) as AutomergeUrl // a white lie + const legacyDocId = Uuid.stringify(binaryDocumentId) as LegacyDocumentId - const handle2 = repo.find(legacyDocumentId) + const handle2 = repo.find(legacyDocId) assert.equal(handle, handle2) assert.deepEqual(handle2.docSync(), { foo: "bar" })