arocapi 1.0.5 → 1.2.0
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 +597 -29
- package/dist/app.d.ts +27 -6
- package/dist/app.js +76 -17
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +124 -0
- package/dist/app.test.js.map +1 -0
- package/dist/express.d.ts +3 -2
- package/dist/express.js +11 -11
- package/dist/express.js.map +1 -1
- package/dist/index.dev.d.ts +8 -0
- package/dist/index.dev.js +146 -0
- package/dist/index.dev.js.map +1 -0
- package/dist/routes/crate.d.ts +7 -0
- package/dist/routes/crate.js +99 -0
- package/dist/routes/crate.js.map +1 -0
- package/dist/routes/crate.test.d.ts +1 -0
- package/dist/routes/crate.test.js +360 -0
- package/dist/routes/crate.test.js.map +1 -0
- package/dist/routes/entities.d.ts +8 -0
- package/dist/routes/entities.js +75 -0
- package/dist/routes/entities.js.map +1 -0
- package/dist/routes/entities.test.d.ts +1 -0
- package/dist/routes/entities.test.js +245 -0
- package/dist/routes/entities.test.js.map +1 -0
- package/dist/routes/entity.d.ts +8 -0
- package/dist/routes/entity.js +45 -0
- package/dist/routes/entity.js.map +1 -0
- package/dist/routes/entity.test.d.ts +1 -0
- package/dist/routes/entity.test.js +109 -0
- package/dist/routes/entity.test.js.map +1 -0
- package/dist/routes/file.d.ts +7 -0
- package/dist/routes/file.js +111 -0
- package/dist/routes/file.js.map +1 -0
- package/dist/routes/file.test.d.ts +1 -0
- package/dist/routes/file.test.js +319 -0
- package/dist/routes/file.test.js.map +1 -0
- package/dist/routes/files.d.ts +8 -0
- package/dist/routes/files.js +61 -0
- package/dist/routes/files.js.map +1 -0
- package/dist/routes/files.test.d.ts +1 -0
- package/dist/routes/files.test.js +244 -0
- package/dist/routes/files.test.js.map +1 -0
- package/dist/routes/search.d.ts +8 -0
- package/dist/routes/{search/index.js → search.js} +45 -19
- package/dist/routes/search.js.map +1 -0
- package/dist/routes/search.test.d.ts +1 -0
- package/dist/routes/search.test.js +698 -0
- package/dist/routes/search.test.js.map +1 -0
- package/dist/test/helpers/fastify.d.ts +9 -0
- package/dist/test/helpers/fastify.js +24 -0
- package/dist/test/helpers/fastify.js.map +1 -0
- package/dist/test/integration.setup.d.ts +58 -0
- package/dist/test/integration.setup.js +181 -0
- package/dist/test/integration.setup.js.map +1 -0
- package/dist/test/integration.test.d.ts +1 -0
- package/dist/test/integration.test.js +337 -0
- package/dist/test/integration.test.js.map +1 -0
- package/dist/transformers/default.d.ts +45 -0
- package/dist/transformers/default.js +46 -0
- package/dist/transformers/default.js.map +1 -0
- package/dist/transformers/default.test.d.ts +1 -0
- package/dist/transformers/default.test.js +177 -0
- package/dist/transformers/default.test.js.map +1 -0
- package/dist/transformers/transformer.test.d.ts +1 -0
- package/dist/transformers/transformer.test.js +176 -0
- package/dist/transformers/transformer.test.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +41 -0
- package/dist/types/fileHandlers.js +2 -0
- package/dist/types/fileHandlers.js.map +1 -0
- package/dist/types/transformers.d.ts +10 -0
- package/dist/types/transformers.js +2 -0
- package/dist/types/transformers.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +21 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/errors.test.d.ts +1 -0
- package/dist/utils/errors.test.js +66 -0
- package/dist/utils/errors.test.js.map +1 -0
- package/package.json +34 -25
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
- package/prisma/models/entity.prisma +16 -12
- package/prisma/models/file.prisma +20 -0
- package/prisma/schema.prisma +1 -20
- package/prisma.config.ts +6 -7
- package/src/__snapshots__/app.test.ts.snap +8 -0
- package/src/app.test.ts +158 -0
- package/src/app.ts +150 -30
- package/src/express.ts +12 -11
- package/src/index.dev.ts +196 -0
- package/src/reset.d.ts +1 -0
- package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
- package/src/routes/__snapshots__/entity.test.ts.snap +66 -0
- package/src/routes/__snapshots__/search.test.ts.snap +139 -0
- package/src/routes/crate.test.ts +441 -0
- package/src/routes/crate.ts +141 -0
- package/src/routes/entities.test.ts +284 -0
- package/src/routes/entities.ts +101 -0
- package/src/routes/entity.test.ts +132 -0
- package/src/routes/entity.ts +63 -0
- package/src/routes/file.test.ts +399 -0
- package/src/routes/file.ts +153 -0
- package/src/routes/files.test.ts +303 -0
- package/src/routes/files.ts +87 -0
- package/src/routes/search.test.ts +807 -0
- package/src/routes/{search/index.ts → search.ts} +67 -24
- package/src/test/__snapshots__/integration.test.ts.snap +18 -0
- package/src/test/helpers/fastify.ts +39 -0
- package/src/test/integration.setup.ts +200 -0
- package/src/test/integration.test.ts +411 -0
- package/src/transformers/default.test.ts +199 -0
- package/src/transformers/default.ts +165 -0
- package/src/transformers/transformer.test.ts +233 -0
- package/src/types/fileHandlers.ts +65 -0
- package/src/types/transformers.ts +52 -0
- package/src/utils/errors.test.ts +75 -0
- package/src/utils/errors.ts +45 -0
- package/dist/plugins/cors.d.ts +0 -3
- package/dist/plugins/cors.js +0 -6
- package/dist/plugins/cors.js.map +0 -1
- package/dist/plugins/opensearch.d.ts +0 -9
- package/dist/plugins/opensearch.js +0 -22
- package/dist/plugins/opensearch.js.map +0 -1
- package/dist/plugins/prisma.d.ts +0 -8
- package/dist/plugins/prisma.js +0 -11
- package/dist/plugins/prisma.js.map +0 -1
- package/dist/plugins/sensible.d.ts +0 -3
- package/dist/plugins/sensible.js +0 -6
- package/dist/plugins/sensible.js.map +0 -1
- package/dist/plugins/support.d.ts +0 -9
- package/dist/plugins/support.js +0 -5
- package/dist/plugins/support.js.map +0 -1
- package/dist/plugins/validation.d.ts +0 -3
- package/dist/plugins/validation.js +0 -32
- package/dist/plugins/validation.js.map +0 -1
- package/dist/routes/entities/index.d.ts +0 -3
- package/dist/routes/entities/index.js +0 -63
- package/dist/routes/entities/index.js.map +0 -1
- package/dist/routes/entity/index.d.ts +0 -3
- package/dist/routes/entity/index.js +0 -24
- package/dist/routes/entity/index.js.map +0 -1
- package/dist/routes/search/index.d.ts +0 -3
- package/dist/routes/search/index.js.map +0 -1
- package/dist/scripts/loadEntities.js +0 -170
- package/dist/scripts/loadEntities.js.map +0 -1
- package/dist/type.d.ts +0 -6
- package/dist/type.js +0 -2
- package/dist/type.js.map +0 -1
- package/src/plugins/README.md +0 -16
- package/src/plugins/cors.ts +0 -11
- package/src/plugins/opensearch.ts +0 -32
- package/src/plugins/prisma.ts +0 -23
- package/src/plugins/sensible.ts +0 -11
- package/src/plugins/support.ts +0 -20
- package/src/plugins/validation.ts +0 -48
- package/src/routes/README.md +0 -29
- package/src/routes/entities/index.ts +0 -83
- package/src/routes/entity/index.ts +0 -28
- package/src/scripts/loadEntities.ts +0 -223
- package/src/type.ts +0 -7
- /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
package/src/app.ts
CHANGED
|
@@ -1,40 +1,160 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import type { FastifyPluginAsync } from 'fastify';
|
|
1
|
+
import cors from '@fastify/cors';
|
|
2
|
+
import sensible from '@fastify/sensible';
|
|
3
|
+
import type { Client } from '@opensearch-project/opensearch';
|
|
4
|
+
import type { FastifyInstance, FastifyPluginAsync } from 'fastify';
|
|
5
|
+
import fp from 'fastify-plugin';
|
|
6
|
+
import { hasZodFastifySchemaValidationErrors, serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
|
7
|
+
import type { PrismaClient } from './generated/prisma/client.js';
|
|
8
|
+
import crate from './routes/crate.js';
|
|
9
|
+
import entities from './routes/entities.js';
|
|
10
|
+
import entity from './routes/entity.js';
|
|
11
|
+
import file from './routes/file.js';
|
|
12
|
+
import files from './routes/files.js';
|
|
13
|
+
import search from './routes/search.js';
|
|
14
|
+
import type { FileHandler, RoCrateHandler } from './types/fileHandlers.js';
|
|
15
|
+
import type {
|
|
16
|
+
AccessTransformer,
|
|
17
|
+
EntityTransformer,
|
|
18
|
+
FileAccessTransformer,
|
|
19
|
+
FileTransformer,
|
|
20
|
+
} from './types/transformers.js';
|
|
21
|
+
import { createValidationError } from './utils/errors.js';
|
|
5
22
|
|
|
6
|
-
|
|
7
|
-
|
|
23
|
+
export type { AuthorisedEntity, AuthorisedFile, StandardEntity, StandardFile } from './transformers/default.js';
|
|
24
|
+
export { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from './transformers/default.js';
|
|
25
|
+
export type {
|
|
26
|
+
FileHandler,
|
|
27
|
+
FileHandlerContext,
|
|
28
|
+
FileMetadata,
|
|
29
|
+
FilePathResult,
|
|
30
|
+
FileRedirectResult,
|
|
31
|
+
FileResult,
|
|
32
|
+
FileStreamResult,
|
|
33
|
+
GetFileHandler,
|
|
34
|
+
GetRoCrateHandler,
|
|
35
|
+
HeadFileHandler,
|
|
36
|
+
HeadRoCrateHandler,
|
|
37
|
+
RoCrateHandler,
|
|
38
|
+
} from './types/fileHandlers.js';
|
|
39
|
+
export type {
|
|
40
|
+
AccessTransformer,
|
|
41
|
+
EntityTransformer,
|
|
42
|
+
FileAccessTransformer,
|
|
43
|
+
FileTransformer,
|
|
44
|
+
TransformerContext,
|
|
45
|
+
} from './types/transformers.js';
|
|
8
46
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
47
|
+
declare module 'fastify' {
|
|
48
|
+
interface FastifyInstance {
|
|
49
|
+
prisma: PrismaClient;
|
|
50
|
+
opensearch: Client;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
12
53
|
|
|
13
|
-
|
|
14
|
-
|
|
54
|
+
const setupValidation = (fastify: FastifyInstance) => {
|
|
55
|
+
fastify.setValidatorCompiler(validatorCompiler);
|
|
56
|
+
fastify.setSerializerCompiler(serializerCompiler);
|
|
15
57
|
|
|
16
|
-
|
|
17
|
-
|
|
58
|
+
fastify.setErrorHandler((err, _req, reply) => {
|
|
59
|
+
if (hasZodFastifySchemaValidationErrors(err)) {
|
|
60
|
+
return reply.code(400).send(createValidationError('The request parameters are invalid', err.validation));
|
|
61
|
+
}
|
|
18
62
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
dir: path.join(__dirname, 'plugins'),
|
|
26
|
-
options: opts,
|
|
27
|
-
forceESM: true,
|
|
63
|
+
// NOTE: We are exposing the error message here for development purposes.
|
|
64
|
+
// In production, consider hiding error details to avoid leaking sensitive information.
|
|
65
|
+
return reply.code(500).send({
|
|
66
|
+
error: 'Internal Server Error',
|
|
67
|
+
message: err.message,
|
|
68
|
+
});
|
|
28
69
|
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const setupDatabase = async (fastify: FastifyInstance, prisma: PrismaClient) => {
|
|
73
|
+
await prisma.$connect();
|
|
29
74
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
fastify.
|
|
33
|
-
|
|
34
|
-
options: opts,
|
|
35
|
-
forceESM: true,
|
|
75
|
+
fastify.decorate('prisma', prisma);
|
|
76
|
+
|
|
77
|
+
fastify.addHook('onClose', async () => {
|
|
78
|
+
await prisma.$disconnect();
|
|
36
79
|
});
|
|
37
80
|
};
|
|
38
81
|
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
const setupSearch = async (fastify: FastifyInstance, opensearch: Client) => {
|
|
83
|
+
try {
|
|
84
|
+
await opensearch.ping();
|
|
85
|
+
fastify.log.info(`Connected to OpenSearch`);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
fastify.log.error(`Failed to connect to OpenSearch: ${error}`);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fastify.decorate('opensearch', opensearch);
|
|
92
|
+
|
|
93
|
+
fastify.addHook('onClose', () => opensearch.close());
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type Options = {
|
|
97
|
+
prisma: PrismaClient;
|
|
98
|
+
opensearch: Client;
|
|
99
|
+
disableCors?: boolean;
|
|
100
|
+
accessTransformer: AccessTransformer;
|
|
101
|
+
entityTransformers?: EntityTransformer[];
|
|
102
|
+
fileAccessTransformer: FileAccessTransformer;
|
|
103
|
+
fileTransformers?: FileTransformer[];
|
|
104
|
+
fileHandler: FileHandler;
|
|
105
|
+
roCrateHandler: RoCrateHandler;
|
|
106
|
+
};
|
|
107
|
+
const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
108
|
+
const {
|
|
109
|
+
prisma,
|
|
110
|
+
opensearch,
|
|
111
|
+
disableCors = false,
|
|
112
|
+
accessTransformer,
|
|
113
|
+
entityTransformers,
|
|
114
|
+
fileAccessTransformer,
|
|
115
|
+
fileTransformers,
|
|
116
|
+
fileHandler,
|
|
117
|
+
roCrateHandler,
|
|
118
|
+
} = options;
|
|
119
|
+
|
|
120
|
+
if (!prisma) {
|
|
121
|
+
throw new Error('Prisma client is required');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!opensearch) {
|
|
125
|
+
throw new Error('OpenSearch client is required');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (!accessTransformer) {
|
|
129
|
+
throw new Error('accessTransformer is required');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!fileAccessTransformer) {
|
|
133
|
+
throw new Error('fileAccessTransformer is required');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!fileHandler) {
|
|
137
|
+
throw new Error('fileHandler is required');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!roCrateHandler) {
|
|
141
|
+
throw new Error('roCrateHandler is required');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fastify.register(sensible);
|
|
145
|
+
if (!disableCors) {
|
|
146
|
+
fastify.register(cors);
|
|
147
|
+
}
|
|
148
|
+
setupValidation(fastify);
|
|
149
|
+
await setupDatabase(fastify, prisma);
|
|
150
|
+
await setupSearch(fastify, opensearch);
|
|
151
|
+
|
|
152
|
+
fastify.register(entities, { accessTransformer, entityTransformers });
|
|
153
|
+
fastify.register(entity, { accessTransformer, entityTransformers });
|
|
154
|
+
fastify.register(files, { fileAccessTransformer, fileTransformers });
|
|
155
|
+
fastify.register(file, { fileHandler });
|
|
156
|
+
fastify.register(crate, { roCrateHandler });
|
|
157
|
+
fastify.register(search, { accessTransformer, entityTransformers });
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export default fp(app);
|
package/src/express.ts
CHANGED
|
@@ -2,19 +2,20 @@ import type { Express } from 'express';
|
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import Fastify from 'fastify';
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import arocapi, { type Options } from './app.js';
|
|
6
6
|
|
|
7
|
-
const fastify = Fastify(
|
|
8
|
-
logger: true,
|
|
9
|
-
});
|
|
10
|
-
fastify.register(rocapi);
|
|
7
|
+
const fastify = Fastify();
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
export default async (options: Options) => {
|
|
10
|
+
fastify.register(arocapi, options);
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
await fastify.ready();
|
|
15
13
|
|
|
16
|
-
app
|
|
17
|
-
fastify.routing(req, res);
|
|
18
|
-
});
|
|
14
|
+
const app: Express = express();
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
app.use(async (req, res) => {
|
|
17
|
+
fastify.routing(req, res);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return app;
|
|
21
|
+
};
|
package/src/index.dev.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { Client } from '@opensearch-project/opensearch';
|
|
4
|
+
import Fastify from 'fastify';
|
|
5
|
+
|
|
6
|
+
import arocapi, {
|
|
7
|
+
AllPublicAccessTransformer,
|
|
8
|
+
AllPublicFileAccessTransformer,
|
|
9
|
+
type EntityTransformer,
|
|
10
|
+
type FileHandler,
|
|
11
|
+
type RoCrateHandler,
|
|
12
|
+
} from './app.js';
|
|
13
|
+
import type { Entity, File } from './generated/prisma/client.js';
|
|
14
|
+
import { PrismaClient } from './generated/prisma/client.js';
|
|
15
|
+
|
|
16
|
+
declare module 'fastify' {
|
|
17
|
+
interface FastifyInstance {
|
|
18
|
+
prisma: PrismaClient;
|
|
19
|
+
opensearch: Client;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const prisma = new PrismaClient();
|
|
24
|
+
|
|
25
|
+
if (!process.env.OPENSEARCH_URL) {
|
|
26
|
+
throw new Error('OPENSEARCH_URL environment variable is not set');
|
|
27
|
+
}
|
|
28
|
+
const opensearchUrl = process.env.OPENSEARCH_URL;
|
|
29
|
+
const opensearch = new Client({ node: opensearchUrl });
|
|
30
|
+
|
|
31
|
+
const fileHandler: FileHandler = {
|
|
32
|
+
head: async (file: File) => {
|
|
33
|
+
const filePath = (file.meta as { storagePath: string }).storagePath;
|
|
34
|
+
|
|
35
|
+
if (!existsSync(filePath)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const stats = statSync(filePath);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
contentType: file.mediaType,
|
|
43
|
+
contentLength: Number(file.size),
|
|
44
|
+
lastModified: stats.mtime,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
get: async (file: File) => {
|
|
48
|
+
const filePath = (file.meta as { storagePath: string }).storagePath;
|
|
49
|
+
|
|
50
|
+
if (!existsSync(filePath)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const stats = statSync(filePath);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
type: 'stream',
|
|
58
|
+
stream: createReadStream(filePath),
|
|
59
|
+
metadata: {
|
|
60
|
+
contentType: file.mediaType,
|
|
61
|
+
contentLength: Number(file.size),
|
|
62
|
+
lastModified: stats.mtime,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const transformRoCrate = (filePath: string, remapRootTo: string): { rocrate: unknown; jsonString: string } | false => {
|
|
69
|
+
const fileContents = readFileSync(filePath, 'utf-8');
|
|
70
|
+
const rocrate = JSON.parse(fileContents) as {
|
|
71
|
+
'@context'?: unknown;
|
|
72
|
+
'@graph'?: Array<{ '@id': string; [key: string]: unknown }>;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const graph = rocrate['@graph'] || [];
|
|
77
|
+
const rootNode = graph.find((node) => node['@id'] === 'ro-crate-metadata.json') as
|
|
78
|
+
| { about: { '@id': string } }
|
|
79
|
+
| undefined;
|
|
80
|
+
|
|
81
|
+
if (!rootNode) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
rootNode.about['@id'] = remapRootTo;
|
|
86
|
+
|
|
87
|
+
const jsonString = JSON.stringify(rocrate);
|
|
88
|
+
|
|
89
|
+
return { rocrate, jsonString };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const roCrateHandler: RoCrateHandler = {
|
|
93
|
+
head: async (entity: Entity) => {
|
|
94
|
+
const meta = entity.meta as { storagePath: string; remapRootTo: string };
|
|
95
|
+
const filePath = meta.storagePath;
|
|
96
|
+
|
|
97
|
+
if (!existsSync(filePath)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (meta.remapRootTo) {
|
|
102
|
+
const result = transformRoCrate(filePath, meta.remapRootTo);
|
|
103
|
+
|
|
104
|
+
if (!result) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
contentType: 'application/ld+json',
|
|
110
|
+
contentLength: Buffer.byteLength(result.jsonString),
|
|
111
|
+
lastModified: statSync(filePath).mtime,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const stats = statSync(filePath);
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
contentType: 'application/ld+json',
|
|
119
|
+
contentLength: stats.size,
|
|
120
|
+
lastModified: stats.mtime,
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
get: async (entity: Entity) => {
|
|
124
|
+
const meta = entity.meta as { storagePath: string; remapRootTo: string };
|
|
125
|
+
const filePath = meta.storagePath;
|
|
126
|
+
|
|
127
|
+
if (!existsSync(filePath)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (meta.remapRootTo) {
|
|
132
|
+
const result = transformRoCrate(filePath, meta.remapRootTo);
|
|
133
|
+
|
|
134
|
+
if (!result) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
type: 'stream',
|
|
140
|
+
stream: Readable.from(result.jsonString),
|
|
141
|
+
metadata: {
|
|
142
|
+
contentType: 'application/ld+json',
|
|
143
|
+
contentLength: Buffer.byteLength(result.jsonString),
|
|
144
|
+
lastModified: statSync(filePath).mtime,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const stats = statSync(filePath);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
type: 'stream',
|
|
153
|
+
stream: createReadStream(filePath),
|
|
154
|
+
metadata: {
|
|
155
|
+
contentType: 'application/ld+json',
|
|
156
|
+
contentLength: stats.size,
|
|
157
|
+
lastModified: stats.mtime,
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const oniTransformer: EntityTransformer = (entity, { request: _r, fastify: _f }) => {
|
|
164
|
+
return {
|
|
165
|
+
...entity,
|
|
166
|
+
accessControl: 'Public',
|
|
167
|
+
counts: {
|
|
168
|
+
collections: 0,
|
|
169
|
+
objects: 0,
|
|
170
|
+
files: 0,
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const fastify = Fastify({
|
|
176
|
+
logger: true,
|
|
177
|
+
});
|
|
178
|
+
fastify.register(arocapi, {
|
|
179
|
+
prisma,
|
|
180
|
+
opensearch,
|
|
181
|
+
fileHandler,
|
|
182
|
+
roCrateHandler,
|
|
183
|
+
accessTransformer: AllPublicAccessTransformer,
|
|
184
|
+
entityTransformers: [oniTransformer],
|
|
185
|
+
fileAccessTransformer: AllPublicFileAccessTransformer,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const start = async () => {
|
|
189
|
+
try {
|
|
190
|
+
await fastify.listen({ port: 9000 });
|
|
191
|
+
} catch (err) {
|
|
192
|
+
fastify.log.error(err);
|
|
193
|
+
process.exit(1);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
start();
|
package/src/reset.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@total-typescript/ts-reset';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`Entities Route > GET /entities > should apply custom entity transformers 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"entities": [
|
|
6
|
+
{
|
|
7
|
+
"access": {
|
|
8
|
+
"content": true,
|
|
9
|
+
"metadata": true,
|
|
10
|
+
},
|
|
11
|
+
"contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
12
|
+
"description": "First test entity",
|
|
13
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
14
|
+
"id": "http://example.com/entity/1",
|
|
15
|
+
"memberOf": null,
|
|
16
|
+
"metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
17
|
+
"name": "Test Entity 1",
|
|
18
|
+
"rootCollection": null,
|
|
19
|
+
"tested": true,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
|
|
25
|
+
exports[`Entities Route > GET /entities > should return 500 when database error occurs 1`] = `
|
|
26
|
+
{
|
|
27
|
+
"error": {
|
|
28
|
+
"code": "INTERNAL_ERROR",
|
|
29
|
+
"message": "Internal server error",
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
exports[`Entities Route > GET /entities > should return entities with default pagination 1`] = `
|
|
35
|
+
{
|
|
36
|
+
"entities": [
|
|
37
|
+
{
|
|
38
|
+
"access": {
|
|
39
|
+
"content": true,
|
|
40
|
+
"metadata": true,
|
|
41
|
+
},
|
|
42
|
+
"contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
43
|
+
"description": "First test entity",
|
|
44
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
45
|
+
"id": "http://example.com/entity/1",
|
|
46
|
+
"memberOf": null,
|
|
47
|
+
"metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
48
|
+
"name": "Test Entity 1",
|
|
49
|
+
"rootCollection": null,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"access": {
|
|
53
|
+
"content": true,
|
|
54
|
+
"metadata": true,
|
|
55
|
+
},
|
|
56
|
+
"contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
57
|
+
"description": "Second test entity",
|
|
58
|
+
"entityType": "http://pcdm.org/models#Object",
|
|
59
|
+
"id": "http://example.com/entity/2",
|
|
60
|
+
"memberOf": "http://example.com/entity/1",
|
|
61
|
+
"metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
62
|
+
"name": "Test Entity 2",
|
|
63
|
+
"rootCollection": "http://example.com/entity/1",
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
"total": 2,
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`Entity Route > GET /entity/:id > should apply custom entity transformers 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"access": {
|
|
6
|
+
"content": true,
|
|
7
|
+
"metadata": true,
|
|
8
|
+
},
|
|
9
|
+
"contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
10
|
+
"description": "A test entity",
|
|
11
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
12
|
+
"id": "http://example.com/entity/123",
|
|
13
|
+
"memberOf": null,
|
|
14
|
+
"metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
15
|
+
"name": "Test Entity",
|
|
16
|
+
"rootCollection": null,
|
|
17
|
+
"tested": true,
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
exports[`Entity Route > GET /entity/:id > should return 404 when entity not found 1`] = `
|
|
22
|
+
{
|
|
23
|
+
"error": {
|
|
24
|
+
"code": "NOT_FOUND",
|
|
25
|
+
"details": {
|
|
26
|
+
"entityId": "http://example.com/entity/nonexistent",
|
|
27
|
+
},
|
|
28
|
+
"message": "The requested entity was not found",
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
exports[`Entity Route > GET /entity/:id > should return 500 when database error occurs 1`] = `
|
|
34
|
+
{
|
|
35
|
+
"error": {
|
|
36
|
+
"code": "INTERNAL_ERROR",
|
|
37
|
+
"message": "Internal server error",
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
exports[`Entity Route > GET /entity/:id > should return entity when found 1`] = `
|
|
43
|
+
{
|
|
44
|
+
"access": {
|
|
45
|
+
"content": true,
|
|
46
|
+
"metadata": true,
|
|
47
|
+
},
|
|
48
|
+
"contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
49
|
+
"description": "A test entity",
|
|
50
|
+
"entityType": "http://schema.org/Person",
|
|
51
|
+
"id": "http://example.com/entity/123",
|
|
52
|
+
"memberOf": null,
|
|
53
|
+
"metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
|
|
54
|
+
"name": "Test Entity",
|
|
55
|
+
"rootCollection": null,
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
exports[`Entity Route > GET /entity/:id > should validate ID parameter format 1`] = `
|
|
60
|
+
{
|
|
61
|
+
"code": "FST_ERR_VALIDATION",
|
|
62
|
+
"error": "Bad Request",
|
|
63
|
+
"message": "params/id Invalid URI format",
|
|
64
|
+
"statusCode": 400,
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`Search Route > POST /search > should apply custom entity transformers 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"entities": [
|
|
6
|
+
{
|
|
7
|
+
"access": {
|
|
8
|
+
"content": true,
|
|
9
|
+
"metadata": true,
|
|
10
|
+
},
|
|
11
|
+
"contentLicenseId": null,
|
|
12
|
+
"description": "A test entity",
|
|
13
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
14
|
+
"id": "http://example.com/entity/1",
|
|
15
|
+
"memberOf": null,
|
|
16
|
+
"metadataLicenseId": null,
|
|
17
|
+
"name": "Test Entity 1",
|
|
18
|
+
"rootCollection": null,
|
|
19
|
+
"searchExtra": {
|
|
20
|
+
"highlight": {},
|
|
21
|
+
"score": 1.5,
|
|
22
|
+
},
|
|
23
|
+
"tested": true,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
"searchTime": 10,
|
|
27
|
+
"total": 1,
|
|
28
|
+
}
|
|
29
|
+
`;
|
|
30
|
+
|
|
31
|
+
exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
|
|
32
|
+
{
|
|
33
|
+
"error": {
|
|
34
|
+
"code": "INTERNAL_ERROR",
|
|
35
|
+
"message": "Search failed",
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
exports[`Search Route > POST /search > should handle rocrateId explicitly set to undefined 1`] = `
|
|
41
|
+
{
|
|
42
|
+
"error": {
|
|
43
|
+
"code": "INTERNAL_ERROR",
|
|
44
|
+
"message": "Search failed",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
exports[`Search Route > POST /search > should perform basic search successfully 1`] = `
|
|
50
|
+
{
|
|
51
|
+
"entities": [
|
|
52
|
+
{
|
|
53
|
+
"access": {
|
|
54
|
+
"content": true,
|
|
55
|
+
"metadata": true,
|
|
56
|
+
},
|
|
57
|
+
"contentLicenseId": null,
|
|
58
|
+
"description": "A test entity",
|
|
59
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
60
|
+
"id": "http://example.com/entity/1",
|
|
61
|
+
"memberOf": null,
|
|
62
|
+
"metadataLicenseId": null,
|
|
63
|
+
"name": "Test Entity 1",
|
|
64
|
+
"rootCollection": null,
|
|
65
|
+
"searchExtra": {
|
|
66
|
+
"highlight": {
|
|
67
|
+
"name": [
|
|
68
|
+
"<em>Test</em> Entity 1",
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
"score": 1.5,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"access": {
|
|
76
|
+
"content": true,
|
|
77
|
+
"metadata": true,
|
|
78
|
+
},
|
|
79
|
+
"contentLicenseId": null,
|
|
80
|
+
"description": "Another test entity",
|
|
81
|
+
"entityType": "http://pcdm.org/models#Object",
|
|
82
|
+
"id": "http://example.com/entity/2",
|
|
83
|
+
"memberOf": null,
|
|
84
|
+
"metadataLicenseId": null,
|
|
85
|
+
"name": "Test Entity 2",
|
|
86
|
+
"rootCollection": null,
|
|
87
|
+
"searchExtra": {
|
|
88
|
+
"highlight": {
|
|
89
|
+
"description": [
|
|
90
|
+
"Another <em>test</em> entity",
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
"score": 1.2,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
"facets": {
|
|
98
|
+
"entityType": [
|
|
99
|
+
{
|
|
100
|
+
"count": 1,
|
|
101
|
+
"name": "http://pcdm.org/models#Collection",
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"count": 1,
|
|
105
|
+
"name": "http://pcdm.org/models#Object",
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
"searchTime": 10,
|
|
110
|
+
"total": 2,
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
|
|
114
|
+
exports[`Search Route > POST /search > should skip entities not found in database and log warning 1`] = `
|
|
115
|
+
{
|
|
116
|
+
"entities": [
|
|
117
|
+
{
|
|
118
|
+
"access": {
|
|
119
|
+
"content": true,
|
|
120
|
+
"metadata": true,
|
|
121
|
+
},
|
|
122
|
+
"contentLicenseId": null,
|
|
123
|
+
"description": "A test entity",
|
|
124
|
+
"entityType": "http://pcdm.org/models#Collection",
|
|
125
|
+
"id": "http://example.com/entity/1",
|
|
126
|
+
"memberOf": null,
|
|
127
|
+
"metadataLicenseId": null,
|
|
128
|
+
"name": "Test Entity 1",
|
|
129
|
+
"rootCollection": null,
|
|
130
|
+
"searchExtra": {
|
|
131
|
+
"highlight": {},
|
|
132
|
+
"score": 1.5,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
"searchTime": 10,
|
|
137
|
+
"total": 2,
|
|
138
|
+
}
|
|
139
|
+
`;
|