diff --git a/docs/usage/packers.md b/docs/usage/packers.md index f991fd2538..10b001b04b 100644 --- a/docs/usage/packers.md +++ b/docs/usage/packers.md @@ -84,6 +84,8 @@ Packer.toArrayBuffer(doc).then((arrayBuffer) => { ### Export as a Stream +This gives a Node.js stream, which emits the document as one `Buffer` and then ends. A browser has no `Buffer`, so there the stream emits an `error` event instead. Use [`toBlob`](#export-as-blob) in a browser. + ```ts live import { Document, Packer, Paragraph } from "docx"; diff --git a/src/export/packer/packer.spec.ts b/src/export/packer/packer.spec.ts index 2184f9526f..b443093da3 100644 --- a/src/export/packer/packer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -243,8 +243,25 @@ describe("Packer", () => { expect(() => Packer.toStream(file)).toThrow("The document can't be written"); }); + it("should emit an error where there's no Buffer, as in a browser, rather than reject where nothing can catch it", async () => { + vi.stubGlobal("Buffer", undefined); + const stream = Packer.toStream(file); + const data = vi.fn(); + const end = vi.fn(); + stream.on("data", data); + stream.on("end", end); + + const error = await new Promise((resolve) => { + stream.on("error", resolve); + }); + expect(error.message).toBe("nodebuffer is not supported by this platform"); + expect(data).not.toHaveBeenCalled(); + expect(end).not.toHaveBeenCalled(); + }); + afterEach(() => { vi.resetAllMocks(); + vi.unstubAllGlobals(); }); }); }); diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts index 2f24332328..e8a4b2e325 100644 --- a/src/export/packer/packer.ts +++ b/src/export/packer/packer.ts @@ -166,6 +166,9 @@ export class Packer { /** * Exports a document to a Node.js Stream. * + * The stream gives the document as one Node.js Buffer, then ends. Where there's no Buffer, as in a browser, it + * emits an `error` event instead, so use `toBlob` there. + * * @param file - The document to export * @param prettify - Whether to prettify the XML output * @param overrides - Optional array of file overrides @@ -179,11 +182,17 @@ export class Packer { const stream = new LegacyStream(); const files = this.compiler.compile(file, convertPrettifyType(prettify), overrides); - // Zipped and given after the caller has had the chance to listen, as when JSZip zipped it, asynchronously - new Promise((resolve) => setTimeout(resolve)).then(() => { - stream.emit("data", zipOutput(zipFiles(files), "nodebuffer", mimeTypeOf(file))); - stream.emit("end"); - }); + // Zipped and given after the caller has had the chance to listen, as when JSZip zipped it, asynchronously. Failing + // to zip, as in a browser, which has no Buffer, is the stream's error, not a rejection nothing can catch + new Promise((resolve) => setTimeout(resolve)) + .then(() => zipOutput(zipFiles(files), "nodebuffer", mimeTypeOf(file))) + .then( + (data) => { + stream.emit("data", data); + stream.emit("end"); + }, + (error) => stream.emit("error", error), + ); return stream; }