the editable site (validation-lab)
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.getBinding = getBinding;
|
||||
exports.getKVBinding = getKVBinding;
|
||||
exports.getR2Binding = getR2Binding;
|
||||
var _index = require("./index.cjs");
|
||||
function getBinding(binding) {
|
||||
let bindingName = "[binding]";
|
||||
if (typeof binding === "string") {
|
||||
bindingName = binding;
|
||||
binding = globalThis[bindingName] || globalThis.__env__?.[bindingName];
|
||||
}
|
||||
if (!binding) {
|
||||
throw (0, _index.createError)("cloudflare", `Invalid binding \`${bindingName}\`: \`${binding}\``);
|
||||
}
|
||||
for (const key of ["get", "put", "delete"]) {
|
||||
if (!(key in binding)) {
|
||||
throw (0, _index.createError)("cloudflare", `Invalid binding \`${bindingName}\`: \`${key}\` key is missing`);
|
||||
}
|
||||
}
|
||||
return binding;
|
||||
}
|
||||
function getKVBinding(binding = "STORAGE") {
|
||||
return getBinding(binding);
|
||||
}
|
||||
function getR2Binding(binding = "BUCKET") {
|
||||
return getBinding(binding);
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export declare function getBinding(binding: KVNamespace | R2Bucket | string): KVNamespace<string> | R2Bucket;
|
||||
export declare function getKVBinding(binding?: KVNamespace | string): KVNamespace;
|
||||
export declare function getR2Binding(binding?: R2Bucket | string): R2Bucket;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export declare function getBinding(binding: KVNamespace | R2Bucket | string): KVNamespace<string> | R2Bucket;
|
||||
export declare function getKVBinding(binding?: KVNamespace | string): KVNamespace;
|
||||
export declare function getR2Binding(binding?: R2Bucket | string): R2Bucket;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export declare function getBinding(binding: KVNamespace | R2Bucket | string): KVNamespace<string> | R2Bucket;
|
||||
export declare function getKVBinding(binding?: KVNamespace | string): KVNamespace;
|
||||
export declare function getR2Binding(binding?: R2Bucket | string): R2Bucket;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { createError } from "./index.mjs";
|
||||
export function getBinding(binding) {
|
||||
let bindingName = "[binding]";
|
||||
if (typeof binding === "string") {
|
||||
bindingName = binding;
|
||||
binding = globalThis[bindingName] || globalThis.__env__?.[bindingName];
|
||||
}
|
||||
if (!binding) {
|
||||
throw createError(
|
||||
"cloudflare",
|
||||
`Invalid binding \`${bindingName}\`: \`${binding}\``
|
||||
);
|
||||
}
|
||||
for (const key of ["get", "put", "delete"]) {
|
||||
if (!(key in binding)) {
|
||||
throw createError(
|
||||
"cloudflare",
|
||||
`Invalid binding \`${bindingName}\`: \`${key}\` key is missing`
|
||||
);
|
||||
}
|
||||
}
|
||||
return binding;
|
||||
}
|
||||
export function getKVBinding(binding = "STORAGE") {
|
||||
return getBinding(binding);
|
||||
}
|
||||
export function getR2Binding(binding = "BUCKET") {
|
||||
return getBinding(binding);
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.assertURLPathKey = assertURLPathKey;
|
||||
exports.createError = createError;
|
||||
exports.createRequiredError = createRequiredError;
|
||||
exports.defineDriver = defineDriver;
|
||||
exports.encodeURLKey = encodeURLKey;
|
||||
exports.encodeURLSegment = encodeURLSegment;
|
||||
exports.escapeRedisGlob = escapeRedisGlob;
|
||||
exports.joinKeys = joinKeys;
|
||||
exports.normalizeKey = normalizeKey;
|
||||
function defineDriver(factory) {
|
||||
return factory;
|
||||
}
|
||||
function normalizeKey(key, sep = ":") {
|
||||
if (!key) {
|
||||
return "";
|
||||
}
|
||||
return key.replace(/[:/\\]/g, sep).replace(/^[:/\\]|[:/\\]$/g, "");
|
||||
}
|
||||
function joinKeys(...keys) {
|
||||
return keys.map(key => normalizeKey(key)).filter(Boolean).join(":");
|
||||
}
|
||||
function encodeURLSegment(driver, segment, key = segment) {
|
||||
if (!segment || segment === "." || segment === "..") {
|
||||
throw createError(driver, `Invalid key: ${JSON.stringify(key)}. Empty, "." and ".." segments are not allowed`);
|
||||
}
|
||||
try {
|
||||
return encodeURIComponent(segment);
|
||||
} catch {
|
||||
throw createError(driver, `Invalid key: ${JSON.stringify(key)}. It contains malformed unicode`);
|
||||
}
|
||||
}
|
||||
function encodeURLKey(driver, key) {
|
||||
return (key || "").split(/[:/\\]/).filter(Boolean).map(segment => encodeURLSegment(driver, segment, key)).join("/");
|
||||
}
|
||||
function assertURLPathKey(driver, key) {
|
||||
const invalid = reason => createError(driver, `Invalid key: ${JSON.stringify(key)}. ${reason}`);
|
||||
if (!key) {
|
||||
throw invalid("Empty keys are not allowed");
|
||||
}
|
||||
if (/[?#]/.test(key)) {
|
||||
throw invalid(`"?" and "#" are not allowed`);
|
||||
}
|
||||
if (/[\u0000-\u001F]|^ | $/.test(key)) {
|
||||
throw invalid("Control characters and leading/trailing spaces are not allowed");
|
||||
}
|
||||
const hasDotSegment = key.split(/[/\\]|%2f|%5c/i).some(segment => {
|
||||
segment = segment.replace(/%2e/gi, ".");
|
||||
return segment === "." || segment === "..";
|
||||
});
|
||||
if (hasDotSegment) {
|
||||
throw invalid(`"." and ".." segments are not allowed`);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
function escapeRedisGlob(pattern) {
|
||||
return pattern.replace(/[*?[\]\\]/g, String.raw`\$&`);
|
||||
}
|
||||
function createError(driver, message, opts) {
|
||||
const err = new Error(`[unstorage] [${driver}] ${message}`, opts);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(err, createError);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
function createRequiredError(driver, name) {
|
||||
if (Array.isArray(name)) {
|
||||
return createError(driver, `Missing some of the required options ${name.map(n => "`" + n + "`").join(", ")}`);
|
||||
}
|
||||
return createError(driver, `Missing required option \`${name}\`.`);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import type { Driver } from "../..";
|
||||
type DriverFactory<OptionsT, InstanceT> = (opts: OptionsT) => Driver<OptionsT, InstanceT>;
|
||||
interface ErrorOptions {
|
||||
}
|
||||
export declare function defineDriver<OptionsT = any, InstanceT = never>(factory: DriverFactory<OptionsT, InstanceT>): DriverFactory<OptionsT, InstanceT>;
|
||||
export declare function normalizeKey(key: string | undefined, sep?: ":" | "/"): string;
|
||||
export declare function joinKeys(...keys: string[]): string;
|
||||
/**
|
||||
* Percent-encode a single URL path segment.
|
||||
*
|
||||
* Throws for empty, `.` and `..` segments (that URL parsers would collapse)
|
||||
* and for malformed unicode (lone surrogates).
|
||||
*/
|
||||
export declare function encodeURLSegment(driver: string, segment: string, key?: string): string;
|
||||
/**
|
||||
* Encode a key as a URL path (`a:b` => `a/b`) with each segment
|
||||
* percent-encoded, so that the key can never escape the base URL or inject
|
||||
* a `?query` / `#hash`.
|
||||
*/
|
||||
export declare function encodeURLKey(driver: string, key: string | undefined): string;
|
||||
/**
|
||||
* Validate a key that a third-party SDK puts into a URL path *without*
|
||||
* encoding it, so that the key can never escape its path.
|
||||
*
|
||||
* Throws for empty keys, `?` / `#`, control characters, leading / trailing
|
||||
* spaces and `.` / `..` segments (including the `%2e` spelling).
|
||||
*/
|
||||
export declare function assertURLPathKey(driver: string, key: string): string;
|
||||
/**
|
||||
* Escape glob special characters (`*`, `?`, `[`, `]`, `\`) so that a key
|
||||
* prefix can be used literally in a redis `SCAN MATCH` pattern.
|
||||
*/
|
||||
export declare function escapeRedisGlob(pattern: string): string;
|
||||
export declare function createError(driver: string, message: string, opts?: ErrorOptions): Error;
|
||||
export declare function createRequiredError(driver: string, name: string | string[]): Error;
|
||||
export {};
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import type { Driver } from "../..";
|
||||
type DriverFactory<OptionsT, InstanceT> = (opts: OptionsT) => Driver<OptionsT, InstanceT>;
|
||||
interface ErrorOptions {
|
||||
}
|
||||
export declare function defineDriver<OptionsT = any, InstanceT = never>(factory: DriverFactory<OptionsT, InstanceT>): DriverFactory<OptionsT, InstanceT>;
|
||||
export declare function normalizeKey(key: string | undefined, sep?: ":" | "/"): string;
|
||||
export declare function joinKeys(...keys: string[]): string;
|
||||
/**
|
||||
* Percent-encode a single URL path segment.
|
||||
*
|
||||
* Throws for empty, `.` and `..` segments (that URL parsers would collapse)
|
||||
* and for malformed unicode (lone surrogates).
|
||||
*/
|
||||
export declare function encodeURLSegment(driver: string, segment: string, key?: string): string;
|
||||
/**
|
||||
* Encode a key as a URL path (`a:b` => `a/b`) with each segment
|
||||
* percent-encoded, so that the key can never escape the base URL or inject
|
||||
* a `?query` / `#hash`.
|
||||
*/
|
||||
export declare function encodeURLKey(driver: string, key: string | undefined): string;
|
||||
/**
|
||||
* Validate a key that a third-party SDK puts into a URL path *without*
|
||||
* encoding it, so that the key can never escape its path.
|
||||
*
|
||||
* Throws for empty keys, `?` / `#`, control characters, leading / trailing
|
||||
* spaces and `.` / `..` segments (including the `%2e` spelling).
|
||||
*/
|
||||
export declare function assertURLPathKey(driver: string, key: string): string;
|
||||
/**
|
||||
* Escape glob special characters (`*`, `?`, `[`, `]`, `\`) so that a key
|
||||
* prefix can be used literally in a redis `SCAN MATCH` pattern.
|
||||
*/
|
||||
export declare function escapeRedisGlob(pattern: string): string;
|
||||
export declare function createError(driver: string, message: string, opts?: ErrorOptions): Error;
|
||||
export declare function createRequiredError(driver: string, name: string | string[]): Error;
|
||||
export {};
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import type { Driver } from "../..";
|
||||
type DriverFactory<OptionsT, InstanceT> = (opts: OptionsT) => Driver<OptionsT, InstanceT>;
|
||||
interface ErrorOptions {
|
||||
}
|
||||
export declare function defineDriver<OptionsT = any, InstanceT = never>(factory: DriverFactory<OptionsT, InstanceT>): DriverFactory<OptionsT, InstanceT>;
|
||||
export declare function normalizeKey(key: string | undefined, sep?: ":" | "/"): string;
|
||||
export declare function joinKeys(...keys: string[]): string;
|
||||
/**
|
||||
* Percent-encode a single URL path segment.
|
||||
*
|
||||
* Throws for empty, `.` and `..` segments (that URL parsers would collapse)
|
||||
* and for malformed unicode (lone surrogates).
|
||||
*/
|
||||
export declare function encodeURLSegment(driver: string, segment: string, key?: string): string;
|
||||
/**
|
||||
* Encode a key as a URL path (`a:b` => `a/b`) with each segment
|
||||
* percent-encoded, so that the key can never escape the base URL or inject
|
||||
* a `?query` / `#hash`.
|
||||
*/
|
||||
export declare function encodeURLKey(driver: string, key: string | undefined): string;
|
||||
/**
|
||||
* Validate a key that a third-party SDK puts into a URL path *without*
|
||||
* encoding it, so that the key can never escape its path.
|
||||
*
|
||||
* Throws for empty keys, `?` / `#`, control characters, leading / trailing
|
||||
* spaces and `.` / `..` segments (including the `%2e` spelling).
|
||||
*/
|
||||
export declare function assertURLPathKey(driver: string, key: string): string;
|
||||
/**
|
||||
* Escape glob special characters (`*`, `?`, `[`, `]`, `\`) so that a key
|
||||
* prefix can be used literally in a redis `SCAN MATCH` pattern.
|
||||
*/
|
||||
export declare function escapeRedisGlob(pattern: string): string;
|
||||
export declare function createError(driver: string, message: string, opts?: ErrorOptions): Error;
|
||||
export declare function createRequiredError(driver: string, name: string | string[]): Error;
|
||||
export {};
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
export function defineDriver(factory) {
|
||||
return factory;
|
||||
}
|
||||
export function normalizeKey(key, sep = ":") {
|
||||
if (!key) {
|
||||
return "";
|
||||
}
|
||||
return key.replace(/[:/\\]/g, sep).replace(/^[:/\\]|[:/\\]$/g, "");
|
||||
}
|
||||
export function joinKeys(...keys) {
|
||||
return keys.map((key) => normalizeKey(key)).filter(Boolean).join(":");
|
||||
}
|
||||
export function encodeURLSegment(driver, segment, key = segment) {
|
||||
if (!segment || segment === "." || segment === "..") {
|
||||
throw createError(
|
||||
driver,
|
||||
`Invalid key: ${JSON.stringify(key)}. Empty, "." and ".." segments are not allowed`
|
||||
);
|
||||
}
|
||||
try {
|
||||
return encodeURIComponent(segment);
|
||||
} catch {
|
||||
throw createError(
|
||||
driver,
|
||||
`Invalid key: ${JSON.stringify(key)}. It contains malformed unicode`
|
||||
);
|
||||
}
|
||||
}
|
||||
export function encodeURLKey(driver, key) {
|
||||
return (key || "").split(/[:/\\]/).filter(Boolean).map((segment) => encodeURLSegment(driver, segment, key)).join("/");
|
||||
}
|
||||
export function assertURLPathKey(driver, key) {
|
||||
const invalid = (reason) => createError(driver, `Invalid key: ${JSON.stringify(key)}. ${reason}`);
|
||||
if (!key) {
|
||||
throw invalid("Empty keys are not allowed");
|
||||
}
|
||||
if (/[?#]/.test(key)) {
|
||||
throw invalid(`"?" and "#" are not allowed`);
|
||||
}
|
||||
if (/[\u0000-\u001F]|^ | $/.test(key)) {
|
||||
throw invalid(
|
||||
"Control characters and leading/trailing spaces are not allowed"
|
||||
);
|
||||
}
|
||||
const hasDotSegment = key.split(/[/\\]|%2f|%5c/i).some((segment) => {
|
||||
segment = segment.replace(/%2e/gi, ".");
|
||||
return segment === "." || segment === "..";
|
||||
});
|
||||
if (hasDotSegment) {
|
||||
throw invalid(`"." and ".." segments are not allowed`);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
export function escapeRedisGlob(pattern) {
|
||||
return pattern.replace(/[*?[\]\\]/g, String.raw`\$&`);
|
||||
}
|
||||
export function createError(driver, message, opts) {
|
||||
const err = new Error(`[unstorage] [${driver}] ${message}`, opts);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(err, createError);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
export function createRequiredError(driver, name) {
|
||||
if (Array.isArray(name)) {
|
||||
return createError(
|
||||
driver,
|
||||
`Missing some of the required options ${name.map((n) => "`" + n + "`").join(", ")}`
|
||||
);
|
||||
}
|
||||
return createError(driver, `Missing required option \`${name}\`.`);
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.ensuredir = ensuredir;
|
||||
exports.readFile = readFile;
|
||||
exports.readdir = readdir;
|
||||
exports.readdirRecursive = readdirRecursive;
|
||||
exports.rmRecursive = rmRecursive;
|
||||
exports.stat = stat;
|
||||
exports.unlink = unlink;
|
||||
exports.writeFile = writeFile;
|
||||
var _nodeFs = require("node:fs");
|
||||
var _nodePath = require("node:path");
|
||||
function ignoreNotfound(err) {
|
||||
return err.code === "ENOENT" || err.code === "EISDIR" ? null : err;
|
||||
}
|
||||
function ignoreExists(err) {
|
||||
return err.code === "EEXIST" ? null : err;
|
||||
}
|
||||
async function writeFile(path, data, encoding) {
|
||||
await ensuredir((0, _nodePath.dirname)(path));
|
||||
return _nodeFs.promises.writeFile(path, data, encoding);
|
||||
}
|
||||
function readFile(path, encoding) {
|
||||
return _nodeFs.promises.readFile(path, encoding ?? null).catch(ignoreNotfound);
|
||||
}
|
||||
function stat(path) {
|
||||
return _nodeFs.promises.stat(path).catch(ignoreNotfound);
|
||||
}
|
||||
function unlink(path) {
|
||||
return _nodeFs.promises.unlink(path).catch(ignoreNotfound);
|
||||
}
|
||||
function readdir(dir) {
|
||||
return _nodeFs.promises.readdir(dir, {
|
||||
withFileTypes: true
|
||||
}).catch(ignoreNotfound).then(r => r || []);
|
||||
}
|
||||
async function ensuredir(dir) {
|
||||
if ((0, _nodeFs.existsSync)(dir)) {
|
||||
return;
|
||||
}
|
||||
await ensuredir((0, _nodePath.dirname)(dir)).catch(ignoreExists);
|
||||
await _nodeFs.promises.mkdir(dir).catch(ignoreExists);
|
||||
}
|
||||
async function readdirRecursive(dir, ignore, maxDepth) {
|
||||
if (ignore && ignore(dir)) {
|
||||
return [];
|
||||
}
|
||||
const entries = await readdir(dir);
|
||||
const files = [];
|
||||
await Promise.all(entries.map(async entry => {
|
||||
const entryPath = (0, _nodePath.resolve)(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (maxDepth === void 0 || maxDepth > 0) {
|
||||
const dirFiles = await readdirRecursive(entryPath, ignore, maxDepth === void 0 ? void 0 : maxDepth - 1);
|
||||
files.push(...dirFiles.map(f => entry.name + "/" + f));
|
||||
}
|
||||
} else {
|
||||
if (!(ignore && ignore(entry.name))) {
|
||||
files.push(entry.name);
|
||||
}
|
||||
}
|
||||
}));
|
||||
return files;
|
||||
}
|
||||
async function rmRecursive(dir) {
|
||||
const entries = await readdir(dir);
|
||||
await Promise.all(entries.map(entry => {
|
||||
const entryPath = (0, _nodePath.resolve)(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
return rmRecursive(entryPath).then(() => _nodeFs.promises.rmdir(entryPath));
|
||||
} else {
|
||||
return _nodeFs.promises.unlink(entryPath);
|
||||
}
|
||||
}));
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { Dirent, promises as fsPromises } from "node:fs";
|
||||
type WriteFileData = Parameters<typeof fsPromises.writeFile>[1];
|
||||
export declare function writeFile(path: string, data: WriteFileData, encoding?: BufferEncoding): Promise<void>;
|
||||
export declare function readFile(path: string, encoding?: BufferEncoding): Promise<any>;
|
||||
export declare function stat(path: string): Promise<any>;
|
||||
export declare function unlink(path: string): Promise<any>;
|
||||
export declare function readdir(dir: string): Promise<Dirent[]>;
|
||||
export declare function ensuredir(dir: string): Promise<void>;
|
||||
export declare function readdirRecursive(dir: string, ignore?: (p: string) => boolean, maxDepth?: number): Promise<string[]>;
|
||||
export declare function rmRecursive(dir: string): Promise<void>;
|
||||
export {};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { Dirent, promises as fsPromises } from "node:fs";
|
||||
type WriteFileData = Parameters<typeof fsPromises.writeFile>[1];
|
||||
export declare function writeFile(path: string, data: WriteFileData, encoding?: BufferEncoding): Promise<void>;
|
||||
export declare function readFile(path: string, encoding?: BufferEncoding): Promise<any>;
|
||||
export declare function stat(path: string): Promise<any>;
|
||||
export declare function unlink(path: string): Promise<any>;
|
||||
export declare function readdir(dir: string): Promise<Dirent[]>;
|
||||
export declare function ensuredir(dir: string): Promise<void>;
|
||||
export declare function readdirRecursive(dir: string, ignore?: (p: string) => boolean, maxDepth?: number): Promise<string[]>;
|
||||
export declare function rmRecursive(dir: string): Promise<void>;
|
||||
export {};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { Dirent, promises as fsPromises } from "node:fs";
|
||||
type WriteFileData = Parameters<typeof fsPromises.writeFile>[1];
|
||||
export declare function writeFile(path: string, data: WriteFileData, encoding?: BufferEncoding): Promise<void>;
|
||||
export declare function readFile(path: string, encoding?: BufferEncoding): Promise<any>;
|
||||
export declare function stat(path: string): Promise<any>;
|
||||
export declare function unlink(path: string): Promise<any>;
|
||||
export declare function readdir(dir: string): Promise<Dirent[]>;
|
||||
export declare function ensuredir(dir: string): Promise<void>;
|
||||
export declare function readdirRecursive(dir: string, ignore?: (p: string) => boolean, maxDepth?: number): Promise<string[]>;
|
||||
export declare function rmRecursive(dir: string): Promise<void>;
|
||||
export {};
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
import { existsSync, promises as fsPromises } from "node:fs";
|
||||
import { resolve, dirname } from "node:path";
|
||||
function ignoreNotfound(err) {
|
||||
return err.code === "ENOENT" || err.code === "EISDIR" ? null : err;
|
||||
}
|
||||
function ignoreExists(err) {
|
||||
return err.code === "EEXIST" ? null : err;
|
||||
}
|
||||
export async function writeFile(path, data, encoding) {
|
||||
await ensuredir(dirname(path));
|
||||
return fsPromises.writeFile(path, data, encoding);
|
||||
}
|
||||
export function readFile(path, encoding) {
|
||||
return fsPromises.readFile(path, encoding ?? null).catch(ignoreNotfound);
|
||||
}
|
||||
export function stat(path) {
|
||||
return fsPromises.stat(path).catch(ignoreNotfound);
|
||||
}
|
||||
export function unlink(path) {
|
||||
return fsPromises.unlink(path).catch(ignoreNotfound);
|
||||
}
|
||||
export function readdir(dir) {
|
||||
return fsPromises.readdir(dir, { withFileTypes: true }).catch(ignoreNotfound).then((r) => r || []);
|
||||
}
|
||||
export async function ensuredir(dir) {
|
||||
if (existsSync(dir)) {
|
||||
return;
|
||||
}
|
||||
await ensuredir(dirname(dir)).catch(ignoreExists);
|
||||
await fsPromises.mkdir(dir).catch(ignoreExists);
|
||||
}
|
||||
export async function readdirRecursive(dir, ignore, maxDepth) {
|
||||
if (ignore && ignore(dir)) {
|
||||
return [];
|
||||
}
|
||||
const entries = await readdir(dir);
|
||||
const files = [];
|
||||
await Promise.all(
|
||||
entries.map(async (entry) => {
|
||||
const entryPath = resolve(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (maxDepth === void 0 || maxDepth > 0) {
|
||||
const dirFiles = await readdirRecursive(
|
||||
entryPath,
|
||||
ignore,
|
||||
maxDepth === void 0 ? void 0 : maxDepth - 1
|
||||
);
|
||||
files.push(...dirFiles.map((f) => entry.name + "/" + f));
|
||||
}
|
||||
} else {
|
||||
if (!(ignore && ignore(entry.name))) {
|
||||
files.push(entry.name);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
return files;
|
||||
}
|
||||
export async function rmRecursive(dir) {
|
||||
const entries = await readdir(dir);
|
||||
await Promise.all(
|
||||
entries.map((entry) => {
|
||||
const entryPath = resolve(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
return rmRecursive(entryPath).then(() => fsPromises.rmdir(entryPath));
|
||||
} else {
|
||||
return fsPromises.unlink(entryPath);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user