Skip to content
bun-fs-helpers logo

Bun Filesystem Helpers

bun-fs-helpers

Pure Bun-native filesystem utilities from @sidequest/core/fs. Use when you need command-injection-safe filesystem operations, prefer Bun over node:fs, or want token-efficient fs helpers. All functions use Bun.spawn, Bun.file(), or Bun.write() - no node:fs dependencies.

SKILL.md

Full skill instructions

Bun Filesystem Helpers

Pure Bun-native filesystem utilities from @sidequest/​core/​fs - zero node:fs dependencies, command-injection safe.

When to Use

  • Writing new code - Always prefer these over node:fs
  • Command injection concerns - All shell commands use array args (safe)
  • Token efficiency - Smaller imports, faster operations
  • Bun-first projects - Leverages Bun's native APIs

Available Functions

File Existence

import { pathExists, pathExistsSync } from "@sidequest/​core/​fs";

// Async
if (await pathExists("/​path/​to/​file")) { }

// Sync
if (pathExistsSync("/​path/​to/​file")) { }

Implementation: Uses Bun.file().exists() (async) or test -e command (sync)

Reading Files

import { readTextFile, readTextFileSync, readJsonFile, readJsonFileSync } from "@sidequest/​core/​fs";

// Async text
const content = await readTextFile("/​path/​to/​file.txt");

// Sync text
const content = readTextFileSync("/​path/​to/​file.txt");

// Async JSON
const data = await readJsonFile<MyType>("/​path/​to/​data.json");

// Sync JSON
const data = readJsonFileSync<MyType>("/​path/​to/​data.json");

Implementation: Uses Bun.file().text() (async) or cat command (sync)

Writing Files

import { writeTextFile, writeTextFileSync, writeJsonFile, writeJsonFileSync } from "@sidequest/​core/​fs";

// Async text
await writeTextFile("/​path/​to/​file.txt", "content");

// Sync text
writeTextFileSync("/​path/​to/​file.txt", "content");

// Async JSON
await writeJsonFile("/​path/​to/​data.json", { foo: "bar" }, 2);

// Sync JSON
writeJsonFileSync("/​path/​to/​data.json", { foo: "bar" }, 2);

Implementation: Uses Bun.write() (async) or printf via shell (sync)

Directory Operations

import { ensureDir, ensureDirSync, readDir, readDirAsync } from "@sidequest/​core/​fs";

// Create directory (recursive)
await ensureDir("/​path/​to/​nested/​dir");
ensureDirSync("/​path/​to/​nested/​dir");

// List directory contents
const files = readDir("/​path/​to/​dir");  // Sync
const files = await readDirAsync("/​path/​to/​dir");  // Async

Implementation: Uses mkdir -p command, ls -1 command

File Operations

import { copyFile, moveFile, rename, unlink, unlinkSync } from "@sidequest/​core/​fs";

// Copy file
await copyFile("/​source.txt", "/​dest.txt");

// Move file (copy + delete)
await moveFile("/​source.txt", "/​dest.txt");

// Rename/​move atomically
await rename("/​old-path.txt", "/​new-path.txt");

// Delete file
await unlink("/​path/​to/​file.txt");
unlinkSync("/​path/​to/​file.txt");

Implementation: Uses Bun.write() for copy, mv command for rename, rm command for delete

File Stats

import { stat } from "@sidequest/​core/​fs";

const stats = await stat("/​path/​to/​file.txt");
console.log(stats.size);      // File size in bytes
console.log(stats.mtimeMs);   // Last modified timestamp

Implementation: Uses Bun.file().size and Bun.file().lastModified

Use case: TOCTOU protection - check file before AND after operations

Hashing

import { sha256, sha256File, fastHash } from "@sidequest/​core/​fs";

// Hash string
const hash = sha256("content");  // Hex string (64 chars)

// Hash file
const hash = await sha256File("/​path/​to/​file.pdf");

// Fast non-cryptographic hash (cache keys)
const hash = fastHash("content");  // bigint or number

Implementation: Uses Bun.CryptoHasher (SHA256) or Bun.hash (xxHash64)

Deep Equality

import { deepEquals } from "@sidequest/​core/​fs";

const equal = deepEquals(obj1, obj2);           // Loose mode
const equal = deepEquals(obj1, obj2, true);     // Strict mode

Implementation: Uses Bun.deepEquals()

Security Guarantees

✅ Command injection safe - All shell commands use array arguments:

// Safe - array args prevent injection
Bun.spawnSync(["mkdir", "-p", userInput]);

// Never - string interpolation allows injection
Bun.spawnSync(`mkdir -p ${userInput}`);  // ❌ DON'T DO THIS

✅ TOCTOU protection - Use stat() before AND after file operations to detect tampering

Migration from node:fs

// Before (node:fs)
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";

existsSync("/​path");
mkdirSync("/​path", { recursive: true });
readFileSync("/​path", "utf8");
writeFileSync("/​path", "content", "utf8");

// After (@sidequest/​core/​fs)
import { pathExistsSync, ensureDirSync, readTextFileSync, writeTextFileSync } from "@sidequest/​core/​fs";

pathExistsSync("/​path");
ensureDirSync("/​path");
readTextFileSync("/​path");
writeTextFileSync("/​path", "content");

Performance Characteristics

OperationSpeedNotes
pathExists~1msBun.file().exists()
pathExistsSync~2mstest -e command
readTextFile~5-50msDepends on file size
writeTextFile~5-20msBun.write() is fast
ensureDir~10msmkdir -p command
readDir~5-15msls -1 command
sha256File~50-500msDepends on file size
fastHash<1msxxHash64 is very fast

Common Patterns

Atomic File Updates

import { pathExistsSync, writeTextFileSync, rename } from "@sidequest/​core/​fs";

// Write to temp file, then atomically rename
const tempPath = `${targetPath}.tmp`;
writeTextFileSync(tempPath, newContent);
await rename(tempPath, targetPath);  // POSIX guarantees atomicity

Safe File Modification with TOCTOU Protection

import { stat, readTextFileSync, writeTextFileSync } from "@sidequest/​core/​fs";

// Get pre-modification stats
const preStat = await stat(filePath);

// Read and modify
const content = readTextFileSync(filePath);
const modified = transform(content);

// Verify file unchanged before writing
const postStat = await stat(filePath);
if (postStat.mtimeMs !== preStat.mtimeMs) {
  throw new Error("File was modified during read (TOCTOU attack detected)");
}

writeTextFileSync(filePath, modified);

Idempotent Processing with Content Hashing

import { sha256File, pathExistsSync } from "@sidequest/​core/​fs";

const hash = await sha256File(sourceFile);
const processedMarker = `.processed/​${hash}`;

if (pathExistsSync(processedMarker)) {
  console.log("Already processed - skipping");
  return;
}

// Process file...
writeTextFileSync(processedMarker, new Date().toISOString());

Implementation Details

All functions are exported from core/​src/​fs/​index.ts and use:

  • Bun.file() - File existence, reading, size, timestamps
  • Bun.write() - Writing files
  • Bun.spawn() / Bun.spawnSync() - Shell commands with array args
  • Bun.CryptoHasher - SHA256 hashing
  • Bun.hash - Fast non-cryptographic hashing (xxHash64)
  • Bun.deepEquals() - Deep equality checks
  • Bun.Glob - Recursive file scanning (used elsewhere, not exported from fs)

Zero node:fs dependencies in production code.

References

  • Core package source: core/​src/​fs/​index.ts
  • Test coverage: 423 tests passing
  • Used by: para-obsidian inbox processing, registry, PDF processor

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View