stegdoc 5.7.0 → 6.1.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.
@@ -1,160 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const { parseMetadata } = require('./metadata');
4
- const { parseFilename } = require('./utils');
5
-
6
- /**
7
- * Extract content and metadata based on format
8
- * @param {object} readResult - Result from readFile
9
- * @param {string} format - File format ('xlsx' or 'docx')
10
- * @returns {object} { encryptedContent, encryptionMeta, metadata }
11
- */
12
- function extractContent(readResult, format) {
13
- // v5 log-embed format returns metadata already parsed
14
- if (readResult.formatVersion === 'v5') {
15
- return {
16
- encryptedContent: null, // v5 uses payloadBuffer instead
17
- encryptionMeta: readResult.encryptionMeta,
18
- metadata: readResult.metadata,
19
- payloadBuffer: readResult.payloadBuffer,
20
- };
21
- }
22
-
23
- if (format === 'xlsx') {
24
- return {
25
- encryptedContent: readResult.base64Content,
26
- encryptionMeta: readResult.encryptionMeta,
27
- metadata: parseMetadata(readResult.metadata),
28
- };
29
- } else {
30
- // DOCX: encryption meta is embedded in content with ||| separator
31
- const { base64Content, metadata } = readResult;
32
-
33
- // Check if this is a v2+ encrypted file
34
- if (base64Content.includes('|||')) {
35
- const [encryptionMeta, encryptedContent] = base64Content.split('|||');
36
- return {
37
- encryptedContent,
38
- encryptionMeta,
39
- metadata,
40
- };
41
- }
42
-
43
- // Legacy unencrypted DOCX
44
- return {
45
- encryptedContent: base64Content,
46
- encryptionMeta: null,
47
- metadata,
48
- };
49
- }
50
- }
51
-
52
- /**
53
- * Find all parts of a multi-part file in a directory
54
- * @param {string} dirPath - Directory to search
55
- * @param {string} hash - Original hash from metadata
56
- * @param {string} format - File format ('xlsx' or 'docx')
57
- * @param {number} [expectedParts] - Expected total parts (optional, for validation)
58
- * @returns {Array<{path: string, partNumber: number, filename: string}>} Array of parts sorted by part number
59
- */
60
- function findMultiPartFiles(dirPath, hash, format, expectedParts = null) {
61
- const files = fs.readdirSync(dirPath);
62
- const parts = [];
63
- const ext = format === 'docx' ? '.docx' : '.xlsx';
64
-
65
- // Also support legacy hex filenames for backward compatibility
66
- const legacyBaseHash = hash.length >= 16 ? hash.slice(0, 16) : hash;
67
-
68
- for (const file of files) {
69
- if (!file.toLowerCase().endsWith(ext)) continue;
70
-
71
- const parsed = parseFilename(file);
72
- if (!parsed || parsed.partNumber === null) continue;
73
-
74
- // Match by new realistic filename pattern
75
- // Check if file matches the expected pattern (same reportId from hash)
76
- if (parsed.reportId) {
77
- // New realistic format - match by reportId (last 4 chars of hash)
78
- // This is deterministic and doesn't depend on current date
79
- const expectedReportId = hash.slice(-4).toUpperCase();
80
- if (parsed.reportId === expectedReportId) {
81
- parts.push({
82
- path: path.join(dirPath, file),
83
- partNumber: parsed.partNumber,
84
- filename: file,
85
- dateStr: parsed.dateStr,
86
- timeStr: parsed.timeStr,
87
- });
88
- }
89
- } else if (parsed.baseHash === legacyBaseHash) {
90
- // Legacy hex format - match by base hash
91
- parts.push({
92
- path: path.join(dirPath, file),
93
- partNumber: parsed.partNumber,
94
- filename: file,
95
- });
96
- }
97
- }
98
-
99
- // Sort by part number
100
- parts.sort((a, b) => a.partNumber - b.partNumber);
101
-
102
- // For realistic filenames, ensure all parts have the same date/time pattern
103
- // This handles edge cases where multiple file sets might share the same reportId
104
- if (parts.length > 0 && parts[0].dateStr) {
105
- const refDateStr = parts[0].dateStr;
106
- const refTimeStr = parts[0].timeStr;
107
- const filteredParts = parts.filter(
108
- (p) => p.dateStr === refDateStr && p.timeStr === refTimeStr
109
- );
110
- // If filtering removed some parts, use the filtered set
111
- if (filteredParts.length !== parts.length) {
112
- parts.length = 0;
113
- parts.push(...filteredParts);
114
- }
115
- }
116
-
117
- // Validate sequential parts if expectedParts is provided
118
- if (expectedParts !== null && parts.length === expectedParts) {
119
- for (let i = 0; i < expectedParts; i++) {
120
- if (parts[i].partNumber !== i + 1) {
121
- throw new Error(`Missing part ${i + 1}. Parts must be sequential.`);
122
- }
123
- }
124
- }
125
-
126
- return parts;
127
- }
128
-
129
- /**
130
- * Check if a directory is writable
131
- * @param {string} dirPath - Directory path to check
132
- * @returns {boolean} True if writable
133
- */
134
- function isDirectoryWritable(dirPath) {
135
- try {
136
- if (!fs.existsSync(dirPath)) {
137
- fs.mkdirSync(dirPath, { recursive: true });
138
- }
139
- fs.accessSync(dirPath, fs.constants.W_OK);
140
- return true;
141
- } catch {
142
- return false;
143
- }
144
- }
145
-
146
- /**
147
- * Merge base64 chunks back into a single string
148
- * @param {Array<string>} chunks - Array of base64 chunks
149
- * @returns {string} Merged base64 string
150
- */
151
- function mergeBase64Chunks(chunks) {
152
- return chunks.join('');
153
- }
154
-
155
- module.exports = {
156
- extractContent,
157
- findMultiPartFiles,
158
- isDirectoryWritable,
159
- mergeBase64Chunks,
160
- };
@@ -1,115 +0,0 @@
1
- /**
2
- * Shared XML parsing utilities for DOCX and XLSX handlers
3
- * Handles namespace prefix variations (w:, ns0:, ns1:, etc.)
4
- */
5
-
6
- const AdmZip = require('adm-zip');
7
- const { XMLParser } = require('fast-xml-parser');
8
-
9
- // Reusable XML parser configured to strip namespace prefixes
10
- const xmlParser = new XMLParser({
11
- ignoreAttributes: false,
12
- attributeNamePrefix: '@_',
13
- removeNSPrefix: true, // Strips ns0:, ns1:, w:, etc.
14
- });
15
-
16
- /**
17
- * Parse XML string with namespace-agnostic parser
18
- * @param {string} xmlString - XML content to parse
19
- * @returns {object} Parsed XML as JavaScript object
20
- */
21
- function parseXml(xmlString) {
22
- return xmlParser.parse(xmlString);
23
- }
24
-
25
- /**
26
- * Read and parse an XML file from a ZIP archive (DOCX/XLSX)
27
- * @param {string|Buffer} zipPath - Path to ZIP file or Buffer
28
- * @param {string} entryPath - Path to XML file within ZIP
29
- * @returns {object|null} Parsed XML or null if entry not found
30
- */
31
- function parseXmlFromZip(zipPath, entryPath) {
32
- const zip = new AdmZip(zipPath);
33
- const entry = zip.getEntry(entryPath);
34
-
35
- if (!entry) {
36
- return null;
37
- }
38
-
39
- const xmlString = entry.getData().toString('utf8');
40
- return parseXml(xmlString);
41
- }
42
-
43
- /**
44
- * Get raw XML string from a ZIP archive
45
- * @param {string|Buffer} zipPath - Path to ZIP file or Buffer
46
- * @param {string} entryPath - Path to XML file within ZIP
47
- * @returns {string|null} XML string or null if entry not found
48
- */
49
- function getXmlStringFromZip(zipPath, entryPath) {
50
- const zip = new AdmZip(zipPath);
51
- const entry = zip.getEntry(entryPath);
52
-
53
- if (!entry) {
54
- return null;
55
- }
56
-
57
- return entry.getData().toString('utf8');
58
- }
59
-
60
- /**
61
- * List all entries in a ZIP file
62
- * @param {string|Buffer} zipPath - Path to ZIP file or Buffer
63
- * @returns {string[]} Array of entry paths
64
- */
65
- function listZipEntries(zipPath) {
66
- const zip = new AdmZip(zipPath);
67
- return zip.getEntries().map(e => e.entryName);
68
- }
69
-
70
- /**
71
- * Ensure value is an array (handles single item vs array in XML parsing)
72
- * @param {*} value - Value that might be an array or single item
73
- * @returns {Array} Always returns an array
74
- */
75
- function ensureArray(value) {
76
- if (value === undefined || value === null) return [];
77
- return Array.isArray(value) ? value : [value];
78
- }
79
-
80
- /**
81
- * Safely get nested property from object
82
- * @param {object} obj - Object to traverse
83
- * @param {string} path - Dot-separated path (e.g., 'worksheet.sheetData.row')
84
- * @returns {*} Value at path or undefined
85
- */
86
- function getNestedValue(obj, path) {
87
- return path.split('.').reduce((current, key) => {
88
- return current && current[key] !== undefined ? current[key] : undefined;
89
- }, obj);
90
- }
91
-
92
- /**
93
- * Extract text content from a parsed XML text node
94
- * Handles both simple strings and objects with #text
95
- * @param {*} textNode - Text node from parsed XML
96
- * @returns {string} Extracted text
97
- */
98
- function extractTextContent(textNode) {
99
- if (typeof textNode === 'string') return textNode;
100
- if (typeof textNode === 'number') return String(textNode);
101
- if (textNode && typeof textNode === 'object') {
102
- if (textNode['#text'] !== undefined) return String(textNode['#text']);
103
- }
104
- return '';
105
- }
106
-
107
- module.exports = {
108
- parseXml,
109
- parseXmlFromZip,
110
- getXmlStringFromZip,
111
- listZipEntries,
112
- ensureArray,
113
- getNestedValue,
114
- extractTextContent,
115
- };