UNPKG

ripstat

Version:

Fetch the stats for a file as if a saber-tooth tiger is chasing you!

37 lines (36 loc) 1.55 kB
/* IMPORT */ import { toNamespacedPath } from 'node:path'; import process from 'node:process'; import fs from 'stubborn-fs'; import { RETRY_TIMEOUT } from './constants.js'; import Stats from './stats.js'; /* HELPERS */ const { stat, FSReqCallback } = process['binding']('fs'); /* MAIN */ const ripstat = (filePath, timeout) => { return new Promise((resolve, reject) => { const req = new FSReqCallback(true); req.oncomplete = (error, statsdata) => { if (error) { const { code } = error; if (code === 'EMFILE' || code === 'ENFILE' || code === 'EAGAIN' || code === 'EBUSY' || code === 'EACCESS' || code === 'EACCS' || code === 'EPERM') { // Retriable error fs.retry.stat(timeout || RETRY_TIMEOUT)(filePath, { bigint: true }).then(nstats => { const statsdata = [nstats.dev, nstats.mode, nstats.nlink, nstats.uid, nstats.gid, nstats.rdev, nstats.blksize, nstats.ino, nstats.size, nstats.blocks, 0n, nstats.atimeNs, 0n, nstats.mtimeNs, 0n, nstats.ctimeNs, 0n, nstats.birthtimeNs]; const stats = new Stats(statsdata); resolve(stats); }, reject); } else { reject(error); } } else { const stats = new Stats(statsdata); resolve(stats); } }; stat(toNamespacedPath(filePath), true, req); }); }; /* EXPORT */ export default ripstat;