fix(config): migrate project config and harden SmartFile and StreamFile defaults for updated toolchain compatibility
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartfile',
|
||||
version: '13.1.0',
|
||||
version: '13.1.3',
|
||||
description: 'High-level file representation classes (SmartFile, StreamFile, VirtualDirectory) for efficient in-memory file management in Node.js using TypeScript. Works seamlessly with @push.rocks/smartfs for filesystem operations.'
|
||||
}
|
||||
|
||||
+13
-10
@@ -23,7 +23,7 @@ export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
* the relative path of the file
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public path: string;
|
||||
public accessor path: string = '';
|
||||
|
||||
/**
|
||||
* a parsed path
|
||||
@@ -43,20 +43,20 @@ export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
* the content of the file as Buffer
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public contentBuffer: Buffer;
|
||||
public accessor contentBuffer: Buffer = Buffer.alloc(0);
|
||||
|
||||
/**
|
||||
* The current working directory of the file
|
||||
* Note:this is similar to gulp and different from native node path base
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public base: string;
|
||||
public accessor base: string = process.cwd();
|
||||
|
||||
/**
|
||||
* sync the file with disk
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public sync: boolean;
|
||||
public accessor sync: boolean = false;
|
||||
|
||||
/**
|
||||
* the constructor of Smartfile
|
||||
@@ -64,13 +64,16 @@ export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
* @param smartFs optional SmartFs instance for filesystem operations
|
||||
*/
|
||||
|
||||
constructor(optionsArg: ISmartfileConstructorOptions, smartFs?: any) {
|
||||
constructor(
|
||||
optionsArg: ISmartfileConstructorOptions = {
|
||||
path: '',
|
||||
contentBuffer: Buffer.alloc(0),
|
||||
base: process.cwd(),
|
||||
},
|
||||
smartFs?: any,
|
||||
) {
|
||||
super();
|
||||
if (optionsArg.contentBuffer) {
|
||||
this.contentBuffer = optionsArg.contentBuffer;
|
||||
} else {
|
||||
console.log('created empty Smartfile?');
|
||||
}
|
||||
this.contentBuffer = optionsArg.contentBuffer || Buffer.alloc(0);
|
||||
this.path = optionsArg.path;
|
||||
this.base = optionsArg.base;
|
||||
this.smartFs = smartFs;
|
||||
|
||||
@@ -33,7 +33,11 @@ export class StreamFile {
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(url)
|
||||
.get();
|
||||
return response.stream();
|
||||
const responseStream = response.stream();
|
||||
if (!responseStream) {
|
||||
throw new Error(`No response stream available for ${url}`);
|
||||
}
|
||||
return responseStream;
|
||||
};
|
||||
const streamFile = new StreamFile(streamSource, undefined, smartFs);
|
||||
streamFile.multiUse = true;
|
||||
@@ -117,9 +121,9 @@ export class StreamFile {
|
||||
|
||||
// enable stream based multi use
|
||||
private cachedStreamBuffer?: Buffer;
|
||||
public multiUse: boolean;
|
||||
public multiUse: boolean = false;
|
||||
public used: boolean = false;
|
||||
public byteLengthComputeFunction: () => Promise<number>;
|
||||
public byteLengthComputeFunction?: () => Promise<number>;
|
||||
|
||||
private constructor(streamSource: TStreamSource, relativeFilePath?: string, smartFs?: any) {
|
||||
this.streamSource = streamSource;
|
||||
@@ -185,6 +189,10 @@ export class StreamFile {
|
||||
}
|
||||
|
||||
this.checkMultiUse();
|
||||
if (!this.relativeFilePath) {
|
||||
throw new Error('Cannot write stream to directory without a relative file path.');
|
||||
}
|
||||
|
||||
const filePath = plugins.path.join(dirPathArg, this.relativeFilePath);
|
||||
const dirPath = plugins.path.parse(filePath).dir;
|
||||
await this.smartFs.directory(dirPath).recursive().create();
|
||||
@@ -213,7 +221,7 @@ export class StreamFile {
|
||||
/**
|
||||
* Returns the size of the file content in bytes.
|
||||
*/
|
||||
public async getSize(): Promise<number> {
|
||||
public async getSize(): Promise<number | null> {
|
||||
if (this.byteLengthComputeFunction) {
|
||||
return this.byteLengthComputeFunction();
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user