fix(fs): Fix issues in file stability check and directory existence verification in fs module

This commit is contained in:
Philipp Kunz 2025-01-07 04:35:56 +01:00
parent 7c07c5c174
commit a9799e05ee
3 changed files with 35 additions and 27 deletions

View File

@ -1,5 +1,12 @@
# Changelog # Changelog
## 2025-01-07 - 11.1.2 - fix(fs)
Fix issues in file stability check and directory existence verification in fs module
- Removed unused variable 'isFileAvailable' in 'waitForFileToBeReady'.
- Fixed logic for ensuring the target directory exists before setting up file stability watcher.
- Refactored directory existence logic into 'ensureDirectoryExists' function.
## 2025-01-07 - 11.1.1 - fix(fs) ## 2025-01-07 - 11.1.1 - fix(fs)
Improve waitForFileToBeReady function for file stability detection Improve waitForFileToBeReady function for file stability detection

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.1.1', version: '11.1.2',
description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.' description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.'
} }

View File

@ -205,7 +205,7 @@ export const toObjectSync = (filePathArg, fileTypeArg?) => {
} catch (err) { } catch (err) {
err.message = `Failed to read file at ${filePathArg}` + err.message; err.message = `Failed to read file at ${filePathArg}` + err.message;
throw err; throw err;
}; }
}; };
/** /**
@ -408,17 +408,34 @@ export const waitForFileToBeReady = (
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let lastFileSize = -1; let lastFileSize = -1;
let fileIsStable = false; let fileIsStable = false;
let isFileAvailable = false;
const startTime = Date.now(); const startTime = Date.now();
const fileDir = plugins.path.dirname(filePathArg); const fileDir = plugins.path.dirname(filePathArg);
const ensureDirectoryExists = async () => {
while (true) {
try {
// Check if the directory exists
await plugins.smartpromise.fromCallback((cb) =>
plugins.fs.access(fileDir, plugins.fs.constants.R_OK, cb)
);
break; // Exit the loop if the directory exists
} catch (err) {
if (Date.now() - startTime > timeoutMs) {
reject(new Error(`Timeout waiting for directory to exist: ${fileDir}`));
return;
}
// Wait and retry
await plugins.smartdelay.delayFor(500);
}
}
};
const checkFileStability = async () => { const checkFileStability = async () => {
try { try {
const stats: any = await plugins.smartpromise.fromCallback((cb) => const stats = await plugins.smartpromise.fromCallback((cb) =>
plugins.fs.stat(filePathArg, cb) plugins.fs.stat(filePathArg, cb)
); );
isFileAvailable = true;
if (stats.size === lastFileSize) { if (stats.size === lastFileSize) {
fileIsStable = true; fileIsStable = true;
} else { } else {
@ -426,27 +443,17 @@ export const waitForFileToBeReady = (
fileIsStable = false; fileIsStable = false;
} }
} catch (err) { } catch (err) {
if (err.code === 'ENOENT') { if (err.code !== 'ENOENT') {
isFileAvailable = false; // File not available yet throw err; // Only ignore ENOENT (file not found) errors
} else {
throw err; // Propagate other errors
} }
} }
}; };
const checkDirectory = async () => { // Ensure the directory exists before setting up the watcher
try { await ensureDirectoryExists();
await plugins.smartpromise.fromCallback((cb) =>
plugins.fs.access(fileDir, plugins.fs.constants.R_OK, cb)
);
return true;
} catch {
return false;
}
};
const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => { const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => {
if (isFileAvailable && !fileIsStable) { if (!fileIsStable) {
await checkFileStability(); await checkFileStability();
} }
}); });
@ -465,12 +472,6 @@ export const waitForFileToBeReady = (
return; return;
} }
// Ensure directory exists
if (!await checkDirectory()) {
await plugins.smartdelay.delayFor(500); // Wait and retry
continue;
}
// Check file stability // Check file stability
await checkFileStability(); await checkFileStability();
if (!fileIsStable) { if (!fileIsStable) {
@ -534,4 +535,4 @@ export let toFs = async (
export const stat = async (filePathArg: string) => { export const stat = async (filePathArg: string) => {
return plugins.fsPromises.stat(filePathArg); return plugins.fsPromises.stat(filePathArg);
} };