From dbddf2a8ba4388e6f9147ebdb964791c951229e6 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 7 Jan 2025 04:41:17 +0100 Subject: [PATCH] fix(fs): Fix file existence check in waitForFileToBeReady method. --- changelog.md | 7 +++++++ ts/00_commitinfo_data.ts | 2 +- ts/fs.ts | 24 ++++++++++++++---------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index 110d18c..996f65a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # 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) Fix TypeScript type issue in fs module diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 4bd6217..af860ce 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { 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.' } diff --git a/ts/fs.ts b/ts/fs.ts index 3cd6ef1..a1e18b7 100644 --- a/ts/fs.ts +++ b/ts/fs.ts @@ -397,6 +397,7 @@ export const listFileTree = async ( /** * 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 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. @@ -412,17 +413,20 @@ export const waitForFileToBeReady = ( const startTime = Date.now(); const fileDir = plugins.path.dirname(filePathArg); - const ensureDirectoryExists = async () => { + const ensureFileExists = async () => { while (true) { try { - // Check if the directory exists + // Check if the file exists 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 - } catch (err) { + break; // Exit the loop if the file exists + } catch (err: any) { + if (err.code !== 'ENOENT') { + throw err; // Propagate unexpected errors + } 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; } // Wait and retry @@ -442,15 +446,15 @@ export const waitForFileToBeReady = ( lastFileSize = stats.size; fileIsStable = false; } - } catch (err) { + } catch (err: any) { if (err.code !== 'ENOENT') { throw err; // Only ignore ENOENT (file not found) errors } } }; - // Ensure the directory exists before setting up the watcher - await ensureDirectoryExists(); + // Ensure the file exists before setting up the watcher + await ensureFileExists(); const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => { if (!fileIsStable) { @@ -468,7 +472,7 @@ export const waitForFileToBeReady = ( // Check for timeout if (Date.now() - startTime > timeoutMs) { watcher.close(); - reject(new Error(`Timeout waiting for file to be ready: ${filePathArg}`)); + reject(new Error(`Timeout waiting for file to stabilize: ${filePathArg}`)); return; }