mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
fix: emit toStream's failure to zip on its error event, as in a browser without Buffer, not as an unhandled rejection (#3844)
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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<Error>((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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user