fix(core): update

This commit is contained in:
Philipp Kunz 2024-04-02 21:37:31 +02:00
parent 915ad00801
commit a1d6c37f18
4 changed files with 90 additions and 122 deletions

View File

@ -10,14 +10,33 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartfile", "gitrepo": "smartfile",
"description": "smart ways to work with files in nodejs", "description": "provides a robust suite of tools for managing files in Node.js using TypeScript.",
"npmPackagename": "@push.rocks/smartfile", "npmPackagename": "@push.rocks/smartfile",
"license": "MIT" "license": "MIT",
"keywords": [
"files management",
"TypeScript",
"Node.js",
"read files",
"write files",
"copy files",
"file streaming",
"directories manipulation",
"virtual file system",
"filesystem utilities",
"ESM syntax",
"memory-efficient streaming"
]
} }
}, },
"tsdoc": { "tsdoc": {
"classes": ["SmartFile", "StreamFile"], "classes": [
"descriptions": ["the purpose of the StreamFile class is to provide a hybrid interface between streaming files and simple handling when writing and reading those files multiple times."], "SmartFile",
"StreamFile"
],
"descriptions": [
"the purpose of the StreamFile class is to provide a hybrid interface between streaming files and simple handling when writing and reading those files multiple times."
],
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n" "legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
} }
} }

View File

@ -2,7 +2,7 @@
"name": "@push.rocks/smartfile", "name": "@push.rocks/smartfile",
"private": false, "private": false,
"version": "11.0.7", "version": "11.0.7",
"description": "offers smart ways to work with files in nodejs", "description": "provides a robust suite of tools for managing files in Node.js using TypeScript.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -16,8 +16,18 @@
"url": "git+https://gitlab.com/push.rocks/smartfile.git" "url": "git+https://gitlab.com/push.rocks/smartfile.git"
}, },
"keywords": [ "keywords": [
"filesystem", "files management",
"json" "TypeScript",
"Node.js",
"read files",
"write files",
"copy files",
"file streaming",
"directories manipulation",
"virtual file system",
"filesystem utilities",
"ESM syntax",
"memory-efficient streaming"
], ],
"author": "Lossless GmbH <hello@lossless.com> (https://lossless.com)", "author": "Lossless GmbH <hello@lossless.com> (https://lossless.com)",
"license": "MIT", "license": "MIT",

163
readme.md
View File

@ -4,7 +4,7 @@
## Install ## Install
To install `@push.rocks/smartfile`, run the following command in your project directory: To integrate `@push.rocks/smartfile` into your project, run:
```bash ```bash
npm install @push.rocks/smartfile npm install @push.rocks/smartfile
@ -12,148 +12,87 @@ npm install @push.rocks/smartfile
## Usage ## Usage
The `@push.rocks/smartfile` library offers a comprehensive suite of tools to work with files in Node.js projects, facilitating a variety of file operations such as reading, writing, streaming, and in-memory manipulation. Below, you'll find detailed examples illustrating how to leverage the library's functionality to tackle real-world tasks. `@push.rocks/smartfile` provides a robust suite of tools for managing files in Node.js projects using modern TypeScript and ESM syntax. It simplifies numerous file operations such as reading, writing, copying, and streaming files, as well as working with directories and virtual file systems.
### Working with Filesystem ### **Key Features and Classes**
#### Ensuring File and Directory Existence - **`SmartFile`**: Facilitates reading from and writing to individual files, managing metadata.
- **`StreamFile`**: Optimizes memory usage by enabling efficient file streaming.
- **`VirtualDirectory`**: Allows manipulation of a group of files or directories as a virtual file system.
### **Getting Started with ESM and TypeScript**
First, ensure your project supports ESM syntax and TypeScript. Then, begin by importing the desired features from `@push.rocks/smartfile`:
```typescript ```typescript
import { fs } from '@push.rocks/smartfile'; import { SmartFile, StreamFile, VirtualDirectory, memory, fs as smartFs } from '@push.rocks/smartfile';
// Ensure a file exists with initial content (Async)
await fs.ensureFile('./path/to/new/file.txt', 'Initial content');
// Ensure a directory exists (Async)
await fs.ensureDir('./path/to/new/dir');
``` ```
#### Getting File Details ### **Reading and Writing Files**
#### Reading Files
Reading a JSON file:
```typescript ```typescript
import { fs } from '@push.rocks/smartfile'; const myJsonFile: SmartFile = await SmartFile.fromFilePath('./data.json');
const jsonData = JSON.parse(myJsonFile.contents.toString());
// Check if a file exists (Async) console.log(jsonData);
const exists: boolean = await fs.fileExists('./path/to/file.txt');
// Check if a path is a directory
const isDir: boolean = fs.isDirectory('./path/to/check');
``` ```
### Reading and Writing Files #### Writing Files
#### Basic Reading and Writing Writing content to a file:
```typescript ```typescript
import { SmartFile, memory } from '@push.rocks/smartfile'; const filePath: string = './output.txt';
const content: string = 'Hello, SmartFile!';
// Read a file to a SmartFile instance (Async) await memory.toFs(content, filePath);
const smartFile: SmartFile = await SmartFile.fromFilePath('./path/to/read/file.txt'); console.log('File saved successfully.');
// Write SmartFile instance to a new path (Async)
await smartFile.writeToDir('./path/to/target/dir');
// Write text content to a file (Async)
await memory.toFs('Hello SmartFile!', './path/to/output/file.txt');
``` ```
#### Working with File Contents ### **Streaming Large Files**
For large files, `StreamFile` provides a memory-efficient streaming solution:
```typescript ```typescript
import { SmartFile } from '@push.rocks/smartfile'; import { createReadStream } from 'fs';
// Creating SmartFile instance from text const sourceStream = createReadStream('./large-video.mp4');
const smartFileFromText: SmartFile = await SmartFile.fromString('./path/to/text/file.txt', 'Text content here', 'utf8'); const myStreamFile = await StreamFile.fromStream(sourceStream, 'large-video.mp4');
await myStreamFile.writeToDir('./storage');
// Reading content from SmartFile instance console.log('Large file streamed to disk successfully.');
const content: string = smartFileFromText.contents.toString();
// Editing content of SmartFile
await smartFileFromText.editContentAsString(async (currentContent: string) => `Modified: ${currentContent}`);
``` ```
### File Streaming ### **Working with Virtual Directories**
`VirtualDirectory` abstracts a collection of files allowing operations to be performed as if they were on disk:
```typescript ```typescript
import { StreamFile } from '@push.rocks/smartfile'; const virtualDir = new VirtualDirectory();
virtualDir.addSmartfiles([smartFile1, smartFile2]); // Assuming these are SmartFile instances
// Creating a StreamFile from a file path (Async) await virtualDir.saveToDisk('./virtual-output');
const streamFile: StreamFile = await StreamFile.fromPath('./path/to/large/file.avi'); console.log('Virtual directory saved to disk.');
// Streaming file content to disk (Async)
await streamFile.writeToDisk('./path/to/target/file.avi');
``` ```
#### Utilizing Stream Files from URLs ### **Advanced File Operations**
```typescript `@push.rocks/smartfile` simplifies complex file operations, including:
import { StreamFile } from '@push.rocks/smartfile';
// Creating a StreamFile from URL (Async) - Copying directories and files
const streamFileFromURL: StreamFile = await StreamFile.fromUrl('https://example.com/data.json'); - Removing files or directories
- Listing files and directories with filters
- Reading file content directly into JavaScript objects
// Accessing StreamFile content as string (Async, for textual data) ### **Web File Handling**
const contentAsString: string = await streamFileFromURL.getContentAsString();
```
### Handling Virtual Directories Handling files from HTTP requests:
```typescript `@push.rocks/smartfile` offers utilities to work with files from web sources, making it simpler to manage downloads and uploads.
import { VirtualDirectory, SmartFile } from '@push.rocks/smartfile';
// Creating a virtual directory from existing directory on disk (Async) ### **Comprehensive File Management**
const virtualDirectory: VirtualDirectory = await VirtualDirectory.fromFsDirPath('./path/to/source/dir');
// Adding files to virtual directory Whether you're dealing with local files, directories, or files over the internet, `@push.rocks/smartfile` provides a comprehensive set of tools to streamline your workflow and reduce the complexity of file management in your Node.js projects.
virtualDirectory.addSmartfiles([await SmartFile.fromFilePath('./path/to/extra/file.txt')]);
// Saving virtual directory to disk (Async)
await virtualDirectory.saveToDisk('./path/to/destination/dir');
```
#### Manipulating File Paths
```typescript
import { SmartFile } from '@push.rocks/smartfile';
// Read SmartFile and change its relative path
const smartFile: SmartFile = await SmartFile.fromFilePath('./path/to/original/file.txt');
smartFile.updateFileName('newFilename.txt'); // Retains original directory but changes file name
```
### Security and Hashing
```typescript
import { SmartFile } from '@push.rocks/smartfile';
// Creating SmartFile instance and getting hash
const smartFileForHash: SmartFile = await SmartFile.fromString('./fileForHashing.txt', 'Content for hashing', 'utf8');
const hash: string = await smartFileForHash.getHash('content'); // 'content', 'path', or 'all'
```
### Advanced Streaming via `SmartReadStream`
For handling edge cases such as streaming large files that are being written concurrently (e.g., log files), `SmartReadStream` can be employed to monitor files for new data and stream content as it becomes available.
```typescript
import { fsStream } from '@push.rocks/smartfile';
const smartReadStream = new fsStream.SmartReadStream('./path/to/live/file.log');
smartReadStream
.on('data', (chunk) => {
// Process streamed data
})
.on('error', (err) => {
console.error('Stream encountered an error:', err);
})
.on('end', () => {
console.log('No more data available.');
});
```
In conclusion, `@push.rocks/smartfile` equips developers with a highly versatile API for file manipulation, enhancing productivity in handling file operations within Node.js applications. By leveraging the examples provided, users can efficiently integrate file processing tasks into their projects, streamlining workflows and achieving granular control over file and directory management.
For more details and advanced use cases, please refer to the `@push.rocks/smartfile` documentation and explore the source code to unlock the full potential of this powerful library in your Node.js applications.
## License and Legal Information ## License and Legal Information

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.0.7', version: '11.0.8',
description: 'offers smart ways to work with files in nodejs' description: 'provides a robust suite of tools for managing files in Node.js using TypeScript.'
} }