fix(core): update
This commit is contained in:
parent
f7e47ae354
commit
f815457801
@ -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'
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import * as smartfileFs from './fs.js';
|
||||
import * as smartfileFsStream from './fsstream.js';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
type StreamSource = () => Promise<Readable>;
|
||||
type TStreamSource = (streamFile: StreamFile) => Promise<Readable>;
|
||||
|
||||
/**
|
||||
* The StreamFile class represents a file as a stream.
|
||||
@ -12,14 +12,14 @@ type StreamSource = () => Promise<Readable>;
|
||||
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<StreamFile> {
|
||||
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<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);
|
||||
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<Readable> {
|
||||
return this.streamSource();
|
||||
return this.streamSource(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user