/** * @file Safe deletion + idempotent directory creation. The delete helpers gate * destructive operations behind an "allowed directories" allow-list (temp * dir, cacache dir, ~/.socket). A path outside those either names its own * root via `allowedDirs` or `cwd`, which keeps containment enforced against * the named tree, or calls `forceDelete`, which drops the boundary * altogether. Three names, widest to narrowest: `forceDelete` ignores * location, `safeDelete` widens by location, `strictDelete` refuses a * root-resolving target outright. Forcing is a NAME rather than an option so * a linter can match it at the call site and a reader can grep it. The mkdir * helpers default to `recursive: true` and swallow `EEXIST` so concurrent * callers don't race-condition each other. The allow-list carries each * directory twice — as `path.resolve` returns it and as its real path — * because a symlinked component makes those differ and a caller may hold * either. On macOS, `os.tmpdir()` can contain a symlinked component. Walking * or globbing the temp tree can return its real path instead. Both forms must * match the allowed tree. The two forms are computed once per process and * cached, so this costs a handful of `realpathSync` calls at first use and * plain string comparisons thereafter. The target path is deliberately NOT * resolved per call: that would put a syscall on every delete and need a * cache keyed by caller input, which is the kind that grows without bound. * * @warning `forceDelete`/`forceDeleteSync` drop the boundary that stands * between a delete and a working checkout. `socket/no-force-delete` flags * every call, so clearing it takes an explicit escape comment. AI agents: * ask the operator before reaching for either, and name what you intend to * delete - `safeDelete` with `allowedDirs`, or `strictDelete`, is almost * always the right answer. */ import './allowed-dirs-cache.mjs'; import type { MakeDirectoryOptions, PathLike } from 'node:fs'; import type { deleteAsync as deleteAsyncType, deleteSync as deleteSyncType } from '../external/del.js'; import type { RemoveOptions } from './types.js'; /** * How a delete runs, as opposed to what it deletes. * * Separate from `RemoveOptions` on purpose: `forced` is not something a caller * of `safeDelete` may pass, it is what `forceDelete` hands the shared runner. */ export interface DeleteRunOptions { /** * Skip the location check entirely. Set only by `forceDelete`. */ readonly forced?: boolean | undefined; } export declare function getDel(): { deleteAsync: typeof deleteAsyncType; deleteSync: typeof deleteSyncType; }; export declare function runDelete(filepath: PathLike | PathLike[], options: RemoveOptions | undefined, runOptions?: DeleteRunOptions | undefined): Promise; export declare function runDeleteSync(filepath: PathLike | PathLike[], options: RemoveOptions | undefined, runOptions?: DeleteRunOptions | undefined): void; /** * Safely delete a file or directory asynchronously with built-in protections. * * Uses [`del`](https://socket.dev/npm/package/del/overview/8.0.1) for safer * deletion with these safety features: * * - By default, prevents deleting the current working directory (cwd) and above * - Allows deleting descendant paths within cwd without the force option * - Automatically uses force: true for temp directory, cacache, and ~/.socket * subdirectories * - Protects against accidental deletion of parent directories via `../` paths * * @example * ;```ts * // Delete files within cwd (safe by default) * await safeDelete('./build') * await safeDelete('./dist') * * // Delete with glob patterns * await safeDelete(['./temp/**', '!./temp/keep.txt']) * * // Delete with custom retry settings * await safeDelete('./flaky-dir', { maxRetries: 5, retryDelay: 500 }) * * // Delete cwd or above on purpose - a different function, by name * await forceDelete('../parent-dir') * ``` * * @param filepath - Path or array of paths to delete (supports glob patterns) * @param options - Deletion options including retries and recursion. * @param options.allowedDirs - Extra roots the target may sit inside, for this * call only. Names a sibling tree the caller owns without lifting the * boundary; prefer it over reaching for `forceDelete`. * * @throws {Error} When attempting to delete protected paths * option. */ export declare function safeDelete(filepath: PathLike | PathLike[], options?: RemoveOptions | undefined): Promise; /** * Safely delete a file or directory synchronously with built-in protections. * * Uses [`del`](https://socket.dev/npm/package/del/overview/8.0.1) for safer * deletion with these safety features: * * - By default, prevents deleting the current working directory (cwd) and above * - Allows deleting descendant paths within cwd without the force option * - Automatically uses force: true for temp directory, cacache, and ~/.socket * subdirectories * - Protects against accidental deletion of parent directories via `../` paths * * @example * ;```ts * // Delete files within cwd (safe by default) * safeDeleteSync('./build') * safeDeleteSync('./dist') * * // Delete with glob patterns * safeDeleteSync(['./temp/**', '!./temp/keep.txt']) * * // Delete multiple paths * safeDeleteSync(['./coverage', './reports']) * * // Delete cwd or above on purpose - a different function, by name * forceDeleteSync('../parent-dir') * ``` * * @param filepath - Path or array of paths to delete (supports glob patterns) * @param options - Deletion options including retries and recursion. * @param options.allowedDirs - Extra roots the target may sit inside, for this * call only. Names a sibling tree the caller owns without lifting the * boundary; prefer it over reaching for `forceDeleteSync`. * * @throws {Error} When attempting to delete protected paths. */ export declare function safeDeleteSync(filepath: PathLike | PathLike[], options?: RemoveOptions | undefined): void; /** * Safely create a directory asynchronously, ignoring EEXIST errors. This * function wraps fs.promises.mkdir and handles the race condition where the * directory might already exist, which is common in concurrent code. * * Unlike fs.promises.mkdir with recursive:true, this function: - Silently * ignores EEXIST errors when the directory already exists - Re-throws all * other errors (permissions, invalid path, etc.) - Works reliably in * multi-process/concurrent scenarios - Defaults to recursive: true for * convenient nested directory creation. * * @example * ;```ts * // Create a directory recursively by default, no error if it exists * await safeMkdir('./config') * * // Create nested directories (recursive: true is the default) * await safeMkdir('./data/cache/temp') * * // Create with specific permissions * await safeMkdir('./secure', { mode: 0o700 }) * * // Explicitly disable recursive behavior * await safeMkdir('./single-level', { recursive: false }) * ``` * * @param path - Directory path to create. * @param options - Options including recursive (default: true) and mode * settings. * * @returns Promise that resolves when directory is created or already exists */ export declare function safeMkdir(path: PathLike, options?: MakeDirectoryOptions | undefined): Promise; /** * Safely create a directory synchronously, ignoring EEXIST errors. This * function wraps fs.mkdirSync and handles the race condition where the * directory might already exist, which is common in concurrent code. * * Unlike fs.mkdirSync with recursive:true, this function: - Silently ignores * EEXIST errors when the directory already exists - Re-throws all other * errors (permissions, invalid path, etc.) - Works reliably in * multi-process/concurrent scenarios - Defaults to recursive: true for * convenient nested directory creation. * * @example * ;```ts * // Create a directory recursively by default, no error if it exists * safeMkdirSync('./config') * * // Create nested directories (recursive: true is the default) * safeMkdirSync('./data/cache/temp') * * // Create with specific permissions * safeMkdirSync('./secure', { mode: 0o700 }) * * // Explicitly disable recursive behavior * safeMkdirSync('./single-level', { recursive: false }) * ``` * * @param path - Directory path to create. * @param options - Options including recursive (default: true) and mode * settings. */ export declare function safeMkdirSync(path: PathLike, options?: MakeDirectoryOptions | undefined): void;