@mdgate/containers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @mdgate/containers
2
+
3
+ ZIP/OPC package reading, OLE compound files (CFB), a namespace-aware XML parser, and container-level format detection.
4
+
5
+ Internal shared library for the mdgate converters. Install a converter
6
+ package (for example `@mdgate/docx`) or the `@mdgate/converters` bundle
7
+ instead of depending on this directly.
@@ -0,0 +1,33 @@
1
+ import { ConvertError } from '@mdgate/core';
2
+ import { type Element } from './xml.js';
3
+ /** A ZIP-based document package (OOXML, ODF, EPUB). */
4
+ export declare class Package {
5
+ private readonly zip;
6
+ private totalRead;
7
+ private readonly cache;
8
+ private constructor();
9
+ static open(bytes: Uint8Array): Package;
10
+ /**
11
+ * Read a part's bytes. `undefined` means the part is absent; throws when
12
+ * it exists but cannot be read.
13
+ */
14
+ part(name: string): Uint8Array | undefined;
15
+ /** True when a part exists, without reading (or budget-charging) it. */
16
+ hasPart(name: string): boolean;
17
+ /** Part names present in the archive (no leading slash). */
18
+ partNames(): string[];
19
+ requiredPart(name: string): Uint8Array;
20
+ /**
21
+ * Absent is valid (`undefined`, silent); an unreadable part is skipped
22
+ * with a log; fatal resource-limit errors always propagate.
23
+ */
24
+ optionalPart(name: string): Uint8Array | undefined;
25
+ optionalXmlPart(name: string): Element | undefined;
26
+ requiredXmlPart(name: string): Element;
27
+ private decompress;
28
+ }
29
+ /**
30
+ * A zip-open failure on OOXML input may actually be an OLE compound file:
31
+ * an encrypted package, or a legacy binary document with the wrong extension.
32
+ */
33
+ export declare function probeOle(bytes: Uint8Array): ConvertError | undefined;
@@ -0,0 +1,12 @@
1
+ import type { CompoundFile } from './cfb.js';
2
+ import { MAX_ENTRY_BYTES } from './limits.js';
3
+ /** Little-endian `u16` at `off`; `undefined` when out of bounds. */
4
+ export declare function getU16(b: Uint8Array, off: number): number | undefined;
5
+ /** Little-endian `u32` at `off`; `undefined` when out of bounds. */
6
+ export declare function getU32(b: Uint8Array, off: number): number | undefined;
7
+ /**
8
+ * Read a named stream from an OLE2 compound file. A missing stream is
9
+ * `missingPart`; the read is hard-capped at `MAX_ENTRY_BYTES`.
10
+ */
11
+ export declare function readOleStream(ole: CompoundFile, name: string): Uint8Array;
12
+ export { MAX_ENTRY_BYTES };
package/dist/cfb.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ declare const NOSTREAM = 4294967295;
2
+ declare const TYPE_STORAGE = 1;
3
+ declare const TYPE_STREAM = 2;
4
+ /** OLE2 compound file (MS-CFB). */
5
+ export declare class CompoundFile {
6
+ private readonly bytes;
7
+ private readonly sectorSize;
8
+ private readonly miniSectorSize;
9
+ private readonly miniCutoff;
10
+ private readonly fat;
11
+ private readonly miniFat;
12
+ private readonly entries;
13
+ private miniStream;
14
+ private constructor();
15
+ static open(bytes: Uint8Array): CompoundFile;
16
+ /** Children of the root storage, in-order. */
17
+ readRootStorage(): {
18
+ name: string;
19
+ type: number;
20
+ }[];
21
+ exists(path: string): boolean;
22
+ readStream(name: string): Uint8Array;
23
+ private lookup;
24
+ private findChild;
25
+ private walkSiblings;
26
+ private readEntryBytes;
27
+ private readMiniChain;
28
+ }
29
+ export declare const OLE_MAGIC_BYTES: number[];
30
+ export declare function hasOleMagic(bytes: Uint8Array): boolean;
31
+ export { NOSTREAM, TYPE_STORAGE, TYPE_STREAM };
@@ -0,0 +1,15 @@
1
+ /** What a ZIP-based document container holds. */
2
+ export type ZipDocKind = 'docx' | 'pptx' | 'xlsx' | 'odt' | 'ods' | 'odp' | 'epub' | 'pages' | 'numbers' | 'keynote';
3
+ /** What an OLE compound file holds. */
4
+ export type OleDocKind = 'doc' | 'ppt' | 'xls';
5
+ /**
6
+ * Classify a ZIP-based document container by its content. Returns undefined
7
+ * for non-ZIP bytes and unrecognized packages. Never throws.
8
+ */
9
+ export declare function detectZipDoc(bytes: Uint8Array): ZipDocKind | undefined;
10
+ /**
11
+ * Classify an OLE compound file by its mandated content stream. Returns
12
+ * undefined for non-OLE bytes and for encrypted OOXML packages (the inner
13
+ * format is unknowable). Never throws.
14
+ */
15
+ export declare function detectOleDoc(bytes: Uint8Array): OleDocKind | undefined;
@@ -0,0 +1,8 @@
1
+ export { Package, probeOle } from './archive.js';
2
+ export { getU16, getU32, readOleStream } from './binary.js';
3
+ export { CompoundFile, hasOleMagic, OLE_MAGIC_BYTES } from './cfb.js';
4
+ export { detectOleDoc, detectZipDoc, type OleDocKind, type ZipDocKind, } from './detect.js';
5
+ export { MAX_ENTRY_BYTES, MAX_ENTRY_COUNT, MAX_RECORD_DEPTH, MAX_RECORDS, MAX_TOTAL_BYTES, MAX_XML_DEPTH, MAX_XML_NODES, } from './limits.js';
6
+ export { decodeFragment, resolve, type Target } from './path.js';
7
+ export { type Relationship, Relationships, type RelTarget, readRels, relsPartFor, relTargetBytes, relType, type TargetMode, } from './relationships.js';
8
+ export { type Attr, Element, normalizeOoxmlUri, ns, parseXml, type XmlNode, } from './xml.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import{ConvertError as P}from"@mdgate/core";import{decode as U6,InflateLimitError as V6,inflateRaw as Y6,warn as T1}from"@mdgate/utils";import{ConvertError as _}from"@mdgate/core";var L=134217728,$1=536870912,m=1e5,c=256,i=2000000,y1=64,f1=16000000;var W1=[208,207,17,224,161,177,26,225],n=4294967295,R1=4294967294,d1=4294967293,m1=4294967292,c1=4294967295,N=4294967290,F1=0;var L1=2,i1=5;class z{bytes;sectorSize;miniSectorSize;miniCutoff;fat;miniFat;entries;miniStream;constructor(J,Q,$,K,W,Z,G){this.bytes=J,this.sectorSize=Q,this.miniSectorSize=$,this.miniCutoff=K,this.fat=W,this.miniFat=Z,this.entries=G}static open(J){if(J.length<512||!M1(J,W1))throw _.malformed("not an OLE2 compound file");let Q=new DataView(J.buffer,J.byteOffset,J.byteLength);if(Q.getUint16(28,!0)!==65534)throw _.malformed("not an OLE2 compound file: bad byte order");let K=Q.getUint16(30,!0);if(K!==9&&K!==12)throw _.malformed("not an OLE2 compound file: bad sector shift");let W=1<<K,Z=Q.getUint16(32,!0),G=Z===0?64:1<<Z,H=Q.getUint16(26,!0),U=Q.getUint32(44,!0),D=Q.getUint32(48,!0),V=Q.getUint32(56,!0)||4096,Y=Q.getUint32(60,!0),X=Q.getUint32(64,!0),h=Q.getUint32(68,!0),k=Q.getUint32(72,!0),F=[];for(let j=0;j<109;j+=1){let q=Q.getUint32(76+j*4,!0);if(q<=N)F.push(q)}let w=h,T=new Set;for(let j=0;j<k&&w<=N;j+=1){if(T.has(w))break;T.add(w);let q=K1(w,W),O=W/4;if(q+W>J.length)break;let d=new DataView(J.buffer,J.byteOffset+q,W);for(let v=0;v<O-1;v+=1){let C=d.getUint32(v*4,!0);if(C<=N)F.push(C)}w=d.getUint32((O-1)*4,!0)}if(F.length<U);let B=[],Q1=Math.min(F.length,U||F.length,1e6);for(let j=0;j<Q1;j+=1){let q=F[j],O=K1(q,W);if(O+W>J.length)break;let d=new DataView(J.buffer,J.byteOffset+O,W),v=W/4;for(let C=0;C<v;C+=1)B.push(d.getUint32(C*4,!0))}let I=Math.max(0,Math.floor((J.length+W-1)/W)-1);while(B.length>I){let j=B[B.length-1];if(j===0||j===m1||j===d1||j===n)B.pop();else break}while(B.length>I&&B[B.length-1]===n)B.pop();if(B.length>I)throw _.malformed(`not an OLE2 compound file: Malformed FAT (FAT has ${B.length} entries, but file has only ${I} sectors)`);while(B.length<I)B.push(n);let y=[];if(X>0&&Y<=N){let j=o(J,B,W,Y,X*W),q=new DataView(j.buffer,j.byteOffset,j.byteLength);for(let O=0;O+4<=j.length;O+=4)y.push(q.getUint32(O,!0))}let f=o(J,B,W,D,L),S=[];for(let j=0;j+128<=f.length;j+=128)if(S.push(a1(f,j,H)),S.length>1e5)break;if(S.length===0||S[0].type!==i1)throw _.malformed("not an OLE2 compound file: missing root storage");return new z(J,W,G,V,B,y,S)}readRootStorage(){let J=this.entries[0];if(J===void 0)return[];let Q=[];return this.walkSiblings(J.child,Q),Q}exists(J){return this.lookup(J)!==void 0}readStream(J){let Q=this.lookup(J);if(Q===void 0||Q.type!==L1)throw _.missingPart(J);if(Q.size>L)throw _.resourceLimit("max_entry_bytes",`${J} stream exceeds the read cap`);let $=this.readEntryBytes(Q,L+1);if($.length>L)throw _.resourceLimit("max_entry_bytes",`${J} stream exceeds the read cap`);return $}lookup(J){let Q=J.split("/").filter((W)=>W.length>0);if(Q.length===0)return this.entries[0];let $=this.entries[0]?.child??c1,K;for(let W of Q){if(K=this.findChild($,W),K===void 0)return;$=K.child}return K}findChild(J,Q){let $=new Set,K=[J];while(K.length>0){let W=K.pop();if(W>N||$.has(W))continue;$.add(W);let Z=this.entries[W];if(Z===void 0||Z.type===F1)continue;if(n1(Z.name,Q))return Z;K.push(Z.left,Z.right)}return}walkSiblings(J,Q){let $=new Set,K=(W)=>{if(W>N||$.has(W)||$.size>1e5)return;$.add(W);let Z=this.entries[W];if(Z===void 0||Z.type===F1)return;K(Z.left),Q.push({name:Z.name,type:Z.type}),K(Z.right)};K(J)}readEntryBytes(J,Q){let $=Math.min(J.size,Q);if(J.size<this.miniCutoff&&J.type===L1)return this.readMiniChain(J.start,$);return o(this.bytes,this.fat,this.sectorSize,J.start,$)}readMiniChain(J,Q){if(this.miniStream===void 0){let G=this.entries[0];this.miniStream=o(this.bytes,this.fat,this.sectorSize,G.start,G.size)}let $=new Uint8Array(Q),K=0,W=J,Z=new Set;while(K<Q&&W<=N){if(Z.has(W))break;Z.add(W);let G=W*this.miniSectorSize,H=Math.min(this.miniSectorSize,Q-K);if(G+H>this.miniStream.length)break;$.set(this.miniStream.subarray(G,G+H),K),K+=H,W=this.miniFat[W]??R1}return K===Q?$:$.subarray(0,K)}}var o1=W1;function E(J){return M1(J,W1)}function M1(J,Q){if(J.length<Q.length)return!1;for(let $=0;$<Q.length;$+=1)if(J[$]!==Q[$])return!1;return!0}function n1(J,Q){return J.length===Q.length&&J.toLowerCase()===Q.toLowerCase()}function K1(J,Q){return(J+1)*Q}function o(J,Q,$,K,W){if(K>N||W<=0)return new Uint8Array(0);let Z=[],G=0,H=K,U=new Set;while(G<W&&H<=N){if(U.has(H)||U.size>1e6)break;U.add(H);let D=K1(H,$);if(D>=J.length)break;let V=Math.min($,W-G,J.length-D);if(V<=0)break;if(Z.push(J.subarray(D,D+V)),G+=V,H=Q[H]??R1,H===n)break}return s1(Z,G)}function a1(J,Q,$){let K=new DataView(J.buffer,J.byteOffset+Q,128),W=K.getUint16(64,!0),Z=Math.max(0,Math.min(64,W>0?W-2:0)),G=new TextDecoder("utf-16le").decode(J.subarray(Q,Q+Z)),H=J[Q+66]??0,U=K.getUint32(68,!0),D=K.getUint32(72,!0),V=K.getUint32(76,!0),Y=K.getUint32(116,!0),X=Number(K.getBigUint64(120,!0));if($===3)X=K.getUint32(120,!0);if(!Number.isFinite(X)||X<0)X=0;return{name:G,type:H,left:U,right:D,child:V,start:Y,size:X}}function s1(J,Q){if(J.length===1&&J[0].length===Q)return J[0];let $=new Uint8Array(Q),K=0;for(let W of J)$.set(W,K),K+=W.length;return $}import{ConvertError as G1}from"@mdgate/core";import{debug as I1,decode as r1,encodingExists as t1,warn as e1}from"@mdgate/utils";var A={W:"http://schemas.openxmlformats.org/wordprocessingml/2006/main",R:"http://schemas.openxmlformats.org/officeDocument/2006/relationships",A:"http://schemas.openxmlformats.org/drawingml/2006/main",PIC:"http://schemas.openxmlformats.org/drawingml/2006/picture",WP:"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",MC:"http://schemas.openxmlformats.org/markup-compatibility/2006",CHART:"http://schemas.openxmlformats.org/drawingml/2006/chart",DGM:"http://schemas.openxmlformats.org/drawingml/2006/diagram",P:"http://schemas.openxmlformats.org/presentationml/2006/main",PKG_RELS:"http://schemas.openxmlformats.org/package/2006/relationships",OFFICE:"urn:oasis:names:tc:opendocument:xmlns:office:1.0",TEXT:"urn:oasis:names:tc:opendocument:xmlns:text:1.0",TABLE:"urn:oasis:names:tc:opendocument:xmlns:table:1.0",DRAW:"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",STYLE:"urn:oasis:names:tc:opendocument:xmlns:style:1.0",FO:"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",PRESENTATION:"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",MANIFEST:"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",XLINK:"http://www.w3.org/1999/xlink",XML:"http://www.w3.org/XML/1998/namespace",SVG_COMPAT:"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",VML:"urn:schemas-microsoft-com:vml",O_VML:"urn:schemas-microsoft-com:office:office",WPS:"http://schemas.microsoft.com/office/word/2010/wordprocessingShape",WPG:"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"},J6=A.XML,Q6="http://www.w3.org/2000/xmlns/",N1=[];class r{ns;local;attrs;children;_elems;constructor(J,Q,$=N1,K=[]){this.ns=J,this.local=Q,this.attrs=$,this.children=K}is(J,Q){return this.local===Q&&this.ns===J}attr(J,Q){return this.attrQualified(J,Q)??this.attrUnqualified(Q)}attrQualified(J,Q){let $=this.attrs;for(let K=0;K<$.length;K+=1){let W=$[K];if(W.local===Q&&W.ns===J)return W.value}return}attrUnqualified(J){let Q=this.attrs;for(let $=0;$<Q.length;$+=1){let K=Q[$];if(K.local===J&&K.ns===void 0)return K.value}return}attrAny(J){let Q=this.attrs;for(let $=0;$<Q.length;$+=1){let K=Q[$];if(K.local===J)return K.value}return}childElems(){if(this._elems!==void 0)return this._elems;let J=[],Q=this.children;for(let $=0;$<Q.length;$+=1){let K=Q[$];if(K.type==="elem")J.push(K.elem)}return this._elems=J,J}find(J,Q){let $=this.children;for(let K=0;K<$.length;K+=1){let W=$[K];if(W.type==="elem"&&W.elem.local===Q&&W.elem.ns===J)return W.elem}return}findAll(J,Q){let $=[],K=this.children;for(let W=0;W<K.length;W+=1){let Z=K[W];if(Z.type==="elem"&&Z.elem.local===Q&&Z.elem.ns===J)$.push(Z.elem)}return $}*descendantNodes(){let J=this.children.slice();J.reverse();while(J.length>0){let Q=J.pop();if(Q.type==="elem"){let $=J.length;for(let K of Q.elem.children)J.push(K);G6(J,$)}yield Q}}*descendantElems(){for(let J of this.descendantNodes())if(J.type==="elem")yield J.elem}firstDescendant(J,Q){for(let $ of this.descendantElems())if($.is(J,Q))return $;return}*descendants(J,Q){for(let $ of this.descendantElems())if($.is(J,Q))yield $}*descendantsAny(J){for(let Q of this.descendantElems())if(Q.local===J)yield Q}text(){let J="";for(let Q of this.descendantNodes())if(Q.type==="text")J+=Q.text;return J}}class w1{prefixes=[new Map];ownsMap=[!0];defaultNs=[void 0];push(){this.prefixes.push(this.prefixes[this.prefixes.length-1]),this.ownsMap.push(!1),this.defaultNs.push(this.defaultNs[this.defaultNs.length-1])}pop(){this.prefixes.pop(),this.ownsMap.pop(),this.defaultNs.pop()}applyDecls(J){for(let Q of J)if(Q.rawName==="xmlns")this.defaultNs[this.defaultNs.length-1]=Q.value.length===0?void 0:Q.value;else if(Q.rawName.startsWith("xmlns:")){if(!this.ownsMap[this.ownsMap.length-1])this.prefixes[this.prefixes.length-1]=new Map(this.prefixes[this.prefixes.length-1]),this.ownsMap[this.ownsMap.length-1]=!0;let $=Q.rawName.slice(6);this.prefixes[this.prefixes.length-1].set($,Q.value.length===0?void 0:Q.value)}}lookupPrefix(J){if(J==="xml")return J6;if(J==="xmlns")return Q6;return this.prefixes[this.prefixes.length-1].get(J)}resolveElement(J){let Q=J.indexOf(":");if(Q>=0){let K=J.slice(0,Q),W=J.slice(Q+1),Z=this.lookupPrefix(K);return{ns:Z,local:W,bound:Z!==void 0||K==="xml"||K==="xmlns"}}let $=this.defaultNs[this.defaultNs.length-1];return{ns:$,local:J,bound:$!==void 0}}resolveAttribute(J){let Q=J.indexOf(":");if(Q>=0){let $=J.slice(0,Q),K=J.slice(Q+1);return{ns:this.lookupPrefix($),local:K}}return{ns:void 0,local:J}}}function u(J){let Q=D6(J,"http://purl.oclc.org/ooxml/");if(Q===void 0)return;let $=Q.indexOf("/");if($<0)return;let K=Q.slice(0,$),W=Q.slice($+1);return`http://schemas.openxmlformats.org/${K}/2006/${W}`}var _1=0,A1=1,k1=2,p=3,D1=4,z1=5;function t(J){let Q=K6(J),$=new g1(Q),K=new w1,W=new Map,Z=new Map,G=new r(void 0,""),H=[],U=0,D=!1;for(;;){let V;try{V=$.next()}catch(Y){let X=Y instanceof Error?Y.message:String(Y);throw G1.malformed(`unparseable xml: ${X}`)}switch(V){case A1:{if(!$.empty&&H.length>=c)throw G1.resourceLimit("max_xml_depth",`element nesting exceeds ${c}`);a(U),U+=1,K.push(),K.applyDecls($.attrs);let Y=$6($.name,$.attrs,K,W,Z);if($.empty)Z1(H,G,{type:"elem",elem:Y}),K.pop();else H.push(Y);break}case k1:{let Y=H.pop();if(Y===void 0)D=!0;else{if(Y.local!==$.local)D=!0;Z1(H,G,{type:"elem",elem:Y}),K.pop()}break}case p:{if($.text.length>0)a(U),U+=1,H1(H,G,$.text);break}case D1:{let Y=U1($.name)??`&${$.name};`;a(U),U+=1,H1(H,G,Y);break}case z1:{a(U),U+=1,H1(H,G,$.text);break}case _1:if(H.length>0){D=!0;while(H.length>0)Z1(H,G,{type:"elem",elem:H.pop()})}if(D)e1("recovered malformed xml (unclosed or mismatched elements)");return G}}}function a(J){if(J+1>i)throw G1.resourceLimit("max_xml_nodes",`part exceeds ${i} xml nodes`)}function Z1(J,Q,$){let K=J[J.length-1];if(K!==void 0)K.children.push($);else Q.children.push($)}function H1(J,Q,$){let K=J[J.length-1]?.children??Q.children,W=K[K.length-1];if(W?.type==="text")W.text+=$;else K.push({type:"text",text:$})}function q1(J,Q){let $=J.get(Q);if($!==void 0)return $;let K=u(Q)??Q;return J.set(Q,K),K}function O1(J,Q){let $=J.get(Q);if($!==void 0)return $;return J.set(Q,Q),Q}function $6(J,Q,$,K,W){let Z=$.resolveElement(J),G=Z.ns!==void 0?q1(K,Z.ns):void 0,H=N1,U=!1;for(let D of Q){if(D.rawName==="xmlns"||D.rawName.startsWith("xmlns:"))continue;let V=$.resolveAttribute(D.rawName);if(!U)H=[],U=!0;H.push({ns:V.ns!==void 0?q1(K,V.ns):void 0,local:O1(W,V.local),value:D.value})}if(Z.local==="Choice"&&G===A.MC){let D=H.find((V)=>V.local==="Requires");if(D!==void 0)D.value=D.value.split(/\s+/).filter((V)=>V.length>0).map((V)=>{let Y=$.resolveElement(`${V}:x`);if(Y.bound&&Y.ns!==void 0)return u(Y.ns)??Y.ns;return V}).join(" ")}return new r(G,O1(W,Z.local),H)}function K6(J){if(J.length>=2&&J[0]===255&&J[1]===254)return new TextDecoder("utf-16le").decode(J);if(J.length>=2&&J[0]===254&&J[1]===255)return new TextDecoder("utf-16be").decode(J);if(J.length>=3&&J[0]===239&&J[1]===187&&J[2]===191)return new TextDecoder("utf-8").decode(J.subarray(3));let Q=J.subarray(0,Math.min(J.length,200)),$=W6(Q);if($!==void 0){let K=Z6($);if(K!==void 0&&K!=="utf8"&&K!=="utf-8")return r1(J,K)}return new TextDecoder("utf-8").decode(J)}function W6(J){let Q;try{Q=new TextDecoder("utf-8",{fatal:!0}).decode(J)}catch{return}let $=Q.indexOf("encoding");if($<0)return;let K=Q.slice($+8).trimStart();if(!K.startsWith("="))return;K=K.slice(1).trimStart();let W=K[0];if(W!=='"'&&W!=="'")return;K=K.slice(1);let Z=K.indexOf(W);if(Z<0)return;return K.slice(0,Z)}function Z6(J){let Q=J.trim().toLowerCase().replace(/_/g,"-"),K={"utf-8":"utf8",utf8:"utf8","utf-16":"utf16-le","utf-16le":"utf16-le","utf-16be":"utf16-be","iso-8859-1":"iso-8859-1","latin-1":"iso-8859-1",latin1:"iso-8859-1","windows-1252":"windows-1252","cp-1252":"windows-1252",cp1252:"windows-1252","windows-1251":"windows-1251","windows-1250":"windows-1250","windows-874":"windows-874",gbk:"gbk",gb2312:"gb2312",gb18030:"gb18030",big5:"big5","shift-jis":"shiftjis",shiftjis:"shiftjis","euc-jp":"euc-jp","euc-kr":"euc-kr",koi8r:"koi8-r","koi8-r":"koi8-r"}[Q]??Q;return t1(K)?K:void 0}function U1(J){if(J.startsWith("#")){let $=J.slice(1),K=$.startsWith("x")||$.startsWith("X"),W=K?$.slice(1):$,Z=Number.parseInt(W,K?16:10);if(!Number.isFinite(Z))return;try{return String.fromCodePoint(Z)}catch{return}}return{amp:"&",lt:"<",gt:">",apos:"'",quot:'"',nbsp:" ",shy:"­",mdash:"—",ndash:"–",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",hellip:"…",copy:"©",reg:"®",trade:"™",deg:"°",middot:"·",bull:"•",sect:"§",para:"¶",laquo:"«",raquo:"»",times:"×",divide:"÷",plusmn:"±",frac12:"½",frac14:"¼",eacute:"é",egrave:"è",agrave:"à",ccedil:"ç",uuml:"ü",ouml:"ö",auml:"ä",szlig:"ß",aring:"å",oslash:"ø",aelig:"æ",euro:"€",pound:"£",yen:"¥",cent:"¢"}[J]}class g1{s;i=0;name="";local="";text="";empty=!1;attrs=[];constructor(J){this.s=J}next(){if(this.i>=this.s.length)return _1;if(this.s.charCodeAt(this.i)===60)return this.markup();return this.textRun()}markup(){if(this.startsWith("<?"))return this.skipUntil("?>"),this.next();if(this.startsWith("<!--")){this.i+=4;let J=this.s.indexOf("-->",this.i);if(J<0)throw Error("unterminated comment");return this.i=J+3,this.next()}if(this.startsWith("<![CDATA[")){this.i+=9;let J=this.s.indexOf("]]>",this.i);if(J<0)throw Error("unterminated cdata");return this.text=this.s.slice(this.i,J),this.i=J+3,z1}if(this.startsWith("<!"))return this.skipDoctype(),this.next();if(this.startsWith("</")){this.i+=2;let J=this.readName();if(this.skipWs(),this.s[this.i]===">")this.i+=1;return this.local=H6(J),k1}if(this.i+=1,this.name=this.readName(),this.attrs=this.readAttrs(),this.skipWs(),this.empty=!1,this.s[this.i]==="/"&&this.s[this.i+1]===">")this.i+=2,this.empty=!0;else if(this.s[this.i]===">")this.i+=1;return A1}textRun(){let J=this.i;while(this.i<this.s.length){let Q=this.s.charCodeAt(this.i);if(Q===60)break;if(Q===38){if(this.i>J)return this.text=this.s.slice(J,this.i),p;let $=this.readReference();if($.type==="general")return this.name=$.name,D1;return this.i=J,this.textRunSlow()}this.i+=1}return this.text=this.s.slice(J,this.i),p}textRunSlow(){let J="";while(this.i<this.s.length){let Q=this.s.charCodeAt(this.i);if(Q===60)break;if(Q===38){let K=this.readReference();if(K.type==="general"){if(J.length>0)return this.i=K.rewind,this.text=J,p;return this.name=K.name,D1}J+=K.text;continue}let $=this.i;this.i+=1;while(this.i<this.s.length){let K=this.s.charCodeAt(this.i);if(K===60||K===38)break;this.i+=1}J+=this.s.slice($,this.i)}return this.text=J,p}readReference(){let J=this.i;this.i+=1;let Q=this.s.indexOf(";",this.i);if(Q<0)return{type:"char",text:"&"};let $=this.s.slice(this.i,Q);if(this.i=Q+1,$.startsWith("#")||$==="amp"||$==="lt"||$==="gt"||$==="apos"||$==="quot")return{type:"char",text:U1($)??`&${$};`};return{type:"general",name:$,rewind:J}}readName(){let J=this.i;while(this.i<this.s.length){let Q=this.s.charCodeAt(this.i);if(Q===47||Q===62||Q===61||Q===32||Q===9||Q===10||Q===13)break;this.i+=1}return this.s.slice(J,this.i)}readAttrs(){let J=[];for(;;){this.skipWs();let Q=this.s[this.i];if(Q===void 0||Q===">"||Q==="/")break;let $=this.readName();if($.length===0){this.i+=1;continue}if(this.skipWs(),this.s[this.i]!=="="){I1(`dropping undecodable attribute: ${$}`);continue}this.i+=1,this.skipWs();let K=this.readAttrValue();if(K===void 0){I1(`dropping undecodable attribute: ${$}`);continue}J.push({rawName:$,value:K})}return J}readAttrValue(){let J=this.s.charCodeAt(this.i);if(J!==34&&J!==39)return;this.i+=1;let Q=this.i,$=!1;while(this.i<this.s.length){let W=this.s.charCodeAt(this.i);if(W===J){let Z=this.s.slice(Q,this.i);return this.i+=1,$?s(Z):Z}if(W===38)return this.i=Q,this.readAttrValueSlow(J);if(W===9||W===10||W===13)$=!0;this.i+=1}let K=this.s.slice(Q,this.i);return $?s(K):K}readAttrValueSlow(J){let Q="";while(this.i<this.s.length){let $=this.s.charCodeAt(this.i);if($===J)return this.i+=1,s(Q);if($===38){let W=this.readReference();if(W.type==="char")Q+=W.text;else Q+=U1(W.name)??`&${W.name};`;continue}let K=this.i;this.i+=1;while(this.i<this.s.length){let W=this.s.charCodeAt(this.i);if(W===J||W===38)break;this.i+=1}Q+=this.s.slice(K,this.i)}return s(Q)}skipWs(){while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J!==32&&J!==9&&J!==10&&J!==13)break;this.i+=1}}startsWith(J){return this.s.startsWith(J,this.i)}skipUntil(J){let Q=this.s.indexOf(J,this.i);this.i=Q<0?this.s.length:Q+J.length}skipDoctype(){this.i+=2;let J=0;while(this.i<this.s.length){let Q=this.s[this.i];if(Q==='"'||Q==="'"){let $=Q;this.i+=1;while(this.i<this.s.length&&this.s[this.i]!==$)this.i+=1;if(this.i<this.s.length)this.i+=1;continue}if(Q==="<")J+=1;else if(Q===">"){if(J===0){this.i+=1;return}J-=1}this.i+=1}}}function H6(J){let Q=J.indexOf(":");return Q<0?J:J.slice(Q+1)}function s(J){if(J.indexOf("\t")<0&&J.indexOf(`
2
+ `)<0&&J.indexOf("\r")<0)return J;return J.replace(/[\t\n\r]/g," ")}function G6(J,Q){let $=Q,K=J.length-1;while($<K){let W=J[$];J[$]=J[K],J[K]=W,$+=1,K-=1}}function D6(J,Q){return J.startsWith(Q)?J.slice(Q.length):void 0}var j6=67324752,B6=33639248;var P6=101075792,X6=117853008,F6=0,L6=8;class V1{length;bytes;byName=new Map;constructor(J,Q){this.bytes=J,this.length=Q.length;for(let $ of Q)if(!this.byName.has($.name))this.byName.set($.name,$)}static open(J){let Q=I6(J);if(Q<0)throw P.malformed("not a readable zip archive: invalid Zip archive: Could not find EOCD");let $=e(J),K=R($,Q+10),W=M($,Q+12),Z=M($,Q+16);if(K===65535||W===4294967295||Z===4294967295){let D=q6(J,Q);if(D!==void 0)K=D.entryCount,W=D.cdSize,Z=D.cdOffset}if(K>m);if(Z+W>J.length)throw P.malformed("not a readable zip archive: truncated central directory");let G=[],H=Z,U=Math.min(J.length,Z+W);while(H+46<=U&&G.length<K){if(M($,H)!==B6)throw P.malformed("not a readable zip archive: bad central directory");let D=R($,H+8),V=R($,H+10),Y=M($,H+16),X=M($,H+20),h=M($,H+24),k=R($,H+28),F=R($,H+30),w=R($,H+32),T=M($,H+42),B=J.subarray(H+46,H+46+k);if(H+46+k+F+w>J.length)throw P.malformed("not a readable zip archive: truncated entry");let Q1=J.subarray(H+46+k,H+46+k+F),I=O6(Q1,{uncompressedSize:h,compressedSize:X,headerOffset:T});h=I.uncompressedSize,X=I.compressedSize,T=I.headerOffset;let y=(D&2048)!==0,f=N6(B,y);G.push({name:f,method:V,flags:D,crc:Y,compressedSize:X,uncompressedSize:h,headerOffset:T}),H+=46+k+F+w}return new V1(J,G)}indexForName(J){return this.byName.get(J)}partNames(){return[...this.byName.keys()]}readCompressed(J){let Q=e(this.bytes),$=J.headerOffset;if($+30>this.bytes.length||M(Q,$)!==j6)throw Error("bad local file header");let K=R(Q,$+26),W=R(Q,$+28),Z=$+30+K+W,G=Z+J.compressedSize;if(Z>this.bytes.length)throw Error("truncated local file header");if(J.compressedSize>0&&G>this.bytes.length)throw Error("truncated compressed data");return this.bytes.subarray(Z,Math.min(G,this.bytes.length))}}class l{zip;totalRead=0;cache=new Map;constructor(J){this.zip=J}static open(J){let Q;try{Q=V1.open(J)}catch($){if($ instanceof P)throw $;let K=$ instanceof Error?$.message:String($);throw P.malformed(`not a readable zip archive: ${K}`)}if(Q.length>m)throw P.resourceLimit("max_entry_count",`archive contains ${Q.length} entries`);return new l(Q)}part(J){J=C1(J);let Q=this.cache.get(J);if(Q!==void 0)return Q;let $=this.zip.indexForName(J);if($===void 0)return;if($.uncompressedSize>L)throw P.resourceLimit("max_entry_bytes",`${J} declares ${$.uncompressedSize} decompressed bytes`);let K=Math.max(0,$1-this.totalRead),W=Math.min(L,K),Z;try{Z=this.decompress($,W+1)}catch(G){if(G instanceof P)throw G;let H=G instanceof Error?G.message:String(G);throw P.malformedPart(J,`corrupt archive entry: ${H}`)}if(Z.length>W){if(K<L)throw P.resourceLimit("max_total_bytes",`${J} exceeds the archive's remaining decompression budget`);throw P.resourceLimit("max_entry_bytes",`${J} exceeds the decompression cap`)}return this.totalRead+=Z.length,this.cache.set(J,Z),Z}hasPart(J){return this.zip.indexForName(C1(J))!==void 0}partNames(){return this.zip.partNames()}requiredPart(J){let Q=this.part(J);if(Q===void 0)throw P.missingPart(J);return Q}optionalPart(J){try{return this.part(J)}catch(Q){if(Q instanceof P&&Q.isFatal())throw Q;T1(`skipping unreadable part ${J}: ${Q instanceof Error?Q.message:String(Q)}`);return}}optionalXmlPart(J){let Q=this.optionalPart(J);if(Q===void 0)return;try{return t(Q)}catch($){if($ instanceof P&&$.isFatal())throw $;T1(`skipping corrupt part ${J}: ${$ instanceof Error?$.message:String($)}`);return}}requiredXmlPart(J){return t(this.requiredPart(J))}decompress(J,Q){if((J.flags&1)!==0)throw Error("encrypted zip entry");let $;try{$=this.zip.readCompressed(J)}catch(W){let Z=W instanceof Error?W.message:String(W);throw P.malformedPart(J.name,`unreadable archive entry: ${Z}`)}let K;if(J.method===F6){let W=J.uncompressedSize>0?J.uncompressedSize:$.length;K=$.subarray(0,Math.min(W,Q,$.length))}else if(J.method===L6)K=M6($,Q);else throw Error(`unsupported compression method ${J.method}`);if(K.length<=Q-1&&J.crc!==0&&k6(K)!==J.crc)throw Error("crc mismatch");return K}}function R6(J){if(!E(J))return;try{let Q=z.open(J);if(Q.exists("EncryptionInfo")||Q.exists("EncryptedPackage"))return P.encrypted()}catch{}return P.malformed("OLE compound document where an OOXML package was expected (legacy binary format?)")}function M6(J,Q){try{return Y6(J,Q)}catch($){if($ instanceof V6)return new Uint8Array(Q);throw $}}function I6(J){let Q=Math.max(0,J.length-65557);for(let $=J.length-22;$>=Q;$-=1)if(J[$]===80&&J[$+1]===75&&J[$+2]===5&&J[$+3]===6){let K=J[$+20]|J[$+21]<<8;if($+22+K===J.length)return $}return-1}function q6(J,Q){if(Q<20)return;let $=e(J),K=Q-20;if(M($,K)!==X6)return;let W=g($,K+8);if(W+56>J.length||M($,W)!==P6)return;return{entryCount:g($,W+32),cdSize:g($,W+40),cdOffset:g($,W+48)}}function O6(J,Q){let $=0,K=e(J);while($+4<=J.length){let W=R(K,$),Z=R(K,$+2),G=$+4;if(G+Z>J.length)break;if(W===1){let H=G,{uncompressedSize:U,compressedSize:D,headerOffset:V}=Q;if(U===4294967295&&H+8<=G+Z)U=g(K,H),H+=8;if(D===4294967295&&H+8<=G+Z)D=g(K,H),H+=8;if(V===4294967295&&H+8<=G+Z)V=g(K,H);return{uncompressedSize:U,compressedSize:D,headerOffset:V}}$=G+Z}return Q}function N6(J,Q){if(Q)return new TextDecoder("utf-8",{fatal:!1}).decode(J);return U6(J,"cp437")}function C1(J){let Q=0;while(Q<J.length&&J.charCodeAt(Q)===47)Q+=1;return J.slice(Q)}function e(J){return new DataView(J.buffer,J.byteOffset,J.byteLength)}function R(J,Q){return J.getUint16(Q,!0)}function M(J,Q){return J.getUint32(Q,!0)}function g(J,Q){let $=J.getUint32(Q,!0),K=J.getUint32(Q+4,!0);if(K>2097151)return Number.POSITIVE_INFINITY;return K*4294967296+$}var w6=_6();function _6(){let J=new Uint32Array(256);for(let Q=0;Q<256;Q+=1){let $=Q;for(let K=0;K<8;K+=1)$=$&1?3988292384^$>>>1:$>>>1;J[Q]=$>>>0}return J}function A6(J){let Q=4294967295;for(let $=0;$<J.length;$+=1)Q=w6[(Q^J[$])&255]^Q>>>8;return(Q^4294967295)>>>0}function k6(J){return A6(J)}import{ConvertError as x1}from"@mdgate/core";function z6(J,Q){if(!Number.isSafeInteger(Q)||Q<0||Q+2>J.length)return;return J[Q]|J[Q+1]<<8}function g6(J,Q){if(!Number.isSafeInteger(Q)||Q<0||Q+4>J.length)return;return(J[Q]|J[Q+1]<<8|J[Q+2]<<16|J[Q+3]<<24)>>>0}function T6(J,Q){try{return J.readStream(Q)}catch($){if($ instanceof x1)throw $;let K=$ instanceof Error?$.message:String($);throw x1.malformedPart(Q,`unreadable stream: ${K}`)}}import{trim as u1}from"@mdgate/utils";import{ConvertError as h1}from"@mdgate/core";function b(J,Q){let $,K=Q.indexOf("#");if(K>=0)$=Y1(Q.slice(K+1)),Q=Q.slice(0,K);let W=Q.indexOf("?");if(W>=0)Q=Q.slice(0,W);if(Q.length===0)return{path:J,fragment:$};let Z=[];if(!Q.startsWith("/")){let G=J.lastIndexOf("/");if(G>=0){for(let H of J.slice(0,G).split("/"))if(H.length>0)Z.push(H)}}for(let G of Q.split("/")){if(G.length===0||G===".")continue;if(G===".."){Z.pop();continue}let H=Y1(G);if(H.includes("/")||H.includes("\\"))throw h1.malformed(`percent-encoded separator in package reference segment ${E1(G)}`);if(H==="."||H==="..")throw h1.malformed(`percent-encoded traversal in package reference segment ${E1(G)}`);Z.push(H)}return{path:Z.join("/"),fragment:$}}function C6(J){return Y1(J)}var x6=new TextEncoder;function Y1(J){let Q=x6.encode(J),$=[],K=0;while(K<Q.length){if(Q[K]===37&&K+2<Q.length){let W=Q[K+1],Z=Q[K+2];if(S1(W)&&S1(Z)){$.push(v1(W)<<4|v1(Z)),K+=3;continue}}$.push(Q[K]),K+=1}return new TextDecoder("utf-8",{fatal:!1}).decode(Uint8Array.from($))}function S1(J){return J>=48&&J<=57||J>=65&&J<=70||J>=97&&J<=102}function v1(J){if(J>=48&&J<=57)return J-48;if(J>=65&&J<=70)return J-55;return J-87}function E1(J){return JSON.stringify(J)}import{warn as p1}from"@mdgate/utils";var j1={OFFICE_DOCUMENT:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",STYLES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",NUMBERING:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",FOOTNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",ENDNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"};class J1{map;constructor(J=new Map){this.map=J}get(J){return this.map.get(J)}internalTarget(J){let Q=this.map.get(J);return Q!==void 0&&Q.mode==="internal"?Q.target:void 0}entries(){return this.map.entries()}firstOfType(J){let Q,$;for(let[K,W]of this.map)if(W.relType===J&&W.mode==="internal"){if(Q===void 0||K<Q)Q=K,$=W}return $}}function B1(J,Q){let $=J.optionalXmlPart(Q);if($===void 0)return new J1;let K=new Map;for(let W of $.descendants(A.PKG_RELS,"Relationship")){let Z=W.attrAny("Id"),G=W.attrAny("Target");if(Z===void 0||G===void 0)continue;let H=W.attrAny("TargetMode"),U=H!==void 0&&H.toLowerCase()==="external"?"external":"internal",D=W.attrAny("Type")??"",V=u(D)??D;K.set(Z,{target:G,relType:V,mode:U})}return new J1(K)}function h6(J,Q,$,K){let W=Q.get(K);if(W===void 0||W.mode!=="internal")return;let Z;try{Z=b($,W.target)}catch(H){p1(`skipping unresolvable relationship target ${JSON.stringify(W.target)}: ${H instanceof Error?H.message:String(H)}`);return}let G=J.optionalPart(Z.path);if(G===void 0){p1(`relationship target ${Z.path} is missing`);return}return[Z.path,G]}function S6(J){let Q=J.lastIndexOf("/");if(Q>=0)return`${J.slice(0,Q)}/_rels/${J.slice(Q+1)}.rels`;return`_rels/${J}.rels`}var l1="http://schemas.openxmlformats.org/package/2006/content-types",P1="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",v6="http://schemas.openxmlformats.org/spreadsheetml/2006/main";function E6(J){if(J.length<4||J[0]!==80||J[1]!==75||J[2]!==3||J[3]!==4)return;try{return u6(J)}catch{return}}function p6(J){if(!E(J))return;let Q;try{Q=z.open(J)}catch{return}try{for(let $ of Q.readRootStorage()){let K=$.name;if(x(K,"WordDocument"))return"doc";if(x(K,"PowerPoint Document"))return"ppt";if(x(K,"Workbook")||x(K,"Book"))return"xls"}}catch{return}return}function u6(J){let Q;try{Q=l.open(J)}catch{return}let $=b6(Q,"mimetype");if($!==void 0){let W=c6($);if(W===void 0)return;return b1(u1(W))}try{let Z=B1(Q,"_rels/.rels").firstOfType(j1.OFFICE_DOCUMENT);if(Z!==void 0){let G;try{G=b("",Z.target)}catch{G=void 0}if(G!==void 0){let H=X1(Q,"[Content_Types].xml");if(H!==void 0){let D=y6(H,G.path);if(D!==void 0){let V=f6(D);if(V!==void 0)return V}}let U=X1(Q,G.path);if(U!==void 0){let D=U.childElems()[0],V=D!==void 0?d6(D):void 0;if(V!==void 0)return V}return m6(G.path)}}}catch{}for(let[W,Z]of[["word/document.xml","docx"],["ppt/presentation.xml","pptx"],["xl/workbook.xml","xlsx"],["xl/workbook.bin","xlsx"]])if(Q.hasPart(W))return Z;try{let W=X1(Q,"META-INF/manifest.xml");if(W!==void 0){for(let Z of W.descendants(P1,"file-entry"))if(Z.attr(P1,"full-path")==="/"){let G=Z.attr(P1,"media-type");if(G!==void 0)return b1(u1(G))}}}catch{}if(Q.hasPart("META-INF/container.xml"))return"epub";let K=l6(Q);if(K!==void 0)return K;return}function l6(J){let Q=J.partNames();if(Q.length===0)return;let $=!1,K=!1,W=!1,Z=!1,G=!1;for(let H of Q){let U=H.toLowerCase();if(U.includes("encrypted")||U.endsWith(".iwae"))return;if(U.endsWith(".iwa")){if($=!0,U.endsWith("/document.iwa")||U==="document.iwa")W=!0;if(U.includes("slide")||U.includes("masterslide"))Z=!0;if(U.includes("calculationengine")||U.includes("tables/"))G=!0}if(U==="index.zip")K=!0}if(!$&&!K)return;if(!$&&K)return;if(!W&&!K&&!Z&&!G)return;if(Z&&!G)return"keynote";if(G&&!Z)return"numbers";if(W)return"pages";return}function b6(J,Q){try{return J.optionalPart(Q)}catch{return}}function X1(J,Q){try{return J.optionalXmlPart(Q)}catch{return}}function b1(J){switch(J.endsWith("-template")?J.slice(0,-9):J){case"application/epub+zip":return"epub";case"application/vnd.oasis.opendocument.text":return"odt";case"application/vnd.oasis.opendocument.spreadsheet":return"ods";case"application/vnd.oasis.opendocument.presentation":return"odp";default:return}}function y6(J,Q){let $=`/${Q}`;for(let Z of J.descendants(l1,"Override")){let G=Z.attrAny("PartName");if(G!==void 0&&x(G,$)){let H=Z.attrAny("ContentType");if(H!==void 0)return H}}let K=Q.lastIndexOf(".");if(K<0)return;let W=Q.slice(K+1);for(let Z of J.descendants(l1,"Default")){let G=Z.attrAny("Extension");if(G!==void 0&&x(G,W))return Z.attrAny("ContentType")}return}function f6(J){let Q=J.toLowerCase();if(Q.includes("wordprocessingml"))return"docx";if(Q.includes("presentationml"))return"pptx";if(Q.includes("spreadsheetml")||Q.includes("ms-excel"))return"xlsx";return}function d6(J){switch(J.ns){case A.W:return"docx";case A.P:return"pptx";case v6:return"xlsx";default:return}}function m6(J){if(J.startsWith("word/"))return"docx";if(J.startsWith("ppt/"))return"pptx";if(J.startsWith("xl/"))return"xlsx";return}function x(J,Q){return J.length===Q.length&&J.toLowerCase()===Q.toLowerCase()}function c6(J){try{return new TextDecoder("utf-8",{fatal:!0}).decode(J)}catch{return}}export{b as resolve,S6 as relsPartFor,j1 as relType,h6 as relTargetBytes,B1 as readRels,T6 as readOleStream,R6 as probeOle,t as parseXml,A as ns,u as normalizeOoxmlUri,E as hasOleMagic,g6 as getU32,z6 as getU16,E6 as detectZipDoc,p6 as detectOleDoc,C6 as decodeFragment,J1 as Relationships,l as Package,o1 as OLE_MAGIC_BYTES,i as MAX_XML_NODES,c as MAX_XML_DEPTH,$1 as MAX_TOTAL_BYTES,y1 as MAX_RECORD_DEPTH,f1 as MAX_RECORDS,m as MAX_ENTRY_COUNT,L as MAX_ENTRY_BYTES,r as Element,z as CompoundFile};
@@ -0,0 +1,18 @@
1
+ /** Maximum decompressed size of a single archive entry: 128 MiB. */
2
+ export declare const MAX_ENTRY_BYTES: number;
3
+ /** Maximum total decompressed bytes read from one archive: 512 MiB. */
4
+ export declare const MAX_TOTAL_BYTES: number;
5
+ /** Maximum number of entries in one archive. */
6
+ export declare const MAX_ENTRY_COUNT = 100000;
7
+ /** Maximum XML element nesting depth. */
8
+ export declare const MAX_XML_DEPTH = 256;
9
+ /**
10
+ * Maximum number of XML nodes (elements + text runs) in one part. Sized
11
+ * from the measured worst-case DOM cost (~400 bytes/node) so a saturating
12
+ * part stays around the archive budget.
13
+ */
14
+ export declare const MAX_XML_NODES = 2000000;
15
+ /** Maximum nesting depth of binary record containers (legacy PPT stream). */
16
+ export declare const MAX_RECORD_DEPTH = 64;
17
+ /** Maximum total binary records visited in one legacy record stream. */
18
+ export declare const MAX_RECORDS = 16000000;
package/dist/path.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export interface Target {
2
+ /** Normalized archive path (no leading slash). */
3
+ path: string;
4
+ fragment: string | undefined;
5
+ }
6
+ /** Resolve a relative or package-absolute reference against the part it appears in. */
7
+ export declare function resolve(basePart: string, reference: string): Target;
8
+ export declare function decodeFragment(fragment: string): string;
@@ -0,0 +1,30 @@
1
+ import type { Package } from './archive.js';
2
+ /** Well-known OPC relationship types (Transitional form). */
3
+ export declare const relType: {
4
+ readonly OFFICE_DOCUMENT: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
5
+ readonly STYLES: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
6
+ readonly NUMBERING: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
7
+ readonly FOOTNOTES: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes";
8
+ readonly ENDNOTES: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes";
9
+ };
10
+ export type TargetMode = 'internal' | 'external';
11
+ export interface Relationship {
12
+ target: string;
13
+ relType: string;
14
+ mode: TargetMode;
15
+ }
16
+ export declare class Relationships {
17
+ private readonly map;
18
+ constructor(map?: Map<string, Relationship>);
19
+ get(id: string): Relationship | undefined;
20
+ internalTarget(id: string): string | undefined;
21
+ entries(): IterableIterator<[string, Relationship]>;
22
+ /** Internal-mode relationship of a given type, lowest id first. */
23
+ firstOfType(type: string): Relationship | undefined;
24
+ }
25
+ export declare function readRels(pkg: Package, part: string): Relationships;
26
+ /** A loaded internal relationship target: its resolved part path and bytes. */
27
+ export type RelTarget = [string, Uint8Array];
28
+ export declare function relTargetBytes(pkg: Package, rels: Relationships, basePart: string, relId: string): RelTarget | undefined;
29
+ /** Conventional rels part name for a part. */
30
+ export declare function relsPartFor(part: string): string;
package/dist/xml.d.ts ADDED
@@ -0,0 +1,74 @@
1
+ /** Well-known namespace URIs. */
2
+ export declare const ns: {
3
+ readonly W: "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
4
+ readonly R: "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
5
+ readonly A: "http://schemas.openxmlformats.org/drawingml/2006/main";
6
+ readonly PIC: "http://schemas.openxmlformats.org/drawingml/2006/picture";
7
+ readonly WP: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing";
8
+ readonly MC: "http://schemas.openxmlformats.org/markup-compatibility/2006";
9
+ readonly CHART: "http://schemas.openxmlformats.org/drawingml/2006/chart";
10
+ readonly DGM: "http://schemas.openxmlformats.org/drawingml/2006/diagram";
11
+ readonly P: "http://schemas.openxmlformats.org/presentationml/2006/main";
12
+ readonly PKG_RELS: "http://schemas.openxmlformats.org/package/2006/relationships";
13
+ readonly OFFICE: "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
14
+ readonly TEXT: "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
15
+ readonly TABLE: "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
16
+ readonly DRAW: "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
17
+ readonly STYLE: "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
18
+ readonly FO: "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
19
+ readonly PRESENTATION: "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0";
20
+ readonly MANIFEST: "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0";
21
+ readonly XLINK: "http://www.w3.org/1999/xlink";
22
+ readonly XML: "http://www.w3.org/XML/1998/namespace";
23
+ readonly SVG_COMPAT: "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0";
24
+ readonly VML: "urn:schemas-microsoft-com:vml";
25
+ readonly O_VML: "urn:schemas-microsoft-com:office:office";
26
+ readonly WPS: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape";
27
+ readonly WPG: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup";
28
+ };
29
+ export type XmlNode = {
30
+ type: 'elem';
31
+ elem: Element;
32
+ } | {
33
+ type: 'text';
34
+ text: string;
35
+ };
36
+ export interface Attr {
37
+ ns: string | undefined;
38
+ local: string;
39
+ value: string;
40
+ }
41
+ export declare class Element {
42
+ ns: string | undefined;
43
+ local: string;
44
+ attrs: Attr[];
45
+ children: XmlNode[];
46
+ private _elems;
47
+ constructor(nsUri: string | undefined, local: string, attrs?: Attr[], children?: XmlNode[]);
48
+ is(nsUri: string, local: string): boolean;
49
+ /**
50
+ * Same-vocabulary attribute lookup: the qualified attribute wins, and an
51
+ * explicitly unqualified one with the same local name is accepted.
52
+ */
53
+ attr(nsUri: string, local: string): string | undefined;
54
+ attrQualified(nsUri: string, local: string): string | undefined;
55
+ attrUnqualified(local: string): string | undefined;
56
+ attrAny(local: string): string | undefined;
57
+ childElems(): Element[];
58
+ find(nsUri: string, local: string): Element | undefined;
59
+ findAll(nsUri: string, local: string): Element[];
60
+ /** Depth-first descendant nodes in document order (iterative). */
61
+ descendantNodes(): Generator<XmlNode>;
62
+ descendantElems(): Generator<Element>;
63
+ firstDescendant(nsUri: string, local: string): Element | undefined;
64
+ descendants(nsUri: string, local: string): Generator<Element>;
65
+ descendantsAny(local: string): Generator<Element>;
66
+ text(): string;
67
+ }
68
+ /**
69
+ * ISO/IEC 29500 Strict namespace and relationship-type URIs are the
70
+ * Transitional ones re-rooted under `http://purl.oclc.org/ooxml/` with the
71
+ * `2006` version segment dropped.
72
+ */
73
+ export declare function normalizeOoxmlUri(uri: string): string | undefined;
74
+ export declare function parseXml(bytes: Uint8Array): Element;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@mdgate/containers",
3
+ "version": "0.1.0",
4
+ "description": "mdgate container parsing: ZIP/OPC packages, OLE compound files, XML",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "dist/**/*.d.ts"
19
+ ],
20
+ "scripts": {
21
+ "build": "bun ../../scripts/build-package.ts",
22
+ "prepublishOnly": "bun run build"
23
+ },
24
+ "dependencies": {
25
+ "@mdgate/core": "0.1.3",
26
+ "@mdgate/utils": "0.1.2"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "engines": {
32
+ "node": ">=20"
33
+ },
34
+ "keywords": [
35
+ "mdgate",
36
+ "zip",
37
+ "opc",
38
+ "ole",
39
+ "cfb",
40
+ "xml"
41
+ ]
42
+ }