@pie-qti/ims-cp-core 1.0.0-beta.0 → 1.0.0-beta.1
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/dist/index.d.ts +14 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/localized-resources.d.ts +308 -0
- package/dist/localized-resources.js +410 -0
- package/dist/localized-resources.js.map +1 -0
- package/dist/manifest-parser.d.ts +82 -0
- package/dist/manifest-parser.js +213 -0
- package/dist/manifest-parser.js.map +1 -0
- package/dist/package-file-resolver.d.ts +48 -0
- package/dist/package-file-resolver.js +312 -0
- package/dist/package-file-resolver.js.map +1 -0
- package/dist/package-graph.d.ts +134 -0
- package/dist/package-graph.js +791 -0
- package/dist/package-graph.js.map +1 -0
- package/dist/package-path.d.ts +13 -0
- package/dist/package-path.js +90 -0
- package/dist/package-path.js.map +1 -0
- package/dist/passage-reusability.d.ts +99 -0
- package/dist/passage-reusability.js +170 -0
- package/dist/passage-reusability.js.map +1 -0
- package/dist/qti-heuristics.d.ts +119 -0
- package/dist/qti-heuristics.js +150 -0
- package/dist/qti-heuristics.js.map +1 -0
- package/dist/qti3-shared-content.d.ts +114 -0
- package/dist/qti3-shared-content.js +422 -0
- package/dist/qti3-shared-content.js.map +1 -0
- package/dist/security-parsing.d.ts +9 -0
- package/dist/security-parsing.js +74 -0
- package/dist/security-parsing.js.map +1 -0
- package/dist/utils/hash.d.ts +20 -0
- package/dist/utils/hash.js +33 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/qti-xml.d.ts +103 -0
- package/dist/utils/qti-xml.js +214 -0
- package/dist/utils/qti-xml.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QTI Manifest (imsmanifest.xml) parser
|
|
3
|
+
*
|
|
4
|
+
* Parses IMS Content Package manifests to extract:
|
|
5
|
+
* - Resource definitions (items, passages, assessments)
|
|
6
|
+
* - Dependencies between resources
|
|
7
|
+
* - File locations and types
|
|
8
|
+
*/
|
|
9
|
+
import { parse } from 'node-html-parser';
|
|
10
|
+
import { PassageRegistry } from './passage-reusability.js';
|
|
11
|
+
/**
|
|
12
|
+
* Parse QTI manifest XML
|
|
13
|
+
*/
|
|
14
|
+
export function parseManifest(manifestXml, basePath) {
|
|
15
|
+
const doc = parse(manifestXml, {
|
|
16
|
+
lowerCaseTagName: false,
|
|
17
|
+
comment: false,
|
|
18
|
+
});
|
|
19
|
+
const manifest = doc.querySelector('manifest') || doc.getElementsByTagName('manifest')[0];
|
|
20
|
+
if (!manifest) {
|
|
21
|
+
throw new Error('No manifest element found in XML');
|
|
22
|
+
}
|
|
23
|
+
const manifestId = manifest.getAttribute('identifier');
|
|
24
|
+
const manifestXmlBase = manifest.getAttribute('xml:base') || undefined;
|
|
25
|
+
// Parse all resources
|
|
26
|
+
const resourcesElement = manifest.querySelector('resources') ||
|
|
27
|
+
manifest.getElementsByTagName('resources')[0];
|
|
28
|
+
if (!resourcesElement) {
|
|
29
|
+
return {
|
|
30
|
+
identifier: manifestId,
|
|
31
|
+
resources: new Map(),
|
|
32
|
+
items: [],
|
|
33
|
+
passages: [],
|
|
34
|
+
tests: [],
|
|
35
|
+
basePath,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const resourceElements = resourcesElement.getElementsByTagName('resource');
|
|
39
|
+
const resources = new Map();
|
|
40
|
+
const items = [];
|
|
41
|
+
const passages = [];
|
|
42
|
+
const tests = [];
|
|
43
|
+
for (const resourceEl of Array.from(resourceElements)) {
|
|
44
|
+
const resource = parseResource(resourceEl);
|
|
45
|
+
resources.set(resource.identifier, resource);
|
|
46
|
+
// Categorize by type
|
|
47
|
+
if (isItemType(resource.type)) {
|
|
48
|
+
items.push(resource);
|
|
49
|
+
}
|
|
50
|
+
else if (isPassageType(resource.type)) {
|
|
51
|
+
passages.push(resource);
|
|
52
|
+
}
|
|
53
|
+
else if (isTestType(resource.type)) {
|
|
54
|
+
tests.push(resource);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
identifier: manifestId,
|
|
59
|
+
xmlBase: manifestXmlBase,
|
|
60
|
+
resources,
|
|
61
|
+
items,
|
|
62
|
+
passages,
|
|
63
|
+
tests,
|
|
64
|
+
basePath,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse individual resource element
|
|
69
|
+
*/
|
|
70
|
+
function parseResource(resourceEl) {
|
|
71
|
+
const identifier = resourceEl.getAttribute('identifier') || '';
|
|
72
|
+
const type = resourceEl.getAttribute('type') || '';
|
|
73
|
+
const href = resourceEl.getAttribute('href') || undefined;
|
|
74
|
+
const xmlBase = resourceEl.getAttribute('xml:base') || undefined;
|
|
75
|
+
// Extract all file elements
|
|
76
|
+
const fileElements = resourceEl.getElementsByTagName('file');
|
|
77
|
+
const files = Array.from(fileElements)
|
|
78
|
+
.map(f => f.getAttribute('href'))
|
|
79
|
+
.filter((h) => !!h);
|
|
80
|
+
// If href not specified but files exist, use first file as href
|
|
81
|
+
const mainHref = href || (files.length > 0 ? files[0] : undefined);
|
|
82
|
+
// Extract dependencies
|
|
83
|
+
const depElements = resourceEl.getElementsByTagName('dependency');
|
|
84
|
+
const dependencies = Array.from(depElements)
|
|
85
|
+
.map(d => d.getAttribute('identifierref'))
|
|
86
|
+
.filter((id) => !!id);
|
|
87
|
+
const metadataEl = resourceEl.querySelector('metadata');
|
|
88
|
+
const metadata = metadataEl ? parseResourceMetadata(metadataEl) : undefined;
|
|
89
|
+
return {
|
|
90
|
+
identifier,
|
|
91
|
+
type,
|
|
92
|
+
xmlBase,
|
|
93
|
+
href: mainHref,
|
|
94
|
+
files,
|
|
95
|
+
dependencies,
|
|
96
|
+
metadata,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Title, description and language of a resource's `<metadata>`
|
|
101
|
+
*
|
|
102
|
+
* LOM is matched by local name, so a prefixed binding (`imsmd:title`) reads the same as an unprefixed
|
|
103
|
+
* one, and LangString text comes from its first `string` (or LOM 1.2 `langstring`). When the metadata
|
|
104
|
+
* holds LOM, only `general` is read: a `classification` description is not the resource's.
|
|
105
|
+
*/
|
|
106
|
+
function parseResourceMetadata(metadataEl) {
|
|
107
|
+
let title;
|
|
108
|
+
let description;
|
|
109
|
+
let language;
|
|
110
|
+
const loms = descendantsNamed(metadataEl, 'lom');
|
|
111
|
+
if (loms.length > 0) {
|
|
112
|
+
const general = loms.flatMap((lom) => childrenNamed(lom, 'general'))[0];
|
|
113
|
+
if (general) {
|
|
114
|
+
title = langStringText(childrenNamed(general, 'title')[0]);
|
|
115
|
+
description = langStringText(childrenNamed(general, 'description')[0]);
|
|
116
|
+
language = textOf(childrenNamed(general, 'language')[0]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
title = langStringText(descendantsNamed(metadataEl, 'title')[0]);
|
|
121
|
+
description = langStringText(descendantsNamed(metadataEl, 'description')[0]);
|
|
122
|
+
const languageEl = descendantsNamed(metadataEl, 'language')[0] ||
|
|
123
|
+
metadataEl.querySelector('[\\:language]') ||
|
|
124
|
+
metadataEl.querySelector('[name="language"]');
|
|
125
|
+
language = textOf(languageEl ?? undefined);
|
|
126
|
+
}
|
|
127
|
+
return title || description || language ? { title, description, language } : undefined;
|
|
128
|
+
}
|
|
129
|
+
function langStringText(el) {
|
|
130
|
+
if (!el)
|
|
131
|
+
return undefined;
|
|
132
|
+
const string = [...childrenNamed(el, 'string'), ...childrenNamed(el, 'langstring')][0];
|
|
133
|
+
return textOf(string ?? el);
|
|
134
|
+
}
|
|
135
|
+
function textOf(el) {
|
|
136
|
+
return el?.textContent?.trim() || undefined;
|
|
137
|
+
}
|
|
138
|
+
/** Tag name without its namespace prefix, lowercased. */
|
|
139
|
+
function localName(el) {
|
|
140
|
+
return (el.rawTagName ?? '').replace(/^[^:]*:/, '').toLowerCase();
|
|
141
|
+
}
|
|
142
|
+
function childrenNamed(el, name) {
|
|
143
|
+
return el.children.filter((child) => localName(child) === name);
|
|
144
|
+
}
|
|
145
|
+
function descendantsNamed(el, name) {
|
|
146
|
+
return el.children.flatMap((child) => [
|
|
147
|
+
...(localName(child) === name ? [child] : []),
|
|
148
|
+
...descendantsNamed(child, name),
|
|
149
|
+
]);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if resource type is an item
|
|
153
|
+
*/
|
|
154
|
+
function isItemType(type) {
|
|
155
|
+
return type.includes('imsqti_item') ||
|
|
156
|
+
type.includes('qti_item') ||
|
|
157
|
+
type === 'associatedcontent/imsqti_item_xmlv2p1';
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Check if resource type is a passage/stimulus
|
|
161
|
+
*/
|
|
162
|
+
function isPassageType(type) {
|
|
163
|
+
return type === 'webcontent' ||
|
|
164
|
+
type.includes('passage') ||
|
|
165
|
+
type.includes('stimulus') ||
|
|
166
|
+
type.includes('apip');
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Check if resource type is a test/assessment
|
|
170
|
+
*/
|
|
171
|
+
function isTestType(type) {
|
|
172
|
+
return type.includes('test') || type.includes('assessment');
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Build passage registry from manifest
|
|
176
|
+
*
|
|
177
|
+
* Analyzes manifest to identify:
|
|
178
|
+
* - Which passages are referenced by items
|
|
179
|
+
* - Which passages are reusable (multiple items reference them)
|
|
180
|
+
*/
|
|
181
|
+
export function buildPassageRegistryFromManifest(manifest) {
|
|
182
|
+
const registry = new PassageRegistry();
|
|
183
|
+
// Register all passage resources
|
|
184
|
+
for (const passage of manifest.passages) {
|
|
185
|
+
// Find all items that depend on this passage
|
|
186
|
+
for (const item of manifest.items) {
|
|
187
|
+
if (item.dependencies.includes(passage.identifier)) {
|
|
188
|
+
registry.registerReference(item.identifier, {
|
|
189
|
+
id: passage.identifier, // Use QTI identifier directly
|
|
190
|
+
source: 'manifest',
|
|
191
|
+
qtiIdentifier: passage.identifier,
|
|
192
|
+
filePath: passage.href,
|
|
193
|
+
isReusable: false, // Will be updated by registry
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return registry;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get passage content file path from manifest
|
|
202
|
+
*/
|
|
203
|
+
export function getPassageFilePath(manifest, passageId) {
|
|
204
|
+
const passage = manifest.resources.get(passageId);
|
|
205
|
+
return passage?.href;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Get all items that depend on a passage
|
|
209
|
+
*/
|
|
210
|
+
export function getItemsReferencingPassage(manifest, passageId) {
|
|
211
|
+
return manifest.items.filter(item => item.dependencies.includes(passageId));
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=manifest-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest-parser.js","sourceRoot":"","sources":["../src/manifest-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAmE3D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,QAAiB;IAClE,MAAM,GAAG,GAAG,KAAK,CAAC,WAAW,EAAE;QAC7B,gBAAgB,EAAE,KAAK;QACvB,OAAO,EAAE,KAAK;KACf,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,GAAG,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1F,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,YAAY,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IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;gBACnD,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,UAAU,EAAE;oBAC1C,EAAE,EAAE,OAAO,CAAC,UAAU,EAAG,8BAA8B;oBACvD,MAAM,EAAE,UAAU;oBAClB,aAAa,EAAE,OAAO,CAAC,UAAU;oBACjC,QAAQ,EAAE,OAAO,CAAC,IAAI;oBACtB,UAAU,EAAE,KAAK,EAAG,8BAA8B;iBACnD,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAAwB,EACxB,SAAiB;IAEjB,MAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAClD,OAAO,OAAO,EAAE,IAAI,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACxC,QAAwB,EACxB,SAAiB;IAEjB,OAAO,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAClC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CACtC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * QTI Manifest (imsmanifest.xml) parser\n *\n * Parses IMS Content Package manifests to extract:\n * - Resource definitions (items, passages, assessments)\n * - Dependencies between resources\n * - File locations and types\n */\n\nimport type { HTMLElement } from 'node-html-parser';\nimport { parse } from 'node-html-parser';\nimport { PassageRegistry } from './passage-reusability.js';\n\n/**\n * QTI resource type\n */\nexport type ResourceType =\n | 'imsqti_item_xmlv2p1'\n | 'imsqti_item_xmlv2p2'\n | 'imsqti_test_xmlv2p1'\n | 'imsqti_test_xmlv2p2'\n | 'imsqti_assessment_xmlv2p1'\n | 'imsqti_apip_xmlv2p2'\n | 'webcontent'\n | 'associatedcontent/imsqti_item_xmlv2p1'\n | string;\n\n/**\n * Manifest resource entry\n */\nexport interface ManifestResource {\n /** Resource identifier */\n identifier: string;\n /** Resource type */\n type: ResourceType;\n /**\n * Optional xml:base for this resource element.\n * Used by IMS CP to resolve relative hrefs when packages use nested bases.\n */\n xmlBase?: string;\n /** Main file href */\n href?: string;\n /** All files in this resource */\n files: string[];\n /** Dependencies (identifierrefs) */\n dependencies: string[];\n /** Metadata */\n metadata?: {\n title?: string;\n description?: string;\n /** IMS LOM language code (e.g., \"en-US\", \"es-ES\") */\n language?: string;\n };\n}\n\n/**\n * Parsed manifest structure\n */\nexport interface ParsedManifest {\n /** Manifest identifier */\n identifier?: string;\n /**\n * Optional xml:base at the manifest element level.\n * Used by IMS CP to resolve relative hrefs.\n */\n xmlBase?: string;\n /** All resources */\n resources: Map<string, ManifestResource>;\n /** Item resources */\n items: ManifestResource[];\n /** Passage/stimulus resources */\n passages: ManifestResource[];\n /** Test/assessment resources */\n tests: ManifestResource[];\n /** Base path for resolving relative paths */\n basePath?: string;\n}\n\n/**\n * Parse QTI manifest XML\n */\nexport function parseManifest(manifestXml: string, basePath?: string): ParsedManifest {\n const doc = parse(manifestXml, {\n lowerCaseTagName: false,\n comment: false,\n });\n\n const manifest = doc.querySelector('manifest') || doc.getElementsByTagName('manifest')[0];\n\n if (!manifest) {\n throw new Error('No manifest element found in XML');\n }\n\n const manifestId = manifest.getAttribute('identifier');\n const manifestXmlBase = manifest.getAttribute('xml:base') || undefined;\n\n // Parse all resources\n const resourcesElement = manifest.querySelector('resources') ||\n manifest.getElementsByTagName('resources')[0];\n\n if (!resourcesElement) {\n return {\n identifier: manifestId,\n resources: new Map(),\n items: [],\n passages: [],\n tests: [],\n basePath,\n };\n }\n\n const resourceElements = resourcesElement.getElementsByTagName('resource');\n const resources = new Map<string, ManifestResource>();\n const items: ManifestResource[] = [];\n const passages: ManifestResource[] = [];\n const tests: ManifestResource[] = [];\n\n for (const resourceEl of Array.from(resourceElements)) {\n const resource = parseResource(resourceEl);\n resources.set(resource.identifier, resource);\n\n // Categorize by type\n if (isItemType(resource.type)) {\n items.push(resource);\n } else if (isPassageType(resource.type)) {\n passages.push(resource);\n } else if (isTestType(resource.type)) {\n tests.push(resource);\n }\n }\n\n return {\n identifier: manifestId,\n xmlBase: manifestXmlBase,\n resources,\n items,\n passages,\n tests,\n basePath,\n };\n}\n\n/**\n * Parse individual resource element\n */\nfunction parseResource(resourceEl: HTMLElement): ManifestResource {\n const identifier = resourceEl.getAttribute('identifier') || '';\n const type = resourceEl.getAttribute('type') || '';\n const href = resourceEl.getAttribute('href') || undefined;\n const xmlBase = resourceEl.getAttribute('xml:base') || undefined;\n\n // Extract all file elements\n const fileElements = resourceEl.getElementsByTagName('file');\n const files = Array.from(fileElements)\n .map(f => f.getAttribute('href'))\n .filter((h): h is string => !!h);\n\n // If href not specified but files exist, use first file as href\n const mainHref = href || (files.length > 0 ? files[0] : undefined);\n\n // Extract dependencies\n const depElements = resourceEl.getElementsByTagName('dependency');\n const dependencies = Array.from(depElements)\n .map(d => d.getAttribute('identifierref'))\n .filter((id): id is string => !!id);\n\n const metadataEl = resourceEl.querySelector('metadata');\n const metadata = metadataEl ? parseResourceMetadata(metadataEl) : undefined;\n\n return {\n identifier,\n type,\n xmlBase,\n href: mainHref,\n files,\n dependencies,\n metadata,\n };\n}\n\n/**\n * Title, description and language of a resource's `<metadata>`\n *\n * LOM is matched by local name, so a prefixed binding (`imsmd:title`) reads the same as an unprefixed\n * one, and LangString text comes from its first `string` (or LOM 1.2 `langstring`). When the metadata\n * holds LOM, only `general` is read: a `classification` description is not the resource's.\n */\nfunction parseResourceMetadata(metadataEl: HTMLElement): ManifestResource['metadata'] {\n let title: string | undefined;\n let description: string | undefined;\n let language: string | undefined;\n\n const loms = descendantsNamed(metadataEl, 'lom');\n if (loms.length > 0) {\n const general = loms.flatMap((lom) => childrenNamed(lom, 'general'))[0];\n if (general) {\n title = langStringText(childrenNamed(general, 'title')[0]);\n description = langStringText(childrenNamed(general, 'description')[0]);\n language = textOf(childrenNamed(general, 'language')[0]);\n }\n } else {\n title = langStringText(descendantsNamed(metadataEl, 'title')[0]);\n description = langStringText(descendantsNamed(metadataEl, 'description')[0]);\n const languageEl = descendantsNamed(metadataEl, 'language')[0] ||\n metadataEl.querySelector('[\\\\:language]') ||\n metadataEl.querySelector('[name=\"language\"]');\n language = textOf(languageEl ?? undefined);\n }\n\n return title || description || language ? { title, description, language } : undefined;\n}\n\nfunction langStringText(el: HTMLElement | undefined): string | undefined {\n if (!el) return undefined;\n const string = [...childrenNamed(el, 'string'), ...childrenNamed(el, 'langstring')][0];\n return textOf(string ?? el);\n}\n\nfunction textOf(el: HTMLElement | undefined): string | undefined {\n return el?.textContent?.trim() || undefined;\n}\n\n/** Tag name without its namespace prefix, lowercased. */\nfunction localName(el: HTMLElement): string {\n return (el.rawTagName ?? '').replace(/^[^:]*:/, '').toLowerCase();\n}\n\nfunction childrenNamed(el: HTMLElement, name: string): HTMLElement[] {\n return el.children.filter((child) => localName(child) === name);\n}\n\nfunction descendantsNamed(el: HTMLElement, name: string): HTMLElement[] {\n return el.children.flatMap((child) => [\n ...(localName(child) === name ? [child] : []),\n ...descendantsNamed(child, name),\n ]);\n}\n\n/**\n * Check if resource type is an item\n */\nfunction isItemType(type: string): boolean {\n return type.includes('imsqti_item') ||\n type.includes('qti_item') ||\n type === 'associatedcontent/imsqti_item_xmlv2p1';\n}\n\n/**\n * Check if resource type is a passage/stimulus\n */\nfunction isPassageType(type: string): boolean {\n return type === 'webcontent' ||\n type.includes('passage') ||\n type.includes('stimulus') ||\n type.includes('apip');\n}\n\n/**\n * Check if resource type is a test/assessment\n */\nfunction isTestType(type: string): boolean {\n return type.includes('test') || type.includes('assessment');\n}\n\n/**\n * Build passage registry from manifest\n *\n * Analyzes manifest to identify:\n * - Which passages are referenced by items\n * - Which passages are reusable (multiple items reference them)\n */\nexport function buildPassageRegistryFromManifest(\n manifest: ParsedManifest\n): PassageRegistry {\n const registry = new PassageRegistry();\n\n // Register all passage resources\n for (const passage of manifest.passages) {\n // Find all items that depend on this passage\n for (const item of manifest.items) {\n if (item.dependencies.includes(passage.identifier)) {\n registry.registerReference(item.identifier, {\n id: passage.identifier, // Use QTI identifier directly\n source: 'manifest',\n qtiIdentifier: passage.identifier,\n filePath: passage.href,\n isReusable: false, // Will be updated by registry\n });\n }\n }\n }\n\n return registry;\n}\n\n/**\n * Get passage content file path from manifest\n */\nexport function getPassageFilePath(\n manifest: ParsedManifest,\n passageId: string\n): string | undefined {\n const passage = manifest.resources.get(passageId);\n return passage?.href;\n}\n\n/**\n * Get all items that depend on a passage\n */\nexport function getItemsReferencingPassage(\n manifest: ParsedManifest,\n passageId: string\n): ManifestResource[] {\n return manifest.items.filter(item =>\n item.dependencies.includes(passageId)\n );\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type QtiHeuristicsConfig } from './qti-heuristics.js';
|
|
2
|
+
export type PackageReferenceKind = 'media-asset' | 'stylesheet' | 'catalog-file' | 'source-xml' | 'assessment-item-ref' | 'other-asset';
|
|
3
|
+
export type PackageReferenceResolutionStrategy = 'source-relative' | 'package-root' | 'unique-suffix' | 'unique-basename';
|
|
4
|
+
export type PackageReferenceUnresolvedStatus = 'skipped' | 'unsafe' | 'ambiguous' | 'missing';
|
|
5
|
+
export interface PackageReferenceDiagnostic {
|
|
6
|
+
severity: 'info' | 'warning' | 'error';
|
|
7
|
+
code: string;
|
|
8
|
+
message: string;
|
|
9
|
+
sourcePath?: string;
|
|
10
|
+
reference?: string;
|
|
11
|
+
candidatePaths?: string[];
|
|
12
|
+
}
|
|
13
|
+
export interface PackageFileIndex {
|
|
14
|
+
readonly exactPaths: ReadonlySet<string>;
|
|
15
|
+
readonly pathsBySuffix: ReadonlyMap<string, readonly string[]>;
|
|
16
|
+
readonly pathsByBasename: ReadonlyMap<string, readonly string[]>;
|
|
17
|
+
readonly diagnostics: readonly PackageReferenceDiagnostic[];
|
|
18
|
+
}
|
|
19
|
+
export interface ResolvePackageReferenceInput {
|
|
20
|
+
readonly fileIndex: PackageFileIndex;
|
|
21
|
+
readonly rawHref: string;
|
|
22
|
+
readonly sourcePath?: string;
|
|
23
|
+
readonly referenceKind: PackageReferenceKind;
|
|
24
|
+
readonly heuristicsConfig?: QtiHeuristicsConfig;
|
|
25
|
+
readonly manifestEvidencePaths?: ReadonlySet<string>;
|
|
26
|
+
}
|
|
27
|
+
export interface ResolvedPackageReference {
|
|
28
|
+
readonly status: 'resolved';
|
|
29
|
+
readonly rawHref: string;
|
|
30
|
+
readonly lookupHref: string;
|
|
31
|
+
readonly renderHref: string;
|
|
32
|
+
readonly resolvedPath: string;
|
|
33
|
+
readonly strategy: PackageReferenceResolutionStrategy;
|
|
34
|
+
readonly heuristic: boolean;
|
|
35
|
+
readonly queryAndFragment: string;
|
|
36
|
+
readonly diagnostics: readonly PackageReferenceDiagnostic[];
|
|
37
|
+
}
|
|
38
|
+
export interface UnresolvedPackageReference {
|
|
39
|
+
readonly status: PackageReferenceUnresolvedStatus;
|
|
40
|
+
readonly rawHref: string;
|
|
41
|
+
readonly lookupHref: string;
|
|
42
|
+
readonly queryAndFragment: string;
|
|
43
|
+
readonly diagnostics: readonly PackageReferenceDiagnostic[];
|
|
44
|
+
readonly candidatePaths?: readonly string[];
|
|
45
|
+
}
|
|
46
|
+
export type PackageReferenceResolution = ResolvedPackageReference | UnresolvedPackageReference;
|
|
47
|
+
export declare function buildPackageFileIndex(paths: readonly string[]): PackageFileIndex;
|
|
48
|
+
export declare function resolvePackageReference(input: ResolvePackageReferenceInput): PackageReferenceResolution;
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { DEFAULT_HEURISTICS_CONFIG, normalizeHeuristicsConfig, } from './qti-heuristics.js';
|
|
2
|
+
export function buildPackageFileIndex(paths) {
|
|
3
|
+
const exactPaths = new Set();
|
|
4
|
+
const pathsBySuffix = new Map();
|
|
5
|
+
const pathsByBasename = new Map();
|
|
6
|
+
const diagnostics = [];
|
|
7
|
+
for (const rawPath of paths) {
|
|
8
|
+
const normalized = normalizePackagePath('', rawPath);
|
|
9
|
+
if (hasUnsafeEncodedPathToken(rawPath) || normalized.unsafe || !normalized.path) {
|
|
10
|
+
diagnostics.push({
|
|
11
|
+
severity: 'warning',
|
|
12
|
+
code: 'IMS_CP_FILE_INDEX_UNSAFE_PATH',
|
|
13
|
+
message: `Package file index skipped unsafe path: ${rawPath}.`,
|
|
14
|
+
reference: rawPath,
|
|
15
|
+
});
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (exactPaths.has(normalized.path)) {
|
|
19
|
+
diagnostics.push({
|
|
20
|
+
severity: 'warning',
|
|
21
|
+
code: 'IMS_CP_FILE_INDEX_COLLISION',
|
|
22
|
+
message: `Package file index saw duplicate canonical path: ${normalized.path}.`,
|
|
23
|
+
reference: rawPath,
|
|
24
|
+
candidatePaths: [normalized.path],
|
|
25
|
+
});
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
exactPaths.add(normalized.path);
|
|
29
|
+
addSuffixes(pathsBySuffix, normalized.path);
|
|
30
|
+
const basename = basenameOf(normalized.path);
|
|
31
|
+
if (basename) {
|
|
32
|
+
mapArrayFor(pathsByBasename, lookupKey(basename)).push(normalized.path);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { exactPaths, pathsBySuffix, pathsByBasename, diagnostics };
|
|
36
|
+
}
|
|
37
|
+
export function resolvePackageReference(input) {
|
|
38
|
+
const rawHref = input.rawHref;
|
|
39
|
+
const trimmed = String(rawHref ?? '').trim();
|
|
40
|
+
const split = splitLookupHref(trimmed);
|
|
41
|
+
const lookupHref = split.lookupHref.replaceAll('\\', '/').trim();
|
|
42
|
+
const diagnostics = [];
|
|
43
|
+
if (!lookupHref || lookupHref.startsWith('#') || isExternalHref(lookupHref)) {
|
|
44
|
+
return {
|
|
45
|
+
status: 'skipped',
|
|
46
|
+
rawHref,
|
|
47
|
+
lookupHref,
|
|
48
|
+
queryAndFragment: split.queryAndFragment,
|
|
49
|
+
diagnostics,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (lookupHref.startsWith('/') || hasUnsafeEncodedPathToken(lookupHref)) {
|
|
53
|
+
return unsafeResolution(rawHref, lookupHref, split.queryAndFragment, input.sourcePath);
|
|
54
|
+
}
|
|
55
|
+
const sourceBase = input.sourcePath ? dirname(input.sourcePath) : '';
|
|
56
|
+
const sourceCandidate = normalizePackagePath(sourceBase, lookupHref);
|
|
57
|
+
if (sourceCandidate.unsafe) {
|
|
58
|
+
return unsafeResolution(rawHref, lookupHref, split.queryAndFragment, input.sourcePath);
|
|
59
|
+
}
|
|
60
|
+
const sourceMatch = exactMatch(input.fileIndex, sourceCandidate.path);
|
|
61
|
+
if (sourceMatch) {
|
|
62
|
+
return resolved(rawHref, lookupHref, split.queryAndFragment, sourceMatch, 'source-relative', false, diagnostics);
|
|
63
|
+
}
|
|
64
|
+
const rootCandidate = normalizePackagePath('', lookupHref);
|
|
65
|
+
if (!rootCandidate.unsafe) {
|
|
66
|
+
const rootMatch = exactMatch(input.fileIndex, rootCandidate.path);
|
|
67
|
+
if (rootMatch) {
|
|
68
|
+
return resolved(rawHref, lookupHref, split.queryAndFragment, rootMatch, 'package-root', false, diagnostics);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const heuristics = normalizeHeuristicsConfig(input.heuristicsConfig ?? DEFAULT_HEURISTICS_CONFIG);
|
|
72
|
+
if (!heuristics.enabled || !heuristics.lenientPackageResourcePaths) {
|
|
73
|
+
return missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, [
|
|
74
|
+
{
|
|
75
|
+
severity: 'info',
|
|
76
|
+
code: 'IMS_CP_REFERENCE_HEURISTIC_DISABLED',
|
|
77
|
+
message: `Heuristic package path resolution is disabled for ${lookupHref}.`,
|
|
78
|
+
sourcePath: input.sourcePath,
|
|
79
|
+
reference: rawHref,
|
|
80
|
+
},
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
const suffixKeys = suffixLookupKeys(sourceCandidate.path, rootCandidate.path, lookupHref);
|
|
84
|
+
let ambiguousSuffixMatches = [];
|
|
85
|
+
let suffixKey = suffixKeys[0] ?? '';
|
|
86
|
+
let eligibleSuffixMatches = [];
|
|
87
|
+
for (const key of suffixKeys) {
|
|
88
|
+
if (!canUseSingleSegmentSuffix(input.referenceKind) && !key.includes('/')) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const suffixMatches = uniqueSorted(input.fileIndex.pathsBySuffix.get(lookupKey(key)) ?? []);
|
|
92
|
+
const eligibleMatches = filterManifestEvidence(suffixMatches, input);
|
|
93
|
+
if (eligibleMatches.length === 1) {
|
|
94
|
+
suffixKey = key;
|
|
95
|
+
eligibleSuffixMatches = eligibleMatches;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (eligibleMatches.length > 1 && ambiguousSuffixMatches.length === 0) {
|
|
99
|
+
suffixKey = key;
|
|
100
|
+
ambiguousSuffixMatches = eligibleMatches;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (eligibleSuffixMatches.length === 1) {
|
|
104
|
+
return resolved(rawHref, lookupHref, split.queryAndFragment, eligibleSuffixMatches[0], 'unique-suffix', true, diagnostics);
|
|
105
|
+
}
|
|
106
|
+
if (ambiguousSuffixMatches.length > 1) {
|
|
107
|
+
return ambiguous(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, ambiguousSuffixMatches);
|
|
108
|
+
}
|
|
109
|
+
if (!canUseBasenameHeuristic(input.referenceKind, heuristics)) {
|
|
110
|
+
return missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, diagnostics);
|
|
111
|
+
}
|
|
112
|
+
const basename = basenameOf(suffixKey);
|
|
113
|
+
const basenameMatches = uniqueSorted(input.fileIndex.pathsByBasename.get(lookupKey(basename)) ?? []);
|
|
114
|
+
if (basenameMatches.length === 1) {
|
|
115
|
+
return resolved(rawHref, lookupHref, split.queryAndFragment, basenameMatches[0], 'unique-basename', true, diagnostics);
|
|
116
|
+
}
|
|
117
|
+
if (basenameMatches.length > 1) {
|
|
118
|
+
return ambiguous(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, basenameMatches);
|
|
119
|
+
}
|
|
120
|
+
return missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, diagnostics);
|
|
121
|
+
}
|
|
122
|
+
function exactMatch(fileIndex, path) {
|
|
123
|
+
return path && fileIndex.exactPaths.has(path) ? path : null;
|
|
124
|
+
}
|
|
125
|
+
function resolved(rawHref, lookupHref, queryAndFragment, resolvedPath, strategy, heuristic, diagnostics) {
|
|
126
|
+
return {
|
|
127
|
+
status: 'resolved',
|
|
128
|
+
rawHref,
|
|
129
|
+
lookupHref,
|
|
130
|
+
renderHref: `${resolvedPath}${queryAndFragment}`,
|
|
131
|
+
resolvedPath,
|
|
132
|
+
strategy,
|
|
133
|
+
heuristic,
|
|
134
|
+
queryAndFragment,
|
|
135
|
+
diagnostics,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function missing(rawHref, lookupHref, queryAndFragment, sourcePath, diagnostics) {
|
|
139
|
+
return {
|
|
140
|
+
status: 'missing',
|
|
141
|
+
rawHref,
|
|
142
|
+
lookupHref,
|
|
143
|
+
queryAndFragment,
|
|
144
|
+
diagnostics: diagnostics.length > 0
|
|
145
|
+
? diagnostics
|
|
146
|
+
: [
|
|
147
|
+
{
|
|
148
|
+
severity: 'warning',
|
|
149
|
+
code: 'IMS_CP_REFERENCE_MISSING',
|
|
150
|
+
message: `Package reference could not be resolved: ${lookupHref}.`,
|
|
151
|
+
sourcePath,
|
|
152
|
+
reference: rawHref,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function ambiguous(rawHref, lookupHref, queryAndFragment, sourcePath, candidatePaths) {
|
|
158
|
+
return {
|
|
159
|
+
status: 'ambiguous',
|
|
160
|
+
rawHref,
|
|
161
|
+
lookupHref,
|
|
162
|
+
queryAndFragment,
|
|
163
|
+
candidatePaths,
|
|
164
|
+
diagnostics: [
|
|
165
|
+
{
|
|
166
|
+
severity: 'warning',
|
|
167
|
+
code: 'IMS_CP_REFERENCE_AMBIGUOUS',
|
|
168
|
+
message: `Package reference is ambiguous: ${lookupHref} matched ${candidatePaths.length} files.`,
|
|
169
|
+
sourcePath,
|
|
170
|
+
reference: rawHref,
|
|
171
|
+
candidatePaths: [...candidatePaths],
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function unsafeResolution(rawHref, lookupHref, queryAndFragment, sourcePath) {
|
|
177
|
+
return {
|
|
178
|
+
status: 'unsafe',
|
|
179
|
+
rawHref,
|
|
180
|
+
lookupHref,
|
|
181
|
+
queryAndFragment,
|
|
182
|
+
diagnostics: [
|
|
183
|
+
{
|
|
184
|
+
severity: 'error',
|
|
185
|
+
code: 'IMS_CP_REFERENCE_UNSAFE',
|
|
186
|
+
message: `Package reference escapes the package root or uses an absolute package path: ${lookupHref}.`,
|
|
187
|
+
sourcePath,
|
|
188
|
+
reference: rawHref,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function splitLookupHref(rawHref) {
|
|
194
|
+
const queryIndex = rawHref.indexOf('?');
|
|
195
|
+
const fragmentIndex = rawHref.indexOf('#');
|
|
196
|
+
const indexes = [queryIndex, fragmentIndex].filter((index) => index >= 0);
|
|
197
|
+
if (indexes.length === 0) {
|
|
198
|
+
return { lookupHref: rawHref, queryAndFragment: '' };
|
|
199
|
+
}
|
|
200
|
+
const splitIndex = Math.min(...indexes);
|
|
201
|
+
return {
|
|
202
|
+
lookupHref: rawHref.slice(0, splitIndex),
|
|
203
|
+
queryAndFragment: rawHref.slice(splitIndex),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function isExternalHref(href) {
|
|
207
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(href);
|
|
208
|
+
}
|
|
209
|
+
function hasUnsafeEncodedPathToken(href) {
|
|
210
|
+
let current = href;
|
|
211
|
+
for (let i = 0; i < 3; i += 1) {
|
|
212
|
+
if (/%(?:2e|2f|5c)/i.test(current)) {
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
const decoded = safeDecodeURIComponent(current);
|
|
216
|
+
if (decoded === current) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
current = decoded;
|
|
220
|
+
}
|
|
221
|
+
return /%(?:2e|2f|5c)/i.test(current);
|
|
222
|
+
}
|
|
223
|
+
function safeDecodeURIComponent(value) {
|
|
224
|
+
try {
|
|
225
|
+
return decodeURIComponent(value);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function normalizePackagePath(basePath, href) {
|
|
232
|
+
const raw = href.replaceAll('\\', '/').trim();
|
|
233
|
+
if (!raw || raw.startsWith('/')) {
|
|
234
|
+
return { path: '', unsafe: raw.startsWith('/') };
|
|
235
|
+
}
|
|
236
|
+
const parts = [...(basePath ? basePath.replaceAll('\\', '/').split('/') : []), ...raw.split('/')];
|
|
237
|
+
const normalized = [];
|
|
238
|
+
for (const part of parts) {
|
|
239
|
+
if (!part || part === '.')
|
|
240
|
+
continue;
|
|
241
|
+
if (part === '..') {
|
|
242
|
+
if (normalized.length === 0) {
|
|
243
|
+
return { path: '', unsafe: true };
|
|
244
|
+
}
|
|
245
|
+
normalized.pop();
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
normalized.push(part);
|
|
249
|
+
}
|
|
250
|
+
return { path: normalized.join('/'), unsafe: false };
|
|
251
|
+
}
|
|
252
|
+
function suffixLookupKeys(sourceCandidate, rootCandidate, lookupHref) {
|
|
253
|
+
return uniqueStrings([
|
|
254
|
+
rootCandidate,
|
|
255
|
+
normalizePackagePath('', lookupHref).path,
|
|
256
|
+
sourceCandidate,
|
|
257
|
+
lookupHref.replaceAll('\\', '/').replace(/^\/+/, ''),
|
|
258
|
+
].filter(Boolean));
|
|
259
|
+
}
|
|
260
|
+
function canUseBasenameHeuristic(referenceKind, heuristics) {
|
|
261
|
+
if (!heuristics.lenientAssetBasenames)
|
|
262
|
+
return false;
|
|
263
|
+
return referenceKind === 'media-asset';
|
|
264
|
+
}
|
|
265
|
+
function canUseSingleSegmentSuffix(referenceKind) {
|
|
266
|
+
return referenceKind === 'media-asset';
|
|
267
|
+
}
|
|
268
|
+
function filterManifestEvidence(matches, input) {
|
|
269
|
+
if (input.referenceKind !== 'source-xml' && input.referenceKind !== 'assessment-item-ref') {
|
|
270
|
+
return matches;
|
|
271
|
+
}
|
|
272
|
+
const evidence = input.manifestEvidencePaths;
|
|
273
|
+
if (!evidence)
|
|
274
|
+
return [];
|
|
275
|
+
return matches.filter((match) => evidence.has(match));
|
|
276
|
+
}
|
|
277
|
+
function addSuffixes(index, path) {
|
|
278
|
+
const parts = path.split('/');
|
|
279
|
+
for (let partIndex = 0; partIndex < parts.length; partIndex += 1) {
|
|
280
|
+
const suffix = parts.slice(partIndex).join('/');
|
|
281
|
+
if (suffix) {
|
|
282
|
+
mapArrayFor(index, lookupKey(suffix)).push(path);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function lookupKey(value) {
|
|
287
|
+
return value.toLowerCase();
|
|
288
|
+
}
|
|
289
|
+
function mapArrayFor(map, key) {
|
|
290
|
+
const existing = map.get(key);
|
|
291
|
+
if (existing)
|
|
292
|
+
return existing;
|
|
293
|
+
const next = [];
|
|
294
|
+
map.set(key, next);
|
|
295
|
+
return next;
|
|
296
|
+
}
|
|
297
|
+
function basenameOf(path) {
|
|
298
|
+
const index = path.lastIndexOf('/');
|
|
299
|
+
return index === -1 ? path : path.slice(index + 1);
|
|
300
|
+
}
|
|
301
|
+
function dirname(path) {
|
|
302
|
+
const normalized = path.replaceAll('\\', '/');
|
|
303
|
+
const index = normalized.lastIndexOf('/');
|
|
304
|
+
return index === -1 ? '' : normalized.slice(0, index);
|
|
305
|
+
}
|
|
306
|
+
function uniqueSorted(values) {
|
|
307
|
+
return [...new Set(values)].sort((left, right) => left.localeCompare(right));
|
|
308
|
+
}
|
|
309
|
+
function uniqueStrings(values) {
|
|
310
|
+
return [...new Set(values)];
|
|
311
|
+
}
|
|
312
|
+
//# sourceMappingURL=package-file-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package-file-resolver.js","sourceRoot":"","sources":["../src/package-file-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,EACzB,yBAAyB,GAEzB,MAAM,qBAAqB,CAAC;AAuE7B,MAAM,UAAU,qBAAqB,CAAC,KAAwB;IAC7D,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;IACrC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAoB,CAAC;IAClD,MAAM,eAAe,GAAG,IAAI,GAAG,EAAoB,CAAC;IACpD,MAAM,WAAW,GAAiC,EAAE,CAAC;IAErD,KAAK,MAAM,OAAO,IAAI,KAAK,EAAE,CAAC;QAC7B,MAAM,UAAU,GAAG,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QACrD,IAAI,yBAAyB,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;YACjF,WAAW,CAAC,IAAI,CAAC;gBAChB,QAAQ,EAAE,SAAS;gBACnB,IAAI,EAAE,+BAA+B;gBACrC,OAAO,EAAE,2CAA2C,OAAO,GAAG;gBAC9D,SAAS,EAAE,OAAO;aAClB,CAAC,CAAC;YACH,SAAS;QACV,CAAC;QACD,IAAI,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACrC,WAAW,CAAC,IAAI,CAAC;gBAChB,QAAQ,EAAE,SAAS;gBACnB,IAA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QAAQ,CAAC;IAC9B,MAAM,IAAI,GAAQ,EAAE,CAAC;IACrB,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACnB,OAAO,IAAI,CAAC;AACb,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IACpC,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,SAAS,OAAO,CAAC,IAAY;IAC5B,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9C,MAAM,KAAK,GAAG,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1C,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,YAAY,CAAC,MAAyB;IAC9C,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,aAAa,CAAC,MAAyB;IAC/C,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["import {\n\tDEFAULT_HEURISTICS_CONFIG,\n\tnormalizeHeuristicsConfig,\n\ttype QtiHeuristicsConfig,\n} from './qti-heuristics.js';\n\nexport type PackageReferenceKind =\n\t| 'media-asset'\n\t| 'stylesheet'\n\t| 'catalog-file'\n\t| 'source-xml'\n\t| 'assessment-item-ref'\n\t| 'other-asset';\n\nexport type PackageReferenceResolutionStrategy =\n\t| 'source-relative'\n\t| 'package-root'\n\t| 'unique-suffix'\n\t| 'unique-basename';\n\nexport type PackageReferenceUnresolvedStatus = 'skipped' | 'unsafe' | 'ambiguous' | 'missing';\n\nexport interface PackageReferenceDiagnostic {\n\tseverity: 'info' | 'warning' | 'error';\n\tcode: string;\n\tmessage: string;\n\tsourcePath?: string;\n\treference?: string;\n\tcandidatePaths?: string[];\n}\n\nexport interface PackageFileIndex {\n\treadonly exactPaths: ReadonlySet<string>;\n\treadonly pathsBySuffix: ReadonlyMap<string, readonly string[]>;\n\treadonly pathsByBasename: ReadonlyMap<string, readonly string[]>;\n\treadonly diagnostics: readonly PackageReferenceDiagnostic[];\n}\n\nexport interface ResolvePackageReferenceInput {\n\treadonly fileIndex: PackageFileIndex;\n\treadonly rawHref: string;\n\treadonly sourcePath?: string;\n\treadonly referenceKind: PackageReferenceKind;\n\treadonly heuristicsConfig?: QtiHeuristicsConfig;\n\treadonly manifestEvidencePaths?: ReadonlySet<string>;\n}\n\nexport interface ResolvedPackageReference {\n\treadonly status: 'resolved';\n\treadonly rawHref: string;\n\treadonly lookupHref: string;\n\treadonly renderHref: string;\n\treadonly resolvedPath: string;\n\treadonly strategy: PackageReferenceResolutionStrategy;\n\treadonly heuristic: boolean;\n\treadonly queryAndFragment: string;\n\treadonly diagnostics: readonly PackageReferenceDiagnostic[];\n}\n\nexport interface UnresolvedPackageReference {\n\treadonly status: PackageReferenceUnresolvedStatus;\n\treadonly rawHref: string;\n\treadonly lookupHref: string;\n\treadonly queryAndFragment: string;\n\treadonly diagnostics: readonly PackageReferenceDiagnostic[];\n\treadonly candidatePaths?: readonly string[];\n}\n\nexport type PackageReferenceResolution = ResolvedPackageReference | UnresolvedPackageReference;\n\ninterface NormalizedPathResult {\n\tpath: string;\n\tunsafe: boolean;\n}\n\nexport function buildPackageFileIndex(paths: readonly string[]): PackageFileIndex {\n\tconst exactPaths = new Set<string>();\n\tconst pathsBySuffix = new Map<string, string[]>();\n\tconst pathsByBasename = new Map<string, string[]>();\n\tconst diagnostics: PackageReferenceDiagnostic[] = [];\n\n\tfor (const rawPath of paths) {\n\t\tconst normalized = normalizePackagePath('', rawPath);\n\t\tif (hasUnsafeEncodedPathToken(rawPath) || normalized.unsafe || !normalized.path) {\n\t\t\tdiagnostics.push({\n\t\t\t\tseverity: 'warning',\n\t\t\t\tcode: 'IMS_CP_FILE_INDEX_UNSAFE_PATH',\n\t\t\t\tmessage: `Package file index skipped unsafe path: ${rawPath}.`,\n\t\t\t\treference: rawPath,\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\t\tif (exactPaths.has(normalized.path)) {\n\t\t\tdiagnostics.push({\n\t\t\t\tseverity: 'warning',\n\t\t\t\tcode: 'IMS_CP_FILE_INDEX_COLLISION',\n\t\t\t\tmessage: `Package file index saw duplicate canonical path: ${normalized.path}.`,\n\t\t\t\treference: rawPath,\n\t\t\t\tcandidatePaths: [normalized.path],\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\t\texactPaths.add(normalized.path);\n\t\taddSuffixes(pathsBySuffix, normalized.path);\n\t\tconst basename = basenameOf(normalized.path);\n\t\tif (basename) {\n\t\t\tmapArrayFor(pathsByBasename, lookupKey(basename)).push(normalized.path);\n\t\t}\n\t}\n\n\treturn { exactPaths, pathsBySuffix, pathsByBasename, diagnostics };\n}\n\nexport function resolvePackageReference(input: ResolvePackageReferenceInput): PackageReferenceResolution {\n\tconst rawHref = input.rawHref;\n\tconst trimmed = String(rawHref ?? '').trim();\n\tconst split = splitLookupHref(trimmed);\n\tconst lookupHref = split.lookupHref.replaceAll('\\\\', '/').trim();\n\tconst diagnostics: PackageReferenceDiagnostic[] = [];\n\n\tif (!lookupHref || lookupHref.startsWith('#') || isExternalHref(lookupHref)) {\n\t\treturn {\n\t\t\tstatus: 'skipped',\n\t\t\trawHref,\n\t\t\tlookupHref,\n\t\t\tqueryAndFragment: split.queryAndFragment,\n\t\t\tdiagnostics,\n\t\t};\n\t}\n\tif (lookupHref.startsWith('/') || hasUnsafeEncodedPathToken(lookupHref)) {\n\t\treturn unsafeResolution(rawHref, lookupHref, split.queryAndFragment, input.sourcePath);\n\t}\n\n\tconst sourceBase = input.sourcePath ? dirname(input.sourcePath) : '';\n\tconst sourceCandidate = normalizePackagePath(sourceBase, lookupHref);\n\tif (sourceCandidate.unsafe) {\n\t\treturn unsafeResolution(rawHref, lookupHref, split.queryAndFragment, input.sourcePath);\n\t}\n\tconst sourceMatch = exactMatch(input.fileIndex, sourceCandidate.path);\n\tif (sourceMatch) {\n\t\treturn resolved(rawHref, lookupHref, split.queryAndFragment, sourceMatch, 'source-relative', false, diagnostics);\n\t}\n\n\tconst rootCandidate = normalizePackagePath('', lookupHref);\n\tif (!rootCandidate.unsafe) {\n\t\tconst rootMatch = exactMatch(input.fileIndex, rootCandidate.path);\n\t\tif (rootMatch) {\n\t\t\treturn resolved(rawHref, lookupHref, split.queryAndFragment, rootMatch, 'package-root', false, diagnostics);\n\t\t}\n\t}\n\n\tconst heuristics = normalizeHeuristicsConfig(input.heuristicsConfig ?? DEFAULT_HEURISTICS_CONFIG);\n\tif (!heuristics.enabled || !heuristics.lenientPackageResourcePaths) {\n\t\treturn missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, [\n\t\t\t{\n\t\t\t\tseverity: 'info',\n\t\t\t\tcode: 'IMS_CP_REFERENCE_HEURISTIC_DISABLED',\n\t\t\t\tmessage: `Heuristic package path resolution is disabled for ${lookupHref}.`,\n\t\t\t\tsourcePath: input.sourcePath,\n\t\t\t\treference: rawHref,\n\t\t\t},\n\t\t]);\n\t}\n\n\tconst suffixKeys = suffixLookupKeys(sourceCandidate.path, rootCandidate.path, lookupHref);\n\tlet ambiguousSuffixMatches: readonly string[] = [];\n\tlet suffixKey = suffixKeys[0] ?? '';\n\tlet eligibleSuffixMatches: readonly string[] = [];\n\tfor (const key of suffixKeys) {\n\t\tif (!canUseSingleSegmentSuffix(input.referenceKind) && !key.includes('/')) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst suffixMatches = uniqueSorted(input.fileIndex.pathsBySuffix.get(lookupKey(key)) ?? []);\n\t\tconst eligibleMatches = filterManifestEvidence(suffixMatches, input);\n\t\tif (eligibleMatches.length === 1) {\n\t\t\tsuffixKey = key;\n\t\t\teligibleSuffixMatches = eligibleMatches;\n\t\t\tbreak;\n\t\t}\n\t\tif (eligibleMatches.length > 1 && ambiguousSuffixMatches.length === 0) {\n\t\t\tsuffixKey = key;\n\t\t\tambiguousSuffixMatches = eligibleMatches;\n\t\t}\n\t}\n\tif (eligibleSuffixMatches.length === 1) {\n\t\treturn resolved(\n\t\t\trawHref,\n\t\t\tlookupHref,\n\t\t\tsplit.queryAndFragment,\n\t\t\teligibleSuffixMatches[0],\n\t\t\t'unique-suffix',\n\t\t\ttrue,\n\t\t\tdiagnostics\n\t\t);\n\t}\n\tif (ambiguousSuffixMatches.length > 1) {\n\t\treturn ambiguous(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, ambiguousSuffixMatches);\n\t}\n\n\tif (!canUseBasenameHeuristic(input.referenceKind, heuristics)) {\n\t\treturn missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, diagnostics);\n\t}\n\tconst basename = basenameOf(suffixKey);\n\tconst basenameMatches = uniqueSorted(input.fileIndex.pathsByBasename.get(lookupKey(basename)) ?? []);\n\tif (basenameMatches.length === 1) {\n\t\treturn resolved(\n\t\t\trawHref,\n\t\t\tlookupHref,\n\t\t\tsplit.queryAndFragment,\n\t\t\tbasenameMatches[0],\n\t\t\t'unique-basename',\n\t\t\ttrue,\n\t\t\tdiagnostics\n\t\t);\n\t}\n\tif (basenameMatches.length > 1) {\n\t\treturn ambiguous(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, basenameMatches);\n\t}\n\n\treturn missing(rawHref, lookupHref, split.queryAndFragment, input.sourcePath, diagnostics);\n}\n\nfunction exactMatch(fileIndex: PackageFileIndex, path: string): string | null {\n\treturn path && fileIndex.exactPaths.has(path) ? path : null;\n}\n\nfunction resolved(\n\trawHref: string,\n\tlookupHref: string,\n\tqueryAndFragment: string,\n\tresolvedPath: string,\n\tstrategy: PackageReferenceResolutionStrategy,\n\theuristic: boolean,\n\tdiagnostics: readonly PackageReferenceDiagnostic[]\n): ResolvedPackageReference {\n\treturn {\n\t\tstatus: 'resolved',\n\t\trawHref,\n\t\tlookupHref,\n\t\trenderHref: `${resolvedPath}${queryAndFragment}`,\n\t\tresolvedPath,\n\t\tstrategy,\n\t\theuristic,\n\t\tqueryAndFragment,\n\t\tdiagnostics,\n\t};\n}\n\nfunction missing(\n\trawHref: string,\n\tlookupHref: string,\n\tqueryAndFragment: string,\n\tsourcePath: string | undefined,\n\tdiagnostics: readonly PackageReferenceDiagnostic[]\n): UnresolvedPackageReference {\n\treturn {\n\t\tstatus: 'missing',\n\t\trawHref,\n\t\tlookupHref,\n\t\tqueryAndFragment,\n\t\tdiagnostics: diagnostics.length > 0\n\t\t\t? diagnostics\n\t\t\t: [\n\t\t\t\t\t{\n\t\t\t\t\t\tseverity: 'warning',\n\t\t\t\t\t\tcode: 'IMS_CP_REFERENCE_MISSING',\n\t\t\t\t\t\tmessage: `Package reference could not be resolved: ${lookupHref}.`,\n\t\t\t\t\t\tsourcePath,\n\t\t\t\t\t\treference: rawHref,\n\t\t\t\t\t},\n\t\t\t\t],\n\t};\n}\n\nfunction ambiguous(\n\trawHref: string,\n\tlookupHref: string,\n\tqueryAndFragment: string,\n\tsourcePath: string | undefined,\n\tcandidatePaths: readonly string[]\n): UnresolvedPackageReference {\n\treturn {\n\t\tstatus: 'ambiguous',\n\t\trawHref,\n\t\tlookupHref,\n\t\tqueryAndFragment,\n\t\tcandidatePaths,\n\t\tdiagnostics: [\n\t\t\t{\n\t\t\t\tseverity: 'warning',\n\t\t\t\tcode: 'IMS_CP_REFERENCE_AMBIGUOUS',\n\t\t\t\tmessage: `Package reference is ambiguous: ${lookupHref} matched ${candidatePaths.length} files.`,\n\t\t\t\tsourcePath,\n\t\t\t\treference: rawHref,\n\t\t\t\tcandidatePaths: [...candidatePaths],\n\t\t\t},\n\t\t],\n\t};\n}\n\nfunction unsafeResolution(\n\trawHref: string,\n\tlookupHref: string,\n\tqueryAndFragment: string,\n\tsourcePath: string | undefined\n): UnresolvedPackageReference {\n\treturn {\n\t\tstatus: 'unsafe',\n\t\trawHref,\n\t\tlookupHref,\n\t\tqueryAndFragment,\n\t\tdiagnostics: [\n\t\t\t{\n\t\t\t\tseverity: 'error',\n\t\t\t\tcode: 'IMS_CP_REFERENCE_UNSAFE',\n\t\t\t\tmessage: `Package reference escapes the package root or uses an absolute package path: ${lookupHref}.`,\n\t\t\t\tsourcePath,\n\t\t\t\treference: rawHref,\n\t\t\t},\n\t\t],\n\t};\n}\n\nfunction splitLookupHref(rawHref: string): { lookupHref: string; queryAndFragment: string } {\n\tconst queryIndex = rawHref.indexOf('?');\n\tconst fragmentIndex = rawHref.indexOf('#');\n\tconst indexes = [queryIndex, fragmentIndex].filter((index) => index >= 0);\n\tif (indexes.length === 0) {\n\t\treturn { lookupHref: rawHref, queryAndFragment: '' };\n\t}\n\tconst splitIndex = Math.min(...indexes);\n\treturn {\n\t\tlookupHref: rawHref.slice(0, splitIndex),\n\t\tqueryAndFragment: rawHref.slice(splitIndex),\n\t};\n}\n\nfunction isExternalHref(href: string): boolean {\n\treturn /^(?:[a-z][a-z0-9+.-]*:|\\/\\/)/i.test(href);\n}\n\nfunction hasUnsafeEncodedPathToken(href: string): boolean {\n\tlet current = href;\n\tfor (let i = 0; i < 3; i += 1) {\n\t\tif (/%(?:2e|2f|5c)/i.test(current)) {\n\t\t\treturn true;\n\t\t}\n\t\tconst decoded = safeDecodeURIComponent(current);\n\t\tif (decoded === current) {\n\t\t\treturn false;\n\t\t}\n\t\tcurrent = decoded;\n\t}\n\treturn /%(?:2e|2f|5c)/i.test(current);\n}\n\nfunction safeDecodeURIComponent(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n\nfunction normalizePackagePath(basePath: string | undefined, href: string): NormalizedPathResult {\n\tconst raw = href.replaceAll('\\\\', '/').trim();\n\tif (!raw || raw.startsWith('/')) {\n\t\treturn { path: '', unsafe: raw.startsWith('/') };\n\t}\n\tconst parts = [...(basePath ? basePath.replaceAll('\\\\', '/').split('/') : []), ...raw.split('/')];\n\tconst normalized: string[] = [];\n\tfor (const part of parts) {\n\t\tif (!part || part === '.') continue;\n\t\tif (part === '..') {\n\t\t\tif (normalized.length === 0) {\n\t\t\t\treturn { path: '', unsafe: true };\n\t\t\t}\n\t\t\tnormalized.pop();\n\t\t\tcontinue;\n\t\t}\n\t\tnormalized.push(part);\n\t}\n\treturn { path: normalized.join('/'), unsafe: false };\n}\n\nfunction suffixLookupKeys(sourceCandidate: string, rootCandidate: string, lookupHref: string): string[] {\n\treturn uniqueStrings([\n\t\trootCandidate,\n\t\tnormalizePackagePath('', lookupHref).path,\n\t\tsourceCandidate,\n\t\tlookupHref.replaceAll('\\\\', '/').replace(/^\\/+/, ''),\n\t].filter(Boolean));\n}\n\nfunction canUseBasenameHeuristic(\n\treferenceKind: PackageReferenceKind,\n\theuristics: ReturnType<typeof normalizeHeuristicsConfig>\n): boolean {\n\tif (!heuristics.lenientAssetBasenames) return false;\n\treturn referenceKind === 'media-asset';\n}\n\nfunction canUseSingleSegmentSuffix(referenceKind: PackageReferenceKind): boolean {\n\treturn referenceKind === 'media-asset';\n}\n\nfunction filterManifestEvidence(\n\tmatches: readonly string[],\n\tinput: ResolvePackageReferenceInput\n): readonly string[] {\n\tif (input.referenceKind !== 'source-xml' && input.referenceKind !== 'assessment-item-ref') {\n\t\treturn matches;\n\t}\n\tconst evidence = input.manifestEvidencePaths;\n\tif (!evidence) return [];\n\treturn matches.filter((match) => evidence.has(match));\n}\n\nfunction addSuffixes(index: Map<string, string[]>, path: string): void {\n\tconst parts = path.split('/');\n\tfor (let partIndex = 0; partIndex < parts.length; partIndex += 1) {\n\t\tconst suffix = parts.slice(partIndex).join('/');\n\t\tif (suffix) {\n\t\t\tmapArrayFor(index, lookupKey(suffix)).push(path);\n\t\t}\n\t}\n}\n\nfunction lookupKey(value: string): string {\n\treturn value.toLowerCase();\n}\n\nfunction mapArrayFor<K, V>(map: Map<K, V[]>, key: K): V[] {\n\tconst existing = map.get(key);\n\tif (existing) return existing;\n\tconst next: V[] = [];\n\tmap.set(key, next);\n\treturn next;\n}\n\nfunction basenameOf(path: string): string {\n\tconst index = path.lastIndexOf('/');\n\treturn index === -1 ? path : path.slice(index + 1);\n}\n\nfunction dirname(path: string): string {\n\tconst normalized = path.replaceAll('\\\\', '/');\n\tconst index = normalized.lastIndexOf('/');\n\treturn index === -1 ? '' : normalized.slice(0, index);\n}\n\nfunction uniqueSorted(values: readonly string[]): string[] {\n\treturn [...new Set(values)].sort((left, right) => left.localeCompare(right));\n}\n\nfunction uniqueStrings(values: readonly string[]): string[] {\n\treturn [...new Set(values)];\n}\n"]}
|