From 28c9acc9f4dcfbfb620c493a0c23749a0a25ac02 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 12:03:51 +1100 Subject: [PATCH 1/6] build!: move ldacapi's OCFL code to @ocfl/ocfl-fs 1.0 beta Opens the repository with openStorageRoot/createStorageRoot, imports through create then update, and reads files from the head. OCFL_SCRATCH is gone: the workspace is inside the repository. File rows no longer carry a CRC32, which nothing read and 1.0 doesn't record. Crates and repository listings no longer carry a disk path. The storage root is opened in its own module so the scripts don't load arocapi, whose OCFL backend still needs 0.4. --- .env.example | 3 - README.md | 19 +++---- deploy/.env.example | 4 +- deploy/compose.yaml | 1 - package.json | 3 +- pnpm-lock.yaml | 41 +++----------- scripts/crates-import.ts | 30 ++++++++-- scripts/ocfl-init.ts | 4 +- src/configuration.test.ts | 7 +-- src/configuration.ts | 4 +- src/indexer/database.test.ts | 3 +- src/indexer/database.ts | 2 +- src/indexer/indexer.ts | 13 ++--- src/indexer/search.test.ts | 2 +- src/ocfl-modules.d.ts | 40 -------------- src/ocfl-repository.ts | 28 ++++++++++ src/ocfl.ts | 104 +++++++++++------------------------ src/repository.ts | 2 +- test/admin.ts | 2 +- 19 files changed, 122 insertions(+), 190 deletions(-) delete mode 100644 src/ocfl-modules.d.ts create mode 100644 src/ocfl-repository.ts diff --git a/.env.example b/.env.example index 634a80b..e7c1aa3 100644 --- a/.env.example +++ b/.env.example @@ -8,9 +8,6 @@ # OPENSEARCH_URL=http://localhost:9200 # OCFL_ROOT=storage/ocfl -# Working directory for OCFL writes. Defaults to `scratch` beside OCFL_ROOT -# OCFL_SCRATCH=storage/scratch - # LDACAPI_PORT=8080 # LDACAPI_HOST=localhost # LDACAPI_MAX_PARAM_LENGTH=500 diff --git a/README.md b/README.md index d551be0..fb86f33 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,6 @@ set. | `DATABASE_URL` | `postgresql://ldacapi:ldacapi@localhost:5432/ldacapi` | | `OPENSEARCH_URL` | `http://localhost:9200` | | `OCFL_ROOT` | `storage/ocfl` | -| `OCFL_SCRATCH` | `scratch` beside `OCFL_ROOT` | | `LDACAPI_PORT` | `8080` | | `LDACAPI_HOST` | `localhost` | | `LDACAPI_MAX_PARAM_LENGTH` | `500` | @@ -352,11 +351,10 @@ docker run --rm ldacapi pnpm generate-keys >> ldacapi.env ### Storage The image has no default `OCFL_ROOT`. Mount your storage and set `OCFL_ROOT` -(and `OCFL_SCRATCH`, if you need it elsewhere) to paths inside the container -(above, `OCFL_ROOT=/storage/ocfl`). The API runs as the -image's `node` user, uid 1000, so the OCFL repository must be readable by uid -1000 and the scratch directory writable by it. `pnpm crates:import` writes to the -repository, so give it write access too when you run it. +to a path inside the container (above, `OCFL_ROOT=/storage/ocfl`). The API runs +as the image's `node` user, uid 1000, so the OCFL repository must be readable by +uid 1000. `pnpm crates:import` writes to the repository, so give it write access +too when you run it. ## Production with Docker Compose @@ -399,10 +397,11 @@ sudo sysctl --system ``` Storage is a directory on the host, usually an NFS mount, set by `STORAGE_DIR` -in `.env`. The OCFL repository is its `ocfl` directory, and imports are staged -in its `scratch` directory. Both must be in the one mount: OCFL commits an -import by renaming it from scratch into the repository, and a rename can't -cross mounts. The API runs as uid 1000, so the directory must be writable by +in `.env`. The OCFL repository is its `ocfl` directory. Imports are staged +inside the repository, in `extensions/ocfl-js-workspace`, and committed by +renaming them into place, so the repository must be on one filesystem, with no +mount point inside it, and that filesystem must support hard links, as NFS +does. The API runs as uid 1000, so the directory must be writable by uid 1000: on NFS, set the owner on the server or map the export to uid 1000, as root in the container is squashed. Otherwise `pnpm ocfl:init` fails with `EACCES: permission denied, mkdir '/storage/ocfl'`. Serving and indexing only diff --git a/deploy/.env.example b/deploy/.env.example index c059498..4e0b6b1 100644 --- a/deploy/.env.example +++ b/deploy/.env.example @@ -8,8 +8,8 @@ ACME_EMAIL=ops@example.org # Generate with: openssl rand -hex 32 POSTGRES_PASSWORD= -# The directory holding the OCFL repository (its `ocfl` directory) and scratch (`scratch`), -# e.g. an NFS mount. It must be writable by uid 1000 to import crates. +# The directory holding the OCFL repository (its `ocfl` directory), e.g. an NFS mount. +# It must be writable by uid 1000 to import crates. STORAGE_DIR=/mnt/ldacapi # OpenSearch's JVM heap; about half the memory you can give it diff --git a/deploy/compose.yaml b/deploy/compose.yaml index 642cdc8..2d0782f 100644 --- a/deploy/compose.yaml +++ b/deploy/compose.yaml @@ -58,7 +58,6 @@ services: DATABASE_URL: postgresql://ldacapi:${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}@db:5432/ldacapi OPENSEARCH_URL: http://opensearch:9200 OCFL_ROOT: /storage/ocfl - OCFL_SCRATCH: /storage/scratch OIDC_ISSUER: https://${DOMAIN:?Set DOMAIN in .env} ADMIN_ORIGIN: https://${ADMIN_DOMAIN:?Set ADMIN_DOMAIN in .env} OIDC_CLIENTS: >- diff --git a/package.json b/package.json index 9e3f74a..ead1c53 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "@fastify/sensible": "^6.0.7", "@fastify/static": "^10.1.7", "@fastify/view": "^12.0.1", - "@ocfl/ocfl-fs": "^0.4.0", + "@ocfl/ocfl-fs": "^1.0.0-beta.58", "@opensearch-project/opensearch": "^3.9.0", "@prisma/adapter-pg": "^7.10.1", "@prisma/client": "^7.10.1", @@ -73,7 +73,6 @@ "@biomejs/biome": "^2.5.15", "@commitlint/cli": "^21.2.4", "@commitlint/config-conventional": "^21.2.4", - "@ocfl/ocfl": "^0.4.0", "@tailwindcss/cli": "^4.3.3", "@types/node": "^26.6.6", "@types/oidc-provider": "^9.12.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 79bb17c..99a8ca8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -185,8 +185,8 @@ importers: specifier: ^12.0.1 version: 12.0.1 '@ocfl/ocfl-fs': - specifier: ^0.4.0 - version: 0.4.0 + specifier: ^1.0.0-beta.58 + version: 1.0.0-beta.58 '@opensearch-project/opensearch': specifier: ^3.9.0 version: 3.9.0 @@ -198,7 +198,7 @@ importers: version: 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) arocapi: specifier: 5.0.0-beta.9 - version: 5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@0.4.0)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) + version: 5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.58)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) eta: specifier: ^4.6.0 version: 4.6.0 @@ -236,9 +236,6 @@ importers: '@commitlint/config-conventional': specifier: ^21.2.4 version: 21.2.4 - '@ocfl/ocfl': - specifier: ^0.4.0 - version: 0.4.0 '@tailwindcss/cli': specifier: ^4.3.3 version: 4.3.3 @@ -518,12 +515,9 @@ packages: '@emnapi/core': ^1.7.1 || ^2.0.0-alpha.4 '@emnapi/runtime': ^1.7.1 || ^2.0.0-alpha.4 - '@ocfl/ocfl-fs@0.4.0': - resolution: {integrity: sha512-eugqs7N5J7OdvMorMLji60DQfORRs2Ef39SITuVp0o80ZKdcuEctltl+OosaHY/rA1YJdcXgwSfGGWjsLtg0pw==} - - '@ocfl/ocfl@0.4.0': - resolution: {integrity: sha512-c9LpGa/V+iYYNmLVIYF1SThx9SPMohe1Y8QsTeyKIyBQAOgMzb45my3HxVkTBA+Gkh+hRglx9adu2Pv8+F8RAg==} - engines: {node: '>=18.0.0'} + '@ocfl/ocfl-fs@1.0.0-beta.58': + resolution: {integrity: sha512-+dbRKEQmpZNr/C2VjHyXciktmBLOr+V0q65PH2Gknqlh135gU8HXP1UFBBH2koBmjNfSo6hwTE9ro+VV9gcfgQ==} + engines: {node: '>=22.12.0'} '@opensearch-project/opensearch@3.9.0': resolution: {integrity: sha512-ksIEi0wyIjVncfRwaxt5v6H4qL1aQO2eE3MxKyXGCLnR70YTSCDp+JGhdx2LpyMjtCFvr71DWtYQg9SK3pcNnA==} @@ -2033,9 +2027,6 @@ packages: graphmatch@1.1.1: resolution: {integrity: sha512-5ykVn/EXM1hF0XCaWh05VbYvEiOL2lY1kBxZtaYsyvjp7cmWOU1XsAdfQBwClraEofXDT197lFbXOEVMHpvQOg==} - hash-wasm@4.12.0: - resolution: {integrity: sha512-+/2B2rYLb48I/evdOIhP+K/DD2ca2fgBjp6O+GBEnCDk2e4rpeXIK8GvIyRPjTezgmWn9gmKwkQjjx6BtqDHVQ==} - help-me@5.0.0: resolution: {integrity: sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg==} @@ -2110,9 +2101,6 @@ packages: resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==} engines: {node: '>=10'} - js-sha512@0.9.0: - resolution: {integrity: sha512-mirki9WS/SUahm+1TbAPkqvbCiCfOAAsyXeHxK1UkullnJVVqoJG2pL9ObvT05CN+tM7fxhfYm0NbXn+1hWoZg==} - js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} @@ -3356,14 +3344,7 @@ snapshots: '@tybys/wasm-util': 0.10.4 optional: true - '@ocfl/ocfl-fs@0.4.0': - dependencies: - '@ocfl/ocfl': 0.4.0 - - '@ocfl/ocfl@0.4.0': - dependencies: - hash-wasm: 4.12.0 - js-sha512: 0.9.0 + '@ocfl/ocfl-fs@1.0.0-beta.58': {} '@opensearch-project/opensearch@3.9.0': dependencies: @@ -4156,7 +4137,7 @@ snapshots: argue-cli@3.2.0: {} - arocapi@5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@0.4.0)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): + arocapi@5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.58)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): dependencies: '@fastify/sensible': 6.0.7 '@prisma/client': 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) @@ -4165,7 +4146,7 @@ snapshots: fastify-type-provider-zod: 7.0.0(@fastify/swagger@9.9.1)(fastify@5.12.7)(openapi-types@12.1.3)(zod@4.6.7) zod: 4.6.7 optionalDependencies: - '@ocfl/ocfl-fs': 0.4.0 + '@ocfl/ocfl-fs': 1.0.0-beta.58 '@opensearch-project/opensearch': 3.9.0 transitivePeerDependencies: - '@fastify/swagger' @@ -4615,8 +4596,6 @@ snapshots: graphmatch@1.1.1: {} - hash-wasm@4.12.0: {} - help-me@5.0.0: {} http-assert@1.5.0: @@ -4679,8 +4658,6 @@ snapshots: joycon@3.1.1: {} - js-sha512@0.9.0: {} - js-tokens@4.0.0: {} js-yaml@4.3.2: diff --git a/scripts/crates-import.ts b/scripts/crates-import.ts index 1551e0b..b699609 100644 --- a/scripts/crates-import.ts +++ b/scripts/crates-import.ts @@ -1,19 +1,41 @@ import { readdir, readFile } from 'node:fs/promises'; import { join, resolve } from 'node:path'; +import { OcflError, type StorageRoot, type VersionDetails } from '@ocfl/ocfl-fs'; import { ROCrate } from 'ro-crate'; import { config } from '../src/configuration.ts'; import { logger } from '../src/logger.ts'; -import { openOrCreateStorage } from '../src/ocfl.ts'; +import { openOrCreateRepository } from '../src/ocfl-repository.ts'; const source = resolve(process.argv[2] ?? 'test-data'); -const repository = await openOrCreateStorage(); +/** Merges the crate at `path` into its OCFL object, creating the object on first import */ +async function importCrate(repository: StorageRoot, crateId: string, path: string) { + const details: VersionDetails = { message: `Imported from ${path}`, user: { name: 'ldacapi crates:import' } }; + try { + return await repository.create(crateId, details, (draft) => draft.import(path)); + } catch (error) { + if (!(error instanceof OcflError && error.code === 'OBJECT_EXISTS')) { + throw error; + } + } + const object = await repository.open(crateId); + return object.update(details, (draft) => draft.import(path)); +} + +const repository = await openOrCreateRepository(); const directories = await readdir(source, { withFileTypes: true }); for (const directory of directories.filter((entry) => entry.isDirectory())) { const path = join(source, directory.name); const crate = await ROCrate.create(JSON.parse(await readFile(join(path, 'ro-crate-metadata.json'), 'utf-8'))); - await repository.object(crate.rootId).import(path); - logger.info({ crateId: crate.rootId, path }, 'Imported crate'); + try { + const object = await importCrate(repository, crate.rootId, path); + logger.info({ crateId: crate.rootId, path, version: object.head.number }, 'Imported crate'); + } catch (error) { + // A crate another writer holds, or one that needs recovery, is left for a person; the rest still import + const lockedAt = error instanceof OcflError ? error.lockedAt : undefined; + logger.error({ err: error, crateId: crate.rootId, path, lockedAt }, 'Cannot import crate'); + process.exitCode = 1; + } } logger.info({ root: config.ocfl.root }, 'Imported crates into the OCFL repository'); diff --git a/scripts/ocfl-init.ts b/scripts/ocfl-init.ts index d3e783d..22b1fb2 100644 --- a/scripts/ocfl-init.ts +++ b/scripts/ocfl-init.ts @@ -1,6 +1,6 @@ import { config } from '../src/configuration.ts'; import { logger } from '../src/logger.ts'; -import { openOrCreateStorage } from '../src/ocfl.ts'; +import { openOrCreateRepository } from '../src/ocfl-repository.ts'; -await openOrCreateStorage(); +await openOrCreateRepository(); logger.info({ root: config.ocfl.root }, 'The OCFL repository is ready'); diff --git a/src/configuration.test.ts b/src/configuration.test.ts index 174a3da..8853194 100644 --- a/src/configuration.test.ts +++ b/src/configuration.test.ts @@ -31,7 +31,7 @@ describe('loadConfig', () => { expect(config.databaseUrl).toBe('postgresql://ldacapi:ldacapi@localhost:5432/ldacapi'); expect(config.opensearchUrl).toBe('http://localhost:9200'); expect(config.port).toBe(8080); - expect(config.ocfl).toEqual({ root: resolve('storage/ocfl'), scratch: resolve('storage/scratch') }); + expect(config.ocfl).toEqual({ root: resolve('storage/ocfl') }); }); it('uses the test database and index, and logs nothing, in test', () => { @@ -49,11 +49,6 @@ describe('loadConfig', () => { expect(config.search.entityIndex).toBe('other'); }); - it('puts OCFL scratch beside the OCFL root unless set', () => { - expect(loadConfig({ OCFL_ROOT: '/data/ocfl' }).ocfl).toEqual({ root: '/data/ocfl', scratch: '/data/scratch' }); - expect(loadConfig({ OCFL_ROOT: '/data/ocfl', OCFL_SCRATCH: '/tmp/ocfl' }).ocfl.scratch).toBe('/tmp/ocfl'); - }); - it('treats empty values as unset', () => { expect(loadConfig({ OPENSEARCH_URL: '' }).opensearchUrl).toBe('http://localhost:9200'); }); diff --git a/src/configuration.ts b/src/configuration.ts index 8733b9d..5010a40 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -1,5 +1,5 @@ import { createPrivateKey, createPublicKey, sign, verify } from 'node:crypto'; -import { dirname, join, resolve } from 'node:path'; +import { resolve } from 'node:path'; import * as z from 'zod'; import { generateCookieKey, generateSigningKey, type Jwk, SIGNING_ALG } from './oidc/keys.ts'; @@ -118,7 +118,6 @@ export function loadConfig(processEnv: Env) { DATABASE_URL: devDefault(`postgresql://ldacapi:ldacapi@localhost:5432/ldacapi${nodeEnv === 'test' ? '_test' : ''}`), OPENSEARCH_URL: devDefault('http://localhost:9200'), OCFL_ROOT: devDefault('storage/ocfl'), - OCFL_SCRATCH: z.string().optional(), LDACAPI_PORT: z.coerce.number().int().default(8080), LDACAPI_HOST: z.string().default('localhost'), LDACAPI_MAX_PARAM_LENGTH: z.coerce.number().int().default(500), @@ -193,7 +192,6 @@ export function loadConfig(processEnv: Env) { maxParamLength: env.LDACAPI_MAX_PARAM_LENGTH, ocfl: { root: resolve(env.OCFL_ROOT), - scratch: resolve(env.OCFL_SCRATCH ?? join(dirname(env.OCFL_ROOT), 'scratch')), }, defaultLicense: env.DEFAULT_LICENSE, defaultMetadataLicense: env.DEFAULT_METADATA_LICENSE, diff --git a/src/indexer/database.test.ts b/src/indexer/database.test.ts index bb3458e..6f76f1c 100644 --- a/src/indexer/database.test.ts +++ b/src/indexer/database.test.ts @@ -49,14 +49,13 @@ describe('DatabaseIndexer', () => { }); const crateObject: CrateObject = { id: 'database-test', - root: '/nowhere', text: async () => '', file: async (path) => { const size = inventory[path]; if (size === undefined) { throw new Error(`Not in inventory: ${path}`); } - return { size, crc32: '' }; + return { size }; }, }; await indexer.index({ crateObject, crate, topCollection: ROOT }); diff --git a/src/indexer/database.ts b/src/indexer/database.ts index 5b041c8..90a822d 100644 --- a/src/indexer/database.ts +++ b/src/indexer/database.ts @@ -64,7 +64,7 @@ export class DatabaseIndexer extends Indexer { filename: (ocflPath ?? entityId).split('/').pop(), mediaType: entity.encodingFormat?.find((v: unknown) => typeof v === 'string') || 'application/octet-stream', size: f?.size ?? statedSize(entity.contentSize?.[0]), - meta: ocflPath ? ({ ocflObject: crateObject.id, ocflPath, crc32: f?.crc32 ?? '' } satisfies OcflStorageMeta & { crc32: string }) : {}, + meta: ocflPath ? ({ ocflObject: crateObject.id, ocflPath } satisfies OcflStorageMeta) : {}, }; } try { diff --git a/src/indexer/indexer.ts b/src/indexer/indexer.ts index 3ee7ee6..093350e 100644 --- a/src/indexer/indexer.ts +++ b/src/indexer/indexer.ts @@ -29,14 +29,11 @@ export type LogicalPaths = Map; export interface CrateFile { size: number; - crc32: string; } export interface CrateObject { /** The id of the OCFL object holding the crate */ id: string; - /** The absolute path of the root directory of the crate */ - root: string; /** The text content of a file within the crate */ text(path: string): Promise; /** The metadata of a file within the crate, such as size */ @@ -117,20 +114,20 @@ export class Indexer { const metadataLicense = firstStringOrId(crate.descriptor.license) || this.defaultMetadataLicense; const license = firstStringOrId(rootDataset.license) || this.defaultLicense; if (!rootDataset) { - log.warn({ path: crateObject.root }, 'Skipped: Does not contain an ROCrate with a valid root dataset'); + log.warn({ crateId: crateObject.id }, 'Skipped: Does not contain an ROCrate with a valid root dataset'); } else if (crateId === './') { - log.warn({ path: crateObject.root }, "Skipped: Cannot process a crate with invalid identifier ('./')"); + log.warn({ crateId: crateObject.id }, "Skipped: Cannot process a crate with invalid identifier ('./')"); } else if (!metadataLicense) { - log.warn({ path: crateObject.root, crateId }, 'Skipped: No metadata license found'); + log.warn({ crateId }, 'Skipped: No metadata license found'); } else if (!license) { - log.warn({ path: crateObject.root, crateId }, 'Skipped: No license found'); + log.warn({ crateId }, 'Skipped: No license found'); } else { log.info({ crateId }, 'Indexing crate'); const paths = this.deriveUniqueEntityIds(crate); try { await this._index({ crateObject, crate, paths, license, metadataLicense, topCollection }); } catch (error) { - log.error({ err: error, crateId, path: crateObject.root }, 'Error indexing crate'); + log.error({ err: error, crateId }, 'Error indexing crate'); } } } diff --git a/src/indexer/search.test.ts b/src/indexer/search.test.ts index 45c6a71..d13c5fa 100644 --- a/src/indexer/search.test.ts +++ b/src/indexer/search.test.ts @@ -23,7 +23,7 @@ const crateWith = async (entity: Record, terms: Record 'hello world', file: async () => ({ size: 11, crc32: '' }) }; +const crateObject: CrateObject = { id: '', text: async () => 'hello world', file: async () => ({ size: 11 }) }; describe('searchDocument', () => { it('flattens an entity into declared fields', async () => { diff --git a/src/ocfl-modules.d.ts b/src/ocfl-modules.d.ts deleted file mode 100644 index f1afc9f..0000000 --- a/src/ocfl-modules.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -declare module '@ocfl/ocfl' { - export interface OcflObjectFile { - size?: string; - fixity?: Record; - text(encoding?: string): Promise; - stream(options?: unknown): Promise; - stat(options?: unknown): Promise<{ size?: number; atime?: Date; mtime?: Date; ctime?: Date }>; - } - - export interface OcflObject { - id: string; - root: string; - load(): Promise; - import(source: string): Promise; - getInventory(): Promise<{ id: string }>; - getFile(ref: { logicalPath: string }): OcflObjectFile; - } - - export interface OcflStorage extends AsyncIterable { - load(): Promise; - create(): Promise; - exists(): Promise; - object(id: string): OcflObject; - } -} - -declare module '@ocfl/ocfl-fs' { - import type { OcflStorage } from '@ocfl/ocfl'; - - function storage(options: { - root: string; - workspace?: string; - ocflVersion?: string; - fixityAlgorithms?: string[]; - layout?: { extensionName: string }; - }): OcflStorage; - - const ocfl: { storage: typeof storage }; - export default ocfl; -} diff --git a/src/ocfl-repository.ts b/src/ocfl-repository.ts new file mode 100644 index 0000000..317ad50 --- /dev/null +++ b/src/ocfl-repository.ts @@ -0,0 +1,28 @@ +import { createStorageRoot, OcflError, openStorageRoot, type StorageRoot } from '@ocfl/ocfl-fs'; +import { config } from './configuration.ts'; + +const isMissing = (error: unknown) => error instanceof OcflError && error.code === 'INVALID_STORAGE_ROOT' && error.ocflCode === 'E069'; + +/** The OCFL repository, which must exist */ +export async function openRepository(): Promise { + try { + return await openStorageRoot(config.ocfl.root); + } catch (error) { + if (isMissing(error)) { + throw new Error(`Cannot load the OCFL repository at ${config.ocfl.root}. Run \`pnpm ocfl:init\` to create it.`, { cause: error }); + } + throw error; + } +} + +/** The OCFL repository, created empty if it doesn't exist yet */ +export async function openOrCreateRepository(): Promise { + try { + return await openStorageRoot(config.ocfl.root); + } catch (error) { + if (!isMissing(error)) { + throw error; + } + return createStorageRoot(config.ocfl.root, { layout: { extensionName: '000N-path-direct-storage-layout' } }); + } +} diff --git a/src/ocfl.ts b/src/ocfl.ts index a19bf45..5a73de0 100644 --- a/src/ocfl.ts +++ b/src/ocfl.ts @@ -1,6 +1,4 @@ -import { crc32 } from 'node:zlib'; -import type { OcflObject, OcflObjectFile } from '@ocfl/ocfl'; -import ocfl from '@ocfl/ocfl-fs'; +import { OcflError, type OcflObject, type StorageRoot } from '@ocfl/ocfl-fs'; import type { Client } from '@opensearch-project/opensearch'; import type { StorageBackend } from 'arocapi'; import { OcflStorage } from 'arocapi/storage/ocfl'; @@ -10,6 +8,7 @@ import { DatabaseIndexer } from './indexer/database.ts'; import { type CrateObject, type Indexer, parentCollection } from './indexer/indexer.ts'; import { createSearchBackend, SearchIndexer } from './indexer/search.ts'; import { logger } from './logger.ts'; +import { openRepository } from './ocfl-repository.ts'; import { INDEX_TYPES, type IndexState, type IndexStates, type IndexType, State } from './repository.ts'; const log = logger.child({ module: 'ocfl' }); @@ -18,7 +17,7 @@ const { defaultLicense, defaultMetadataLicense } = config; const stateCache: Record = {}; let INDEXER: Record; -let repository: ReturnType; +let repository: StorageRoot; let backend: StorageBackend; export async function init(opts: { opensearchClient: Client; search?: ReturnType }) { @@ -35,16 +34,8 @@ export async function init(opts: { opensearchClient: Client; search?: ReturnType search: opts.search ?? createSearchBackend(opts.opensearchClient), }), }; - repository = createStorage(); - try { - await repository.load(); - backend = await OcflStorage({ root: config.ocfl.root }); - } catch (e) { - if (await repository.exists()) { - throw e; - } - throw new Error(`Cannot load the OCFL repository at ${config.ocfl.root}. Run \`pnpm ocfl:init\` to create it.`, { cause: e }); - } + repository = await openRepository(); + backend = await OcflStorage({ root: config.ocfl.root }); } /** arocapi's storage backend over the repository */ @@ -52,40 +43,9 @@ export function storage() { return backend; } -/** The OCFL repository, created empty if it doesn't exist yet */ -export async function openOrCreateStorage() { - const storage = createStorage(); - if (await storage.exists()) { - await storage.load(); - } else { - await storage.create(); - } - return storage; -} - -export function createStorage() { - return ocfl.storage({ - root: config.ocfl.root, - workspace: config.ocfl.scratch, - ocflVersion: '1.1', - fixityAlgorithms: ['crc32'], - layout: { - extensionName: '000N-path-direct-storage-layout', - }, - }); -} - -async function calculateCrc32(file: OcflObjectFile) { - let crc = 0; - for await (const chunk of await file.stream()) { - crc = crc32(chunk, crc); - } - return crc.toString(16).padStart(8, '0'); -} - function wrap(ocflObject: OcflObject): CrateObject { - const getInventoryFile = (path: string) => { - const file = ocflObject.getFile({ logicalPath: path }); + const fileAt = (path: string) => { + const file = ocflObject.head.files.get(path); if (!file) { throw new Error(`File not found in ocfl inventory: ${path}`); } @@ -93,17 +53,8 @@ function wrap(ocflObject: OcflObject): CrateObject { }; return { id: ocflObject.id, - root: ocflObject.root, - async text(path: string) { - return await getInventoryFile(path).text(); - }, - async file(path: string) { - const file = getInventoryFile(path); - return { - size: Number(file.size ?? file.fixity?.size ?? (await file.stat()).size), - crc32: file.fixity?.crc32 ?? (await calculateCrc32(file)), - }; - }, + text: (path) => fileAt(path).text(), + file: async (path) => ({ size: await fileAt(path).size() }), }; } @@ -156,17 +107,29 @@ async function deleteFrom(types: IndexType[], crateId?: string) { } } -const readCrateJson = async (ocflObject: OcflObject): Promise<{ '@graph': RawEntity[] }> => - JSON.parse(await ocflObject.getFile({ logicalPath: 'ro-crate-metadata.json' }).text()); +const readCrateJson = async (ocflObject: OcflObject): Promise<{ '@graph': RawEntity[] }> => { + const file = ocflObject.head.files.get('ro-crate-metadata.json'); + if (!file) { + throw new Error(`No ro-crate-metadata.json in ${ocflObject.id}`); + } + return JSON.parse(await file.text()); +}; /** The root entity of a crate's raw JSON-LD, whose id is the OCFL object's */ const rawRoot = (json: { '@graph': RawEntity[] }, crateId: string) => json['@graph'].find((e) => e['@id'] === crateId); /** A crate's OCFL object, whose id is the crate's root id */ -async function loadObject(crateId: string) { - const ocflObject = repository.object(crateId); - await ocflObject.load(); - return ocflObject; +const loadObject = (crateId: string) => repository.open(crateId); + +/** Each crate's OCFL object, logging those that can't be read */ +async function* readableObjects() { + for await (const entry of repository.objects()) { + if (entry instanceof OcflError) { + log.error({ err: entry }, 'Error reading object'); + } else { + yield entry; + } + } } /** @@ -246,8 +209,7 @@ async function indexObject(crateId: string, types: IndexType[], findTopCollectio async function idsMatching(pattern?: RegExp) { const ids: string[] = []; - for await (const ocflObject of repository) { - await ocflObject.load(); + for await (const ocflObject of readableObjects()) { if (!pattern || ocflObject.id.match(pattern)) { ids.push(ocflObject.id); } @@ -288,13 +250,13 @@ export async function* objects(prefix?: string, refresh?: boolean) { } else if (prefix == null) { prefix = ''; } - for await (const ocflObject of repository) { + for await (const ocflObject of readableObjects()) { + const { id } = ocflObject; try { - const inv = await ocflObject.getInventory(); - const name = rawRoot(await readCrateJson(ocflObject), inv.id)?.name?.toString(); - yield { id: inv.id, name, path: ocflObject.root, state: await getState(inv.id) }; + const name = rawRoot(await readCrateJson(ocflObject), id)?.name?.toString(); + yield { id, name, state: await getState(id) }; } catch (error) { - log.error({ err: error, path: ocflObject.root }, 'Error reading object'); + log.error({ err: error, crateId: id }, 'Error reading object'); } } } diff --git a/src/repository.ts b/src/repository.ts index 93f01f3..d83cf60 100644 --- a/src/repository.ts +++ b/src/repository.ts @@ -20,7 +20,7 @@ export type IndexStates = Partial>; export const hasState = (state: IndexStates, ...values: IndexState[]) => Object.values(state).some((value) => value !== undefined && values.includes(value)); /** A crate in the repository, with the state of each index that isn't idle */ -export type RepositoryObject = { id: string; path: string; name?: string | undefined; state: IndexStates }; +export type RepositoryObject = { id: string; name?: string | undefined; state: IndexStates }; /** * Abstraction for repository operations, such as indexing and listing objects. This is used by the API routes to interact with the underlying data store, which can be an OCFL storage or a database. diff --git a/test/admin.ts b/test/admin.ts index 849ff84..93723cb 100644 --- a/test/admin.ts +++ b/test/admin.ts @@ -25,7 +25,7 @@ export const repositoryOf = (crates: { id: string; name?: string; state: Record< storage: () => ({ content: {} }), async *objects() { for (const { id, name = id, state } of crates) { - yield { id, name, path: '', state }; + yield { id, name, state }; } }, getState: async (crateId: string) => crates.find(({ id }) => id === crateId)?.state ?? {}, From 381d6dda217cf2571acd666b85baf71e677389e7 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 12:07:01 +1100 Subject: [PATCH 2/6] test: cover importing a crate again --- test/crates-import.test.ts | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/crates-import.test.ts diff --git a/test/crates-import.test.ts b/test/crates-import.test.ts new file mode 100644 index 0000000..db2e141 --- /dev/null +++ b/test/crates-import.test.ts @@ -0,0 +1,54 @@ +import { execFileSync } from 'node:child_process'; +import { cp, mkdtemp, rm, unlink, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { openStorageRoot } from '@ocfl/ocfl-fs'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; + +const BUNDLED = 'arcp://name,bundled'; + +describe('crates:import', () => { + let storage: string; + let source: string; + const root = () => join(storage, 'ocfl'); + const importCrates = () => + execFileSync('node', ['scripts/crates-import.ts', source], { env: { ...process.env, OCFL_ROOT: root(), LOG_LEVEL: 'silent' }, stdio: 'ignore' }); + const head = async (id: string) => (await (await openStorageRoot(root())).open(id)).head; + + beforeEach(async () => { + storage = await mkdtemp(join(tmpdir(), 'ldacapi-')); + source = join(storage, 'crates'); + await cp('test-data', source, { recursive: true }); + }); + + afterEach(async () => { + await rm(storage, { recursive: true, force: true }); + }); + + it('records who imported each crate, and from where', async () => { + importCrates(); + + expect(await head(BUNDLED)).toMatchObject({ number: 1, message: `Imported from ${join(source, 'bundled')}`, user: { name: 'ldacapi crates:import' } }); + }); + + it('adds no version when a crate is imported again unchanged', async () => { + importCrates(); + + importCrates(); + + expect((await head(BUNDLED)).number).toBe(1); + }); + + it('merges a changed crate into a new version, keeping files it no longer has', async () => { + importCrates(); + const before = [...(await head(BUNDLED)).files.keys()]; + await writeFile(join(source, 'bundled', 'notes.txt'), 'added'); + await unlink(join(source, 'bundled', 'facts.csv')); + + importCrates(); + + const after = await head(BUNDLED); + expect(after.number).toBe(2); + expect([...after.files.keys()].sort()).toEqual([...before, 'notes.txt'].sort()); + }); +}); From 760d534e8510778456b5cbf693b690dddcebda32 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 14:29:09 +1100 Subject: [PATCH 3/6] feat: name ldacapi by URL as the user of imported versions --- scripts/crates-import.ts | 5 ++++- test/crates-import.test.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts/crates-import.ts b/scripts/crates-import.ts index b699609..c2aed28 100644 --- a/scripts/crates-import.ts +++ b/scripts/crates-import.ts @@ -10,7 +10,10 @@ const source = resolve(process.argv[2] ?? 'test-data'); /** Merges the crate at `path` into its OCFL object, creating the object on first import */ async function importCrate(repository: StorageRoot, crateId: string, path: string) { - const details: VersionDetails = { message: `Imported from ${path}`, user: { name: 'ldacapi crates:import' } }; + const details: VersionDetails = { + message: `Imported from ${path}`, + user: { name: 'ldacapi crates:import', address: 'https://github.com/Language-Research-Technology/ldacapi' }, + }; try { return await repository.create(crateId, details, (draft) => draft.import(path)); } catch (error) { diff --git a/test/crates-import.test.ts b/test/crates-import.test.ts index db2e141..3098944 100644 --- a/test/crates-import.test.ts +++ b/test/crates-import.test.ts @@ -28,7 +28,11 @@ describe('crates:import', () => { it('records who imported each crate, and from where', async () => { importCrates(); - expect(await head(BUNDLED)).toMatchObject({ number: 1, message: `Imported from ${join(source, 'bundled')}`, user: { name: 'ldacapi crates:import' } }); + expect(await head(BUNDLED)).toMatchObject({ + number: 1, + message: `Imported from ${join(source, 'bundled')}`, + user: { name: 'ldacapi crates:import', address: 'https://github.com/Language-Research-Technology/ldacapi' }, + }); }); it('adds no version when a crate is imported again unchanged', async () => { From 7025ea57b2b60770a700b74eea138726b9a9b407 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 18:35:47 +1100 Subject: [PATCH 4/6] build!: upgrade arocapi to 5.0.0-beta.10 and @ocfl/ocfl-fs to 1.0.0-beta.59 arocapi's OcflStorage now serves files through @ocfl/ocfl-fs 1.0, so ldacapi installs only 1.0. Listing reads beta.59's entries, and logs a damaged crate's object root and id. --- package.json | 4 ++-- pnpm-lock.yaml | 24 ++++++++++++------------ pnpm-workspace.yaml | 3 ++- src/ocfl.ts | 8 ++++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index ead1c53..22d6419 100644 --- a/package.json +++ b/package.json @@ -54,11 +54,11 @@ "@fastify/sensible": "^6.0.7", "@fastify/static": "^10.1.7", "@fastify/view": "^12.0.1", - "@ocfl/ocfl-fs": "^1.0.0-beta.58", + "@ocfl/ocfl-fs": "1.0.0-beta.59", "@opensearch-project/opensearch": "^3.9.0", "@prisma/adapter-pg": "^7.10.1", "@prisma/client": "^7.10.1", - "arocapi": "5.0.0-beta.9", + "arocapi": "5.0.0-beta.10", "eta": "^4.6.0", "fastify": "^5.12.7", "jose": "^6.2.13", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 99a8ca8..d7c3d62 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -185,8 +185,8 @@ importers: specifier: ^12.0.1 version: 12.0.1 '@ocfl/ocfl-fs': - specifier: ^1.0.0-beta.58 - version: 1.0.0-beta.58 + specifier: 1.0.0-beta.59 + version: 1.0.0-beta.59 '@opensearch-project/opensearch': specifier: ^3.9.0 version: 3.9.0 @@ -197,8 +197,8 @@ importers: specifier: ^7.10.1 version: 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) arocapi: - specifier: 5.0.0-beta.9 - version: 5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.58)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) + specifier: 5.0.0-beta.10 + version: 5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) eta: specifier: ^4.6.0 version: 4.6.0 @@ -515,8 +515,8 @@ packages: '@emnapi/core': ^1.7.1 || ^2.0.0-alpha.4 '@emnapi/runtime': ^1.7.1 || ^2.0.0-alpha.4 - '@ocfl/ocfl-fs@1.0.0-beta.58': - resolution: {integrity: sha512-+dbRKEQmpZNr/C2VjHyXciktmBLOr+V0q65PH2Gknqlh135gU8HXP1UFBBH2koBmjNfSo6hwTE9ro+VV9gcfgQ==} + '@ocfl/ocfl-fs@1.0.0-beta.59': + resolution: {integrity: sha512-CoF8xJ6xz7rlDLahNI+VOMp2OMcvUmFjHUS/7GZ72idVMx2Tr8QFet4fuYNrUqWN6w1tqAFbWc6GrFOoM8ItWA==} engines: {node: '>=22.12.0'} '@opensearch-project/opensearch@3.9.0': @@ -1498,13 +1498,13 @@ packages: resolution: {integrity: sha512-VipTB0gXgGIFO2Rg9yEVN5wLt2AurJcZqDbgmYSwPwsykhLrQhs240/bfceev4w68lI8JshoOJIk9uW7tFzROw==} engines: {node: '>=22'} - arocapi@5.0.0-beta.9: - resolution: {integrity: sha512-qBT0SqR9AsvjHbOZyk3GvzGstdqxm5zp4ufANCXcAJ2UpIefEnZma+1BourvY3gbvYNpcGx/9fx2l4s74Zsj9g==} + arocapi@5.0.0-beta.10: + resolution: {integrity: sha512-BXxqc5tywkcbszcKIU8c2wX4aaiydXYVvaQpjlakT/YEB7WF1SHn/ljM5SHJwO/q/3BSkiXXxvTekuyhdN0oIA==} engines: {node: '>=22.18'} peerDependencies: '@aws-sdk/client-s3': ^3 '@aws-sdk/s3-request-presigner': ^3 - '@ocfl/ocfl-fs': ^0.4.0 + '@ocfl/ocfl-fs': ^1.0.0-beta.59 '@opensearch-project/opensearch': ^3.9.0 '@prisma/client': ^7.10.0 fastify: ^5.12.7 @@ -3344,7 +3344,7 @@ snapshots: '@tybys/wasm-util': 0.10.4 optional: true - '@ocfl/ocfl-fs@1.0.0-beta.58': {} + '@ocfl/ocfl-fs@1.0.0-beta.59': {} '@opensearch-project/opensearch@3.9.0': dependencies: @@ -4137,7 +4137,7 @@ snapshots: argue-cli@3.2.0: {} - arocapi@5.0.0-beta.9(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.58)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): + arocapi@5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): dependencies: '@fastify/sensible': 6.0.7 '@prisma/client': 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) @@ -4146,7 +4146,7 @@ snapshots: fastify-type-provider-zod: 7.0.0(@fastify/swagger@9.9.1)(fastify@5.12.7)(openapi-types@12.1.3)(zod@4.6.7) zod: 4.6.7 optionalDependencies: - '@ocfl/ocfl-fs': 1.0.0-beta.58 + '@ocfl/ocfl-fs': 1.0.0-beta.59 '@opensearch-project/opensearch': 3.9.0 transitivePeerDependencies: - '@fastify/swagger' diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 2e2268f..fafaa6c 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -11,4 +11,5 @@ allowBuilds: # ships prebuilt binaries; its install script only builds from source "@parcel/watcher": false minimumReleaseAgeExclude: - - arocapi@5.0.0-beta.9 + - arocapi@5.0.0-beta.10 + - "@ocfl/ocfl-fs@1.0.0-beta.59" diff --git a/src/ocfl.ts b/src/ocfl.ts index 5a73de0..5003b00 100644 --- a/src/ocfl.ts +++ b/src/ocfl.ts @@ -1,4 +1,4 @@ -import { OcflError, type OcflObject, type StorageRoot } from '@ocfl/ocfl-fs'; +import type { OcflObject, StorageRoot } from '@ocfl/ocfl-fs'; import type { Client } from '@opensearch-project/opensearch'; import type { StorageBackend } from 'arocapi'; import { OcflStorage } from 'arocapi/storage/ocfl'; @@ -124,10 +124,10 @@ const loadObject = (crateId: string) => repository.open(crateId); /** Each crate's OCFL object, logging those that can't be read */ async function* readableObjects() { for await (const entry of repository.objects()) { - if (entry instanceof OcflError) { - log.error({ err: entry }, 'Error reading object'); + if ('object' in entry) { + yield entry.object; } else { - yield entry; + log.error({ err: entry.error, objectRoot: entry.objectRoot, crateId: entry.id }, 'Error reading object'); } } } From ced1b07917ed9947155f9e3a5c14accb1c0481dd Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 18:36:16 +1100 Subject: [PATCH 5/6] test: a damaged crate is left out of indexing and the crate list --- src/damaged-crate.test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/damaged-crate.test.ts diff --git a/src/damaged-crate.test.ts b/src/damaged-crate.test.ts new file mode 100644 index 0000000..57c23be --- /dev/null +++ b/src/damaged-crate.test.ts @@ -0,0 +1,32 @@ +import { writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { beforeAll, describe, expect, it } from 'vitest'; +import { ocflTestbed } from '../test/app.ts'; +import { prisma } from './prisma.ts'; + +const PEOPLE = 'arcp://name,people'; +const BUNDLED = 'arcp://name,bundled'; + +const testbed = ocflTestbed(); + +describe('a crate whose OCFL object is damaged', () => { + beforeAll(async () => { + const { config, repository } = testbed.started; + await writeFile(join(config.ocfl.root, 'arcp_name_people', '__object__', 'inventory.json'), 'not json'); + await repository.deleteIndex(); + await repository.createIndex(); + }); + + it('is left out when every crate is indexed, and the rest are indexed', async () => { + const ids = (await prisma.entity.findMany({ where: { id: { in: [PEOPLE, BUNDLED] } }, select: { id: true } })).map(({ id }) => id); + + expect(ids).toEqual([BUNDLED]); + }); + + it('is left out of the list of crates', async () => { + const ids = (await Array.fromAsync(testbed.started.repository.objects())).map(({ id }) => id); + + expect(ids).toContain(BUNDLED); + expect(ids).not.toContain(PEOPLE); + }); +}); From 498869f39ccb5935ce72daaa6de4637d3cee6d57 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Mon, 16 Nov 2026 19:15:22 +1100 Subject: [PATCH 6/6] test: damage a crate in the testbed's repository, not the default one --- src/damaged-crate.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/damaged-crate.test.ts b/src/damaged-crate.test.ts index 57c23be..e3f26d3 100644 --- a/src/damaged-crate.test.ts +++ b/src/damaged-crate.test.ts @@ -1,8 +1,8 @@ import { writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { beforeAll, describe, expect, it } from 'vitest'; import { ocflTestbed } from '../test/app.ts'; -import { prisma } from './prisma.ts'; const PEOPLE = 'arcp://name,people'; const BUNDLED = 'arcp://name,bundled'; @@ -12,13 +12,14 @@ const testbed = ocflTestbed(); describe('a crate whose OCFL object is damaged', () => { beforeAll(async () => { const { config, repository } = testbed.started; + expect(config.ocfl.root.startsWith(tmpdir())).toBe(true); await writeFile(join(config.ocfl.root, 'arcp_name_people', '__object__', 'inventory.json'), 'not json'); await repository.deleteIndex(); await repository.createIndex(); }); it('is left out when every crate is indexed, and the rest are indexed', async () => { - const ids = (await prisma.entity.findMany({ where: { id: { in: [PEOPLE, BUNDLED] } }, select: { id: true } })).map(({ id }) => id); + const ids = (await testbed.started.prisma.entity.findMany({ where: { id: { in: [PEOPLE, BUNDLED] } }, select: { id: true } })).map(({ id }) => id); expect(ids).toEqual([BUNDLED]); });