import { Stream } from "stream"; import { File } from "@file/file"; import { strFromU8 } from "fflate"; import { Compiler } from "./next-compiler"; /** * Use blanks to prettify */ export enum PrettifyType { NONE = "", WITH_2_BLANKS = " ", WITH_4_BLANKS = " ", WITH_TAB = "\t", } export class Packer { public static async toString(file: File, prettify?: boolean | PrettifyType): Promise { const zip = await this.compiler.compile( file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify, ); return strFromU8(zip); } public static async toBuffer(file: File, prettify?: boolean | PrettifyType): Promise { const zip = await this.compiler.compile( file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify, ); return Buffer.from(zip.buffer); } public static async toBase64String(file: File, prettify?: boolean | PrettifyType): Promise { const zip = await this.compiler.compile( file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify, ); return Promise.resolve(strFromU8(zip)); } public static async toBlob(file: File, prettify?: boolean | PrettifyType): Promise { const zip = await this.compiler.compile( file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify, ); return new Blob([zip.buffer]); } public static toStream(file: File, prettify?: boolean | PrettifyType): Stream { const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify); const stream = new Stream(); zip.then((z) => { stream.emit("data", z); stream.emit("end"); }); return stream; } private static readonly compiler = new Compiler(); }