You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cloneDocument runs on every headless agent edit (executeCommand → cloneDocument) and deep-clones via JSON.parse(JSON.stringify(doc)). JSON can't represent several fields the document model carries, so the clone silently corrupts them:
package.headers / package.footers / package.media are Maps → become {}
originalBuffer and each MediaFile.data are ArrayBuffers → become {}
package.properties.created / modified are Dates → become strings
The result is load-bearing: a no-edit export works, but after the first edit export throws and images are lost.
To Reproduce
import{DocumentAgent}from'@eigenpal/docx-editor-core/headless';constagent=awaitDocumentAgent.fromBuffer(bytes);// any real .docxawaitagent.toBuffer();// ✅ works (no edit)constedited=agent.insertText({paragraphIndex: 0,offset: 0},'Hello ');awaitedited.toBuffer();// ❌ throws
Depending on the document, the throw is one of:
Can't read the data of 'the loaded zip file' — repackDocx → JSZip.loadAsync(originalBuffer) on the {} left where the ArrayBuffer was.
map.entries is not a function — repackDocx's collectParts iterating package.headers / footers, now {} instead of Maps.
Any document with images also loses every image on the first edit (the mediaMap is gone). insertTable(...).toBuffer() fails the same way.
Expected behavior
A headless generate → edit → export round-trip should produce a valid .docx (and keep headers/footers and images), since this is the documented fromBuffer → edit → toBuffer flow.
Root cause
cloneDocument (packages/core/src/agent/executor/helpers.ts) uses JSON.parse(JSON.stringify(doc)), which cannot round-trip Map, ArrayBuffer, or Date.
Additional context
Affects the fully headless / server-side workflow (generate + edit + export without ever opening the editor).
Describe the bug
cloneDocumentruns on every headless agent edit (executeCommand→cloneDocument) and deep-clones viaJSON.parse(JSON.stringify(doc)). JSON can't represent several fields the document model carries, so the clone silently corrupts them:package.headers/package.footers/package.mediaareMaps → become{}originalBufferand eachMediaFile.dataareArrayBuffers → become{}package.properties.created/modifiedareDates → become stringsThe result is load-bearing: a no-edit export works, but after the first edit export throws and images are lost.
To Reproduce
Depending on the document, the throw is one of:
Can't read the data of 'the loaded zip file'—repackDocx→JSZip.loadAsync(originalBuffer)on the{}left where theArrayBufferwas.map.entries is not a function—repackDocx'scollectPartsiteratingpackage.headers/footers, now{}instead ofMaps.Any document with images also loses every image on the first edit (the
mediaMapis gone).insertTable(...).toBuffer()fails the same way.Expected behavior
A headless generate → edit → export round-trip should produce a valid
.docx(and keep headers/footers and images), since this is the documentedfromBuffer → edit → toBufferflow.Root cause
cloneDocument(packages/core/src/agent/executor/helpers.ts) usesJSON.parse(JSON.stringify(doc)), which cannot round-tripMap,ArrayBuffer, orDate.Additional context
@eigenpal/docx-editor-core1.9.0.structuredClone; share the read-onlyoriginalBufferand shallow-copy the immutablemediamap so large binaries aren't recopied per edit).