feat(smartfs.directory): Add directory treeHash: deterministic content-based hashing of directory trees with streaming and algorithm option

This commit is contained in:
2025-12-02 08:35:37 +00:00
parent c73a931dad
commit ebc7de4503
6 changed files with 242 additions and 28 deletions

View File

@@ -1,5 +1,14 @@
# Changelog # Changelog
## 2025-12-02 - 1.2.0 - feat(smartfs.directory)
Add directory treeHash: deterministic content-based hashing of directory trees with streaming and algorithm option
- Implement treeHash(options?) on SmartFsDirectory which computes a deterministic hash of a directory tree by hashing relative file paths and streaming file contents (default algorithm: 'sha256').
- Introduce ITreeHashOptions type (algorithm?: string) to allow selecting the hash algorithm (e.g. 'sha256', 'sha512').
- Use Node.js crypto to update the hash incrementally while streaming file data to keep memory usage low.
- Add tests in test/test.node.provider.ts covering treeHash behavior, determinism, algorithm selection, and empty-directory hashing.
- Update README with documentation, examples and explanation of treeHash use cases and behavior.
## 2025-11-30 - 1.1.3 - fix(smartfs.provider.node) ## 2025-11-30 - 1.1.3 - fix(smartfs.provider.node)
Default createDirectory to recursive=true when option not provided in Node provider Default createDirectory to recursive=true when option not provided in Node provider

View File

@@ -4,23 +4,26 @@ Modern, pluggable filesystem module with fluent API, Web Streams support, and mu
## Issue Reporting and Security ## 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 want to sign a contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly. 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.
## Features ## Features
- **🎯 Fluent API** - Action-last chainable interface for elegant code - 🎯 **Fluent API** - Action-last chainable interface for elegant code
- **🔌 Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.) - 🔌 **Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.)
- **🌊 Web Streams** - Modern streaming with Web Streams API - 🌊 **Web Streams** - Modern streaming with Web Streams API
- **💾 Transactions** - Atomic multi-file operations with automatic rollback - 💾 **Transactions** - Atomic multi-file operations with automatic rollback
- **👀 File Watching** - Event-based file system monitoring - 👀 **File Watching** - Event-based file system monitoring
- **⚡ Async-Only** - Modern async/await patterns throughout - 🔐 **Tree Hashing** - SHA-256 directory hashing for cache-busting
- **📦 Zero Dependencies** - Core functionality with minimal dependencies - **Async-Only** - Modern async/await patterns throughout
- **🎨 TypeScript** - Full type safety and IntelliSense support - 📦 **Zero Dependencies** - Core functionality with minimal footprint
- 🎨 **TypeScript** - Full type safety and IntelliSense support
## Installation ## Installation
```bash ```bash
pnpm install @push.rocks/smartfs npm install @push.rocks/smartfs
# or
pnpm add @push.rocks/smartfs
``` ```
## Quick Start ## Quick Start
@@ -132,6 +135,41 @@ await fs.directory('/path/to/dir')
const exists = await fs.directory('/path/to/dir').exists(); const exists = await fs.directory('/path/to/dir').exists();
``` ```
### 🔐 Tree Hashing (Cache-Busting)
Compute a deterministic hash of all files in a directory - perfect for cache invalidation:
```typescript
// Hash all files in a directory recursively
const hash = await fs.directory('/assets')
.recursive()
.treeHash();
// Returns: "a3f2b8c9d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
// Hash only specific file types
const cssHash = await fs.directory('/styles')
.filter(/\.css$/)
.recursive()
.treeHash();
// Use different algorithm
const sha512Hash = await fs.directory('/data')
.recursive()
.treeHash({ algorithm: 'sha512' });
```
**How it works:**
- Files are sorted by path for deterministic ordering
- Hashes relative paths + file contents (streaming, memory-efficient)
- Does NOT include metadata (mtime/size) - pure content-based
- Same content always produces same hash, regardless of timestamps
**Use cases:**
- 🚀 Cache-busting static assets
- 📦 Detecting when served files change
- 🔄 Incremental build triggers
- ✅ Content verification
### Streaming Operations ### Streaming Operations
SmartFS uses **Web Streams API** for efficient handling of large files: SmartFS uses **Web Streams API** for efficient handling of large files:
@@ -247,6 +285,7 @@ const fs = new SmartFs(new SmartFsProviderNode());
- ✅ Streaming - ✅ Streaming
- ✅ Symbolic links - ✅ Symbolic links
- ✅ File permissions - ✅ File permissions
- ✅ Tree hashing
### Memory Provider ### Memory Provider
@@ -272,6 +311,7 @@ fs.provider.clear();
- ✅ Streaming - ✅ Streaming
- ❌ Symbolic links - ❌ Symbolic links
- ✅ File permissions - ✅ File permissions
- ✅ Tree hashing
### Custom Providers ### Custom Providers
@@ -382,24 +422,12 @@ import type {
IDirectoryEntry, IDirectoryEntry,
IWatchEvent, IWatchEvent,
ITransactionOperation, ITransactionOperation,
ITreeHashOptions,
TEncoding, TEncoding,
TFileMode, TFileMode,
} from '@push.rocks/smartfs'; } from '@push.rocks/smartfs';
``` ```
## Testing
```bash
# Run all tests
pnpm test
# Run specific test
pnpm tstest test/test.memory.provider.ts --verbose
# Run with log output
pnpm tstest test/test.node.provider.ts --logfile .nogit/testlogs/test.log
```
## Error Handling ## Error Handling
SmartFS throws descriptive errors: SmartFS throws descriptive errors:
@@ -431,22 +459,25 @@ try {
3. **Use memory provider** for testing 3. **Use memory provider** for testing
4. **Enable atomic writes** for critical data 4. **Enable atomic writes** for critical data
5. **Debounce watchers** to reduce event spam 5. **Debounce watchers** to reduce event spam
6. **Use treeHash** instead of reading files for change detection
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 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. **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 ### Trademarks
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 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, and any usage must be approved in writing by Task Venture Capital GmbH. 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.
### Company Information ### Company Information
Task Venture Capital GmbH Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. For any legal inquiries or further information, please contact us via email at hello@task.vc.
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. 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.

View File

@@ -256,6 +256,117 @@ tap.test('should handle file watching', async () => {
}); });
}); });
// --- treeHash tests ---
tap.test('should compute treeHash for a directory', async () => {
const dirPath = path.join(tempDir, 'hash-test');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'file1.txt')).write('content1');
await smartFs.file(path.join(dirPath, 'file2.txt')).write('content2');
const hash = await smartFs.directory(dirPath).treeHash();
expect(hash).toBeTruthy();
expect(typeof hash).toEqual('string');
expect(hash.length).toEqual(64); // SHA-256 produces 64 hex chars
});
tap.test('treeHash should be deterministic (same content = same hash)', async () => {
const dirPath = path.join(tempDir, 'hash-deterministic');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'a.txt')).write('aaa');
await smartFs.file(path.join(dirPath, 'b.txt')).write('bbb');
const hash1 = await smartFs.directory(dirPath).treeHash();
const hash2 = await smartFs.directory(dirPath).treeHash();
expect(hash1).toEqual(hash2);
});
tap.test('treeHash should change when file content changes', async () => {
const dirPath = path.join(tempDir, 'hash-content-change');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'file.txt')).write('original');
const hashBefore = await smartFs.directory(dirPath).treeHash();
await smartFs.file(path.join(dirPath, 'file.txt')).write('modified');
const hashAfter = await smartFs.directory(dirPath).treeHash();
expect(hashBefore).not.toEqual(hashAfter);
});
tap.test('treeHash should change when file is added', async () => {
const dirPath = path.join(tempDir, 'hash-file-add');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'file1.txt')).write('content1');
const hashBefore = await smartFs.directory(dirPath).treeHash();
await smartFs.file(path.join(dirPath, 'file2.txt')).write('content2');
const hashAfter = await smartFs.directory(dirPath).treeHash();
expect(hashBefore).not.toEqual(hashAfter);
});
tap.test('treeHash should work recursively', async () => {
const dirPath = path.join(tempDir, 'hash-recursive');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'root.txt')).write('root');
await smartFs.directory(path.join(dirPath, 'sub')).create();
await smartFs.file(path.join(dirPath, 'sub', 'nested.txt')).write('nested');
// Non-recursive should only include root.txt
const hashNonRecursive = await smartFs.directory(dirPath).treeHash();
// Recursive should include both files
const hashRecursive = await smartFs.directory(dirPath).recursive().treeHash();
expect(hashNonRecursive).not.toEqual(hashRecursive);
});
tap.test('treeHash should respect filter', async () => {
const dirPath = path.join(tempDir, 'hash-filter');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'file.ts')).write('typescript');
await smartFs.file(path.join(dirPath, 'file.js')).write('javascript');
// Hash only .ts files
const hashTs = await smartFs.directory(dirPath).filter(/\.ts$/).treeHash();
// Hash only .js files
const hashJs = await smartFs.directory(dirPath).filter(/\.js$/).treeHash();
// Should be different since they're hashing different files
expect(hashTs).not.toEqual(hashJs);
});
tap.test('treeHash should support different algorithms', async () => {
const dirPath = path.join(tempDir, 'hash-algorithm');
await smartFs.directory(dirPath).create();
await smartFs.file(path.join(dirPath, 'file.txt')).write('test content');
const sha256 = await smartFs.directory(dirPath).treeHash({ algorithm: 'sha256' });
const sha512 = await smartFs.directory(dirPath).treeHash({ algorithm: 'sha512' });
expect(sha256.length).toEqual(64); // SHA-256 = 64 hex chars
expect(sha512.length).toEqual(128); // SHA-512 = 128 hex chars
expect(sha256).not.toEqual(sha512);
});
tap.test('treeHash of empty directory should return consistent hash', async () => {
const dirPath = path.join(tempDir, 'hash-empty');
await smartFs.directory(dirPath).create();
const hash1 = await smartFs.directory(dirPath).treeHash();
const hash2 = await smartFs.directory(dirPath).treeHash();
expect(hash1).toEqual(hash2);
expect(hash1.length).toEqual(64);
});
tap.test('cleanup temp directory', async () => { tap.test('cleanup temp directory', async () => {
await fs.rm(tempDir, { recursive: true, force: true }); await fs.rm(tempDir, { recursive: true, force: true });
expect(true).toEqual(true); expect(true).toEqual(true);

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfs', name: '@push.rocks/smartfs',
version: '1.1.3', version: '1.2.0',
description: 'a cross platform extendable fs module' description: 'a cross platform extendable fs module'
} }

View File

@@ -2,12 +2,14 @@
* Directory builder for fluent directory operations * Directory builder for fluent directory operations
*/ */
import * as crypto from 'crypto';
import type { ISmartFsProvider } from '../interfaces/mod.provider.js'; import type { ISmartFsProvider } from '../interfaces/mod.provider.js';
import type { import type {
TFileMode, TFileMode,
IFileStats, IFileStats,
IDirectoryEntry, IDirectoryEntry,
IListOptions, IListOptions,
ITreeHashOptions,
} from '../interfaces/mod.types.js'; } from '../interfaces/mod.types.js';
/** /**
@@ -136,4 +138,55 @@ export class SmartFsDirectory {
public getPath(): string { public getPath(): string {
return this.path; return this.path;
} }
/**
* Compute a hash of all files in the directory tree
* Uses configured filter and recursive options
* @param options - Hash options (algorithm defaults to 'sha256')
* @returns Hex-encoded hash string
*
* @example
* ```typescript
* // Hash all files recursively
* const hash = await fs.directory('/assets').recursive().treeHash();
*
* // Hash only TypeScript files
* const hash = await fs.directory('/src').filter('*.ts').recursive().treeHash();
*
* // Use different algorithm
* const hash = await fs.directory('/data').recursive().treeHash({ algorithm: 'sha512' });
* ```
*/
public async treeHash(options?: ITreeHashOptions): Promise<string> {
const { algorithm = 'sha256' } = options ?? {};
const hash = crypto.createHash(algorithm);
// Get all entries using existing filter/recursive configuration
const entries = await this.list();
// Filter to files only and sort by path for deterministic ordering
const files = entries
.filter((entry) => entry.isFile)
.sort((a, b) => a.path.localeCompare(b.path));
// Hash each file's relative path and contents
for (const file of files) {
// Compute relative path from directory root
const relativePath = file.path.slice(this.path.length + 1);
// Hash the relative path (with null separator)
hash.update(relativePath + '\0');
// Stream file contents and update hash incrementally
const stream = await this.provider.createReadStream(file.path);
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
hash.update(value);
}
}
return hash.digest('hex');
}
} }

View File

@@ -215,3 +215,13 @@ export interface IWatchOptions {
filter?: string | RegExp | ((path: string) => boolean); filter?: string | RegExp | ((path: string) => boolean);
debounce?: number; debounce?: number;
} }
/**
* Tree hash options interface
*/
export interface ITreeHashOptions {
/**
* Hash algorithm to use (default: 'sha256')
*/
algorithm?: string;
}