fix(smartshell): avoid triple shell nesting, improve WSL path filtering, and use chunked log buffer to reduce memory usage
This commit is contained in:
@@ -5,7 +5,41 @@ import * as plugins from './plugins.js';
|
||||
* making sure the process doesn't run out of memory
|
||||
*/
|
||||
export class ShellLog {
|
||||
public logStore = Buffer.from('');
|
||||
private chunks: Buffer[] = [];
|
||||
private totalLength = 0;
|
||||
|
||||
/**
|
||||
* Get the accumulated log as a single Buffer.
|
||||
* Concatenation happens lazily only when accessed.
|
||||
*/
|
||||
public get logStore(): Buffer {
|
||||
if (this.chunks.length === 0) {
|
||||
return Buffer.alloc(0);
|
||||
}
|
||||
if (this.chunks.length === 1) {
|
||||
return this.chunks[0];
|
||||
}
|
||||
// Flatten chunks into a single buffer
|
||||
const combined = Buffer.concat(this.chunks, this.totalLength);
|
||||
// Replace chunks array with the single combined buffer for future access
|
||||
this.chunks = [combined];
|
||||
return combined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the log store directly (used for truncation).
|
||||
*/
|
||||
public set logStore(value: Buffer) {
|
||||
this.chunks = [value];
|
||||
this.totalLength = value.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current total length of buffered data without concatenating.
|
||||
*/
|
||||
public get logLength(): number {
|
||||
return this.totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* log data to console
|
||||
@@ -22,13 +56,9 @@ export class ShellLog {
|
||||
*/
|
||||
public addToBuffer(dataArg: string | Buffer): void {
|
||||
// make sure we have the data as Buffer
|
||||
const dataBuffer: Buffer = (() => {
|
||||
if (!Buffer.isBuffer(dataArg)) {
|
||||
return Buffer.from(dataArg);
|
||||
}
|
||||
return dataArg;
|
||||
})();
|
||||
this.logStore = Buffer.concat([this.logStore, dataBuffer]);
|
||||
const dataBuffer: Buffer = Buffer.isBuffer(dataArg) ? dataArg : Buffer.from(dataArg);
|
||||
this.chunks.push(dataBuffer);
|
||||
this.totalLength += dataBuffer.length;
|
||||
}
|
||||
|
||||
public logAndAdd(dataArg: string | Buffer): void {
|
||||
|
||||
Reference in New Issue
Block a user