@stll/docx-core 0.39.1 → 0.40.0
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/dist/archive-yBR7X-Qc.js +456 -0
- package/dist/{document-BkGU2ctn.d.ts → document-BWDHON3T.d.ts} +1 -47
- package/dist/{document-oU-dz9zs.js → document-NtScxyHH.js} +18 -36
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/errors-Dm3jsb5q.d.ts +57 -0
- package/dist/format-DXPXB02u.js +378 -0
- package/dist/{href-2XSCK-T8.js → href-ogmJmncc.js} +2 -4
- package/dist/index.d.ts +1 -34
- package/dist/index.js +125 -228
- package/dist/model/document.d.ts +2 -2
- package/dist/model/document.js +1 -1
- package/dist/ops/documentOps.d.ts +1 -61
- package/dist/ops/documentOps.js +55 -108
- package/dist/{preservedMarkup-G8XjWhs2.d.ts → preservedMarkup-9L4lbGnE.d.ts} +0 -2
- package/dist/projection.d.ts +0 -4
- package/dist/projection.js +2 -4
- package/dist/schema/sequenceChildren.d.ts +1 -5
- package/dist/schema/sequenceChildren.js +1 -1
- package/dist/{sequenceChildren-9SUK1Tm1.js → sequenceChildren-CrMcryEF.js} +2 -4
- package/dist/zip/archive.d.ts +114 -0
- package/dist/zip/archive.js +3 -0
- package/dist/zip/stream.d.ts +30 -0
- package/dist/zip/stream.js +117 -0
- package/package.json +10 -6
- package/dist/zip/writeZipPart.d.ts +0 -12
- package/dist/zip/writeZipPart.js +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { panic } from "better-result";
|
|
2
|
-
|
|
2
|
+
// src/model/runPropertyReview.ts
|
|
3
3
|
/** A run-property container has one pending formatting revision. */
|
|
4
4
|
const getSingularRunPropertyChange = (propertyChanges) => {
|
|
5
5
|
const propertyChangeCount = propertyChanges?.length ?? 0;
|
|
@@ -9,8 +9,7 @@ const getSingularRunPropertyChange = (propertyChanges) => {
|
|
|
9
9
|
});
|
|
10
10
|
return propertyChanges?.at(0);
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
//#region src/model/canonical.ts
|
|
12
|
+
// src/model/canonical.ts
|
|
14
13
|
/** Set modeled comments while keeping an empty comments part out of the document model. */
|
|
15
14
|
const setDocumentBodyComments = (body, comments) => {
|
|
16
15
|
if (comments.length === 0) {
|
|
@@ -31,8 +30,7 @@ const finalizeParagraphFormatting = (formatting) => {
|
|
|
31
30
|
};
|
|
32
31
|
/** Whether paragraph formatting states no modeled properties. */
|
|
33
32
|
const isCanonicallyEmptyParagraphFormatting = (formatting) => formatting === void 0 || Object.keys(formatting).every((key) => key === "sourceXml");
|
|
34
|
-
|
|
35
|
-
//#region src/model/parseWarning.ts
|
|
33
|
+
// src/model/parseWarning.ts
|
|
36
34
|
/**
|
|
37
35
|
* What folio normalised to open a document, as data rather than as prose.
|
|
38
36
|
*
|
|
@@ -132,8 +130,7 @@ const IMPORTED_ID_COLLISION_KINDS = {
|
|
|
132
130
|
* thousand records. Occurrences past the cap are counted, not kept.
|
|
133
131
|
*/
|
|
134
132
|
const MAX_RETAINED_PARSE_WARNINGS_PER_CODE = 20;
|
|
135
|
-
|
|
136
|
-
//#region src/model/paragraphNumbering.ts
|
|
133
|
+
// src/model/paragraphNumbering.ts
|
|
137
134
|
/**
|
|
138
135
|
* `w:numPr`: what a tier states about a paragraph's numbering, and what the
|
|
139
136
|
* cascade resolves that to.
|
|
@@ -300,8 +297,7 @@ const sameEffectiveParagraphNumbering = (left, right) => {
|
|
|
300
297
|
if (resolvedLeft.kind === "none" || resolvedRight.kind === "none") return resolvedLeft.kind === resolvedRight.kind;
|
|
301
298
|
return resolvedLeft.numId === resolvedRight.numId && resolvedLeft.ilvl === resolvedRight.ilvl;
|
|
302
299
|
};
|
|
303
|
-
|
|
304
|
-
//#region src/model/numberingLevel.ts
|
|
300
|
+
// src/model/numberingLevel.ts
|
|
305
301
|
/** A w:ilvl outside the nine numbering levels names no level. */
|
|
306
302
|
const NUMBERING_LEVELS = /* @__PURE__ */ new Set([
|
|
307
303
|
0,
|
|
@@ -316,8 +312,7 @@ const NUMBERING_LEVELS = /* @__PURE__ */ new Set([
|
|
|
316
312
|
]);
|
|
317
313
|
/** Resolve only defined numbering levels; preserve unsupported source values elsewhere. */
|
|
318
314
|
const isNumberingLevel = (value) => NUMBERING_LEVELS.has(value);
|
|
319
|
-
|
|
320
|
-
//#region src/model/outlineLevel.ts
|
|
315
|
+
// src/model/outlineLevel.ts
|
|
321
316
|
/**
|
|
322
317
|
* `w:outlineLvl`: nine heading levels and one reserved "not a heading".
|
|
323
318
|
*
|
|
@@ -383,8 +378,7 @@ const outlineLevelStatedValue = (outlineLevel) => {
|
|
|
383
378
|
* cascade still has a tier left to ask.
|
|
384
379
|
*/
|
|
385
380
|
const headingLevelOf = (outlineLevel) => outlineLevel?.kind === "heading" ? outlineLevel.level : void 0;
|
|
386
|
-
|
|
387
|
-
//#region src/model/relationshipId.ts
|
|
381
|
+
// src/model/relationshipId.ts
|
|
388
382
|
/**
|
|
389
383
|
* Whether a string names a relationship. The refinement is a type predicate
|
|
390
384
|
* rather than a cast, so no site that mints an id needs an assertion.
|
|
@@ -409,8 +403,7 @@ const mintRelationshipId = (ordinal) => {
|
|
|
409
403
|
const id = `rId${ordinal}`;
|
|
410
404
|
return isRelationshipId(id) ? id : panic("A minted relationship id names nothing.");
|
|
411
405
|
};
|
|
412
|
-
|
|
413
|
-
//#region src/model/themeColor.gen.ts
|
|
406
|
+
// src/model/themeColor.gen.ts
|
|
414
407
|
/**
|
|
415
408
|
* GENERATED FILE — do not edit.
|
|
416
409
|
*
|
|
@@ -474,8 +467,7 @@ const SCHEME_COLOR_VALUES = [
|
|
|
474
467
|
"dk2",
|
|
475
468
|
"lt2"
|
|
476
469
|
];
|
|
477
|
-
|
|
478
|
-
//#region src/model/themeColor.ts
|
|
470
|
+
// src/model/themeColor.ts
|
|
479
471
|
/**
|
|
480
472
|
* The one mapping between a WordprocessingML theme reference and a theme slot.
|
|
481
473
|
*
|
|
@@ -636,8 +628,7 @@ const themeColorSlot = (value) => {
|
|
|
636
628
|
default: return target;
|
|
637
629
|
}
|
|
638
630
|
};
|
|
639
|
-
|
|
640
|
-
//#region src/model/ooxmlEnumerations.gen.ts
|
|
631
|
+
// src/model/ooxmlEnumerations.gen.ts
|
|
641
632
|
/**
|
|
642
633
|
* GENERATED FILE — do not edit.
|
|
643
634
|
*
|
|
@@ -839,8 +830,7 @@ const NUMBER_FORMATS = [
|
|
|
839
830
|
"dollarText",
|
|
840
831
|
"custom"
|
|
841
832
|
];
|
|
842
|
-
|
|
843
|
-
//#region src/model/borderStyle.gen.ts
|
|
833
|
+
// src/model/borderStyle.gen.ts
|
|
844
834
|
/**
|
|
845
835
|
* GENERATED FILE — do not edit.
|
|
846
836
|
*
|
|
@@ -1052,8 +1042,7 @@ const BORDER_STYLES = [
|
|
|
1052
1042
|
"zigZagStitch",
|
|
1053
1043
|
"custom"
|
|
1054
1044
|
];
|
|
1055
|
-
|
|
1056
|
-
//#region src/model/borderStyle.ts
|
|
1045
|
+
// src/model/borderStyle.ts
|
|
1057
1046
|
/**
|
|
1058
1047
|
* The one place a `CT_Border` `w:val` is decided.
|
|
1059
1048
|
*
|
|
@@ -1093,8 +1082,7 @@ const isBorderNil = (style) => style === "nil";
|
|
|
1093
1082
|
* the value keeps it.
|
|
1094
1083
|
*/
|
|
1095
1084
|
const statesNoBorder = (style) => isBorderNone(style) || isBorderNil(style);
|
|
1096
|
-
|
|
1097
|
-
//#region src/model/presetLineDash.gen.ts
|
|
1085
|
+
// src/model/presetLineDash.gen.ts
|
|
1098
1086
|
/**
|
|
1099
1087
|
* GENERATED FILE — do not edit.
|
|
1100
1088
|
*
|
|
@@ -1122,8 +1110,7 @@ const PRESET_LINE_DASH_VALS = [
|
|
|
1122
1110
|
"sysDashDot",
|
|
1123
1111
|
"sysDashDotDot"
|
|
1124
1112
|
];
|
|
1125
|
-
|
|
1126
|
-
//#region src/model/presetLineDash.ts
|
|
1113
|
+
// src/model/presetLineDash.ts
|
|
1127
1114
|
/**
|
|
1128
1115
|
* The one place an `a:ln/a:prstDash` `@val` is decided.
|
|
1129
1116
|
*
|
|
@@ -1152,8 +1139,7 @@ const presetLineDashFrom = (value) => isPresetLineDashVal(value) ? value : {
|
|
|
1152
1139
|
};
|
|
1153
1140
|
/** The token to write back, whether or not the schema declares it. */
|
|
1154
1141
|
const presetLineDashToken = (dash) => typeof dash === "string" ? dash : dash.raw;
|
|
1155
|
-
|
|
1156
|
-
//#region src/model/lists.ts
|
|
1142
|
+
// src/model/lists.ts
|
|
1157
1143
|
/**
|
|
1158
1144
|
* What the marker renderer counts in: folio's own vocabulary, not the format's.
|
|
1159
1145
|
*
|
|
@@ -1169,8 +1155,7 @@ const COUNTER_FORMATS = [
|
|
|
1169
1155
|
"decimalZero4",
|
|
1170
1156
|
"decimalZero5"
|
|
1171
1157
|
];
|
|
1172
|
-
|
|
1173
|
-
//#region src/model/numberingLevels.ts
|
|
1158
|
+
// src/model/numberingLevels.ts
|
|
1174
1159
|
const MIN_NUMBERING_LEVEL = 0;
|
|
1175
1160
|
const MAX_NUMBERING_LEVEL = 8;
|
|
1176
1161
|
/** Whether a numbering level reserves space for a visible marker. */
|
|
@@ -1287,8 +1272,7 @@ const levelShowsMarker = (definitions, numId, ilvl) => {
|
|
|
1287
1272
|
return numberingLevelHasMarkerSlot(referenced.level, level.isLgl) ? "x" : "";
|
|
1288
1273
|
}).trim().length > 0;
|
|
1289
1274
|
};
|
|
1290
|
-
|
|
1291
|
-
//#region src/model/content.ts
|
|
1275
|
+
// src/model/content.ts
|
|
1292
1276
|
const OOXML_SYMBOL_CHARACTER_PATTERN = /^[\dA-Fa-f]{4}$/u;
|
|
1293
1277
|
/** Whether a symbol character is exactly four hexadecimal digits. */
|
|
1294
1278
|
const isOoxmlSymbolCharacter = (value) => OOXML_SYMBOL_CHARACTER_PATTERN.test(value);
|
|
@@ -1381,8 +1365,7 @@ const PARAGRAPH_MARK_CHANGE_KINDS = Object.freeze([
|
|
|
1381
1365
|
"del"
|
|
1382
1366
|
]);
|
|
1383
1367
|
const REVIEW_CARRIERS = { TERMINAL_TABLE: "terminal-table" };
|
|
1384
|
-
|
|
1385
|
-
//#region src/model/document.ts
|
|
1368
|
+
// src/model/document.ts
|
|
1386
1369
|
/** OOXML default per §17.6.13 when `w:defaultTabStop` is absent. */
|
|
1387
1370
|
const DEFAULT_TAB_STOP_TWIPS = 720;
|
|
1388
1371
|
/** DOCX package conformance classes. */
|
|
@@ -1402,5 +1385,4 @@ const COMMENT_PART_RELATIONSHIPS = {
|
|
|
1402
1385
|
target: "commentsExtended.xml"
|
|
1403
1386
|
}
|
|
1404
1387
|
};
|
|
1405
|
-
//#endregion
|
|
1406
1388
|
export { outlineLevelStatedValue as $, BORDER_STYLES as A, isSchemeColorValue as B, PRESET_LINE_DASH_VALS as C, isBorderNone as D, isBorderNil as E, TEXT_DIRECTIONS as F, themeColorToken as G, knownThemeColor as H, TEXT_DIRECTION_FLOWS as I, relationshipIdOf as J, THEME_COLORS as K, TEXT_DIRECTION_FLOW_BY_TOKEN as L, PARAGRAPH_ALIGNMENTS as M, TABLE_ALIGNMENTS as N, isBorderStyle as O, TAB_STOP_ALIGNMENTS as P, outlineLevelFromStatedValue as Q, SCHEME_COLOR_VALUE_BY_THEME_COLOR as R, presetLineDashToken as S, borderStyleToken as T, readThemeColor as U, isThemeColor as V, themeColorSlot as W, headingLevelOf as X, BODY_TEXT_OUTLINE_LEVEL as Y, headingOutlineLevel as Z, numberingLevelHasMarkerSlot as _, isCanonicallyEmptyParagraphFormatting as _t, DRAWING_RAW_XML_MODES as a, paragraphNumberingFromSlots as at, isPresetLineDashVal as b, PARAGRAPH_MARK_CHANGE_KINDS as c, paragraphNumberingReferenceId as ct, isNoteReferenceId as d, sameEffectiveParagraphNumbering as dt, isNumberingLevel as et, isOoxmlSymbolCharacter as f, sameStatedParagraphNumbering as ft, numberingLevelEntries as g, finalizeParagraphFormatting as gt, numberingInstanceResolves as h, PARSE_WARNING_CODES as ht, BIDI_CONTROLS as i, mergeParagraphNumbering as it, NUMBER_FORMATS as j, statesNoBorder as k, PROPERTY_REVISION_KINDS as l, paragraphNumberingSlots as lt, levelShowsMarker as m, MAX_RETAINED_PARSE_WARNINGS_PER_CODE as mt, DEFAULT_TAB_STOP_TWIPS as n, NO_PARAGRAPH_NUMBERING as nt, INSERTION_SEAM_POLICIES as o, paragraphNumberingLevel as ot, normalizeRevisionId as p, IMPORTED_ID_COLLISION_KINDS as pt, mintRelationshipId as q, DOCX_CONFORMANCE_CLASSES as r, isNumberingReference as rt, MAX_REVISION_ID as s, paragraphNumberingReference as st, COMMENT_PART_RELATIONSHIPS as t, NO_NUMBERING_NUM_ID as tt, REVIEW_CARRIERS as u, resolveParagraphNumbering as ut, resolveNumberingLevel as v, setDocumentBodyComments as vt, borderStyleFrom as w, presetLineDashFrom as x, COUNTER_FORMATS as y, getSingularRunPropertyChange as yt, THEME_COLOR_BY_SCHEME_COLOR_VALUE as z };
|
package/dist/docx_kernel_bg.wasm
CHANGED
|
Binary file
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { TaggedErrorClass } from "better-result";
|
|
2
|
+
/** How an inflation ended. `overflow` means the data inflates past `size`. */
|
|
3
|
+
type InflateOutcome = {
|
|
4
|
+
readonly type: "ok";
|
|
5
|
+
readonly bytes: Uint8Array<ArrayBuffer>;
|
|
6
|
+
} | {
|
|
7
|
+
readonly type: "overflow";
|
|
8
|
+
} | {
|
|
9
|
+
readonly type: "corrupt";
|
|
10
|
+
readonly cause: unknown;
|
|
11
|
+
};
|
|
12
|
+
/** What an entry declares about its inflated content. */
|
|
13
|
+
type InflateExpectation = {
|
|
14
|
+
/** Inflated size in bytes. */
|
|
15
|
+
readonly size: number;
|
|
16
|
+
/** CRC-32 of the inflated content. */
|
|
17
|
+
readonly crc32: number;
|
|
18
|
+
};
|
|
19
|
+
/** DEFLATE and CRC-32 for one platform. */
|
|
20
|
+
type ZipCodec = {
|
|
21
|
+
/**
|
|
22
|
+
* Inflate raw DEFLATE `data` that must produce exactly `expected.size`
|
|
23
|
+
* bytes. Output stops at that size, so memory never exceeds it.
|
|
24
|
+
*/
|
|
25
|
+
readonly inflate: (data: Uint8Array, expected: InflateExpectation) => Promise<InflateOutcome>;
|
|
26
|
+
/** Raw DEFLATE at `level` (0-9). */
|
|
27
|
+
readonly deflate: (data: Uint8Array, level: number) => Promise<Uint8Array<ArrayBuffer>>;
|
|
28
|
+
/** CRC-32 of `data`, continuing from `previous` when given. */
|
|
29
|
+
readonly crc32: (data: Uint8Array, previous?: number) => number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Why an archive or entry was refused.
|
|
33
|
+
*
|
|
34
|
+
* - `malformed`: the records cannot be read, or disagree with each other.
|
|
35
|
+
* - `unsupported`: encryption, a compression method other than STORE or
|
|
36
|
+
* DEFLATE, or a multi-disk archive.
|
|
37
|
+
* - `invalid-entry-name`: an absolute, parent-relative, backslash, NUL or
|
|
38
|
+
* non-UTF-8 entry name.
|
|
39
|
+
* - `duplicate-entry`: two records name the same entry.
|
|
40
|
+
* - `too-many-entries`, `entry-too-large`, `total-too-large`,
|
|
41
|
+
* `compression-ratio`: a read limit.
|
|
42
|
+
* - `declared-size-exceeded`: entry data inflates past its declared size;
|
|
43
|
+
* inflation stops there.
|
|
44
|
+
* - `corrupt-entry`: entry data does not inflate, inflates short of its
|
|
45
|
+
* declared size, or fails its CRC-32.
|
|
46
|
+
*/
|
|
47
|
+
type ZipErrorReason = "malformed" | "unsupported" | "invalid-entry-name" | "duplicate-entry" | "too-many-entries" | "entry-too-large" | "total-too-large" | "compression-ratio" | "declared-size-exceeded" | "corrupt-entry";
|
|
48
|
+
declare const ZipErrorBase: TaggedErrorClass<"ZipError">;
|
|
49
|
+
/** A ZIP archive or entry the module refuses to read or write. */
|
|
50
|
+
declare class ZipError extends ZipErrorBase<{
|
|
51
|
+
message: string;
|
|
52
|
+
reason: ZipErrorReason;
|
|
53
|
+
/** The entry the error concerns, when it concerns one. */
|
|
54
|
+
path?: string;
|
|
55
|
+
cause?: unknown;
|
|
56
|
+
}> {}
|
|
57
|
+
export { ZipCodec as a, InflateOutcome as i, ZipErrorReason as n, InflateExpectation as r, ZipError as t };
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { Result, TaggedError } from "better-result";
|
|
2
|
+
import { encodeUtf8 } from "@stll/utf8-boundaries";
|
|
3
|
+
// src/zip/codec.ts
|
|
4
|
+
/**
|
|
5
|
+
* The platform's DEFLATE and CRC-32.
|
|
6
|
+
*
|
|
7
|
+
* Bun and Node.js expose native zlib through `process.getBuiltinModule`, which
|
|
8
|
+
* needs no import a browser bundler would have to resolve. Everywhere else
|
|
9
|
+
* (browsers, web workers, and runtimes too old for `getBuiltinModule`) uses
|
|
10
|
+
* `CompressionStream` / `DecompressionStream` and a table CRC-32. Runtimes
|
|
11
|
+
* whose streams lack `"deflate-raw"` get the same raw DEFLATE data framed as
|
|
12
|
+
* gzip: a fixed ten-byte header before it and the entry's CRC-32 and size,
|
|
13
|
+
* known from the central directory, after it.
|
|
14
|
+
*/
|
|
15
|
+
const isNativeZlib = (value) => typeof value === "object" && value !== null && "inflateRawSync" in value && typeof value.inflateRawSync === "function" && "deflateRawSync" in value && typeof value.deflateRawSync === "function";
|
|
16
|
+
const nativeZlibCrc32 = (zlib) => {
|
|
17
|
+
if (!("crc32" in zlib) || typeof zlib.crc32 !== "function") return null;
|
|
18
|
+
const { crc32 } = zlib;
|
|
19
|
+
return (data, previous) => crc32(data, previous);
|
|
20
|
+
};
|
|
21
|
+
const loadNativeZlib = () => {
|
|
22
|
+
const runtimeProcess = Reflect.get(globalThis, "process");
|
|
23
|
+
if (typeof runtimeProcess !== "object" || runtimeProcess === null || !("getBuiltinModule" in runtimeProcess) || typeof runtimeProcess.getBuiltinModule !== "function") return null;
|
|
24
|
+
const zlib = runtimeProcess.getBuiltinModule("node:zlib");
|
|
25
|
+
return isNativeZlib(zlib) ? zlib : null;
|
|
26
|
+
};
|
|
27
|
+
const loadBunCrc32 = () => {
|
|
28
|
+
const bun = Reflect.get(globalThis, "Bun");
|
|
29
|
+
if (typeof bun !== "object" || bun === null || !("hash" in bun)) return null;
|
|
30
|
+
const { hash } = bun;
|
|
31
|
+
if (typeof hash !== "function" || !("crc32" in hash) || typeof hash.crc32 !== "function") return null;
|
|
32
|
+
const { crc32 } = hash;
|
|
33
|
+
return (data, previous) => crc32(data, previous);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A copy of `bytes` in a new `ArrayBuffer` of exactly its length. Node.js
|
|
37
|
+
* `Buffer#slice` returns a view into a shared pool, so copies never go
|
|
38
|
+
* through `slice`.
|
|
39
|
+
*/
|
|
40
|
+
const copyBytes = (bytes) => new Uint8Array(bytes);
|
|
41
|
+
/** `bytes` backed by an `ArrayBuffer` of exactly its length. */
|
|
42
|
+
const exactBytes = (bytes) => {
|
|
43
|
+
const { buffer } = bytes;
|
|
44
|
+
return buffer instanceof ArrayBuffer && bytes.byteOffset === 0 && bytes.byteLength === buffer.byteLength ? new Uint8Array(buffer) : copyBytes(bytes);
|
|
45
|
+
};
|
|
46
|
+
let crcTable = null;
|
|
47
|
+
const tableCrc32 = (data, previous = 0) => {
|
|
48
|
+
if (crcTable === null) {
|
|
49
|
+
crcTable = /* @__PURE__ */ new Uint32Array(256);
|
|
50
|
+
for (let index = 0; index < 256; index++) {
|
|
51
|
+
let value = index;
|
|
52
|
+
for (let bit = 0; bit < 8; bit++) value = value & 1 ? 3988292384 ^ value >>> 1 : value >>> 1;
|
|
53
|
+
crcTable[index] = value >>> 0;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const table = crcTable;
|
|
57
|
+
let crc = ~previous >>> 0;
|
|
58
|
+
for (const byte of data) crc = table[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
59
|
+
return ~crc >>> 0;
|
|
60
|
+
};
|
|
61
|
+
const BUFFER_TOO_LARGE = "ERR_BUFFER_TOO_LARGE";
|
|
62
|
+
const isBufferTooLarge = (error) => typeof error === "object" && error !== null && "code" in error && error.code === BUFFER_TOO_LARGE;
|
|
63
|
+
const shortOf = (size) => ({
|
|
64
|
+
type: "corrupt",
|
|
65
|
+
cause: /* @__PURE__ */ new RangeError(`Entry inflates short of its declared ${size} bytes`)
|
|
66
|
+
});
|
|
67
|
+
const nativeCodec = (zlib) => ({
|
|
68
|
+
inflate: (data, { size }) => {
|
|
69
|
+
const inflated = Result.try(() => zlib.inflateRawSync(data, { maxOutputLength: Math.max(1, size) }));
|
|
70
|
+
if (Result.isError(inflated)) {
|
|
71
|
+
const cause = inflated.error.cause;
|
|
72
|
+
return Promise.resolve(isBufferTooLarge(cause) ? { type: "overflow" } : {
|
|
73
|
+
type: "corrupt",
|
|
74
|
+
cause
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const bytes = inflated.value;
|
|
78
|
+
if (bytes.byteLength > size) return Promise.resolve({ type: "overflow" });
|
|
79
|
+
if (bytes.byteLength < size) return Promise.resolve(shortOf(size));
|
|
80
|
+
return Promise.resolve({
|
|
81
|
+
type: "ok",
|
|
82
|
+
bytes: exactBytes(bytes)
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
deflate: (data, level) => Promise.resolve(exactBytes(zlib.deflateRawSync(data, { level }))),
|
|
86
|
+
crc32: loadBunCrc32() ?? nativeZlibCrc32(zlib) ?? tableCrc32
|
|
87
|
+
});
|
|
88
|
+
const concatChunks = (chunks, length) => {
|
|
89
|
+
const merged = new Uint8Array(length);
|
|
90
|
+
let offset = 0;
|
|
91
|
+
for (const chunk of chunks) {
|
|
92
|
+
merged.set(chunk, offset);
|
|
93
|
+
offset += chunk.byteLength;
|
|
94
|
+
}
|
|
95
|
+
return merged;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Feed `data` through a transform without awaiting the write, so the reader
|
|
99
|
+
* drains the output while the input is still being written. The write
|
|
100
|
+
* settles with the readable side; its rejection carries the same failure the
|
|
101
|
+
* read loop reports, so it is observed here rather than left unhandled.
|
|
102
|
+
*/
|
|
103
|
+
const pipeBytes = (transform, chunks) => {
|
|
104
|
+
const writer = transform.writable.getWriter();
|
|
105
|
+
const write = async () => {
|
|
106
|
+
for (const chunk of chunks) await writer.write(exactBytes(chunk));
|
|
107
|
+
await writer.close();
|
|
108
|
+
};
|
|
109
|
+
write().catch(() => {});
|
|
110
|
+
return transform.readable.getReader();
|
|
111
|
+
};
|
|
112
|
+
/** Whether this runtime's streams accept `"deflate-raw"`. */
|
|
113
|
+
const detectFraming = () => Result.isOk(Result.try(() => new DecompressionStream("deflate-raw"))) ? "deflate-raw" : "gzip";
|
|
114
|
+
const GZIP_HEADER = Uint8Array.of(31, 139, 8, 0, 0, 0, 0, 0, 0, 255);
|
|
115
|
+
const GZIP_HEADER_SIZE = GZIP_HEADER.byteLength;
|
|
116
|
+
const GZIP_TRAILER_SIZE = 8;
|
|
117
|
+
const gzipTrailer = ({ size, crc32 }) => {
|
|
118
|
+
const trailer = new Uint8Array(GZIP_TRAILER_SIZE);
|
|
119
|
+
const view = new DataView(trailer.buffer);
|
|
120
|
+
view.setUint32(0, crc32, true);
|
|
121
|
+
view.setUint32(4, size % 4294967296, true);
|
|
122
|
+
return trailer;
|
|
123
|
+
};
|
|
124
|
+
const streamInflate = async (framing, data, expected) => {
|
|
125
|
+
const { size } = expected;
|
|
126
|
+
const reader = pipeBytes(new DecompressionStream(framing), framing === "gzip" ? [
|
|
127
|
+
GZIP_HEADER,
|
|
128
|
+
data,
|
|
129
|
+
gzipTrailer(expected)
|
|
130
|
+
] : [data]);
|
|
131
|
+
const bytes = new Uint8Array(size);
|
|
132
|
+
let offset = 0;
|
|
133
|
+
const read = await Result.tryPromise(async () => {
|
|
134
|
+
for (;;) {
|
|
135
|
+
const { done, value } = await reader.read();
|
|
136
|
+
if (done) return "ended";
|
|
137
|
+
if (offset + value.byteLength > size) {
|
|
138
|
+
await reader.cancel();
|
|
139
|
+
return "overflow";
|
|
140
|
+
}
|
|
141
|
+
bytes.set(value, offset);
|
|
142
|
+
offset += value.byteLength;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
if (Result.isError(read)) return {
|
|
146
|
+
type: "corrupt",
|
|
147
|
+
cause: read.error.cause
|
|
148
|
+
};
|
|
149
|
+
if (read.value === "overflow") return { type: "overflow" };
|
|
150
|
+
if (offset < size) return shortOf(size);
|
|
151
|
+
return {
|
|
152
|
+
type: "ok",
|
|
153
|
+
bytes
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
const streamDeflate = async (framing, data) => {
|
|
157
|
+
const reader = pipeBytes(new CompressionStream(framing), [data]);
|
|
158
|
+
const chunks = [];
|
|
159
|
+
let length = 0;
|
|
160
|
+
for (;;) {
|
|
161
|
+
const { done, value } = await reader.read();
|
|
162
|
+
if (done) break;
|
|
163
|
+
chunks.push(value);
|
|
164
|
+
length += value.byteLength;
|
|
165
|
+
}
|
|
166
|
+
const output = concatChunks(chunks, length);
|
|
167
|
+
return framing === "gzip" ? output.slice(GZIP_HEADER_SIZE, output.byteLength - GZIP_TRAILER_SIZE) : output;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* A codec over the web compression streams with the given framing.
|
|
171
|
+
* `CompressionStream` has no level parameter; it writes the platform's
|
|
172
|
+
* default level.
|
|
173
|
+
*/
|
|
174
|
+
const streamZipCodec = (framing) => ({
|
|
175
|
+
inflate: async (data, expected) => await streamInflate(framing, data, expected),
|
|
176
|
+
deflate: async (data) => await streamDeflate(framing, data),
|
|
177
|
+
crc32: tableCrc32
|
|
178
|
+
});
|
|
179
|
+
let webCodec = null;
|
|
180
|
+
/** The web compression streams, in the framing this runtime supports. */
|
|
181
|
+
const webZipCodec = () => {
|
|
182
|
+
webCodec ??= streamZipCodec(detectFraming());
|
|
183
|
+
return webCodec;
|
|
184
|
+
};
|
|
185
|
+
let platformCodec = null;
|
|
186
|
+
/** Native zlib where the runtime has it, the web streams otherwise. */
|
|
187
|
+
const platformZipCodec = () => {
|
|
188
|
+
if (platformCodec === null) {
|
|
189
|
+
const zlib = loadNativeZlib();
|
|
190
|
+
platformCodec = zlib === null ? webZipCodec() : nativeCodec(zlib);
|
|
191
|
+
}
|
|
192
|
+
return platformCodec;
|
|
193
|
+
};
|
|
194
|
+
// src/zip/errors.ts
|
|
195
|
+
const ZipErrorBase = TaggedError("ZipError");
|
|
196
|
+
/** A ZIP archive or entry the module refuses to read or write. */
|
|
197
|
+
var ZipError = class extends ZipErrorBase {};
|
|
198
|
+
// src/zip/format.ts
|
|
199
|
+
/** ZIP record layouts (APPNOTE 6.3.10) shared by the readers and writers. */
|
|
200
|
+
const LOCAL_FILE_HEADER_SIGNATURE = 67324752;
|
|
201
|
+
const CENTRAL_DIRECTORY_SIGNATURE = 33639248;
|
|
202
|
+
const END_OF_CENTRAL_DIRECTORY_SIGNATURE = 101010256;
|
|
203
|
+
const ZIP64_END_OF_CENTRAL_DIRECTORY_SIGNATURE = 101075792;
|
|
204
|
+
const ZIP64_END_LOCATOR_SIGNATURE = 117853008;
|
|
205
|
+
const DATA_DESCRIPTOR_SIGNATURE = 134695760;
|
|
206
|
+
const FLAG_UTF8_NAME = 2048;
|
|
207
|
+
/** A 32-bit field at this value defers to the Zip64 extra field. */
|
|
208
|
+
const UINT32_SENTINEL = 4294967295;
|
|
209
|
+
/** A 16-bit count at this value defers to the Zip64 end record. */
|
|
210
|
+
const UINT16_SENTINEL = 65535;
|
|
211
|
+
const VERSION_DEFAULT = 20;
|
|
212
|
+
const VERSION_ZIP64 = 45;
|
|
213
|
+
const DOS_DIRECTORY_ATTRIBUTE = 16;
|
|
214
|
+
const DOS_EPOCH_YEAR = 1980;
|
|
215
|
+
const DOS_LAST_YEAR = 2107;
|
|
216
|
+
/** `date` as DOS fields in UTC, clamped to the range DOS dates can hold. */
|
|
217
|
+
const toDosDateTime = (date) => {
|
|
218
|
+
const year = date.getUTCFullYear();
|
|
219
|
+
if (Number.isNaN(year) || year < DOS_EPOCH_YEAR) return {
|
|
220
|
+
date: 33,
|
|
221
|
+
time: 0
|
|
222
|
+
};
|
|
223
|
+
if (year > DOS_LAST_YEAR) return {
|
|
224
|
+
date: 65439,
|
|
225
|
+
time: 49021
|
|
226
|
+
};
|
|
227
|
+
return {
|
|
228
|
+
date: year - DOS_EPOCH_YEAR << 9 | date.getUTCMonth() + 1 << 5 | date.getUTCDate(),
|
|
229
|
+
time: date.getUTCHours() << 11 | date.getUTCMinutes() << 5 | date.getUTCSeconds() >> 1
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
/** DOS fields as a UTC `Date`; out-of-range fields roll over like `Date.UTC`. */
|
|
233
|
+
const fromDosDateTime = ({ date, time }) => new Date(Date.UTC(DOS_EPOCH_YEAR + (date >> 9), (date >> 5 & 15) - 1, date & 31, time >> 11, time >> 5 & 63, (time & 31) * 2));
|
|
234
|
+
/** The reproducible timestamp written for new entries: 2000-01-01T00:00Z. */
|
|
235
|
+
const FIXED_DOS_DATE_TIME = toDosDateTime(new Date(Date.UTC(2e3, 0, 1)));
|
|
236
|
+
/** Entry name bytes and the general-purpose flag they need. */
|
|
237
|
+
const encodeEntryName = (path) => {
|
|
238
|
+
const bytes = encodeUtf8(path);
|
|
239
|
+
return {
|
|
240
|
+
bytes,
|
|
241
|
+
flags: bytes.every((byte) => byte < 128) ? 0 : FLAG_UTF8_NAME
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
/** Whether `path` is a relative, forward-slash name with no `..` segment. */
|
|
245
|
+
const isSafeZipPath = (path) => {
|
|
246
|
+
if (path.length === 0 || path.startsWith("/")) return false;
|
|
247
|
+
if (path.includes("\\") || path.includes("\0")) return false;
|
|
248
|
+
if (/^[A-Za-z]:/u.test(path)) return false;
|
|
249
|
+
return !path.split("/").some((segment) => segment === "..");
|
|
250
|
+
};
|
|
251
|
+
const writeCommonFields = (view, offset, record) => {
|
|
252
|
+
view.setUint16(offset, record.flags, true);
|
|
253
|
+
view.setUint16(offset + 2, record.method, true);
|
|
254
|
+
view.setUint16(offset + 4, record.dateTime.time, true);
|
|
255
|
+
view.setUint16(offset + 6, record.dateTime.date, true);
|
|
256
|
+
view.setUint32(offset + 8, record.crc32, true);
|
|
257
|
+
};
|
|
258
|
+
const needsZip64 = (value) => value >= UINT32_SENTINEL;
|
|
259
|
+
/**
|
|
260
|
+
* A local file header. Sizes go in the header unless the entry streams them
|
|
261
|
+
* in a data descriptor; a header that carries sizes of 4 GiB or more puts
|
|
262
|
+
* both in a Zip64 extra field, as APPNOTE 4.5.3 requires.
|
|
263
|
+
*/
|
|
264
|
+
const encodeLocalHeader = (record) => {
|
|
265
|
+
const streamed = (record.flags & 8) !== 0;
|
|
266
|
+
const zip64 = !streamed && (needsZip64(record.compressedSize) || needsZip64(record.uncompressedSize));
|
|
267
|
+
const extraLength = zip64 ? 20 : 0;
|
|
268
|
+
const bytes = new Uint8Array(30 + record.name.byteLength + extraLength);
|
|
269
|
+
const view = new DataView(bytes.buffer);
|
|
270
|
+
view.setUint32(0, LOCAL_FILE_HEADER_SIGNATURE, true);
|
|
271
|
+
view.setUint16(4, zip64 ? VERSION_ZIP64 : VERSION_DEFAULT, true);
|
|
272
|
+
writeCommonFields(view, 6, record);
|
|
273
|
+
if (streamed) view.setUint32(14, 0, true);
|
|
274
|
+
else {
|
|
275
|
+
view.setUint32(18, zip64 ? UINT32_SENTINEL : record.compressedSize, true);
|
|
276
|
+
view.setUint32(22, zip64 ? UINT32_SENTINEL : record.uncompressedSize, true);
|
|
277
|
+
}
|
|
278
|
+
view.setUint16(26, record.name.byteLength, true);
|
|
279
|
+
view.setUint16(28, extraLength, true);
|
|
280
|
+
bytes.set(record.name, 30);
|
|
281
|
+
if (zip64) {
|
|
282
|
+
const extra = 30 + record.name.byteLength;
|
|
283
|
+
view.setUint16(extra, 1, true);
|
|
284
|
+
view.setUint16(extra + 2, 16, true);
|
|
285
|
+
view.setBigUint64(extra + 4, BigInt(record.uncompressedSize), true);
|
|
286
|
+
view.setBigUint64(extra + 12, BigInt(record.compressedSize), true);
|
|
287
|
+
}
|
|
288
|
+
return bytes;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* The data descriptor that follows a streamed entry. Sizes of 4 GiB or more
|
|
292
|
+
* are written as 8-byte fields.
|
|
293
|
+
*/
|
|
294
|
+
const encodeDataDescriptor = (record) => {
|
|
295
|
+
const zip64 = needsZip64(record.compressedSize) || needsZip64(record.uncompressedSize);
|
|
296
|
+
const bytes = new Uint8Array(zip64 ? 24 : 16);
|
|
297
|
+
const view = new DataView(bytes.buffer);
|
|
298
|
+
view.setUint32(0, DATA_DESCRIPTOR_SIGNATURE, true);
|
|
299
|
+
view.setUint32(4, record.crc32, true);
|
|
300
|
+
if (zip64) {
|
|
301
|
+
view.setBigUint64(8, BigInt(record.compressedSize), true);
|
|
302
|
+
view.setBigUint64(16, BigInt(record.uncompressedSize), true);
|
|
303
|
+
} else {
|
|
304
|
+
view.setUint32(8, record.compressedSize, true);
|
|
305
|
+
view.setUint32(12, record.uncompressedSize, true);
|
|
306
|
+
}
|
|
307
|
+
return bytes;
|
|
308
|
+
};
|
|
309
|
+
/**
|
|
310
|
+
* A central directory header. Each field of 4 GiB or more is written as the
|
|
311
|
+
* sentinel and carried, in APPNOTE order, by a Zip64 extra field.
|
|
312
|
+
*/
|
|
313
|
+
const encodeCentralHeader = (record, localOffset) => {
|
|
314
|
+
const overflow = [
|
|
315
|
+
record.uncompressedSize,
|
|
316
|
+
record.compressedSize,
|
|
317
|
+
localOffset
|
|
318
|
+
].filter((value) => needsZip64(value));
|
|
319
|
+
const extraLength = overflow.length === 0 ? 0 : 4 + overflow.length * 8;
|
|
320
|
+
const bytes = new Uint8Array(46 + record.name.byteLength + extraLength);
|
|
321
|
+
const view = new DataView(bytes.buffer);
|
|
322
|
+
const version = extraLength === 0 ? VERSION_DEFAULT : VERSION_ZIP64;
|
|
323
|
+
view.setUint32(0, CENTRAL_DIRECTORY_SIGNATURE, true);
|
|
324
|
+
view.setUint16(4, version, true);
|
|
325
|
+
view.setUint16(6, version, true);
|
|
326
|
+
writeCommonFields(view, 8, record);
|
|
327
|
+
view.setUint32(20, needsZip64(record.compressedSize) ? UINT32_SENTINEL : record.compressedSize, true);
|
|
328
|
+
view.setUint32(24, needsZip64(record.uncompressedSize) ? UINT32_SENTINEL : record.uncompressedSize, true);
|
|
329
|
+
view.setUint16(28, record.name.byteLength, true);
|
|
330
|
+
view.setUint16(30, extraLength, true);
|
|
331
|
+
view.setUint32(38, record.directory ? DOS_DIRECTORY_ATTRIBUTE : 0, true);
|
|
332
|
+
view.setUint32(42, needsZip64(localOffset) ? UINT32_SENTINEL : localOffset, true);
|
|
333
|
+
bytes.set(record.name, 46);
|
|
334
|
+
if (extraLength !== 0) {
|
|
335
|
+
let field = 46 + record.name.byteLength;
|
|
336
|
+
view.setUint16(field, 1, true);
|
|
337
|
+
view.setUint16(field + 2, overflow.length * 8, true);
|
|
338
|
+
field += 4;
|
|
339
|
+
for (const value of overflow) {
|
|
340
|
+
view.setBigUint64(field, BigInt(value), true);
|
|
341
|
+
field += 8;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return bytes;
|
|
345
|
+
};
|
|
346
|
+
/**
|
|
347
|
+
* The end-of-central-directory record, preceded by the Zip64 end record and
|
|
348
|
+
* its locator when a count, size or offset does not fit the classic fields.
|
|
349
|
+
*/
|
|
350
|
+
const encodeEndOfCentralDirectory = ({ entryCount, offset, size }) => {
|
|
351
|
+
const zip64 = entryCount >= 65535 || needsZip64(offset) || needsZip64(size);
|
|
352
|
+
const zip64Length = zip64 ? 76 : 0;
|
|
353
|
+
const bytes = new Uint8Array(zip64Length + 22);
|
|
354
|
+
const view = new DataView(bytes.buffer);
|
|
355
|
+
if (zip64) {
|
|
356
|
+
const recordOffset = offset + size;
|
|
357
|
+
view.setUint32(0, ZIP64_END_OF_CENTRAL_DIRECTORY_SIGNATURE, true);
|
|
358
|
+
view.setBigUint64(4, BigInt(44), true);
|
|
359
|
+
view.setUint16(12, VERSION_ZIP64, true);
|
|
360
|
+
view.setUint16(14, VERSION_ZIP64, true);
|
|
361
|
+
view.setBigUint64(24, BigInt(entryCount), true);
|
|
362
|
+
view.setBigUint64(32, BigInt(entryCount), true);
|
|
363
|
+
view.setBigUint64(40, BigInt(size), true);
|
|
364
|
+
view.setBigUint64(48, BigInt(offset), true);
|
|
365
|
+
view.setUint32(56, ZIP64_END_LOCATOR_SIGNATURE, true);
|
|
366
|
+
view.setBigUint64(64, BigInt(recordOffset), true);
|
|
367
|
+
view.setUint32(72, 1, true);
|
|
368
|
+
}
|
|
369
|
+
const end = zip64Length;
|
|
370
|
+
view.setUint32(end, END_OF_CENTRAL_DIRECTORY_SIGNATURE, true);
|
|
371
|
+
const count = Math.min(entryCount, UINT16_SENTINEL);
|
|
372
|
+
view.setUint16(end + 8, count, true);
|
|
373
|
+
view.setUint16(end + 10, count, true);
|
|
374
|
+
view.setUint32(end + 12, needsZip64(size) ? UINT32_SENTINEL : size, true);
|
|
375
|
+
view.setUint32(end + 16, needsZip64(offset) ? UINT32_SENTINEL : offset, true);
|
|
376
|
+
return bytes;
|
|
377
|
+
};
|
|
378
|
+
export { ZipError as _, UINT16_SENTINEL as a, ZIP64_END_OF_CENTRAL_DIRECTORY_SIGNATURE as c, encodeEndOfCentralDirectory as d, encodeEntryName as f, toDosDateTime as g, isSafeZipPath as h, LOCAL_FILE_HEADER_SIGNATURE as i, encodeCentralHeader as l, fromDosDateTime as m, END_OF_CENTRAL_DIRECTORY_SIGNATURE as n, UINT32_SENTINEL as o, encodeLocalHeader as p, FIXED_DOS_DATE_TIME as r, ZIP64_END_LOCATOR_SIGNATURE as s, CENTRAL_DIRECTORY_SIGNATURE as t, encodeDataDescriptor as u, copyBytes as v, platformZipCodec as y };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/serialize/xmlEscape.ts
|
|
2
2
|
/**
|
|
3
3
|
* The one owner of XML character escaping for every part folio writes.
|
|
4
4
|
*
|
|
@@ -88,8 +88,7 @@ const hasIllegalXmlCharacters = (value) => ILLEGAL_XML_CHARACTER_PROBE.test(valu
|
|
|
88
88
|
* further down, but by then nobody can say which input lost a character.
|
|
89
89
|
*/
|
|
90
90
|
const sanitizeXmlCharacters = (value) => hasIllegalXmlCharacters(value) ? value.replace(LONE_SURROGATE_PATTERN, REPLACEMENT_CHARACTER).replace(ILLEGAL_XML_CHARACTER_PATTERN, "") : value;
|
|
91
|
-
|
|
92
|
-
//#region src/markdown/href.ts
|
|
91
|
+
// src/markdown/href.ts
|
|
93
92
|
const ALLOWED_URL_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
94
93
|
"http:",
|
|
95
94
|
"https:",
|
|
@@ -136,5 +135,4 @@ const sanitizeMarkdownHref = (rawHref) => {
|
|
|
136
135
|
}
|
|
137
136
|
return sanitizeExternalUrl(trimmed);
|
|
138
137
|
};
|
|
139
|
-
//#endregion
|
|
140
138
|
export { hasIllegalXmlCharacters as a, escapeXmlText as i, sanitizeMarkdownHref as n, sanitizeXmlCharacters as o, escapeXmlAttribute as r, sanitizeExternalUrl as t };
|