arocapi 5.0.0-beta.6 → 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.
Files changed (53) hide show
  1. package/dist/app.d.ts +3 -4
  2. package/dist/app.js +10 -7
  3. package/dist/app.js.map +1 -1
  4. package/dist/routes/file.d.ts +2 -2
  5. package/dist/routes/file.js +25 -72
  6. package/dist/routes/file.js.map +1 -1
  7. package/dist/routes/metadata.d.ts +5 -5
  8. package/dist/routes/metadata.js +22 -61
  9. package/dist/routes/metadata.js.map +1 -1
  10. package/dist/storage/backend.d.ts +58 -0
  11. package/dist/storage/backend.js +2 -0
  12. package/dist/storage/backend.js.map +1 -0
  13. package/dist/storage/deliver.d.ts +12 -0
  14. package/dist/storage/deliver.js +34 -0
  15. package/dist/storage/deliver.js.map +1 -0
  16. package/dist/storage/directory/directory.d.ts +6 -0
  17. package/dist/storage/directory/directory.js +24 -0
  18. package/dist/storage/directory/directory.js.map +1 -0
  19. package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
  20. package/dist/storage/entity-meta/entity-meta.js +14 -0
  21. package/dist/storage/entity-meta/entity-meta.js.map +1 -0
  22. package/dist/storage/location.d.ts +20 -0
  23. package/dist/storage/location.js +40 -0
  24. package/dist/storage/location.js.map +1 -0
  25. package/dist/storage/ocfl/ocfl.d.ts +7 -0
  26. package/dist/storage/ocfl/ocfl.js +79 -0
  27. package/dist/storage/ocfl/ocfl.js.map +1 -0
  28. package/dist/storage/ocfl/ocflFs.d.ts +7 -0
  29. package/dist/storage/ocfl/ocflFs.js +33 -0
  30. package/dist/storage/ocfl/ocflFs.js.map +1 -0
  31. package/dist/storage/results.d.ts +7 -0
  32. package/dist/storage/results.js +22 -0
  33. package/dist/storage/results.js.map +1 -0
  34. package/dist/storage/s3/s3.d.ts +10 -0
  35. package/dist/storage/s3/s3.js +47 -0
  36. package/dist/storage/s3/s3.js.map +1 -0
  37. package/package.json +23 -3
  38. package/src/app.ts +32 -24
  39. package/src/routes/file.ts +23 -99
  40. package/src/routes/metadata.ts +24 -86
  41. package/src/storage/backend.ts +75 -0
  42. package/src/storage/deliver.ts +65 -0
  43. package/src/storage/directory/directory.ts +39 -0
  44. package/src/storage/entity-meta/entity-meta.ts +25 -0
  45. package/src/storage/location.ts +85 -0
  46. package/src/storage/ocfl/ocfl.ts +129 -0
  47. package/src/storage/ocfl/ocflFs.ts +67 -0
  48. package/src/storage/results.ts +32 -0
  49. package/src/storage/s3/s3.ts +79 -0
  50. package/dist/types/fileHandlers.d.ts +0 -37
  51. package/dist/types/fileHandlers.js +0 -2
  52. package/dist/types/fileHandlers.js.map +0 -1
  53. package/src/types/fileHandlers.ts +0 -57
@@ -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
+ };
@@ -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,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=fileHandlers.js.map
@@ -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
- };