2022-08-22 13:25:59 -07:00
|
|
|
import { Stream } from "stream";
|
2024-10-21 03:57:15 +01:00
|
|
|
|
2022-09-15 20:00:50 +01:00
|
|
|
import { File } from "@file/file";
|
2025-02-16 13:24:15 -05:00
|
|
|
import { OutputByType, OutputType } from "@util/output-type";
|
2022-06-26 23:26:42 +01:00
|
|
|
|
2025-01-27 02:39:23 -08:00
|
|
|
import { Compiler, IXmlifyedFile } from "./next-compiler";
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2022-03-07 20:02:14 +08:00
|
|
|
/**
|
|
|
|
* Use blanks to prettify
|
|
|
|
*/
|
2023-12-22 10:25:00 +09:00
|
|
|
export const PrettifyType = {
|
|
|
|
NONE: "",
|
|
|
|
WITH_2_BLANKS: " ",
|
|
|
|
WITH_4_BLANKS: " ",
|
2024-10-21 03:57:15 +01:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
WITH_TAB: "\t",
|
|
|
|
} as const;
|
2022-03-07 20:02:14 +08:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
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;
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
export class Packer {
|
2025-02-16 13:24:15 -05:00
|
|
|
// eslint-disable-next-line require-await
|
|
|
|
public static async pack<T extends OutputType>(
|
2025-01-27 02:39:23 -08:00
|
|
|
file: File,
|
2025-02-16 13:24:15 -05:00
|
|
|
type: T,
|
2025-01-27 02:39:23 -08:00
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
overrides: readonly IXmlifyedFile[] = [],
|
2025-02-16 13:24:15 -05:00
|
|
|
): Promise<OutputByType[T]> {
|
2025-01-27 02:39:23 -08:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
|
2025-02-16 13:24:15 -05:00
|
|
|
return zip.generateAsync({
|
|
|
|
type,
|
2023-06-12 18:43:29 +01:00
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
2025-02-16 13:24:15 -05:00
|
|
|
}
|
2023-06-12 18:43:29 +01:00
|
|
|
|
2025-02-16 13:24:15 -05: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
|
|
|
}
|
|
|
|
|
2025-02-16 13:24:15 -05:00
|
|
|
public static toBuffer(
|
2025-01-27 02:39:23 -08:00
|
|
|
file: File,
|
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
overrides: readonly IXmlifyedFile[] = [],
|
|
|
|
): Promise<Buffer> {
|
2025-02-16 13:24:15 -05:00
|
|
|
return Packer.pack(file, "nodebuffer", prettify, overrides);
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
2018-01-31 20:08:36 +00:00
|
|
|
|
2025-02-16 13:24:15 -05:00
|
|
|
public static toBase64String(
|
2025-01-27 02:39:23 -08:00
|
|
|
file: File,
|
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
overrides: readonly IXmlifyedFile[] = [],
|
|
|
|
): Promise<string> {
|
2025-02-16 13:24:15 -05:00
|
|
|
return Packer.pack(file, "base64", prettify, overrides);
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
|
|
|
|
2025-02-16 13:24:15 -05:00
|
|
|
public static toBlob(
|
2025-01-27 02:39:23 -08:00
|
|
|
file: File,
|
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
overrides: readonly IXmlifyedFile[] = [],
|
|
|
|
): Promise<Blob> {
|
2025-02-16 13:24:15 -05:00
|
|
|
return Packer.pack(file, "blob", prettify, overrides);
|
|
|
|
}
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2025-02-16 13:24:15 -05:00
|
|
|
public static toArrayBuffer(
|
|
|
|
file: File,
|
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
overrides: readonly IXmlifyedFile[] = [],
|
|
|
|
): Promise<ArrayBuffer> {
|
|
|
|
return Packer.pack(file, "arraybuffer", prettify, overrides);
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
2025-01-27 02:39:23 -08: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();
|
2025-01-27 02:39:23 -08:00
|
|
|
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;
|
2022-08-22 13:25:59 -07:00
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
|
|
|
private static readonly compiler = new Compiler();
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|