Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
fs-promise-util
Advanced tools
A utility library for file system interaction. All methods return a promise.
This library utilizes the graceful-fs, an improvement over the fs module.
Node.js
: >= v4.x
Platform
: Darwin
, Unix
or Linux
(Windows is not supported at this time)npm install fs-promise-util
This module exposes the following methods:
fs-promise-util.appendFile appends data to a file, creating the file if it does not exist. It returns a promise for fs.appendFile.
file
- [ string | Buffer | number ] - filename or file descriptor
data
- [ string | Buffer ]
options
- [ Object | string ]
encoding
- [ string | null ] - default = utf8
mode
- [ integer ] - default = 0o666flag
- [ string ] - default = a
import fs from 'fs-promise-util';
export async function saveMessage (message = '') {
return await fs
.appendFile(
'/path/to/messages.log',
message,
{
encoding : 'utf8'
}
).catch((err) => {
console.error(err);
});
}
fs-promise-util.createReadStream allows you to open up a readable stream. All you have to do is pass the path of the file to start streaming in.
path
- [ string | Buffer ]options
- [ string | Object ]
flags
- [ string ]encoding
- [ string ]fd
- [ integer ]mode
- [ integer ]autoClose
- [ boolean ]start
- [ integer ]end
- [ integer]import fs from 'fs-promise-util';
export async function getContent () {
return await new Promise((resolve, reject) => {
let
chunks = [],
reader = fs
.createReadStream(
'/path/to/messages.log',
{
encoding : 'utf8'
}
);
// capture events
reader.on('data', (chunk) => chunks.push(chunk));
reader.on('end', () => resolve(chunks.join('')));
reader.on('error', reject);
});
}
options
is an object or string with the following defaults:
{
flags: 'r',
encoding: null,
fd: null,
mode: 0o666,
autoClose: true
}
fs-promise-util.createWriteStream creates a writable stream. After a call to fs-promise-util.createWriteStream with the filepath, you have a writeable stream to work with.
path
- [ string | Buffer ]options
- [ string | Object ]
flags
- [ string ]defaultEncoding
- [ string ]fd
- [ integer ]mode
- [ integer ]autoClose
- [ boolean ]start
- [ integer ]options
is an object or string with the following defaults:
{
flags: 'w',
defaultEncoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
}
import fs from 'fs-promise-util';
export async function writeContent () {
return await new Promise((resolve, reject) => {
let writer = fs
.createWriteStream(
'/path/to/messages.log',
{
encoding : 'utf8'
}
);
// capture events
writer.on('error', reject);
writer.on('finish', resolve);
// write data
writer.end(data);
});
}
fs-promise-util.ensurePath creates a given path and returns a promise. It takes in a string value which is the directory path.
directoryPath
- [ string ]import fs from 'fs-promise-util';
export async function writeContent () {
return await fs
.ensurePath(
'/path/to/messages'
).then((path) => {
console.info('directory created');
return Promise.resolve(path);
}).catch((err) => {
console.error(err);
});
}
fs-promise-util.exists takes in a path as an argument. It checks whether a given path exists in the file system and resolves to a true or a false.
import fs from 'fs-promise-util';
export async function checkIfExists () {
let exists = await fs
.exists(
'/path/to/messages.log'
);
return exists;
}
fs-promise-util.lstat returns a promise for lstat.
import fs from 'fs-promise-util';
export async function getStatus () {
return fs
.lstat(
'/path/to/messages.log'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.prune removes 'x' number of least recent files matching a given pattern from a directory.
directoryPath
- [ string ] - directory to remove the filesfilter
- [ string ] - pattern for the file removal, i.e. a regular expression matching a file nameretainCount
- [ number ] - number of files you want to keep in the directoryimport fs from 'fs-promise-util';
export async function removeFiles () {
return await fs
.prune(
'/path/to/messages',
new RegExp('\\w+'),
'number of files to keep'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.readdir reads the contents of a directory and returns a promise.
path
- [ string | Buffer ]options
- [ string | Object ]
encoding
- [ string ] - default = utf8
import fs from 'fs-promise-util';
export async function getFiles () {
return await fs
.readdir(
'/path/to/messages directory',
{
encoding : 'utf8'
}
).catch((err) => {
console.error(err);
});
}
fs-promise-util.readlink returns the absolute path of a file or folder pointed by a symlink as a promise. If the path is not a symlink or shortcut, it will resolve to an empty string.
path
- [ string | Buffer ]options
- [ string | Object ]
encoding
- [ string ] - default = utf8
import fs from 'fs-promise-util';
export async function getPath () {
return await fs
.readlink(
'/path/to/messages.log',
'utf8'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.readAndSort reads the content of the directory passed and sorts files based on date and returns files. 'options' object can be used to pass in:
options.sort
- [ string ] - sort files based on dateoptions.filter
- [ string ] - any filters passed with the file name (options.filter.name
)import fs from 'fs-promise-util';
export async function sortFiles () {
let exists = await fs
.readAndSort(
'/path/to/messages directory',
{
filter : {
name : new RegExp('\\w+')
}
}
).catch((err) => {
console.error(err);
});
}
fs-promise-util.readFile reads the entire contents of a file asynchronously and returns a promise.
file
- [string | Buffer | integer ] - filename or file descriptoroptions
- [ Object | string ]
encoding
- [ string | null ] - default = nullflag
- [ string ] - default = r
import fs from 'fs-promise-util';
export async function getFileContent () {
return await fs
.readFile(
'/path/to/log.txt'
).catch((err) => {
console.error(err);
});
}
If options
is a string, then it specifies the encoding.
import fs from 'fs-promise-util';
export async function getFileContent () {
return await fs
.readFile(
'/path/to/messages directory',
'utf8'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.realpath returns the absolute pathname for the given path as a promise. In other words, it returns a promise for fs.realPath.
The options
argument can be a string specifying an encoding or an object with an encoding property specifying the character encoding to use for the path passed to the callback. If the encoding is set to buffer, the path returned will be passed as a Buffer object.
path
- [ string | Buffer ]options
- [ string | Object ]
encoding
- [ string ] - default = utf8
Lets say the directory structure is /etc/readme
:
import fs from 'fs-promise-util';
export async function getAbsolutePath () {
return await fs
.realPath(
'/messages directory',
'utf8'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.rename renames a file, moving it between directories if required, and returns a promise for fs.rename
.
oldPath
- [ string | Buffer ]newPath
- [ string | Buffer ]import fs from 'fs-promise-util';
export async function renameFile () {
return await fs
.rename(
'/path/to/tmp dir',
'/path/to/messages dir'
).catch((err) => {
console.error(err);
});
}
newPath
already exists, it will be automatically replaced so that there is no point in which another process attempting to access newPath
will find it missing. However, there will probably be a window in which both oldPath
and newPath
refer to the file being renamed.oldPath
and newPath
are existing hard links referring to the same file, then rename()
does nothing and returns a success status.newPath
exists but the operation fails for some reason, rename()
guarantees to leave an instance of newPath
in place.oldPath
can specify a directory. In this case, newPath
must either not exist, or it must specify an empty directory.oldPath
refers to a symbolic link, the link is renamed; if newPath
refers to a symbolic link, the link will be overwritten.This method retrieves information about a file pointed to by the given path and returns a promise for fs.stat.
path
- [ string | Buffer ]import fs from 'fs-promise-util';
export async function getStat () {
return fs
.stat(
'/path/to/info.log'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.symlink creates a symbolic link named path
which contains the string target and returns a promise for fs.symlink.
target
- [ string | Buffer ]path
- [ string | Buffer ]import fs from 'fs-promise-util';
export async function createSymink () {
return await fs
.symlink(
'./foo',
'./bar'
).catch((err) => {
console.error(err);
});
}
The above function creates a symbolic link named bar
that points to foo
.
path
exists, it will not be overwritten.fs-promise-util.tryWriteFile is a wrapper for fs-promise-util.writeFile
that always resolves to a promise. It asynchronously writes data to a file, replacing the file if it already exists.
The encoding option is ignored if data
is a buffer and defaults to utf8
.
file
- [ string | Buffer | number ] - filename or file descriptordata
- [ string | Buffer ]options
- [ Object | string ]
encoding
- [ string | null ] - default = utf8
mode
- [ integer ] - default = 0o666flag
- [ string ] - default = w
import fs from 'fs-promise-util';
export async function tryWriteContent (data = '') {
return await fs
.tryWriteFile(
'/path/to/info.log',
'data',
{
encoding : 'utf8'
}
).catch((err) => {
console.error(err);
});
}
fs-promise-util.unlink deletes a name from the filesystem and returns a promise for fs.unlink
.
path
- [ string | Buffer ]import fs from 'fs-promise-util';
export async function delete () {
return fs
.unlink(
'/path/to/file'
).catch((err) => {
console.error(err);
});
}
fs-promise-util.writeFile asynchronously writes data to a file, replacing the file if it already exists, and returns a promise for fs.writeFile.
The encoding option is ignored if data
is a buffer. It defaults to utf8
.
If options
is a string, then it specifies the encoding.
file
- [ string | Buffer | number ] - filename or file descriptordata
- [ string | Buffer | Uint8Array ]options
- [ Object | string ]
encoding
- [ string | null ] - default = utf8
mode
- [ integer ] - default = 0o666flag
[ string ] - default = w
import fs from 'fs-promise-util';
export async function tryWriteContent () {
return await fs
.writeFile(
'/path/to/messages.log',
'data to write',
{
encoding : 'utf8'
}
).catch((err) => {
console.error(err);
});
}
FAQs
a utility library for file system interaction on *nix machines
We found that fs-promise-util demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.