@learnosity/lib-qti-converter 0.16.0 → 0.16.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.
Files changed (41) hide show
  1. package/package.json +10 -10
  2. package/packages/cli/dist/{batchProcessors-C7czhqX0.js → batchProcessors-BI6C89vy.js} +67 -94
  3. package/packages/cli/dist/batchProcessors-BI6C89vy.js.map +1 -0
  4. package/packages/cli/dist/batchWorker.js +2 -3
  5. package/packages/cli/dist/{configOutput-FwkFyQmk.js → configOutput-CJ-ryN3j.js} +5 -9
  6. package/packages/cli/dist/{configOutput-FwkFyQmk.js.map → configOutput-CJ-ryN3j.js.map} +1 -1
  7. package/packages/cli/dist/{create-prompt-dWckcotJ.js → create-prompt-m0MC0vT_.js} +38 -57
  8. package/packages/cli/dist/create-prompt-m0MC0vT_.js.map +1 -0
  9. package/packages/cli/dist/{dist-DXDR2peT.js → dist-8MaFD1oj.js} +6 -10
  10. package/packages/cli/dist/dist-8MaFD1oj.js.map +1 -0
  11. package/packages/cli/dist/{learnosityToQti-D4ddrXaz.js → learnosityToQti-BDf_faIV.js} +5 -6
  12. package/packages/cli/dist/{learnosityToQti-D4ddrXaz.js.map → learnosityToQti-BDf_faIV.js.map} +1 -1
  13. package/packages/cli/dist/{learnosityToQtiPackage-Bwzm-uLX.js → learnosityToQtiPackage-w-BXgsLt.js} +15 -14
  14. package/packages/cli/dist/learnosityToQtiPackage-w-BXgsLt.js.map +1 -0
  15. package/packages/cli/dist/main.js +22 -29
  16. package/packages/cli/dist/main.js.map +1 -1
  17. package/packages/cli/dist/{nodeFileAccess-BhCoGDOi.js → nodeFileAccess-BBS4FAK7.js} +5 -9
  18. package/packages/cli/dist/{nodeFileAccess-BhCoGDOi.js.map → nodeFileAccess-BBS4FAK7.js.map} +1 -1
  19. package/packages/cli/dist/pathUtils-BUJJCvvp.js +2919 -0
  20. package/packages/cli/dist/pathUtils-BUJJCvvp.js.map +1 -0
  21. package/packages/cli/dist/{qtiPackageToLearnosity-CX9htPId.js → qtiPackageToLearnosity-C24effmB.js} +7 -8
  22. package/packages/cli/dist/{qtiPackageToLearnosity-CX9htPId.js.map → qtiPackageToLearnosity-C24effmB.js.map} +1 -1
  23. package/packages/cli/dist/{qtiToLearnosity-JQYOkNbM.js → qtiToLearnosity-BEOaukuf.js} +6 -7
  24. package/packages/cli/dist/{qtiToLearnosity-JQYOkNbM.js.map → qtiToLearnosity-BEOaukuf.js.map} +1 -1
  25. package/packages/cli/dist/{resolveOptions-BuNN2KMA.js → resolveOptions-l0Hpn8hY.js} +322 -359
  26. package/packages/cli/dist/resolveOptions-l0Hpn8hY.js.map +1 -0
  27. package/packages/cli/dist/{rolldown-runtime-DktXgydw.js → rolldown-runtime-BVN1G_yT.js} +5 -6
  28. package/packages/cli/dist/{table-DsKq-B4-.js → table-BkrwlSAQ.js} +9724 -2071
  29. package/packages/cli/dist/table-BkrwlSAQ.js.map +1 -0
  30. package/packages/core/dist/index.d.ts +8 -58
  31. package/packages/core/dist/index.js +9712 -2049
  32. package/packages/core/dist/node.d.ts +10 -80
  33. package/packages/core/dist/node.js +9731 -2084
  34. package/packages/cli/dist/batchProcessors-C7czhqX0.js.map +0 -1
  35. package/packages/cli/dist/create-prompt-dWckcotJ.js.map +0 -1
  36. package/packages/cli/dist/dist-DXDR2peT.js.map +0 -1
  37. package/packages/cli/dist/learnosityToQtiPackage-Bwzm-uLX.js.map +0 -1
  38. package/packages/cli/dist/pathUtils-D_VsVde8.js +0 -2272
  39. package/packages/cli/dist/pathUtils-D_VsVde8.js.map +0 -1
  40. package/packages/cli/dist/resolveOptions-BuNN2KMA.js.map +0 -1
  41. package/packages/cli/dist/table-DsKq-B4-.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "path";
2
2
  import { closeSync, existsSync, openSync, readFileSync, readSync, realpathSync } from "fs";
3
- //#region ../core/src/qti-to-learnosity/utils/passageResolver.ts
3
+ // ../core/src/qti-to-learnosity/utils/passageResolver.ts
4
4
  /**
5
5
  * Passage HTML file resolver
6
6
  * Resolves and reads external HTML files referenced by <object> tags in QTI passages
@@ -50,8 +50,7 @@ function resolvePassageFile(objectDataPath, sourceFile) {
50
50
  }
51
51
  }
52
52
  }
53
- //#endregion
54
- //#region ../core/src/qti-to-learnosity/utils/lomFinder.ts
53
+ // ../core/src/qti-to-learnosity/utils/lomFinder.ts
55
54
  /**
56
55
  * LOM file discovery utility
57
56
  * Locates LOM metadata files for QTI items
@@ -91,8 +90,7 @@ function findAndReadLomFile(itemPath) {
91
90
  return;
92
91
  }
93
92
  }
94
- //#endregion
95
- //#region ../core/src/qti-to-learnosity/utils/imageDimensions.ts
93
+ // ../core/src/qti-to-learnosity/utils/imageDimensions.ts
96
94
  /**
97
95
  * Image dimension resolver
98
96
  *
@@ -281,14 +279,12 @@ function resolveImageDimensions(objectDataPath, sourceFile, packageRoot) {
281
279
  if (buf) return readImageDimensionsFromBuffer(buf);
282
280
  }
283
281
  }
284
- //#endregion
285
- //#region ../core/src/qti-to-learnosity/nodeFileAccess.ts
282
+ // ../core/src/qti-to-learnosity/nodeFileAccess.ts
286
283
  const nodeFileAccess = {
287
284
  resolvePassageFile,
288
285
  findAndReadLomFile,
289
286
  resolveImageDimensions
290
287
  };
291
- //#endregion
292
288
  export { nodeFileAccess as t };
293
289
 
294
- //# sourceMappingURL=nodeFileAccess-BhCoGDOi.js.map
290
+ //# sourceMappingURL=nodeFileAccess-BBS4FAK7.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"nodeFileAccess-BhCoGDOi.js","names":["containedPath"],"sources":["../../core/src/qti-to-learnosity/utils/passageResolver.ts","../../core/src/qti-to-learnosity/utils/lomFinder.ts","../../core/src/qti-to-learnosity/utils/imageDimensions.ts","../../core/src/qti-to-learnosity/nodeFileAccess.ts"],"sourcesContent":["/**\n * Passage HTML file resolver\n * Resolves and reads external HTML files referenced by <object> tags in QTI passages\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, join, isAbsolute, resolve, relative, sep } from 'path';\n\n/**\n * Resolve `candidate` and confirm it stays inside `root`.\n *\n * The `data` attribute of an <object> tag comes from the QTI file being\n * converted, which is untrusted input. Without this check a crafted value such\n * as \"../../../../etc/passwd\" would let a QTI package read arbitrary files.\n *\n * @param candidate - Path to check (absolute or already joined)\n * @param root - Directory the path must stay within\n * @returns Absolute path if contained by root, undefined otherwise\n */\nfunction containedPath(candidate: string, root: string): string | undefined {\n // These two resolve() calls are the sanitiser, not a sink: normalising both\n // sides is what makes the relative() comparison below meaningful.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(root);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedCandidate = resolve(candidate);\n const rel = relative(resolvedRoot, resolvedCandidate);\n\n // Empty means the candidate is the root itself; a leading \"..\" segment or an\n // absolute result means it escaped the root (e.g. a different drive).\n if (rel === '' || isAbsolute(rel) || rel.split(sep)[0] === '..') {\n return undefined;\n }\n\n return resolvedCandidate;\n}\n\n/**\n * Resolve and read a passage HTML file referenced by an <object> tag\n *\n * @param objectDataPath - The path from the object's data attribute (e.g., \"passages/passage.html\")\n * @param sourceFile - The absolute path to the source QTI XML file\n * @returns The HTML content of the passage file, or undefined if not found\n */\nexport function resolvePassageFile(\n objectDataPath: string,\n sourceFile?: string\n): string | undefined {\n if (!sourceFile || !objectDataPath) {\n return undefined;\n }\n\n // Get the directory containing the source XML file\n const sourceDir = dirname(sourceFile);\n\n // Passages sit either beside the item (passages/passage.html) or one level\n // up (items/item.xml -> ../passages/passage.html), so the parent directory\n // is the widest location a passage may legitimately be read from.\n const root = dirname(sourceDir);\n\n // Candidates, in priority order: relative to the source directory, then\n // relative to its parent. Absolute values are taken as-is. Each one is passed\n // through containedPath() below before it reaches the filesystem.\n const candidates: string[] = [];\n\n if (isAbsolute(objectDataPath)) {\n candidates.push(objectDataPath);\n } else {\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(sourceDir, objectDataPath));\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(root, objectDataPath));\n }\n\n for (const candidate of candidates) {\n // Every candidate is confined to `root` before it reaches the filesystem\n const passagePath = containedPath(candidate, root);\n\n if (!passagePath || !existsSync(passagePath)) {\n continue;\n }\n\n try {\n return readFileSync(passagePath, 'utf-8').replace(/^\\uFEFF/, '');\n } catch {\n // File exists but couldn't be read\n return undefined;\n }\n }\n\n return undefined;\n}\n\n","/**\n * LOM file discovery utility\n * Locates LOM metadata files for QTI items\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, basename, join } from 'path';\n\n/**\n * Find LOM file for a given QTI item file path\n * Checks for:\n * - .../items/<id>.xml → .../metadata/<id>.lom.xml\n * - <dir>/<id>.xml → <dir>/metadata/<id>.lom.xml\n * - <dir>/<id>.xml → <dir>/../metadata/<id>.lom.xml\n *\n * @param itemPath - Absolute path to the QTI item XML file\n * @returns LOM XML content if found, undefined otherwise\n */\nexport function findAndReadLomFile(itemPath: string): string | undefined {\n const dir = dirname(itemPath);\n const filename = basename(itemPath);\n const itemId = filename.replace(/\\.xml$/i, '');\n\n // basename() strips every directory separator, so lomFilename cannot carry\n // traversal segments. The joins below stay within dir or its parent, both of\n // which are derived from the item path the operator asked us to convert.\n const lomFilename = `${itemId}.lom.xml`;\n\n // Pattern 1: .../items/<id>.xml → .../metadata/<id>.lom.xml (sibling folder)\n if (dir.endsWith('/items') || dir.endsWith('\\\\items')) {\n const parentDir = dirname(dir);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath = join(parentDir, 'metadata', lomFilename);\n\n if (existsSync(lomPath)) {\n try {\n return readFileSync(lomPath, 'utf-8');\n } catch {\n return undefined;\n }\n }\n }\n\n // Pattern 2: <dir>/<id>.xml → <dir>/metadata/<id>.lom.xml\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath1 = join(dir, 'metadata', lomFilename);\n\n if (existsSync(lomPath1)) {\n try {\n return readFileSync(lomPath1, 'utf-8');\n } catch {\n return undefined;\n }\n }\n\n // Pattern 3: <dir>/<id>.xml → <dir>/../metadata/<id>.lom.xml\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath2 = join(dir, '..', 'metadata', lomFilename);\n\n if (existsSync(lomPath2)) {\n try {\n return readFileSync(lomPath2, 'utf-8');\n } catch {\n return undefined;\n }\n }\n\n return undefined;\n}\n","/**\n * Image dimension resolver\n *\n * Resolves an image file referenced by a QTI `<object data=\"...\">` /\n * `<img src=\"...\">` tag and reads its intrinsic pixel dimensions directly from\n * the file header. This is a dependency-free reader for the formats commonly\n * found in QTI packages (GIF, PNG, JPEG). It reads only the small header region\n * needed to determine dimensions, not the whole file.\n *\n * QTI legitimately allows an `<object>` without width/height attributes, but\n * some Learnosity question types (e.g. hotspot) need pixel dimensions to convert\n * absolute coordinates into percentages. When the markup omits them, the\n * dimensions are recovered from the referenced image file.\n */\n\nimport { existsSync, closeSync, openSync, readSync, realpathSync } from 'fs';\nimport { dirname, join, isAbsolute, resolve, relative, sep } from 'path';\n\nexport interface ImageDimensions {\n width: number;\n height: number;\n}\n\n/**\n * Only the leading bytes of an image are needed to read its dimensions: GIF and\n * PNG carry them in the first ~24 bytes, and a JPEG's first Start-Of-Frame\n * marker is virtually always within the first segments. Cap the read so a huge\n * asset can never be loaded whole into memory.\n */\nconst MAX_HEADER_BYTES = 64 * 1024;\n\n/**\n * Check that `resolvedCandidate` stays lexically inside `resolvedRoot`.\n *\n * Both arguments must already be absolute/normalised. Returns true when the\n * candidate is contained (and is not the root itself).\n */\nfunction isContained(resolvedCandidate: string, resolvedRoot: string): boolean {\n const rel = relative(resolvedRoot, resolvedCandidate);\n\n return rel !== '' && !isAbsolute(rel) && rel.split(sep)[0] !== '..';\n}\n\n/**\n * Resolve `candidate` and confirm it stays inside `root`, both lexically and\n * after dereferencing symlinks.\n *\n * The `data`/`src` attribute comes from the QTI file being converted, which is\n * untrusted input. A lexical check alone is insufficient: a package could place\n * a symlink under `root` that targets a file outside it (e.g. `/dev/zero`), so\n * the real path is re-verified against the real root before it is returned for\n * reading.\n *\n * @returns The real (symlink-dereferenced) absolute path if contained, else undefined.\n */\nfunction containedPath(candidate: string, root: string): string | undefined {\n // These resolve() calls are the sanitiser, not a sink: normalising both\n // sides is what makes the relative() comparison below meaningful.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(root);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedCandidate = resolve(candidate);\n\n // Lexical containment first (cheap, and rejects obvious \"../\" escapes even\n // when the target does not exist).\n if (!isContained(resolvedCandidate, resolvedRoot)) {\n return undefined;\n }\n\n if (!existsSync(resolvedCandidate)) {\n return undefined;\n }\n\n // Dereference symlinks on both sides and re-verify: this closes the gap\n // where a symlink inside root points outside it. If realpath fails (broken\n // link, race), treat as not resolvable.\n let realCandidate: string;\n let realRoot: string;\n\n try {\n realCandidate = realpathSync(resolvedCandidate);\n realRoot = realpathSync(resolvedRoot);\n } catch {\n return undefined;\n }\n\n if (!isContained(realCandidate, realRoot)) {\n return undefined;\n }\n\n return realCandidate;\n}\n\n/**\n * Read up to {@link MAX_HEADER_BYTES} from the start of a file without loading\n * the whole file into memory. Returns the bytes actually read.\n */\nfunction readHeaderBytes(path: string): Buffer | undefined {\n let fd: number | undefined;\n\n try {\n fd = openSync(path, 'r');\n const buf = Buffer.alloc(MAX_HEADER_BYTES);\n const bytesRead = readSync(fd, buf, 0, MAX_HEADER_BYTES, 0);\n\n return buf.subarray(0, bytesRead);\n } catch {\n return undefined;\n } finally {\n if (fd !== undefined) {\n try {\n closeSync(fd);\n } catch {\n // Best-effort close; nothing actionable if it fails.\n }\n }\n }\n}\n\n/**\n * Read image dimensions from an in-memory file header buffer.\n * Exported for unit testing without touching the filesystem.\n *\n * Supports GIF (87a/89a), PNG, and JPEG (baseline and progressive).\n *\n * @param buf - The image file bytes (at least the header region)\n * @returns The pixel dimensions, or undefined if the format is unrecognised\n */\nexport function readImageDimensionsFromBuffer(buf: Buffer): ImageDimensions | undefined {\n // GIF: \"GIF87a\" or \"GIF89a\"; width/height are little-endian uint16 at offset 6/8.\n if (\n buf.length >= 10 &&\n buf[0] === 0x47 && // G\n buf[1] === 0x49 && // I\n buf[2] === 0x46 // F\n ) {\n const width = buf.readUInt16LE(6);\n const height = buf.readUInt16LE(8);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // PNG: 8-byte signature, then IHDR with width/height as big-endian uint32\n // at offset 16/20.\n if (\n buf.length >= 24 &&\n buf[0] === 0x89 &&\n buf[1] === 0x50 && // P\n buf[2] === 0x4e && // N\n buf[3] === 0x47 // G\n ) {\n const width = buf.readUInt32BE(16);\n const height = buf.readUInt32BE(20);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // JPEG: starts with 0xFFD8. Scan marker segments for a Start Of Frame\n // (SOF0-SOF15, excluding non-SOF markers 0xC4/0xC8/0xCC) which carries\n // height/width as big-endian uint16.\n if (buf.length >= 4 && buf[0] === 0xff && buf[1] === 0xd8) {\n let offset = 2;\n\n while (offset + 9 < buf.length) {\n // A marker is 0xFF followed by a non-0xFF, non-0x00 marker code.\n // JPEG permits any number of 0xFF fill bytes before the marker code,\n // so skip the whole run of 0xFF and land on the marker byte itself.\n if (buf[offset] !== 0xff) {\n offset++;\n continue;\n }\n\n let markerOffset = offset;\n\n while (markerOffset < buf.length && buf[markerOffset] === 0xff) {\n markerOffset++;\n }\n\n if (markerOffset >= buf.length) {\n break;\n }\n\n const marker = buf[markerOffset];\n // The length field (if any) follows the marker code byte.\n const segmentStart = markerOffset + 1;\n\n // 0x00 after 0xFF is a stuffed byte inside entropy-coded data, not a\n // marker; skip past it and keep scanning.\n if (marker === 0x00) {\n offset = segmentStart;\n continue;\n }\n\n // Standalone markers without a length payload.\n if (marker === 0xd8 || marker === 0xd9 || (marker >= 0xd0 && marker <= 0xd7)) {\n offset = segmentStart;\n continue;\n }\n\n if (segmentStart + 1 >= buf.length) {\n break;\n }\n\n const segmentLength = buf.readUInt16BE(segmentStart);\n\n // SOF markers carrying frame dimensions (exclude DHT 0xC4, JPG 0xC8,\n // DAC 0xCC which share the 0xCn range but are not frame headers).\n const isSof =\n marker >= 0xc0 &&\n marker <= 0xcf &&\n marker !== 0xc4 &&\n marker !== 0xc8 &&\n marker !== 0xcc;\n\n if (isSof) {\n // SOF payload after the 2-byte length: precision (1 byte),\n // height (2 bytes), width (2 bytes).\n if (segmentStart + 6 >= buf.length) {\n break;\n }\n\n const height = buf.readUInt16BE(segmentStart + 3);\n const width = buf.readUInt16BE(segmentStart + 5);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // Advance past this segment: length field + its payload.\n offset = segmentStart + segmentLength;\n }\n }\n\n return undefined;\n}\n\n/**\n * Resolve and read the intrinsic dimensions of an image referenced by a QTI\n * `<object>`/`<img>` tag.\n *\n * Security model: the `objectDataPath` comes from untrusted QTI markup, so every\n * resolved path is confined to a containment root before it is read.\n *\n * - When `packageRoot` is supplied (the normal case for package conversion), any\n * asset located anywhere inside the package is allowed — including assets a\n * sibling directory up, e.g. `questions/item.xml` referencing\n * `../images/foo.png`. This does not rely on directory names.\n * - When `packageRoot` is omitted (in-memory callers with no package context),\n * the read is confined to the source file's own directory. This is the safe\n * default: no path is read from outside where the item lives.\n *\n * @param objectDataPath - Path from the data/src attribute (e.g. \"754493.gif\")\n * @param sourceFile - Absolute path to the source QTI XML file\n * @param packageRoot - Absolute path to the package root that bounds all reads\n * @returns The image dimensions, or undefined if it cannot be resolved/read.\n */\nexport function resolveImageDimensions(\n objectDataPath: string,\n sourceFile?: string,\n packageRoot?: string\n): ImageDimensions | undefined {\n if (!sourceFile || !objectDataPath) {\n return undefined;\n }\n\n const sourceDir = dirname(sourceFile);\n\n // The containment root: the package root when known (so any in-package asset\n // resolves regardless of directory naming), otherwise the item's own\n // directory. If a packageRoot is given but the source file somehow sits\n // outside it, fall back to the source directory rather than trusting it.\n let root = sourceDir;\n let shouldTryPackageRoot = false;\n\n if (packageRoot) {\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(packageRoot);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedSourceDir = resolve(sourceDir);\n const rel = relative(resolvedRoot, resolvedSourceDir);\n const sourceInsideRoot = rel === '' || (!isAbsolute(rel) && rel.split(sep)[0] !== '..');\n\n root = sourceInsideRoot ? resolvedRoot : resolvedSourceDir;\n shouldTryPackageRoot = sourceInsideRoot && resolvedRoot !== resolvedSourceDir;\n }\n\n const candidates: string[] = [];\n\n if (isAbsolute(objectDataPath)) {\n candidates.push(objectDataPath);\n } else {\n // Resolve relative to the item first (most common), then relative to the\n // package root. Both are re-checked for containment before any read.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(sourceDir, objectDataPath));\n\n if (shouldTryPackageRoot) {\n // The candidate is rechecked by containedPath() for lexical and\n // realpath containment before it can reach the filesystem.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(root, objectDataPath));\n }\n }\n\n for (const candidate of candidates) {\n // containedPath dereferences symlinks and re-verifies containment, so\n // the returned path is safe to read.\n const imagePath = containedPath(candidate, root);\n\n if (!imagePath) {\n continue;\n }\n\n // Read only a bounded header region: enough for GIF/PNG and to scan the\n // early JPEG marker segments that carry frame dimensions, without\n // loading a potentially very large asset into memory.\n const buf = readHeaderBytes(imagePath);\n\n if (buf) {\n return readImageDimensionsFromBuffer(buf);\n }\n }\n\n return undefined;\n}\n","/**\n * Filesystem-backed {@link FileAccess} for Node.js callers (the CLI and\n * disk-based package conversion). Reads passage HTML and LOM metadata from the\n * local filesystem.\n *\n * This module imports Node built-ins (via the resolver helpers) and is therefore\n * NOT part of the browser-safe surface — it is wired in by Node entry points.\n */\nimport type { FileAccess } from './fileAccess.js';\nimport { resolvePassageFile } from './utils/passageResolver.js';\nimport { findAndReadLomFile } from './utils/lomFinder.js';\nimport { resolveImageDimensions } from './utils/imageDimensions.js';\n\nexport const nodeFileAccess: FileAccess = {\n resolvePassageFile,\n findAndReadLomFile,\n resolveImageDimensions,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,SAASA,gBAAc,WAAmB,MAAkC;CAIxE,MAAM,eAAe,QAAQ,IAAI;CAEjC,MAAM,oBAAoB,QAAQ,SAAS;CAC3C,MAAM,MAAM,SAAS,cAAc,iBAAiB;CAIpD,IAAI,QAAQ,MAAM,WAAW,GAAG,KAAK,IAAI,MAAM,GAAG,CAAC,CAAC,OAAO,MACvD;CAGJ,OAAO;AACX;;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b,MAAM,eAAe,QAAQ,WAAW;EAExC,MAAM,oBAAoB,QAAQ,SAAS;EAC3C,MAAM,MAAM,SAAS,cAAc,iBAAiB;EACpD,MAAM,mBAAmB,QAAQ,MAAO,CAAC,WAAW,GAAG,KAAK,IAAI,MAAM,GAAG,CAAC,CAAC,OAAO;EAElF,OAAO,mBAAmB,eAAe;EACzC,uBAAuB,oBAAoB,iBAAiB;CAChE;CAEA,MAAM,aAAuB,CAAC;CAE9B,IAAI,WAAW,cAAc,GACzB,WAAW,KAAK,cAAc;MAC3B;EAIH,WAAW,KAAK,KAAK,WAAW,cAAc,CAAC;EAE/C,IAAI,sBAIA,WAAW,KAAK,KAAK,MAAM,cAAc,CAAC;CAElD;CAEA,KAAK,MAAM,aAAa,YAAY;EAGhC,MAAM,YAAY,cAAc,WAAW,IAAI;EAE/C,IAAI,CAAC,WACD;EAMJ,MAAM,MAAM,gBAAgB,SAAS;EAErC,IAAI,KACA,OAAO,8BAA8B,GAAG;CAEhD;AAGJ;;;AClUA,MAAa,iBAA6B;CACtC;CACA;CACA;AACJ"}
1
+ {"version":3,"file":"nodeFileAccess-BBS4FAK7.js","names":["containedPath"],"sources":["../../core/src/qti-to-learnosity/utils/passageResolver.ts","../../core/src/qti-to-learnosity/utils/lomFinder.ts","../../core/src/qti-to-learnosity/utils/imageDimensions.ts","../../core/src/qti-to-learnosity/nodeFileAccess.ts"],"sourcesContent":["/**\n * Passage HTML file resolver\n * Resolves and reads external HTML files referenced by <object> tags in QTI passages\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, join, isAbsolute, resolve, relative, sep } from 'path';\n\n/**\n * Resolve `candidate` and confirm it stays inside `root`.\n *\n * The `data` attribute of an <object> tag comes from the QTI file being\n * converted, which is untrusted input. Without this check a crafted value such\n * as \"../../../../etc/passwd\" would let a QTI package read arbitrary files.\n *\n * @param candidate - Path to check (absolute or already joined)\n * @param root - Directory the path must stay within\n * @returns Absolute path if contained by root, undefined otherwise\n */\nfunction containedPath(candidate: string, root: string): string | undefined {\n // These two resolve() calls are the sanitiser, not a sink: normalising both\n // sides is what makes the relative() comparison below meaningful.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(root);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedCandidate = resolve(candidate);\n const rel = relative(resolvedRoot, resolvedCandidate);\n\n // Empty means the candidate is the root itself; a leading \"..\" segment or an\n // absolute result means it escaped the root (e.g. a different drive).\n if (rel === '' || isAbsolute(rel) || rel.split(sep)[0] === '..') {\n return undefined;\n }\n\n return resolvedCandidate;\n}\n\n/**\n * Resolve and read a passage HTML file referenced by an <object> tag\n *\n * @param objectDataPath - The path from the object's data attribute (e.g., \"passages/passage.html\")\n * @param sourceFile - The absolute path to the source QTI XML file\n * @returns The HTML content of the passage file, or undefined if not found\n */\nexport function resolvePassageFile(\n objectDataPath: string,\n sourceFile?: string\n): string | undefined {\n if (!sourceFile || !objectDataPath) {\n return undefined;\n }\n\n // Get the directory containing the source XML file\n const sourceDir = dirname(sourceFile);\n\n // Passages sit either beside the item (passages/passage.html) or one level\n // up (items/item.xml -> ../passages/passage.html), so the parent directory\n // is the widest location a passage may legitimately be read from.\n const root = dirname(sourceDir);\n\n // Candidates, in priority order: relative to the source directory, then\n // relative to its parent. Absolute values are taken as-is. Each one is passed\n // through containedPath() below before it reaches the filesystem.\n const candidates: string[] = [];\n\n if (isAbsolute(objectDataPath)) {\n candidates.push(objectDataPath);\n } else {\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(sourceDir, objectDataPath));\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(root, objectDataPath));\n }\n\n for (const candidate of candidates) {\n // Every candidate is confined to `root` before it reaches the filesystem\n const passagePath = containedPath(candidate, root);\n\n if (!passagePath || !existsSync(passagePath)) {\n continue;\n }\n\n try {\n return readFileSync(passagePath, 'utf-8').replace(/^\\uFEFF/, '');\n } catch {\n // File exists but couldn't be read\n return undefined;\n }\n }\n\n return undefined;\n}\n\n","/**\n * LOM file discovery utility\n * Locates LOM metadata files for QTI items\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, basename, join } from 'path';\n\n/**\n * Find LOM file for a given QTI item file path\n * Checks for:\n * - .../items/<id>.xml → .../metadata/<id>.lom.xml\n * - <dir>/<id>.xml → <dir>/metadata/<id>.lom.xml\n * - <dir>/<id>.xml → <dir>/../metadata/<id>.lom.xml\n *\n * @param itemPath - Absolute path to the QTI item XML file\n * @returns LOM XML content if found, undefined otherwise\n */\nexport function findAndReadLomFile(itemPath: string): string | undefined {\n const dir = dirname(itemPath);\n const filename = basename(itemPath);\n const itemId = filename.replace(/\\.xml$/i, '');\n\n // basename() strips every directory separator, so lomFilename cannot carry\n // traversal segments. The joins below stay within dir or its parent, both of\n // which are derived from the item path the operator asked us to convert.\n const lomFilename = `${itemId}.lom.xml`;\n\n // Pattern 1: .../items/<id>.xml → .../metadata/<id>.lom.xml (sibling folder)\n if (dir.endsWith('/items') || dir.endsWith('\\\\items')) {\n const parentDir = dirname(dir);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath = join(parentDir, 'metadata', lomFilename);\n\n if (existsSync(lomPath)) {\n try {\n return readFileSync(lomPath, 'utf-8');\n } catch {\n return undefined;\n }\n }\n }\n\n // Pattern 2: <dir>/<id>.xml → <dir>/metadata/<id>.lom.xml\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath1 = join(dir, 'metadata', lomFilename);\n\n if (existsSync(lomPath1)) {\n try {\n return readFileSync(lomPath1, 'utf-8');\n } catch {\n return undefined;\n }\n }\n\n // Pattern 3: <dir>/<id>.xml → <dir>/../metadata/<id>.lom.xml\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const lomPath2 = join(dir, '..', 'metadata', lomFilename);\n\n if (existsSync(lomPath2)) {\n try {\n return readFileSync(lomPath2, 'utf-8');\n } catch {\n return undefined;\n }\n }\n\n return undefined;\n}\n","/**\n * Image dimension resolver\n *\n * Resolves an image file referenced by a QTI `<object data=\"...\">` /\n * `<img src=\"...\">` tag and reads its intrinsic pixel dimensions directly from\n * the file header. This is a dependency-free reader for the formats commonly\n * found in QTI packages (GIF, PNG, JPEG). It reads only the small header region\n * needed to determine dimensions, not the whole file.\n *\n * QTI legitimately allows an `<object>` without width/height attributes, but\n * some Learnosity question types (e.g. hotspot) need pixel dimensions to convert\n * absolute coordinates into percentages. When the markup omits them, the\n * dimensions are recovered from the referenced image file.\n */\n\nimport { existsSync, closeSync, openSync, readSync, realpathSync } from 'fs';\nimport { dirname, join, isAbsolute, resolve, relative, sep } from 'path';\n\nexport interface ImageDimensions {\n width: number;\n height: number;\n}\n\n/**\n * Only the leading bytes of an image are needed to read its dimensions: GIF and\n * PNG carry them in the first ~24 bytes, and a JPEG's first Start-Of-Frame\n * marker is virtually always within the first segments. Cap the read so a huge\n * asset can never be loaded whole into memory.\n */\nconst MAX_HEADER_BYTES = 64 * 1024;\n\n/**\n * Check that `resolvedCandidate` stays lexically inside `resolvedRoot`.\n *\n * Both arguments must already be absolute/normalised. Returns true when the\n * candidate is contained (and is not the root itself).\n */\nfunction isContained(resolvedCandidate: string, resolvedRoot: string): boolean {\n const rel = relative(resolvedRoot, resolvedCandidate);\n\n return rel !== '' && !isAbsolute(rel) && rel.split(sep)[0] !== '..';\n}\n\n/**\n * Resolve `candidate` and confirm it stays inside `root`, both lexically and\n * after dereferencing symlinks.\n *\n * The `data`/`src` attribute comes from the QTI file being converted, which is\n * untrusted input. A lexical check alone is insufficient: a package could place\n * a symlink under `root` that targets a file outside it (e.g. `/dev/zero`), so\n * the real path is re-verified against the real root before it is returned for\n * reading.\n *\n * @returns The real (symlink-dereferenced) absolute path if contained, else undefined.\n */\nfunction containedPath(candidate: string, root: string): string | undefined {\n // These resolve() calls are the sanitiser, not a sink: normalising both\n // sides is what makes the relative() comparison below meaningful.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(root);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedCandidate = resolve(candidate);\n\n // Lexical containment first (cheap, and rejects obvious \"../\" escapes even\n // when the target does not exist).\n if (!isContained(resolvedCandidate, resolvedRoot)) {\n return undefined;\n }\n\n if (!existsSync(resolvedCandidate)) {\n return undefined;\n }\n\n // Dereference symlinks on both sides and re-verify: this closes the gap\n // where a symlink inside root points outside it. If realpath fails (broken\n // link, race), treat as not resolvable.\n let realCandidate: string;\n let realRoot: string;\n\n try {\n realCandidate = realpathSync(resolvedCandidate);\n realRoot = realpathSync(resolvedRoot);\n } catch {\n return undefined;\n }\n\n if (!isContained(realCandidate, realRoot)) {\n return undefined;\n }\n\n return realCandidate;\n}\n\n/**\n * Read up to {@link MAX_HEADER_BYTES} from the start of a file without loading\n * the whole file into memory. Returns the bytes actually read.\n */\nfunction readHeaderBytes(path: string): Buffer | undefined {\n let fd: number | undefined;\n\n try {\n fd = openSync(path, 'r');\n const buf = Buffer.alloc(MAX_HEADER_BYTES);\n const bytesRead = readSync(fd, buf, 0, MAX_HEADER_BYTES, 0);\n\n return buf.subarray(0, bytesRead);\n } catch {\n return undefined;\n } finally {\n if (fd !== undefined) {\n try {\n closeSync(fd);\n } catch {\n // Best-effort close; nothing actionable if it fails.\n }\n }\n }\n}\n\n/**\n * Read image dimensions from an in-memory file header buffer.\n * Exported for unit testing without touching the filesystem.\n *\n * Supports GIF (87a/89a), PNG, and JPEG (baseline and progressive).\n *\n * @param buf - The image file bytes (at least the header region)\n * @returns The pixel dimensions, or undefined if the format is unrecognised\n */\nexport function readImageDimensionsFromBuffer(buf: Buffer): ImageDimensions | undefined {\n // GIF: \"GIF87a\" or \"GIF89a\"; width/height are little-endian uint16 at offset 6/8.\n if (\n buf.length >= 10 &&\n buf[0] === 0x47 && // G\n buf[1] === 0x49 && // I\n buf[2] === 0x46 // F\n ) {\n const width = buf.readUInt16LE(6);\n const height = buf.readUInt16LE(8);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // PNG: 8-byte signature, then IHDR with width/height as big-endian uint32\n // at offset 16/20.\n if (\n buf.length >= 24 &&\n buf[0] === 0x89 &&\n buf[1] === 0x50 && // P\n buf[2] === 0x4e && // N\n buf[3] === 0x47 // G\n ) {\n const width = buf.readUInt32BE(16);\n const height = buf.readUInt32BE(20);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // JPEG: starts with 0xFFD8. Scan marker segments for a Start Of Frame\n // (SOF0-SOF15, excluding non-SOF markers 0xC4/0xC8/0xCC) which carries\n // height/width as big-endian uint16.\n if (buf.length >= 4 && buf[0] === 0xff && buf[1] === 0xd8) {\n let offset = 2;\n\n while (offset + 9 < buf.length) {\n // A marker is 0xFF followed by a non-0xFF, non-0x00 marker code.\n // JPEG permits any number of 0xFF fill bytes before the marker code,\n // so skip the whole run of 0xFF and land on the marker byte itself.\n if (buf[offset] !== 0xff) {\n offset++;\n continue;\n }\n\n let markerOffset = offset;\n\n while (markerOffset < buf.length && buf[markerOffset] === 0xff) {\n markerOffset++;\n }\n\n if (markerOffset >= buf.length) {\n break;\n }\n\n const marker = buf[markerOffset];\n // The length field (if any) follows the marker code byte.\n const segmentStart = markerOffset + 1;\n\n // 0x00 after 0xFF is a stuffed byte inside entropy-coded data, not a\n // marker; skip past it and keep scanning.\n if (marker === 0x00) {\n offset = segmentStart;\n continue;\n }\n\n // Standalone markers without a length payload.\n if (marker === 0xd8 || marker === 0xd9 || (marker >= 0xd0 && marker <= 0xd7)) {\n offset = segmentStart;\n continue;\n }\n\n if (segmentStart + 1 >= buf.length) {\n break;\n }\n\n const segmentLength = buf.readUInt16BE(segmentStart);\n\n // SOF markers carrying frame dimensions (exclude DHT 0xC4, JPG 0xC8,\n // DAC 0xCC which share the 0xCn range but are not frame headers).\n const isSof =\n marker >= 0xc0 &&\n marker <= 0xcf &&\n marker !== 0xc4 &&\n marker !== 0xc8 &&\n marker !== 0xcc;\n\n if (isSof) {\n // SOF payload after the 2-byte length: precision (1 byte),\n // height (2 bytes), width (2 bytes).\n if (segmentStart + 6 >= buf.length) {\n break;\n }\n\n const height = buf.readUInt16BE(segmentStart + 3);\n const width = buf.readUInt16BE(segmentStart + 5);\n\n if (width > 0 && height > 0) {\n return { width, height };\n }\n\n return undefined;\n }\n\n // Advance past this segment: length field + its payload.\n offset = segmentStart + segmentLength;\n }\n }\n\n return undefined;\n}\n\n/**\n * Resolve and read the intrinsic dimensions of an image referenced by a QTI\n * `<object>`/`<img>` tag.\n *\n * Security model: the `objectDataPath` comes from untrusted QTI markup, so every\n * resolved path is confined to a containment root before it is read.\n *\n * - When `packageRoot` is supplied (the normal case for package conversion), any\n * asset located anywhere inside the package is allowed — including assets a\n * sibling directory up, e.g. `questions/item.xml` referencing\n * `../images/foo.png`. This does not rely on directory names.\n * - When `packageRoot` is omitted (in-memory callers with no package context),\n * the read is confined to the source file's own directory. This is the safe\n * default: no path is read from outside where the item lives.\n *\n * @param objectDataPath - Path from the data/src attribute (e.g. \"754493.gif\")\n * @param sourceFile - Absolute path to the source QTI XML file\n * @param packageRoot - Absolute path to the package root that bounds all reads\n * @returns The image dimensions, or undefined if it cannot be resolved/read.\n */\nexport function resolveImageDimensions(\n objectDataPath: string,\n sourceFile?: string,\n packageRoot?: string\n): ImageDimensions | undefined {\n if (!sourceFile || !objectDataPath) {\n return undefined;\n }\n\n const sourceDir = dirname(sourceFile);\n\n // The containment root: the package root when known (so any in-package asset\n // resolves regardless of directory naming), otherwise the item's own\n // directory. If a packageRoot is given but the source file somehow sits\n // outside it, fall back to the source directory rather than trusting it.\n let root = sourceDir;\n let shouldTryPackageRoot = false;\n\n if (packageRoot) {\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedRoot = resolve(packageRoot);\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n const resolvedSourceDir = resolve(sourceDir);\n const rel = relative(resolvedRoot, resolvedSourceDir);\n const sourceInsideRoot = rel === '' || (!isAbsolute(rel) && rel.split(sep)[0] !== '..');\n\n root = sourceInsideRoot ? resolvedRoot : resolvedSourceDir;\n shouldTryPackageRoot = sourceInsideRoot && resolvedRoot !== resolvedSourceDir;\n }\n\n const candidates: string[] = [];\n\n if (isAbsolute(objectDataPath)) {\n candidates.push(objectDataPath);\n } else {\n // Resolve relative to the item first (most common), then relative to the\n // package root. Both are re-checked for containment before any read.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(sourceDir, objectDataPath));\n\n if (shouldTryPackageRoot) {\n // The candidate is rechecked by containedPath() for lexical and\n // realpath containment before it can reach the filesystem.\n // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal, javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal\n candidates.push(join(root, objectDataPath));\n }\n }\n\n for (const candidate of candidates) {\n // containedPath dereferences symlinks and re-verifies containment, so\n // the returned path is safe to read.\n const imagePath = containedPath(candidate, root);\n\n if (!imagePath) {\n continue;\n }\n\n // Read only a bounded header region: enough for GIF/PNG and to scan the\n // early JPEG marker segments that carry frame dimensions, without\n // loading a potentially very large asset into memory.\n const buf = readHeaderBytes(imagePath);\n\n if (buf) {\n return readImageDimensionsFromBuffer(buf);\n }\n }\n\n return undefined;\n}\n","/**\n * Filesystem-backed {@link FileAccess} for Node.js callers (the CLI and\n * disk-based package conversion). Reads passage HTML and LOM metadata from the\n * local filesystem.\n *\n * This module imports Node built-ins (via the resolver helpers) and is therefore\n * NOT part of the browser-safe surface — it is wired in by Node entry points.\n */\nimport type { FileAccess } from './fileAccess.js';\nimport { resolvePassageFile } from './utils/passageResolver.js';\nimport { findAndReadLomFile } from './utils/lomFinder.js';\nimport { resolveImageDimensions } from './utils/imageDimensions.js';\n\nexport const nodeFileAccess: FileAccess = {\n resolvePassageFile,\n findAndReadLomFile,\n resolveImageDimensions,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,SAASA,gBAAc,WAAmB,MAAkC;CAIxE,MAAM,eAAe,QAAQ,IAAI;CAEjC,MAAM,oBAAoB,QAAQ,SAAS;CAC3C,MAAM,MAAM,SAAS,cAAc,iBAAiB;CAIpD,IAAI,QAAQ,MAAM,WAAW,GAAG,KAAK,IAAI,MAAM,GAAG,CAAC,CAAC,OAAO,MACvD;CAGJ,OAAO;AACX;;;;;;;;AASA,SAAgB,mBACZ,gBACA,YACkB;CAClB,IAAI,CAAC,cAAc,CAAC,gBAChB;CAIJ,MAAM,YAAY,QAAQ,UAAU;CAKpC,MAAM,OAAO,QAAQ,SAAS;CAK9B,MAAM,aAAuB,CAAC;CAE9B,IAAI,WAAW,cAAc,GACzB,WAAW,KAAK,cAAc;MAC3B;EAEH,WAAW,KAAK,KAAK,WAAW,cAAc,CAAC;EAE/C,WAAW,KA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