Compare commits

..

15 Commits

Author SHA1 Message Date
915ad00801 11.0.7 2024-04-02 20:58:34 +02:00
910671bfc6 fix(core): update 2024-04-02 20:58:33 +02:00
ae8835d430 11.0.6 2024-04-02 20:53:03 +02:00
d08cc0f350 fix(core): update 2024-04-02 20:53:02 +02:00
1311039127 update npmextra.json: githost 2024-04-01 21:35:01 +02:00
c267d2f226 update npmextra.json: githost 2024-04-01 19:58:14 +02:00
5a9e1b5798 11.0.5 2024-04-01 17:46:40 +02:00
b1ec86ee2d fix(core): update 2024-04-01 17:46:40 +02:00
a1353170f6 update npmextra.json: githost 2024-03-30 21:47:12 +01:00
3ff6de201d 11.0.4 2023-11-24 19:29:00 +01:00
f5c106b2ca fix(core): update 2023-11-24 19:28:59 +01:00
d3c26d0d46 11.0.3 2023-11-24 19:15:41 +01:00
9935fe2d3c fix(core): update 2023-11-24 19:15:41 +01:00
3b05aab39b 11.0.2 2023-11-07 21:32:00 +01:00
53be2eb59d fix(core): update 2023-11-07 21:32:00 +01:00
15 changed files with 719 additions and 376 deletions

View File

@ -7,12 +7,17 @@
"gitzone": { "gitzone": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartfile", "gitrepo": "smartfile",
"description": "smart ways to work with files in nodejs", "description": "smart ways to work with files in nodejs",
"npmPackagename": "@push.rocks/smartfile", "npmPackagename": "@push.rocks/smartfile",
"license": "MIT" "license": "MIT"
} }
},
"tsdoc": {
"classes": ["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"
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "@push.rocks/smartfile", "name": "@push.rocks/smartfile",
"private": false, "private": false,
"version": "11.0.1", "version": "11.0.7",
"description": "offers smart ways to work with files in nodejs", "description": "offers smart ways to work with files in nodejs",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
@ -26,7 +26,7 @@
}, },
"homepage": "https://gitlab.com/push.rocks/smartfile#readme", "homepage": "https://gitlab.com/push.rocks/smartfile#readme",
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.0.5", "@push.rocks/lik": "^6.0.12",
"@push.rocks/smartdelay": "^3.0.5", "@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfile-interfaces": "^1.0.7", "@push.rocks/smartfile-interfaces": "^1.0.7",
"@push.rocks/smarthash": "^3.0.4", "@push.rocks/smarthash": "^3.0.4",
@ -34,11 +34,11 @@
"@push.rocks/smartmime": "^1.0.5", "@push.rocks/smartmime": "^1.0.5",
"@push.rocks/smartpath": "^5.0.11", "@push.rocks/smartpath": "^5.0.11",
"@push.rocks/smartpromise": "^4.0.2", "@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartrequest": "^2.0.20", "@push.rocks/smartrequest": "^2.0.21",
"@push.rocks/smartstream": "^3.0.7", "@push.rocks/smartstream": "^3.0.30",
"@types/fs-extra": "^11.0.3", "@types/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0", "@types/glob": "^8.1.0",
"@types/js-yaml": "^4.0.8", "@types/js-yaml": "^4.0.9",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"glob": "^10.3.10", "glob": "^10.3.10",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
@ -46,9 +46,9 @@
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.70", "@git.zone/tsbuild": "^2.1.70",
"@git.zone/tsrun": "^1.2.46", "@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.81", "@git.zone/tstest": "^1.0.84",
"@push.rocks/tapbundle": "^5.0.15", "@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.10" "@types/node": "^20.10.0"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

736
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

196
readme.md
View File

@ -1,49 +1,175 @@
# @push.rocks/smartfile # @push.rocks/smartfile
smart ways to work with files in nodejs
## Availabililty and Links > offers smart ways to work with files in nodejs
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartfile)
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartfile)
* [github.com (source mirror)](https://github.com/push.rocks/smartfile)
* [docs (typedoc)](https://push.rocks.gitlab.io/smartfile/)
## Status for master ## Install
Status Category | Status Badge To install `@push.rocks/smartfile`, run the following command in your project directory:
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartfile/badges/master/pipeline.svg)](https://lossless.cloud) ```bash
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartfile/badges/master/coverage.svg)](https://lossless.cloud) npm install @push.rocks/smartfile
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartfile)](https://lossless.cloud) ```
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartfile)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartfile)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartfile)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartfile)](https://lossless.cloud)
## Usage ## Usage
smartfile is an approach of being one tool to handle files in diverse environments. 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.
### Smartfile Sections ### Working with Filesystem
smartfile thinks in sections: #### Ensuring File and Directory Existence
| section | description | ```typescript
| ----------- | ---------------------------------------------------------------------------- | import { fs } from '@push.rocks/smartfile';
| fs | (object) gets data from fs to somewhere |
| memory | gets data from memory to somewhere |
| remote | gets data from remote locations to somewhere |
| interpreter | (object) handles yaml and json |
| smartfile | (class) a virtual representation of a file, alternative to vinyl file format |
## Contribution // Ensure a file exists with initial content (Async)
await fs.ensureFile('./path/to/new/file.txt', 'Initial content');
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) // Ensure a directory exists (Async)
await fs.ensureDir('./path/to/new/dir');
```
For further information read the linked docs at the top of this readme. #### Getting File Details
## Legal ```typescript
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc) import { fs } from '@push.rocks/smartfile';
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
// Check if a file exists (Async)
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
#### Basic Reading and Writing
```typescript
import { SmartFile, memory } from '@push.rocks/smartfile';
// Read a file to a SmartFile instance (Async)
const smartFile: SmartFile = await SmartFile.fromFilePath('./path/to/read/file.txt');
// 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
```typescript
import { SmartFile } from '@push.rocks/smartfile';
// Creating SmartFile instance from text
const smartFileFromText: SmartFile = await SmartFile.fromString('./path/to/text/file.txt', 'Text content here', 'utf8');
// Reading content from SmartFile instance
const content: string = smartFileFromText.contents.toString();
// Editing content of SmartFile
await smartFileFromText.editContentAsString(async (currentContent: string) => `Modified: ${currentContent}`);
```
### File Streaming
```typescript
import { StreamFile } from '@push.rocks/smartfile';
// Creating a StreamFile from a file path (Async)
const streamFile: StreamFile = await StreamFile.fromPath('./path/to/large/file.avi');
// Streaming file content to disk (Async)
await streamFile.writeToDisk('./path/to/target/file.avi');
```
#### Utilizing Stream Files from URLs
```typescript
import { StreamFile } from '@push.rocks/smartfile';
// Creating a StreamFile from URL (Async)
const streamFileFromURL: StreamFile = await StreamFile.fromUrl('https://example.com/data.json');
// Accessing StreamFile content as string (Async, for textual data)
const contentAsString: string = await streamFileFromURL.getContentAsString();
```
### Handling Virtual Directories
```typescript
import { VirtualDirectory, SmartFile } from '@push.rocks/smartfile';
// Creating a virtual directory from existing directory on disk (Async)
const virtualDirectory: VirtualDirectory = await VirtualDirectory.fromFsDirPath('./path/to/source/dir');
// Adding files to virtual directory
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
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.
**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.
### Company Information
Task Venture Capital GmbH
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.
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

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.0.1', version: '11.0.7',
description: 'offers smart ways to work with files in nodejs' description: 'offers smart ways to work with files in nodejs'
} }

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as fs from './fs.js'; import * as fs from './fs.js';
import * as memory from './memory.js'; import * as memory from './memory.js';
@ -91,6 +91,12 @@ export class SmartFile extends plugins.smartjson.Smartjson {
}); });
} }
public static async fromUrl (urlArg: string) {
const response = await plugins.smartrequest.getBinary(urlArg);
const smartfile = await SmartFile.fromBuffer(urlArg, response.body);
return smartfile;
}
// ======== // ========
// INSTANCE // INSTANCE
// ======== // ========

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as smartfileFs from './fs.js'; import * as smartfileFs from './fs.js';
import * as smartfileFsStream from './fsstream.js'; import * as smartfileFsStream from './fsstream.js';
import { Readable } from 'stream'; import { Readable } from 'stream';

View File

@ -1,5 +1,5 @@
import { SmartFile } from './classes.smartfile.js'; import { SmartFile } from './classes.smartfile.js';
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as fs from './fs.js'; import * as fs from './fs.js';

105
ts/fs.ts
View File

@ -1,9 +1,10 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as interpreter from './interpreter.js'; import * as interpreter from './interpreter.js';
import { SmartFile } from './classes.smartfile.js'; import { SmartFile } from './classes.smartfile.js';
import * as memory from './memory.js'; import * as memory from './memory.js';
import type { StreamFile } from './classes.streamfile.js';
/*=============================================================== /*===============================================================
============================ Checks ============================= ============================ Checks =============================
===============================================================*/ ===============================================================*/
@ -392,49 +393,99 @@ export const listFileTree = async (
* Watches for file stability before resolving the promise. * Watches for file stability before resolving the promise.
*/ */
export const waitForFileToBeReady = (filePathArg: string): Promise<void> => { export const waitForFileToBeReady = (filePathArg: string): Promise<void> => {
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
let lastSize = -1; let lastFileSize = -1;
let stableCheckTimeout: NodeJS.Timeout | null = null; let fileIsStable = false;
const clearStableCheckTimeout = () => { const checkFileStability = async () => {
if (stableCheckTimeout) { let currentFileSize: number;
clearTimeout(stableCheckTimeout); const deferred = plugins.smartpromise.defer();
stableCheckTimeout = null; plugins.fs.stat(filePathArg, (err, stats) => {
if (err) {
fileIsStable = true;
watcher.close();
reject(err);
return;
}
currentFileSize = stats.size;
deferred.resolve();
});
await deferred.promise;
if (currentFileSize === lastFileSize) {
fileIsStable = true;
await plugins.smartdelay.delayFor(100);
resolve();
} }
lastFileSize = currentFileSize;
}; };
const watcher = plugins.fs.watch(filePathArg, (eventType, filename) => { const watcher = plugins.fs.watch(filePathArg, (eventType, filename) => {
if (eventType === 'change') { if (eventType === 'change') {
plugins.fs.stat(filePathArg, (err, stats) => { checkFileStability();
if (err) {
watcher.close();
clearStableCheckTimeout();
reject(err);
return;
}
if (stats.size === lastSize) {
clearStableCheckTimeout();
stableCheckTimeout = setTimeout(() => {
watcher.close();
resolve();
}, 5000); // stability duration
} else {
lastSize = stats.size;
}
});
} }
}); });
watcher.on('error', (error) => { watcher.on('error', (error) => {
clearStableCheckTimeout();
watcher.close(); watcher.close();
reject(error); reject(error);
}); });
while (!fileIsStable) {
await checkFileStability();
if (!fileIsStable) {
await plugins.smartdelay.delayFor(5000);
}
}
watcher.close();
}); });
}; };
/**
* writes string or Smartfile to disk.
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = async (
filePathArg: string,
fileContentArg: string | Buffer | SmartFile | StreamFile,
optionsArg: {
respectRelative?: boolean;
} = {}
) => {
const done = plugins.smartpromise.defer();
// check args
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments');
}
// prepare actual write action
let fileContent: string | Buffer;
let fileEncoding: 'utf8' | 'binary' = 'utf8';
let filePath: string = filePathArg;
// handle Smartfile
if (fileContentArg instanceof SmartFile) {
fileContent = fileContentArg.contentBuffer;
// handle options
if (optionsArg.respectRelative) {
filePath = plugins.path.join(filePath, fileContentArg.path);
}
} else if (Buffer.isBuffer(fileContentArg)) {
fileContent = fileContentArg;
fileEncoding = 'binary';
} else if (typeof fileContentArg === 'string') {
fileContent = fileContentArg;
} else {
throw new Error('fileContent is neither string nor Smartfile');
}
await ensureDir(plugins.path.parse(filePath).dir);
plugins.fsExtra.writeFile(filePath, fileContent, { encoding: fileEncoding }, done.resolve);
return await done.promise;
};

View File

@ -1,7 +1,7 @@
/* /*
This file contains logic for streaming things from and to the filesystem This file contains logic for streaming things from and to the filesystem
*/ */
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
export const createReadStream = (pathArg: string) => { export const createReadStream = (pathArg: string) => {
return plugins.fs.createReadStream(pathArg); return plugins.fs.createReadStream(pathArg);
@ -97,7 +97,7 @@ export const waitForFileToBeReadyForStreaming = (filePathArg: string): Promise<v
}); });
}; };
class SmartReadStream extends plugins.stream.Readable { export class SmartReadStream extends plugins.stream.Readable {
private watcher: plugins.fs.FSWatcher | null = null; private watcher: plugins.fs.FSWatcher | null = null;
private lastReadSize: number = 0; private lastReadSize: number = 0;
private endTimeout: NodeJS.Timeout | null = null; private endTimeout: NodeJS.Timeout | null = null;

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as fsMod from './fs.js'; import * as fsMod from './fs.js';
import * as fsStreamMod from './fsstream.js'; import * as fsStreamMod from './fsstream.js';
import * as interpreterMod from './interpreter.js'; import * as interpreterMod from './interpreter.js';

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
export let filetype = (pathArg: string): string => { export let filetype = (pathArg: string): string => {
const extName = plugins.path.extname(pathArg); const extName = plugins.path.extname(pathArg);

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import { SmartFile } from './classes.smartfile.js'; import { SmartFile } from './classes.smartfile.js';
import * as smartfileFs from './fs.js'; import * as smartfileFs from './fs.js';
import * as interpreter from './interpreter.js'; import * as interpreter from './interpreter.js';

View File

@ -3,9 +3,12 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"useDefineForClassFields": false, "useDefineForClassFields": false,
"target": "ES2022", "target": "ES2022",
"module": "ES2022", "module": "NodeNext",
"moduleResolution": "nodenext", "moduleResolution": "NodeNext",
"esModuleInterop": true, "esModuleInterop": true,
"verbatimModuleSyntax": true, "verbatimModuleSyntax": true
} },
"exclude": [
"dist_*/**/*.d.ts"
]
} }