@js-ak/excel-toolbox 1.2.6 → 1.3.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.
Files changed (87) hide show
  1. package/README.md +2 -2
  2. package/build/cjs/lib/index.js +1 -0
  3. package/build/cjs/lib/template/index.js +17 -0
  4. package/build/cjs/lib/template/template-fs.js +465 -0
  5. package/build/cjs/lib/template/utils/check-row.js +23 -0
  6. package/build/cjs/lib/template/utils/check-rows.js +19 -0
  7. package/build/cjs/lib/template/utils/check-start-row.js +18 -0
  8. package/build/cjs/lib/template/utils/column-index-to-letter.js +17 -0
  9. package/build/cjs/lib/template/utils/escape-xml.js +24 -0
  10. package/build/cjs/lib/template/utils/get-max-row-number.js +20 -0
  11. package/build/cjs/lib/template/utils/get-rows-above.js +23 -0
  12. package/build/cjs/lib/template/utils/get-rows-below.js +23 -0
  13. package/build/cjs/lib/template/utils/index.js +27 -0
  14. package/build/cjs/lib/template/utils/parse-rows.js +30 -0
  15. package/build/cjs/lib/template/utils/to-excel-column-object.js +29 -0
  16. package/build/cjs/lib/template/utils/write-rows-to-stream.js +41 -0
  17. package/build/cjs/lib/xml/build-merged-sheet.js +1 -1
  18. package/build/cjs/lib/zip/constants.js +16 -1
  19. package/build/cjs/lib/zip/create-sync.js +32 -32
  20. package/build/cjs/lib/zip/create-with-stream.js +150 -0
  21. package/build/cjs/lib/zip/create.js +32 -32
  22. package/build/cjs/lib/zip/index.js +1 -0
  23. package/build/cjs/lib/zip/read-sync.js +60 -12
  24. package/build/cjs/lib/zip/read.js +60 -12
  25. package/build/cjs/lib/zip/utils/crc-32-stream.js +36 -0
  26. package/build/cjs/lib/zip/utils/crc-32.js +43 -0
  27. package/build/cjs/lib/zip/utils/dos-time.js +50 -0
  28. package/build/cjs/lib/zip/utils/find-data-descriptor.js +29 -0
  29. package/build/cjs/lib/zip/utils/index.js +21 -0
  30. package/build/cjs/lib/zip/utils/to-bytes.js +37 -0
  31. package/build/esm/lib/index.js +1 -0
  32. package/build/esm/lib/template/index.js +1 -0
  33. package/build/esm/lib/template/template-fs.js +428 -0
  34. package/build/esm/lib/template/utils/check-row.js +20 -0
  35. package/build/esm/lib/template/utils/check-rows.js +16 -0
  36. package/build/esm/lib/template/utils/check-start-row.js +15 -0
  37. package/build/esm/lib/template/utils/column-index-to-letter.js +14 -0
  38. package/build/esm/lib/template/utils/escape-xml.js +21 -0
  39. package/build/esm/lib/template/utils/get-max-row-number.js +17 -0
  40. package/build/esm/lib/template/utils/get-rows-above.js +20 -0
  41. package/build/esm/lib/template/utils/get-rows-below.js +20 -0
  42. package/build/esm/lib/template/utils/index.js +11 -0
  43. package/build/esm/lib/template/utils/parse-rows.js +27 -0
  44. package/build/esm/lib/template/utils/to-excel-column-object.js +26 -0
  45. package/build/esm/lib/template/utils/write-rows-to-stream.js +38 -0
  46. package/build/esm/lib/xml/build-merged-sheet.js +1 -1
  47. package/build/esm/lib/zip/constants.js +15 -0
  48. package/build/esm/lib/zip/create-sync.js +1 -1
  49. package/build/esm/lib/zip/create-with-stream.js +111 -0
  50. package/build/esm/lib/zip/create.js +1 -1
  51. package/build/esm/lib/zip/index.js +1 -0
  52. package/build/esm/lib/zip/read-sync.js +27 -12
  53. package/build/esm/lib/zip/read.js +27 -12
  54. package/build/esm/lib/zip/utils/crc-32-stream.js +33 -0
  55. package/build/esm/lib/zip/utils/crc-32.js +40 -0
  56. package/build/esm/lib/zip/utils/dos-time.js +47 -0
  57. package/build/esm/lib/zip/utils/find-data-descriptor.js +26 -0
  58. package/build/esm/lib/zip/utils/index.js +5 -0
  59. package/build/esm/lib/zip/utils/to-bytes.js +34 -0
  60. package/build/types/lib/index.d.ts +1 -0
  61. package/build/types/lib/template/index.d.ts +1 -0
  62. package/build/types/lib/template/template-fs.d.ts +122 -0
  63. package/build/types/lib/template/utils/check-row.d.ts +14 -0
  64. package/build/types/lib/template/utils/check-rows.d.ts +11 -0
  65. package/build/types/lib/template/utils/check-start-row.d.ts +8 -0
  66. package/build/types/lib/template/utils/column-index-to-letter.d.ts +7 -0
  67. package/build/types/lib/template/utils/escape-xml.d.ts +14 -0
  68. package/build/types/lib/template/utils/get-max-row-number.d.ts +7 -0
  69. package/build/types/lib/template/utils/get-rows-above.d.ts +12 -0
  70. package/build/types/lib/template/utils/get-rows-below.d.ts +12 -0
  71. package/build/types/lib/template/utils/index.d.ts +11 -0
  72. package/build/types/lib/template/utils/parse-rows.d.ts +1 -0
  73. package/build/types/lib/template/utils/to-excel-column-object.d.ts +10 -0
  74. package/build/types/lib/template/utils/write-rows-to-stream.d.ts +25 -0
  75. package/build/types/lib/zip/constants.d.ts +9 -0
  76. package/build/types/lib/zip/create-with-stream.d.ts +13 -0
  77. package/build/types/lib/zip/index.d.ts +1 -0
  78. package/build/types/lib/zip/utils/crc-32-stream.d.ts +11 -0
  79. package/build/types/lib/zip/utils/crc-32.d.ts +15 -0
  80. package/build/types/lib/zip/utils/dos-time.d.ts +25 -0
  81. package/build/types/lib/zip/utils/find-data-descriptor.d.ts +15 -0
  82. package/build/types/lib/zip/utils/index.d.ts +5 -0
  83. package/build/types/lib/zip/utils/to-bytes.d.ts +20 -0
  84. package/package.json +1 -1
  85. package/build/cjs/lib/zip/utils.js +0 -157
  86. package/build/esm/lib/zip/utils.js +0 -152
  87. package/build/types/lib/zip/utils.d.ts +0 -58
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
@@ -6,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
39
  exports.read = read;
7
40
  const node_util_1 = __importDefault(require("node:util"));
8
41
  const node_zlib_1 = __importDefault(require("node:zlib"));
42
+ const Utils = __importStar(require("./utils/index.js"));
9
43
  const inflateRaw = node_util_1.default.promisify(node_zlib_1.default.inflateRaw);
10
44
  /**
11
45
  * Parses a ZIP archive from a buffer and extracts the files within.
@@ -29,22 +63,37 @@ async function read(buffer) {
29
63
  const fileNameEnd = fileNameStart + fileNameLength;
30
64
  const fileName = buffer.subarray(fileNameStart, fileNameEnd).toString();
31
65
  const dataStart = fileNameEnd + extraFieldLength;
32
- const compressedSize = buffer.readUInt32LE(offset + 18);
33
66
  const useDataDescriptor = (generalPurposeBitFlag & 0x08) !== 0;
34
- if (useDataDescriptor) {
35
- throw new Error(`File ${fileName} uses data descriptor. Not supported in this minimal parser.`);
36
- }
37
- const compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
67
+ let compressedData;
38
68
  let content;
39
69
  try {
40
- if (compressionMethod === 0) {
41
- content = compressedData;
42
- }
43
- else if (compressionMethod === 8) {
44
- content = await inflateRaw(compressedData);
70
+ if (useDataDescriptor) {
71
+ const { compressedSize, offset: ddOffset } = Utils.findDataDescriptor(buffer, dataStart);
72
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
73
+ if (compressionMethod === 0) {
74
+ content = compressedData;
75
+ }
76
+ else if (compressionMethod === 8) {
77
+ content = await inflateRaw(compressedData);
78
+ }
79
+ else {
80
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
81
+ }
82
+ offset = ddOffset + 16; // Skip over data descriptor
45
83
  }
46
84
  else {
47
- throw new Error(`Unsupported compression method ${compressionMethod}`);
85
+ const compressedSize = buffer.readUInt32LE(offset + 18);
86
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
87
+ if (compressionMethod === 0) {
88
+ content = compressedData;
89
+ }
90
+ else if (compressionMethod === 8) {
91
+ content = await inflateRaw(compressedData);
92
+ }
93
+ else {
94
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
95
+ }
96
+ offset = dataStart + compressedSize;
48
97
  }
49
98
  }
50
99
  catch (error) {
@@ -52,7 +101,6 @@ async function read(buffer) {
52
101
  throw new Error(`Error unpacking file ${fileName}: ${message}`);
53
102
  }
54
103
  files[fileName] = content;
55
- offset = dataStart + compressedSize;
56
104
  }
57
105
  return files;
58
106
  }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.crc32Stream = crc32Stream;
4
+ const node_stream_1 = require("node:stream");
5
+ const constants_js_1 = require("../constants.js");
6
+ /**
7
+ * Computes the CRC-32 checksum for the given byte, using the standard IEEE 802.3 polynomial.
8
+ * This is a low-level function that is used by the crc32Stream() function.
9
+ *
10
+ * @param {number} byte - The byte (0-255) to add to the checksum.
11
+ * @param {number} crc - The current checksum value to update.
12
+ * @returns {number} - The new checksum value.
13
+ */
14
+ function crc32(byte, crc = 0xffffffff) {
15
+ return constants_js_1.CRC32_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
16
+ }
17
+ /**
18
+ * Creates a Transform stream that computes the CRC-32 checksum of the input data.
19
+ *
20
+ * The `digest()` method can be called on the returned stream to get the final checksum value.
21
+ *
22
+ * @returns {Transform & { digest: () => number }} - The Transform stream.
23
+ */
24
+ function crc32Stream() {
25
+ let crc = 0xffffffff;
26
+ const transform = new node_stream_1.Transform({
27
+ transform(chunk, _encoding, callback) {
28
+ for (let i = 0; i < chunk.length; i++) {
29
+ crc = crc32(chunk[i], crc);
30
+ }
31
+ callback(null, chunk);
32
+ },
33
+ });
34
+ transform.digest = () => (crc ^ 0xffffffff) >>> 0;
35
+ return transform;
36
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.crc32 = crc32;
4
+ const constants_js_1 = require("../constants.js");
5
+ /**
6
+ * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
7
+ * This implementation uses a precomputed lookup table for optimal performance.
8
+ *
9
+ * The algorithm follows these characteristics:
10
+ * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
11
+ * - Initial value: 0xFFFFFFFF (inverted by ~0)
12
+ * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
13
+ * - Input and output reflection: Yes
14
+ *
15
+ * @param {Buffer} buf - The input buffer to calculate checksum for
16
+ * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
17
+ */
18
+ function crc32(buf) {
19
+ // Initialize CRC with all 1's (0xFFFFFFFF) using bitwise NOT
20
+ let crc = ~0;
21
+ // Process each byte in the buffer
22
+ for (let i = 0; i < buf.length; i++) {
23
+ /*
24
+ * CRC update algorithm steps:
25
+ * 1. XOR current CRC with next byte (lowest 8 bits)
26
+ * 2. Use result as index in precomputed table (0-255)
27
+ * 3. XOR the table value with right-shifted CRC (8 bits)
28
+ *
29
+ * The operation breakdown:
30
+ * - (crc ^ buf[i]) - XOR with next byte
31
+ * - & 0xff - Isolate lowest 8 bits
32
+ * - crc >>> 8 - Shift CRC right by 8 bits (unsigned)
33
+ * - ^ crcTable[...] - XOR with precomputed table value
34
+ */
35
+ crc = (crc >>> 8) ^ constants_js_1.CRC32_TABLE[(crc ^ buf[i]) & 0xff];
36
+ }
37
+ /*
38
+ * Final processing:
39
+ * 1. Invert all bits (~crc) to match standard CRC-32 output
40
+ * 2. Convert to unsigned 32-bit integer (>>> 0)
41
+ */
42
+ return ~crc >>> 0;
43
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dosTime = dosTime;
4
+ const node_buffer_1 = require("node:buffer");
5
+ const to_bytes_js_1 = require("./to-bytes.js");
6
+ /**
7
+ * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
8
+ * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
9
+ *
10
+ * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
11
+ * the following bit layout:
12
+ *
13
+ * Time portion (2 bytes/16 bits):
14
+ * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
15
+ * - Bits 05-10: Minutes (0-59)
16
+ * - Bits 11-15: Hours (0-23)
17
+ *
18
+ * Date portion (2 bytes/16 bits):
19
+ * - Bits 00-04: Day (1-31)
20
+ * - Bits 05-08: Month (1-12)
21
+ * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
22
+ *
23
+ * @param {Date} date - The JavaScript Date object to convert
24
+ * @returns {Buffer} - 4-byte Buffer containing:
25
+ * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
26
+ * - Bytes 2-3: DOS date (year-1980, month, day)
27
+ * @throws {RangeError} - If the date is before 1980 or after 2107
28
+ */
29
+ function dosTime(date) {
30
+ // Pack time components into 2 bytes (16 bits):
31
+ // - Hours (5 bits) shifted left 11 positions (bits 11-15)
32
+ // - Minutes (6 bits) shifted left 5 positions (bits 5-10)
33
+ // - Seconds/2 (5 bits) in least significant bits (bits 0-4)
34
+ const time = (date.getHours() << 11) | // Hours occupy bits 11-15
35
+ (date.getMinutes() << 5) | // Minutes occupy bits 5-10
36
+ (Math.floor(date.getSeconds() / 2)); // Seconds/2 occupy bits 0-4
37
+ // Pack date components into 2 bytes (16 bits):
38
+ // - (Year-1980) (7 bits) shifted left 9 positions (bits 9-15)
39
+ // - Month (4 bits) shifted left 5 positions (bits 5-8)
40
+ // - Day (5 bits) in least significant bits (bits 0-4)
41
+ const day = ((date.getFullYear() - 1980) << 9) | // Years since 1980 (bits 9-15)
42
+ ((date.getMonth() + 1) << 5) | // Month 1-12 (bits 5-8)
43
+ date.getDate(); // Day 1-31 (bits 0-4)
44
+ // Combine both 2-byte values into a single 4-byte Buffer
45
+ // Note: Using little-endian byte order for each 2-byte segment
46
+ return node_buffer_1.Buffer.from([
47
+ ...(0, to_bytes_js_1.toBytes)(time, 2), // Convert time to 2 bytes (LSB first)
48
+ ...(0, to_bytes_js_1.toBytes)(day, 2), // Convert date to 2 bytes (LSB first)
49
+ ]);
50
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findDataDescriptor = findDataDescriptor;
4
+ /**
5
+ * Finds a Data Descriptor in a ZIP archive buffer.
6
+ *
7
+ * The Data Descriptor is an optional 16-byte structure that appears at the end of a file's compressed data.
8
+ * It contains the compressed size of the file, and must be used when the Local File Header does not contain this information.
9
+ *
10
+ * @param buffer - The buffer containing the ZIP archive data.
11
+ * @param start - The starting offset in the buffer to search for the Data Descriptor.
12
+ * @returns - An object with `offset` and `compressedSize` properties.
13
+ * @throws {Error} - If the Data Descriptor is not found.
14
+ */
15
+ function findDataDescriptor(buffer, start) {
16
+ const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
17
+ const DATA_DESCRIPTOR_TOTAL_LENGTH = 16;
18
+ const COMPRESSED_SIZE_OFFSET_FROM_SIGNATURE = 8;
19
+ for (let i = start; i <= buffer.length - DATA_DESCRIPTOR_TOTAL_LENGTH; i++) {
20
+ if (buffer.readUInt32LE(i) === DATA_DESCRIPTOR_SIGNATURE) {
21
+ const compressedSize = buffer.readUInt32LE(i + COMPRESSED_SIZE_OFFSET_FROM_SIGNATURE);
22
+ return {
23
+ compressedSize,
24
+ offset: i,
25
+ };
26
+ }
27
+ }
28
+ throw new Error("Data Descriptor not found");
29
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./crc-32-stream.js"), exports);
18
+ __exportStar(require("./crc-32.js"), exports);
19
+ __exportStar(require("./dos-time.js"), exports);
20
+ __exportStar(require("./find-data-descriptor.js"), exports);
21
+ __exportStar(require("./to-bytes.js"), exports);
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBytes = toBytes;
4
+ const node_buffer_1 = require("node:buffer");
5
+ /**
6
+ * Converts a numeric value into a fixed-length Buffer representation,
7
+ * storing the value in little-endian format with right-padding of zeros.
8
+ *
9
+ * This is particularly useful for binary protocols or file formats that
10
+ * require fixed-width numeric fields.
11
+ *
12
+ * @param {number} value - The numeric value to convert to bytes.
13
+ * Note: JavaScript numbers are IEEE 754 doubles, but only the
14
+ * integer portion will be used (up to 53-bit precision).
15
+ * @param {number} len - The desired length of the output Buffer in bytes.
16
+ * Must be a positive integer.
17
+ * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
18
+ * 1. The value's bytes in little-endian order (least significant byte first)
19
+ * 2. Zero padding in any remaining higher-order bytes
20
+ * @throws {RangeError} - If the value requires more bytes than `len` to represent
21
+ * (though this is currently not explicitly checked)
22
+ */
23
+ function toBytes(value, len) {
24
+ // Allocate a new Buffer of the requested length, automatically zero-filled
25
+ const buf = node_buffer_1.Buffer.alloc(len);
26
+ // Process each byte position from least significant to most significant
27
+ for (let i = 0; i < len; i++) {
28
+ // Store the least significant byte of the current value
29
+ buf[i] = value & 0xff; // Mask to get bottom 8 bits
30
+ // Right-shift the value by 8 bits to process the next byte
31
+ // Note: This uses unsigned right shift (>>> would be signed)
32
+ value >>= 8;
33
+ // If the loop completes with value != 0, we've overflowed the buffer length,
34
+ // but this isn't currently checked/handled
35
+ }
36
+ return buf;
37
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./merge-sheets-to-base-file-sync.js";
2
2
  export * from "./merge-sheets-to-base-file.js";
3
+ export * from "./template/index.js";
@@ -0,0 +1 @@
1
+ export * from "./template-fs.js";