@cj-tech-master/excelts 1.4.3 → 1.4.5

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.
Files changed (54) hide show
  1. package/README.md +3 -3
  2. package/README_zh.md +3 -3
  3. package/dist/browser/excelts.iife.js +12841 -7484
  4. package/dist/browser/excelts.iife.js.map +1 -1
  5. package/dist/browser/excelts.iife.min.js +86 -23
  6. package/dist/cjs/doc/column.js +1 -1
  7. package/dist/cjs/doc/row.js +9 -4
  8. package/dist/cjs/doc/worksheet.js +9 -4
  9. package/dist/cjs/stream/xlsx/workbook-writer.js +3 -2
  10. package/dist/cjs/utils/unzip/extract.js +166 -0
  11. package/dist/cjs/utils/unzip/index.js +7 -1
  12. package/dist/cjs/utils/xml-stream.js +25 -3
  13. package/dist/cjs/utils/zip/compress.js +261 -0
  14. package/dist/cjs/utils/zip/crc32.js +154 -0
  15. package/dist/cjs/utils/zip/index.js +70 -0
  16. package/dist/cjs/utils/zip/zip-builder.js +378 -0
  17. package/dist/cjs/utils/zip-stream.js +30 -34
  18. package/dist/cjs/xlsx/xform/book/defined-name-xform.js +36 -2
  19. package/dist/cjs/xlsx/xform/list-xform.js +6 -0
  20. package/dist/cjs/xlsx/xform/sheet/cell-xform.js +6 -1
  21. package/dist/cjs/xlsx/xform/sheet/row-xform.js +24 -2
  22. package/dist/cjs/xlsx/xform/table/filter-column-xform.js +4 -0
  23. package/dist/esm/doc/column.js +1 -1
  24. package/dist/esm/doc/row.js +9 -4
  25. package/dist/esm/doc/worksheet.js +9 -4
  26. package/dist/esm/stream/xlsx/workbook-writer.js +3 -2
  27. package/dist/esm/utils/unzip/extract.js +160 -0
  28. package/dist/esm/utils/unzip/index.js +2 -0
  29. package/dist/esm/utils/xml-stream.js +25 -3
  30. package/dist/esm/utils/zip/compress.js +220 -0
  31. package/dist/esm/utils/zip/crc32.js +116 -0
  32. package/dist/esm/utils/zip/index.js +55 -0
  33. package/dist/esm/utils/zip/zip-builder.js +372 -0
  34. package/dist/esm/utils/zip-stream.js +30 -34
  35. package/dist/esm/xlsx/xform/book/defined-name-xform.js +36 -2
  36. package/dist/esm/xlsx/xform/list-xform.js +6 -0
  37. package/dist/esm/xlsx/xform/sheet/cell-xform.js +6 -1
  38. package/dist/esm/xlsx/xform/sheet/row-xform.js +24 -2
  39. package/dist/esm/xlsx/xform/table/filter-column-xform.js +4 -0
  40. package/dist/types/doc/cell.d.ts +10 -6
  41. package/dist/types/doc/column.d.ts +8 -4
  42. package/dist/types/doc/row.d.ts +9 -8
  43. package/dist/types/doc/worksheet.d.ts +2 -2
  44. package/dist/types/utils/unzip/extract.d.ts +92 -0
  45. package/dist/types/utils/unzip/index.d.ts +1 -0
  46. package/dist/types/utils/xml-stream.d.ts +2 -0
  47. package/dist/types/utils/zip/compress.d.ts +83 -0
  48. package/dist/types/utils/zip/crc32.d.ts +55 -0
  49. package/dist/types/utils/zip/index.d.ts +52 -0
  50. package/dist/types/utils/zip/zip-builder.d.ts +110 -0
  51. package/dist/types/utils/zip-stream.d.ts +6 -12
  52. package/dist/types/xlsx/xform/list-xform.d.ts +1 -0
  53. package/dist/types/xlsx/xform/sheet/row-xform.d.ts +2 -0
  54. package/package.json +8 -8
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Native compression utilities using platform APIs
3
+ *
4
+ * - Node.js: Uses native zlib module (C++ implementation, fastest)
5
+ * - Browser: Uses CompressionStream API (Chrome 80+, Firefox 113+, Safari 16.4+)
6
+ *
7
+ * Both use "deflate-raw" format which is required for ZIP files
8
+ * (raw DEFLATE without zlib header/trailer)
9
+ */
10
+ // Detect environment
11
+ const isNode = typeof process !== "undefined" && process.versions?.node;
12
+ // Lazy-loaded zlib module for Node.js
13
+ let _zlib = null;
14
+ let _zlibLoading = null;
15
+ // Auto-initialize zlib in Node.js environment
16
+ if (isNode) {
17
+ _zlibLoading = import("zlib")
18
+ .then(module => {
19
+ _zlib = module.default ?? module;
20
+ return _zlib;
21
+ })
22
+ .catch(() => {
23
+ _zlib = null;
24
+ return null;
25
+ });
26
+ }
27
+ /**
28
+ * Get zlib module (Node.js only)
29
+ * Returns null if not yet loaded or not in Node.js
30
+ */
31
+ function getZlib() {
32
+ return _zlib;
33
+ }
34
+ /**
35
+ * Ensure zlib is loaded (Node.js only)
36
+ * Call this before using sync methods if you need to guarantee availability
37
+ */
38
+ export async function ensureZlib() {
39
+ if (_zlibLoading) {
40
+ return _zlibLoading;
41
+ }
42
+ return _zlib;
43
+ }
44
+ /**
45
+ * Check if native zlib is available (Node.js)
46
+ */
47
+ export function hasNativeZlib() {
48
+ const zlib = getZlib();
49
+ return zlib !== null && typeof zlib.deflateRawSync === "function";
50
+ }
51
+ /**
52
+ * Check if CompressionStream is available (Browser/Node.js 17+)
53
+ */
54
+ export function hasCompressionStream() {
55
+ return typeof CompressionStream !== "undefined";
56
+ }
57
+ /**
58
+ * Compress data using the best available native method
59
+ *
60
+ * Priority:
61
+ * 1. Node.js zlib (if available) - fastest, supports compression levels
62
+ * 2. CompressionStream (browser/Node.js 17+) - no level support
63
+ * 3. Return uncompressed data (fallback)
64
+ *
65
+ * @param data - Data to compress
66
+ * @param options - Compression options
67
+ * @returns Compressed data
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const data = new TextEncoder().encode("Hello, World!");
72
+ * const compressed = await compress(data, { level: 6 });
73
+ * ```
74
+ */
75
+ export async function compress(data, options = {}) {
76
+ const level = options.level ?? 6;
77
+ // Level 0 means no compression
78
+ if (level === 0) {
79
+ return data;
80
+ }
81
+ // Ensure zlib is loaded first
82
+ const zlib = await ensureZlib();
83
+ // Try Node.js zlib first (fastest, supports levels)
84
+ if (zlib && typeof zlib.deflateRawSync === "function") {
85
+ const result = zlib.deflateRawSync(Buffer.from(data), { level });
86
+ return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
87
+ }
88
+ // Fall back to CompressionStream (browser/Node.js 17+)
89
+ if (typeof CompressionStream !== "undefined") {
90
+ return compressWithCompressionStream(data);
91
+ }
92
+ // No compression available - return original data
93
+ console.warn("No native compression available, returning uncompressed data");
94
+ return data;
95
+ }
96
+ /**
97
+ * Compress data synchronously using Node.js zlib
98
+ * Only available in Node.js environment
99
+ *
100
+ * @param data - Data to compress
101
+ * @param options - Compression options
102
+ * @returns Compressed data
103
+ * @throws Error if not in Node.js environment
104
+ */
105
+ export function compressSync(data, options = {}) {
106
+ const level = options.level ?? 6;
107
+ if (level === 0) {
108
+ return data;
109
+ }
110
+ const zlib = getZlib();
111
+ if (!zlib || typeof zlib.deflateRawSync !== "function") {
112
+ throw new Error("Synchronous compression is only available in Node.js environment");
113
+ }
114
+ const result = zlib.deflateRawSync(Buffer.from(data), { level });
115
+ return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
116
+ }
117
+ /**
118
+ * Compress using browser's native CompressionStream
119
+ * Uses "deflate-raw" format (required for ZIP files)
120
+ *
121
+ * Note: CompressionStream does not support compression level configuration
122
+ *
123
+ * @param data - Data to compress
124
+ * @returns Compressed data
125
+ */
126
+ async function compressWithCompressionStream(data) {
127
+ const cs = new CompressionStream("deflate-raw");
128
+ const writer = cs.writable.getWriter();
129
+ const reader = cs.readable.getReader();
130
+ // Write data and close
131
+ writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
132
+ writer.close();
133
+ // Read all compressed chunks
134
+ const chunks = [];
135
+ let totalLength = 0;
136
+ while (true) {
137
+ const { done, value } = await reader.read();
138
+ if (done) {
139
+ break;
140
+ }
141
+ chunks.push(value);
142
+ totalLength += value.length;
143
+ }
144
+ // Combine chunks into single array
145
+ const result = new Uint8Array(totalLength);
146
+ let offset = 0;
147
+ for (const chunk of chunks) {
148
+ result.set(chunk, offset);
149
+ offset += chunk.length;
150
+ }
151
+ return result;
152
+ }
153
+ /**
154
+ * Decompress data using the best available native method
155
+ *
156
+ * @param data - Compressed data (deflate-raw format)
157
+ * @returns Decompressed data
158
+ */
159
+ export async function decompress(data) {
160
+ // Ensure zlib is loaded first
161
+ const zlib = await ensureZlib();
162
+ // Try Node.js zlib first
163
+ if (zlib && typeof zlib.inflateRawSync === "function") {
164
+ const result = zlib.inflateRawSync(Buffer.from(data));
165
+ return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
166
+ }
167
+ // Fall back to DecompressionStream
168
+ if (typeof DecompressionStream !== "undefined") {
169
+ return decompressWithDecompressionStream(data);
170
+ }
171
+ throw new Error("No native decompression available");
172
+ }
173
+ /**
174
+ * Decompress data synchronously using Node.js zlib
175
+ *
176
+ * @param data - Compressed data (deflate-raw format)
177
+ * @returns Decompressed data
178
+ * @throws Error if not in Node.js environment
179
+ */
180
+ export function decompressSync(data) {
181
+ const zlib = getZlib();
182
+ if (!zlib || typeof zlib.inflateRawSync !== "function") {
183
+ throw new Error("Synchronous decompression is only available in Node.js environment");
184
+ }
185
+ const result = zlib.inflateRawSync(Buffer.from(data));
186
+ return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
187
+ }
188
+ /**
189
+ * Decompress using browser's native DecompressionStream
190
+ *
191
+ * @param data - Compressed data (deflate-raw format)
192
+ * @returns Decompressed data
193
+ */
194
+ async function decompressWithDecompressionStream(data) {
195
+ const ds = new DecompressionStream("deflate-raw");
196
+ const writer = ds.writable.getWriter();
197
+ const reader = ds.readable.getReader();
198
+ // Write data and close
199
+ writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
200
+ writer.close();
201
+ // Read all decompressed chunks
202
+ const chunks = [];
203
+ let totalLength = 0;
204
+ while (true) {
205
+ const { done, value } = await reader.read();
206
+ if (done) {
207
+ break;
208
+ }
209
+ chunks.push(value);
210
+ totalLength += value.length;
211
+ }
212
+ // Combine chunks into single array
213
+ const result = new Uint8Array(totalLength);
214
+ let offset = 0;
215
+ for (const chunk of chunks) {
216
+ result.set(chunk, offset);
217
+ offset += chunk.length;
218
+ }
219
+ return result;
220
+ }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * CRC32 calculation utility for ZIP files
3
+ *
4
+ * - Node.js: Uses native zlib.crc32 (C++ implementation, ~100x faster)
5
+ * - Browser: Uses lookup table optimization
6
+ *
7
+ * The polynomial used is the standard CRC-32 IEEE 802.3:
8
+ * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
9
+ * Represented as 0xEDB88320 in reversed (LSB-first) form
10
+ */
11
+ // Detect Node.js environment
12
+ const isNode = typeof process !== "undefined" && process.versions?.node;
13
+ // Lazy-loaded zlib module for Node.js
14
+ let _zlib = null;
15
+ let _zlibLoading = null;
16
+ // Auto-initialize zlib in Node.js environment
17
+ if (isNode) {
18
+ _zlibLoading = import("zlib")
19
+ .then(module => {
20
+ _zlib = module.default ?? module;
21
+ return _zlib;
22
+ })
23
+ .catch(() => {
24
+ _zlib = null;
25
+ return null;
26
+ });
27
+ }
28
+ /**
29
+ * Pre-computed CRC32 lookup table (256 entries)
30
+ * Generated using the standard polynomial 0xEDB88320
31
+ * Used as fallback when native zlib is not available
32
+ */
33
+ const CRC32_TABLE = /* @__PURE__ */ (() => {
34
+ const table = new Uint32Array(256);
35
+ for (let i = 0; i < 256; i++) {
36
+ let crc = i;
37
+ for (let j = 0; j < 8; j++) {
38
+ crc = crc & 1 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1;
39
+ }
40
+ table[i] = crc;
41
+ }
42
+ return table;
43
+ })();
44
+ /**
45
+ * JavaScript fallback CRC32 implementation using lookup table
46
+ */
47
+ function crc32JS(data) {
48
+ let crc = 0xffffffff;
49
+ for (let i = 0; i < data.length; i++) {
50
+ crc = CRC32_TABLE[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
51
+ }
52
+ return (crc ^ 0xffffffff) >>> 0;
53
+ }
54
+ /**
55
+ * Calculate CRC32 checksum for the given data
56
+ * Uses native zlib.crc32 in Node.js for ~100x better performance
57
+ *
58
+ * @param data - Input data as Uint8Array or Buffer
59
+ * @returns CRC32 checksum as unsigned 32-bit integer
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * const data = new TextEncoder().encode("Hello, World!");
64
+ * const checksum = crc32(data);
65
+ * console.log(checksum.toString(16)); // "ec4ac3d0"
66
+ * ```
67
+ */
68
+ export function crc32(data) {
69
+ // Use native zlib.crc32 if available (Node.js)
70
+ if (_zlib && typeof _zlib.crc32 === "function") {
71
+ return _zlib.crc32(data) >>> 0;
72
+ }
73
+ // Fallback to JS implementation
74
+ return crc32JS(data);
75
+ }
76
+ /**
77
+ * Ensure zlib is loaded (for use before calling crc32)
78
+ */
79
+ export async function ensureCrc32() {
80
+ if (_zlibLoading) {
81
+ await _zlibLoading;
82
+ }
83
+ }
84
+ /**
85
+ * Calculate CRC32 incrementally (useful for streaming)
86
+ * Call with initial crc of 0xffffffff, then finalize with crc32Finalize
87
+ * Note: This always uses JS implementation for consistency in streaming
88
+ *
89
+ * @param crc - Current CRC value (start with 0xffffffff)
90
+ * @param data - Input data chunk
91
+ * @returns Updated CRC value (not finalized)
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * let crc = 0xffffffff;
96
+ * crc = crc32Update(crc, chunk1);
97
+ * crc = crc32Update(crc, chunk2);
98
+ * const checksum = crc32Finalize(crc);
99
+ * ```
100
+ */
101
+ export function crc32Update(crc, data) {
102
+ for (let i = 0; i < data.length; i++) {
103
+ crc = CRC32_TABLE[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
104
+ }
105
+ return crc;
106
+ }
107
+ /**
108
+ * Finalize CRC32 calculation
109
+ * XOR with 0xffffffff and convert to unsigned 32-bit
110
+ *
111
+ * @param crc - CRC value from crc32Update
112
+ * @returns Final CRC32 checksum
113
+ */
114
+ export function crc32Finalize(crc) {
115
+ return (crc ^ 0xffffffff) >>> 0;
116
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Native ZIP utilities - Pure native implementation without third-party dependencies
3
+ *
4
+ * This module provides ZIP file creation using only native platform APIs:
5
+ * - Node.js: Uses native zlib module (C++ implementation, fastest)
6
+ * - Browser: Uses CompressionStream API (Chrome 80+, Firefox 113+, Safari 16.4+)
7
+ *
8
+ * Features:
9
+ * - Full ZIP format support (Local File Headers, Central Directory, EOCD)
10
+ * - DEFLATE compression (level 0-9 on Node.js, fixed level on browser)
11
+ * - STORE mode (no compression)
12
+ * - UTF-8 filename support
13
+ * - File comments and ZIP comments
14
+ * - Streaming API for large files
15
+ * - Both sync (Node.js) and async APIs
16
+ *
17
+ * @example Basic usage
18
+ * ```ts
19
+ * import { createZip } from "./utils/zip/index.js";
20
+ *
21
+ * const zipData = await createZip([
22
+ * { name: "hello.txt", data: new TextEncoder().encode("Hello!") },
23
+ * { name: "folder/nested.txt", data: new TextEncoder().encode("Nested file") }
24
+ * ], { level: 6 });
25
+ *
26
+ * // Write to file (Node.js)
27
+ * fs.writeFileSync("output.zip", zipData);
28
+ * ```
29
+ *
30
+ * @example Streaming usage
31
+ * ```ts
32
+ * import { ZipBuilder } from "./utils/zip/index.js";
33
+ *
34
+ * const builder = new ZipBuilder({ level: 1 });
35
+ *
36
+ * // Add files one by one
37
+ * const [header1, data1] = await builder.addFile({
38
+ * name: "file1.txt",
39
+ * data: new TextEncoder().encode("File 1 content")
40
+ * });
41
+ * stream.write(header1);
42
+ * stream.write(data1);
43
+ *
44
+ * // Finalize and write central directory
45
+ * for (const chunk of builder.finalize()) {
46
+ * stream.write(chunk);
47
+ * }
48
+ * ```
49
+ */
50
+ // CRC32 utilities
51
+ export { crc32, crc32Update, crc32Finalize } from "./crc32.js";
52
+ // Compression utilities
53
+ export { compress, compressSync, decompress, decompressSync, hasNativeZlib, hasCompressionStream } from "./compress.js";
54
+ // ZIP builder
55
+ export { createZip, createZipSync, ZipBuilder } from "./zip-builder.js";