diff --git a/changelog.md b/changelog.md index 3e724ad..7fc1b24 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,14 @@ # 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) Default createDirectory to recursive=true when option not provided in Node provider diff --git a/readme.md b/readme.md index 800c367..1207fc9 100644 --- a/readme.md +++ b/readme.md @@ -4,23 +4,26 @@ Modern, pluggable filesystem module with fluent API, Web Streams support, and mu ## 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 -- **🎯 Fluent API** - Action-last chainable interface for elegant code -- **🔌 Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.) -- **🌊 Web Streams** - Modern streaming with Web Streams API -- **💾 Transactions** - Atomic multi-file operations with automatic rollback -- **👀 File Watching** - Event-based file system monitoring -- **⚡ Async-Only** - Modern async/await patterns throughout -- **📦 Zero Dependencies** - Core functionality with minimal dependencies -- **🎨 TypeScript** - Full type safety and IntelliSense support +- 🎯 **Fluent API** - Action-last chainable interface for elegant code +- 🔌 **Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.) +- 🌊 **Web Streams** - Modern streaming with Web Streams API +- 💾 **Transactions** - Atomic multi-file operations with automatic rollback +- 👀 **File Watching** - Event-based file system monitoring +- 🔐 **Tree Hashing** - SHA-256 directory hashing for cache-busting +- ⚡ **Async-Only** - Modern async/await patterns throughout +- 📦 **Zero Dependencies** - Core functionality with minimal footprint +- 🎨 **TypeScript** - Full type safety and IntelliSense support ## Installation ```bash -pnpm install @push.rocks/smartfs +npm install @push.rocks/smartfs +# or +pnpm add @push.rocks/smartfs ``` ## Quick Start @@ -132,6 +135,41 @@ await fs.directory('/path/to/dir') 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 SmartFS uses **Web Streams API** for efficient handling of large files: @@ -247,6 +285,7 @@ const fs = new SmartFs(new SmartFsProviderNode()); - ✅ Streaming - ✅ Symbolic links - ✅ File permissions +- ✅ Tree hashing ### Memory Provider @@ -272,6 +311,7 @@ fs.provider.clear(); - ✅ Streaming - ❌ Symbolic links - ✅ File permissions +- ✅ Tree hashing ### Custom Providers @@ -382,24 +422,12 @@ import type { IDirectoryEntry, IWatchEvent, ITransactionOperation, + ITreeHashOptions, TEncoding, TFileMode, } 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 SmartFS throws descriptive errors: @@ -431,22 +459,25 @@ try { 3. **Use memory provider** for testing 4. **Enable atomic writes** for critical data 5. **Debounce watchers** to reduce event spam +6. **Use treeHash** instead of reading files for change detection ## 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. ### 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 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. diff --git a/test/test.node.provider.ts b/test/test.node.provider.ts index 9d29b4d..cf5f230 100644 --- a/test/test.node.provider.ts +++ b/test/test.node.provider.ts @@ -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 () => { await fs.rm(tempDir, { recursive: true, force: true }); expect(true).toEqual(true); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index a16ca04..36a7976 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfs', - version: '1.1.3', + version: '1.2.0', description: 'a cross platform extendable fs module' } diff --git a/ts/classes/smartfs.directory.ts b/ts/classes/smartfs.directory.ts index 9587495..118d25d 100644 --- a/ts/classes/smartfs.directory.ts +++ b/ts/classes/smartfs.directory.ts @@ -2,12 +2,14 @@ * Directory builder for fluent directory operations */ +import * as crypto from 'crypto'; import type { ISmartFsProvider } from '../interfaces/mod.provider.js'; import type { TFileMode, IFileStats, IDirectoryEntry, IListOptions, + ITreeHashOptions, } from '../interfaces/mod.types.js'; /** @@ -136,4 +138,55 @@ export class SmartFsDirectory { public getPath(): string { 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 { + 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'); + } } diff --git a/ts/interfaces/mod.types.ts b/ts/interfaces/mod.types.ts index 04af6a5..fa60704 100644 --- a/ts/interfaces/mod.types.ts +++ b/ts/interfaces/mod.types.ts @@ -215,3 +215,13 @@ export interface IWatchOptions { filter?: string | RegExp | ((path: string) => boolean); debounce?: number; } + +/** + * Tree hash options interface + */ +export interface ITreeHashOptions { + /** + * Hash algorithm to use (default: 'sha256') + */ + algorithm?: string; +}