fix(fs): Fix file existence check in waitForFileToBeReady method.
This commit is contained in:
parent
207320ff26
commit
dbddf2a8ba
@ -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
|
||||
|
||||
|
@ -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.'
|
||||
}
|
||||
|
24
ts/fs.ts
24
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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user