@stll/docx-core 0.39.2 → 0.41.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.
@@ -0,0 +1,117 @@
1
+ import { _ as ZipError, d as encodeEndOfCentralDirectory, f as encodeEntryName, g as toDosDateTime, h as isSafeZipPath, l as encodeCentralHeader, p as encodeLocalHeader, r as FIXED_DOS_DATE_TIME, u as encodeDataDescriptor, y as platformZipCodec } from "../format-DXPXB02u.js";
2
+ import { encodeUtf8 } from "@stll/utf8-boundaries";
3
+ // src/zip/stream.ts
4
+ /**
5
+ * A streaming ZIP writer for archives too large to hold in memory.
6
+ *
7
+ * Entries are stored, not compressed, and their bytes flow straight from the
8
+ * input to the output: each entry's CRC-32 and size are counted as it passes
9
+ * and written in a data descriptor after it, and Zip64 records are used when
10
+ * a size, an offset or the entry count outgrows the classic fields. Memory
11
+ * holds one chunk at a time plus one small central-directory record per
12
+ * entry.
13
+ */
14
+ async function* entryChunks(data) {
15
+ if (data === void 0) return;
16
+ if (typeof data === "string") {
17
+ yield encodeUtf8(data);
18
+ return;
19
+ }
20
+ if (data instanceof Uint8Array) {
21
+ yield data;
22
+ return;
23
+ }
24
+ yield* data;
25
+ }
26
+ const invalidName = (path) => new ZipError({
27
+ message: "Archive entry name must be a relative path",
28
+ reason: "invalid-entry-name",
29
+ path
30
+ });
31
+ /**
32
+ * The archive bytes for `entries`, produced as they are consumed. Entries
33
+ * are pulled one at a time, so a source that opens a download per entry
34
+ * opens it only when the archive reaches it.
35
+ */
36
+ async function* zipStream(entries, { codec = platformZipCodec() } = {}) {
37
+ const centralHeaders = [];
38
+ const names = /* @__PURE__ */ new Set();
39
+ let offset = 0;
40
+ for await (const { path, data, modified } of entries) {
41
+ if (!isSafeZipPath(path)) throw invalidName(path);
42
+ if (names.has(path)) throw new ZipError({
43
+ message: "Archive names an entry twice",
44
+ reason: "duplicate-entry",
45
+ path
46
+ });
47
+ names.add(path);
48
+ const directory = path.endsWith("/");
49
+ if (directory && data !== void 0) throw invalidName(path);
50
+ const name = encodeEntryName(path);
51
+ const header = {
52
+ name: name.bytes,
53
+ flags: name.flags | 8,
54
+ method: 0,
55
+ dateTime: modified === void 0 ? FIXED_DOS_DATE_TIME : toDosDateTime(modified),
56
+ crc32: 0,
57
+ compressedSize: 0,
58
+ uncompressedSize: 0,
59
+ directory
60
+ };
61
+ const localOffset = offset;
62
+ const local = encodeLocalHeader(header);
63
+ yield local;
64
+ offset += local.byteLength;
65
+ let crc32 = 0;
66
+ let size = 0;
67
+ for await (const chunk of entryChunks(data)) {
68
+ if (chunk.byteLength === 0) continue;
69
+ crc32 = codec.crc32(chunk, crc32);
70
+ size += chunk.byteLength;
71
+ yield chunk;
72
+ }
73
+ offset += size;
74
+ const record = {
75
+ ...header,
76
+ crc32,
77
+ compressedSize: size,
78
+ uncompressedSize: size
79
+ };
80
+ const descriptor = encodeDataDescriptor(record);
81
+ yield descriptor;
82
+ offset += descriptor.byteLength;
83
+ centralHeaders.push(encodeCentralHeader(record, localOffset));
84
+ }
85
+ let directorySize = 0;
86
+ for (const header of centralHeaders) {
87
+ yield header;
88
+ directorySize += header.byteLength;
89
+ }
90
+ yield encodeEndOfCentralDirectory({
91
+ entryCount: centralHeaders.length,
92
+ offset,
93
+ size: directorySize
94
+ });
95
+ }
96
+ /**
97
+ * {@link zipStream} as a `ReadableStream`. The stream pulls the next chunk
98
+ * only when its consumer asks, so a slow consumer slows the sources rather
99
+ * than buffering them.
100
+ */
101
+ const createZipStream = (entries, options = {}) => {
102
+ const chunks = zipStream(entries, options);
103
+ return new ReadableStream({
104
+ pull: async (controller) => {
105
+ const { done, value } = await chunks.next();
106
+ if (done) {
107
+ controller.close();
108
+ return;
109
+ }
110
+ controller.enqueue(value);
111
+ },
112
+ cancel: async () => {
113
+ await chunks.return(void 0);
114
+ }
115
+ });
116
+ };
117
+ export { ZipError, createZipStream, zipStream };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stll/docx-core",
3
- "version": "0.39.2",
3
+ "version": "0.41.0",
4
4
  "description": "Typed OOXML/DOCX model, validation, serialization, legal-source compilation, and browser-native package projection.",
5
5
  "keywords": [
6
6
  "document-model",
@@ -57,10 +57,30 @@
57
57
  "import": "./dist/schema/sequenceChildren.js",
58
58
  "default": "./dist/schema/sequenceChildren.js"
59
59
  },
60
+ "./xml": {
61
+ "types": "./dist/xml/streamingXmlParser.d.ts",
62
+ "import": "./dist/xml/streamingXmlParser.js",
63
+ "default": "./dist/xml/streamingXmlParser.js"
64
+ },
65
+ "./xml/limits": {
66
+ "types": "./dist/xml/xmlResourceLimits.d.ts",
67
+ "import": "./dist/xml/xmlResourceLimits.js",
68
+ "default": "./dist/xml/xmlResourceLimits.js"
69
+ },
70
+ "./xml/namespaces": {
71
+ "types": "./dist/xml/xmlNamespaceContext.d.ts",
72
+ "import": "./dist/xml/xmlNamespaceContext.js",
73
+ "default": "./dist/xml/xmlNamespaceContext.js"
74
+ },
60
75
  "./zip": {
61
- "types": "./dist/zip/writeZipPart.d.ts",
62
- "import": "./dist/zip/writeZipPart.js",
63
- "default": "./dist/zip/writeZipPart.js"
76
+ "types": "./dist/zip/archive.d.ts",
77
+ "import": "./dist/zip/archive.js",
78
+ "default": "./dist/zip/archive.js"
79
+ },
80
+ "./zip/stream": {
81
+ "types": "./dist/zip/stream.d.ts",
82
+ "import": "./dist/zip/stream.js",
83
+ "default": "./dist/zip/stream.js"
64
84
  }
65
85
  },
66
86
  "publishConfig": {
@@ -77,12 +97,9 @@
77
97
  "prepack": "bun run build"
78
98
  },
79
99
  "dependencies": {
80
- "@nodable/entities": "^3.0.0",
81
100
  "@stll/docx-utils": "^0.2.0",
82
- "@stll/utf8-boundaries": "0.1.2",
101
+ "@stll/utf8-boundaries": "0.2.0",
83
102
  "better-result": "3.0.1",
84
- "fast-xml-parser": "^5.11.2",
85
- "jszip": "3.10.2",
86
103
  "marked": "^18.1.1"
87
104
  },
88
105
  "devDependencies": {
@@ -1,10 +0,0 @@
1
- import JSZip from "jszip";
2
- type WriteZipPartOptions = {
3
- zip: JSZip;
4
- path: string;
5
- data: Parameters<JSZip["loadAsync"]>[0];
6
- options?: Omit<JSZip.JSZipFileOptions, "createFolders">;
7
- };
8
- /** Write exactly one entry, without synthesizing parent directory entries. */
9
- export declare const writeZipPart: ({ zip, path, data, options }: WriteZipPartOptions) => void;
10
- export {};
@@ -1,16 +0,0 @@
1
- import { writeZipEntry } from "@stll/utf8-boundaries";
2
- // src/zip/writeZipPart.ts
3
- const FIXED_ZIP_DATE_MS = Date.UTC(2e3, 0, 1);
4
- const UNIX_DIRECTORY_FLAG = 16384;
5
- const DOS_DIRECTORY_FLAG = 16;
6
- /** Write exactly one entry, without synthesizing parent directory entries. */
7
- const writeZipPart = ({ zip, path, data, options = {} }) => {
8
- const unixPermissions = typeof options.unixPermissions === "string" ? Number.parseInt(options.unixPermissions, 8) : options.unixPermissions ?? 0;
9
- if (options.dir === true || (unixPermissions & UNIX_DIRECTORY_FLAG) !== 0 || ((options.dosPermissions ?? 0) & DOS_DIRECTORY_FLAG) !== 0) data = /* @__PURE__ */ new Uint8Array();
10
- writeZipEntry(zip, path, data, {
11
- ...options,
12
- date: options.date ?? new Date(FIXED_ZIP_DATE_MS),
13
- createFolders: false
14
- });
15
- };
16
- export { writeZipPart };