fix(fs): Fix file existence check in waitForFileToBeReady method.

This commit is contained in:
Philipp Kunz 2025-01-07 04:41:17 +01:00
parent 207320ff26
commit dbddf2a8ba
3 changed files with 22 additions and 11 deletions

View File

@ -1,5 +1,12 @@
# Changelog # Changelog
## 2025-01-07 - 11.1.4 - fix(fs)
Fix file existence check in waitForFileToBeReady method.
- Ensured that the directory and file exist before setting up the watcher in waitForFileToBeReady.
- Changed ensureDirectoryExists to ensureFileExists for correct file path verification.
- Handled ENOENT errors correctly to retry file existence checks until timeout is reached.
## 2025-01-07 - 11.1.3 - fix(fs) ## 2025-01-07 - 11.1.3 - fix(fs)
Fix TypeScript type issue in fs module Fix TypeScript type issue in fs module

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.1.3', version: '11.1.4',
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

@ -397,6 +397,7 @@ export const listFileTree = async (
/** /**
* Watches for file stability before resolving the promise. * Watches for file stability before resolving the promise.
* Ensures that the directory and file exist before setting up the watcher.
* @param filePathArg The path of the file to monitor. * @param filePathArg The path of the file to monitor.
* @param timeoutMs The maximum time to wait for the file to stabilize (in milliseconds). Default is 60 seconds. * @param timeoutMs The maximum time to wait for the file to stabilize (in milliseconds). Default is 60 seconds.
* @returns A promise that resolves when the file is stable or rejects on timeout or error. * @returns A promise that resolves when the file is stable or rejects on timeout or error.
@ -412,17 +413,20 @@ export const waitForFileToBeReady = (
const startTime = Date.now(); const startTime = Date.now();
const fileDir = plugins.path.dirname(filePathArg); const fileDir = plugins.path.dirname(filePathArg);
const ensureDirectoryExists = async () => { const ensureFileExists = async () => {
while (true) { while (true) {
try { try {
// Check if the directory exists // Check if the file exists
await plugins.smartpromise.fromCallback((cb) => await plugins.smartpromise.fromCallback((cb) =>
plugins.fs.access(fileDir, plugins.fs.constants.R_OK, cb) plugins.fs.access(filePathArg, plugins.fs.constants.F_OK, cb)
); );
break; // Exit the loop if the directory exists break; // Exit the loop if the file exists
} catch (err) { } catch (err: any) {
if (err.code !== 'ENOENT') {
throw err; // Propagate unexpected errors
}
if (Date.now() - startTime > timeoutMs) { if (Date.now() - startTime > timeoutMs) {
reject(new Error(`Timeout waiting for directory to exist: ${fileDir}`)); reject(new Error(`Timeout waiting for file to exist: ${filePathArg}`));
return; return;
} }
// Wait and retry // Wait and retry
@ -442,15 +446,15 @@ export const waitForFileToBeReady = (
lastFileSize = stats.size; lastFileSize = stats.size;
fileIsStable = false; fileIsStable = false;
} }
} catch (err) { } catch (err: any) {
if (err.code !== 'ENOENT') { if (err.code !== 'ENOENT') {
throw err; // Only ignore ENOENT (file not found) errors throw err; // Only ignore ENOENT (file not found) errors
} }
} }
}; };
// Ensure the directory exists before setting up the watcher // Ensure the file exists before setting up the watcher
await ensureDirectoryExists(); await ensureFileExists();
const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => { const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => {
if (!fileIsStable) { if (!fileIsStable) {
@ -468,7 +472,7 @@ export const waitForFileToBeReady = (
// Check for timeout // Check for timeout
if (Date.now() - startTime > timeoutMs) { if (Date.now() - startTime > timeoutMs) {
watcher.close(); watcher.close();
reject(new Error(`Timeout waiting for file to be ready: ${filePathArg}`)); reject(new Error(`Timeout waiting for file to stabilize: ${filePathArg}`));
return; return;
} }