Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rrdir

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rrdir - npm Package Compare versions

Comparing version 13.1.0 to 13.1.1

300

dist/index.js

@@ -1,214 +0,164 @@

import { readdir, stat, lstat } from "node:fs/promises";
import { readdirSync, statSync, lstatSync } from "node:fs";
import { sep, resolve } from "node:path";
import picomatch from "picomatch";
const encoder = new TextEncoder();
const toUint8Array = encoder.encode.bind(encoder);
const decoder = new TextDecoder();
const toString = decoder.decode.bind(decoder);
const sepUint8Array = toUint8Array(sep);
const getEncoding = (dir) => dir instanceof Uint8Array ? "buffer" : "utf8";
const defaultOpts = {
strict: false,
stats: false,
followSymlinks: false,
import { readdir as U, stat as b, lstat as g } from "node:fs/promises";
import { readdirSync as I, statSync as $, lstatSync as E } from "node:fs";
import { sep as F, resolve as A } from "node:path";
import P from "picomatch";
const T = new TextEncoder(), O = T.encode.bind(T), p = new TextDecoder(), h = p.decode.bind(p), j = O(F), S = (t) => t instanceof Uint8Array ? "buffer" : "utf8", k = {
strict: !1,
stats: !1,
followSymlinks: !1,
exclude: void 0,
include: void 0,
insensitive: false
insensitive: !1
};
function makePath({ name }, dir, encoding) {
if (encoding === "buffer") {
return dir === "." ? name : Uint8Array.from([...dir, ...sepUint8Array, ...name]);
} else {
return dir === "." ? name : `${dir}${sep}${name}`;
}
function v({ name: t }, i, s) {
return s === "buffer" ? i === "." ? t : Uint8Array.from([...i, ...j, ...t]) : i === "." ? t : `${i}${F}${t}`;
}
function build(dirent, path, stats, opts) {
function D(t, i, s, l) {
return {
path,
directory: (stats || dirent).isDirectory(),
symlink: (stats || dirent).isSymbolicLink(),
...opts.stats ? { stats } : {}
path: i,
directory: (s || t).isDirectory(),
symlink: (s || t).isSymbolicLink(),
...l.stats ? { stats: s } : {}
};
}
function makeMatchers({ include, exclude, insensitive }) {
const opts = {
dot: true,
flags: insensitive ? "i" : void 0
function L({ include: t, exclude: i, insensitive: s }) {
const l = {
dot: !0,
flags: s ? "i" : void 0
};
return {
includeMatcher: (include == null ? void 0 : include.length) ? (path) => picomatch(include, opts)(resolve(path)) : null,
excludeMatcher: (exclude == null ? void 0 : exclude.length) ? (path) => picomatch(exclude, opts)(resolve(path)) : null
includeMatcher: t?.length ? (f) => P(t, l)(A(f)) : null,
excludeMatcher: i?.length ? (f) => P(i, l)(A(f)) : null
};
}
async function* rrdir(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
if (includeMatcher === void 0) {
opts = { ...defaultOpts, ...opts };
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
if (typeof dir === "string" && /[/\\]$/.test(dir))
dir = dir.substring(0, dir.length - 1);
encoding = getEncoding(dir);
}
let dirents = [];
async function* q(t, i = {}, { includeMatcher: s, excludeMatcher: l, encoding: f } = {}) {
s === void 0 && (i = { ...k, ...i }, { includeMatcher: s, excludeMatcher: l } = L(i), typeof t == "string" && /[/\\]$/.test(t) && (t = t.substring(0, t.length - 1)), f = S(t));
let n = [];
try {
dirents = await readdir(dir, { encoding, withFileTypes: true });
} catch (err) {
if (opts.strict)
throw err;
yield { path: dir, err };
n = await U(t, { encoding: f, withFileTypes: !0 });
} catch (y) {
if (i.strict)
throw y;
yield { path: t, err: y };
}
if (!dirents.length)
return;
for (const dirent of dirents) {
const path = makePath(dirent, dir, encoding);
if (excludeMatcher == null ? void 0 : excludeMatcher(encoding === "buffer" ? toString(path) : path))
continue;
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
const encodedPath = encoding === "buffer" ? toString(path) : path;
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
let stats;
if (isIncluded) {
if (opts.stats || isSymbolicLink) {
try {
stats = await (opts.followSymlinks ? stat : lstat)(path);
} catch (err) {
if (opts.strict)
throw err;
yield { path, err };
}
if (n.length)
for (const y of n) {
const r = v(y, t, f);
if (l?.(f === "buffer" ? h(r) : r))
continue;
const e = i.followSymlinks && y.isSymbolicLink(), u = f === "buffer" ? h(r) : r, m = !s || s(u);
let c;
if (m) {
if (i.stats || e)
try {
c = await (i.followSymlinks ? b : g)(r);
} catch (a) {
if (i.strict)
throw a;
yield { path: r, err: a };
}
yield D(y, r, c, i);
}
yield build(dirent, path, stats, opts);
let o = !1;
if (e) {
if (!c)
try {
c = await b(r);
} catch {
}
c && c.isDirectory() && (o = !0);
} else
y.isDirectory() && (o = !0);
o && (yield* q(r, i, { includeMatcher: s, excludeMatcher: l, encoding: f }));
}
let recurse = false;
if (isSymbolicLink) {
if (!stats)
try {
stats = await stat(path);
} catch {
}
if (stats && stats.isDirectory())
recurse = true;
} else if (dirent.isDirectory()) {
recurse = true;
}
if (recurse)
yield* rrdir(path, opts, { includeMatcher, excludeMatcher, encoding });
}
}
async function rrdirAsync(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
if (includeMatcher === void 0) {
opts = { ...defaultOpts, ...opts };
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
if (typeof dir === "string" && /[/\\]$/.test(dir))
dir = dir.substring(0, dir.length - 1);
encoding = getEncoding(dir);
}
const results = [];
let dirents = [];
async function z(t, i = {}, { includeMatcher: s, excludeMatcher: l, encoding: f } = {}) {
s === void 0 && (i = { ...k, ...i }, { includeMatcher: s, excludeMatcher: l } = L(i), typeof t == "string" && /[/\\]$/.test(t) && (t = t.substring(0, t.length - 1)), f = S(t));
const n = [];
let y = [];
try {
dirents = await readdir(dir, { encoding, withFileTypes: true });
} catch (err) {
if (opts.strict)
throw err;
results.push({ path: dir, err });
y = await U(t, { encoding: f, withFileTypes: !0 });
} catch (r) {
if (i.strict)
throw r;
n.push({ path: t, err: r });
}
if (!dirents.length)
return results;
await Promise.all(dirents.map(async (dirent) => {
const path = makePath(dirent, dir, encoding);
if (excludeMatcher == null ? void 0 : excludeMatcher(encoding === "buffer" ? toString(path) : path))
return y.length && await Promise.all(y.map(async (r) => {
const e = v(r, t, f);
if (l?.(f === "buffer" ? h(e) : e))
return;
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
const encodedPath = encoding === "buffer" ? toString(path) : path;
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
let stats;
if (isIncluded) {
if (opts.stats || isSymbolicLink) {
const u = i.followSymlinks && r.isSymbolicLink(), m = f === "buffer" ? h(e) : e, c = !s || s(m);
let o;
if (c) {
if (i.stats || u)
try {
stats = await (opts.followSymlinks ? stat : lstat)(path);
} catch (err) {
if (opts.strict)
throw err;
results.push({ path, err });
o = await (i.followSymlinks ? b : g)(e);
} catch (w) {
if (i.strict)
throw w;
n.push({ path: e, err: w });
}
}
results.push(build(dirent, path, stats, opts));
n.push(D(r, e, o, i));
}
let recurse = false;
if (isSymbolicLink) {
if (!stats)
let a = !1;
if (u) {
if (!o)
try {
stats = await stat(path);
o = await b(e);
} catch {
}
if (stats && stats.isDirectory())
recurse = true;
} else if (dirent.isDirectory()) {
recurse = true;
}
if (recurse)
results.push(...await rrdirAsync(path, opts, { includeMatcher, excludeMatcher, encoding }));
}));
return results;
o && o.isDirectory() && (a = !0);
} else
r.isDirectory() && (a = !0);
a && n.push(...await z(e, i, { includeMatcher: s, excludeMatcher: l, encoding: f }));
})), n;
}
function rrdirSync(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
if (includeMatcher === void 0) {
opts = { ...defaultOpts, ...opts };
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
if (typeof dir === "string" && /[/\\]$/.test(dir))
dir = dir.substring(0, dir.length - 1);
encoding = getEncoding(dir);
}
const results = [];
let dirents = [];
function B(t, i = {}, { includeMatcher: s, excludeMatcher: l, encoding: f } = {}) {
s === void 0 && (i = { ...k, ...i }, { includeMatcher: s, excludeMatcher: l } = L(i), typeof t == "string" && /[/\\]$/.test(t) && (t = t.substring(0, t.length - 1)), f = S(t));
const n = [];
let y = [];
try {
dirents = readdirSync(dir, { encoding, withFileTypes: true });
} catch (err) {
if (opts.strict)
throw err;
results.push({ path: dir, err });
y = I(t, { encoding: f, withFileTypes: !0 });
} catch (r) {
if (i.strict)
throw r;
n.push({ path: t, err: r });
}
if (!dirents.length)
return results;
for (const dirent of dirents) {
const path = makePath(dirent, dir, encoding);
if (excludeMatcher == null ? void 0 : excludeMatcher(encoding === "buffer" ? toString(path) : path))
if (!y.length)
return n;
for (const r of y) {
const e = v(r, t, f);
if (l?.(f === "buffer" ? h(e) : e))
continue;
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
const encodedPath = encoding === "buffer" ? toString(path) : path;
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
let stats;
if (isIncluded) {
if (opts.stats || isSymbolicLink) {
const u = i.followSymlinks && r.isSymbolicLink(), m = f === "buffer" ? h(e) : e, c = !s || s(m);
let o;
if (c) {
if (i.stats || u)
try {
stats = (opts.followSymlinks ? statSync : lstatSync)(path);
} catch (err) {
if (opts.strict)
throw err;
results.push({ path, err });
o = (i.followSymlinks ? $ : E)(e);
} catch (w) {
if (i.strict)
throw w;
n.push({ path: e, err: w });
}
}
results.push(build(dirent, path, stats, opts));
n.push(D(r, e, o, i));
}
let recurse = false;
if (isSymbolicLink) {
if (!stats)
let a = !1;
if (u) {
if (!o)
try {
stats = statSync(path);
o = $(e);
} catch {
}
if (stats && stats.isDirectory())
recurse = true;
} else if (dirent.isDirectory()) {
recurse = true;
}
if (recurse)
results.push(...rrdirSync(path, opts, { includeMatcher, excludeMatcher, encoding }));
o && o.isDirectory() && (a = !0);
} else
r.isDirectory() && (a = !0);
a && n.push(...B(e, i, { includeMatcher: s, excludeMatcher: l, encoding: f }));
}
return results;
return n;
}
export {
rrdir,
rrdirAsync,
rrdirSync
q as rrdir,
z as rrdirAsync,
B as rrdirSync
};
{
"name": "rrdir",
"version": "13.1.0",
"version": "13.1.1",
"description": "Recursive directory reader with a delightful API",

@@ -32,3 +32,3 @@ "author": "silverwind <me@silverwind.io>",

"vite": "5.2.11",
"vite-plugin-dts": "3.9.1",
"vite-config-silverwind": "2.0.3",
"vitest": "1.5.0",

@@ -35,0 +35,0 @@ "vitest-config-silverwind": "8.0.4"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc