slimp/node_modules/strtok3/lib/FsPromise.js
2023-06-12 11:40:38 -04:00

70 lines
1.8 KiB
JavaScript

/**
* 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);
});
});
}