Files
smartfs/readme.md

567 lines
17 KiB
Markdown
Raw Permalink Normal View History

2025-11-21 18:36:31 +00:00
# @push.rocks/smartfs
Modern, pluggable filesystem module with fluent API, Web Streams, Rust-powered durability, and multiple storage backends.
2025-11-21 18:36:31 +00:00
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
2025-11-21 18:36:31 +00:00
## Features
- 🎯 **Fluent API** — Action-last chainable interface for elegant, readable code
- 🔌 **Pluggable Providers** — Swap backends (Node.js fs, in-memory, Rust) without changing a line of application code
- 🦀 **Rust Provider** — XFS-safe `fsync` durability, cross-compiled binary via IPC for production-grade reliability
- 🌊 **Web Streams** — True chunked streaming with the Web Streams API (including over IPC for the Rust provider)
- 💾 **Transactions** — Atomic multi-file operations with automatic rollback on failure
- 👀 **File Watching** — Event-based filesystem monitoring with debounce, filters, and recursive watching
- 🔐 **Tree Hashing** — Deterministic SHA-256 directory hashing for cache-busting and change detection
- 📁 **Directory Copy & Move** — Full directory tree operations with conflict handling, filtering, and timestamp preservation
-**Async-Only** — Modern `async`/`await` patterns throughout — no sync footguns
- 🎨 **TypeScript-First** — Full type safety, IntelliSense, and exported interfaces
- 🌍 **Multi-Runtime** — Works on Node.js, Bun, and Deno
2025-11-21 18:36:31 +00:00
## Installation
```bash
npm install @push.rocks/smartfs
# or
pnpm add @push.rocks/smartfs
2025-11-21 18:36:31 +00:00
```
## Quick Start
```typescript
import { SmartFs, SmartFsProviderNode } from '@push.rocks/smartfs';
// Create a SmartFS instance with the Node.js provider
2025-11-21 18:36:31 +00:00
const fs = new SmartFs(new SmartFsProviderNode());
// Write a file
2025-11-21 18:36:31 +00:00
await fs.file('/path/to/file.txt')
.encoding('utf8')
.write('Hello, World!');
// Read it back
2025-11-21 18:36:31 +00:00
const content = await fs.file('/path/to/file.txt')
.encoding('utf8')
.read();
console.log(content); // "Hello, World!"
```
## API Overview
### 📄 File Operations
2025-11-21 18:36:31 +00:00
The fluent API uses an **action-last pattern** — configure first, then execute:
2025-11-21 18:36:31 +00:00
```typescript
// Read
2025-11-21 18:36:31 +00:00
const content = await fs.file('/path/to/file.txt')
.encoding('utf8')
.read();
// Write
2025-11-21 18:36:31 +00:00
await fs.file('/path/to/file.txt')
.encoding('utf8')
.mode(0o644)
.write('content');
// Atomic write (write to temp file, then rename — crash-safe)
2025-11-21 18:36:31 +00:00
await fs.file('/path/to/file.txt')
.atomic()
.write('content');
// Append
2025-11-21 18:36:31 +00:00
await fs.file('/path/to/file.txt')
.append('more content');
// Copy with preserved timestamps
2025-11-21 18:36:31 +00:00
await fs.file('/source.txt')
.preserveTimestamps()
.copy('/destination.txt');
// Move / rename
await fs.file('/old.txt').move('/new.txt');
2025-11-21 18:36:31 +00:00
// Delete
await fs.file('/path/to/file.txt').delete();
2025-11-21 18:36:31 +00:00
// Existence check
2025-11-21 18:36:31 +00:00
const exists = await fs.file('/path/to/file.txt').exists();
// Stats (size, timestamps, permissions, etc.)
2025-11-21 18:36:31 +00:00
const stats = await fs.file('/path/to/file.txt').stat();
```
### 📂 Directory Operations
2025-11-21 18:36:31 +00:00
```typescript
// Create directory (recursive by default)
await fs.directory('/path/to/nested/dir').create();
2025-11-21 18:36:31 +00:00
// List contents
2025-11-21 18:36:31 +00:00
const entries = await fs.directory('/path/to/dir').list();
// List recursively with glob filter and stats
const tsFiles = await fs.directory('/src')
2025-11-21 18:36:31 +00:00
.recursive()
.filter('*.ts')
.includeStats()
.list();
// Filter with RegExp
const configs = await fs.directory('/project')
.filter(/\.config\.(ts|js)$/)
2025-11-21 18:36:31 +00:00
.list();
// Filter with function
const largeFiles = await fs.directory('/data')
2025-11-21 18:36:31 +00:00
.includeStats()
.filter(entry => entry.stats && entry.stats.size > 1024)
.list();
// Delete directory recursively
await fs.directory('/path/to/dir').recursive().delete();
2025-11-21 18:36:31 +00:00
// Check existence
const exists = await fs.directory('/path/to/dir').exists();
```
2025-12-16 10:10:06 +00:00
### 📁 Directory Copy & Move
Copy or move entire directory trees with fine-grained control:
```typescript
// Basic copy
2025-12-16 10:10:06 +00:00
await fs.directory('/source').copy('/destination');
// Basic move
2025-12-16 10:10:06 +00:00
await fs.directory('/old-location').move('/new-location');
// Copy with options
await fs.directory('/source')
.filter(/\.ts$/) // Only copy TypeScript files
.overwrite(true) // Overwrite existing files
.preserveTimestamps(true) // Keep original timestamps
.copy('/destination');
// Ignore filter for copy (copy everything regardless of list filter)
2025-12-16 10:10:06 +00:00
await fs.directory('/source')
.filter('*.ts')
.applyFilter(false)
2025-12-16 10:10:06 +00:00
.copy('/destination');
// Handle target directory conflicts
await fs.directory('/source')
.onConflict('merge') // Default: merge contents
.copy('/destination');
await fs.directory('/source')
.onConflict('error') // Throw if target exists
.copy('/destination');
await fs.directory('/source')
.onConflict('replace') // Delete target first, then copy
.copy('/destination');
```
**Configuration Options:**
2025-12-16 10:10:06 +00:00
| Method | Default | Description |
|--------|---------|-------------|
| `filter(pattern)` | none | Filter files by glob, regex, or function |
| `applyFilter(bool)` | `true` | Whether to apply filter during copy/move |
| `overwrite(bool)` | `false` | Overwrite existing files at destination |
| `preserveTimestamps(bool)` | `false` | Preserve original file timestamps |
| `onConflict(mode)` | `'merge'` | `'merge'`, `'error'`, or `'replace'` |
### 🌊 Streaming Operations
SmartFS uses the **Web Streams API** for efficient, memory-friendly handling of large files. All providers — including the Rust provider over IPC — support true chunked streaming:
2025-11-21 18:36:31 +00:00
```typescript
// Read stream
const readStream = await fs.file('/large-file.bin')
.chunkSize(64 * 1024) // 64 KB chunks
2025-11-21 18:36:31 +00:00
.readStream();
const reader = readStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
// Process chunk (Uint8Array)
}
// Write stream
const writeStream = await fs.file('/output.bin').writeStream();
const writer = writeStream.getWriter();
await writer.write(new Uint8Array([1, 2, 3]));
await writer.write(new Uint8Array([4, 5, 6]));
await writer.close();
// Pipe one stream to another
2025-11-21 18:36:31 +00:00
const input = await fs.file('/input.txt').readStream();
const output = await fs.file('/output.txt').writeStream();
await input.pipeTo(output);
```
### 💾 Transactions
2025-11-21 18:36:31 +00:00
Execute multiple file operations atomically with automatic rollback on failure:
```typescript
// Simple transaction — all-or-nothing
2025-11-21 18:36:31 +00:00
await fs.transaction()
.file('/file1.txt').write('content 1')
.file('/file2.txt').write('content 2')
.file('/file3.txt').delete()
.commit();
// Transaction with error handling
const tx = fs.transaction()
.file('/important.txt').write('critical data')
.file('/backup.txt').copy('/backup-old.txt')
.file('/temp.txt').delete();
try {
await tx.commit();
console.log('Transaction completed successfully');
} catch (error) {
console.error('Transaction failed and was rolled back:', error);
// All operations are automatically reverted
}
```
### 👀 File Watching
2025-11-21 18:36:31 +00:00
Monitor filesystem changes with event-based watching:
```typescript
// Watch a single file
const watcher = await fs.watch('/path/to/file.txt')
.onChange(event => console.log('Changed:', event.path))
2025-11-21 18:36:31 +00:00
.start();
// Watch a directory recursively with filters and debounce
const dirWatcher = await fs.watch('/src')
2025-11-21 18:36:31 +00:00
.recursive()
.filter(/\.ts$/)
.debounce(100) // ms
2025-11-21 18:36:31 +00:00
.onChange(event => console.log('Changed:', event.path))
.onAdd(event => console.log('Added:', event.path))
.onDelete(event => console.log('Deleted:', event.path))
.start();
// Watch with a function filter
const customWatcher = await fs.watch('/src')
.recursive()
.filter(path => path.endsWith('.ts') && !path.includes('test'))
.onAll(event => console.log(`${event.type}: ${event.path}`))
.start();
2025-11-21 18:36:31 +00:00
// Stop watching
await dirWatcher.stop();
```
### 🔐 Tree Hashing (Cache-Busting)
2025-11-21 18:36:31 +00:00
Compute a deterministic hash of all files in a directory — ideal for cache invalidation, change detection, and build triggers:
```typescript
// Hash all files in a directory recursively
const hash = await fs.directory('/assets')
2025-11-21 18:36:31 +00:00
.recursive()
.treeHash();
// → "a3f2b8c9d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
// Hash only specific file types
const cssHash = await fs.directory('/styles')
.filter(/\.css$/)
.recursive()
.treeHash();
// Use a different algorithm
const sha512Hash = await fs.directory('/data')
.recursive()
.treeHash({ algorithm: 'sha512' });
2025-11-21 18:36:31 +00:00
```
**How it works:**
- Files are sorted by path for deterministic ordering
- Hashes relative path + file contents (streaming, memory-efficient)
- Does **not** include metadata (mtime/size) — pure content-based
- Same content always produces the same hash, regardless of timestamps
**Use cases:**
- 🚀 Cache-busting static assets
- 📦 Detecting when served files have changed
- 🔄 Incremental build triggers
- ✅ Content integrity verification
2025-11-21 18:36:31 +00:00
## Providers
SmartFS supports multiple storage backends through its provider architecture. Swap providers without changing any application code.
2025-11-21 18:36:31 +00:00
### 🟢 Node.js Provider
2025-11-21 18:36:31 +00:00
Uses Node.js `fs/promises` for local filesystem operations. The default choice for most applications:
2025-11-21 18:36:31 +00:00
```typescript
import { SmartFs, SmartFsProviderNode } from '@push.rocks/smartfs';
const fs = new SmartFs(new SmartFsProviderNode());
```
| Capability | Status |
|---|---|
| File watching | ✅ |
| Atomic writes | ✅ |
| Transactions | ✅ |
| Streaming | ✅ |
| Symbolic links | ✅ |
| File permissions | ✅ |
### 🦀 Rust Provider
A high-durability provider powered by a cross-compiled Rust binary that communicates via JSON-over-IPC. The Rust provider adds **XFS-safe `fsync` guarantees** that the Node.js `fs` module cannot provide — after every metadata-changing operation (`write`, `rename`, `unlink`, `mkdir`), the parent directory is explicitly `fsync`'d to ensure durability on delayed-logging filesystems like XFS.
```typescript
import { SmartFs, SmartFsProviderRust } from '@push.rocks/smartfs';
const fs = new SmartFs(new SmartFsProviderRust());
// Use it exactly like any other provider
await fs.file('/data/important.json')
.atomic()
.write(JSON.stringify(data));
// Don't forget to shut down when done
const provider = fs.provider as SmartFsProviderRust;
await provider.shutdown();
```
| Capability | Status |
|---|---|
| File watching | ✅ (via `notify` crate) |
| Atomic writes | ✅ (with fsync + parent fsync) |
| Transactions | ✅ (with batch fsync) |
| Streaming | ✅ (chunked IPC) |
| Symbolic links | ✅ |
| File permissions | ✅ |
**Key advantages over the Node.js provider:**
- `fsync` on parent directories after all metadata changes (crash-safe on XFS)
- Atomic writes with `fsync``rename``fsync parent` sequence
- Batch `fsync` for transactions (collect affected directories, sync once at end)
- Cross-device move with fallback (`EXDEV` handling)
- Uses the [`notify`](https://crates.io/crates/notify) crate for reliable file watching
2025-11-21 18:36:31 +00:00
### 🧪 Memory Provider
2025-11-21 18:36:31 +00:00
In-memory virtual filesystem — perfect for testing:
2025-11-21 18:36:31 +00:00
```typescript
import { SmartFs, SmartFsProviderMemory } from '@push.rocks/smartfs';
const fs = new SmartFs(new SmartFsProviderMemory());
// All operations work in memory — fast, isolated, no cleanup needed
2025-11-21 18:36:31 +00:00
await fs.file('/virtual/file.txt').write('data');
const content = await fs.file('/virtual/file.txt').encoding('utf8').read();
2025-11-21 18:36:31 +00:00
// Clear all data between tests
(fs.provider as SmartFsProviderMemory).clear();
2025-11-21 18:36:31 +00:00
```
| Capability | Status |
|---|---|
| File watching | ✅ |
| Atomic writes | ✅ |
| Transactions | ✅ |
| Streaming | ✅ |
| Symbolic links | ❌ |
| File permissions | ✅ |
2025-11-21 18:36:31 +00:00
### 🔧 Custom Providers
2025-11-21 18:36:31 +00:00
Build your own provider by implementing the `ISmartFsProvider` interface:
2025-11-21 18:36:31 +00:00
```typescript
import type { ISmartFsProvider } from '@push.rocks/smartfs';
class MyS3Provider implements ISmartFsProvider {
public readonly name = 's3';
2025-11-21 18:36:31 +00:00
public readonly capabilities = {
supportsWatch: false,
2025-11-21 18:36:31 +00:00
supportsAtomic: true,
supportsTransactions: true,
supportsStreaming: true,
supportsSymlinks: false,
supportsPermissions: false,
2025-11-21 18:36:31 +00:00
};
// Implement all required methods...
async readFile(path: string, options?) { /* ... */ }
async writeFile(path: string, content, options?) { /* ... */ }
// ... etc
}
const fs = new SmartFs(new MyS3Provider());
2025-11-21 18:36:31 +00:00
```
## Advanced Usage
### Encoding Options
```typescript
// UTF-8 (default for text)
await fs.file('/file.txt').encoding('utf8').write('text');
// Binary (Buffer)
2025-11-21 18:36:31 +00:00
const buffer = Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
await fs.file('/file.bin').write(buffer);
const data = await fs.file('/file.bin').read(); // Returns Buffer
2025-11-21 18:36:31 +00:00
// Base64
await fs.file('/file.txt').encoding('base64').write('SGVsbG8=');
// Hex
await fs.file('/file.txt').encoding('hex').write('48656c6c6f');
```
### File Permissions
```typescript
// Set file mode
await fs.file('/script.sh')
.mode(0o755)
.write('#!/bin/bash\necho "Hello"');
// Set directory mode
await fs.directory('/private')
.mode(0o700)
.create();
```
### Complex Filtering
```typescript
const recentLargeTs = await fs.directory('/src')
2025-11-21 18:36:31 +00:00
.recursive()
.includeStats()
.filter(entry => {
if (!entry.stats) return false;
return entry.isFile &&
entry.name.endsWith('.ts') &&
entry.stats.size > 1024 &&
entry.stats.mtime > new Date('2024-01-01');
})
.list();
```
### Transaction Operations
```typescript
const tx = fs.transaction();
// Build up operations
2025-11-21 18:36:31 +00:00
tx.file('/data/file1.json').write(JSON.stringify(data1));
tx.file('/data/file2.json').write(JSON.stringify(data2));
tx.file('/data/file1.json').copy('/backup/file1.json');
tx.file('/data/old.json').delete();
2025-11-21 18:36:31 +00:00
// Execute atomically — all succeed or all revert
2025-11-21 18:36:31 +00:00
await tx.commit();
```
## Type Definitions
SmartFS is fully typed. All interfaces and types are exported:
2025-11-21 18:36:31 +00:00
```typescript
import type {
// Provider interface
ISmartFsProvider,
IProviderCapabilities,
TWatchCallback,
IWatcherHandle,
// Core types
TEncoding, // 'utf8' | 'utf-8' | 'ascii' | 'base64' | 'hex' | 'binary' | 'buffer'
TFileMode, // number
2025-11-21 18:36:31 +00:00
IFileStats,
IDirectoryEntry,
// Watch types
TWatchEventType, // 'add' | 'change' | 'delete'
2025-11-21 18:36:31 +00:00
IWatchEvent,
IWatchOptions,
// Operation types
TTransactionOperationType, // 'write' | 'delete' | 'copy' | 'move' | 'append'
2025-11-21 18:36:31 +00:00
ITransactionOperation,
IReadOptions,
IWriteOptions,
IStreamOptions,
ICopyOptions,
IListOptions,
2025-11-21 18:36:31 +00:00
} from '@push.rocks/smartfs';
```
## Error Handling
SmartFS throws descriptive errors that mirror POSIX conventions:
2025-11-21 18:36:31 +00:00
```typescript
try {
await fs.file('/nonexistent.txt').read();
} catch (error) {
console.error(error.message);
// "ENOENT: no such file or directory, open '/nonexistent.txt'"
}
// Transactions automatically rollback on error
try {
await fs.transaction()
.file('/file1.txt').write('data')
.file('/readonly/file2.txt').write('data') // fails
2025-11-21 18:36:31 +00:00
.commit();
} catch (error) {
// file1.txt is reverted to its original state
2025-11-21 18:36:31 +00:00
console.error('Transaction failed:', error);
}
```
## Performance Tips
1. **Use streaming** for large files (> 1MB) — avoids loading entire files into memory
2. **Batch operations** with transactions for durability and performance
3. **Use the memory provider** for testing — instant, isolated, no disk I/O
4. **Enable atomic writes** for critical data — prevents partial writes on crash
5. **Debounce watchers** to reduce event noise during rapid changes
6. **Use `treeHash`** instead of reading individual files for change detection
7. **Use the Rust provider** on XFS or when you need guaranteed durability
2025-11-21 18:36:31 +00:00
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
2025-11-21 18:36:31 +00:00
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
2025-11-21 18:36:31 +00:00
### Company Information
2025-11-21 18:36:31 +00:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2025-11-21 18:36:31 +00:00
For any legal inquiries or further information, please contact us via email at hello@task.vc.
2025-11-21 18:36:31 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.