Skip to content

Commit

Permalink
revert exec
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jun 28, 2024
1 parent 01b3edd commit e5c6189
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
49 changes: 28 additions & 21 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable promise/prefer-await-to-callbacks */

import { spawn } from 'child_process';
import { quote } from 'shell-quote';
import B from 'bluebird';
import _ from 'lodash';
import { formatEnoent } from './helpers';
import {LRUCache} from 'lru-cache';


const MAX_BUFFER_SIZE = 100 * 1024 * 1024;

Expand All @@ -22,7 +22,7 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) {

// extend default options; we're basically re-implementing exec's options
// for use here with spawn under the hood
const opts = /** @type {T} */(_.defaults(_.cloneDeep(originalOpts), {
const opts = /** @type {T} */(_.defaults(originalOpts, {
timeout: null,
encoding: 'utf8',
killSignal: 'SIGTERM',
Expand All @@ -44,14 +44,10 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) {
// spawn the child process with options; we don't currently expose any of
// the other 'spawn' options through the API
const proc = spawn(cmd, args, {cwd: opts.cwd, env: opts.env, shell: opts.shell});
/** @type {LRUCache<number, Buffer>} */
const stdoutCache = new LRUCache({
max: opts.maxStdoutBufferSize ?? MAX_BUFFER_SIZE,
});;
/** @type {LRUCache<number, Buffer>} */
const stderrCache = new LRUCache({
max: opts.maxStdoutBufferSize ?? MAX_BUFFER_SIZE,
});;
/** @type {Buffer[]} */
const stdoutArr = [];
/** @type {Buffer[]} */
const stderrArr = [];
let timer = null;

// if the process errors out, reject the promise
Expand All @@ -66,7 +62,7 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) {
reject(new Error(`Standard input '${err.syscall}' error: ${err.stack}`));
});
}
const handleStream = (/** @type {string} */ streamType, /** @type {LRUCache<number, Buffer>} */ cache) => {
const handleStream = (streamType, streamProps) => {
if (!proc[streamType]) {
return;
}
Expand All @@ -82,15 +78,28 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) {
}

// keep track of the stream if we don't want to ignore it
const {chunks, maxSize} = streamProps;
let size = 0;
proc[streamType].on('data', (/** @type {Buffer} */ chunk) => {
cache.set(cache.size, chunk);
chunks.push(chunk);
size += chunk.length;
while (chunks.length > 1 && size >= maxSize) {
size -= chunks[0].length;
chunks.shift();
}
if (opts.logger && _.isFunction(opts.logger.debug)) {
opts.logger.debug(chunk.toString());
}
});
};
handleStream('stdout', stdoutCache);
handleStream('stderr', stderrCache);
handleStream('stdout', {
maxSize: opts.maxStdoutBufferSize,
chunks: stdoutArr,
});
handleStream('stderr', {
maxSize: opts.maxStderrBufferSize,
chunks: stderrArr,
});

/**
* @template {boolean} U
Expand All @@ -103,13 +112,11 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) {
/** @type {string | Buffer} */
let stderr;
if (isBuffer) {
stdout = Buffer.concat(/** @type {Uint8Array[]} */ ([...stdoutCache.values()]));
stderr = Buffer.concat(/** @type {Uint8Array[]} */ ([...stderrCache.values()]));
stdout = Buffer.concat(stdoutArr);
stderr = Buffer.concat(stderrArr);
} else {
stdout = Buffer.concat(/** @type {Uint8Array[]} */ ([...stdoutCache.values()]))
.toString(opts.encoding);
stderr = Buffer.concat(/** @type {Uint8Array[]} */ ([...stderrCache.values()]))
.toString(opts.encoding);
stdout = Buffer.concat(stdoutArr).toString(opts.encoding);
stderr = Buffer.concat(stderrArr).toString(opts.encoding);
}
return /** @type {U extends true ? {stdout: Buffer, stderr: Buffer} : {stdout: string, stderr: string}} */({stdout, stderr});
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"dependencies": {
"bluebird": "^3.7.2",
"lodash": "^4.17.21",
"lru-cache": "^10.2.2",
"shell-quote": "^1.8.1",
"source-map-support": "^0.x"
},
Expand Down

0 comments on commit e5c6189

Please sign in to comment.