Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
bf52f01365 | |||
14adec5ba3 | |||
fb2880c995 | |||
0e0ae7e42f | |||
1391dbddbc | |||
477736da82 |
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@push.rocks/smartfile",
|
||||
"private": false,
|
||||
"version": "11.0.17",
|
||||
"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.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartfile',
|
||||
version: '11.0.17',
|
||||
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.'
|
||||
}
|
||||
|
@ -306,4 +306,11 @@ export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
stream.push(null); // Push null to signify the end of the stream (EOF)
|
||||
return stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the file in bytes
|
||||
*/
|
||||
public async getSize(): Promise<number> {
|
||||
return this.contentBuffer.length;
|
||||
}
|
||||
}
|
||||
|
@ -13,9 +13,13 @@ export class StreamFile {
|
||||
// STATIC
|
||||
|
||||
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);
|
||||
streamFile.multiUse = true;
|
||||
streamFile.byteLengthComputeFunction = async () => {
|
||||
const stats = await smartfileFs.stat(filePath);
|
||||
return stats.size;
|
||||
}
|
||||
return streamFile;
|
||||
}
|
||||
|
||||
@ -23,6 +27,10 @@ export class StreamFile {
|
||||
const streamSource: TStreamSource = async (streamFileArg) => plugins.smartrequest.getStream(url); // Replace with actual plugin method
|
||||
const streamFile = new StreamFile(streamSource);
|
||||
streamFile.multiUse = true;
|
||||
streamFile.byteLengthComputeFunction = async () => {
|
||||
const response = await plugins.smartrequest.getBinary(url); // TODO: switch to future .getBinaryByteLength()
|
||||
return response.body.length;
|
||||
}
|
||||
return streamFile;
|
||||
}
|
||||
|
||||
@ -35,6 +43,7 @@ export class StreamFile {
|
||||
};
|
||||
const streamFile = new StreamFile(streamSource, relativeFilePath);
|
||||
streamFile.multiUse = true;
|
||||
streamFile.byteLengthComputeFunction = async () => buffer.length;
|
||||
return streamFile;
|
||||
}
|
||||
|
||||
@ -86,6 +95,7 @@ export class StreamFile {
|
||||
private cachedStreamBuffer?: Buffer;
|
||||
public multiUse: boolean;
|
||||
public used: boolean = false;
|
||||
public byteLengthComputeFunction: () => Promise<number>;
|
||||
|
||||
private constructor(streamSource: TStreamSource, relativeFilePath?: string) {
|
||||
this.streamSource = streamSource;
|
||||
@ -150,4 +160,15 @@ export class StreamFile {
|
||||
const contentBuffer = await this.getContentAsBuffer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
7
ts/fs.ts
7
ts/fs.ts
@ -486,7 +486,6 @@ export let toFs = async (
|
||||
return await done.promise;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const stat = async (filePathArg: string) => {
|
||||
return plugins.fsPromises.stat(filePathArg);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
// node native scope
|
||||
import * as fs from 'fs';
|
||||
import * as fsPromises from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import * as stream from 'stream';
|
||||
|
||||
export { fs, path, stream };
|
||||
export { fs, fsPromises, path, stream };
|
||||
|
||||
// @pushrocks scope
|
||||
import * as lik from '@push.rocks/lik';
|
||||
|
Reference in New Issue
Block a user