arocapi 5.0.0-beta.6 → 5.0.0-beta.8
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/README.md +10 -10
- package/dist/app.d.ts +3 -4
- package/dist/app.js +10 -7
- package/dist/app.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/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/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/package.json +27 -7
- package/src/app.ts +32 -24
- package/src/routes/file.ts +23 -99
- package/src/routes/metadata.ts +24 -86
- 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/utils/headers.ts +22 -2
- package/dist/types/fileHandlers.d.ts +0 -37
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/src/types/fileHandlers.ts +0 -57
|
@@ -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/utils/headers.ts
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import type { FastifyReply } from 'fastify';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/** Sandboxes deposited bytes; see docs/adr/0005-library-sandboxes-delivered-bytes.md */
|
|
4
|
+
const contentSecurityPolicy = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
|
|
5
|
+
|
|
6
|
+
const token = "[!#$%&'*+\\-.^_`|~0-9a-z]+";
|
|
7
|
+
const quotedString = '"(?:[^"\\\\]|\\\\.)*"';
|
|
8
|
+
const mediaType = new RegExp(
|
|
9
|
+
`^(?:audio|video)/${token}(?:[ \\t]*;[ \\t]*${token}=(?:${token}|${quotedString}))*[ \\t]*$`,
|
|
10
|
+
'i',
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
// Chrome won't play media opened directly under this CSP, and a media document can't run script.
|
|
14
|
+
// RFC 9110 grammar, so no comma outside quotes: browsers split the header as a list, respecting
|
|
15
|
+
// quotes, and take the last type, so `audio/mpeg, text/html` is HTML
|
|
16
|
+
const isMedia = (contentType: string) => mediaType.test(contentType);
|
|
17
|
+
|
|
18
|
+
export const setDeliveryHeaders = (
|
|
4
19
|
reply: FastifyReply,
|
|
5
20
|
metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
|
|
6
21
|
) => {
|
|
7
22
|
reply.header('Content-Type', metadata.contentType);
|
|
8
23
|
reply.header('Content-Length', metadata.contentLength.toString());
|
|
9
|
-
|
|
10
24
|
if (metadata.etag) {
|
|
11
25
|
reply.header('ETag', metadata.etag);
|
|
12
26
|
}
|
|
13
27
|
if (metadata.lastModified) {
|
|
14
28
|
reply.header('Last-Modified', metadata.lastModified.toUTCString());
|
|
15
29
|
}
|
|
30
|
+
|
|
31
|
+
// Last, so a throw above can't leave them on the 500
|
|
32
|
+
reply.header('X-Content-Type-Options', 'nosniff');
|
|
33
|
+
if (!isMedia(metadata.contentType)) {
|
|
34
|
+
reply.header('Content-Security-Policy', contentSecurityPolicy);
|
|
35
|
+
}
|
|
16
36
|
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream';
|
|
2
|
-
import type { Entity, File } from '../generated/prisma/client.js';
|
|
3
|
-
import type { RequestContext } from './context.js';
|
|
4
|
-
export type FileMetadata = {
|
|
5
|
-
contentType: string;
|
|
6
|
-
contentLength: number;
|
|
7
|
-
etag?: string;
|
|
8
|
-
lastModified?: Date;
|
|
9
|
-
};
|
|
10
|
-
export type FileRedirectResult = {
|
|
11
|
-
type: 'redirect';
|
|
12
|
-
url: string;
|
|
13
|
-
};
|
|
14
|
-
export type FileStreamResult = {
|
|
15
|
-
type: 'stream';
|
|
16
|
-
stream: Readable | ReadableStream;
|
|
17
|
-
metadata: FileMetadata;
|
|
18
|
-
};
|
|
19
|
-
export type FilePathResult = {
|
|
20
|
-
type: 'file';
|
|
21
|
-
path: string;
|
|
22
|
-
metadata: FileMetadata;
|
|
23
|
-
accelPath?: string;
|
|
24
|
-
};
|
|
25
|
-
export type FileResult = FileRedirectResult | FileStreamResult | FilePathResult;
|
|
26
|
-
export type GetFileHandler = (file: File, context: RequestContext) => Promise<FileResult | false> | FileResult | false;
|
|
27
|
-
export type HeadFileHandler = (file: File, context: RequestContext) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
28
|
-
export type FileHandler = {
|
|
29
|
-
get: GetFileHandler;
|
|
30
|
-
head: HeadFileHandler;
|
|
31
|
-
};
|
|
32
|
-
export type GetRoCrateHandler = (entity: Entity, context: RequestContext) => Promise<FileResult | false> | FileResult | false;
|
|
33
|
-
export type HeadRoCrateHandler = (entity: Entity, context: RequestContext) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
34
|
-
export type RoCrateHandler = {
|
|
35
|
-
get: GetRoCrateHandler;
|
|
36
|
-
head: HeadRoCrateHandler;
|
|
37
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fileHandlers.js","sourceRoot":"","sources":["../../src/types/fileHandlers.ts"],"names":[],"mappings":""}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream';
|
|
2
|
-
import type { Entity, File } from '../generated/prisma/client.js';
|
|
3
|
-
import type { RequestContext } from './context.js';
|
|
4
|
-
|
|
5
|
-
export type FileMetadata = {
|
|
6
|
-
contentType: string;
|
|
7
|
-
contentLength: number;
|
|
8
|
-
etag?: string;
|
|
9
|
-
lastModified?: Date;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type FileRedirectResult = {
|
|
13
|
-
type: 'redirect';
|
|
14
|
-
url: string;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type FileStreamResult = {
|
|
18
|
-
type: 'stream';
|
|
19
|
-
stream: Readable | ReadableStream;
|
|
20
|
-
metadata: FileMetadata;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type FilePathResult = {
|
|
24
|
-
type: 'file';
|
|
25
|
-
path: string; // Absolute file path on disk
|
|
26
|
-
metadata: FileMetadata;
|
|
27
|
-
accelPath?: string; // Optional nginx internal path for X-Accel-Redirect
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type FileResult = FileRedirectResult | FileStreamResult | FilePathResult;
|
|
31
|
-
|
|
32
|
-
export type GetFileHandler = (file: File, context: RequestContext) => Promise<FileResult | false> | FileResult | false;
|
|
33
|
-
|
|
34
|
-
export type HeadFileHandler = (
|
|
35
|
-
file: File,
|
|
36
|
-
context: RequestContext,
|
|
37
|
-
) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
38
|
-
|
|
39
|
-
export type FileHandler = {
|
|
40
|
-
get: GetFileHandler;
|
|
41
|
-
head: HeadFileHandler;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type GetRoCrateHandler = (
|
|
45
|
-
entity: Entity,
|
|
46
|
-
context: RequestContext,
|
|
47
|
-
) => Promise<FileResult | false> | FileResult | false;
|
|
48
|
-
|
|
49
|
-
export type HeadRoCrateHandler = (
|
|
50
|
-
entity: Entity,
|
|
51
|
-
context: RequestContext,
|
|
52
|
-
) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
53
|
-
|
|
54
|
-
export type RoCrateHandler = {
|
|
55
|
-
get: GetRoCrateHandler;
|
|
56
|
-
head: HeadRoCrateHandler;
|
|
57
|
-
};
|