arocapi 5.0.0-beta.1 → 5.0.0-beta.10
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 +37 -802
- package/dist/app.d.ts +38 -20
- package/dist/app.js +62 -40
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +37 -57
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +13 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +37 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +11 -5
- package/dist/routes/metadata.js +24 -68
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +90 -119
- 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 +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.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/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +82 -40
- 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 +168 -95
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +48 -67
- package/src/routes/entity.ts +21 -35
- package/src/routes/file.ts +53 -103
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +42 -98
- package/src/routes/search.ts +131 -151
- 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 +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -82
- package/dist/app.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- 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 -273
- 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 -130
- 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 -315
- 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 -222
- 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 -780
- 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 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -187
- 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 -396
- 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 -163
- 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 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- 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 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -112
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- 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/crate.test.ts +0 -423
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -209
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
|
@@ -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 { setDeliveryHeaders } 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
|
+
setDeliveryHeaders(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
|
+
setDeliveryHeaders(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,78 @@
|
|
|
1
|
+
import { isAbsolute } 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
|
+
type DefaultKeys<Key> = {
|
|
40
|
+
metadata: (entity: Entity) => Key | null;
|
|
41
|
+
content: (file: File) => Key | null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Find each item's key from `locate`, or else from `meta`, throwing through `check` on an unsafe one
|
|
46
|
+
*/
|
|
47
|
+
export const locator = <Key>(
|
|
48
|
+
locate: StorageLocate<Key>,
|
|
49
|
+
defaults: DefaultKeys<Key>,
|
|
50
|
+
check: (key: Key, id: string) => unknown,
|
|
51
|
+
) => {
|
|
52
|
+
const checked = (key: Key | null, id: string) => {
|
|
53
|
+
if (key !== null) {
|
|
54
|
+
check(key, id);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return key;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
metadata: async (entity: Entity) => checked(await (locate.metadata ?? defaults.metadata)(entity), entity.id),
|
|
62
|
+
content: async (file: File, entity: Entity) =>
|
|
63
|
+
checked(await (locate.content ?? defaults.content)(file, entity), file.id),
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A `locator` for string keys read from the same `meta` key of `Meta` on entities and files
|
|
69
|
+
*/
|
|
70
|
+
export const keyLocator = <Meta>(locate: StorageLocate, key: Extract<keyof Meta, string>) =>
|
|
71
|
+
locator(
|
|
72
|
+
locate,
|
|
73
|
+
{
|
|
74
|
+
metadata: (entity) => metaString<Meta>(entity.meta, entity.id, key),
|
|
75
|
+
content: (file) => metaString<Meta>(file.meta, file.id, key),
|
|
76
|
+
},
|
|
77
|
+
safeKey,
|
|
78
|
+
);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { openStorageRoot } from '@ocfl/ocfl-fs';
|
|
2
|
+
import type { Entity, File } from '../../generated/prisma/client.js';
|
|
3
|
+
import type { OcflLocation, OcflStorageMeta, StorageBackend, StorageLocate, StreamDelivery } from '../backend.js';
|
|
4
|
+
import { locator, metaString, safeKey } from '../location.js';
|
|
5
|
+
import { nullIfMissing, withContentType } from '../results.js';
|
|
6
|
+
|
|
7
|
+
export type OcflStorageOptions = {
|
|
8
|
+
/** The OCFL storage root, whose ocfl_layout.json names a storage layout @ocfl/ocfl-fs implements */
|
|
9
|
+
root: string;
|
|
10
|
+
/** Where each item sits, instead of `meta.ocflObject` and `meta.ocflPath` */
|
|
11
|
+
locate?: StorageLocate<OcflLocation>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const METADATA_PATH = 'ro-crate-metadata.json';
|
|
15
|
+
|
|
16
|
+
const MISSING: ReadonlySet<string> = new Set(['OBJECT_NOT_FOUND']);
|
|
17
|
+
|
|
18
|
+
const metaKey = (row: Entity | File, key: keyof OcflStorageMeta) => metaString<OcflStorageMeta>(row.meta, row.id, key);
|
|
19
|
+
|
|
20
|
+
const defaultLocations = {
|
|
21
|
+
metadata: (entity: Entity): OcflLocation | null => {
|
|
22
|
+
const object = metaKey(entity, 'ocflObject');
|
|
23
|
+
const path = metaKey(entity, 'ocflPath') ?? METADATA_PATH;
|
|
24
|
+
|
|
25
|
+
return object === null ? null : { object, path };
|
|
26
|
+
},
|
|
27
|
+
content: (file: File): OcflLocation | null => {
|
|
28
|
+
const object = metaKey(file, 'ocflObject');
|
|
29
|
+
const path = metaKey(file, 'ocflPath');
|
|
30
|
+
|
|
31
|
+
return object === null || path === null ? null : { object, path };
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** An object id is mapped by the layout, so only the logical path is a key */
|
|
36
|
+
const safeLocation = ({ path }: OcflLocation, id: string) => safeKey(path, id);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Serve Metadata and Content from the head version of objects in an OCFL storage root on local disk
|
|
40
|
+
* Throws an `OcflError` when `root` isn't a storage root, or uses a storage layout @ocfl/ocfl-fs doesn't implement
|
|
41
|
+
*/
|
|
42
|
+
export const OcflStorage = async ({ root, locate = {} }: OcflStorageOptions): Promise<StorageBackend> => {
|
|
43
|
+
const storageRoot = await openStorageRoot(root);
|
|
44
|
+
const locations = locator(locate, defaultLocations, safeLocation);
|
|
45
|
+
|
|
46
|
+
const deliver = async (location: OcflLocation | null): Promise<StreamDelivery | null> => {
|
|
47
|
+
if (location === null) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const object = await nullIfMissing(storageRoot.open(location.object), MISSING);
|
|
52
|
+
const file = object?.head.files.get(location.path);
|
|
53
|
+
if (!file) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
type: 'stream',
|
|
59
|
+
open: () => file.stream(),
|
|
60
|
+
contentLength: await file.size(),
|
|
61
|
+
etag: `"${file.digest}"`,
|
|
62
|
+
lastModified: file.modifiedAt,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
metadata: async (entity) => deliver(await locations.metadata(entity)),
|
|
68
|
+
content: async (file, entity) =>
|
|
69
|
+
withContentType(await deliver(await locations.content(file, entity)), file.mediaType),
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -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
|
+
};
|