arocapi 5.0.0-beta.5 → 5.0.0-beta.7
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/LICENSE.md +1 -1
- package/README.md +2 -2
- package/dist/app.d.ts +22 -17
- package/dist/app.js +22 -16
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +2 -2
- package/dist/routes/capabilities.js +2 -2
- package/dist/routes/capabilities.js.map +1 -1
- package/dist/routes/entities.js +8 -9
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/file.d.ts +2 -2
- package/dist/routes/file.js +25 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/metadata.d.ts +5 -5
- package/dist/routes/metadata.js +22 -61
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +2 -4
- package/dist/routes/search.js +75 -97
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +20 -0
- package/dist/storage/location.js +40 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +7 -0
- package/dist/storage/ocfl/ocfl.js +79 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/ocfl/ocflFs.d.ts +7 -0
- package/dist/storage/ocfl/ocflFs.js +33 -0
- package/dist/storage/ocfl/ocflFs.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/types/visibility.d.ts +3 -2
- package/dist/utils/visibility.js +1 -1
- package/dist/utils/visibility.js.map +1 -1
- package/package.json +66 -26
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +85 -48
- package/src/routes/capabilities.ts +4 -4
- package/src/routes/entities.ts +10 -9
- package/src/routes/file.ts +23 -99
- package/src/routes/metadata.ts +24 -86
- package/src/routes/search.ts +104 -118
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +85 -0
- package/src/storage/ocfl/ocfl.ts +129 -0
- package/src/storage/ocfl/ocflFs.ts +67 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/types/visibility.ts +6 -3
- package/src/utils/visibility.ts +1 -1
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -67
- package/dist/app.test.js.map +0 -1
- package/dist/errorHandling.test.d.ts +0 -1
- package/dist/errorHandling.test.js +0 -222
- package/dist/errorHandling.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -157
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/capabilities.test.d.ts +0 -1
- package/dist/routes/capabilities.test.js +0 -81
- package/dist/routes/capabilities.test.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -390
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -277
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -136
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -435
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -245
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -833
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -17
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -47
- package/dist/test/integration.setup.js +0 -195
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -762
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -225
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -165
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -37
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -79
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -47
- package/dist/utils/queryBuilder.js +0 -107
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -91
- package/src/errorHandling.test.ts +0 -288
- package/src/index.dev.ts +0 -202
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/capabilities.test.ts +0 -102
- package/src/routes/crate.test.ts +0 -484
- package/src/routes/entities.test.ts +0 -324
- package/src/routes/entity.test.ts +0 -166
- package/src/routes/file.test.ts +0 -538
- package/src/routes/files.test.ts +0 -304
- package/src/routes/search.test.ts +0 -959
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -26
- package/src/test/integration.setup.ts +0 -220
- package/src/test/integration.test.ts +0 -953
- package/src/transformers/default.test.ts +0 -276
- package/src/transformers/transformer.test.ts +0 -226
- package/src/types/fileHandlers.ts +0 -57
- package/src/utils/errors.test.ts +0 -90
- package/src/utils/queryBuilder.ts +0 -157
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { Entity, File } from '../generated/prisma/client.js';
|
|
3
|
+
import type { RequestContext } from '../types/context.js';
|
|
4
|
+
|
|
5
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Headers a `stream` or `path` delivery carries, read from storage rather than the database
|
|
9
|
+
*/
|
|
10
|
+
export type DeliveryHeaders = {
|
|
11
|
+
contentLength: number;
|
|
12
|
+
etag?: string;
|
|
13
|
+
lastModified?: Date;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Bytes the library streams; `open` is called only on GET */
|
|
17
|
+
export type StreamDelivery = { type: 'stream'; open: () => MaybePromise<Readable> } & DeliveryHeaders;
|
|
18
|
+
|
|
19
|
+
/** An absolute path on disk the library opens on GET */
|
|
20
|
+
export type PathDelivery = { type: 'path'; path: string } & DeliveryHeaders;
|
|
21
|
+
|
|
22
|
+
/** A URL the client is sent to: 302, or 200 `{ location }` under `?noRedirect=true` */
|
|
23
|
+
export type RedirectDelivery = { type: 'redirect'; url: string };
|
|
24
|
+
|
|
25
|
+
/** A value the library serialises once, with the length and a strong ETag from those bytes */
|
|
26
|
+
export type JsonDelivery = { type: 'json'; value: unknown };
|
|
27
|
+
|
|
28
|
+
/** Content's `stream` and `path` deliveries name their Content-Type; Metadata's is always `application/ld+json` */
|
|
29
|
+
export type ContentDelivery =
|
|
30
|
+
| (StreamDelivery & { contentType: string })
|
|
31
|
+
| (PathDelivery & { contentType: string })
|
|
32
|
+
| RedirectDelivery;
|
|
33
|
+
|
|
34
|
+
export type MetadataDelivery = StreamDelivery | PathDelivery | RedirectDelivery | JsonDelivery;
|
|
35
|
+
|
|
36
|
+
export type ContentContext = RequestContext & {
|
|
37
|
+
/** The `Content-Disposition` the library sends on `stream` and `path` deliveries, for baking into a redirect */
|
|
38
|
+
contentDisposition: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Deliver an entity's Metadata, or `null` for a 404; HEAD and GET make the same call
|
|
43
|
+
*/
|
|
44
|
+
export type MetadataSource = (entity: Entity, context: RequestContext) => MaybePromise<MetadataDelivery | null>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Deliver a file's Content, or `null` for a 404; `entity` is the file's entity, with its `meta`
|
|
48
|
+
*/
|
|
49
|
+
export type ContentSource = (
|
|
50
|
+
file: File,
|
|
51
|
+
entity: Entity,
|
|
52
|
+
context: ContentContext,
|
|
53
|
+
) => MaybePromise<ContentDelivery | null>;
|
|
54
|
+
|
|
55
|
+
export type StorageBackend = { metadata: MetadataSource; content: ContentSource };
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Where a stored reference backend finds each item, overriding the default key it reads from `meta`
|
|
59
|
+
*/
|
|
60
|
+
export type StorageLocate<Key = string> = {
|
|
61
|
+
metadata?: (entity: Entity) => MaybePromise<Key | null>;
|
|
62
|
+
content?: (file: File, entity: Entity) => MaybePromise<Key | null>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** What `DirectoryStorage` reads from `File.meta` and `Entity.meta`: a path relative to its root */
|
|
66
|
+
export type DirectoryStorageMeta = { storagePath: string };
|
|
67
|
+
|
|
68
|
+
/** What `S3Storage` reads from `File.meta` and `Entity.meta`: a key relative to the bucket */
|
|
69
|
+
export type S3StorageMeta = { storageKey: string };
|
|
70
|
+
|
|
71
|
+
/** What `OcflStorage` reads from `File.meta` and `Entity.meta`; Metadata's `ocflPath` defaults to `ro-crate-metadata.json` */
|
|
72
|
+
export type OcflStorageMeta = { ocflObject: string; ocflPath?: string };
|
|
73
|
+
|
|
74
|
+
/** An OCFL object id and a logical path inside its head version */
|
|
75
|
+
export type OcflLocation = { object: string; path: string };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { open } from 'node:fs/promises';
|
|
3
|
+
import type { Readable } from 'node:stream';
|
|
4
|
+
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
5
|
+
import { setFileHeaders } from '../utils/headers.js';
|
|
6
|
+
import type { ContentDelivery, JsonDelivery } from './backend.js';
|
|
7
|
+
|
|
8
|
+
type SendOptions = {
|
|
9
|
+
contentDisposition?: string;
|
|
10
|
+
noRedirect?: boolean;
|
|
11
|
+
/** Whether a `json` delivery may be sent: Metadata's only */
|
|
12
|
+
json?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** A delivery with its Content-Type settled: Content's own, or the one the Metadata route gives */
|
|
16
|
+
type TypedDelivery = ContentDelivery | (JsonDelivery & { contentType: string });
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Turn a delivery into the response, opening bytes only on GET so HEAD sends GET's headers alone
|
|
20
|
+
* Throws on a delivery type the caller doesn't take, as a half typed loosely at runtime could return one
|
|
21
|
+
*/
|
|
22
|
+
export const sendDelivery = async (
|
|
23
|
+
request: FastifyRequest,
|
|
24
|
+
reply: FastifyReply,
|
|
25
|
+
delivery: TypedDelivery,
|
|
26
|
+
{ contentDisposition, noRedirect = false, json = false }: SendOptions = {},
|
|
27
|
+
) => {
|
|
28
|
+
if (delivery.type === 'redirect') {
|
|
29
|
+
return noRedirect ? reply.code(200).send({ location: delivery.url }) : reply.redirect(delivery.url, 302);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const head = request.method === 'HEAD';
|
|
33
|
+
|
|
34
|
+
if (delivery.type === 'json' && json) {
|
|
35
|
+
const body = Buffer.from(JSON.stringify(delivery.value));
|
|
36
|
+
const etag = `"${createHash('sha256').update(body).digest('base64url')}"`;
|
|
37
|
+
setFileHeaders(reply, { contentType: delivery.contentType, contentLength: body.length, etag });
|
|
38
|
+
|
|
39
|
+
return reply.code(200).send(head ? undefined : body);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (delivery.type !== 'stream' && delivery.type !== 'path') {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Storage returned a delivery of type ${JSON.stringify(delivery.type)}, which this route can't send`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Opened before any header is set, so a failure leaves a clean 500
|
|
49
|
+
let body: Readable | undefined;
|
|
50
|
+
if (!head) {
|
|
51
|
+
body = delivery.type === 'stream' ? await delivery.open() : (await open(delivery.path)).createReadStream();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
setFileHeaders(reply, delivery);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
body?.destroy();
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
if (contentDisposition) {
|
|
61
|
+
reply.header('Content-Disposition', contentDisposition);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return reply.code(200).send(body);
|
|
65
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import type { DirectoryStorageMeta, PathDelivery, StorageBackend, StorageLocate } from '../backend.js';
|
|
4
|
+
import { keyLocator } from '../location.js';
|
|
5
|
+
import { nullIfMissing, pathDelivery, withContentType } from '../results.js';
|
|
6
|
+
|
|
7
|
+
export type DirectoryStorageOptions = {
|
|
8
|
+
/** The directory every Storage key is relative to */
|
|
9
|
+
root: string;
|
|
10
|
+
/** Where each item sits below `root`, instead of `meta.storagePath` */
|
|
11
|
+
locate?: StorageLocate;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Serve Metadata and Content as files below `root`, found from `Entity.meta.storagePath` and `File.meta.storagePath`
|
|
16
|
+
*/
|
|
17
|
+
export const DirectoryStorage = ({ root, locate = {} }: DirectoryStorageOptions): StorageBackend => {
|
|
18
|
+
const base = resolve(root);
|
|
19
|
+
const keys = keyLocator<DirectoryStorageMeta>(locate, 'storagePath');
|
|
20
|
+
|
|
21
|
+
const deliver = async (key: string | null): Promise<PathDelivery | null> => {
|
|
22
|
+
if (key === null) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const path = join(base, key);
|
|
27
|
+
const stats = await nullIfMissing(stat(path));
|
|
28
|
+
if (!stats?.isFile()) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return pathDelivery(path, stats, `W/"${stats.size.toString(16)}-${stats.mtime.getTime().toString(16)}"`);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
metadata: async (entity) => deliver(await keys.metadata(entity)),
|
|
37
|
+
content: async (file, entity) => withContentType(await deliver(await keys.content(file, entity)), file.mediaType),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MetadataSource } from '../backend.js';
|
|
2
|
+
import { isJsonObject, metaValue } from '../location.js';
|
|
3
|
+
|
|
4
|
+
export type EntityMetaMetadataOptions = {
|
|
5
|
+
/** The key in `Entity.meta` holding the RO-Crate metadata object */
|
|
6
|
+
key?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Serve Metadata from the JSON object at `Entity.meta.rocrate`, or another `key`; a Metadata half only
|
|
11
|
+
*/
|
|
12
|
+
export const EntityMetaMetadata = ({ key = 'rocrate' }: EntityMetaMetadataOptions = {}): MetadataSource => {
|
|
13
|
+
return async (entity) => {
|
|
14
|
+
const value = metaValue(entity.meta, key);
|
|
15
|
+
if (value === undefined) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!isJsonObject(value)) {
|
|
20
|
+
throw new Error(`${entity.id} has a meta.${key} that is not a JSON object`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return { type: 'json', value };
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { isAbsolute, relative, sep } from 'node:path';
|
|
2
|
+
import type { Entity, File, Prisma } from '../generated/prisma/client.js';
|
|
3
|
+
import type { StorageLocate } from './backend.js';
|
|
4
|
+
|
|
5
|
+
export const isJsonObject = (value: Prisma.JsonValue | undefined): value is Prisma.JsonObject =>
|
|
6
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
7
|
+
|
|
8
|
+
/** The value at `key` in `meta`, or `undefined` when `meta` isn't an object */
|
|
9
|
+
export const metaValue = (meta: Prisma.JsonValue, key: string) => (isJsonObject(meta) ? meta[key] : undefined);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Read a key of the `meta` shape `Meta`: `null` when absent, a throw naming the entity and key when not a string,
|
|
13
|
+
* `null` included
|
|
14
|
+
*/
|
|
15
|
+
export const metaString = <Meta>(meta: Prisma.JsonValue, id: string, key: Extract<keyof Meta, string>) => {
|
|
16
|
+
const value = metaValue(meta, key);
|
|
17
|
+
if (value === undefined) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof value !== 'string') {
|
|
22
|
+
throw new Error(`${id} has a meta.${key} that is not a string`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Refuse a Storage key that could escape its root: absolute, or with a `..` segment
|
|
30
|
+
*/
|
|
31
|
+
export const safeKey = (key: string, id: string) => {
|
|
32
|
+
if (isAbsolute(key) || key.split(/[\\/]/).includes('..')) {
|
|
33
|
+
throw new Error(`${id} has an unsafe storage key ${JSON.stringify(key)}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return key;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Whether `path` is strictly below `root` */
|
|
40
|
+
export const inside = (root: string, path: string) => {
|
|
41
|
+
const rel = relative(root, path);
|
|
42
|
+
|
|
43
|
+
return rel !== '' && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type DefaultKeys<Key> = {
|
|
47
|
+
metadata: (entity: Entity) => Key | null;
|
|
48
|
+
content: (file: File) => Key | null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Find each item's key from `locate`, or else from `meta`, throwing through `check` on an unsafe one
|
|
53
|
+
*/
|
|
54
|
+
export const locator = <Key>(
|
|
55
|
+
locate: StorageLocate<Key>,
|
|
56
|
+
defaults: DefaultKeys<Key>,
|
|
57
|
+
check: (key: Key, id: string) => unknown,
|
|
58
|
+
) => {
|
|
59
|
+
const checked = (key: Key | null, id: string) => {
|
|
60
|
+
if (key !== null) {
|
|
61
|
+
check(key, id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return key;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
metadata: async (entity: Entity) => checked(await (locate.metadata ?? defaults.metadata)(entity), entity.id),
|
|
69
|
+
content: async (file: File, entity: Entity) =>
|
|
70
|
+
checked(await (locate.content ?? defaults.content)(file, entity), file.id),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A `locator` for string keys read from the same `meta` key of `Meta` on entities and files
|
|
76
|
+
*/
|
|
77
|
+
export const keyLocator = <Meta>(locate: StorageLocate, key: Extract<keyof Meta, string>) =>
|
|
78
|
+
locator(
|
|
79
|
+
locate,
|
|
80
|
+
{
|
|
81
|
+
metadata: (entity) => metaString<Meta>(entity.meta, entity.id, key),
|
|
82
|
+
content: (file) => metaString<Meta>(file.meta, file.id, key),
|
|
83
|
+
},
|
|
84
|
+
safeKey,
|
|
85
|
+
);
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import type { Entity, File } from '../../generated/prisma/client.js';
|
|
4
|
+
import type {
|
|
5
|
+
MaybePromise,
|
|
6
|
+
OcflLocation,
|
|
7
|
+
OcflStorageMeta,
|
|
8
|
+
PathDelivery,
|
|
9
|
+
StorageBackend,
|
|
10
|
+
StorageLocate,
|
|
11
|
+
} from '../backend.js';
|
|
12
|
+
import { inside, locator, metaString, safeKey } from '../location.js';
|
|
13
|
+
import { nullIfMissing, pathDelivery, withContentType } from '../results.js';
|
|
14
|
+
import { headFile, layoutObjectRoot } from './ocflFs.js';
|
|
15
|
+
|
|
16
|
+
export type OcflStorageOptions = {
|
|
17
|
+
/** The OCFL storage root */
|
|
18
|
+
root: string;
|
|
19
|
+
/** Where an object sits, relative to `root`, for a storage layout OcflStorage doesn't implement */
|
|
20
|
+
objectRoot?: (id: string) => MaybePromise<string>;
|
|
21
|
+
/** Where each item sits, instead of `meta.ocflObject` and `meta.ocflPath` */
|
|
22
|
+
locate?: StorageLocate<OcflLocation>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const LAYOUTS = new Set([
|
|
26
|
+
'0002-flat-direct-storage-layout',
|
|
27
|
+
'0003-hash-and-id-n-tuple-storage-layout',
|
|
28
|
+
'0004-hashed-n-tuple-storage-layout',
|
|
29
|
+
'000N-path-direct-storage-layout',
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const METADATA_PATH = 'ro-crate-metadata.json';
|
|
33
|
+
|
|
34
|
+
const MARKERS = ['0=ocfl_1.1', '0=ocfl_1.0'];
|
|
35
|
+
|
|
36
|
+
const readJson = async (path: string): Promise<unknown> => {
|
|
37
|
+
const text = await nullIfMissing(readFile(path, 'utf8'));
|
|
38
|
+
|
|
39
|
+
return text === null ? null : JSON.parse(text);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const readLayoutObjectRoot = async (base: string) => {
|
|
43
|
+
const layout = await readJson(join(base, 'ocfl_layout.json'));
|
|
44
|
+
const extension = (layout as { extension?: unknown } | null)?.extension;
|
|
45
|
+
if (typeof extension !== 'string') {
|
|
46
|
+
throw new Error(`${base} has no ocfl_layout.json naming its storage layout; pass objectRoot`);
|
|
47
|
+
}
|
|
48
|
+
if (!LAYOUTS.has(extension)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`${base} uses the storage layout ${extension}, which OcflStorage doesn't implement; pass objectRoot`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const config = await readJson(join(base, 'extensions', extension, 'config.json'));
|
|
55
|
+
|
|
56
|
+
return layoutObjectRoot(base, extension, config as object | null);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const metaKey = (row: Entity | File, key: keyof OcflStorageMeta) => metaString<OcflStorageMeta>(row.meta, row.id, key);
|
|
60
|
+
|
|
61
|
+
const defaultLocations = {
|
|
62
|
+
metadata: (entity: Entity): OcflLocation | null => {
|
|
63
|
+
const object = metaKey(entity, 'ocflObject');
|
|
64
|
+
const path = metaKey(entity, 'ocflPath') ?? METADATA_PATH;
|
|
65
|
+
|
|
66
|
+
return object === null ? null : { object, path };
|
|
67
|
+
},
|
|
68
|
+
content: (file: File): OcflLocation | null => {
|
|
69
|
+
const object = metaKey(file, 'ocflObject');
|
|
70
|
+
const path = metaKey(file, 'ocflPath');
|
|
71
|
+
|
|
72
|
+
return object === null || path === null ? null : { object, path };
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** An object id is mapped by the layout, so only the logical path is a key */
|
|
77
|
+
const safeLocation = ({ path }: OcflLocation, id: string) => safeKey(path, id);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Serve Metadata and Content from the head version of objects in an OCFL storage root on local disk
|
|
81
|
+
* Throws when `root` isn't a storage root, or uses a storage layout it doesn't implement and no `objectRoot` is given
|
|
82
|
+
*/
|
|
83
|
+
export const OcflStorage = async ({
|
|
84
|
+
root,
|
|
85
|
+
objectRoot: custom,
|
|
86
|
+
locate = {},
|
|
87
|
+
}: OcflStorageOptions): Promise<StorageBackend> => {
|
|
88
|
+
const base = resolve(root);
|
|
89
|
+
const markers = await Promise.all(MARKERS.map((name) => nullIfMissing(stat(join(base, name)))));
|
|
90
|
+
if (!markers.some(Boolean)) {
|
|
91
|
+
throw new Error(`${base} is not an OCFL storage root`);
|
|
92
|
+
}
|
|
93
|
+
const objectRoot = custom ?? (await readLayoutObjectRoot(base));
|
|
94
|
+
const locations = locator(locate, defaultLocations, safeLocation);
|
|
95
|
+
|
|
96
|
+
const deliver = async (location: OcflLocation | null, id: string): Promise<PathDelivery | null> => {
|
|
97
|
+
if (location === null) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const objectPath = resolve(base, await objectRoot(location.object));
|
|
102
|
+
if (!inside(base, objectPath)) {
|
|
103
|
+
throw new Error(`${id}: the root of OCFL object ${location.object} is outside the storage root`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const file = await headFile(objectPath, location.path);
|
|
107
|
+
if (file === null) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const path = resolve(objectPath, file.contentPath);
|
|
112
|
+
if (!inside(objectPath, path)) {
|
|
113
|
+
throw new Error(`${id}: OCFL object ${location.object} has a manifest path outside its root`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const stats = await stat(path);
|
|
117
|
+
if (!stats.isFile()) {
|
|
118
|
+
throw new Error(`${id}: OCFL object ${location.object} has a manifest path that is not a file`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return pathDelivery(path, stats, `"${file.digest}"`);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
metadata: async (entity) => deliver(await locations.metadata(entity), entity.id),
|
|
126
|
+
content: async (file, entity) =>
|
|
127
|
+
withContentType(await deliver(await locations.content(file, entity), file.id), file.mediaType),
|
|
128
|
+
};
|
|
129
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Workarounds for @ocfl/ocfl-fs gaps, each removable once upstream ships types, keeps a given layout and reports an
|
|
2
|
+
// unknown one distinctly in loadStorage, maps hashed ids without loadStorage, and encapsulates 0003 ids per spec
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
// @ts-expect-error @ocfl/ocfl-fs ships no types, so the little used here is typed below
|
|
5
|
+
import ocfl from '@ocfl/ocfl-fs';
|
|
6
|
+
|
|
7
|
+
type InventoryFile = { digest: string; contentPath: string };
|
|
8
|
+
|
|
9
|
+
type OcflFs = {
|
|
10
|
+
object: (config: { root: string }) => {
|
|
11
|
+
getInventory: () => Promise<{ getFile: (logicalPath: string) => InventoryFile | undefined } | undefined>;
|
|
12
|
+
};
|
|
13
|
+
loadStorage: (config: { root: string }) => Promise<unknown>;
|
|
14
|
+
storageLayout: (config: string | object) => { map: (id: string) => string; get: (param: string) => unknown };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const { loadStorage, object, storageLayout } = ocfl as OcflFs;
|
|
18
|
+
|
|
19
|
+
const HASH_AND_ID = '0003-hash-and-id-n-tuple-storage-layout';
|
|
20
|
+
|
|
21
|
+
/** Every UTF-8 byte outside A-Z a-z 0-9 - _ as lowercase %xx, past 100 characters truncated and suffixed with the digest */
|
|
22
|
+
const encapsulate = (id: string, digest: string) => {
|
|
23
|
+
const encoded = Array.from(new TextEncoder().encode(id), (byte) => {
|
|
24
|
+
const char = String.fromCharCode(byte);
|
|
25
|
+
|
|
26
|
+
return /[A-Za-z0-9_-]/.test(char) ? char : `%${byte.toString(16).padStart(2, '0')}`;
|
|
27
|
+
}).join('');
|
|
28
|
+
|
|
29
|
+
return encoded.length > 100 ? `${encoded.slice(0, 100)}-${digest}` : encoded;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Map ids with the storage layout extension and config of the storage root at `root`
|
|
34
|
+
* `loadStorage` would replace a layout it was given, and fails on an unknown one without a distinct error, so the
|
|
35
|
+
* caller reads ocfl_layout.json itself
|
|
36
|
+
*/
|
|
37
|
+
export const layoutObjectRoot = async (root: string, extension: string, config: object | null) => {
|
|
38
|
+
const layout = storageLayout(config ? { ...config, extensionName: extension } : extension);
|
|
39
|
+
const digestAlgorithm = layout.get('digestAlgorithm');
|
|
40
|
+
if (digestAlgorithm) {
|
|
41
|
+
// Hashed layouts map only after OcflDigest.initSync, which nothing exported calls but loadStorage
|
|
42
|
+
await loadStorage({ root });
|
|
43
|
+
}
|
|
44
|
+
if (extension !== HASH_AND_ID) {
|
|
45
|
+
return (id: string) => layout.map(id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// @ocfl/ocfl-fs lowercases the whole encapsulation directory, leaves !'()*~ unencoded and never truncates it
|
|
49
|
+
const digest = storageLayout({
|
|
50
|
+
extensionName: '0004-hashed-n-tuple-storage-layout',
|
|
51
|
+
digestAlgorithm,
|
|
52
|
+
tupleSize: 0,
|
|
53
|
+
numberOfTuples: 0,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return (id: string) => join(dirname(layout.map(id)), encapsulate(id, digest.map(id)));
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Find a logical path in the head version of the object at `root`, or `null` when there is no object or no such path
|
|
61
|
+
* Throws when the inventory is damaged
|
|
62
|
+
*/
|
|
63
|
+
export const headFile = async (root: string, logicalPath: string) => {
|
|
64
|
+
const inventory = await object({ root }).getInventory();
|
|
65
|
+
|
|
66
|
+
return inventory?.getFile(logicalPath) ?? null;
|
|
67
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Stats } from 'node:fs';
|
|
2
|
+
import type { PathDelivery } from './backend.js';
|
|
3
|
+
|
|
4
|
+
const FS_MISSING: ReadonlySet<string> = new Set(['ENOENT', 'ENOTDIR']);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolve to `null` when the error's code or name is in `missing`, else rethrow
|
|
8
|
+
*/
|
|
9
|
+
export const nullIfMissing = async <T>(promise: Promise<T>, missing = FS_MISSING): Promise<T | null> => {
|
|
10
|
+
try {
|
|
11
|
+
return await promise;
|
|
12
|
+
} catch (error) {
|
|
13
|
+
const { code, name } = error as NodeJS.ErrnoException;
|
|
14
|
+
if (missing.has(String(code)) || missing.has(name)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const pathDelivery = (path: string, stats: Stats, etag: string): PathDelivery => ({
|
|
23
|
+
type: 'path',
|
|
24
|
+
path,
|
|
25
|
+
contentLength: stats.size,
|
|
26
|
+
etag,
|
|
27
|
+
lastModified: stats.mtime,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** A Content delivery from a backend's shared one, typed by `File.mediaType` */
|
|
31
|
+
export const withContentType = <Delivery extends object>(delivery: Delivery | null, contentType: string) =>
|
|
32
|
+
delivery && { ...delivery, contentType };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import { GetObjectCommand, HeadObjectCommand, type S3Client } from '@aws-sdk/client-s3';
|
|
3
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
4
|
+
import type { S3StorageMeta, StorageBackend, StorageLocate, StreamDelivery } from '../backend.js';
|
|
5
|
+
import { keyLocator } from '../location.js';
|
|
6
|
+
import { nullIfMissing, withContentType } from '../results.js';
|
|
7
|
+
|
|
8
|
+
export type S3StorageOptions = {
|
|
9
|
+
/** Your S3 client; arocapi never configures or destroys it */
|
|
10
|
+
client: S3Client;
|
|
11
|
+
bucket: string;
|
|
12
|
+
/** Where each item sits in the bucket, instead of `meta.storageKey` */
|
|
13
|
+
locate?: StorageLocate;
|
|
14
|
+
/** How Content reaches the client: a presigned URL (default), or proxied through arocapi. Metadata always streams */
|
|
15
|
+
delivery?: 'redirect' | 'stream';
|
|
16
|
+
/** Seconds a presigned URL stays valid */
|
|
17
|
+
expiresIn?: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const MISSING: ReadonlySet<string> = new Set(['NoSuchKey', 'NotFound']);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Serve Metadata and Content from an S3-compatible bucket, found from `Entity.meta.storageKey` and `File.meta.storageKey`
|
|
24
|
+
*/
|
|
25
|
+
export const S3Storage = ({
|
|
26
|
+
client,
|
|
27
|
+
bucket,
|
|
28
|
+
locate = {},
|
|
29
|
+
delivery = 'redirect',
|
|
30
|
+
expiresIn = 300,
|
|
31
|
+
}: S3StorageOptions): StorageBackend => {
|
|
32
|
+
const keys = keyLocator<S3StorageMeta>(locate, 'storageKey');
|
|
33
|
+
|
|
34
|
+
const stream = async (key: string | null): Promise<StreamDelivery | null> => {
|
|
35
|
+
if (key === null) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const head = await nullIfMissing(client.send(new HeadObjectCommand({ Bucket: bucket, Key: key })), MISSING);
|
|
40
|
+
if (!head) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
type: 'stream',
|
|
46
|
+
contentLength: Number(head.ContentLength),
|
|
47
|
+
etag: head.ETag,
|
|
48
|
+
lastModified: head.LastModified,
|
|
49
|
+
// IfMatch keeps GET to the version HEAD described; a newer one fails the request
|
|
50
|
+
open: async () => {
|
|
51
|
+
const object = await client.send(new GetObjectCommand({ Bucket: bucket, Key: key, IfMatch: head.ETag }));
|
|
52
|
+
|
|
53
|
+
return object.Body as Readable;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
metadata: async (entity) => stream(await keys.metadata(entity)),
|
|
60
|
+
content: async (file, entity, { contentDisposition }) => {
|
|
61
|
+
const Key = await keys.content(file, entity);
|
|
62
|
+
if (delivery === 'stream') {
|
|
63
|
+
return withContentType(await stream(Key), file.mediaType);
|
|
64
|
+
}
|
|
65
|
+
if (Key === null) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const command = new GetObjectCommand({
|
|
70
|
+
Bucket: bucket,
|
|
71
|
+
Key,
|
|
72
|
+
ResponseContentType: file.mediaType,
|
|
73
|
+
ResponseContentDisposition: contentDisposition,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return { type: 'redirect', url: await getSignedUrl(client, command, { expiresIn }) };
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|
package/src/types/visibility.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { QueryContainer } from '@opensearch-project/opensearch/api/_types/_common.query_dsl.js';
|
|
2
1
|
import type { Prisma } from '../generated/prisma/client.js';
|
|
2
|
+
import type { FieldDeclarations } from '../search/fields.js';
|
|
3
|
+
import type { SearchVisibilityWhere } from '../search/visibility.js';
|
|
3
4
|
import type { RequestContext } from './context.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -12,7 +13,9 @@ export type EntityVisibilityFilter = (
|
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Search visibility filter - required
|
|
15
|
-
* Returns
|
|
16
|
+
* Returns a condition over `id` and the search backend's declared fields matching the entities visible to this request
|
|
16
17
|
* Must agree with the entity visibility filter
|
|
17
18
|
*/
|
|
18
|
-
export type SearchVisibilityFilter
|
|
19
|
+
export type SearchVisibilityFilter<Fields extends FieldDeclarations = FieldDeclarations> = (
|
|
20
|
+
context: RequestContext,
|
|
21
|
+
) => Promise<SearchVisibilityWhere<Fields>> | SearchVisibilityWhere<Fields>;
|
package/src/utils/visibility.ts
CHANGED
|
@@ -16,7 +16,7 @@ export const AllVisibleEntityFilter: EntityVisibilityFilter = () => ({});
|
|
|
16
16
|
*
|
|
17
17
|
* Use together with AllVisibleEntityFilter for repositories with no hidden entities.
|
|
18
18
|
*/
|
|
19
|
-
export const AllVisibleSearchFilter: SearchVisibilityFilter = () => ({
|
|
19
|
+
export const AllVisibleSearchFilter: SearchVisibilityFilter = () => ({});
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Whether a request has no credentials; without isAuthenticated the repository has no logins, so no request is
|
package/dist/app.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|