@js-ak/excel-toolbox 1.2.5 → 1.2.7

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 (42) hide show
  1. package/README.md +2 -2
  2. package/build/cjs/lib/merge-sheets-to-base-file-process.js +5 -5
  3. package/build/cjs/lib/utils/remove-sheet-by-name.js +9 -7
  4. package/build/cjs/lib/xml/build-merged-sheet.js +2 -2
  5. package/build/cjs/lib/xml/extract-rows-from-sheet.js +1 -0
  6. package/build/cjs/lib/zip/create-sync.js +32 -32
  7. package/build/cjs/lib/zip/create.js +32 -32
  8. package/build/cjs/lib/zip/read-sync.js +72 -24
  9. package/build/cjs/lib/zip/read.js +68 -23
  10. package/build/cjs/lib/zip/utils/crc-32.js +76 -0
  11. package/build/cjs/lib/zip/utils/dos-time.js +50 -0
  12. package/build/cjs/lib/zip/utils/find-data-descriptor.js +29 -0
  13. package/build/cjs/lib/zip/utils/index.js +20 -0
  14. package/build/cjs/lib/zip/utils/to-bytes.js +37 -0
  15. package/build/esm/lib/merge-sheets-to-base-file-process.js +5 -5
  16. package/build/esm/lib/utils/remove-sheet-by-name.js +9 -7
  17. package/build/esm/lib/xml/build-merged-sheet.js +2 -2
  18. package/build/esm/lib/xml/extract-rows-from-sheet.js +1 -0
  19. package/build/esm/lib/zip/create-sync.js +1 -1
  20. package/build/esm/lib/zip/create.js +1 -1
  21. package/build/esm/lib/zip/read-sync.js +36 -24
  22. package/build/esm/lib/zip/read.js +35 -23
  23. package/build/esm/lib/zip/utils/crc-32.js +73 -0
  24. package/build/esm/lib/zip/utils/dos-time.js +47 -0
  25. package/build/esm/lib/zip/utils/find-data-descriptor.js +26 -0
  26. package/build/esm/lib/zip/utils/index.js +4 -0
  27. package/build/esm/lib/zip/utils/to-bytes.js +34 -0
  28. package/build/types/lib/merge-sheets-to-base-file-process.d.ts +2 -2
  29. package/build/types/lib/utils/remove-sheet-by-name.d.ts +1 -1
  30. package/build/types/lib/xml/build-merged-sheet.d.ts +2 -2
  31. package/build/types/lib/xml/extract-rows-from-sheet.d.ts +1 -0
  32. package/build/types/lib/zip/read-sync.d.ts +2 -2
  33. package/build/types/lib/zip/read.d.ts +2 -2
  34. package/build/types/lib/zip/utils/crc-32.d.ts +15 -0
  35. package/build/types/lib/zip/utils/dos-time.d.ts +25 -0
  36. package/build/types/lib/zip/utils/find-data-descriptor.d.ts +15 -0
  37. package/build/types/lib/zip/utils/index.d.ts +4 -0
  38. package/build/types/lib/zip/utils/to-bytes.d.ts +20 -0
  39. package/package.json +1 -1
  40. package/build/cjs/lib/zip/utils.js +0 -157
  41. package/build/esm/lib/zip/utils.js +0 -152
  42. package/build/types/lib/zip/utils.d.ts +0 -58
@@ -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,20 @@
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.js"), exports);
18
+ __exportStar(require("./dos-time.js"), exports);
19
+ __exportStar(require("./find-data-descriptor.js"), exports);
20
+ __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
+ }
@@ -22,7 +22,7 @@ export function mergeSheetsToBaseFileProcess(data) {
22
22
  if (!baseFiles[basePath]) {
23
23
  throw new Error(`Base file does not contain ${basePath}`);
24
24
  }
25
- const { lastRowNumber, mergeCells: baseMergeCells, rows: baseRows, } = Xml.extractRowsFromSheet(baseFiles[basePath]);
25
+ const { lastRowNumber, mergeCells: baseMergeCells, rows: baseRows, xml, } = Xml.extractRowsFromSheet(baseFiles[basePath]);
26
26
  const allRows = [...baseRows];
27
27
  const allMergeCells = [...baseMergeCells];
28
28
  let currentRowOffset = lastRowNumber + gap;
@@ -48,19 +48,19 @@ export function mergeSheetsToBaseFileProcess(data) {
48
48
  currentRowOffset += Utils.getMaxRowNumber(rows) + gap;
49
49
  }
50
50
  }
51
- const mergedXml = Xml.buildMergedSheet(baseFiles[basePath], allRows, allMergeCells);
51
+ const mergedXml = Xml.buildMergedSheet(xml, allRows, allMergeCells);
52
52
  baseFiles[basePath] = mergedXml;
53
53
  for (const sheetIndex of sheetsToRemove) {
54
54
  const sheetPath = `xl/worksheets/sheet${sheetIndex}.xml`;
55
55
  delete baseFiles[sheetPath];
56
56
  if (baseFiles["xl/workbook.xml"]) {
57
- baseFiles["xl/workbook.xml"] = Utils.removeSheetFromWorkbook(baseFiles["xl/workbook.xml"], sheetIndex);
57
+ baseFiles["xl/workbook.xml"] = Buffer.from(Utils.removeSheetFromWorkbook(baseFiles["xl/workbook.xml"].toString(), sheetIndex));
58
58
  }
59
59
  if (baseFiles["xl/_rels/workbook.xml.rels"]) {
60
- baseFiles["xl/_rels/workbook.xml.rels"] = Utils.removeSheetFromRels(baseFiles["xl/_rels/workbook.xml.rels"], sheetIndex);
60
+ baseFiles["xl/_rels/workbook.xml.rels"] = Buffer.from(Utils.removeSheetFromRels(baseFiles["xl/_rels/workbook.xml.rels"].toString(), sheetIndex));
61
61
  }
62
62
  if (baseFiles["[Content_Types].xml"]) {
63
- baseFiles["[Content_Types].xml"] = Utils.removeSheetFromContentTypes(baseFiles["[Content_Types].xml"], sheetIndex);
63
+ baseFiles["[Content_Types].xml"] = Buffer.from(Utils.removeSheetFromContentTypes(baseFiles["[Content_Types].xml"].toString(), sheetIndex));
64
64
  }
65
65
  }
66
66
  for (const sheetName of sheetNamesToRemove) {
@@ -5,8 +5,8 @@
5
5
  * @returns {void}
6
6
  */
7
7
  export function removeSheetByName(files, sheetName) {
8
- const workbookXml = files["xl/workbook.xml"];
9
- const relsXml = files["xl/_rels/workbook.xml.rels"];
8
+ const workbookXml = files["xl/workbook.xml"]?.toString();
9
+ const relsXml = files["xl/_rels/workbook.xml.rels"]?.toString();
10
10
  if (!workbookXml || !relsXml) {
11
11
  return;
12
12
  }
@@ -31,11 +31,13 @@ export function removeSheetByName(files, sheetName) {
31
31
  return;
32
32
  }
33
33
  const targetPath = `xl/${targetMatch[1]}`.replace(/\\/g, "/");
34
- delete files[targetPath];
35
- files["xl/workbook.xml"] = workbookXml.replace(sheetTag, "");
36
- files["xl/_rels/workbook.xml.rels"] = relsXml.replace(relTag, "");
37
- const contentTypes = files["[Content_Types].xml"];
34
+ if (targetPath) {
35
+ delete files[targetPath];
36
+ }
37
+ files["xl/workbook.xml"] = Buffer.from(workbookXml.replace(sheetTag, ""));
38
+ files["xl/_rels/workbook.xml.rels"] = Buffer.from(relsXml.replace(relTag, ""));
39
+ const contentTypes = files["[Content_Types].xml"]?.toString();
38
40
  if (contentTypes) {
39
- files["[Content_Types].xml"] = contentTypes.replace(new RegExp(`<Override[^>]+PartName=["']/${targetPath}["'][^>]*/>`, "g"), "");
41
+ files["[Content_Types].xml"] = Buffer.from(contentTypes.replace(new RegExp(`<Override[^>]+PartName=["']/${targetPath}["'][^>]*/>`, "g"), ""));
40
42
  }
41
43
  }
@@ -9,7 +9,7 @@
9
9
  * @param {string[]} mergedRows - Array of XML strings representing each row in the merged sheet.
10
10
  * @param {Object[]} [mergeCells] - Optional array of merge cell definitions.
11
11
  * Each object should have a 'ref' property specifying the merge range (e.g., "A1:B2").
12
- * @returns {string} - The reconstructed XML string with merged content.
12
+ * @returns {Buffer} - The reconstructed XML string with merged content.
13
13
  */
14
14
  export function buildMergedSheet(originalXml, mergedRows, mergeCells = []) {
15
15
  // Remove any existing <mergeCells> section from the XML
@@ -24,5 +24,5 @@ export function buildMergedSheet(originalXml, mergedRows, mergeCells = []) {
24
24
  // Insert <mergeCells> after </sheetData> and before the next XML tag
25
25
  xmlData = xmlData.replace(/(<\/sheetData>)(\s*<)/, `$1\n${mergeCellsXml}\n$2`);
26
26
  }
27
- return xmlData;
27
+ return Buffer.from(xmlData);
28
28
  }
@@ -57,5 +57,6 @@ export function extractRowsFromSheet(sheet) {
57
57
  lastRowNumber,
58
58
  mergeCells,
59
59
  rows,
60
+ xml,
60
61
  };
61
62
  }
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from "node:buffer";
2
2
  import { deflateRawSync } from "node:zlib";
3
- import { crc32, dosTime, toBytes } from "./utils.js";
3
+ import { crc32, dosTime, toBytes } from "./utils/index.js";
4
4
  import { CENTRAL_DIR_HEADER_SIG, END_OF_CENTRAL_DIR_SIG, LOCAL_FILE_HEADER_SIG, } from "./constants.js";
5
5
  /**
6
6
  * Creates a ZIP archive from a collection of files.
@@ -2,7 +2,7 @@ import { Buffer } from "node:buffer";
2
2
  import util from "node:util";
3
3
  import zlib from "node:zlib";
4
4
  const deflateRaw = util.promisify(zlib.deflateRaw);
5
- import { crc32, dosTime, toBytes } from "./utils.js";
5
+ import { crc32, dosTime, toBytes } from "./utils/index.js";
6
6
  import { CENTRAL_DIR_HEADER_SIG, END_OF_CENTRAL_DIR_SIG, LOCAL_FILE_HEADER_SIG, } from "./constants.js";
7
7
  /**
8
8
  * Creates a ZIP archive from a collection of files.
@@ -1,45 +1,58 @@
1
- import { inflateRawSync } from "node:zlib";
1
+ import zlib from "node:zlib";
2
+ import * as Utils from "./utils/index.js";
2
3
  /**
3
4
  * Parses a ZIP archive from a buffer and extracts the files within.
4
5
  *
5
6
  * @param {Buffer} buffer - The buffer containing the ZIP archive data.
6
- * @returns {Object.<string, string>} - An object where keys are file names and values are file contents.
7
+ * @returns {Object.<string, Buffer>} - An object where keys are file names and values are file contents as Buffers.
7
8
  * @throws {Error} - Throws an error if an unsupported compression method is encountered or if decompression fails.
8
9
  */
9
10
  export function readSync(buffer) {
10
11
  const files = {};
11
12
  let offset = 0;
12
- while (offset + 4 <= buffer.length) {
13
+ while (offset + 30 <= buffer.length) {
13
14
  const signature = buffer.readUInt32LE(offset);
14
15
  if (signature !== 0x04034b50)
15
- break;
16
+ break; // not a local file header
17
+ const generalPurposeBitFlag = buffer.readUInt16LE(offset + 6);
16
18
  const compressionMethod = buffer.readUInt16LE(offset + 8);
17
19
  const fileNameLength = buffer.readUInt16LE(offset + 26);
18
- const extraLength = buffer.readUInt16LE(offset + 28);
20
+ const extraFieldLength = buffer.readUInt16LE(offset + 28);
19
21
  const fileNameStart = offset + 30;
20
22
  const fileNameEnd = fileNameStart + fileNameLength;
21
23
  const fileName = buffer.subarray(fileNameStart, fileNameEnd).toString();
22
- const dataStart = fileNameEnd + extraLength;
23
- let nextOffset = dataStart;
24
- while (nextOffset + 4 <= buffer.length) {
25
- if (buffer.readUInt32LE(nextOffset) === 0x04034b50)
26
- break;
27
- nextOffset++;
28
- }
29
- if (nextOffset + 4 > buffer.length) {
30
- nextOffset = buffer.length;
31
- }
32
- const compressedData = buffer.subarray(dataStart, nextOffset);
33
- let content = "";
24
+ const dataStart = fileNameEnd + extraFieldLength;
25
+ const useDataDescriptor = (generalPurposeBitFlag & 0x08) !== 0;
26
+ let compressedData;
27
+ let content;
34
28
  try {
35
- if (compressionMethod === 0) {
36
- content = compressedData.toString();
37
- }
38
- else if (compressionMethod === 8) {
39
- content = inflateRawSync(new Uint8Array(compressedData)).toString();
29
+ if (useDataDescriptor) {
30
+ const { compressedSize, offset: ddOffset } = Utils.findDataDescriptor(buffer, dataStart);
31
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
32
+ if (compressionMethod === 0) {
33
+ content = compressedData;
34
+ }
35
+ else if (compressionMethod === 8) {
36
+ content = zlib.inflateRawSync(compressedData);
37
+ }
38
+ else {
39
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
40
+ }
41
+ offset = ddOffset + 16; // Skip over data descriptor
40
42
  }
41
43
  else {
42
- throw new Error(`Unsupported compression method ${compressionMethod}`);
44
+ const compressedSize = buffer.readUInt32LE(offset + 18);
45
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
46
+ if (compressionMethod === 0) {
47
+ content = compressedData;
48
+ }
49
+ else if (compressionMethod === 8) {
50
+ content = zlib.inflateRawSync(compressedData);
51
+ }
52
+ else {
53
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
54
+ }
55
+ offset = dataStart + compressedSize;
43
56
  }
44
57
  }
45
58
  catch (error) {
@@ -47,7 +60,6 @@ export function readSync(buffer) {
47
60
  throw new Error(`Error unpacking file ${fileName}: ${message}`);
48
61
  }
49
62
  files[fileName] = content;
50
- offset = nextOffset;
51
63
  }
52
64
  return files;
53
65
  }
@@ -1,47 +1,60 @@
1
1
  import util from "node:util";
2
2
  import zlib from "node:zlib";
3
+ import * as Utils from "./utils/index.js";
3
4
  const inflateRaw = util.promisify(zlib.inflateRaw);
4
5
  /**
5
6
  * Parses a ZIP archive from a buffer and extracts the files within.
6
7
  *
7
8
  * @param {Buffer} buffer - The buffer containing the ZIP archive data.
8
- * @returns {Object.<string, string>} - An object where keys are file names and values are file contents.
9
+ * @returns {Object.<string, Buffer>} - An object where keys are file names and values are file contents as Buffers.
9
10
  * @throws {Error} - Throws an error if an unsupported compression method is encountered or if decompression fails.
10
11
  */
11
12
  export async function read(buffer) {
12
13
  const files = {};
13
14
  let offset = 0;
14
- while (offset + 4 <= buffer.length) {
15
+ while (offset + 30 <= buffer.length) {
15
16
  const signature = buffer.readUInt32LE(offset);
16
17
  if (signature !== 0x04034b50)
17
- break;
18
+ break; // not a local file header
19
+ const generalPurposeBitFlag = buffer.readUInt16LE(offset + 6);
18
20
  const compressionMethod = buffer.readUInt16LE(offset + 8);
19
21
  const fileNameLength = buffer.readUInt16LE(offset + 26);
20
- const extraLength = buffer.readUInt16LE(offset + 28);
22
+ const extraFieldLength = buffer.readUInt16LE(offset + 28);
21
23
  const fileNameStart = offset + 30;
22
24
  const fileNameEnd = fileNameStart + fileNameLength;
23
25
  const fileName = buffer.subarray(fileNameStart, fileNameEnd).toString();
24
- const dataStart = fileNameEnd + extraLength;
25
- let nextOffset = dataStart;
26
- while (nextOffset + 4 <= buffer.length) {
27
- if (buffer.readUInt32LE(nextOffset) === 0x04034b50)
28
- break;
29
- nextOffset++;
30
- }
31
- if (nextOffset + 4 > buffer.length) {
32
- nextOffset = buffer.length;
33
- }
34
- const compressedData = buffer.subarray(dataStart, nextOffset);
35
- let content = "";
26
+ const dataStart = fileNameEnd + extraFieldLength;
27
+ const useDataDescriptor = (generalPurposeBitFlag & 0x08) !== 0;
28
+ let compressedData;
29
+ let content;
36
30
  try {
37
- if (compressionMethod === 0) {
38
- content = compressedData.toString();
39
- }
40
- else if (compressionMethod === 8) {
41
- content = (await inflateRaw(new Uint8Array(compressedData))).toString();
31
+ if (useDataDescriptor) {
32
+ const { compressedSize, offset: ddOffset } = Utils.findDataDescriptor(buffer, dataStart);
33
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
34
+ if (compressionMethod === 0) {
35
+ content = compressedData;
36
+ }
37
+ else if (compressionMethod === 8) {
38
+ content = await inflateRaw(compressedData);
39
+ }
40
+ else {
41
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
42
+ }
43
+ offset = ddOffset + 16; // Skip over data descriptor
42
44
  }
43
45
  else {
44
- throw new Error(`Unsupported compression method ${compressionMethod}`);
46
+ const compressedSize = buffer.readUInt32LE(offset + 18);
47
+ compressedData = buffer.subarray(dataStart, dataStart + compressedSize);
48
+ if (compressionMethod === 0) {
49
+ content = compressedData;
50
+ }
51
+ else if (compressionMethod === 8) {
52
+ content = await inflateRaw(compressedData);
53
+ }
54
+ else {
55
+ throw new Error(`Unsupported compression method ${compressionMethod}`);
56
+ }
57
+ offset = dataStart + compressedSize;
45
58
  }
46
59
  }
47
60
  catch (error) {
@@ -49,7 +62,6 @@ export async function read(buffer) {
49
62
  throw new Error(`Error unpacking file ${fileName}: ${message}`);
50
63
  }
51
64
  files[fileName] = content;
52
- offset = nextOffset;
53
65
  }
54
66
  return files;
55
67
  }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Precomputed CRC-32 lookup table for optimized checksum calculation.
3
+ * The table is generated using the standard IEEE 802.3 (Ethernet) polynomial:
4
+ * 0xEDB88320 (reversed representation of 0x04C11DB7).
5
+ *
6
+ * The table is immediately invoked and cached as a constant for performance,
7
+ * following the common implementation pattern for CRC algorithms.
8
+ */
9
+ const crcTable = (() => {
10
+ // Create a typed array for better performance with 256 32-bit unsigned integers
11
+ const table = new Uint32Array(256);
12
+ // Generate table entries for all possible byte values (0-255)
13
+ for (let i = 0; i < 256; i++) {
14
+ let crc = i; // Initialize with current byte value
15
+ // Process each bit (8 times)
16
+ for (let j = 0; j < 8; j++) {
17
+ /*
18
+ * CRC division algorithm:
19
+ * 1. If LSB is set (crc & 1), XOR with polynomial
20
+ * 2. Right-shift by 1 (unsigned)
21
+ *
22
+ * The polynomial 0xEDB88320 is:
23
+ * - Bit-reversed version of 0x04C11DB7
24
+ * - Uses reflected input/output algorithm
25
+ */
26
+ crc = crc & 1
27
+ ? 0xedb88320 ^ (crc >>> 1) // XOR with polynomial if LSB is set
28
+ : crc >>> 1; // Just shift right if LSB is not set
29
+ }
30
+ // Store final 32-bit value (>>> 0 ensures unsigned 32-bit representation)
31
+ table[i] = crc >>> 0;
32
+ }
33
+ return table;
34
+ })();
35
+ /**
36
+ * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
37
+ * This implementation uses a precomputed lookup table for optimal performance.
38
+ *
39
+ * The algorithm follows these characteristics:
40
+ * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
41
+ * - Initial value: 0xFFFFFFFF (inverted by ~0)
42
+ * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
43
+ * - Input and output reflection: Yes
44
+ *
45
+ * @param {Buffer} buf - The input buffer to calculate checksum for
46
+ * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
47
+ */
48
+ export function crc32(buf) {
49
+ // Initialize CRC with all 1's (0xFFFFFFFF) using bitwise NOT
50
+ let crc = ~0;
51
+ // Process each byte in the buffer
52
+ for (let i = 0; i < buf.length; i++) {
53
+ /*
54
+ * CRC update algorithm steps:
55
+ * 1. XOR current CRC with next byte (lowest 8 bits)
56
+ * 2. Use result as index in precomputed table (0-255)
57
+ * 3. XOR the table value with right-shifted CRC (8 bits)
58
+ *
59
+ * The operation breakdown:
60
+ * - (crc ^ buf[i]) - XOR with next byte
61
+ * - & 0xff - Isolate lowest 8 bits
62
+ * - crc >>> 8 - Shift CRC right by 8 bits (unsigned)
63
+ * - ^ crcTable[...] - XOR with precomputed table value
64
+ */
65
+ crc = (crc >>> 8) ^ crcTable[(crc ^ buf[i]) & 0xff];
66
+ }
67
+ /*
68
+ * Final processing:
69
+ * 1. Invert all bits (~crc) to match standard CRC-32 output
70
+ * 2. Convert to unsigned 32-bit integer (>>> 0)
71
+ */
72
+ return ~crc >>> 0;
73
+ }
@@ -0,0 +1,47 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { toBytes } from "./to-bytes.js";
3
+ /**
4
+ * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
5
+ * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
6
+ *
7
+ * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
8
+ * the following bit layout:
9
+ *
10
+ * Time portion (2 bytes/16 bits):
11
+ * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
12
+ * - Bits 05-10: Minutes (0-59)
13
+ * - Bits 11-15: Hours (0-23)
14
+ *
15
+ * Date portion (2 bytes/16 bits):
16
+ * - Bits 00-04: Day (1-31)
17
+ * - Bits 05-08: Month (1-12)
18
+ * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
19
+ *
20
+ * @param {Date} date - The JavaScript Date object to convert
21
+ * @returns {Buffer} - 4-byte Buffer containing:
22
+ * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
23
+ * - Bytes 2-3: DOS date (year-1980, month, day)
24
+ * @throws {RangeError} - If the date is before 1980 or after 2107
25
+ */
26
+ export function dosTime(date) {
27
+ // Pack time components into 2 bytes (16 bits):
28
+ // - Hours (5 bits) shifted left 11 positions (bits 11-15)
29
+ // - Minutes (6 bits) shifted left 5 positions (bits 5-10)
30
+ // - Seconds/2 (5 bits) in least significant bits (bits 0-4)
31
+ const time = (date.getHours() << 11) | // Hours occupy bits 11-15
32
+ (date.getMinutes() << 5) | // Minutes occupy bits 5-10
33
+ (Math.floor(date.getSeconds() / 2)); // Seconds/2 occupy bits 0-4
34
+ // Pack date components into 2 bytes (16 bits):
35
+ // - (Year-1980) (7 bits) shifted left 9 positions (bits 9-15)
36
+ // - Month (4 bits) shifted left 5 positions (bits 5-8)
37
+ // - Day (5 bits) in least significant bits (bits 0-4)
38
+ const day = ((date.getFullYear() - 1980) << 9) | // Years since 1980 (bits 9-15)
39
+ ((date.getMonth() + 1) << 5) | // Month 1-12 (bits 5-8)
40
+ date.getDate(); // Day 1-31 (bits 0-4)
41
+ // Combine both 2-byte values into a single 4-byte Buffer
42
+ // Note: Using little-endian byte order for each 2-byte segment
43
+ return Buffer.from([
44
+ ...toBytes(time, 2), // Convert time to 2 bytes (LSB first)
45
+ ...toBytes(day, 2), // Convert date to 2 bytes (LSB first)
46
+ ]);
47
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Finds a Data Descriptor in a ZIP archive buffer.
3
+ *
4
+ * The Data Descriptor is an optional 16-byte structure that appears at the end of a file's compressed data.
5
+ * It contains the compressed size of the file, and must be used when the Local File Header does not contain this information.
6
+ *
7
+ * @param buffer - The buffer containing the ZIP archive data.
8
+ * @param start - The starting offset in the buffer to search for the Data Descriptor.
9
+ * @returns - An object with `offset` and `compressedSize` properties.
10
+ * @throws {Error} - If the Data Descriptor is not found.
11
+ */
12
+ export function findDataDescriptor(buffer, start) {
13
+ const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
14
+ const DATA_DESCRIPTOR_TOTAL_LENGTH = 16;
15
+ const COMPRESSED_SIZE_OFFSET_FROM_SIGNATURE = 8;
16
+ for (let i = start; i <= buffer.length - DATA_DESCRIPTOR_TOTAL_LENGTH; i++) {
17
+ if (buffer.readUInt32LE(i) === DATA_DESCRIPTOR_SIGNATURE) {
18
+ const compressedSize = buffer.readUInt32LE(i + COMPRESSED_SIZE_OFFSET_FROM_SIGNATURE);
19
+ return {
20
+ compressedSize,
21
+ offset: i,
22
+ };
23
+ }
24
+ }
25
+ throw new Error("Data Descriptor not found");
26
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./crc-32.js";
2
+ export * from "./dos-time.js";
3
+ export * from "./find-data-descriptor.js";
4
+ export * from "./to-bytes.js";
@@ -0,0 +1,34 @@
1
+ import { Buffer } from "node:buffer";
2
+ /**
3
+ * Converts a numeric value into a fixed-length Buffer representation,
4
+ * storing the value in little-endian format with right-padding of zeros.
5
+ *
6
+ * This is particularly useful for binary protocols or file formats that
7
+ * require fixed-width numeric fields.
8
+ *
9
+ * @param {number} value - The numeric value to convert to bytes.
10
+ * Note: JavaScript numbers are IEEE 754 doubles, but only the
11
+ * integer portion will be used (up to 53-bit precision).
12
+ * @param {number} len - The desired length of the output Buffer in bytes.
13
+ * Must be a positive integer.
14
+ * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
15
+ * 1. The value's bytes in little-endian order (least significant byte first)
16
+ * 2. Zero padding in any remaining higher-order bytes
17
+ * @throws {RangeError} - If the value requires more bytes than `len` to represent
18
+ * (though this is currently not explicitly checked)
19
+ */
20
+ export function toBytes(value, len) {
21
+ // Allocate a new Buffer of the requested length, automatically zero-filled
22
+ const buf = Buffer.alloc(len);
23
+ // Process each byte position from least significant to most significant
24
+ for (let i = 0; i < len; i++) {
25
+ // Store the least significant byte of the current value
26
+ buf[i] = value & 0xff; // Mask to get bottom 8 bits
27
+ // Right-shift the value by 8 bits to process the next byte
28
+ // Note: This uses unsigned right shift (>>> would be signed)
29
+ value >>= 8;
30
+ // If the loop completes with value != 0, we've overflowed the buffer length,
31
+ // but this isn't currently checked/handled
32
+ }
33
+ return buf;
34
+ }
@@ -16,10 +16,10 @@
16
16
  */
17
17
  export declare function mergeSheetsToBaseFileProcess(data: {
18
18
  additions: {
19
- files: Record<string, string>;
19
+ files: Record<string, Buffer>;
20
20
  sheetIndexes: number[];
21
21
  }[];
22
- baseFiles: Record<string, string>;
22
+ baseFiles: Record<string, Buffer>;
23
23
  baseSheetIndex: number;
24
24
  gap: number;
25
25
  sheetNamesToRemove: string[];
@@ -4,4 +4,4 @@
4
4
  * @param {string} sheetName - The name of the sheet to remove.
5
5
  * @returns {void}
6
6
  */
7
- export declare function removeSheetByName(files: Record<string, string>, sheetName: string): void;
7
+ export declare function removeSheetByName(files: Record<string, Buffer>, sheetName: string): void;