Files
docx-js/src/export/packer/packer.ts

102 lines
3.3 KiB
TypeScript
Raw Normal View History

import { Stream } from "stream";
import { File } from "@file/file";
import { OutputByType, OutputType } from "@util/output-type";
import { Compiler, IXmlifyedFile } from "./next-compiler";
/**
* Use blanks to prettify
*/
export const PrettifyType = {
NONE: "",
WITH_2_BLANKS: " ",
WITH_4_BLANKS: " ",
WITH_TAB: "\t",
} as const;
const convertPrettifyType = (
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
): (typeof PrettifyType)[keyof typeof PrettifyType] | undefined =>
2023-06-12 18:43:29 +01:00
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify;
export class Packer {
// 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<OutputByType[T]> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
return zip.generateAsync({
type,
2023-06-12 18:43:29 +01:00
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
}
2023-06-12 18:43:29 +01:00
public static toString(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<string> {
return Packer.pack(file, "string", prettify, overrides);
2022-08-17 18:52:19 +02:00
}
public static toBuffer(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<Buffer> {
return Packer.pack(file, "nodebuffer", prettify, overrides);
}
public static toBase64String(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<string> {
return Packer.pack(file, "base64", prettify, overrides);
}
public static toBlob(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<Blob> {
return Packer.pack(file, "blob", prettify, overrides);
}
public static toArrayBuffer(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<ArrayBuffer> {
return Packer.pack(file, "arraybuffer", prettify, overrides);
}
2019-08-07 22:12:14 +01:00
public static toStream(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Stream {
2023-06-01 02:05:35 +01:00
const stream = new Stream();
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
2023-06-05 00:33:43 +01:00
2023-06-12 18:43:29 +01:00
zip.generateAsync({
type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
}).then((z) => {
2023-06-05 00:33:43 +01:00
stream.emit("data", z);
stream.emit("end");
});
2023-06-01 02:05:35 +01:00
return stream;
}
2019-08-07 22:12:14 +01:00
private static readonly compiler = new Compiler();
}