arocapi 5.0.0-beta.5 → 5.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +2 -2
- package/dist/app.d.ts +22 -17
- package/dist/app.js +22 -16
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +2 -2
- package/dist/routes/capabilities.js +2 -2
- package/dist/routes/capabilities.js.map +1 -1
- package/dist/routes/entities.js +8 -9
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/file.d.ts +2 -2
- package/dist/routes/file.js +25 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/metadata.d.ts +5 -5
- package/dist/routes/metadata.js +22 -61
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +2 -4
- package/dist/routes/search.js +75 -97
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +20 -0
- package/dist/storage/location.js +40 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +7 -0
- package/dist/storage/ocfl/ocfl.js +79 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/ocfl/ocflFs.d.ts +7 -0
- package/dist/storage/ocfl/ocflFs.js +33 -0
- package/dist/storage/ocfl/ocflFs.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/types/visibility.d.ts +3 -2
- package/dist/utils/visibility.js +1 -1
- package/dist/utils/visibility.js.map +1 -1
- package/package.json +66 -26
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +85 -48
- package/src/routes/capabilities.ts +4 -4
- package/src/routes/entities.ts +10 -9
- package/src/routes/file.ts +23 -99
- package/src/routes/metadata.ts +24 -86
- package/src/routes/search.ts +104 -118
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +85 -0
- package/src/storage/ocfl/ocfl.ts +129 -0
- package/src/storage/ocfl/ocflFs.ts +67 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/types/visibility.ts +6 -3
- package/src/utils/visibility.ts +1 -1
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -67
- package/dist/app.test.js.map +0 -1
- package/dist/errorHandling.test.d.ts +0 -1
- package/dist/errorHandling.test.js +0 -222
- package/dist/errorHandling.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -157
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/capabilities.test.d.ts +0 -1
- package/dist/routes/capabilities.test.js +0 -81
- package/dist/routes/capabilities.test.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -390
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -277
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -136
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -435
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -245
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -833
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -17
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -47
- package/dist/test/integration.setup.js +0 -195
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -762
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -225
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -165
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -37
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -79
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -47
- package/dist/utils/queryBuilder.js +0 -107
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -91
- package/src/errorHandling.test.ts +0 -288
- package/src/index.dev.ts +0 -202
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/capabilities.test.ts +0 -102
- package/src/routes/crate.test.ts +0 -484
- package/src/routes/entities.test.ts +0 -324
- package/src/routes/entity.test.ts +0 -166
- package/src/routes/file.test.ts +0 -538
- package/src/routes/files.test.ts +0 -304
- package/src/routes/search.test.ts +0 -959
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -26
- package/src/test/integration.setup.ts +0 -220
- package/src/test/integration.test.ts +0 -953
- package/src/transformers/default.test.ts +0 -276
- package/src/transformers/transformer.test.ts +0 -226
- package/src/types/fileHandlers.ts +0 -57
- package/src/utils/errors.test.ts +0 -90
- package/src/utils/queryBuilder.ts +0 -157
package/src/routes/file.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import { createReadStream } from 'node:fs';
|
|
2
1
|
import { create as contentDisposition } from 'content-disposition';
|
|
3
2
|
import type { FastifyPluginAsync } from 'fastify';
|
|
4
3
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
5
4
|
import { z } from 'zod';
|
|
6
5
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
6
|
+
import type { ContentSource } from '../storage/backend.js';
|
|
7
|
+
import { sendDelivery } from '../storage/deliver.js';
|
|
7
8
|
import type { AccessFunction } from '../types/access.js';
|
|
8
|
-
import type { RequestContext } from '../types/context.js';
|
|
9
|
-
import type { FileHandler, FileMetadata } from '../types/fileHandlers.js';
|
|
10
9
|
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
11
10
|
import { createAccessGuard } from '../utils/access.js';
|
|
12
11
|
import { createNotFoundError } from '../utils/errors.js';
|
|
13
|
-
import { setFileHeaders } from '../utils/headers.js';
|
|
14
12
|
import { createNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
15
13
|
|
|
16
14
|
const paramsSchema = z.object({
|
|
@@ -29,29 +27,14 @@ const querySchema = z.object({
|
|
|
29
27
|
|
|
30
28
|
type FileRouteOptions = {
|
|
31
29
|
prisma: PrismaClient;
|
|
32
|
-
|
|
30
|
+
content: ContentSource;
|
|
33
31
|
access: AccessFunction;
|
|
34
32
|
entityVisibilityFilter: EntityVisibilityFilter;
|
|
35
33
|
refusal: RefusalOptions;
|
|
36
34
|
};
|
|
37
35
|
|
|
38
36
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
39
|
-
const { prisma,
|
|
40
|
-
|
|
41
|
-
// Split the entity off so handlers receive only the File they're typed for
|
|
42
|
-
const findVisibleFile = async (id: string, context: RequestContext) => {
|
|
43
|
-
const found = await prisma.file.findFirst({
|
|
44
|
-
where: { id, entity: await entityVisibilityFilter(context) },
|
|
45
|
-
include: { entity: { omit: { meta: true } } },
|
|
46
|
-
});
|
|
47
|
-
if (!found) {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const { entity, ...file } = found;
|
|
52
|
-
|
|
53
|
-
return { file, entity };
|
|
54
|
-
};
|
|
37
|
+
const { prisma, content, access, entityVisibilityFilter, refusal } = opts;
|
|
55
38
|
|
|
56
39
|
const denyContent = createAccessGuard({ access, isAuthenticated: refusal.isAuthenticated, level: 'content' });
|
|
57
40
|
|
|
@@ -61,101 +44,42 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
61
44
|
exists: async (id) => (await prisma.file.count({ where: { id } })) > 0,
|
|
62
45
|
});
|
|
63
46
|
|
|
64
|
-
fastify.withTypeProvider<ZodTypeProvider>().
|
|
65
|
-
'
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
47
|
+
fastify.withTypeProvider<ZodTypeProvider>().route({
|
|
48
|
+
method: ['GET', 'HEAD'],
|
|
49
|
+
url: '/file/:id',
|
|
50
|
+
schema: {
|
|
51
|
+
params: paramsSchema,
|
|
52
|
+
querystring: querySchema,
|
|
70
53
|
},
|
|
71
|
-
async (request, reply) => {
|
|
54
|
+
handler: async (request, reply) => {
|
|
72
55
|
const { id } = request.params;
|
|
73
56
|
|
|
74
57
|
const context = { request, fastify };
|
|
75
|
-
const
|
|
58
|
+
const found = await prisma.file.findFirst({
|
|
59
|
+
where: { id, entity: await entityVisibilityFilter(context) },
|
|
60
|
+
include: { entity: true },
|
|
61
|
+
});
|
|
76
62
|
|
|
77
|
-
if (!
|
|
63
|
+
if (!found) {
|
|
78
64
|
return sendFileNotVisible(reply, id, context);
|
|
79
65
|
}
|
|
80
66
|
|
|
81
|
-
const {
|
|
67
|
+
const { entity, ...file } = found;
|
|
82
68
|
if (await denyContent(reply, entity, context)) {
|
|
83
69
|
return reply;
|
|
84
70
|
}
|
|
85
71
|
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
setFileHeaders(reply, metadata);
|
|
93
|
-
|
|
94
|
-
return reply.code(200).send();
|
|
95
|
-
},
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
99
|
-
'/file/:id',
|
|
100
|
-
{
|
|
101
|
-
schema: {
|
|
102
|
-
params: paramsSchema,
|
|
103
|
-
querystring: querySchema,
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
async (request, reply) => {
|
|
107
|
-
const { id } = request.params;
|
|
108
|
-
|
|
109
|
-
const context = { request, fastify };
|
|
110
|
-
const visible = await findVisibleFile(id, context);
|
|
111
|
-
|
|
112
|
-
if (!visible) {
|
|
113
|
-
return sendFileNotVisible(reply, id, context);
|
|
114
|
-
}
|
|
72
|
+
const { disposition, filename, noRedirect } = request.query;
|
|
73
|
+
const disposed = contentDisposition(filename || file.filename, { type: disposition });
|
|
74
|
+
const delivery = await content(file, entity, { ...context, contentDisposition: disposed });
|
|
115
75
|
|
|
116
|
-
|
|
117
|
-
if (await denyContent(reply, entity, context)) {
|
|
118
|
-
return reply;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const result = await fileHandler.get(file, context);
|
|
122
|
-
|
|
123
|
-
if (!result) {
|
|
76
|
+
if (!delivery) {
|
|
124
77
|
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
125
78
|
}
|
|
126
79
|
|
|
127
|
-
|
|
128
|
-
if (request.query.noRedirect) {
|
|
129
|
-
return reply.code(200).send({ location: result.url });
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return reply.code(302).redirect(result.url);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const { disposition, filename: customFilename } = request.query;
|
|
136
|
-
setFileHeaders(reply, result.metadata);
|
|
137
|
-
reply.header('Content-Disposition', contentDisposition(customFilename || file.filename, { type: disposition }));
|
|
138
|
-
|
|
139
|
-
// Handle stream response
|
|
140
|
-
if (result.type === 'stream') {
|
|
141
|
-
return reply.code(200).send(result.stream);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (result.type === 'file') {
|
|
145
|
-
if (result.accelPath) {
|
|
146
|
-
reply.header('X-Accel-Redirect', result.accelPath);
|
|
147
|
-
return reply.code(200).send();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const stream = createReadStream(result.path);
|
|
151
|
-
return reply.code(200).send(stream);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Exhaustiveness check - should never reach here with proper types
|
|
155
|
-
reply.removeHeader('Content-Disposition');
|
|
156
|
-
throw new Error(`Unexpected file result type: ${(result as { type: string }).type}`);
|
|
80
|
+
return sendDelivery(request, reply, delivery, { contentDisposition: disposed, noRedirect });
|
|
157
81
|
},
|
|
158
|
-
);
|
|
82
|
+
});
|
|
159
83
|
};
|
|
160
84
|
|
|
161
85
|
export default file;
|
package/src/routes/metadata.ts
CHANGED
|
@@ -1,50 +1,44 @@
|
|
|
1
|
-
import { createReadStream } from 'node:fs';
|
|
2
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
3
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
3
|
import { z } from 'zod';
|
|
5
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
+
import type { MetadataSource } from '../storage/backend.js';
|
|
6
|
+
import { sendDelivery } from '../storage/deliver.js';
|
|
6
7
|
import type { AccessFunction } from '../types/access.js';
|
|
7
|
-
import type { RequestContext } from '../types/context.js';
|
|
8
|
-
import type { FileMetadata, RoCrateHandler } from '../types/fileHandlers.js';
|
|
9
8
|
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
10
9
|
import { createAccessGuard } from '../utils/access.js';
|
|
11
10
|
import { createNotFoundError } from '../utils/errors.js';
|
|
12
|
-
import { setFileHeaders } from '../utils/headers.js';
|
|
13
11
|
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
14
12
|
|
|
15
13
|
const paramsSchema = z.object({
|
|
16
14
|
id: z.url(),
|
|
17
15
|
});
|
|
18
16
|
|
|
19
|
-
type
|
|
17
|
+
type MetadataRouteOptions = {
|
|
20
18
|
prisma: PrismaClient;
|
|
21
|
-
|
|
19
|
+
metadata: MetadataSource;
|
|
22
20
|
access: AccessFunction;
|
|
23
21
|
entityVisibilityFilter: EntityVisibilityFilter;
|
|
24
22
|
refusal: RefusalOptions;
|
|
25
23
|
};
|
|
26
24
|
|
|
27
|
-
const
|
|
28
|
-
const { prisma,
|
|
29
|
-
|
|
30
|
-
const findVisibleEntity = async (id: string, context: RequestContext) =>
|
|
31
|
-
prisma.entity.findFirst({ where: { AND: [{ id }, await entityVisibilityFilter(context)] } });
|
|
25
|
+
const metadataRoute: FastifyPluginAsync<MetadataRouteOptions> = async (fastify, opts) => {
|
|
26
|
+
const { prisma, metadata, access, entityVisibilityFilter, refusal } = opts;
|
|
32
27
|
|
|
33
28
|
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
34
29
|
const denyMetadata = createAccessGuard({ access, isAuthenticated: refusal.isAuthenticated, level: 'metadata' });
|
|
35
30
|
|
|
36
|
-
fastify.withTypeProvider<ZodTypeProvider>().
|
|
37
|
-
'
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
31
|
+
fastify.withTypeProvider<ZodTypeProvider>().route({
|
|
32
|
+
method: ['GET', 'HEAD'],
|
|
33
|
+
url: '/entity/:id/metadata',
|
|
34
|
+
schema: {
|
|
35
|
+
params: paramsSchema,
|
|
42
36
|
},
|
|
43
|
-
async (request, reply) => {
|
|
37
|
+
handler: async (request, reply) => {
|
|
44
38
|
const { id } = request.params;
|
|
45
39
|
|
|
46
40
|
const context = { request, fastify };
|
|
47
|
-
const entity = await
|
|
41
|
+
const entity = await prisma.entity.findFirst({ where: { AND: [{ id }, await entityVisibilityFilter(context)] } });
|
|
48
42
|
|
|
49
43
|
if (!entity) {
|
|
50
44
|
return sendEntityNotVisible(reply, id, context);
|
|
@@ -54,76 +48,20 @@ const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
|
|
|
54
48
|
return reply;
|
|
55
49
|
}
|
|
56
50
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
if (!metadata) {
|
|
60
|
-
return reply.code(404).send(createNotFoundError('The requested RO-Crate metadata was not found', id));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
setFileHeaders(reply, metadata);
|
|
64
|
-
|
|
65
|
-
return reply.code(200).send();
|
|
66
|
-
},
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
70
|
-
'/entity/:id/metadata',
|
|
71
|
-
{
|
|
72
|
-
schema: {
|
|
73
|
-
params: paramsSchema,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
async (request, reply) => {
|
|
77
|
-
const { id } = request.params;
|
|
78
|
-
|
|
79
|
-
const context = { request, fastify };
|
|
80
|
-
const entity = await findVisibleEntity(id, context);
|
|
81
|
-
|
|
82
|
-
if (!entity) {
|
|
83
|
-
return sendEntityNotVisible(reply, id, context);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (await denyMetadata(reply, entity, context)) {
|
|
87
|
-
return reply;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const result = await roCrateHandler.get(entity, context);
|
|
91
|
-
|
|
92
|
-
if (!result) {
|
|
93
|
-
return reply.code(404).send(createNotFoundError('The requested RO-Crate could not be retrieved', id));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (result.type === 'redirect') {
|
|
97
|
-
return reply.code(302).redirect(result.url);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Set content type to application/ld+json for RO-Crate
|
|
101
|
-
const metadata = {
|
|
102
|
-
...result.metadata,
|
|
103
|
-
contentType: 'application/ld+json',
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
setFileHeaders(reply, metadata);
|
|
107
|
-
|
|
108
|
-
// Handle stream response
|
|
109
|
-
if (result.type === 'stream') {
|
|
110
|
-
return reply.code(200).send(result.stream);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (result.type === 'file') {
|
|
114
|
-
if (result.accelPath) {
|
|
115
|
-
reply.header('X-Accel-Redirect', result.accelPath);
|
|
116
|
-
return reply.code(200).send();
|
|
117
|
-
}
|
|
51
|
+
const delivery = await metadata(entity, context);
|
|
118
52
|
|
|
119
|
-
|
|
120
|
-
return reply.code(
|
|
53
|
+
if (!delivery) {
|
|
54
|
+
return reply.code(404).send(createNotFoundError('The requested Metadata was not found', id));
|
|
121
55
|
}
|
|
122
56
|
|
|
123
|
-
|
|
124
|
-
|
|
57
|
+
return sendDelivery(
|
|
58
|
+
request,
|
|
59
|
+
reply,
|
|
60
|
+
delivery.type === 'redirect' ? delivery : { ...delivery, contentType: 'application/ld+json' },
|
|
61
|
+
{ json: true },
|
|
62
|
+
);
|
|
125
63
|
},
|
|
126
|
-
);
|
|
64
|
+
});
|
|
127
65
|
};
|
|
128
66
|
|
|
129
|
-
export default
|
|
67
|
+
export default metadataRoute;
|
package/src/routes/search.ts
CHANGED
|
@@ -1,60 +1,86 @@
|
|
|
1
|
-
import type { Client } from '@opensearch-project/opensearch';
|
|
2
|
-
import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
|
|
3
|
-
import type { Search_Request, Search_Response } from '@opensearch-project/opensearch/api/index.js';
|
|
4
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
5
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
6
3
|
import { z } from 'zod';
|
|
7
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
+
import { normaliseQuery } from '../search/advanced.js';
|
|
6
|
+
import {
|
|
7
|
+
type SearchBackend,
|
|
8
|
+
type SearchExtra,
|
|
9
|
+
SearchQueryError,
|
|
10
|
+
type SearchRequest,
|
|
11
|
+
type SearchResult,
|
|
12
|
+
} from '../search/backend.js';
|
|
13
|
+
import {
|
|
14
|
+
boundingBoxSchema,
|
|
15
|
+
filterValueSchema,
|
|
16
|
+
geoViolations,
|
|
17
|
+
splitFilters,
|
|
18
|
+
type Violation,
|
|
19
|
+
} from '../search/requestFilters.js';
|
|
8
20
|
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
9
|
-
import type { AccessFunction } from '../types/access.js';
|
|
21
|
+
import type { AccessFunction, AccessInfo } from '../types/access.js';
|
|
10
22
|
import type { EntityTransformer } from '../types/transformers.js';
|
|
11
23
|
import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
|
|
12
|
-
import { createError, ERROR_CODES } from '../utils/errors.js';
|
|
13
|
-
import type { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
|
|
14
|
-
|
|
15
|
-
const boundingBoxSchema = z.object({
|
|
16
|
-
topRight: z.object({
|
|
17
|
-
lat: z.number(),
|
|
18
|
-
lng: z.number(),
|
|
19
|
-
}),
|
|
20
|
-
bottomLeft: z.object({
|
|
21
|
-
lat: z.number(),
|
|
22
|
-
lng: z.number(),
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
24
|
+
import { createError, createValidationError, ERROR_CODES } from '../utils/errors.js';
|
|
25
25
|
|
|
26
26
|
const searchParamsSchema = z.object({
|
|
27
27
|
searchType: z.enum(['basic', 'advanced']).default('basic'),
|
|
28
28
|
query: z.string(),
|
|
29
|
-
filters: z.record(z.string(),
|
|
29
|
+
filters: z.record(z.string(), filterValueSchema).default({}),
|
|
30
30
|
boundingBox: boundingBoxSchema.optional(),
|
|
31
31
|
geohashPrecision: z.number().int().min(1).max(12).optional(),
|
|
32
32
|
limit: z.number().int().min(1).max(1000).default(100),
|
|
33
33
|
offset: z.number().int().min(0).default(0),
|
|
34
|
-
sort: z.
|
|
34
|
+
sort: z.string().default('relevance'),
|
|
35
35
|
order: z.enum(['asc', 'desc']).default('asc'),
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
type SearchParams = z.infer<typeof searchParamsSchema>;
|
|
39
|
+
|
|
38
40
|
type SearchRouteOptions = {
|
|
39
41
|
prisma: PrismaClient;
|
|
40
|
-
|
|
42
|
+
search: SearchBackend;
|
|
41
43
|
access: AccessFunction;
|
|
42
44
|
entityTransformers?: EntityTransformer[];
|
|
43
|
-
queryBuilder: OpensearchQueryBuilder;
|
|
44
45
|
entityVisibilityFilter: EntityVisibilityFilter;
|
|
45
46
|
searchVisibilityFilter: SearchVisibilityFilter;
|
|
46
47
|
};
|
|
47
48
|
|
|
49
|
+
// Highlights quote the Record's text, so a hit denied either part keeps only its score
|
|
50
|
+
const visibleExtra = (searchExtra: SearchExtra | undefined, { metadata, content }: AccessInfo) => {
|
|
51
|
+
if (!searchExtra || (metadata && content)) {
|
|
52
|
+
return searchExtra;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const { highlight: _, ...rest } = searchExtra;
|
|
56
|
+
|
|
57
|
+
return Object.keys(rest).length > 0 ? rest : undefined;
|
|
58
|
+
};
|
|
59
|
+
|
|
48
60
|
const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
|
|
49
61
|
const {
|
|
50
62
|
prisma,
|
|
51
|
-
|
|
63
|
+
search: backend,
|
|
52
64
|
access,
|
|
53
65
|
entityTransformers = [],
|
|
54
|
-
queryBuilder,
|
|
55
66
|
entityVisibilityFilter,
|
|
56
67
|
searchVisibilityFilter,
|
|
57
68
|
} = opts;
|
|
69
|
+
const { capabilities } = backend;
|
|
70
|
+
const noFacets = Object.fromEntries(Object.keys(capabilities.facets).map((name) => [name, []]));
|
|
71
|
+
|
|
72
|
+
const violations = ({ searchType, sort }: SearchParams) => {
|
|
73
|
+
const found: Violation[] = [];
|
|
74
|
+
if (!Object.hasOwn(capabilities.searchTypes, searchType)) {
|
|
75
|
+
found.push({ field: '/searchType', message: `${searchType} search isn't supported`, value: searchType });
|
|
76
|
+
}
|
|
77
|
+
if (sort !== 'relevance' && !Object.hasOwn(capabilities.sorts, sort)) {
|
|
78
|
+
found.push({ field: '/sort', message: `${sort} isn't a declared sort`, value: sort });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return found;
|
|
82
|
+
};
|
|
83
|
+
|
|
58
84
|
fastify.withTypeProvider<ZodTypeProvider>().post(
|
|
59
85
|
'/search',
|
|
60
86
|
{
|
|
@@ -63,60 +89,62 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
63
89
|
},
|
|
64
90
|
},
|
|
65
91
|
async (request, reply) => {
|
|
66
|
-
const {
|
|
92
|
+
const { filters, violations: filterProblems } = splitFilters(capabilities.filters, request.body.filters);
|
|
93
|
+
const problems = [
|
|
94
|
+
...violations(request.body),
|
|
95
|
+
...filterProblems,
|
|
96
|
+
...geoViolations(capabilities.geo, request.body),
|
|
97
|
+
];
|
|
98
|
+
if (problems.length > 0) {
|
|
99
|
+
return reply.code(400).send(createValidationError('The request parameters are invalid', problems));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const { searchType, query, filters: _, boundingBox, geohashPrecision, ...paging } = request.body;
|
|
103
|
+
|
|
104
|
+
const searchQuery = normaliseQuery(searchType, query, capabilities.searchTypes.advanced?.fields ?? {});
|
|
105
|
+
if ('problem' in searchQuery) {
|
|
106
|
+
const { problem } = searchQuery;
|
|
107
|
+
|
|
108
|
+
return reply.code(422).send(createValidationError(problem, [{ field: 'query', message: problem }]));
|
|
109
|
+
}
|
|
67
110
|
|
|
68
111
|
const context = { request, fastify };
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
|
|
84
|
-
highlight: {
|
|
85
|
-
fields: {
|
|
86
|
-
name: {},
|
|
87
|
-
description: {},
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
sort: queryBuilder.buildSort(sort, order),
|
|
91
|
-
from: offset,
|
|
92
|
-
size: limit,
|
|
93
|
-
},
|
|
112
|
+
// Only the database query needs it, so the search needn't wait; awaited before any reply so its error wins
|
|
113
|
+
const visibilityWhere = Promise.resolve(entityVisibilityFilter(context));
|
|
114
|
+
visibilityWhere.catch(() => undefined);
|
|
115
|
+
const visibility = await searchVisibilityFilter(context);
|
|
116
|
+
|
|
117
|
+
const searchRequest: SearchRequest = {
|
|
118
|
+
...searchQuery,
|
|
119
|
+
filters,
|
|
120
|
+
...(boundingBox && { boundingBox }),
|
|
121
|
+
...(geohashPrecision !== undefined && { geohashPrecision }),
|
|
122
|
+
...paging,
|
|
123
|
+
visibility,
|
|
94
124
|
};
|
|
95
|
-
|
|
96
|
-
|
|
125
|
+
|
|
126
|
+
const start = performance.now();
|
|
127
|
+
let result: SearchResult;
|
|
97
128
|
try {
|
|
98
|
-
|
|
129
|
+
result = await backend.search(searchRequest, context);
|
|
99
130
|
} catch (error) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
131
|
+
await visibilityWhere.catch((visibilityError: unknown) => {
|
|
132
|
+
fastify.log.error({ err: error }, 'Search backend failed');
|
|
133
|
+
throw visibilityError;
|
|
134
|
+
});
|
|
135
|
+
if (!(error instanceof SearchQueryError)) {
|
|
103
136
|
throw error;
|
|
104
137
|
}
|
|
105
138
|
|
|
106
|
-
fastify.log.warn(`Invalid search query: ${message}`);
|
|
107
|
-
|
|
108
|
-
return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, message));
|
|
109
|
-
}
|
|
139
|
+
fastify.log.warn(`Invalid search query: ${error.message}`);
|
|
110
140
|
|
|
111
|
-
|
|
112
|
-
throw new Error('Invalid search response: missing hits data');
|
|
141
|
+
return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, error.message));
|
|
113
142
|
}
|
|
114
|
-
|
|
115
|
-
const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
|
|
143
|
+
const searchTime = Math.round(performance.now() - start);
|
|
116
144
|
|
|
117
145
|
const dbEntities = await prisma.entity.findMany({
|
|
118
146
|
where: {
|
|
119
|
-
AND: [{ id: { in:
|
|
147
|
+
AND: [{ id: { in: result.hits.map(({ id }) => id) } }, await visibilityWhere],
|
|
120
148
|
},
|
|
121
149
|
include: { file: { select: { id: true } } },
|
|
122
150
|
});
|
|
@@ -125,70 +153,28 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
125
153
|
const entityMap = new Map(authorisedEntities.map((entity) => [entity.id, entity]));
|
|
126
154
|
|
|
127
155
|
const entities = await Promise.all(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
throw new Error('Missing id in search hit');
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const authorisedEntity = entityMap.get(hit._source.id);
|
|
156
|
+
result.hits.map(async ({ id, searchExtra }) => {
|
|
157
|
+
const authorisedEntity = entityMap.get(id);
|
|
134
158
|
if (!authorisedEntity) {
|
|
135
|
-
fastify.log.warn(`Entity ${
|
|
159
|
+
fastify.log.warn(`Entity ${id} found by the search backend but missing or not visible in database`);
|
|
136
160
|
|
|
137
161
|
return null;
|
|
138
162
|
}
|
|
139
163
|
|
|
140
|
-
const
|
|
164
|
+
const transformed = await applyTransformers(authorisedEntity, entityTransformers, context);
|
|
165
|
+
const extra = visibleExtra(searchExtra, authorisedEntity.access);
|
|
141
166
|
|
|
142
|
-
|
|
143
|
-
return {
|
|
144
|
-
...(result as Record<string, unknown>),
|
|
145
|
-
searchExtra: {
|
|
146
|
-
score: hit._score,
|
|
147
|
-
highlight: hit.highlight,
|
|
148
|
-
},
|
|
149
|
-
};
|
|
167
|
+
return extra === undefined ? transformed : { ...(transformed as object), searchExtra: extra };
|
|
150
168
|
}),
|
|
151
|
-
).then((results) => results.filter(
|
|
152
|
-
|
|
153
|
-
const facets: Record<string, Array<{ name: string; count: number }>> = {};
|
|
154
|
-
if (response.body.aggregations) {
|
|
155
|
-
Object.keys(response.body.aggregations).forEach((key) => {
|
|
156
|
-
if (key !== 'geohash_grid') {
|
|
157
|
-
const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
|
|
158
|
-
if (agg?.buckets && Array.isArray(agg.buckets)) {
|
|
159
|
-
facets[key] = agg.buckets.map((bucket) => ({
|
|
160
|
-
name: bucket.key,
|
|
161
|
-
count: bucket.doc_count,
|
|
162
|
-
}));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}
|
|
169
|
+
).then((results) => results.filter((entity) => entity !== null));
|
|
167
170
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
|
|
172
|
-
geohashAgg.buckets.forEach((bucket) => {
|
|
173
|
-
geohashGrid ||= {};
|
|
174
|
-
geohashGrid[bucket.key] = bucket.doc_count;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/* v8 ignore next 2 -- Not sure how to force opensearch to hit the other path -- @preserve */
|
|
180
|
-
const total =
|
|
181
|
-
typeof response.body.hits.total === 'number' ? response.body.hits.total : response.body.hits.total?.value || 0;
|
|
182
|
-
|
|
183
|
-
const result = {
|
|
184
|
-
total,
|
|
185
|
-
searchTime: response.body.took,
|
|
171
|
+
return {
|
|
172
|
+
total: result.total,
|
|
173
|
+
searchTime,
|
|
186
174
|
entities,
|
|
187
|
-
facets:
|
|
188
|
-
geohashGrid,
|
|
175
|
+
facets: { ...noFacets, ...result.facets },
|
|
176
|
+
...(result.geohashGrid && { geohashGrid: result.geohashGrid }),
|
|
189
177
|
};
|
|
190
|
-
|
|
191
|
-
return result;
|
|
192
178
|
},
|
|
193
179
|
);
|
|
194
180
|
};
|