Compare commits

...

9 Commits

Author SHA1 Message Date
bf52f01365 11.0.20 2024-06-07 17:13:08 +02:00
14adec5ba3 fix(core): update 2024-06-07 17:13:07 +02:00
fb2880c995 11.0.19 2024-06-07 12:24:54 +02:00
0e0ae7e42f fix(core): update 2024-06-07 12:24:53 +02:00
1391dbddbc 11.0.18 2024-06-06 23:33:36 +02:00
477736da82 fix(core): update 2024-06-06 23:33:35 +02:00
26a67d9662 11.0.17 2024-06-06 22:29:06 +02:00
14771fab27 fix(core): update 2024-06-06 22:29:06 +02:00
b0ae383622 update description 2024-05-29 14:13:08 +02:00
6 changed files with 56 additions and 26 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@push.rocks/smartfile", "name": "@push.rocks/smartfile",
"private": false, "private": false,
"version": "11.0.16", "version": "11.0.20",
"description": "Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.", "description": "Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
@ -13,7 +13,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://gitlab.com/push.rocks/smartfile.git" "url": "https://code.foss.global/push.rocks/smartfile.git"
}, },
"keywords": [ "keywords": [
"file management", "file management",
@ -40,7 +40,7 @@
"bugs": { "bugs": {
"url": "https://gitlab.com/push.rocks/smartfile/issues" "url": "https://gitlab.com/push.rocks/smartfile/issues"
}, },
"homepage": "https://gitlab.com/push.rocks/smartfile#readme", "homepage": "https://code.foss.global/push.rocks/smartfile",
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.0.15", "@push.rocks/lik": "^6.0.15",
"@push.rocks/smartdelay": "^3.0.5", "@push.rocks/smartdelay": "^3.0.5",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.0.16', version: '11.0.20',
description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.' description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.'
} }

View File

@ -306,4 +306,11 @@ export class SmartFile extends plugins.smartjson.Smartjson {
stream.push(null); // Push null to signify the end of the stream (EOF) stream.push(null); // Push null to signify the end of the stream (EOF)
return stream; return stream;
} }
/**
* Returns the size of the file in bytes
*/
public async getSize(): Promise<number> {
return this.contentBuffer.length;
}
} }

View File

@ -10,26 +10,16 @@ type TStreamSource = (streamFile: StreamFile) => Promise<Readable>;
* It allows creating streams from a file path, a URL, or a buffer. * It allows creating streams from a file path, a URL, or a buffer.
*/ */
export class StreamFile { export class StreamFile {
// INSTANCE
relativeFilePath?: string;
private streamSource: TStreamSource;
// enable stream based multi use
private cachedStreamBuffer?: Buffer;
public multiUse: boolean;
public used: boolean = false;
private constructor(streamSource: TStreamSource, relativeFilePath?: string) {
this.streamSource = streamSource;
this.relativeFilePath = relativeFilePath;
}
// STATIC // STATIC
public static async fromPath(filePath: string): Promise<StreamFile> { public static async fromPath(filePath: string): Promise<StreamFile> {
const streamSource: TStreamSource = async (stremFileArg) => smartfileFsStream.createReadStream(filePath); const streamSource: TStreamSource = async (streamFileArg) => smartfileFsStream.createReadStream(filePath);
const streamFile = new StreamFile(streamSource, filePath); const streamFile = new StreamFile(streamSource, filePath);
streamFile.multiUse = true; streamFile.multiUse = true;
streamFile.byteLengthComputeFunction = async () => {
const stats = await smartfileFs.stat(filePath);
return stats.size;
}
return streamFile; return streamFile;
} }
@ -37,6 +27,10 @@ export class StreamFile {
const streamSource: TStreamSource = async (streamFileArg) => plugins.smartrequest.getStream(url); // Replace with actual plugin method const streamSource: TStreamSource = async (streamFileArg) => plugins.smartrequest.getStream(url); // Replace with actual plugin method
const streamFile = new StreamFile(streamSource); const streamFile = new StreamFile(streamSource);
streamFile.multiUse = true; streamFile.multiUse = true;
streamFile.byteLengthComputeFunction = async () => {
const response = await plugins.smartrequest.getBinary(url); // TODO: switch to future .getBinaryByteLength()
return response.body.length;
}
return streamFile; return streamFile;
} }
@ -49,6 +43,7 @@ export class StreamFile {
}; };
const streamFile = new StreamFile(streamSource, relativeFilePath); const streamFile = new StreamFile(streamSource, relativeFilePath);
streamFile.multiUse = true; streamFile.multiUse = true;
streamFile.byteLengthComputeFunction = async () => buffer.length;
return streamFile; return streamFile;
} }
@ -91,6 +86,22 @@ export class StreamFile {
return streamFile; return streamFile;
} }
// INSTANCE
relativeFilePath?: string;
private streamSource: TStreamSource;
// enable stream based multi use
private cachedStreamBuffer?: Buffer;
public multiUse: boolean;
public used: boolean = false;
public byteLengthComputeFunction: () => Promise<number>;
private constructor(streamSource: TStreamSource, relativeFilePath?: string) {
this.streamSource = streamSource;
this.relativeFilePath = relativeFilePath;
}
// METHODS // METHODS
private checkMultiUse() { private checkMultiUse() {
@ -149,4 +160,15 @@ export class StreamFile {
const contentBuffer = await this.getContentAsBuffer(); const contentBuffer = await this.getContentAsBuffer();
return contentBuffer.toString(formatArg); return contentBuffer.toString(formatArg);
} }
/**
* Returns the size of the file content in bytes.
*/
public async getSize(): Promise<number> {
if (this.byteLengthComputeFunction) {
return this.byteLengthComputeFunction();
} else {
return null;
}
}
} }

View File

@ -190,7 +190,8 @@ export const removeManySync = (filePathArrayArg: string[]): void => {
===============================================================*/ ===============================================================*/
/** /**
* * reads a file content to an object
* good for JSON, YAML, TOML, etc.
* @param filePathArg * @param filePathArg
* @param fileTypeArg * @param fileTypeArg
* @returns {any} * @returns {any}
@ -485,7 +486,6 @@ export let toFs = async (
return await done.promise; return await done.promise;
}; };
export const stat = async (filePathArg: string) => {
return plugins.fsPromises.stat(filePathArg);
}

View File

@ -1,9 +1,10 @@
// node native scope // node native scope
import * as fs from 'fs'; import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
import * as path from 'path'; import * as path from 'path';
import * as stream from 'stream'; import * as stream from 'stream';
export { fs, path, stream }; export { fs, fsPromises, path, stream };
// @pushrocks scope // @pushrocks scope
import * as lik from '@push.rocks/lik'; import * as lik from '@push.rocks/lik';