@pie-qti/ims-cp-core 0.1.6 → 0.1.8
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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/package-graph.d.ts +126 -0
- package/dist/package-graph.d.ts.map +1 -0
- package/dist/package-graph.js +651 -0
- package/dist/package-graph.js.map +1 -0
- package/dist/qti3-shared-content.d.ts +75 -0
- package/dist/qti3-shared-content.d.ts.map +1 -0
- package/dist/qti3-shared-content.js +369 -0
- package/dist/qti3-shared-content.js.map +1 -0
- package/package.json +10 -2
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import { parseManifest } from './manifest-parser.js';
|
|
2
|
+
const ASSET_EXTENSIONS = {
|
|
3
|
+
'.apng': 'image',
|
|
4
|
+
'.avif': 'image',
|
|
5
|
+
'.gif': 'image',
|
|
6
|
+
'.jpg': 'image',
|
|
7
|
+
'.jpeg': 'image',
|
|
8
|
+
'.png': 'image',
|
|
9
|
+
'.svg': 'image',
|
|
10
|
+
'.webp': 'image',
|
|
11
|
+
'.css': 'stylesheet',
|
|
12
|
+
'.mp3': 'audio',
|
|
13
|
+
'.m4a': 'audio',
|
|
14
|
+
'.ogg': 'audio',
|
|
15
|
+
'.wav': 'audio',
|
|
16
|
+
'.mp4': 'video',
|
|
17
|
+
'.mov': 'video',
|
|
18
|
+
'.webm': 'video',
|
|
19
|
+
'.html': 'html',
|
|
20
|
+
'.htm': 'html',
|
|
21
|
+
'.xml': 'source-qti',
|
|
22
|
+
};
|
|
23
|
+
export async function analyzeContentPackage({ packageId, manifestXml, basePath, fileAccess, }) {
|
|
24
|
+
const manifest = parseManifest(manifestXml, basePath);
|
|
25
|
+
const diagnostics = [];
|
|
26
|
+
const resources = new Map();
|
|
27
|
+
const references = new Map();
|
|
28
|
+
const assets = new Map();
|
|
29
|
+
const files = fileAccess?.listFiles ? await fileAccess.listFiles() : [];
|
|
30
|
+
const listedFiles = fileAccess?.listFiles ? new Set(files) : undefined;
|
|
31
|
+
const versionSignals = [
|
|
32
|
+
...detectManifestVersionSignals(manifestXml),
|
|
33
|
+
...detectResourceTypeVersionSignals(manifest.resources.values()),
|
|
34
|
+
];
|
|
35
|
+
for (const resource of manifest.resources.values()) {
|
|
36
|
+
const node = toResourceNode(resource, manifest);
|
|
37
|
+
resources.set(node.identifier, node);
|
|
38
|
+
for (const filePath of node.resolvedFiles) {
|
|
39
|
+
maybeRegisterAsset(assets, filePath, {
|
|
40
|
+
ownerResourceId: node.identifier,
|
|
41
|
+
sourcePath: node.resolvedHref,
|
|
42
|
+
});
|
|
43
|
+
if (fileAccess && !(await packagePathExists(filePath, fileAccess, listedFiles))) {
|
|
44
|
+
diagnostics.push({
|
|
45
|
+
severity: 'warning',
|
|
46
|
+
code: 'IMS_CP_MISSING_FILE',
|
|
47
|
+
message: `Resource ${node.identifier} declares missing package file ${filePath}.`,
|
|
48
|
+
resourceId: node.identifier,
|
|
49
|
+
sourcePath: node.resolvedHref,
|
|
50
|
+
reference: filePath,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const node of resources.values()) {
|
|
56
|
+
const resourceReferences = [];
|
|
57
|
+
for (const dependency of node.dependencies) {
|
|
58
|
+
if (resources.has(dependency)) {
|
|
59
|
+
resourceReferences.push({
|
|
60
|
+
kind: 'manifest-dependency',
|
|
61
|
+
rawHref: dependency,
|
|
62
|
+
targetResourceId: dependency,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
diagnostics.push({
|
|
67
|
+
severity: 'warning',
|
|
68
|
+
code: 'IMS_CP_DANGLING_DEPENDENCY',
|
|
69
|
+
message: `Resource ${node.identifier} depends on missing resource ${dependency}.`,
|
|
70
|
+
resourceId: node.identifier,
|
|
71
|
+
reference: dependency,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (fileAccess && node.resolvedHref && isXmlLike(node.resolvedHref)) {
|
|
76
|
+
const xml = await fileAccess.readText(node.resolvedHref);
|
|
77
|
+
if (xml) {
|
|
78
|
+
versionSignals.push(...detectQtiXmlVersionSignals(xml, node));
|
|
79
|
+
const discovered = discoverReferences(xml, node);
|
|
80
|
+
for (const reference of discovered.references) {
|
|
81
|
+
if (reference.kind === 'assessment-item-ref' && reference.resolvedPath) {
|
|
82
|
+
const target = findResourceByResolvedHref(resources, reference.resolvedPath);
|
|
83
|
+
if (target) {
|
|
84
|
+
reference.targetResourceId = target.identifier;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
diagnostics.push({
|
|
88
|
+
severity: 'warning',
|
|
89
|
+
code: 'IMS_CP_DANGLING_ITEM_REF',
|
|
90
|
+
message: `Resource ${node.identifier} references missing assessment item ${reference.rawHref}.`,
|
|
91
|
+
resourceId: node.identifier,
|
|
92
|
+
sourcePath: node.resolvedHref,
|
|
93
|
+
reference: reference.rawHref,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
resourceReferences.push(reference);
|
|
98
|
+
}
|
|
99
|
+
for (const asset of discovered.assets) {
|
|
100
|
+
registerAsset(assets, asset);
|
|
101
|
+
if (!(await packagePathExists(asset.resolvedPath, fileAccess, listedFiles))) {
|
|
102
|
+
diagnostics.push({
|
|
103
|
+
severity: 'warning',
|
|
104
|
+
code: 'IMS_CP_MISSING_ASSET',
|
|
105
|
+
message: `Resource ${node.identifier} references missing package asset ${asset.resolvedPath}.`,
|
|
106
|
+
resourceId: node.identifier,
|
|
107
|
+
sourcePath: node.resolvedHref,
|
|
108
|
+
reference: asset.rawHref,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else if (!hasMissingFileDiagnostic(diagnostics, node.identifier, node.resolvedHref)) {
|
|
114
|
+
diagnostics.push({
|
|
115
|
+
severity: 'warning',
|
|
116
|
+
code: 'IMS_CP_MISSING_FILE',
|
|
117
|
+
message: `Resource ${node.identifier} could not read package XML file ${node.resolvedHref}.`,
|
|
118
|
+
resourceId: node.identifier,
|
|
119
|
+
sourcePath: node.resolvedHref,
|
|
120
|
+
reference: node.resolvedHref,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
references.set(node.identifier, resourceReferences);
|
|
125
|
+
}
|
|
126
|
+
const closures = new Map();
|
|
127
|
+
for (const entrypoint of [...manifest.tests, ...manifest.items, ...manifest.passages]) {
|
|
128
|
+
closures.set(entrypoint.identifier, buildClosure(entrypoint.identifier, resources, references, diagnostics));
|
|
129
|
+
}
|
|
130
|
+
const qtiVersion = summarizeQtiVersion(versionSignals);
|
|
131
|
+
const packageDiagnostics = [...diagnostics, ...qtiVersion.diagnostics];
|
|
132
|
+
return {
|
|
133
|
+
packageId: packageId ?? manifest.identifier ?? 'qti-package',
|
|
134
|
+
manifest,
|
|
135
|
+
resources,
|
|
136
|
+
entrypoints: [
|
|
137
|
+
...manifest.tests.map((resource) => toEntrypoint(resource, 'test')),
|
|
138
|
+
...manifest.items.map((resource) => toEntrypoint(resource, 'item')),
|
|
139
|
+
...manifest.passages.map((resource) => toEntrypoint(resource, 'passage')),
|
|
140
|
+
],
|
|
141
|
+
references,
|
|
142
|
+
closures,
|
|
143
|
+
assets,
|
|
144
|
+
qtiVersion,
|
|
145
|
+
diagnostics: packageDiagnostics,
|
|
146
|
+
files,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
export function serializeContentPackageEvidence(packageGraph) {
|
|
150
|
+
const resources = [...packageGraph.resources.values()]
|
|
151
|
+
.map((resource) => ({
|
|
152
|
+
...resource,
|
|
153
|
+
files: sortStrings(resource.files),
|
|
154
|
+
resolvedFiles: sortStrings(resource.resolvedFiles),
|
|
155
|
+
dependencies: sortStrings(resource.dependencies),
|
|
156
|
+
}))
|
|
157
|
+
.sort(compareBy((resource) => resource.identifier));
|
|
158
|
+
const references = [...packageGraph.references.entries()]
|
|
159
|
+
.flatMap(([resourceId, resourceReferences]) => resourceReferences.map((reference) => ({ resourceId, ...reference })))
|
|
160
|
+
.sort(compareReferenceEvidence);
|
|
161
|
+
const assets = [...packageGraph.assets.values()]
|
|
162
|
+
.map((asset) => ({
|
|
163
|
+
...asset,
|
|
164
|
+
ownerResourceIds: asset.ownerResourceIds ? sortStrings(asset.ownerResourceIds) : undefined,
|
|
165
|
+
sourcePaths: asset.sourcePaths ? sortStrings(asset.sourcePaths) : undefined,
|
|
166
|
+
}))
|
|
167
|
+
.sort(compareBy((asset) => asset.resolvedPath));
|
|
168
|
+
const closures = [...packageGraph.closures.values()]
|
|
169
|
+
.map((closure) => ({
|
|
170
|
+
...closure,
|
|
171
|
+
resourceIds: sortStrings(closure.resourceIds),
|
|
172
|
+
filePaths: sortStrings(closure.filePaths),
|
|
173
|
+
assetPaths: sortStrings(closure.assetPaths),
|
|
174
|
+
diagnostics: sortDiagnostics(closure.diagnostics),
|
|
175
|
+
}))
|
|
176
|
+
.sort(compareBy((closure) => closure.resourceId));
|
|
177
|
+
return {
|
|
178
|
+
packageId: packageGraph.packageId,
|
|
179
|
+
manifestIdentifier: packageGraph.manifest.identifier,
|
|
180
|
+
entrypoints: [...packageGraph.entrypoints].sort(compareBy((entrypoint) => entrypoint.resourceId)),
|
|
181
|
+
resources,
|
|
182
|
+
references,
|
|
183
|
+
closures,
|
|
184
|
+
assets,
|
|
185
|
+
relationshipHints: buildRelationshipHints(references, assets),
|
|
186
|
+
qtiVersion: {
|
|
187
|
+
...packageGraph.qtiVersion,
|
|
188
|
+
signals: [...packageGraph.qtiVersion.signals].sort(compareVersionSignal),
|
|
189
|
+
diagnostics: sortDiagnostics(packageGraph.qtiVersion.diagnostics),
|
|
190
|
+
},
|
|
191
|
+
diagnostics: sortDiagnostics(packageGraph.diagnostics),
|
|
192
|
+
files: sortStrings(packageGraph.files),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function toResourceNode(resource, manifest) {
|
|
196
|
+
const base = joinPackagePath(manifest.xmlBase, resource.xmlBase);
|
|
197
|
+
return {
|
|
198
|
+
identifier: resource.identifier,
|
|
199
|
+
type: resource.type,
|
|
200
|
+
kind: classifyResource(resource),
|
|
201
|
+
href: resource.href,
|
|
202
|
+
resolvedHref: resource.href ? resolvePackagePath(base, resource.href) : undefined,
|
|
203
|
+
xmlBase: resource.xmlBase,
|
|
204
|
+
files: resource.files,
|
|
205
|
+
resolvedFiles: resource.files.map((file) => resolvePackagePath(base, file)),
|
|
206
|
+
dependencies: resource.dependencies,
|
|
207
|
+
metadata: resource.metadata,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function toEntrypoint(resource, kind) {
|
|
211
|
+
return {
|
|
212
|
+
resourceId: resource.identifier,
|
|
213
|
+
kind,
|
|
214
|
+
href: resource.href,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function classifyResource(resource) {
|
|
218
|
+
if (resource.type.includes('imsqti_item') || resource.type.includes('qti_item'))
|
|
219
|
+
return 'item';
|
|
220
|
+
if (resource.type.includes('test') || resource.type.includes('assessment'))
|
|
221
|
+
return 'test';
|
|
222
|
+
if (resource.type.includes('passage') || resource.type.includes('stimulus'))
|
|
223
|
+
return 'passage';
|
|
224
|
+
if (resource.type === 'webcontent')
|
|
225
|
+
return 'webcontent';
|
|
226
|
+
if (resource.files.some((file) => classifyAssetUsage(file) !== 'other'))
|
|
227
|
+
return 'asset';
|
|
228
|
+
return 'other';
|
|
229
|
+
}
|
|
230
|
+
function discoverReferences(xml, owner) {
|
|
231
|
+
const references = [];
|
|
232
|
+
const assets = [];
|
|
233
|
+
const basePath = owner.resolvedHref ? dirname(owner.resolvedHref) : '';
|
|
234
|
+
for (const match of findAttributeReferences(xml)) {
|
|
235
|
+
const resolvedPath = resolvePackagePath(basePath, match.rawHref);
|
|
236
|
+
const reference = {
|
|
237
|
+
kind: match.kind,
|
|
238
|
+
rawHref: match.rawHref,
|
|
239
|
+
resolvedPath,
|
|
240
|
+
sourcePath: owner.resolvedHref,
|
|
241
|
+
sourceElement: match.element,
|
|
242
|
+
sourceAttribute: match.attribute,
|
|
243
|
+
};
|
|
244
|
+
references.push(reference);
|
|
245
|
+
if (match.kind !== 'assessment-item-ref') {
|
|
246
|
+
assets.push({
|
|
247
|
+
rawHref: match.rawHref,
|
|
248
|
+
resolvedPath,
|
|
249
|
+
usage: usageFromReference(match.kind, resolvedPath),
|
|
250
|
+
ownerResourceId: owner.identifier,
|
|
251
|
+
sourcePath: owner.resolvedHref,
|
|
252
|
+
sourceElement: match.element,
|
|
253
|
+
sourceAttribute: match.attribute,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return { references, assets };
|
|
258
|
+
}
|
|
259
|
+
function findAttributeReferences(xml) {
|
|
260
|
+
const refs = [];
|
|
261
|
+
const pattern = /<(?<element>assessmentItemRef|img|object|audio|video|source|stylesheet|link|file)\b(?<attrs>[^>]*)>/gi;
|
|
262
|
+
for (const match of xml.matchAll(pattern)) {
|
|
263
|
+
const element = match.groups?.element ?? '';
|
|
264
|
+
const attrs = match.groups?.attrs ?? '';
|
|
265
|
+
const attribute = element === 'object' ? 'data' : element === 'img' ? 'src' : 'href';
|
|
266
|
+
const rawHref = readAttribute(attrs, attribute) ?? readAttribute(attrs, 'src') ?? readAttribute(attrs, 'href');
|
|
267
|
+
if (!rawHref || isExternalHref(rawHref))
|
|
268
|
+
continue;
|
|
269
|
+
refs.push({
|
|
270
|
+
kind: kindFromElement(element),
|
|
271
|
+
element,
|
|
272
|
+
attribute: rawHref === readAttribute(attrs, 'data') ? 'data' : rawHref === readAttribute(attrs, 'src') ? 'src' : 'href',
|
|
273
|
+
rawHref,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
return refs;
|
|
277
|
+
}
|
|
278
|
+
function kindFromElement(element) {
|
|
279
|
+
switch (element) {
|
|
280
|
+
case 'assessmentItemRef':
|
|
281
|
+
return 'assessment-item-ref';
|
|
282
|
+
case 'stylesheet':
|
|
283
|
+
case 'link':
|
|
284
|
+
return 'stylesheet';
|
|
285
|
+
case 'object':
|
|
286
|
+
return 'object';
|
|
287
|
+
default:
|
|
288
|
+
return 'asset';
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function usageFromReference(kind, path) {
|
|
292
|
+
if (kind === 'stylesheet')
|
|
293
|
+
return 'stylesheet';
|
|
294
|
+
if (kind === 'catalog')
|
|
295
|
+
return 'catalog';
|
|
296
|
+
return classifyAssetUsage(path);
|
|
297
|
+
}
|
|
298
|
+
function buildClosure(resourceId, resources, references, packageDiagnostics) {
|
|
299
|
+
const visited = new Set();
|
|
300
|
+
const filePaths = new Set();
|
|
301
|
+
const assetPaths = new Set();
|
|
302
|
+
const diagnostics = [];
|
|
303
|
+
function visit(id) {
|
|
304
|
+
if (visited.has(id))
|
|
305
|
+
return;
|
|
306
|
+
const node = resources.get(id);
|
|
307
|
+
if (!node) {
|
|
308
|
+
diagnostics.push({
|
|
309
|
+
severity: 'warning',
|
|
310
|
+
code: 'IMS_CP_MISSING_CLOSURE_RESOURCE',
|
|
311
|
+
message: `Closure references missing resource ${id}.`,
|
|
312
|
+
resourceId,
|
|
313
|
+
reference: id,
|
|
314
|
+
});
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
visited.add(id);
|
|
318
|
+
if (node.resolvedHref)
|
|
319
|
+
filePaths.add(node.resolvedHref);
|
|
320
|
+
for (const file of node.resolvedFiles) {
|
|
321
|
+
filePaths.add(file);
|
|
322
|
+
if (classifyAssetUsage(file) !== 'source-qti')
|
|
323
|
+
assetPaths.add(file);
|
|
324
|
+
}
|
|
325
|
+
for (const reference of references.get(id) ?? []) {
|
|
326
|
+
if (reference.targetResourceId)
|
|
327
|
+
visit(reference.targetResourceId);
|
|
328
|
+
if (reference.resolvedPath && reference.kind !== 'assessment-item-ref') {
|
|
329
|
+
assetPaths.add(reference.resolvedPath);
|
|
330
|
+
filePaths.add(reference.resolvedPath);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
visit(resourceId);
|
|
335
|
+
return {
|
|
336
|
+
resourceId,
|
|
337
|
+
resourceIds: [...visited],
|
|
338
|
+
filePaths: [...filePaths],
|
|
339
|
+
assetPaths: [...assetPaths],
|
|
340
|
+
diagnostics: [...diagnostics, ...packageDiagnostics.filter((diagnostic) => diagnostic.resourceId === resourceId)],
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function maybeRegisterAsset(assets, path, context) {
|
|
344
|
+
const usage = classifyAssetUsage(path);
|
|
345
|
+
if (usage === 'source-qti' || usage === 'other')
|
|
346
|
+
return;
|
|
347
|
+
registerAsset(assets, {
|
|
348
|
+
rawHref: path,
|
|
349
|
+
resolvedPath: path,
|
|
350
|
+
usage,
|
|
351
|
+
...context,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function registerAsset(assets, asset) {
|
|
355
|
+
const existing = assets.get(asset.resolvedPath);
|
|
356
|
+
if (!existing) {
|
|
357
|
+
assets.set(asset.resolvedPath, {
|
|
358
|
+
...asset,
|
|
359
|
+
ownerResourceIds: asset.ownerResourceId ? [asset.ownerResourceId] : [],
|
|
360
|
+
sourcePaths: asset.sourcePath ? [asset.sourcePath] : [],
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const ownerResourceIds = new Set([
|
|
365
|
+
...(existing.ownerResourceIds ?? (existing.ownerResourceId ? [existing.ownerResourceId] : [])),
|
|
366
|
+
...(asset.ownerResourceIds ?? (asset.ownerResourceId ? [asset.ownerResourceId] : [])),
|
|
367
|
+
]);
|
|
368
|
+
const sourcePaths = new Set([
|
|
369
|
+
...(existing.sourcePaths ?? (existing.sourcePath ? [existing.sourcePath] : [])),
|
|
370
|
+
...(asset.sourcePaths ?? (asset.sourcePath ? [asset.sourcePath] : [])),
|
|
371
|
+
]);
|
|
372
|
+
assets.set(asset.resolvedPath, {
|
|
373
|
+
...existing,
|
|
374
|
+
rawHref: existing.rawHref || asset.rawHref,
|
|
375
|
+
usage: existing.usage === 'other' ? asset.usage : existing.usage,
|
|
376
|
+
ownerResourceId: existing.ownerResourceId ?? asset.ownerResourceId,
|
|
377
|
+
ownerResourceIds: [...ownerResourceIds],
|
|
378
|
+
sourcePath: existing.sourcePath ?? asset.sourcePath,
|
|
379
|
+
sourcePaths: [...sourcePaths],
|
|
380
|
+
sourceElement: existing.sourceElement ?? asset.sourceElement,
|
|
381
|
+
sourceAttribute: existing.sourceAttribute ?? asset.sourceAttribute,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
function findResourceByResolvedHref(resources, resolvedHref) {
|
|
385
|
+
return [...resources.values()].find((resource) => resource.resolvedHref === resolvedHref);
|
|
386
|
+
}
|
|
387
|
+
function classifyAssetUsage(path) {
|
|
388
|
+
const lower = path.toLowerCase();
|
|
389
|
+
const extension = Object.keys(ASSET_EXTENSIONS).find((candidate) => lower.endsWith(candidate));
|
|
390
|
+
return extension ? ASSET_EXTENSIONS[extension] : 'other';
|
|
391
|
+
}
|
|
392
|
+
function isXmlLike(path) {
|
|
393
|
+
return path.toLowerCase().endsWith('.xml');
|
|
394
|
+
}
|
|
395
|
+
function readAttribute(attrs, name) {
|
|
396
|
+
const match = attrs.match(new RegExp(`\\b${name}\\s*=\\s*["']([^"']+)["']`, 'i'));
|
|
397
|
+
return match?.[1];
|
|
398
|
+
}
|
|
399
|
+
function isExternalHref(href) {
|
|
400
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/|#)/i.test(href);
|
|
401
|
+
}
|
|
402
|
+
function joinPackagePath(...parts) {
|
|
403
|
+
return parts.filter(Boolean).join('/');
|
|
404
|
+
}
|
|
405
|
+
function resolvePackagePath(basePath, href) {
|
|
406
|
+
const raw = href.replaceAll('\\', '/');
|
|
407
|
+
const combined = raw.startsWith('/') ? raw.slice(1) : joinPackagePath(basePath, raw);
|
|
408
|
+
const normalized = [];
|
|
409
|
+
for (const part of combined.split('/')) {
|
|
410
|
+
if (!part || part === '.')
|
|
411
|
+
continue;
|
|
412
|
+
if (part === '..') {
|
|
413
|
+
normalized.pop();
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
normalized.push(part);
|
|
417
|
+
}
|
|
418
|
+
return normalized.join('/');
|
|
419
|
+
}
|
|
420
|
+
function dirname(path) {
|
|
421
|
+
const normalized = path.replaceAll('\\', '/');
|
|
422
|
+
const index = normalized.lastIndexOf('/');
|
|
423
|
+
return index === -1 ? '' : normalized.slice(0, index);
|
|
424
|
+
}
|
|
425
|
+
async function packagePathExists(path, fileAccess, listedFiles) {
|
|
426
|
+
if (listedFiles)
|
|
427
|
+
return listedFiles.has(path);
|
|
428
|
+
if (fileAccess.exists)
|
|
429
|
+
return Boolean(await fileAccess.exists(path));
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
function hasMissingFileDiagnostic(diagnostics, resourceId, path) {
|
|
433
|
+
return diagnostics.some((diagnostic) => diagnostic.code === 'IMS_CP_MISSING_FILE' &&
|
|
434
|
+
diagnostic.resourceId === resourceId &&
|
|
435
|
+
diagnostic.reference === path);
|
|
436
|
+
}
|
|
437
|
+
function detectManifestVersionSignals(manifestXml) {
|
|
438
|
+
return [
|
|
439
|
+
...detectVersionPatterns(manifestXml, {
|
|
440
|
+
source: 'schema_location',
|
|
441
|
+
confidence: 'medium',
|
|
442
|
+
includeResourceTypes: false,
|
|
443
|
+
}),
|
|
444
|
+
...detectVersionPatterns(manifestXml, {
|
|
445
|
+
source: 'package_metadata',
|
|
446
|
+
confidence: 'low',
|
|
447
|
+
includeResourceTypes: false,
|
|
448
|
+
}),
|
|
449
|
+
];
|
|
450
|
+
}
|
|
451
|
+
function detectResourceTypeVersionSignals(resources) {
|
|
452
|
+
const signals = [];
|
|
453
|
+
for (const resource of resources) {
|
|
454
|
+
signals.push(...detectVersionPatterns(resource.type, {
|
|
455
|
+
source: 'manifest',
|
|
456
|
+
confidence: 'high',
|
|
457
|
+
resourceId: resource.identifier,
|
|
458
|
+
}));
|
|
459
|
+
}
|
|
460
|
+
return dedupeVersionSignals(signals);
|
|
461
|
+
}
|
|
462
|
+
function detectQtiXmlVersionSignals(xml, node) {
|
|
463
|
+
return dedupeVersionSignals(detectVersionPatterns(xml, {
|
|
464
|
+
source: 'qti_xml',
|
|
465
|
+
confidence: 'high',
|
|
466
|
+
resourceId: node.identifier,
|
|
467
|
+
sourcePath: node.resolvedHref,
|
|
468
|
+
}));
|
|
469
|
+
}
|
|
470
|
+
function detectVersionPatterns(value, context) {
|
|
471
|
+
const includeResourceTypes = context.includeResourceTypes ?? true;
|
|
472
|
+
const patterns = [
|
|
473
|
+
{
|
|
474
|
+
version: '3.0',
|
|
475
|
+
patterns: [
|
|
476
|
+
/imsqtiasi_v3p0/i,
|
|
477
|
+
/imsqti_v3p0/i,
|
|
478
|
+
/qti[_-]?v?3p0/i,
|
|
479
|
+
/qti[_\s-]?3\.0/i,
|
|
480
|
+
],
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
version: '2.2',
|
|
484
|
+
patterns: [
|
|
485
|
+
/imsqti_v2p2/i,
|
|
486
|
+
/qti[_-]?v?2p2/i,
|
|
487
|
+
/qti[_\s-]?2\.2/i,
|
|
488
|
+
...(includeResourceTypes ? [/xmlv2p2/i] : []),
|
|
489
|
+
],
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
version: '2.1',
|
|
493
|
+
patterns: [
|
|
494
|
+
/imsqti_v2p1/i,
|
|
495
|
+
/qti[_-]?v?2p1/i,
|
|
496
|
+
/qti[_\s-]?2\.1/i,
|
|
497
|
+
...(includeResourceTypes ? [/xmlv2p1/i] : []),
|
|
498
|
+
],
|
|
499
|
+
},
|
|
500
|
+
];
|
|
501
|
+
const signals = [];
|
|
502
|
+
for (const { version, patterns: versionPatterns } of patterns) {
|
|
503
|
+
for (const pattern of versionPatterns) {
|
|
504
|
+
const match = value.match(pattern);
|
|
505
|
+
if (!match)
|
|
506
|
+
continue;
|
|
507
|
+
signals.push({
|
|
508
|
+
source: context.source,
|
|
509
|
+
version,
|
|
510
|
+
confidence: context.confidence,
|
|
511
|
+
evidence: match[0],
|
|
512
|
+
resourceId: context.resourceId,
|
|
513
|
+
sourcePath: context.sourcePath,
|
|
514
|
+
});
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return dedupeVersionSignals(signals);
|
|
519
|
+
}
|
|
520
|
+
function summarizeQtiVersion(signals) {
|
|
521
|
+
const dedupedSignals = dedupeVersionSignals(signals);
|
|
522
|
+
const versions = [...new Set(dedupedSignals.map((signal) => signal.version))].sort();
|
|
523
|
+
if (versions.length === 0) {
|
|
524
|
+
return {
|
|
525
|
+
version: 'unknown',
|
|
526
|
+
confidence: 'none',
|
|
527
|
+
signals: [],
|
|
528
|
+
diagnostics: [
|
|
529
|
+
{
|
|
530
|
+
severity: 'warning',
|
|
531
|
+
code: 'QTI_VERSION_UNKNOWN',
|
|
532
|
+
message: 'Could not determine the QTI package version from manifest, schema, metadata, or readable QTI XML.',
|
|
533
|
+
},
|
|
534
|
+
],
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
if (versions.length > 1) {
|
|
538
|
+
return {
|
|
539
|
+
version: 'conflicting',
|
|
540
|
+
confidence: 'conflicting',
|
|
541
|
+
signals: dedupedSignals,
|
|
542
|
+
diagnostics: [
|
|
543
|
+
{
|
|
544
|
+
severity: 'warning',
|
|
545
|
+
code: 'QTI_VERSION_CONFLICT',
|
|
546
|
+
message: `Conflicting QTI package version signals were found: ${versions.join(', ')}.`,
|
|
547
|
+
reference: versions.join(','),
|
|
548
|
+
},
|
|
549
|
+
],
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
return {
|
|
553
|
+
version: versions[0],
|
|
554
|
+
confidence: strongestConfidence(dedupedSignals),
|
|
555
|
+
signals: dedupedSignals,
|
|
556
|
+
diagnostics: [],
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
function strongestConfidence(signals) {
|
|
560
|
+
if (signals.some((signal) => signal.confidence === 'high'))
|
|
561
|
+
return 'high';
|
|
562
|
+
if (signals.some((signal) => signal.confidence === 'medium'))
|
|
563
|
+
return 'medium';
|
|
564
|
+
if (signals.some((signal) => signal.confidence === 'low'))
|
|
565
|
+
return 'low';
|
|
566
|
+
return 'none';
|
|
567
|
+
}
|
|
568
|
+
function dedupeVersionSignals(signals) {
|
|
569
|
+
const byKey = new Map();
|
|
570
|
+
for (const signal of signals) {
|
|
571
|
+
const key = [
|
|
572
|
+
signal.source,
|
|
573
|
+
signal.version,
|
|
574
|
+
signal.confidence,
|
|
575
|
+
signal.resourceId ?? '',
|
|
576
|
+
signal.sourcePath ?? '',
|
|
577
|
+
signal.evidence,
|
|
578
|
+
].join('|');
|
|
579
|
+
byKey.set(key, signal);
|
|
580
|
+
}
|
|
581
|
+
return [...byKey.values()];
|
|
582
|
+
}
|
|
583
|
+
function buildRelationshipHints(references, assets) {
|
|
584
|
+
const hints = [];
|
|
585
|
+
for (const reference of references) {
|
|
586
|
+
if (reference.kind !== 'manifest-dependency' && reference.kind !== 'assessment-item-ref')
|
|
587
|
+
continue;
|
|
588
|
+
hints.push({
|
|
589
|
+
kind: reference.kind,
|
|
590
|
+
sourceResourceId: reference.resourceId,
|
|
591
|
+
targetResourceId: reference.targetResourceId,
|
|
592
|
+
targetPath: reference.resolvedPath,
|
|
593
|
+
rawHref: reference.rawHref,
|
|
594
|
+
sourcePath: reference.sourcePath,
|
|
595
|
+
sourceElement: reference.sourceElement,
|
|
596
|
+
sourceAttribute: reference.sourceAttribute,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
for (const asset of assets) {
|
|
600
|
+
const ownerResourceIds = asset.ownerResourceIds ?? (asset.ownerResourceId ? [asset.ownerResourceId] : []);
|
|
601
|
+
if (ownerResourceIds.length < 2)
|
|
602
|
+
continue;
|
|
603
|
+
hints.push({
|
|
604
|
+
kind: 'shared-asset',
|
|
605
|
+
sourceResourceIds: sortStrings(ownerResourceIds),
|
|
606
|
+
targetPath: asset.resolvedPath,
|
|
607
|
+
rawHref: asset.rawHref,
|
|
608
|
+
sourcePath: asset.sourcePath,
|
|
609
|
+
sourceElement: asset.sourceElement,
|
|
610
|
+
sourceAttribute: asset.sourceAttribute,
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
return hints.sort(compareRelationshipHint);
|
|
614
|
+
}
|
|
615
|
+
function sortStrings(values) {
|
|
616
|
+
return [...values].sort((left, right) => left.localeCompare(right));
|
|
617
|
+
}
|
|
618
|
+
function sortDiagnostics(diagnostics) {
|
|
619
|
+
return [...diagnostics].sort((left, right) => left.severity.localeCompare(right.severity) ||
|
|
620
|
+
left.code.localeCompare(right.code) ||
|
|
621
|
+
(left.resourceId ?? '').localeCompare(right.resourceId ?? '') ||
|
|
622
|
+
(left.sourcePath ?? '').localeCompare(right.sourcePath ?? '') ||
|
|
623
|
+
(left.reference ?? '').localeCompare(right.reference ?? '') ||
|
|
624
|
+
left.message.localeCompare(right.message));
|
|
625
|
+
}
|
|
626
|
+
function compareBy(select) {
|
|
627
|
+
return (left, right) => (select(left) ?? '').localeCompare(select(right) ?? '');
|
|
628
|
+
}
|
|
629
|
+
function compareReferenceEvidence(left, right) {
|
|
630
|
+
return (left.resourceId.localeCompare(right.resourceId) ||
|
|
631
|
+
left.kind.localeCompare(right.kind) ||
|
|
632
|
+
(left.targetResourceId ?? '').localeCompare(right.targetResourceId ?? '') ||
|
|
633
|
+
(left.resolvedPath ?? '').localeCompare(right.resolvedPath ?? '') ||
|
|
634
|
+
left.rawHref.localeCompare(right.rawHref));
|
|
635
|
+
}
|
|
636
|
+
function compareVersionSignal(left, right) {
|
|
637
|
+
return (left.version.localeCompare(right.version) ||
|
|
638
|
+
left.source.localeCompare(right.source) ||
|
|
639
|
+
(left.resourceId ?? '').localeCompare(right.resourceId ?? '') ||
|
|
640
|
+
(left.sourcePath ?? '').localeCompare(right.sourcePath ?? '') ||
|
|
641
|
+
left.evidence.localeCompare(right.evidence));
|
|
642
|
+
}
|
|
643
|
+
function compareRelationshipHint(left, right) {
|
|
644
|
+
return (left.kind.localeCompare(right.kind) ||
|
|
645
|
+
(left.sourceResourceId ?? '').localeCompare(right.sourceResourceId ?? '') ||
|
|
646
|
+
(left.sourceResourceIds?.join(',') ?? '').localeCompare(right.sourceResourceIds?.join(',') ?? '') ||
|
|
647
|
+
(left.targetResourceId ?? '').localeCompare(right.targetResourceId ?? '') ||
|
|
648
|
+
(left.targetPath ?? '').localeCompare(right.targetPath ?? '') ||
|
|
649
|
+
(left.rawHref ?? '').localeCompare(right.rawHref ?? ''));
|
|
650
|
+
}
|
|
651
|
+
//# sourceMappingURL=package-graph.js.map
|