feat(smartfs.directory): Add directory treeHash: deterministic content-based hashing of directory trees with streaming and algorithm option
This commit is contained in:
85
readme.md
85
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.
|
||||
|
||||
Reference in New Issue
Block a user