@evotecit/officeimo 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1222 -2
- package/bundles/officeimo-canopyx.js +11741 -0
- package/bundles/officeimo-canopyx.mjs +11639 -0
- package/bundles/officeimo-csv.js +1264 -0
- package/bundles/officeimo-csv.mjs +1239 -0
- package/bundles/officeimo-datatables.js +12018 -0
- package/bundles/officeimo-datatables.mjs +11915 -0
- package/bundles/officeimo-execution.js +1513 -0
- package/bundles/officeimo-execution.mjs +1489 -0
- package/bundles/officeimo-pdf.js +7009 -0
- package/bundles/officeimo-pdf.mjs +6946 -0
- package/bundles/officeimo-xlsx.js +5344 -0
- package/bundles/officeimo-xlsx.mjs +5290 -0
- package/bundles/officeimo.js +11468 -0
- package/bundles/officeimo.mjs +11369 -0
- package/dist/core/dates.d.ts +1 -0
- package/dist/core/dates.js +16 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/grouped-report.d.ts +87 -0
- package/dist/core/grouped-report.js +252 -0
- package/dist/core/index.d.ts +74 -0
- package/dist/core/index.js +37 -0
- package/dist/core/iteration.d.ts +7 -0
- package/dist/core/iteration.js +149 -0
- package/dist/core/limits.d.ts +7 -0
- package/dist/core/limits.js +56 -0
- package/dist/core/links.d.ts +5 -0
- package/dist/core/links.js +14 -0
- package/dist/core/presentation.d.ts +29 -0
- package/dist/core/presentation.js +59 -0
- package/dist/core/progress.d.ts +1 -0
- package/dist/core/progress.js +10 -0
- package/dist/core/sinks.d.ts +33 -0
- package/dist/core/sinks.js +102 -0
- package/dist/core/table.d.ts +8 -0
- package/dist/core/table.js +1 -0
- package/dist/core/value-text.d.ts +7 -0
- package/dist/core/value-text.js +13 -0
- package/dist/csv/index.d.ts +44 -0
- package/dist/csv/index.js +133 -0
- package/dist/csv/options.d.ts +1 -0
- package/dist/csv/options.js +8 -0
- package/dist/csv/structured-rows.d.ts +3 -0
- package/dist/csv/structured-rows.js +15 -0
- package/dist/execution/host.d.ts +5 -0
- package/dist/execution/host.js +164 -0
- package/dist/execution/index.d.ts +3 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/protocol.d.ts +48 -0
- package/dist/execution/protocol.js +173 -0
- package/dist/execution/types.d.ts +42 -0
- package/dist/execution/types.js +1 -0
- package/dist/execution/worker.d.ts +3 -0
- package/dist/execution/worker.js +147 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +14 -0
- package/dist/integrations/canopyx/dates.d.ts +1 -0
- package/dist/integrations/canopyx/dates.js +34 -0
- package/dist/integrations/canopyx/index.d.ts +10 -0
- package/dist/integrations/canopyx/index.js +45 -0
- package/dist/integrations/canopyx/source.d.ts +3 -0
- package/dist/integrations/canopyx/source.js +122 -0
- package/dist/integrations/canopyx/types.d.ts +83 -0
- package/dist/integrations/canopyx/types.js +1 -0
- package/dist/integrations/datatables/api.d.ts +1 -0
- package/dist/integrations/datatables/api.js +27 -0
- package/dist/integrations/datatables/headings.d.ts +1 -0
- package/dist/integrations/datatables/headings.js +65 -0
- package/dist/integrations/datatables/index.d.ts +12 -0
- package/dist/integrations/datatables/index.js +167 -0
- package/dist/integrations/datatables/snapshot.d.ts +3 -0
- package/dist/integrations/datatables/snapshot.js +206 -0
- package/dist/integrations/datatables/types.d.ts +98 -0
- package/dist/integrations/datatables/types.js +1 -0
- package/dist/integrations/options.d.ts +13 -0
- package/dist/integrations/options.js +34 -0
- package/dist/internal/capture.d.ts +4 -0
- package/dist/internal/capture.js +14 -0
- package/dist/internal/columns.d.ts +27 -0
- package/dist/internal/columns.js +1 -0
- package/dist/internal/crc32.d.ts +6 -0
- package/dist/internal/crc32.js +17 -0
- package/dist/internal/export-values.d.ts +3 -0
- package/dist/internal/export-values.js +14 -0
- package/dist/internal/rows.d.ts +6 -0
- package/dist/internal/rows.js +77 -0
- package/dist/internal/table-spans.d.ts +11 -0
- package/dist/internal/table-spans.js +58 -0
- package/dist/internal/totals.d.ts +15 -0
- package/dist/internal/totals.js +42 -0
- package/dist/opc/index.d.ts +67 -0
- package/dist/opc/index.js +233 -0
- package/dist/opc/properties.d.ts +15 -0
- package/dist/opc/properties.js +18 -0
- package/dist/opc/uri.d.ts +5 -0
- package/dist/opc/uri.js +41 -0
- package/dist/pdf/annotations.d.ts +1 -0
- package/dist/pdf/annotations.js +38 -0
- package/dist/pdf/binary.d.ts +6 -0
- package/dist/pdf/binary.js +24 -0
- package/dist/pdf/document-drawings.d.ts +15 -0
- package/dist/pdf/document-drawings.js +237 -0
- package/dist/pdf/document-grouped.d.ts +1 -0
- package/dist/pdf/document-grouped.js +244 -0
- package/dist/pdf/document-images.d.ts +19 -0
- package/dist/pdf/document-images.js +125 -0
- package/dist/pdf/document-types.d.ts +160 -0
- package/dist/pdf/document-types.js +1 -0
- package/dist/pdf/document-vectors.d.ts +11 -0
- package/dist/pdf/document-vectors.js +77 -0
- package/dist/pdf/document.d.ts +9 -0
- package/dist/pdf/document.js +270 -0
- package/dist/pdf/drawing-types.d.ts +53 -0
- package/dist/pdf/drawing-types.js +1 -0
- package/dist/pdf/font-resources.d.ts +43 -0
- package/dist/pdf/font-resources.js +202 -0
- package/dist/pdf/font-support.d.ts +1 -0
- package/dist/pdf/font-support.js +60 -0
- package/dist/pdf/font.d.ts +17 -0
- package/dist/pdf/font.js +56 -0
- package/dist/pdf/grouped-table.d.ts +6 -0
- package/dist/pdf/grouped-table.js +40 -0
- package/dist/pdf/grouped-types.d.ts +34 -0
- package/dist/pdf/grouped-types.js +1 -0
- package/dist/pdf/grouped.d.ts +10 -0
- package/dist/pdf/grouped.js +37 -0
- package/dist/pdf/index.d.ts +22 -0
- package/dist/pdf/index.js +8 -0
- package/dist/pdf/inline-text.d.ts +30 -0
- package/dist/pdf/inline-text.js +183 -0
- package/dist/pdf/inline-types.d.ts +15 -0
- package/dist/pdf/inline-types.js +1 -0
- package/dist/pdf/list-layout.d.ts +6 -0
- package/dist/pdf/list-layout.js +49 -0
- package/dist/pdf/objects.d.ts +2 -0
- package/dist/pdf/objects.js +320 -0
- package/dist/pdf/outlines.d.ts +1 -0
- package/dist/pdf/outlines.js +55 -0
- package/dist/pdf/page-context.d.ts +1 -0
- package/dist/pdf/page-context.js +12 -0
- package/dist/pdf/pages.d.ts +1 -0
- package/dist/pdf/pages.js +678 -0
- package/dist/pdf/paragraph-layout.d.ts +21 -0
- package/dist/pdf/paragraph-layout.js +71 -0
- package/dist/pdf/paragraph-styles.d.ts +1 -0
- package/dist/pdf/paragraph-styles.js +66 -0
- package/dist/pdf/png.d.ts +9 -0
- package/dist/pdf/png.js +259 -0
- package/dist/pdf/preflight.d.ts +7 -0
- package/dist/pdf/preflight.js +32 -0
- package/dist/pdf/presentation-settings.d.ts +5 -0
- package/dist/pdf/presentation-settings.js +28 -0
- package/dist/pdf/providers/harfbuzz.d.ts +60 -0
- package/dist/pdf/providers/harfbuzz.js +112 -0
- package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
- package/dist/pdf/providers/unicode13-profile.js +115 -0
- package/dist/pdf/section-types.d.ts +15 -0
- package/dist/pdf/section-types.js +40 -0
- package/dist/pdf/session.d.ts +25 -0
- package/dist/pdf/session.js +55 -0
- package/dist/pdf/settings.d.ts +17 -0
- package/dist/pdf/settings.js +196 -0
- package/dist/pdf/shaped-text.d.ts +5 -0
- package/dist/pdf/shaped-text.js +205 -0
- package/dist/pdf/standard-widths.d.ts +1 -0
- package/dist/pdf/standard-widths.js +317 -0
- package/dist/pdf/structure-parent-tree.d.ts +1 -0
- package/dist/pdf/structure-parent-tree.js +57 -0
- package/dist/pdf/structure.d.ts +6 -0
- package/dist/pdf/structure.js +302 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +129 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +83 -0
- package/dist/pdf/table-layout.d.ts +75 -0
- package/dist/pdf/table-layout.js +217 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +108 -0
- package/dist/pdf/text-shaping-types.d.ts +39 -0
- package/dist/pdf/text-shaping-types.js +1 -0
- package/dist/pdf/text.d.ts +13 -0
- package/dist/pdf/text.js +101 -0
- package/dist/pdf/truetype-subset.d.ts +5 -0
- package/dist/pdf/truetype-subset.js +217 -0
- package/dist/pdf/truetype.d.ts +1 -0
- package/dist/pdf/truetype.js +235 -0
- package/dist/pdf/types.d.ts +171 -0
- package/dist/pdf/types.js +1 -0
- package/dist/pdf/write.d.ts +6 -0
- package/dist/pdf/write.js +57 -0
- package/dist/pdf/xref.d.ts +1 -0
- package/dist/pdf/xref.js +30 -0
- package/dist/xlsx/attachments.d.ts +30 -0
- package/dist/xlsx/attachments.js +67 -0
- package/dist/xlsx/chart-bindings.d.ts +1 -0
- package/dist/xlsx/chart-bindings.js +122 -0
- package/dist/xlsx/chart-types.d.ts +73 -0
- package/dist/xlsx/chart-types.js +1 -0
- package/dist/xlsx/chart-xml.d.ts +2 -0
- package/dist/xlsx/chart-xml.js +103 -0
- package/dist/xlsx/charts.d.ts +1 -0
- package/dist/xlsx/charts.js +159 -0
- package/dist/xlsx/conditional-formatting.d.ts +1 -0
- package/dist/xlsx/conditional-formatting.js +158 -0
- package/dist/xlsx/conditional-types.d.ts +64 -0
- package/dist/xlsx/conditional-types.js +1 -0
- package/dist/xlsx/data-row-ranges.d.ts +5 -0
- package/dist/xlsx/data-row-ranges.js +39 -0
- package/dist/xlsx/differential-styles.d.ts +1 -0
- package/dist/xlsx/differential-styles.js +102 -0
- package/dist/xlsx/formula.d.ts +8 -0
- package/dist/xlsx/formula.js +104 -0
- package/dist/xlsx/grouped-layout.d.ts +61 -0
- package/dist/xlsx/grouped-layout.js +79 -0
- package/dist/xlsx/grouped.d.ts +16 -0
- package/dist/xlsx/grouped.js +161 -0
- package/dist/xlsx/index.d.ts +24 -0
- package/dist/xlsx/index.js +10 -0
- package/dist/xlsx/layout.d.ts +1 -0
- package/dist/xlsx/layout.js +186 -0
- package/dist/xlsx/options.d.ts +1 -0
- package/dist/xlsx/options.js +16 -0
- package/dist/xlsx/portable.d.ts +27 -0
- package/dist/xlsx/portable.js +44 -0
- package/dist/xlsx/preservation.d.ts +1 -0
- package/dist/xlsx/preservation.js +60 -0
- package/dist/xlsx/ranges.d.ts +1 -0
- package/dist/xlsx/ranges.js +24 -0
- package/dist/xlsx/regions.d.ts +1 -0
- package/dist/xlsx/regions.js +97 -0
- package/dist/xlsx/structured-rows.d.ts +11 -0
- package/dist/xlsx/structured-rows.js +28 -0
- package/dist/xlsx/style-xml.d.ts +1 -0
- package/dist/xlsx/style-xml.js +6 -0
- package/dist/xlsx/styles.d.ts +70 -0
- package/dist/xlsx/styles.js +203 -0
- package/dist/xlsx/table.d.ts +12 -0
- package/dist/xlsx/table.js +35 -0
- package/dist/xlsx/types.d.ts +180 -0
- package/dist/xlsx/types.js +1 -0
- package/dist/xlsx/validation-types.d.ts +18 -0
- package/dist/xlsx/validation-types.js +1 -0
- package/dist/xlsx/validation.d.ts +1 -0
- package/dist/xlsx/validation.js +92 -0
- package/dist/xlsx/values.d.ts +13 -0
- package/dist/xlsx/values.js +87 -0
- package/dist/xlsx/workbook.d.ts +50 -0
- package/dist/xlsx/workbook.js +452 -0
- package/dist/xlsx/worksheet.d.ts +71 -0
- package/dist/xlsx/worksheet.js +700 -0
- package/dist/xlsx/write.d.ts +15 -0
- package/dist/xlsx/write.js +47 -0
- package/dist/xml/index.d.ts +24 -0
- package/dist/xml/index.js +74 -0
- package/dist/zip/entry.d.ts +39 -0
- package/dist/zip/entry.js +122 -0
- package/dist/zip/index.d.ts +44 -0
- package/dist/zip/index.js +248 -0
- package/package.json +104 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Column, ExportResult, OutputDestination } from "../core/index.js";
|
|
2
|
+
import type { PortableWorkbookOptions, PortableSheetOptions } from "./portable.js";
|
|
3
|
+
/** One table, using the same workbook engine as the advanced multi-worksheet API. */
|
|
4
|
+
export interface XlsxOptions<T = never> extends PortableWorkbookOptions {
|
|
5
|
+
readonly columns: readonly (Column<T> & {
|
|
6
|
+
readonly style?: never;
|
|
7
|
+
})[];
|
|
8
|
+
readonly sheet?: PortableSheetOptions & {
|
|
9
|
+
readonly name?: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Write one worksheet to a Blob. Async sources are consumed once; call a source factory for each export. */
|
|
13
|
+
export declare function writeXlsx<T extends object>(rows: Iterable<T> | AsyncIterable<T>, options: XlsxOptions<NoInfer<T>>): Promise<Blob>;
|
|
14
|
+
/** Await accepted bytes without closing/aborting the caller's destination. Its partial bytes remain caller-owned. */
|
|
15
|
+
export declare function writeXlsxTo<T extends object>(rows: Iterable<T> | AsyncIterable<T>, destination: OutputDestination, options: XlsxOptions<NoInfer<T>>): Promise<ExportResult>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { withDestination } from "../core/sinks.js";
|
|
2
|
+
import { copyColumns } from "../internal/rows.js";
|
|
3
|
+
import { Workbook } from "./workbook.js";
|
|
4
|
+
import { portableSheet, portableWorkbook } from "./portable.js";
|
|
5
|
+
import { captureFields } from "../internal/capture.js";
|
|
6
|
+
import { sheetOptionFields, workbookOptionFields } from "./options.js";
|
|
7
|
+
function prepare(options) {
|
|
8
|
+
options = captureFields(options, [...workbookOptionFields, "columns", "sheet"]);
|
|
9
|
+
const columns = copyColumns(options.columns), sheet = options.sheet && captureFields(options.sheet, [...sheetOptionFields, "name"]);
|
|
10
|
+
return { ...portableWorkbook(options), columns, sheet: { ...portableSheet(sheet), ...(sheet?.name === undefined ? {} : { name: sheet.name }) } };
|
|
11
|
+
}
|
|
12
|
+
function worksheet(book, options) {
|
|
13
|
+
const { name = "Data", ...sheet } = options.sheet ?? {};
|
|
14
|
+
return book.addWorksheet(name, { boldHeader: true, autoFilter: sheet.includeHeader !== false,
|
|
15
|
+
autoSize: { minWidth: 6, maxWidth: 54 }, ...sheet, columns: options.columns });
|
|
16
|
+
}
|
|
17
|
+
export async function writeXlsx(rows, configuration) {
|
|
18
|
+
const options = prepare(configuration);
|
|
19
|
+
const { columns: _columns, sheet: _sheet, ...settings } = options;
|
|
20
|
+
const book = Workbook.forTable(settings);
|
|
21
|
+
try {
|
|
22
|
+
await worksheet(book, options).addRows(rows);
|
|
23
|
+
return await book.toBlob();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
await book.discard(error);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function writeXlsxTo(rows, destination, configuration) {
|
|
31
|
+
const options = prepare(configuration);
|
|
32
|
+
return withDestination(destination, async (sink) => {
|
|
33
|
+
const { columns: _columns, sheet: _sheet, ...settings } = options;
|
|
34
|
+
const book = Workbook.forTable({ ...settings, sink });
|
|
35
|
+
try {
|
|
36
|
+
const sheet = worksheet(book, options);
|
|
37
|
+
const columns = options.columns.length;
|
|
38
|
+
await sheet.addRows(rows);
|
|
39
|
+
const result = await book.finish();
|
|
40
|
+
return { rows: result.rows, columns, bytes: result.bytes };
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
await book.discard(error);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ByteSink } from "../core/sinks.js";
|
|
2
|
+
export type InvalidCharacterPolicy = "strip" | "reject";
|
|
3
|
+
export declare const xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
|
|
4
|
+
/** XML 1.0 characters; Unicode mode retains valid surrogate pairs. */
|
|
5
|
+
export declare function cleanXml(value: unknown, policy?: InvalidCharacterPolicy): string;
|
|
6
|
+
/** Escape data, including whitespace that XML parsers otherwise normalize. */
|
|
7
|
+
export declare function escapeXml(value: unknown, policy?: InvalidCharacterPolicy): string;
|
|
8
|
+
/** Encode an OOXML ST_Xstring attribute without interpreting literal escape tokens as characters. */
|
|
9
|
+
export declare function escapeOoxmlAttribute(value: unknown, policy?: InvalidCharacterPolicy): string;
|
|
10
|
+
/** Names are schema-owned ASCII QNames; data is accepted only as text or attribute values. */
|
|
11
|
+
export declare function validateXmlName(name: string): string;
|
|
12
|
+
export declare class XmlWriter {
|
|
13
|
+
private readonly policy;
|
|
14
|
+
private readonly sink;
|
|
15
|
+
private readonly stack;
|
|
16
|
+
private roots;
|
|
17
|
+
private closed;
|
|
18
|
+
constructor(sink: ByteSink, policy?: InvalidCharacterPolicy, signal?: AbortSignal);
|
|
19
|
+
startElement(name: string, attributes?: Readonly<Record<string, unknown>>): Promise<void>;
|
|
20
|
+
text(value: unknown): Promise<void>;
|
|
21
|
+
endElement(): Promise<void>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
private open;
|
|
24
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
2
|
+
import { ChunkedTextSink } from "../core/sinks.js";
|
|
3
|
+
export const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
|
|
4
|
+
/** XML 1.0 characters; Unicode mode retains valid surrogate pairs. */
|
|
5
|
+
export function cleanXml(value, policy = "strip") {
|
|
6
|
+
if (policy !== "strip" && policy !== "reject")
|
|
7
|
+
throw new TypeError("Invalid XML character policy.");
|
|
8
|
+
const text = String(value), invalid = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/gu;
|
|
9
|
+
if (policy === "reject" && invalid.test(text))
|
|
10
|
+
throw new OfficeIMOError("INVALID_XML", "Text contains XML 1.0-invalid characters.");
|
|
11
|
+
return text.replace(invalid, "");
|
|
12
|
+
}
|
|
13
|
+
/** Escape data, including whitespace that XML parsers otherwise normalize. */
|
|
14
|
+
export function escapeXml(value, policy = "strip") {
|
|
15
|
+
const escapes = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'",
|
|
16
|
+
"\r": " ", "\n": " ", "\t": "	" };
|
|
17
|
+
return cleanXml(value, policy).replace(/[&<>"'\r\n\t]/g, c => escapes[c]);
|
|
18
|
+
}
|
|
19
|
+
/** Encode an OOXML ST_Xstring attribute without interpreting literal escape tokens as characters. */
|
|
20
|
+
export function escapeOoxmlAttribute(value, policy = "strip") {
|
|
21
|
+
const whitespace = { "\r": "_x000D_", "\n": "_x000A_", "\t": "_x0009_" };
|
|
22
|
+
const text = cleanXml(value, policy).replace(/_(?=x[0-9a-f]{4}_)|[\r\n\t]/gi, token => token === "_" ? "_x005F_" : whitespace[token]);
|
|
23
|
+
return escapeXml(text, policy);
|
|
24
|
+
}
|
|
25
|
+
/** Names are schema-owned ASCII QNames; data is accepted only as text or attribute values. */
|
|
26
|
+
export function validateXmlName(name) {
|
|
27
|
+
if (typeof name !== "string" || !/^[A-Za-z_][A-Za-z0-9_.-]*(?::[A-Za-z_][A-Za-z0-9_.-]*)?$/.test(name))
|
|
28
|
+
throw new OfficeIMOError("INVALID_XML", "Invalid XML name: " + name);
|
|
29
|
+
return name;
|
|
30
|
+
}
|
|
31
|
+
export class XmlWriter {
|
|
32
|
+
policy;
|
|
33
|
+
sink;
|
|
34
|
+
stack = [];
|
|
35
|
+
roots = 0;
|
|
36
|
+
closed = false;
|
|
37
|
+
constructor(sink, policy = "strip", signal) {
|
|
38
|
+
this.policy = policy;
|
|
39
|
+
cleanXml("", policy);
|
|
40
|
+
this.sink = new ChunkedTextSink(sink, signal);
|
|
41
|
+
this.sink.append(xmlDeclaration);
|
|
42
|
+
}
|
|
43
|
+
async startElement(name, attributes = {}) {
|
|
44
|
+
this.open();
|
|
45
|
+
validateXmlName(name);
|
|
46
|
+
const text = "<" + name + Object.entries(attributes).map(([key, value]) => " " + validateXmlName(key) + '="' + escapeXml(value, this.policy) + '"').join("") + ">";
|
|
47
|
+
if (!this.stack.length && this.roots++)
|
|
48
|
+
throw new OfficeIMOError("INVALID_XML", "XML must have one root element.");
|
|
49
|
+
this.stack.push(name);
|
|
50
|
+
await this.sink.write(text);
|
|
51
|
+
}
|
|
52
|
+
async text(value) {
|
|
53
|
+
this.open();
|
|
54
|
+
if (!this.stack.length)
|
|
55
|
+
throw new OfficeIMOError("INVALID_XML", "Text needs an open element.");
|
|
56
|
+
await this.sink.write(escapeXml(value, this.policy));
|
|
57
|
+
}
|
|
58
|
+
async endElement() {
|
|
59
|
+
this.open();
|
|
60
|
+
const name = this.stack.pop();
|
|
61
|
+
if (!name)
|
|
62
|
+
throw new OfficeIMOError("INVALID_XML", "No open XML element.");
|
|
63
|
+
await this.sink.write("</" + name + ">");
|
|
64
|
+
}
|
|
65
|
+
async close() {
|
|
66
|
+
this.open();
|
|
67
|
+
if (this.stack.length || this.roots !== 1)
|
|
68
|
+
throw new OfficeIMOError("INVALID_XML", "XML document is incomplete.");
|
|
69
|
+
await this.sink.close();
|
|
70
|
+
this.closed = true;
|
|
71
|
+
}
|
|
72
|
+
open() { if (this.closed)
|
|
73
|
+
throw new OfficeIMOError("INVALID_STATE", "XML writer is closed."); }
|
|
74
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ByteSink } from "../core/sinks.js";
|
|
2
|
+
export { Crc32 } from "../internal/crc32.js";
|
|
3
|
+
export type Compression = "auto" | "store";
|
|
4
|
+
export declare const zipLimit = 4294967295;
|
|
5
|
+
export declare function zipSize(value: number): number;
|
|
6
|
+
export interface EntryInfo {
|
|
7
|
+
readonly crc: number;
|
|
8
|
+
readonly size: number;
|
|
9
|
+
readonly compressedSize: number;
|
|
10
|
+
readonly method: 0 | 8;
|
|
11
|
+
}
|
|
12
|
+
export interface PreparedEntry extends EntryInfo {
|
|
13
|
+
readonly chunks: readonly Uint8Array[];
|
|
14
|
+
}
|
|
15
|
+
/** Internal compressor shared by buffered worksheets and the streaming ZIP surface. */
|
|
16
|
+
export declare class EntryWriter implements ByteSink {
|
|
17
|
+
private readonly sink;
|
|
18
|
+
private readonly signal?;
|
|
19
|
+
readonly method: 0 | 8;
|
|
20
|
+
private readonly writer;
|
|
21
|
+
private readonly reader;
|
|
22
|
+
private readonly drain;
|
|
23
|
+
private readonly crc;
|
|
24
|
+
private size;
|
|
25
|
+
private compressedSize;
|
|
26
|
+
private failed;
|
|
27
|
+
private failure;
|
|
28
|
+
private closed;
|
|
29
|
+
private cleaned;
|
|
30
|
+
private readonly lifetime;
|
|
31
|
+
private readonly abort;
|
|
32
|
+
constructor(compression: Compression, sink: ByteSink, signal?: AbortSignal | undefined);
|
|
33
|
+
private consume;
|
|
34
|
+
private fail;
|
|
35
|
+
write(bytes: Uint8Array): Promise<void>;
|
|
36
|
+
close(): Promise<EntryInfo>;
|
|
37
|
+
discard(error: unknown): Promise<void>;
|
|
38
|
+
private cleanup;
|
|
39
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { checkAbort, withAbort } from "../core/iteration.js";
|
|
2
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
3
|
+
import { Crc32 } from "../internal/crc32.js";
|
|
4
|
+
export { Crc32 } from "../internal/crc32.js";
|
|
5
|
+
export const zipLimit = 0xffffffff;
|
|
6
|
+
export function zipSize(value) {
|
|
7
|
+
if (!Number.isSafeInteger(value) || value < 0 || value >= zipLimit)
|
|
8
|
+
throw new OfficeIMOError("ZIP64_REQUIRED", "Classic ZIP entries and archives must stay below 4 GiB; ZIP64 is not supported yet.");
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
/** Internal compressor shared by buffered worksheets and the streaming ZIP surface. */
|
|
12
|
+
export class EntryWriter {
|
|
13
|
+
sink;
|
|
14
|
+
signal;
|
|
15
|
+
method;
|
|
16
|
+
writer;
|
|
17
|
+
reader;
|
|
18
|
+
drain;
|
|
19
|
+
crc = new Crc32();
|
|
20
|
+
size = 0;
|
|
21
|
+
compressedSize = 0;
|
|
22
|
+
failed = false;
|
|
23
|
+
failure;
|
|
24
|
+
closed = false;
|
|
25
|
+
cleaned = false;
|
|
26
|
+
lifetime = new AbortController();
|
|
27
|
+
abort = () => { this.fail(this.signal?.reason); };
|
|
28
|
+
constructor(compression, sink, signal) {
|
|
29
|
+
this.sink = sink;
|
|
30
|
+
this.signal = signal;
|
|
31
|
+
if (compression !== "auto" && compression !== "store")
|
|
32
|
+
throw new RangeError("compression must be auto or store.");
|
|
33
|
+
checkAbort(signal);
|
|
34
|
+
let stream;
|
|
35
|
+
if (compression === "auto" && typeof CompressionStream === "function") {
|
|
36
|
+
try {
|
|
37
|
+
stream = new CompressionStream("deflate-raw");
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (!(error instanceof TypeError))
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this.method = stream ? 8 : 0;
|
|
45
|
+
this.writer = stream?.writable.getWriter();
|
|
46
|
+
this.reader = stream?.readable.getReader();
|
|
47
|
+
signal?.addEventListener("abort", this.abort, { once: true });
|
|
48
|
+
this.drain = this.reader ? this.consume(this.reader) : Promise.resolve();
|
|
49
|
+
}
|
|
50
|
+
async consume(reader) {
|
|
51
|
+
try {
|
|
52
|
+
while (true) {
|
|
53
|
+
const { value, done } = await withAbort(reader.read(), this.lifetime.signal);
|
|
54
|
+
if (done)
|
|
55
|
+
return;
|
|
56
|
+
checkAbort(this.signal);
|
|
57
|
+
this.compressedSize = zipSize(this.compressedSize + value.length);
|
|
58
|
+
await withAbort(Promise.resolve(this.sink.write(value)), this.lifetime.signal);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
this.fail(error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
fail(error) {
|
|
66
|
+
if (this.failed)
|
|
67
|
+
return;
|
|
68
|
+
this.failed = true;
|
|
69
|
+
this.failure = error;
|
|
70
|
+
this.lifetime.abort(error);
|
|
71
|
+
if (!this.cleaned) {
|
|
72
|
+
this.writer?.abort(error).catch(() => { });
|
|
73
|
+
this.reader?.cancel(error).catch(() => { });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async write(bytes) {
|
|
77
|
+
checkAbort(this.signal);
|
|
78
|
+
if (this.failed)
|
|
79
|
+
throw this.failure;
|
|
80
|
+
if (this.closed)
|
|
81
|
+
throw new OfficeIMOError("INVALID_STATE", "ZIP entry is closed.");
|
|
82
|
+
if (!(bytes instanceof Uint8Array))
|
|
83
|
+
throw new TypeError("ZIP chunks must be Uint8Array.");
|
|
84
|
+
this.size = zipSize(this.size + bytes.length);
|
|
85
|
+
this.crc.update(bytes);
|
|
86
|
+
if (this.writer)
|
|
87
|
+
await withAbort(this.writer.write(new Uint8Array(bytes)), this.lifetime.signal);
|
|
88
|
+
else {
|
|
89
|
+
await withAbort(Promise.resolve(this.sink.write(bytes)), this.lifetime.signal);
|
|
90
|
+
this.compressedSize = this.size;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async close() {
|
|
94
|
+
try {
|
|
95
|
+
checkAbort(this.signal);
|
|
96
|
+
if (this.failed)
|
|
97
|
+
throw this.failure;
|
|
98
|
+
if (this.closed)
|
|
99
|
+
throw new OfficeIMOError("INVALID_STATE", "ZIP entry is closed.");
|
|
100
|
+
this.closed = true;
|
|
101
|
+
if (this.writer)
|
|
102
|
+
await withAbort(this.writer.close(), this.lifetime.signal);
|
|
103
|
+
await this.drain;
|
|
104
|
+
if (this.failed)
|
|
105
|
+
throw this.failure;
|
|
106
|
+
checkAbort(this.signal);
|
|
107
|
+
return { crc: this.crc.value, size: this.size, compressedSize: this.compressedSize, method: this.method };
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
this.cleanup();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async discard(error) { this.fail(error); await this.drain; this.closed = true; this.cleanup(); }
|
|
114
|
+
cleanup() {
|
|
115
|
+
if (this.cleaned)
|
|
116
|
+
return;
|
|
117
|
+
this.cleaned = true;
|
|
118
|
+
this.signal?.removeEventListener("abort", this.abort);
|
|
119
|
+
this.writer?.releaseLock();
|
|
120
|
+
this.reader?.releaseLock();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export { Crc32 } from "./entry.js";
|
|
2
|
+
export type { Compression, EntryInfo } from "./entry.js";
|
|
3
|
+
import type { Compression, EntryInfo } from "./entry.js";
|
|
4
|
+
import type { ByteSink, ByteSource } from "../core/sinks.js";
|
|
5
|
+
export interface ZipWriterOptions {
|
|
6
|
+
readonly compression?: Compression;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
readonly maxOutputBytes?: number;
|
|
9
|
+
}
|
|
10
|
+
/** An appendable entry. Await writes and close it before opening another entry. */
|
|
11
|
+
export interface ZipEntry extends ByteSink {
|
|
12
|
+
close(): Promise<EntryInfo>;
|
|
13
|
+
discard(error: unknown): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export type ZipEntrySource = ByteSource | ((sink: ByteSink) => void | Promise<void>);
|
|
16
|
+
export declare function validateEntryName(name: string): string;
|
|
17
|
+
/** Sequential ZIP writer. Entry bytes use data descriptors; only central-directory metadata is retained. */
|
|
18
|
+
export declare class ZipWriter {
|
|
19
|
+
private readonly sink;
|
|
20
|
+
private readonly owned;
|
|
21
|
+
private readonly names;
|
|
22
|
+
private readonly central;
|
|
23
|
+
private offset;
|
|
24
|
+
private active;
|
|
25
|
+
private state;
|
|
26
|
+
private readonly options;
|
|
27
|
+
private readonly lifetime;
|
|
28
|
+
private disposal;
|
|
29
|
+
constructor(sink?: ByteSink, options?: ZipWriterOptions);
|
|
30
|
+
/** Pending header/directory writes and source reads must also stop on explicit discard. */
|
|
31
|
+
private acceptance;
|
|
32
|
+
private emit;
|
|
33
|
+
private reserve;
|
|
34
|
+
private local;
|
|
35
|
+
private record;
|
|
36
|
+
openEntry(name: string): Promise<ZipEntry>;
|
|
37
|
+
add(name: string, source: ZipEntrySource): Promise<void>;
|
|
38
|
+
get bytesWritten(): number;
|
|
39
|
+
/** Drop owned output and release the active compressor. Caller-owned partial bytes remain with the caller. */
|
|
40
|
+
discard(error: unknown): Promise<void>;
|
|
41
|
+
/** Complete directory; does not close a caller-owned sink. A failed sink owns disposal of partial bytes. */
|
|
42
|
+
finish(): Promise<void>;
|
|
43
|
+
toBlob(type?: string): Promise<Blob>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
export { Crc32 } from "./entry.js";
|
|
2
|
+
import { EntryWriter, zipSize } from "./entry.js";
|
|
3
|
+
import { checkAbort, withAbort, pause, taskYieldDue } from "../core/iteration.js";
|
|
4
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
5
|
+
import { BlobByteSink, writeBytes } from "../core/sinks.js";
|
|
6
|
+
import { captureFields } from "../internal/capture.js";
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
export function validateEntryName(name) {
|
|
9
|
+
if (typeof name !== "string" || !name || /[\u0000-\u001f\\]/.test(name) || name.startsWith("/") ||
|
|
10
|
+
/^[A-Za-z]:/.test(name) || name.split("/").some(s => !s || s === "." || s === ".."))
|
|
11
|
+
throw new TypeError("ZIP entry names must be relative paths without empty, dot or parent segments.");
|
|
12
|
+
for (let i = 0; i < name.length; i++) {
|
|
13
|
+
const unit = name.charCodeAt(i);
|
|
14
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
15
|
+
const next = name.charCodeAt(++i);
|
|
16
|
+
if (!(next >= 0xdc00 && next <= 0xdfff))
|
|
17
|
+
throw new TypeError("ZIP entry names must contain well-formed Unicode.");
|
|
18
|
+
}
|
|
19
|
+
else if (unit >= 0xdc00 && unit <= 0xdfff)
|
|
20
|
+
throw new TypeError("ZIP entry names must contain well-formed Unicode.");
|
|
21
|
+
}
|
|
22
|
+
if (encoder.encode(name).length > 65535)
|
|
23
|
+
throw new RangeError("ZIP entry name exceeds 65,535 UTF-8 bytes.");
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
function header(signature, length, name = new Uint8Array()) {
|
|
27
|
+
const bytes = new Uint8Array(length + name.length), data = new DataView(bytes.buffer);
|
|
28
|
+
data.setUint32(0, signature, true);
|
|
29
|
+
bytes.set(name, length);
|
|
30
|
+
return { bytes, data };
|
|
31
|
+
}
|
|
32
|
+
/** Sequential ZIP writer. Entry bytes use data descriptors; only central-directory metadata is retained. */
|
|
33
|
+
export class ZipWriter {
|
|
34
|
+
sink;
|
|
35
|
+
owned;
|
|
36
|
+
names = new Set();
|
|
37
|
+
central = [];
|
|
38
|
+
offset = 0;
|
|
39
|
+
active;
|
|
40
|
+
state = "open";
|
|
41
|
+
options;
|
|
42
|
+
lifetime = new AbortController();
|
|
43
|
+
disposal;
|
|
44
|
+
constructor(sink, options = {}) {
|
|
45
|
+
const captured = captureFields(options, ["compression", "signal", "maxOutputBytes"]);
|
|
46
|
+
const { compression, maxOutputBytes } = captured;
|
|
47
|
+
if (maxOutputBytes !== undefined && (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes < 0))
|
|
48
|
+
throw new RangeError("maxOutputBytes must be a nonnegative safe integer.");
|
|
49
|
+
if (compression !== undefined && compression !== "auto" && compression !== "store")
|
|
50
|
+
throw new RangeError("compression must be auto or store.");
|
|
51
|
+
this.options = Object.freeze(captured);
|
|
52
|
+
this.owned = sink ? undefined : new BlobByteSink();
|
|
53
|
+
this.sink = sink ?? this.owned;
|
|
54
|
+
}
|
|
55
|
+
/** Pending header/directory writes and source reads must also stop on explicit discard. */
|
|
56
|
+
async acceptance(operation) {
|
|
57
|
+
const signal = this.options.signal;
|
|
58
|
+
const abort = () => this.lifetime.abort(signal?.reason);
|
|
59
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
60
|
+
try {
|
|
61
|
+
checkAbort(signal);
|
|
62
|
+
checkAbort(this.lifetime.signal);
|
|
63
|
+
return await withAbort(Promise.resolve(operation()), this.lifetime.signal);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
signal?.removeEventListener("abort", abort);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async emit(bytes) {
|
|
70
|
+
if (this.options.maxOutputBytes !== undefined && this.offset + bytes.length > this.options.maxOutputBytes)
|
|
71
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxOutputBytes exceeded.");
|
|
72
|
+
this.offset = zipSize(this.offset + bytes.length);
|
|
73
|
+
await this.acceptance(() => this.sink.write(bytes));
|
|
74
|
+
}
|
|
75
|
+
reserve(name) {
|
|
76
|
+
checkAbort(this.options.signal);
|
|
77
|
+
if (this.state !== "open")
|
|
78
|
+
throw new OfficeIMOError("INVALID_STATE", "ZIP writer is busy, failed or finalized.");
|
|
79
|
+
validateEntryName(name);
|
|
80
|
+
if (this.names.has(name))
|
|
81
|
+
throw new TypeError("Duplicate ZIP entry: " + name);
|
|
82
|
+
if (this.names.size >= 65534)
|
|
83
|
+
throw new OfficeIMOError("ZIP64_REQUIRED", "ZIP64 is required for 65,535 entries.");
|
|
84
|
+
this.names.add(name);
|
|
85
|
+
this.state = "writing";
|
|
86
|
+
return encoder.encode(name);
|
|
87
|
+
}
|
|
88
|
+
local(name, info, descriptor) {
|
|
89
|
+
const h = header(0x04034b50, 30, name);
|
|
90
|
+
h.data.setUint16(4, 20, true);
|
|
91
|
+
h.data.setUint16(6, descriptor ? 0x808 : 0x800, true);
|
|
92
|
+
h.data.setUint16(8, info.method, true);
|
|
93
|
+
h.data.setUint16(12, 33, true);
|
|
94
|
+
h.data.setUint16(26, name.length, true);
|
|
95
|
+
return h.bytes;
|
|
96
|
+
}
|
|
97
|
+
record(name, info, offset, descriptor) {
|
|
98
|
+
const h = header(0x02014b50, 46, name);
|
|
99
|
+
h.data.setUint16(4, 20, true);
|
|
100
|
+
h.data.setUint16(6, 20, true);
|
|
101
|
+
h.data.setUint16(8, descriptor ? 0x808 : 0x800, true);
|
|
102
|
+
h.data.setUint16(10, info.method, true);
|
|
103
|
+
h.data.setUint16(14, 33, true);
|
|
104
|
+
h.data.setUint32(16, info.crc, true);
|
|
105
|
+
h.data.setUint32(20, info.compressedSize, true);
|
|
106
|
+
h.data.setUint32(24, info.size, true);
|
|
107
|
+
h.data.setUint16(28, name.length, true);
|
|
108
|
+
h.data.setUint32(42, offset, true);
|
|
109
|
+
this.central.push(h.bytes);
|
|
110
|
+
}
|
|
111
|
+
async openEntry(name) {
|
|
112
|
+
const encoded = this.reserve(name), offset = this.offset;
|
|
113
|
+
let entry;
|
|
114
|
+
try {
|
|
115
|
+
entry = new EntryWriter(this.options.compression ?? "auto", { write: bytes => this.emit(bytes) }, this.options.signal);
|
|
116
|
+
await this.emit(this.local(encoded, entry, true));
|
|
117
|
+
const writer = entry;
|
|
118
|
+
let busy = false, closed = false, completion;
|
|
119
|
+
const discard = async (error) => { closed = true; this.state = "failed"; this.lifetime.abort(error); await writer.discard(error); this.owned?.discard(); this.active = undefined; };
|
|
120
|
+
return this.active = {
|
|
121
|
+
write: async (bytes) => {
|
|
122
|
+
if (busy || closed || this.state !== "writing")
|
|
123
|
+
throw new OfficeIMOError("INVALID_STATE", "Await the current ZIP entry operation.");
|
|
124
|
+
busy = true;
|
|
125
|
+
try {
|
|
126
|
+
await writer.write(bytes);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
await discard(error);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
busy = false;
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
close: () => {
|
|
137
|
+
if (completion)
|
|
138
|
+
return completion;
|
|
139
|
+
if (busy || closed || this.state !== "writing")
|
|
140
|
+
throw new OfficeIMOError("INVALID_STATE", "ZIP entry is busy or closed.");
|
|
141
|
+
closed = true;
|
|
142
|
+
completion = (async () => {
|
|
143
|
+
try {
|
|
144
|
+
const info = await writer.close(), descriptor = header(0x08074b50, 16);
|
|
145
|
+
descriptor.data.setUint32(4, info.crc, true);
|
|
146
|
+
descriptor.data.setUint32(8, info.compressedSize, true);
|
|
147
|
+
descriptor.data.setUint32(12, info.size, true);
|
|
148
|
+
await this.emit(descriptor.bytes);
|
|
149
|
+
this.record(encoded, info, offset, true);
|
|
150
|
+
this.state = "open";
|
|
151
|
+
this.active = undefined;
|
|
152
|
+
return info;
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
await discard(error);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
})();
|
|
159
|
+
return completion;
|
|
160
|
+
}, discard
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
this.state = "failed";
|
|
165
|
+
await entry?.discard(error);
|
|
166
|
+
this.owned?.discard();
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async add(name, source) {
|
|
171
|
+
const entry = await this.openEntry(name);
|
|
172
|
+
try {
|
|
173
|
+
if (typeof source === "function")
|
|
174
|
+
await this.acceptance(() => source(entry));
|
|
175
|
+
else
|
|
176
|
+
await this.acceptance(() => writeBytes(source, entry, this.lifetime.signal));
|
|
177
|
+
await entry.close();
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
await entry.discard(error);
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
get bytesWritten() { return this.offset; }
|
|
185
|
+
/** Drop owned output and release the active compressor. Caller-owned partial bytes remain with the caller. */
|
|
186
|
+
discard(error) {
|
|
187
|
+
this.state = "failed";
|
|
188
|
+
this.lifetime.abort(error);
|
|
189
|
+
return this.disposal ??= (async () => { await this.active?.discard(error); this.owned?.discard(); })();
|
|
190
|
+
}
|
|
191
|
+
/** @internal Buffered worksheet compression stays in the format owner. */
|
|
192
|
+
async addPrepared(name, entry) {
|
|
193
|
+
const encoded = this.reserve(name), offset = this.offset;
|
|
194
|
+
try {
|
|
195
|
+
const local = this.local(encoded, entry, false), data = new DataView(local.buffer);
|
|
196
|
+
data.setUint32(14, entry.crc, true);
|
|
197
|
+
data.setUint32(18, entry.compressedSize, true);
|
|
198
|
+
data.setUint32(22, entry.size, true);
|
|
199
|
+
await this.emit(local);
|
|
200
|
+
for (const bytes of entry.chunks) {
|
|
201
|
+
checkAbort(this.options.signal);
|
|
202
|
+
await this.emit(bytes);
|
|
203
|
+
}
|
|
204
|
+
this.record(encoded, entry, offset, false);
|
|
205
|
+
this.state = "open";
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
this.state = "failed";
|
|
209
|
+
this.owned?.discard();
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Complete directory; does not close a caller-owned sink. A failed sink owns disposal of partial bytes. */
|
|
214
|
+
async finish() {
|
|
215
|
+
checkAbort(this.options.signal);
|
|
216
|
+
if (this.state !== "open")
|
|
217
|
+
throw new OfficeIMOError("INVALID_STATE", "ZIP writer is busy, failed or finalized.");
|
|
218
|
+
this.state = "writing";
|
|
219
|
+
const offset = this.offset;
|
|
220
|
+
try {
|
|
221
|
+
for (let i = 0; i < this.central.length; i++) {
|
|
222
|
+
await this.emit(this.central[i]);
|
|
223
|
+
if (i % 256 === 255 && taskYieldDue())
|
|
224
|
+
await pause();
|
|
225
|
+
}
|
|
226
|
+
const end = header(0x06054b50, 22);
|
|
227
|
+
end.data.setUint16(8, this.central.length, true);
|
|
228
|
+
end.data.setUint16(10, this.central.length, true);
|
|
229
|
+
end.data.setUint32(12, this.offset - offset, true);
|
|
230
|
+
end.data.setUint32(16, offset, true);
|
|
231
|
+
await this.emit(end.bytes);
|
|
232
|
+
checkAbort(this.options.signal);
|
|
233
|
+
this.state = "finished";
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
this.state = "failed";
|
|
237
|
+
this.owned?.discard();
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async toBlob(type = "application/zip") {
|
|
242
|
+
if (!this.owned)
|
|
243
|
+
throw new OfficeIMOError("INVALID_STATE", "This ZIP uses a caller-owned sink; use finish().");
|
|
244
|
+
if (this.state !== "finished")
|
|
245
|
+
await this.finish();
|
|
246
|
+
return this.owned.toBlob(type);
|
|
247
|
+
}
|
|
248
|
+
}
|