arocapi 5.0.0-beta.1 → 5.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +37 -802
- package/dist/app.d.ts +38 -20
- package/dist/app.js +62 -40
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +37 -57
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +13 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +37 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +11 -5
- package/dist/routes/metadata.js +24 -68
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +90 -119
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +82 -40
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +168 -95
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +48 -67
- package/src/routes/entity.ts +21 -35
- package/src/routes/file.ts +53 -103
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +42 -98
- package/src/routes/search.ts +131 -151
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -82
- package/dist/app.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -273
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -130
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -315
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -222
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -780
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -187
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -396
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -163
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -112
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/crate.test.ts +0 -423
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -209
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
package/src/routes/metadata.ts
CHANGED
|
@@ -1,123 +1,67 @@
|
|
|
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';
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
5
|
+
import type { MetadataSource } from '../storage/backend.js';
|
|
6
|
+
import { sendDelivery } from '../storage/deliver.js';
|
|
7
|
+
import type { AccessFunction } from '../types/access.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
9
|
+
import { createAccessGuard } from '../utils/access.js';
|
|
10
|
+
import { createNotFoundError } from '../utils/errors.js';
|
|
11
|
+
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
9
12
|
|
|
10
13
|
const paramsSchema = z.object({
|
|
11
14
|
id: z.url(),
|
|
12
15
|
});
|
|
13
16
|
|
|
14
|
-
type
|
|
17
|
+
type MetadataRouteOptions = {
|
|
15
18
|
prisma: PrismaClient;
|
|
16
|
-
|
|
19
|
+
metadata: MetadataSource;
|
|
20
|
+
access: AccessFunction;
|
|
21
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
22
|
+
refusal: RefusalOptions;
|
|
17
23
|
};
|
|
18
24
|
|
|
19
|
-
const
|
|
20
|
-
const { prisma,
|
|
25
|
+
const metadataRoute: FastifyPluginAsync<MetadataRouteOptions> = async (fastify, opts) => {
|
|
26
|
+
const { prisma, metadata, access, entityVisibilityFilter, refusal } = opts;
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{
|
|
25
|
-
schema: {
|
|
26
|
-
params: paramsSchema,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
async (request, reply) => {
|
|
30
|
-
const { id } = request.params;
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
const entity = await prisma.entity.findUnique({
|
|
34
|
-
where: { id },
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
if (!entity) {
|
|
38
|
-
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const metadata: FileMetadata | false = await roCrateHandler.head(entity, { request, fastify });
|
|
42
|
-
|
|
43
|
-
if (!metadata) {
|
|
44
|
-
return reply.code(404).send(createNotFoundError('The requested RO-Crate metadata was not found', id));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
setFileHeaders(reply, metadata);
|
|
48
|
-
|
|
49
|
-
return reply.code(200).send();
|
|
50
|
-
} catch (error) {
|
|
51
|
-
const err = error as Error;
|
|
52
|
-
fastify.log.error(`RO-Crate metadata retrieval error: ${err}`);
|
|
53
|
-
|
|
54
|
-
return reply.code(500).send(createInternalError());
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
);
|
|
28
|
+
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
29
|
+
const denyMetadata = createAccessGuard({ access, isAuthenticated: refusal.isAuthenticated, level: 'metadata' });
|
|
58
30
|
|
|
59
|
-
fastify.withTypeProvider<ZodTypeProvider>().
|
|
60
|
-
'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
},
|
|
31
|
+
fastify.withTypeProvider<ZodTypeProvider>().route({
|
|
32
|
+
method: ['GET', 'HEAD'],
|
|
33
|
+
url: '/entity/:id/metadata',
|
|
34
|
+
schema: {
|
|
35
|
+
params: paramsSchema,
|
|
65
36
|
},
|
|
66
|
-
async (request, reply) => {
|
|
37
|
+
handler: async (request, reply) => {
|
|
67
38
|
const { id } = request.params;
|
|
68
39
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
where: { id },
|
|
72
|
-
});
|
|
40
|
+
const context = { request, fastify };
|
|
41
|
+
const entity = await prisma.entity.findFirst({ where: { AND: [{ id }, await entityVisibilityFilter(context)] } });
|
|
73
42
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const result = await roCrateHandler.get(entity, { request, fastify });
|
|
79
|
-
|
|
80
|
-
if (!result) {
|
|
81
|
-
return reply.code(404).send(createNotFoundError('The requested RO-Crate could not be retrieved', id));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (result.type === 'redirect') {
|
|
85
|
-
return reply.code(302).redirect(result.url);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Set content type to application/ld+json for RO-Crate
|
|
89
|
-
const metadata = {
|
|
90
|
-
...result.metadata,
|
|
91
|
-
contentType: 'application/ld+json',
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
setFileHeaders(reply, metadata);
|
|
95
|
-
|
|
96
|
-
// Handle stream response
|
|
97
|
-
if (result.type === 'stream') {
|
|
98
|
-
return reply.code(200).send(result.stream);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (result.type === 'file') {
|
|
102
|
-
if (result.accelPath) {
|
|
103
|
-
reply.header('X-Accel-Redirect', result.accelPath);
|
|
104
|
-
return reply.code(200).send();
|
|
105
|
-
}
|
|
43
|
+
if (!entity) {
|
|
44
|
+
return sendEntityNotVisible(reply, id, context);
|
|
45
|
+
}
|
|
106
46
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
47
|
+
if (await denyMetadata(reply, entity, context)) {
|
|
48
|
+
return reply;
|
|
49
|
+
}
|
|
110
50
|
|
|
111
|
-
|
|
112
|
-
throw new Error(`Unexpected RO-Crate result type: ${(result as { type: string }).type}`);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
const err = error as Error;
|
|
115
|
-
fastify.log.error(`RO-Crate retrieval error: ${err.message}`);
|
|
51
|
+
const delivery = await metadata(entity, context);
|
|
116
52
|
|
|
117
|
-
|
|
53
|
+
if (!delivery) {
|
|
54
|
+
return reply.code(404).send(createNotFoundError('The requested Metadata was not found', id));
|
|
118
55
|
}
|
|
56
|
+
|
|
57
|
+
return sendDelivery(
|
|
58
|
+
request,
|
|
59
|
+
reply,
|
|
60
|
+
delivery.type === 'redirect' ? delivery : { ...delivery, contentType: 'application/ld+json' },
|
|
61
|
+
{ json: true },
|
|
62
|
+
);
|
|
119
63
|
},
|
|
120
|
-
);
|
|
64
|
+
});
|
|
121
65
|
};
|
|
122
66
|
|
|
123
|
-
export default
|
|
67
|
+
export default metadataRoute;
|
package/src/routes/search.ts
CHANGED
|
@@ -1,57 +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 } 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';
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
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';
|
|
20
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
21
|
+
import type { AccessFunction, AccessInfo } from '../types/access.js';
|
|
22
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
23
|
+
import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
|
|
24
|
+
import { createError, createValidationError, ERROR_CODES } from '../utils/errors.js';
|
|
23
25
|
|
|
24
26
|
const searchParamsSchema = z.object({
|
|
25
27
|
searchType: z.enum(['basic', 'advanced']).default('basic'),
|
|
26
28
|
query: z.string(),
|
|
27
|
-
filters: z.record(z.string(),
|
|
29
|
+
filters: z.record(z.string(), filterValueSchema).default({}),
|
|
28
30
|
boundingBox: boundingBoxSchema.optional(),
|
|
29
31
|
geohashPrecision: z.number().int().min(1).max(12).optional(),
|
|
30
32
|
limit: z.number().int().min(1).max(1000).default(100),
|
|
31
33
|
offset: z.number().int().min(0).default(0),
|
|
32
|
-
sort: z.
|
|
34
|
+
sort: z.string().default('relevance'),
|
|
33
35
|
order: z.enum(['asc', 'desc']).default('asc'),
|
|
34
36
|
});
|
|
35
37
|
|
|
38
|
+
type SearchParams = z.infer<typeof searchParamsSchema>;
|
|
39
|
+
|
|
36
40
|
type SearchRouteOptions = {
|
|
37
41
|
prisma: PrismaClient;
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
search: SearchBackend;
|
|
43
|
+
access: AccessFunction;
|
|
40
44
|
entityTransformers?: EntityTransformer[];
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
46
|
+
searchVisibilityFilter: SearchVisibilityFilter;
|
|
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;
|
|
43
58
|
};
|
|
44
59
|
|
|
45
60
|
const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
|
|
46
61
|
const {
|
|
47
62
|
prisma,
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
search: backend,
|
|
64
|
+
access,
|
|
50
65
|
entityTransformers = [],
|
|
51
|
-
|
|
52
|
-
|
|
66
|
+
entityVisibilityFilter,
|
|
67
|
+
searchVisibilityFilter,
|
|
53
68
|
} = opts;
|
|
54
|
-
const
|
|
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
|
+
|
|
55
84
|
fastify.withTypeProvider<ZodTypeProvider>().post(
|
|
56
85
|
'/search',
|
|
57
86
|
{
|
|
@@ -60,140 +89,91 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
60
89
|
},
|
|
61
90
|
},
|
|
62
91
|
async (request, reply) => {
|
|
63
|
-
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
|
+
}
|
|
64
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
|
+
}
|
|
110
|
+
|
|
111
|
+
const context = { request, fastify };
|
|
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,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const start = performance.now();
|
|
127
|
+
let result: SearchResult;
|
|
65
128
|
try {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
description: {},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
sort: queryBuilder.buildSort(sort, order),
|
|
78
|
-
from: offset,
|
|
79
|
-
size: limit,
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
fastify.log.debug(opensearchQuery);
|
|
83
|
-
const response = await opensearch.search(opensearchQuery);
|
|
84
|
-
|
|
85
|
-
if (!response.body?.hits?.hits) {
|
|
86
|
-
throw new Error('Invalid search response: missing hits data');
|
|
129
|
+
result = await backend.search(searchRequest, context);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
await visibilityWhere.catch((visibilityError: unknown) => {
|
|
132
|
+
fastify.log.error({ err: error }, 'Search backend failed');
|
|
133
|
+
throw visibilityError;
|
|
134
|
+
});
|
|
135
|
+
if (!(error instanceof SearchQueryError)) {
|
|
136
|
+
throw error;
|
|
87
137
|
}
|
|
88
138
|
|
|
89
|
-
|
|
139
|
+
fastify.log.warn(`Invalid search query: ${error.message}`);
|
|
90
140
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
in: entityIds,
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
include: { file: { select: { id: true } } },
|
|
98
|
-
});
|
|
141
|
+
return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, error.message));
|
|
142
|
+
}
|
|
143
|
+
const searchTime = Math.round(performance.now() - start);
|
|
99
144
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const entities = await Promise.all(
|
|
106
|
-
response.body.hits.hits.map(async (hit) => {
|
|
107
|
-
if (!hit._source?.id) {
|
|
108
|
-
throw new Error('Missing id in search hit');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const dbEntity = entityMap.get(hit._source.id);
|
|
112
|
-
if (!dbEntity) {
|
|
113
|
-
fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but not in database`);
|
|
114
|
-
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const base = baseEntityTransformer(dbEntity);
|
|
119
|
-
const standardEntity = {
|
|
120
|
-
...base,
|
|
121
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
122
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
123
|
-
};
|
|
124
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
125
|
-
|
|
126
|
-
let result = authorisedEntity;
|
|
127
|
-
for (const transformer of entityTransformers) {
|
|
128
|
-
result = await transformer(result, { request, fastify });
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Add search-specific metadata
|
|
132
|
-
return {
|
|
133
|
-
...(result as Record<string, unknown>),
|
|
134
|
-
searchExtra: {
|
|
135
|
-
score: hit._score,
|
|
136
|
-
highlight: hit.highlight,
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
}),
|
|
140
|
-
).then((results) => results.filter(Boolean));
|
|
141
|
-
|
|
142
|
-
const facets: Record<string, Array<{ name: string; count: number }>> = {};
|
|
143
|
-
if (response.body.aggregations) {
|
|
144
|
-
Object.keys(response.body.aggregations).forEach((key) => {
|
|
145
|
-
if (key !== 'geohash_grid') {
|
|
146
|
-
const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
|
|
147
|
-
if (agg?.buckets && Array.isArray(agg.buckets)) {
|
|
148
|
-
facets[key] = agg.buckets.map((bucket) => ({
|
|
149
|
-
name: bucket.key,
|
|
150
|
-
count: bucket.doc_count,
|
|
151
|
-
}));
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
145
|
+
const dbEntities = await prisma.entity.findMany({
|
|
146
|
+
where: {
|
|
147
|
+
AND: [{ id: { in: result.hits.map(({ id }) => id) } }, await visibilityWhere],
|
|
148
|
+
},
|
|
149
|
+
});
|
|
156
150
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
|
|
160
|
-
if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
|
|
161
|
-
geohashAgg.buckets.forEach((bucket) => {
|
|
162
|
-
geohashGrid ||= {};
|
|
163
|
-
geohashGrid[bucket.key] = bucket.doc_count;
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
151
|
+
const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
|
|
152
|
+
const entityMap = new Map(authorisedEntities.map((entity, i) => [entity.id, { entity, row: dbEntities[i] }]));
|
|
167
153
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const result = {
|
|
175
|
-
total,
|
|
176
|
-
searchTime: response.body.took,
|
|
177
|
-
entities,
|
|
178
|
-
facets: Object.keys(facets).length > 0 ? facets : undefined,
|
|
179
|
-
geohashGrid,
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
return result;
|
|
183
|
-
} catch (error) {
|
|
184
|
-
const err = error as Error;
|
|
154
|
+
const entities = await Promise.all(
|
|
155
|
+
result.hits.map(async ({ id, searchExtra }) => {
|
|
156
|
+
const found = entityMap.get(id);
|
|
157
|
+
if (!found) {
|
|
158
|
+
fastify.log.warn(`Entity ${id} found by the search backend but missing or not visible in database`);
|
|
185
159
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
fastify.log.warn(`Invalid search query: ${err.message}`);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
189
162
|
|
|
190
|
-
|
|
191
|
-
|
|
163
|
+
const transformed = await applyTransformers(found.entity, entityTransformers, context, found.row);
|
|
164
|
+
const extra = visibleExtra(searchExtra, found.entity.access);
|
|
192
165
|
|
|
193
|
-
|
|
166
|
+
return extra === undefined ? transformed : { ...(transformed as object), searchExtra: extra };
|
|
167
|
+
}),
|
|
168
|
+
).then((results) => results.filter((entity) => entity !== null));
|
|
194
169
|
|
|
195
|
-
|
|
196
|
-
|
|
170
|
+
return {
|
|
171
|
+
total: result.total,
|
|
172
|
+
searchTime,
|
|
173
|
+
entities,
|
|
174
|
+
facets: { ...noFacets, ...result.facets },
|
|
175
|
+
...(result.geohashGrid && { geohashGrid: result.geohashGrid }),
|
|
176
|
+
};
|
|
197
177
|
},
|
|
198
178
|
);
|
|
199
179
|
};
|