2024-12-09 02:39:31 +01:00
|
|
|
import * as plugins from './plugins.js';
|
2018-07-30 16:03:48 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* a log handler for spawned logs
|
|
|
|
|
* making sure the process doesn't run out of memory
|
|
|
|
|
*/
|
|
|
|
|
export class ShellLog {
|
2026-03-05 10:18:43 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2018-07-30 16:03:48 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* log data to console
|
2018-07-30 16:08:14 +02:00
|
|
|
* @param dataArg
|
2018-07-30 16:03:48 +02:00
|
|
|
*/
|
2019-05-28 10:43:54 +02:00
|
|
|
public writeToConsole(dataArg: string | Buffer): void {
|
2018-07-30 16:03:48 +02:00
|
|
|
// make sure we have the data as string
|
2018-12-13 16:50:32 +01:00
|
|
|
process.stdout.write(dataArg);
|
2018-07-30 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* add data to Buffer for later consumption
|
|
|
|
|
* @param dataArg
|
|
|
|
|
*/
|
2019-05-28 10:43:54 +02:00
|
|
|
public addToBuffer(dataArg: string | Buffer): void {
|
2018-07-30 16:03:48 +02:00
|
|
|
// make sure we have the data as Buffer
|
2026-03-05 10:18:43 +00:00
|
|
|
const dataBuffer: Buffer = Buffer.isBuffer(dataArg) ? dataArg : Buffer.from(dataArg);
|
|
|
|
|
this.chunks.push(dataBuffer);
|
|
|
|
|
this.totalLength += dataBuffer.length;
|
2018-07-30 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 10:43:54 +02:00
|
|
|
public logAndAdd(dataArg: string | Buffer): void {
|
|
|
|
|
this.writeToConsole(dataArg);
|
2018-07-30 16:03:48 +02:00
|
|
|
this.addToBuffer(dataArg);
|
|
|
|
|
}
|
2018-07-30 16:08:14 +02:00
|
|
|
}
|