@nulib/canopy 0.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/bin/canopy.js +16 -0
  2. package/lib/AGENTS.md +107 -0
  3. package/lib/base-path.js +67 -0
  4. package/lib/build/appearance.js +76 -0
  5. package/lib/build/assets.js +30 -0
  6. package/lib/build/build.js +350 -0
  7. package/lib/build/dev.js +1355 -0
  8. package/lib/build/devtoast.config.json +6 -0
  9. package/lib/build/devtoast.css +14 -0
  10. package/lib/build/facets.js +19 -0
  11. package/lib/build/fonts.js +105 -0
  12. package/lib/build/iiif.js +2990 -0
  13. package/lib/build/log.js +31 -0
  14. package/lib/build/mdx.js +1967 -0
  15. package/lib/build/package-names.js +111 -0
  16. package/lib/build/pages.js +420 -0
  17. package/lib/build/rehype-task-list-labels.js +54 -0
  18. package/lib/build/runtimes.js +141 -0
  19. package/lib/build/search-index.js +55 -0
  20. package/lib/build/search-text.js +160 -0
  21. package/lib/build/search-workflow.js +72 -0
  22. package/lib/build/search.js +302 -0
  23. package/lib/build/sitemap.js +197 -0
  24. package/lib/build/styles.js +140 -0
  25. package/lib/build/theme-tokens.js +78 -0
  26. package/lib/build/ui-styles.js +25 -0
  27. package/lib/build/upgrade-notices.js +121 -0
  28. package/lib/cli.js +64 -0
  29. package/lib/common.js +580 -0
  30. package/lib/components/bibliography.js +197 -0
  31. package/lib/components/featured.js +237 -0
  32. package/lib/components/hero-slider-runtime.js +215 -0
  33. package/lib/components/image-story-runtime-entry.js +249 -0
  34. package/lib/components/map-runtime.js +114 -0
  35. package/lib/components/metadata-index.js +32 -0
  36. package/lib/components/nav-place.js +227 -0
  37. package/lib/components/navigation.js +381 -0
  38. package/lib/components/referenced.js +217 -0
  39. package/lib/components/slider-runtime-entry.js +106 -0
  40. package/lib/components/timeline-runtime.js +80 -0
  41. package/lib/components/viewer-runtime-entry.js +88 -0
  42. package/lib/config-path.js +78 -0
  43. package/lib/default-locale.js +113 -0
  44. package/lib/discovery/browser-loader.js +107 -0
  45. package/lib/discovery/browser.js +115 -0
  46. package/lib/discovery/config.js +25 -0
  47. package/lib/discovery/envelope.js +124 -0
  48. package/lib/discovery/export.js +226 -0
  49. package/lib/discovery/ingestion.js +62 -0
  50. package/lib/discovery/pagination.js +66 -0
  51. package/lib/discovery/provenance.js +79 -0
  52. package/lib/discovery/search.js +81 -0
  53. package/lib/discovery/tools.js +155 -0
  54. package/lib/discovery/validation.js +121 -0
  55. package/lib/head.js +154 -0
  56. package/lib/iiif/thumbnail.js +402 -0
  57. package/lib/index.js +9 -0
  58. package/lib/locale-runtime.js +69 -0
  59. package/lib/locales.js +203 -0
  60. package/lib/orchestrator.js +231 -0
  61. package/lib/page-context.js +24 -0
  62. package/lib/search/bundle.js +25 -0
  63. package/lib/search/data.js +202 -0
  64. package/lib/search/engine.js +86 -0
  65. package/lib/search/index-cache.js +95 -0
  66. package/lib/search/search-app.jsx +835 -0
  67. package/lib/search/search-engine-runtime.js +44 -0
  68. package/lib/search/search-form-runtime.js +462 -0
  69. package/lib/search/search.js +676 -0
  70. package/package.json +109 -3
  71. package/types/base-path.d.ts +4 -0
  72. package/types/orchestrator.d.ts +17 -0
  73. package/ui/dist/index.mjs +7358 -0
  74. package/ui/dist/index.mjs.map +7 -0
  75. package/ui/dist/server.mjs +12731 -0
  76. package/ui/dist/server.mjs.map +7 -0
  77. package/ui/fonts/OFL.txt +98 -0
  78. package/ui/fonts/google-sans-flex-latin-ext.woff2 +0 -0
  79. package/ui/fonts/google-sans-flex-latin.woff2 +0 -0
  80. package/ui/fonts/source-serif-4-italic-latin-ext.woff2 +0 -0
  81. package/ui/fonts/source-serif-4-italic-latin.woff2 +0 -0
  82. package/ui/fonts/source-serif-4-latin-ext.woff2 +0 -0
  83. package/ui/fonts/source-serif-4-latin.woff2 +0 -0
  84. package/ui/styles/base/_common.scss +97 -0
  85. package/ui/styles/base/_heading.scss +61 -0
  86. package/ui/styles/base/_markdown.scss +188 -0
  87. package/ui/styles/base/_type.scss +51 -0
  88. package/ui/styles/base/_utilities.scss +7 -0
  89. package/ui/styles/base/index.scss +5 -0
  90. package/ui/styles/components/_article-card.scss +108 -0
  91. package/ui/styles/components/_band.scss +104 -0
  92. package/ui/styles/components/_bibliography.scss +88 -0
  93. package/ui/styles/components/_buttons.scss +143 -0
  94. package/ui/styles/components/_card.scss +109 -0
  95. package/ui/styles/components/_diagram.scss +76 -0
  96. package/ui/styles/components/_feature.scss +66 -0
  97. package/ui/styles/components/_footer.scss +136 -0
  98. package/ui/styles/components/_gallery.scss +476 -0
  99. package/ui/styles/components/_image-story.scss +56 -0
  100. package/ui/styles/components/_interstitial-hero.scss +625 -0
  101. package/ui/styles/components/_layout.scss +83 -0
  102. package/ui/styles/components/_map.scss +296 -0
  103. package/ui/styles/components/_metadata-index.scss +68 -0
  104. package/ui/styles/components/_nav-tree.scss +80 -0
  105. package/ui/styles/components/_referenced-items.scss +69 -0
  106. package/ui/styles/components/_sub-navigation.scss +208 -0
  107. package/ui/styles/components/_swoosh.scss +50 -0
  108. package/ui/styles/components/_timeline.scss +350 -0
  109. package/ui/styles/components/_work.scss +71 -0
  110. package/ui/styles/components/header/_header.scss +294 -0
  111. package/ui/styles/components/header/_logo.scss +99 -0
  112. package/ui/styles/components/header/_navbar.scss +143 -0
  113. package/ui/styles/components/header/index.scss +3 -0
  114. package/ui/styles/components/iiif/_image.scss +32 -0
  115. package/ui/styles/components/iiif/_slider.scss +5 -0
  116. package/ui/styles/components/iiif/index.scss +2 -0
  117. package/ui/styles/components/index.scss +25 -0
  118. package/ui/styles/components/modal/_modal.scss +130 -0
  119. package/ui/styles/components/modal/index.scss +1 -0
  120. package/ui/styles/components/search/_filters.scss +259 -0
  121. package/ui/styles/components/search/_form.scss +187 -0
  122. package/ui/styles/components/search/_panel.scss +7 -0
  123. package/ui/styles/components/search/_results.scss +241 -0
  124. package/ui/styles/components/search/_tabs.scss +60 -0
  125. package/ui/styles/components/search/index.scss +4 -0
  126. package/ui/styles/index.css +5007 -0
  127. package/ui/styles/index.scss +11 -0
  128. package/ui/styles/settings/_breakpoints.scss +7 -0
  129. package/ui/styles/settings/_focus.scss +15 -0
  130. package/ui/styles/settings/_tokens.scss +170 -0
  131. package/ui/styles/settings/_type.scss +126 -0
  132. package/ui/styles/version-parity.css +116 -0
  133. package/ui/tailwind-canopy-iiif-plugin.js +39 -0
  134. package/ui/tailwind-canopy-iiif-preset.js +32 -0
  135. package/ui/tailwind-config.d.ts +29 -0
  136. package/ui/tailwind-config.js +159 -0
  137. package/ui/tailwind-default.config.mjs +4 -0
  138. package/ui/theme-color.js +130 -0
  139. package/ui/theme-palette.js +337 -0
  140. package/ui/theme.js +153 -0
  141. package/README.md +0 -3
@@ -0,0 +1,124 @@
1
+ const { discoveryError } = require('./validation');
2
+
3
+ const MAX_RESPONSE_BYTES = 64 * 1024;
4
+ const summaryFields = [
5
+ 'id',
6
+ 'type',
7
+ 'label',
8
+ 'summary',
9
+ 'metadata',
10
+ 'rights',
11
+ 'requiredStatement',
12
+ 'provider',
13
+ ];
14
+
15
+ function displayLabel(label, language) {
16
+ if (typeof label === 'string') return label;
17
+ if (!label || typeof label !== 'object') return '';
18
+ const entries = Object.entries(label);
19
+ const preferred = language?.toLowerCase();
20
+ const entry =
21
+ (preferred && entries.find(([key]) => key.toLowerCase() === preferred)) ||
22
+ (preferred && entries.find(([key]) => key.toLowerCase() === preferred.split('-')[0])) ||
23
+ (preferred &&
24
+ entries.find(([key]) => key.toLowerCase().split('-')[0] === preferred.split('-')[0])) ||
25
+ entries.find(([key]) => key === 'none') ||
26
+ entries[0];
27
+ const values = entry?.[1];
28
+ return Array.isArray(values) ? values.join(' · ') : typeof values === 'string' ? values : '';
29
+ }
30
+
31
+ function baseEnvelope(catalog, representation = 'summary') {
32
+ return {
33
+ schemaVersion: '1.0',
34
+ protocol: 'IIIF',
35
+ api: 'Presentation',
36
+ version: '3.0',
37
+ representation,
38
+ datasetVersion: catalog.datasetVersion,
39
+ generatedAt: catalog.generatedAt ?? null,
40
+ };
41
+ }
42
+
43
+ function resourceSummary(record) {
44
+ return Object.fromEntries(
45
+ summaryFields.filter((key) => record[key] !== undefined).map((key) => [key, record[key]]),
46
+ );
47
+ }
48
+
49
+ function resourceEnvelope(record, catalog, language, resource, representation = 'summary') {
50
+ return {
51
+ ...baseEnvelope(catalog, representation),
52
+ title: displayLabel(record.label, language) || record.id,
53
+ resource: resource || resourceSummary(record),
54
+ source: {
55
+ uri: record.source?.uri ?? null,
56
+ presentationVersion: record.source?.presentationVersion ?? null,
57
+ retrievedAt: record.source?.retrievedAt ?? null,
58
+ transformation: record.source?.transformation ?? null,
59
+ },
60
+ snapshotUrl: record.snapshotUrl ?? null,
61
+ citationUrl: record.citationUrl ?? null,
62
+ collectionIds: record.collectionIds || [],
63
+ retrievalTool: record.type === 'Collection' ? 'iiif_get_collection' : 'iiif_get_manifest',
64
+ };
65
+ }
66
+
67
+ function byteLength(value) {
68
+ return new TextEncoder().encode(JSON.stringify(value)).length;
69
+ }
70
+
71
+ function boundedResponse(result) {
72
+ if (byteLength(result) > MAX_RESPONSE_BYTES) {
73
+ throw discoveryError(
74
+ 'RESOURCE_TOO_LARGE',
75
+ 'The IIIF response exceeds 64 KiB. Narrow the search or retrieve the published catalog or resource snapshot directly.',
76
+ );
77
+ }
78
+ return result;
79
+ }
80
+
81
+ function manifestEnvelope(record, catalog, language, resource) {
82
+ if (!resource || resource.id !== record.id || resource.type !== 'Manifest') {
83
+ throw discoveryError(
84
+ 'RESOURCE_UNAVAILABLE',
85
+ 'The snapshot does not match the requested IIIF Manifest.',
86
+ );
87
+ }
88
+ const full = resourceEnvelope(record, catalog, language, resource, 'full');
89
+ const originalBytes = byteLength(full);
90
+ if (originalBytes <= MAX_RESPONSE_BYTES) return full;
91
+ const partial = resourceEnvelope(record, catalog, language, undefined, 'partial');
92
+ partial.partial = {
93
+ reason: 'response-limit',
94
+ maxBytes: MAX_RESPONSE_BYTES,
95
+ fullResponseBytes: originalBytes,
96
+ omittedFields: Object.keys(resource).filter((key) => !(key in partial.resource)),
97
+ };
98
+ for (const field of ['metadata', 'summary', 'provider', 'label', 'requiredStatement', 'rights']) {
99
+ if (byteLength(partial) <= MAX_RESPONSE_BYTES) break;
100
+ if (field in partial.resource) {
101
+ delete partial.resource[field];
102
+ partial.partial.omittedFields.push(field);
103
+ }
104
+ if (field === 'label') partial.title = record.id;
105
+ }
106
+ if (byteLength(partial) > MAX_RESPONSE_BYTES) {
107
+ throw discoveryError(
108
+ 'RESOURCE_TOO_LARGE',
109
+ 'The IIIF summary exceeds the response limit. Retrieve its published snapshot.',
110
+ );
111
+ }
112
+ return partial;
113
+ }
114
+
115
+ module.exports = {
116
+ baseEnvelope,
117
+ resourceSummary,
118
+ resourceEnvelope,
119
+ manifestEnvelope,
120
+ displayLabel,
121
+ MAX_RESPONSE_BYTES,
122
+ byteLength,
123
+ boundedResponse,
124
+ };
@@ -0,0 +1,226 @@
1
+ const fs = require('node:fs/promises');
2
+ const path = require('node:path');
3
+ const { createHash } = require('node:crypto');
4
+ const { webMcpEnabled } = require('./config');
5
+ const { resourceKey, publicUrl, presentationVersion, readProvenance } = require('./provenance');
6
+
7
+ // Match the IIIF builder's search configuration coercion, including quoted YAML booleans.
8
+ function resolveBoolean(value) {
9
+ if (typeof value === 'boolean') return value;
10
+ if (value === undefined || value === null) return false;
11
+ return ['1', 'true', 'yes'].includes(String(value).trim().toLowerCase());
12
+ }
13
+ function indexEnabled(config, fallback) {
14
+ return Object.prototype.hasOwnProperty.call(config || {}, 'enabled')
15
+ ? resolveBoolean(config.enabled)
16
+ : fallback;
17
+ }
18
+
19
+ const values = (value) =>
20
+ typeof value === 'string'
21
+ ? [value]
22
+ : Object.values(value || {})
23
+ .flat()
24
+ .filter((v) => typeof v === 'string');
25
+ const labelKey = (value) =>
26
+ value
27
+ .trim()
28
+ .replace(/[:\s]+$/g, '')
29
+ .toLowerCase();
30
+ const text = (value) =>
31
+ values(value)
32
+ .join(' ')
33
+ .replace(/<[^>]*>/g, ' ')
34
+ .replace(/\s+/g, ' ')
35
+ .trim();
36
+ function stable(value) {
37
+ if (Array.isArray(value)) return value.map(stable);
38
+ if (value && typeof value === 'object')
39
+ return Object.fromEntries(
40
+ Object.keys(value)
41
+ .sort()
42
+ .map((key) => [key, stable(value[key])]),
43
+ );
44
+ return value;
45
+ }
46
+ function safeResource(resource) {
47
+ // Never publish local paths or credential-bearing URLs, including nested IIIF IDs.
48
+ if (Array.isArray(resource)) return resource.map(safeResource);
49
+ if (!resource || typeof resource !== 'object') return resource;
50
+ return Object.fromEntries(
51
+ Object.entries(resource).flatMap(([key, value]) => {
52
+ if ((key === 'id' || key === '@id') && typeof value === 'string' && !publicUrl(value))
53
+ throw new Error('Resource contains a non-public identifier.');
54
+ return [[key, safeResource(value)]];
55
+ }),
56
+ );
57
+ }
58
+ function facetsFor(resource, config) {
59
+ const index = config.search?.index?.metadata || {};
60
+ if (!indexEnabled(index, true)) return {};
61
+ const includeAll = resolveBoolean(index.all);
62
+ const allowed = new Set((config.metadata || []).map((label) => labelKey(String(label))));
63
+ const result = Object.create(null);
64
+ for (const entry of resource.metadata || []) {
65
+ const label = text(entry.label);
66
+ if (!label || (!includeAll && !allowed.has(labelKey(label)))) continue;
67
+ result[label] = [
68
+ ...new Set([...(result[label] || []), ...values(entry.value).map(text).filter(Boolean)]),
69
+ ];
70
+ }
71
+ return result;
72
+ }
73
+ function addMembership(records) {
74
+ const byId = new Map(records.map((record) => [record.id, record]));
75
+ for (const parent of records.filter((record) => record.type === 'Collection')) {
76
+ const visit = (id, seen) => {
77
+ if (seen.has(id)) return;
78
+ seen.add(id);
79
+ const child = byId.get(id);
80
+ if (!child) return;
81
+ if (child.id !== parent.id && !child.collectionIds.includes(parent.id))
82
+ child.collectionIds.push(parent.id);
83
+ for (const item of child.items || []) visit(item.id, seen);
84
+ };
85
+ for (const item of parent.items || []) visit(item.id, new Set([parent.id]));
86
+ }
87
+ for (const record of records) record.collectionIds.sort();
88
+ }
89
+
90
+ async function exportDiscovery({
91
+ config = {},
92
+ entries = [],
93
+ roots = [],
94
+ requestedManifests = [],
95
+ failures = [],
96
+ records = [],
97
+ outDir,
98
+ cacheDir,
99
+ absoluteUrl = (url) => url,
100
+ }) {
101
+ const dest = path.join(outDir, 'api', 'discovery');
102
+ if (!webMcpEnabled(config)) {
103
+ await fs.rm(dest, { recursive: true, force: true });
104
+ return null;
105
+ }
106
+ const index = config.search?.index || {};
107
+ const metadataEnabled = indexEnabled(index.metadata, true);
108
+ const summaryEnabled = indexEnabled(index.summary, true);
109
+ const annotationsEnabled = indexEnabled(index.annotations, false);
110
+ const recordsById = new Map(records.map((record) => [record.id, record]));
111
+ const resources = [];
112
+ const snapshots = new Map();
113
+ const unavailable = failures.map((entry) => ({
114
+ id: publicUrl(entry.id),
115
+ reason: entry.reason || 'retrieval_failed',
116
+ }));
117
+ for (const entry of entries) {
118
+ const original = entry.resource;
119
+ const id = original?.id;
120
+ if (
121
+ !publicUrl(id) ||
122
+ !['Manifest', 'Collection'].includes(original?.type) ||
123
+ presentationVersion(original) !== '3.0'
124
+ ) {
125
+ unavailable.push({ id: publicUrl(id), reason: 'unsupported_resource' });
126
+ continue;
127
+ }
128
+ if (snapshots.has(id)) continue;
129
+ let resource;
130
+ try {
131
+ resource = safeResource(original);
132
+ } catch (_) {
133
+ unavailable.push({ id, reason: 'non_public_identifier' });
134
+ continue;
135
+ }
136
+ const source = await readProvenance(original, cacheDir);
137
+ const display = recordsById.get(id);
138
+ const facets = facetsFor(resource, config);
139
+ const record = {
140
+ id,
141
+ type: resource.type,
142
+ label: resource.label || { none: [id] },
143
+ source: { ...source, uri: publicUrl(source.uri) },
144
+ snapshotUrl: `resources/${resourceKey(id)}-${resourceKey(JSON.stringify(stable(resource)))}.json`,
145
+ citationUrl: display?.href
146
+ ? absoluteUrl(display.href)
147
+ : publicUrl(resource.homepage?.[0]?.id) || publicUrl(id),
148
+ collectionIds: [],
149
+ facets,
150
+ searchText: [
151
+ text(resource.label),
152
+ ...(metadataEnabled ? display?.searchMetadataValues || Object.values(facets).flat() : []),
153
+ summaryEnabled ? text(resource.summary) : '',
154
+ annotationsEnabled ? display?.searchAnnotation || '' : '',
155
+ ]
156
+ .join(' ')
157
+ .trim(),
158
+ };
159
+ for (const key of ['summary', 'metadata', 'rights', 'requiredStatement', 'provider'])
160
+ if (resource[key] !== undefined) record[key] = resource[key];
161
+ if (resource.type === 'Collection') {
162
+ record.items = (entry.items || resource.items || [])
163
+ .filter((item) => publicUrl(item.id) && ['Manifest', 'Collection'].includes(item.type))
164
+ .map((item) => ({
165
+ id: item.id,
166
+ type: item.type,
167
+ ...(item.label ? { label: item.label } : {}),
168
+ }));
169
+ }
170
+ resources.push(record);
171
+ snapshots.set(id, resource);
172
+ }
173
+ resources.sort((a, b) => a.id.localeCompare(b.id));
174
+ addMembership(resources);
175
+ const ids = new Set(resources.map((record) => record.id));
176
+ for (const id of requestedManifests)
177
+ if (!ids.has(id)) unavailable.push({ id: publicUrl(id), reason: 'manifest_unavailable' });
178
+ const uniqueFailures = [
179
+ ...new Map(unavailable.map((entry) => [JSON.stringify(entry), entry])).values(),
180
+ ];
181
+ const datasetVersion = createHash('sha256')
182
+ .update(
183
+ JSON.stringify(
184
+ stable({
185
+ resources,
186
+ roots,
187
+ failures: uniqueFailures,
188
+ snapshots: [...snapshots].sort(([a], [b]) => a.localeCompare(b)),
189
+ }),
190
+ ),
191
+ )
192
+ .digest('hex');
193
+ const catalog = {
194
+ schemaVersion: '1.0',
195
+ protocol: 'IIIF',
196
+ datasetVersion,
197
+ generatedAt: new Date().toISOString(),
198
+ title: config.title || config.site?.title || 'IIIF Collections',
199
+ roots: roots.filter((id) => ids.has(id)),
200
+ coverage: {
201
+ manifests: resources.filter((r) => r.type === 'Manifest').length,
202
+ collections: resources.filter((r) => r.type === 'Collection').length,
203
+ providerTotal: null,
204
+ complete: uniqueFailures.length === 0,
205
+ unavailable: uniqueFailures,
206
+ },
207
+ resources,
208
+ };
209
+ await fs.mkdir(path.join(outDir, 'api'), { recursive: true });
210
+ const staging = await fs.mkdtemp(path.join(outDir, 'api', '.discovery-'));
211
+ try {
212
+ await fs.mkdir(path.join(staging, 'resources'));
213
+ for (const record of resources)
214
+ await fs.writeFile(
215
+ path.join(staging, record.snapshotUrl),
216
+ JSON.stringify(snapshots.get(record.id)),
217
+ );
218
+ await fs.writeFile(path.join(staging, 'index.json'), JSON.stringify(catalog));
219
+ await fs.rm(dest, { recursive: true, force: true });
220
+ await fs.rename(staging, dest);
221
+ } finally {
222
+ await fs.rm(staging, { recursive: true, force: true });
223
+ }
224
+ return catalog;
225
+ }
226
+ module.exports = { exportDiscovery, facetsFor, addMembership };
@@ -0,0 +1,62 @@
1
+ const { webMcpEnabled } = require('./config');
2
+ const { publicUrl } = require('./provenance');
3
+
4
+ function createIngestionReport(config) {
5
+ const enabled = webMcpEnabled(config);
6
+ const entries = new Map();
7
+ const roots = new Set();
8
+ const failures = [];
9
+ const requested = new Set();
10
+ const aliases = new Map();
11
+ const resolveId = (id) => {
12
+ const seen = new Set();
13
+ while (aliases.has(id) && !seen.has(id)) {
14
+ seen.add(id);
15
+ id = aliases.get(id);
16
+ }
17
+ return id;
18
+ };
19
+ return {
20
+ collection(resource, items, root = false, requestedId) {
21
+ if (!enabled || !resource?.id) return;
22
+ if (requestedId && requestedId !== resource.id) aliases.set(requestedId, resource.id);
23
+ const existing = entries.get(resource.id);
24
+ const children = [...(existing?.items || []), ...items];
25
+ entries.set(resource.id, {
26
+ resource,
27
+ items: [...new Map(children.map((child) => [child.id, child])).values()],
28
+ });
29
+ if (root) roots.add(resource.id);
30
+ },
31
+ manifest(resource, requestedId) {
32
+ if (enabled && resource?.id) {
33
+ if (requestedId && requestedId !== resource.id) aliases.set(requestedId, resource.id);
34
+ entries.set(resource.id, { resource });
35
+ requested.delete(requestedId);
36
+ requested.add(resource.id);
37
+ }
38
+ },
39
+ request(id) {
40
+ if (enabled) requested.add(id);
41
+ },
42
+ failure(id, reason = 'retrieval_failed') {
43
+ if (enabled) failures.push({ id: publicUrl(id), reason });
44
+ },
45
+ result() {
46
+ return {
47
+ entries: [...entries.values()].map((entry) => {
48
+ if (!entry.items) return entry;
49
+ const children = entry.items.map((child) => ({ ...child, id: resolveId(child.id) }));
50
+ return {
51
+ ...entry,
52
+ items: [...new Map(children.map((child) => [child.id, child])).values()],
53
+ };
54
+ }),
55
+ roots: [...new Set([...roots].map(resolveId))],
56
+ requestedManifests: [...new Set([...requested].map(resolveId))],
57
+ failures,
58
+ };
59
+ },
60
+ };
61
+ }
62
+ module.exports = { createIngestionReport };
@@ -0,0 +1,66 @@
1
+ const { discoveryError, DEFAULT_PAGE_SIZE } = require('./validation');
2
+ const { byteLength, boundedResponse, MAX_RESPONSE_BYTES } = require('./envelope');
3
+
4
+ function fingerprint(value) {
5
+ const text = JSON.stringify(value);
6
+ let hash = 2166136261;
7
+ for (let index = 0; index < text.length; index += 1) {
8
+ hash = Math.imul(hash ^ text.charCodeAt(index), 16777619);
9
+ }
10
+ return (hash >>> 0).toString(36);
11
+ }
12
+
13
+ function paginate(records, input, catalog, scope) {
14
+ let offset = 0;
15
+ const query = fingerprint(scope);
16
+ if (input.cursor !== undefined) {
17
+ let cursor;
18
+ try {
19
+ cursor = JSON.parse(decodeURIComponent(input.cursor));
20
+ } catch {
21
+ throw discoveryError('INVALID_CURSOR', 'The cursor is invalid. Restart the query.');
22
+ }
23
+ if (!cursor || typeof cursor !== 'object' || cursor.version !== catalog.datasetVersion) {
24
+ throw discoveryError(
25
+ 'STALE_CURSOR',
26
+ 'The dataset changed. Restart the query without a cursor.',
27
+ );
28
+ }
29
+ if (
30
+ cursor.query !== query ||
31
+ !Number.isSafeInteger(cursor.offset) ||
32
+ cursor.offset < 0 ||
33
+ cursor.offset > records.length
34
+ ) {
35
+ throw discoveryError('INVALID_CURSOR', 'The cursor does not belong to this query.');
36
+ }
37
+ offset = cursor.offset;
38
+ }
39
+ const limit = input.limit ?? DEFAULT_PAGE_SIZE;
40
+ const end = Math.min(offset + limit, records.length);
41
+ const nextCursor =
42
+ end < records.length
43
+ ? encodeURIComponent(JSON.stringify({ version: catalog.datasetVersion, query, offset: end }))
44
+ : null;
45
+ return {
46
+ records: records.slice(offset, end),
47
+ pagination: { total: records.length, limit, nextCursor },
48
+ };
49
+ }
50
+
51
+ function paginateResponse(records, input, catalog, scope, render) {
52
+ let page = paginate(records, input, catalog, scope);
53
+ let result = render(page);
54
+ while (byteLength(result) > MAX_RESPONSE_BYTES && page.records.length > 1) {
55
+ page = paginate(
56
+ records,
57
+ { ...input, limit: Math.floor(page.records.length / 2) },
58
+ catalog,
59
+ scope,
60
+ );
61
+ result = render(page);
62
+ }
63
+ return boundedResponse(result);
64
+ }
65
+
66
+ module.exports = { paginateResponse };
@@ -0,0 +1,79 @@
1
+ const fs = require('node:fs/promises');
2
+ const path = require('node:path');
3
+ const { createHash } = require('node:crypto');
4
+
5
+ const retrieved = new WeakMap();
6
+ const resourceKey = (id) => createHash('sha256').update(String(id)).digest('hex');
7
+ function publicUrl(value) {
8
+ try {
9
+ const url = new URL(value);
10
+ return /^https?:$/.test(url.protocol) && !url.username && !url.password ? url.href : null;
11
+ } catch (_) {
12
+ return null;
13
+ }
14
+ }
15
+ function presentationVersion(resource) {
16
+ const context = JSON.stringify(resource?.['@context'] || '');
17
+ if (context.includes('/presentation/3/')) return '3.0';
18
+ if (context.includes('/presentation/2/')) return '2.1';
19
+ return null;
20
+ }
21
+ function rememberRetrieved(resource, uri) {
22
+ if (resource && typeof resource === 'object') {
23
+ retrieved.set(resource, {
24
+ uri: publicUrl(uri),
25
+ presentationVersion: presentationVersion(resource),
26
+ retrievedAt: new Date().toISOString(),
27
+ transformation: null,
28
+ });
29
+ }
30
+ return resource;
31
+ }
32
+ function inheritProvenance(original, normalized) {
33
+ if (normalized && typeof normalized === 'object' && retrieved.has(original)) {
34
+ const source = retrieved.get(original);
35
+ retrieved.set(normalized, {
36
+ ...source,
37
+ transformation:
38
+ source.presentationVersion === '2.1' && presentationVersion(normalized) === '3.0'
39
+ ? 'presentation-2-to-3'
40
+ : source.transformation,
41
+ });
42
+ }
43
+ return normalized;
44
+ }
45
+ async function saveProvenance(resource, cacheDir = path.resolve('.cache/iiif')) {
46
+ const source = retrieved.get(resource);
47
+ const id = resource?.id || resource?.['@id'];
48
+ if (!source || !id) return;
49
+ const dir = path.join(cacheDir, 'provenance');
50
+ await fs.mkdir(dir, { recursive: true });
51
+ await fs.writeFile(path.join(dir, `${resourceKey(id)}.json`), JSON.stringify(source));
52
+ }
53
+ async function readProvenance(resource, cacheDir = path.resolve('.cache/iiif')) {
54
+ if (retrieved.has(resource)) return retrieved.get(resource);
55
+ try {
56
+ return JSON.parse(
57
+ await fs.readFile(
58
+ path.join(cacheDir, 'provenance', `${resourceKey(resource.id)}.json`),
59
+ 'utf8',
60
+ ),
61
+ );
62
+ } catch (_) {
63
+ return {
64
+ uri: publicUrl(resource.id),
65
+ presentationVersion: null,
66
+ retrievedAt: null,
67
+ transformation: null,
68
+ };
69
+ }
70
+ }
71
+ module.exports = {
72
+ resourceKey,
73
+ publicUrl,
74
+ presentationVersion,
75
+ rememberRetrieved,
76
+ inheritProvenance,
77
+ saveProvenance,
78
+ readProvenance,
79
+ };
@@ -0,0 +1,81 @@
1
+ const { discoveryError } = require('./validation');
2
+
3
+ const normalize = (value) => String(value).normalize('NFKC').toLowerCase().trim();
4
+
5
+ function searchCriteria(input) {
6
+ const facets = Object.entries(input.facets || {})
7
+ .map(([label, values]) => [normalize(label), [...new Set(values.map(normalize))].sort()])
8
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
9
+ return {
10
+ query: [...new Set(normalize(input.query).split(/\s+/).filter(Boolean))].sort(),
11
+ collectionId: input.collectionId ?? null,
12
+ facets,
13
+ };
14
+ }
15
+
16
+ function facetEntries(record) {
17
+ return Object.entries(record.facets || {}).map(([label, values]) => [
18
+ normalize(label),
19
+ values.map(normalize),
20
+ ]);
21
+ }
22
+
23
+ function searchRecords(catalog, criteria) {
24
+ const manifests = catalog.resources.filter((record) => record.type === 'Manifest');
25
+ const knownLabels = new Set(
26
+ manifests.flatMap((record) => facetEntries(record).map(([label]) => label)),
27
+ );
28
+ for (const [label] of criteria.facets) {
29
+ if (!knownLabels.has(label)) {
30
+ throw discoveryError(
31
+ 'INVALID_ARGUMENT',
32
+ `Unknown indexed facet: ${label}. Use iiif_describe_site to find facet labels.`,
33
+ );
34
+ }
35
+ }
36
+ return manifests
37
+ .filter((record) => {
38
+ if (criteria.collectionId && !record.collectionIds?.includes(criteria.collectionId))
39
+ return false;
40
+ // Search only the exported index text, never excluded metadata in the descriptive record.
41
+ const text = normalize(record.searchText || '');
42
+ if (!criteria.query.every((token) => text.includes(token))) return false;
43
+ const facets = facetEntries(record);
44
+ return criteria.facets.every(([label, accepted]) =>
45
+ facets.some(
46
+ ([key, values]) => key === label && accepted.some((value) => values.includes(value)),
47
+ ),
48
+ );
49
+ })
50
+ .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
51
+ }
52
+
53
+ function facetSummary(catalog) {
54
+ const labels = new Map();
55
+ for (const record of catalog.resources) {
56
+ if (record.type !== 'Manifest') continue;
57
+ for (const [label, values] of Object.entries(record.facets || {})) {
58
+ if (!labels.has(label)) labels.set(label, new Set());
59
+ for (const value of values) labels.get(label).add(value);
60
+ }
61
+ }
62
+ return [...labels]
63
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
64
+ .map(([label, values]) => ({
65
+ label,
66
+ values: [...values].sort().slice(0, 100),
67
+ totalValues: values.size,
68
+ truncated: values.size > 100,
69
+ }));
70
+ }
71
+
72
+ function matchSummary(record, tokens) {
73
+ const text = String(record.searchText || '')
74
+ .replace(/\s+/g, ' ')
75
+ .trim();
76
+ const firstMatch = tokens.length ? normalize(text).indexOf(tokens[0]) : 0;
77
+ const start = Math.max(0, firstMatch - 80);
78
+ return `${start ? '…' : ''}${text.slice(start, start + 400)}${start + 400 < text.length ? '…' : ''}`;
79
+ }
80
+
81
+ module.exports = { searchCriteria, searchRecords, facetSummary, matchSummary };