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.
Files changed (205) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/dist/app.d.ts +22 -17
  4. package/dist/app.js +22 -16
  5. package/dist/app.js.map +1 -1
  6. package/dist/routes/capabilities.d.ts +2 -2
  7. package/dist/routes/capabilities.js +2 -2
  8. package/dist/routes/capabilities.js.map +1 -1
  9. package/dist/routes/entities.js +8 -9
  10. package/dist/routes/entities.js.map +1 -1
  11. package/dist/routes/file.d.ts +2 -2
  12. package/dist/routes/file.js +25 -72
  13. package/dist/routes/file.js.map +1 -1
  14. package/dist/routes/metadata.d.ts +5 -5
  15. package/dist/routes/metadata.js +22 -61
  16. package/dist/routes/metadata.js.map +1 -1
  17. package/dist/routes/search.d.ts +2 -4
  18. package/dist/routes/search.js +75 -97
  19. package/dist/routes/search.js.map +1 -1
  20. package/dist/search/advanced.d.ts +8 -0
  21. package/dist/search/advanced.js +198 -0
  22. package/dist/search/advanced.js.map +1 -0
  23. package/dist/search/backend.d.ts +95 -0
  24. package/dist/search/backend.js +5 -0
  25. package/dist/search/backend.js.map +1 -0
  26. package/dist/search/fields.d.ts +63 -0
  27. package/dist/search/fields.js +203 -0
  28. package/dist/search/fields.js.map +1 -0
  29. package/dist/search/opensearch/advanced.d.ts +4 -0
  30. package/dist/search/opensearch/advanced.js +28 -0
  31. package/dist/search/opensearch/advanced.js.map +1 -0
  32. package/dist/search/opensearch/opensearch.d.ts +16 -0
  33. package/dist/search/opensearch/opensearch.js +187 -0
  34. package/dist/search/opensearch/opensearch.js.map +1 -0
  35. package/dist/search/opensearch/visibility.d.ts +3 -0
  36. package/dist/search/opensearch/visibility.js +27 -0
  37. package/dist/search/opensearch/visibility.js.map +1 -0
  38. package/dist/search/postgres/advanced.d.ts +7 -0
  39. package/dist/search/postgres/advanced.js +54 -0
  40. package/dist/search/postgres/advanced.js.map +1 -0
  41. package/dist/search/postgres/facets.d.ts +2 -0
  42. package/dist/search/postgres/facets.js +27 -0
  43. package/dist/search/postgres/facets.js.map +1 -0
  44. package/dist/search/postgres/geo.d.ts +6 -0
  45. package/dist/search/postgres/geo.js +42 -0
  46. package/dist/search/postgres/geo.js.map +1 -0
  47. package/dist/search/postgres/postgres.d.ts +19 -0
  48. package/dist/search/postgres/postgres.js +157 -0
  49. package/dist/search/postgres/postgres.js.map +1 -0
  50. package/dist/search/postgres/values.d.ts +8 -0
  51. package/dist/search/postgres/values.js +20 -0
  52. package/dist/search/postgres/values.js.map +1 -0
  53. package/dist/search/postgres/visibility.d.ts +4 -0
  54. package/dist/search/postgres/visibility.js +38 -0
  55. package/dist/search/postgres/visibility.js.map +1 -0
  56. package/dist/search/requestFilters.d.ts +39 -0
  57. package/dist/search/requestFilters.js +125 -0
  58. package/dist/search/requestFilters.js.map +1 -0
  59. package/dist/search/visibility.d.ts +51 -0
  60. package/dist/search/visibility.js +100 -0
  61. package/dist/search/visibility.js.map +1 -0
  62. package/dist/storage/backend.d.ts +58 -0
  63. package/dist/storage/backend.js +2 -0
  64. package/dist/storage/backend.js.map +1 -0
  65. package/dist/storage/deliver.d.ts +12 -0
  66. package/dist/storage/deliver.js +34 -0
  67. package/dist/storage/deliver.js.map +1 -0
  68. package/dist/storage/directory/directory.d.ts +6 -0
  69. package/dist/storage/directory/directory.js +24 -0
  70. package/dist/storage/directory/directory.js.map +1 -0
  71. package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
  72. package/dist/storage/entity-meta/entity-meta.js +14 -0
  73. package/dist/storage/entity-meta/entity-meta.js.map +1 -0
  74. package/dist/storage/location.d.ts +20 -0
  75. package/dist/storage/location.js +40 -0
  76. package/dist/storage/location.js.map +1 -0
  77. package/dist/storage/ocfl/ocfl.d.ts +7 -0
  78. package/dist/storage/ocfl/ocfl.js +79 -0
  79. package/dist/storage/ocfl/ocfl.js.map +1 -0
  80. package/dist/storage/ocfl/ocflFs.d.ts +7 -0
  81. package/dist/storage/ocfl/ocflFs.js +33 -0
  82. package/dist/storage/ocfl/ocflFs.js.map +1 -0
  83. package/dist/storage/results.d.ts +7 -0
  84. package/dist/storage/results.js +22 -0
  85. package/dist/storage/results.js.map +1 -0
  86. package/dist/storage/s3/s3.d.ts +10 -0
  87. package/dist/storage/s3/s3.js +47 -0
  88. package/dist/storage/s3/s3.js.map +1 -0
  89. package/dist/types/visibility.d.ts +3 -2
  90. package/dist/utils/visibility.js +1 -1
  91. package/dist/utils/visibility.js.map +1 -1
  92. package/package.json +66 -26
  93. package/prisma/models/entity.prisma +9 -8
  94. package/prisma/models/entitySearch.prisma +21 -0
  95. package/prisma/models/file.prisma +3 -3
  96. package/src/app.ts +85 -48
  97. package/src/routes/capabilities.ts +4 -4
  98. package/src/routes/entities.ts +10 -9
  99. package/src/routes/file.ts +23 -99
  100. package/src/routes/metadata.ts +24 -86
  101. package/src/routes/search.ts +104 -118
  102. package/src/search/advanced.ts +258 -0
  103. package/src/search/backend.ts +91 -0
  104. package/src/search/fields.ts +320 -0
  105. package/src/search/opensearch/advanced.ts +36 -0
  106. package/src/search/opensearch/opensearch.ts +290 -0
  107. package/src/search/opensearch/visibility.ts +38 -0
  108. package/src/search/postgres/advanced.ts +76 -0
  109. package/src/search/postgres/facets.ts +34 -0
  110. package/src/search/postgres/geo.ts +58 -0
  111. package/src/search/postgres/postgres.ts +232 -0
  112. package/src/search/postgres/values.ts +33 -0
  113. package/src/search/postgres/visibility.ts +54 -0
  114. package/src/search/requestFilters.ts +178 -0
  115. package/src/search/visibility.ts +193 -0
  116. package/src/storage/backend.ts +75 -0
  117. package/src/storage/deliver.ts +65 -0
  118. package/src/storage/directory/directory.ts +39 -0
  119. package/src/storage/entity-meta/entity-meta.ts +25 -0
  120. package/src/storage/location.ts +85 -0
  121. package/src/storage/ocfl/ocfl.ts +129 -0
  122. package/src/storage/ocfl/ocflFs.ts +67 -0
  123. package/src/storage/results.ts +32 -0
  124. package/src/storage/s3/s3.ts +79 -0
  125. package/src/types/visibility.ts +6 -3
  126. package/src/utils/visibility.ts +1 -1
  127. package/dist/app.test.d.ts +0 -1
  128. package/dist/app.test.js +0 -67
  129. package/dist/app.test.js.map +0 -1
  130. package/dist/errorHandling.test.d.ts +0 -1
  131. package/dist/errorHandling.test.js +0 -222
  132. package/dist/errorHandling.test.js.map +0 -1
  133. package/dist/index.dev.d.ts +0 -1
  134. package/dist/index.dev.js +0 -157
  135. package/dist/index.dev.js.map +0 -1
  136. package/dist/routes/capabilities.test.d.ts +0 -1
  137. package/dist/routes/capabilities.test.js +0 -81
  138. package/dist/routes/capabilities.test.js.map +0 -1
  139. package/dist/routes/crate.test.d.ts +0 -1
  140. package/dist/routes/crate.test.js +0 -390
  141. package/dist/routes/crate.test.js.map +0 -1
  142. package/dist/routes/entities.test.d.ts +0 -1
  143. package/dist/routes/entities.test.js +0 -277
  144. package/dist/routes/entities.test.js.map +0 -1
  145. package/dist/routes/entity.test.d.ts +0 -1
  146. package/dist/routes/entity.test.js +0 -136
  147. package/dist/routes/entity.test.js.map +0 -1
  148. package/dist/routes/file.test.d.ts +0 -1
  149. package/dist/routes/file.test.js +0 -435
  150. package/dist/routes/file.test.js.map +0 -1
  151. package/dist/routes/files.test.d.ts +0 -1
  152. package/dist/routes/files.test.js +0 -245
  153. package/dist/routes/files.test.js.map +0 -1
  154. package/dist/routes/search.test.d.ts +0 -1
  155. package/dist/routes/search.test.js +0 -833
  156. package/dist/routes/search.test.js.map +0 -1
  157. package/dist/test/helpers/fastify.d.ts +0 -9
  158. package/dist/test/helpers/fastify.js +0 -17
  159. package/dist/test/helpers/fastify.js.map +0 -1
  160. package/dist/test/integration.setup.d.ts +0 -47
  161. package/dist/test/integration.setup.js +0 -195
  162. package/dist/test/integration.setup.js.map +0 -1
  163. package/dist/test/integration.test.d.ts +0 -1
  164. package/dist/test/integration.test.js +0 -762
  165. package/dist/test/integration.test.js.map +0 -1
  166. package/dist/transformers/default.test.d.ts +0 -1
  167. package/dist/transformers/default.test.js +0 -225
  168. package/dist/transformers/default.test.js.map +0 -1
  169. package/dist/transformers/transformer.test.d.ts +0 -1
  170. package/dist/transformers/transformer.test.js +0 -165
  171. package/dist/transformers/transformer.test.js.map +0 -1
  172. package/dist/types/fileHandlers.d.ts +0 -37
  173. package/dist/types/fileHandlers.js +0 -2
  174. package/dist/types/fileHandlers.js.map +0 -1
  175. package/dist/utils/errors.test.d.ts +0 -1
  176. package/dist/utils/errors.test.js +0 -79
  177. package/dist/utils/errors.test.js.map +0 -1
  178. package/dist/utils/queryBuilder.d.ts +0 -47
  179. package/dist/utils/queryBuilder.js +0 -107
  180. package/dist/utils/queryBuilder.js.map +0 -1
  181. package/prisma.config.ts +0 -9
  182. package/src/app.test.ts +0 -91
  183. package/src/errorHandling.test.ts +0 -288
  184. package/src/index.dev.ts +0 -202
  185. package/src/reset.d.ts +0 -1
  186. package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
  187. package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
  188. package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
  189. package/src/routes/__snapshots__/search.test.ts.snap +0 -165
  190. package/src/routes/capabilities.test.ts +0 -102
  191. package/src/routes/crate.test.ts +0 -484
  192. package/src/routes/entities.test.ts +0 -324
  193. package/src/routes/entity.test.ts +0 -166
  194. package/src/routes/file.test.ts +0 -538
  195. package/src/routes/files.test.ts +0 -304
  196. package/src/routes/search.test.ts +0 -959
  197. package/src/test/__snapshots__/integration.test.ts.snap +0 -18
  198. package/src/test/helpers/fastify.ts +0 -26
  199. package/src/test/integration.setup.ts +0 -220
  200. package/src/test/integration.test.ts +0 -953
  201. package/src/transformers/default.test.ts +0 -276
  202. package/src/transformers/transformer.test.ts +0 -226
  203. package/src/types/fileHandlers.ts +0 -57
  204. package/src/utils/errors.test.ts +0 -90
  205. package/src/utils/queryBuilder.ts +0 -157
@@ -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
- fileHandler: FileHandler;
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, fileHandler, access, entityVisibilityFilter, refusal } = opts;
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>().head(
65
- '/file/:id',
66
- {
67
- schema: {
68
- params: paramsSchema,
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 visible = await findVisibleFile(id, context);
58
+ const found = await prisma.file.findFirst({
59
+ where: { id, entity: await entityVisibilityFilter(context) },
60
+ include: { entity: true },
61
+ });
76
62
 
77
- if (!visible) {
63
+ if (!found) {
78
64
  return sendFileNotVisible(reply, id, context);
79
65
  }
80
66
 
81
- const { file, entity } = visible;
67
+ const { entity, ...file } = found;
82
68
  if (await denyContent(reply, entity, context)) {
83
69
  return reply;
84
70
  }
85
71
 
86
- const metadata: FileMetadata | false = await fileHandler.head(file, context);
87
-
88
- if (!metadata) {
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
- const { file, entity } = visible;
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
- if (result.type === 'redirect') {
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;
@@ -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 CrateRouteOptions = {
17
+ type MetadataRouteOptions = {
20
18
  prisma: PrismaClient;
21
- roCrateHandler: RoCrateHandler;
19
+ metadata: MetadataSource;
22
20
  access: AccessFunction;
23
21
  entityVisibilityFilter: EntityVisibilityFilter;
24
22
  refusal: RefusalOptions;
25
23
  };
26
24
 
27
- const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
28
- const { prisma, roCrateHandler, access, entityVisibilityFilter, refusal } = opts;
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>().head(
37
- '/entity/:id/metadata',
38
- {
39
- schema: {
40
- params: paramsSchema,
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 findVisibleEntity(id, context);
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 metadata: FileMetadata | false = await roCrateHandler.head(entity, context);
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
- const stream = createReadStream(result.path);
120
- return reply.code(200).send(stream);
53
+ if (!delivery) {
54
+ return reply.code(404).send(createNotFoundError('The requested Metadata was not found', id));
121
55
  }
122
56
 
123
- // Exhaustiveness check - should never reach here with proper types
124
- throw new Error(`Unexpected RO-Crate result type: ${(result as { type: string }).type}`);
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 crate;
67
+ export default metadataRoute;
@@ -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(), z.array(z.string())).optional(),
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.enum(['id', 'name', 'createdAt', 'updatedAt', 'relevance']).default('relevance'),
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
- opensearch: Client;
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
- opensearch,
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 { searchType, query, filters, boundingBox, geohashPrecision, limit, offset, sort, order } = request.body;
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
- const [visibilityWhere, visibilityQuery] = await Promise.all([
70
- entityVisibilityFilter(context),
71
- searchVisibilityFilter(context),
72
- ]);
73
-
74
- const opensearchQuery: Search_Request = {
75
- index: 'entities',
76
- body: {
77
- query: {
78
- bool: {
79
- must: [queryBuilder.buildQuery(searchType, query, filters, boundingBox)],
80
- filter: [visibilityQuery],
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
- fastify.log.debug(opensearchQuery);
96
- let response: Search_Response;
125
+
126
+ const start = performance.now();
127
+ let result: SearchResult;
97
128
  try {
98
- response = await opensearch.search(opensearchQuery);
129
+ result = await backend.search(searchRequest, context);
99
130
  } catch (error) {
100
- const { statusCode, message } = error as Error & { statusCode?: number };
101
- // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
102
- if (statusCode !== 400) {
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
- if (!response.body?.hits?.hits) {
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: entityIds } }, visibilityWhere],
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
- response.body.hits.hits.map(async (hit) => {
129
- if (!hit._source?.id) {
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 ${hit._source.id} found in OpenSearch but missing or not visible in database`);
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 result = await applyTransformers(authorisedEntity, entityTransformers, context);
164
+ const transformed = await applyTransformers(authorisedEntity, entityTransformers, context);
165
+ const extra = visibleExtra(searchExtra, authorisedEntity.access);
141
166
 
142
- // Add search-specific metadata
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(Boolean));
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
- let geohashGrid: Record<string, number> | undefined;
169
- if (response.body.aggregations?.geohash_grid) {
170
- const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
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: Object.keys(facets).length > 0 ? facets : undefined,
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
  };