Skip to content

Commit

Permalink
Add Packer.pack and Packer.toArrayBuffer (#2959)
Browse files Browse the repository at this point in the history
* Add Packer.pack and Packer.toArrayBuffer

To mirror patchDocument's outputType parameter.

See #2920

* Ignore coverage

---------

Co-authored-by: Dolan Miu <[email protected]>
  • Loading branch information
joshkel and dolanmiu authored Feb 16, 2025
1 parent 05fcf6e commit 170309a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 46 deletions.
27 changes: 27 additions & 0 deletions src/export/packer/packer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ describe("Packer", () => {
});
});

describe("#toArrayBuffer()", () => {
it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn((Packer as any).compiler, "compile").mockReturnValue({
generateAsync: () => vi.fn(),
});
const str = await Packer.toArrayBuffer(file);

assert.isDefined(str);
});

it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});

return Packer.toArrayBuffer(file).catch((error) => {
assert.isDefined(error);
});
});

afterEach(() => {
vi.resetAllMocks();
});
});

describe("#toStream()", () => {
it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
58 changes: 27 additions & 31 deletions src/export/packer/packer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Stream } from "stream";

import { File } from "@file/file";
import { OutputByType, OutputType } from "@util/output-type";

import { Compiler, IXmlifyedFile } from "./next-compiler";

Expand All @@ -21,64 +22,59 @@ const convertPrettifyType = (
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify;

export class Packer {
public static async toString(
// eslint-disable-next-line require-await
public static async pack<T extends OutputType>(
file: File,
type: T,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<string> {
): Promise<OutputByType[T]> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
const zipData = await zip.generateAsync({
type: "string",
return zip.generateAsync({
type,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
}

return zipData;
public static toString(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<string> {
return Packer.pack(file, "string", prettify, overrides);
}

public static async toBuffer(
public static toBuffer(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<Buffer> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
const zipData = await zip.generateAsync({
type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});

return zipData;
return Packer.pack(file, "nodebuffer", prettify, overrides);
}

public static async toBase64String(
public static toBase64String(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<string> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
const zipData = await zip.generateAsync({
type: "base64",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});

return zipData;
return Packer.pack(file, "base64", prettify, overrides);
}

public static async toBlob(
public static toBlob(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<Blob> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
const zipData = await zip.generateAsync({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
return Packer.pack(file, "blob", prettify, overrides);
}

return zipData;
public static toArrayBuffer(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<ArrayBuffer> {
return Packer.pack(file, "arraybuffer", prettify, overrides);
}

public static toStream(
Expand Down
17 changes: 2 additions & 15 deletions src/patcher/from-docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ConcreteHyperlink, ExternalHyperlink, ParagraphChild } from "@file/para
import { TargetModeType } from "@file/relationships/relationship/relationship";
import { IContext } from "@file/xml-components";
import { uniqueId } from "@util/convenience-functions";
import { OutputByType, OutputType } from "@util/output-type";

import { appendContentType } from "./content-types-manager";
import { appendRelationship, getNextRelationshipIndex } from "./relationship-manager";
Expand Down Expand Up @@ -47,21 +48,7 @@ type IHyperlinkRelationshipAddition = {

export type IPatch = ParagraphPatch | FilePatch;

// From JSZip
type OutputByType = {
readonly base64: string;
// eslint-disable-next-line id-denylist
readonly string: string;
readonly text: string;
readonly binarystring: string;
readonly array: readonly number[];
readonly uint8array: Uint8Array;
readonly arraybuffer: ArrayBuffer;
readonly blob: Blob;
readonly nodebuffer: Buffer;
};

export type PatchDocumentOutputType = keyof OutputByType;
export type PatchDocumentOutputType = OutputType;

export type PatchDocumentOptions<T extends PatchDocumentOutputType = PatchDocumentOutputType> = {
readonly outputType: T;
Expand Down
18 changes: 18 additions & 0 deletions src/util/output-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* v8 ignore start */
// Simply type definitions. Can ignore testing and coverage
// From JSZip
export type OutputByType = {
readonly base64: string;
// eslint-disable-next-line id-denylist
readonly string: string;
readonly text: string;
readonly binarystring: string;
readonly array: readonly number[];
readonly uint8array: Uint8Array;
readonly arraybuffer: ArrayBuffer;
readonly blob: Blob;
readonly nodebuffer: Buffer;
};

export type OutputType = keyof OutputByType;
/* v8 ignore stop */

0 comments on commit 170309a

Please sign in to comment.