diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e6e4fa9..82d6530 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '10.0.39', + version: '10.0.40', description: 'offers smart ways to work with files in nodejs' } diff --git a/ts/classes.streamfile.ts b/ts/classes.streamfile.ts index e5f9bc9..15ccce9 100644 --- a/ts/classes.streamfile.ts +++ b/ts/classes.streamfile.ts @@ -3,7 +3,7 @@ import * as smartfileFs from './fs.js'; import * as smartfileFsStream from './fsstream.js'; import { Readable } from 'stream'; -type StreamSource = () => Promise; +type TStreamSource = (streamFile: StreamFile) => Promise; /** * The StreamFile class represents a file as a stream. @@ -12,14 +12,14 @@ type StreamSource = () => Promise; export class StreamFile { // INSTANCE relativeFilePath?: string; - private streamSource: StreamSource; + private streamSource: TStreamSource; // enable stream based multi use private cachedStreamBuffer?: Buffer; public multiUse: boolean; public used: boolean = false; - private constructor(streamSource: StreamSource, relativeFilePath?: string) { + private constructor(streamSource: TStreamSource, relativeFilePath?: string) { this.streamSource = streamSource; this.relativeFilePath = relativeFilePath; } @@ -27,25 +27,25 @@ export class StreamFile { // STATIC public static async fromPath(filePath: string): Promise { - const streamSource = () => Promise.resolve(smartfileFsStream.createReadStream(filePath)); + const streamSource: TStreamSource = async (stremFileArg) => smartfileFsStream.createReadStream(filePath); const streamFile = new StreamFile(streamSource, filePath); streamFile.multiUse = true; return streamFile; } public static async fromUrl(url: string): Promise { - const streamSource = async () => 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); streamFile.multiUse = true; return streamFile; } public static fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile { - const streamSource = () => { + const streamSource: TStreamSource = async (streamFileArg) => { const stream = new Readable(); stream.push(buffer); stream.push(null); // End of stream - return Promise.resolve(stream); + return stream; }; const streamFile = new StreamFile(streamSource, relativeFilePath); streamFile.multiUse = true; @@ -60,11 +60,11 @@ export class StreamFile { * @returns A StreamFile instance. */ public static fromStream(stream: Readable, relativeFilePath?: string, multiUse: boolean = false): StreamFile { - const streamSource = function() { - if (this.multiUse) { + const streamSource: TStreamSource = (streamFileArg) => { + if (streamFileArg.multiUse) { // If multi-use is enabled and we have cached content, create a new readable stream from the buffer const bufferedStream = new Readable(); - bufferedStream.push(this.cachedStreamBuffer); + bufferedStream.push(streamFileArg.cachedStreamBuffer); bufferedStream.push(null); // No more data to push return Promise.resolve(bufferedStream); } else { @@ -104,7 +104,7 @@ export class StreamFile { * Creates a new readable stream from the source. */ public async createReadStream(): Promise { - return this.streamSource(); + return this.streamSource(this); } /**