securityos/node_modules/strtok3/lib/FsPromise.js

70 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
/**
* Module convert fs functions to promise based functions
*/
import fs from 'node:fs';
export const pathExists = fs.existsSync;
export const createReadStream = fs.createReadStream;
export async function stat(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err)
reject(err);
else
resolve(stats);
});
});
}
export async function close(fd) {
return new Promise((resolve, reject) => {
fs.close(fd, err => {
if (err)
reject(err);
else
resolve();
});
});
}
export async function open(path, mode) {
return new Promise((resolve, reject) => {
fs.open(path, mode, (err, fd) => {
if (err)
reject(err);
else
resolve(fd);
});
});
}
export async function read(fd, buffer, offset, length, position) {
return new Promise((resolve, reject) => {
fs.read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
if (err)
reject(err);
else
resolve({ bytesRead, buffer: _buffer });
});
});
}
export async function writeFile(path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, err => {
if (err)
reject(err);
else
resolve();
});
});
}
export function writeFileSync(path, data) {
fs.writeFileSync(path, data);
}
export async function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, buffer) => {
if (err)
reject(err);
else
resolve(buffer);
});
});
}