// A small XML reader for S3 responses: elements, text, CDATA and every XML character reference // (`&`, `"`, `"`, ...). Attributes are skipped (S3 answers carry only `xmlns`), so are // the prolog and comments; a DOCTYPE is refused. Namespace prefixes are dropped from names. export interface XmlElement { name: string children: XmlElement[] text: string } const NAMED: Record = { amp: '&', lt: '<', gt: '>', quot: '"', apos: '\'' } export function decodeEntities(s: string): string { return s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);|&/gi, (whole, ref: string | undefined) => { if (ref === undefined) throw new Error('XML: a bare "&"') if (ref[0] !== '#') { const named = NAMED[ref] if (named === undefined) throw new Error(`XML: unknown entity ${whole}`) return named } const point = ref[1] === 'x' || ref[1] === 'X' ? Number.parseInt(ref.slice(2), 16) : Number.parseInt(ref.slice(1), 10) if (!(point >= 0 && point <= 0x10FFFF)) throw new Error(`XML: bad character reference ${whole}`) return String.fromCodePoint(point) }) } function localName(name: string): string { const colon = name.indexOf(':') return colon === -1 ? name : name.slice(colon + 1) } /** The end of the tag that starts at `from` (the index of its `>`), skipping quoted attribute values. */ function tagEnd(src: string, from: number): number { let quote = '' for (let i = from; i < src.length; i++) { const c = src[i] if (quote) { if (c === quote) quote = '' } else if (c === '"' || c === '\'') { quote = c } else if (c === '>') { return i } } throw new Error('XML: unterminated tag') } function skipTo(src: string, from: number, end: string): number { const at = src.indexOf(end, from) if (at === -1) throw new Error(`XML: missing ${end}`) return at + end.length } /** The document's root element. Throws on anything that isn't well-formed. */ export function parseXml(src: string): XmlElement { const stack: XmlElement[] = [] let root: XmlElement | null = null let i = 0 const addText = (text: string): void => { const top = stack.at(-1) if (top) top.text += text else if (text.trim() !== '') throw new Error('XML: text outside the root element') } while (i < src.length) { const lt = src.indexOf('<', i) if (lt === -1) { addText(decodeEntities(src.slice(i))) break } if (lt > i) addText(decodeEntities(src.slice(i, lt))) if (src.startsWith('') } else if (src.startsWith('') } else if (src.startsWith('') addText(src.slice(lt + 9, end - 3)) i = end } else if (src.startsWith('`) i = end + 1 } else { const end = tagEnd(src, lt) const selfClosing = src[end - 1] === '/' const inner = src.slice(lt + 1, selfClosing ? end - 1 : end) const name = localName(inner.split(/[\s/]/, 1)[0]) if (!name) throw new Error('XML: empty tag name') const element: XmlElement = { name, children: [], text: '' } const parent = stack.at(-1) if (parent) parent.children.push(element) else if (root) throw new Error('XML: more than one root element') else root = element if (!selfClosing) stack.push(element) i = end + 1 } } if (stack.length > 0) throw new Error(`XML: <${stack.at(-1)?.name}> is not closed`) if (!root) throw new Error('XML: no root element') return root } export function children(element: XmlElement, name: string): XmlElement[] { return element.children.filter(c => c.name === name) } export function child(element: XmlElement, name: string): XmlElement | undefined { return element.children.find(c => c.name === name) } /** The text of the first child named `name`, untrimmed (keys may end in spaces). */ export function childText(element: XmlElement, name: string): string | undefined { return child(element, name)?.text } export function escapeXml(s: string): string { return s.replace(/[&<>"']/g, c => `&${{ '&': 'amp', '<': 'lt', '>': 'gt', '"': 'quot', '\'': 'apos' }[c]};`) }