fix(core): update

This commit is contained in:
Philipp Kunz 2023-11-04 20:54:13 +01:00
parent f7e47ae354
commit f815457801
2 changed files with 12 additions and 12 deletions

View File

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

View File

@ -3,7 +3,7 @@ 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';
type StreamSource = () => Promise<Readable>; type TStreamSource = (streamFile: StreamFile) => Promise<Readable>;
/** /**
* The StreamFile class represents a file as a stream. * The StreamFile class represents a file as a stream.
@ -12,14 +12,14 @@ type StreamSource = () => Promise<Readable>;
export class StreamFile { export class StreamFile {
// INSTANCE // INSTANCE
relativeFilePath?: string; relativeFilePath?: string;
private streamSource: StreamSource; private streamSource: TStreamSource;
// enable stream based multi use // enable stream based multi use
private cachedStreamBuffer?: Buffer; private cachedStreamBuffer?: Buffer;
public multiUse: boolean; public multiUse: boolean;
public used: boolean = false; public used: boolean = false;
private constructor(streamSource: StreamSource, relativeFilePath?: string) { private constructor(streamSource: TStreamSource, relativeFilePath?: string) {
this.streamSource = streamSource; this.streamSource = streamSource;
this.relativeFilePath = relativeFilePath; this.relativeFilePath = relativeFilePath;
} }
@ -27,25 +27,25 @@ export class StreamFile {
// STATIC // STATIC
public static async fromPath(filePath: string): Promise<StreamFile> { public static async fromPath(filePath: string): Promise<StreamFile> {
const streamSource = () => Promise.resolve(smartfileFsStream.createReadStream(filePath)); const streamSource: TStreamSource = async (stremFileArg) => smartfileFsStream.createReadStream(filePath);
const streamFile = new StreamFile(streamSource, filePath); const streamFile = new StreamFile(streamSource, filePath);
streamFile.multiUse = true; streamFile.multiUse = true;
return streamFile; return streamFile;
} }
public static async fromUrl(url: string): Promise<StreamFile> { public static async fromUrl(url: string): Promise<StreamFile> {
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); const streamFile = new StreamFile(streamSource);
streamFile.multiUse = true; streamFile.multiUse = true;
return streamFile; return streamFile;
} }
public static fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile { public static fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile {
const streamSource = () => { const streamSource: TStreamSource = async (streamFileArg) => {
const stream = new Readable(); const stream = new Readable();
stream.push(buffer); stream.push(buffer);
stream.push(null); // End of stream stream.push(null); // End of stream
return Promise.resolve(stream); return stream;
}; };
const streamFile = new StreamFile(streamSource, relativeFilePath); const streamFile = new StreamFile(streamSource, relativeFilePath);
streamFile.multiUse = true; streamFile.multiUse = true;
@ -60,11 +60,11 @@ export class StreamFile {
* @returns A StreamFile instance. * @returns A StreamFile instance.
*/ */
public static fromStream(stream: Readable, relativeFilePath?: string, multiUse: boolean = false): StreamFile { public static fromStream(stream: Readable, relativeFilePath?: string, multiUse: boolean = false): StreamFile {
const streamSource = function() { const streamSource: TStreamSource = (streamFileArg) => {
if (this.multiUse) { if (streamFileArg.multiUse) {
// If multi-use is enabled and we have cached content, create a new readable stream from the buffer // If multi-use is enabled and we have cached content, create a new readable stream from the buffer
const bufferedStream = new Readable(); const bufferedStream = new Readable();
bufferedStream.push(this.cachedStreamBuffer); bufferedStream.push(streamFileArg.cachedStreamBuffer);
bufferedStream.push(null); // No more data to push bufferedStream.push(null); // No more data to push
return Promise.resolve(bufferedStream); return Promise.resolve(bufferedStream);
} else { } else {
@ -104,7 +104,7 @@ export class StreamFile {
* Creates a new readable stream from the source. * Creates a new readable stream from the source.
*/ */
public async createReadStream(): Promise<Readable> { public async createReadStream(): Promise<Readable> {
return this.streamSource(); return this.streamSource(this);
} }
/** /**