From 89dc470e845133aac6a45a888646b92b458ffbff Mon Sep 17 00:00:00 2001 From: m0ar Date: Mon, 5 Feb 2024 12:48:14 +0100 Subject: [PATCH] Add pidFromStringID function to interface easier with resolve --- .github/workflows/publish.yml | 16 ++++++++++++++-- packages/lib/package.json | 5 +++-- packages/lib/src/resolve.ts | 26 ++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 27f5093..2ae605b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -15,10 +15,22 @@ jobs: check-latest: false registry-url: 'https://registry.npmjs.org' - run: npm ci - - run: npm --workspace packages/composedb publish --access public + - run: | + if ! npm --workspace packages/composedb view .@${npm_package_version} > /dev/null; then + echo "New version of composedb package; running publish..." + npm --workspace packages/composedb publish --access public + else + echo "This version of the composedb package already exists on npm; doing nothing." + fi env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - run: npm --workspace packages/lib run build - - run: npm --workspace packages/lib publish --access public + - run: | + if ! npm --workspace packages/lib view .@${npm_package_version} > /dev/null; then + echo "New version of lib package; running publish..." + npm --workspace packages/lib publish --access public + else + echo "This version of the lib package already exists on npm; doing nothing." + fi env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/packages/lib/package.json b/packages/lib/package.json index f46fbed..9e7b27b 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -1,6 +1,6 @@ { "name": "@desci-labs/desci-codex-lib", - "version": "1.0.4", + "version": "1.0.6", "description": "Codex interaction primitives", "license": "MIT", "author": "Edvard Hübinette", @@ -10,7 +10,8 @@ "scripts": { "build": "rm -rf ./dist && tsc --project tsconfig.build.json", "populate": "node --no-warnings=ExperimentalWarning --loader ts-node/esm scripts/populate.ts", - "test": "export ADMIN_SEED=$(<../composedb/admin_seed.txt) && vitest --run --config vitest.config.ts" + "test": "export ADMIN_SEED=$(<../composedb/admin_seed.txt) && vitest --run --config vitest.config.ts", + "doPublish": "npm run build && cd ../.. && npm publish --workspace packages/lib --access public --dry-run" }, "devDependencies": { "ts-node": "^10.9.1", diff --git a/packages/lib/src/resolve.ts b/packages/lib/src/resolve.ts index 01dc146..e020aec 100644 --- a/packages/lib/src/resolve.ts +++ b/packages/lib/src/resolve.ts @@ -118,13 +118,31 @@ export const resolveState = async ( * necessary to discern between multiple updates under the same anchor. * * @param client - A Ceramic client instance. - * @param pid - The address of a node state. + * @param id - The address of a node state. * @param includeAnchors - Optionally include anchor commits in the log. * @returns an array of the historical events of the stream. */ export const resolveHistory = async ( client: CeramicClient, - pid: RootNode, + id: StreamID | string, includeAnchors: boolean = false, -): Promise => - await loadID(client, pid.id).then((s) => getVersionLog(s, includeAnchors)); +): Promise => { + if (typeof id === "string") { + id = StreamID.fromString(id); + } + return await loadID(client, id).then((s) => getVersionLog(s, includeAnchors)); +}; + +export const pidFromStringID = (stringID: string): RootNode | VersionedNode => { + const commit = CommitID.fromStringNoThrow(stringID); + if (commit instanceof CommitID) { + return { tag: "versioned", id: commit }; + } + + const stream = StreamID.fromStringNoThrow(stringID); + if (stream instanceof StreamID) { + return { tag: "root", id: stream }; + } + + throw new Error("Passed string is neither a stream nor a commit ID"); +};