@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
@@ -9,8 +9,8 @@
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 declare function buildMergedSheet(originalXml: string, mergedRows: string[], mergeCells?: {
15
15
  ref: string;
16
- }[]): string;
16
+ }[]): Buffer;
@@ -24,4 +24,5 @@ export declare function extractRowsFromSheet(sheet: Buffer | string): {
24
24
  mergeCells: {
25
25
  ref: string;
26
26
  }[];
27
+ xml: string;
27
28
  };
@@ -2,7 +2,7 @@
2
2
  * Parses a ZIP archive from a buffer and extracts the files within.
3
3
  *
4
4
  * @param {Buffer} buffer - The buffer containing the ZIP archive data.
5
- * @returns {Object.<string, string>} - An object where keys are file names and values are file contents.
5
+ * @returns {Object.<string, Buffer>} - An object where keys are file names and values are file contents as Buffers.
6
6
  * @throws {Error} - Throws an error if an unsupported compression method is encountered or if decompression fails.
7
7
  */
8
- export declare function readSync(buffer: Buffer): Record<string, string>;
8
+ export declare function readSync(buffer: Buffer): Record<string, Buffer>;
@@ -2,7 +2,7 @@
2
2
  * Parses a ZIP archive from a buffer and extracts the files within.
3
3
  *
4
4
  * @param {Buffer} buffer - The buffer containing the ZIP archive data.
5
- * @returns {Object.<string, string>} - An object where keys are file names and values are file contents.
5
+ * @returns {Object.<string, Buffer>} - An object where keys are file names and values are file contents as Buffers.
6
6
  * @throws {Error} - Throws an error if an unsupported compression method is encountered or if decompression fails.
7
7
  */
8
- export declare function read(buffer: Buffer): Promise<Record<string, string>>;
8
+ export declare function read(buffer: Buffer): Promise<Record<string, Buffer>>;
@@ -0,0 +1,15 @@
1
+ import { Buffer } from "node:buffer";
2
+ /**
3
+ * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
4
+ * This implementation uses a precomputed lookup table for optimal performance.
5
+ *
6
+ * The algorithm follows these characteristics:
7
+ * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
8
+ * - Initial value: 0xFFFFFFFF (inverted by ~0)
9
+ * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
10
+ * - Input and output reflection: Yes
11
+ *
12
+ * @param {Buffer} buf - The input buffer to calculate checksum for
13
+ * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
14
+ */
15
+ export declare function crc32(buf: Buffer): number;
@@ -0,0 +1,25 @@
1
+ import { Buffer } from "node:buffer";
2
+ /**
3
+ * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
4
+ * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
5
+ *
6
+ * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
7
+ * the following bit layout:
8
+ *
9
+ * Time portion (2 bytes/16 bits):
10
+ * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
11
+ * - Bits 05-10: Minutes (0-59)
12
+ * - Bits 11-15: Hours (0-23)
13
+ *
14
+ * Date portion (2 bytes/16 bits):
15
+ * - Bits 00-04: Day (1-31)
16
+ * - Bits 05-08: Month (1-12)
17
+ * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
18
+ *
19
+ * @param {Date} date - The JavaScript Date object to convert
20
+ * @returns {Buffer} - 4-byte Buffer containing:
21
+ * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
22
+ * - Bytes 2-3: DOS date (year-1980, month, day)
23
+ * @throws {RangeError} - If the date is before 1980 or after 2107
24
+ */
25
+ export declare function dosTime(date: Date): Buffer;
@@ -0,0 +1,15 @@
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 declare function findDataDescriptor(buffer: Buffer, start: number): {
13
+ offset: number;
14
+ compressedSize: number;
15
+ };
@@ -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,20 @@
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 declare function toBytes(value: number, len: number): Buffer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-ak/excel-toolbox",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "excel-toolbox",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -1,157 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.crc32 = crc32;
4
- exports.dosTime = dosTime;
5
- exports.toBytes = toBytes;
6
- const node_buffer_1 = require("node:buffer");
7
- /**
8
- * Precomputed CRC-32 lookup table for optimized checksum calculation.
9
- * The table is generated using the standard IEEE 802.3 (Ethernet) polynomial:
10
- * 0xEDB88320 (reversed representation of 0x04C11DB7).
11
- *
12
- * The table is immediately invoked and cached as a constant for performance,
13
- * following the common implementation pattern for CRC algorithms.
14
- */
15
- const crcTable = (() => {
16
- // Create a typed array for better performance with 256 32-bit unsigned integers
17
- const table = new Uint32Array(256);
18
- // Generate table entries for all possible byte values (0-255)
19
- for (let i = 0; i < 256; i++) {
20
- let crc = i; // Initialize with current byte value
21
- // Process each bit (8 times)
22
- for (let j = 0; j < 8; j++) {
23
- /*
24
- * CRC division algorithm:
25
- * 1. If LSB is set (crc & 1), XOR with polynomial
26
- * 2. Right-shift by 1 (unsigned)
27
- *
28
- * The polynomial 0xEDB88320 is:
29
- * - Bit-reversed version of 0x04C11DB7
30
- * - Uses reflected input/output algorithm
31
- */
32
- crc = crc & 1
33
- ? 0xedb88320 ^ (crc >>> 1) // XOR with polynomial if LSB is set
34
- : crc >>> 1; // Just shift right if LSB is not set
35
- }
36
- // Store final 32-bit value (>>> 0 ensures unsigned 32-bit representation)
37
- table[i] = crc >>> 0;
38
- }
39
- return table;
40
- })();
41
- /**
42
- * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
43
- * This implementation uses a precomputed lookup table for optimal performance.
44
- *
45
- * The algorithm follows these characteristics:
46
- * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
47
- * - Initial value: 0xFFFFFFFF (inverted by ~0)
48
- * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
49
- * - Input and output reflection: Yes
50
- *
51
- * @param {Buffer} buf - The input buffer to calculate checksum for
52
- * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
53
- */
54
- function crc32(buf) {
55
- // Initialize CRC with all 1's (0xFFFFFFFF) using bitwise NOT
56
- let crc = ~0;
57
- // Process each byte in the buffer
58
- for (let i = 0; i < buf.length; i++) {
59
- /*
60
- * CRC update algorithm steps:
61
- * 1. XOR current CRC with next byte (lowest 8 bits)
62
- * 2. Use result as index in precomputed table (0-255)
63
- * 3. XOR the table value with right-shifted CRC (8 bits)
64
- *
65
- * The operation breakdown:
66
- * - (crc ^ buf[i]) - XOR with next byte
67
- * - & 0xff - Isolate lowest 8 bits
68
- * - crc >>> 8 - Shift CRC right by 8 bits (unsigned)
69
- * - ^ crcTable[...] - XOR with precomputed table value
70
- */
71
- crc = (crc >>> 8) ^ crcTable[(crc ^ buf[i]) & 0xff];
72
- }
73
- /*
74
- * Final processing:
75
- * 1. Invert all bits (~crc) to match standard CRC-32 output
76
- * 2. Convert to unsigned 32-bit integer (>>> 0)
77
- */
78
- return ~crc >>> 0;
79
- }
80
- /**
81
- * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
82
- * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
83
- *
84
- * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
85
- * the following bit layout:
86
- *
87
- * Time portion (2 bytes/16 bits):
88
- * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
89
- * - Bits 05-10: Minutes (0-59)
90
- * - Bits 11-15: Hours (0-23)
91
- *
92
- * Date portion (2 bytes/16 bits):
93
- * - Bits 00-04: Day (1-31)
94
- * - Bits 05-08: Month (1-12)
95
- * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
96
- *
97
- * @param {Date} date - The JavaScript Date object to convert
98
- * @returns {Buffer} - 4-byte Buffer containing:
99
- * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
100
- * - Bytes 2-3: DOS date (year-1980, month, day)
101
- * @throws {RangeError} - If the date is before 1980 or after 2107
102
- */
103
- function dosTime(date) {
104
- // Pack time components into 2 bytes (16 bits):
105
- // - Hours (5 bits) shifted left 11 positions (bits 11-15)
106
- // - Minutes (6 bits) shifted left 5 positions (bits 5-10)
107
- // - Seconds/2 (5 bits) in least significant bits (bits 0-4)
108
- const time = (date.getHours() << 11) | // Hours occupy bits 11-15
109
- (date.getMinutes() << 5) | // Minutes occupy bits 5-10
110
- (Math.floor(date.getSeconds() / 2)); // Seconds/2 occupy bits 0-4
111
- // Pack date components into 2 bytes (16 bits):
112
- // - (Year-1980) (7 bits) shifted left 9 positions (bits 9-15)
113
- // - Month (4 bits) shifted left 5 positions (bits 5-8)
114
- // - Day (5 bits) in least significant bits (bits 0-4)
115
- const day = ((date.getFullYear() - 1980) << 9) | // Years since 1980 (bits 9-15)
116
- ((date.getMonth() + 1) << 5) | // Month 1-12 (bits 5-8)
117
- date.getDate(); // Day 1-31 (bits 0-4)
118
- // Combine both 2-byte values into a single 4-byte Buffer
119
- // Note: Using little-endian byte order for each 2-byte segment
120
- return node_buffer_1.Buffer.from([
121
- ...toBytes(time, 2), // Convert time to 2 bytes (LSB first)
122
- ...toBytes(day, 2), // Convert date to 2 bytes (LSB first)
123
- ]);
124
- }
125
- /**
126
- * Converts a numeric value into a fixed-length Buffer representation,
127
- * storing the value in little-endian format with right-padding of zeros.
128
- *
129
- * This is particularly useful for binary protocols or file formats that
130
- * require fixed-width numeric fields.
131
- *
132
- * @param {number} value - The numeric value to convert to bytes.
133
- * Note: JavaScript numbers are IEEE 754 doubles, but only the
134
- * integer portion will be used (up to 53-bit precision).
135
- * @param {number} len - The desired length of the output Buffer in bytes.
136
- * Must be a positive integer.
137
- * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
138
- * 1. The value's bytes in little-endian order (least significant byte first)
139
- * 2. Zero padding in any remaining higher-order bytes
140
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
141
- * (though this is currently not explicitly checked)
142
- */
143
- function toBytes(value, len) {
144
- // Allocate a new Buffer of the requested length, automatically zero-filled
145
- const buf = node_buffer_1.Buffer.alloc(len);
146
- // Process each byte position from least significant to most significant
147
- for (let i = 0; i < len; i++) {
148
- // Store the least significant byte of the current value
149
- buf[i] = value & 0xff; // Mask to get bottom 8 bits
150
- // Right-shift the value by 8 bits to process the next byte
151
- // Note: This uses unsigned right shift (>>> would be signed)
152
- value >>= 8;
153
- // If the loop completes with value != 0, we've overflowed the buffer length,
154
- // but this isn't currently checked/handled
155
- }
156
- return buf;
157
- }
@@ -1,152 +0,0 @@
1
- import { Buffer } from "node:buffer";
2
- /**
3
- * Precomputed CRC-32 lookup table for optimized checksum calculation.
4
- * The table is generated using the standard IEEE 802.3 (Ethernet) polynomial:
5
- * 0xEDB88320 (reversed representation of 0x04C11DB7).
6
- *
7
- * The table is immediately invoked and cached as a constant for performance,
8
- * following the common implementation pattern for CRC algorithms.
9
- */
10
- const crcTable = (() => {
11
- // Create a typed array for better performance with 256 32-bit unsigned integers
12
- const table = new Uint32Array(256);
13
- // Generate table entries for all possible byte values (0-255)
14
- for (let i = 0; i < 256; i++) {
15
- let crc = i; // Initialize with current byte value
16
- // Process each bit (8 times)
17
- for (let j = 0; j < 8; j++) {
18
- /*
19
- * CRC division algorithm:
20
- * 1. If LSB is set (crc & 1), XOR with polynomial
21
- * 2. Right-shift by 1 (unsigned)
22
- *
23
- * The polynomial 0xEDB88320 is:
24
- * - Bit-reversed version of 0x04C11DB7
25
- * - Uses reflected input/output algorithm
26
- */
27
- crc = crc & 1
28
- ? 0xedb88320 ^ (crc >>> 1) // XOR with polynomial if LSB is set
29
- : crc >>> 1; // Just shift right if LSB is not set
30
- }
31
- // Store final 32-bit value (>>> 0 ensures unsigned 32-bit representation)
32
- table[i] = crc >>> 0;
33
- }
34
- return table;
35
- })();
36
- /**
37
- * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
38
- * This implementation uses a precomputed lookup table for optimal performance.
39
- *
40
- * The algorithm follows these characteristics:
41
- * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
42
- * - Initial value: 0xFFFFFFFF (inverted by ~0)
43
- * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
44
- * - Input and output reflection: Yes
45
- *
46
- * @param {Buffer} buf - The input buffer to calculate checksum for
47
- * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
48
- */
49
- export function crc32(buf) {
50
- // Initialize CRC with all 1's (0xFFFFFFFF) using bitwise NOT
51
- let crc = ~0;
52
- // Process each byte in the buffer
53
- for (let i = 0; i < buf.length; i++) {
54
- /*
55
- * CRC update algorithm steps:
56
- * 1. XOR current CRC with next byte (lowest 8 bits)
57
- * 2. Use result as index in precomputed table (0-255)
58
- * 3. XOR the table value with right-shifted CRC (8 bits)
59
- *
60
- * The operation breakdown:
61
- * - (crc ^ buf[i]) - XOR with next byte
62
- * - & 0xff - Isolate lowest 8 bits
63
- * - crc >>> 8 - Shift CRC right by 8 bits (unsigned)
64
- * - ^ crcTable[...] - XOR with precomputed table value
65
- */
66
- crc = (crc >>> 8) ^ crcTable[(crc ^ buf[i]) & 0xff];
67
- }
68
- /*
69
- * Final processing:
70
- * 1. Invert all bits (~crc) to match standard CRC-32 output
71
- * 2. Convert to unsigned 32-bit integer (>>> 0)
72
- */
73
- return ~crc >>> 0;
74
- }
75
- /**
76
- * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
77
- * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
78
- *
79
- * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
80
- * the following bit layout:
81
- *
82
- * Time portion (2 bytes/16 bits):
83
- * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
84
- * - Bits 05-10: Minutes (0-59)
85
- * - Bits 11-15: Hours (0-23)
86
- *
87
- * Date portion (2 bytes/16 bits):
88
- * - Bits 00-04: Day (1-31)
89
- * - Bits 05-08: Month (1-12)
90
- * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
91
- *
92
- * @param {Date} date - The JavaScript Date object to convert
93
- * @returns {Buffer} - 4-byte Buffer containing:
94
- * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
95
- * - Bytes 2-3: DOS date (year-1980, month, day)
96
- * @throws {RangeError} - If the date is before 1980 or after 2107
97
- */
98
- export function dosTime(date) {
99
- // Pack time components into 2 bytes (16 bits):
100
- // - Hours (5 bits) shifted left 11 positions (bits 11-15)
101
- // - Minutes (6 bits) shifted left 5 positions (bits 5-10)
102
- // - Seconds/2 (5 bits) in least significant bits (bits 0-4)
103
- const time = (date.getHours() << 11) | // Hours occupy bits 11-15
104
- (date.getMinutes() << 5) | // Minutes occupy bits 5-10
105
- (Math.floor(date.getSeconds() / 2)); // Seconds/2 occupy bits 0-4
106
- // Pack date components into 2 bytes (16 bits):
107
- // - (Year-1980) (7 bits) shifted left 9 positions (bits 9-15)
108
- // - Month (4 bits) shifted left 5 positions (bits 5-8)
109
- // - Day (5 bits) in least significant bits (bits 0-4)
110
- const day = ((date.getFullYear() - 1980) << 9) | // Years since 1980 (bits 9-15)
111
- ((date.getMonth() + 1) << 5) | // Month 1-12 (bits 5-8)
112
- date.getDate(); // Day 1-31 (bits 0-4)
113
- // Combine both 2-byte values into a single 4-byte Buffer
114
- // Note: Using little-endian byte order for each 2-byte segment
115
- return Buffer.from([
116
- ...toBytes(time, 2), // Convert time to 2 bytes (LSB first)
117
- ...toBytes(day, 2), // Convert date to 2 bytes (LSB first)
118
- ]);
119
- }
120
- /**
121
- * Converts a numeric value into a fixed-length Buffer representation,
122
- * storing the value in little-endian format with right-padding of zeros.
123
- *
124
- * This is particularly useful for binary protocols or file formats that
125
- * require fixed-width numeric fields.
126
- *
127
- * @param {number} value - The numeric value to convert to bytes.
128
- * Note: JavaScript numbers are IEEE 754 doubles, but only the
129
- * integer portion will be used (up to 53-bit precision).
130
- * @param {number} len - The desired length of the output Buffer in bytes.
131
- * Must be a positive integer.
132
- * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
133
- * 1. The value's bytes in little-endian order (least significant byte first)
134
- * 2. Zero padding in any remaining higher-order bytes
135
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
136
- * (though this is currently not explicitly checked)
137
- */
138
- export function toBytes(value, len) {
139
- // Allocate a new Buffer of the requested length, automatically zero-filled
140
- const buf = Buffer.alloc(len);
141
- // Process each byte position from least significant to most significant
142
- for (let i = 0; i < len; i++) {
143
- // Store the least significant byte of the current value
144
- buf[i] = value & 0xff; // Mask to get bottom 8 bits
145
- // Right-shift the value by 8 bits to process the next byte
146
- // Note: This uses unsigned right shift (>>> would be signed)
147
- value >>= 8;
148
- // If the loop completes with value != 0, we've overflowed the buffer length,
149
- // but this isn't currently checked/handled
150
- }
151
- return buf;
152
- }
@@ -1,58 +0,0 @@
1
- import { Buffer } from "node:buffer";
2
- /**
3
- * Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
4
- * This implementation uses a precomputed lookup table for optimal performance.
5
- *
6
- * The algorithm follows these characteristics:
7
- * - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
8
- * - Initial value: 0xFFFFFFFF (inverted by ~0)
9
- * - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
10
- * - Input and output reflection: Yes
11
- *
12
- * @param {Buffer} buf - The input buffer to calculate checksum for
13
- * @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
14
- */
15
- export declare function crc32(buf: Buffer): number;
16
- /**
17
- * Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
18
- * as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
19
- *
20
- * The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
21
- * the following bit layout:
22
- *
23
- * Time portion (2 bytes/16 bits):
24
- * - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
25
- * - Bits 05-10: Minutes (0-59)
26
- * - Bits 11-15: Hours (0-23)
27
- *
28
- * Date portion (2 bytes/16 bits):
29
- * - Bits 00-04: Day (1-31)
30
- * - Bits 05-08: Month (1-12)
31
- * - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
32
- *
33
- * @param {Date} date - The JavaScript Date object to convert
34
- * @returns {Buffer} - 4-byte Buffer containing:
35
- * - Bytes 0-1: DOS time (hours, minutes, seconds/2)
36
- * - Bytes 2-3: DOS date (year-1980, month, day)
37
- * @throws {RangeError} - If the date is before 1980 or after 2107
38
- */
39
- export declare function dosTime(date: Date): Buffer;
40
- /**
41
- * Converts a numeric value into a fixed-length Buffer representation,
42
- * storing the value in little-endian format with right-padding of zeros.
43
- *
44
- * This is particularly useful for binary protocols or file formats that
45
- * require fixed-width numeric fields.
46
- *
47
- * @param {number} value - The numeric value to convert to bytes.
48
- * Note: JavaScript numbers are IEEE 754 doubles, but only the
49
- * integer portion will be used (up to 53-bit precision).
50
- * @param {number} len - The desired length of the output Buffer in bytes.
51
- * Must be a positive integer.
52
- * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
53
- * 1. The value's bytes in little-endian order (least significant byte first)
54
- * 2. Zero padding in any remaining higher-order bytes
55
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
56
- * (though this is currently not explicitly checked)
57
- */
58
- export declare function toBytes(value: number, len: number): Buffer;