Use Buffer.concat to avoid issues with partial data streams

This commit is contained in:
gustavderdrache 2025-02-20 15:58:56 -05:00
parent af869eb4a0
commit 805b174a6e
2 changed files with 22 additions and 17 deletions

19
dist/index.js vendored
View File

@ -964,33 +964,34 @@ class GitCommandManager {
}); });
} }
exec(args_1) { exec(args_1) {
return __awaiter(this, arguments, void 0, function* (args, opts = {}) { return __awaiter(this, arguments, void 0, function* (args, { encoding = 'utf8', allowAllExitCodes = false } = {}) {
var _a;
const result = new GitOutput(); const result = new GitOutput();
const env = {}; const env = {};
for (const key of Object.keys(process.env)) { for (const key of Object.keys(process.env)) {
env[key] = process.env[key]; env[key] = process.env[key];
} }
const stdout = []; const stdout = [];
let stdoutLength = 0;
const stderr = []; const stderr = [];
let stderrLength = 0;
const options = { const options = {
cwd: this.workingDirectory, cwd: this.workingDirectory,
env, env,
ignoreReturnCode: (_a = opts.allowAllExitCodes) !== null && _a !== void 0 ? _a : false, ignoreReturnCode: allowAllExitCodes,
listeners: { listeners: {
stdout: (data) => { stdout: (data) => {
var _a; stdout.push(data);
stdout.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); stdoutLength += data.length;
}, },
stderr: (data) => { stderr: (data) => {
var _a; stderr.push(data);
stderr.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); stderrLength += data.length;
} }
} }
}; };
result.exitCode = yield exec.exec(`"${this.gitPath}"`, args, options); result.exitCode = yield exec.exec(`"${this.gitPath}"`, args, options);
result.stdout = stdout.join(''); result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding);
result.stderr = stderr.join(''); result.stderr = Buffer.concat(stderr, stderrLength).toString(encoding);
return result; return result;
}); });
} }

View File

@ -360,7 +360,7 @@ export class GitCommandManager {
return stdout return stdout
} }
async exec(args: string[], opts: ExecOpts = {}): Promise<GitOutput> { async exec(args: string[], { encoding = 'utf8', allowAllExitCodes = false }: ExecOpts = {}): Promise<GitOutput> {
const result = new GitOutput() const result = new GitOutput()
const env = {} const env = {}
@ -368,26 +368,30 @@ export class GitCommandManager {
env[key] = process.env[key] env[key] = process.env[key]
} }
const stdout: string[] = [] const stdout: Buffer[] = []
const stderr: string[] = [] let stdoutLength = 0
const stderr: Buffer[] = []
let stderrLength = 0
const options = { const options = {
cwd: this.workingDirectory, cwd: this.workingDirectory,
env, env,
ignoreReturnCode: opts.allowAllExitCodes ?? false, ignoreReturnCode: allowAllExitCodes,
listeners: { listeners: {
stdout: (data: Buffer) => { stdout: (data: Buffer) => {
stdout.push(data.toString(opts.encoding ?? 'utf8')) stdout.push(data)
stdoutLength += data.length
}, },
stderr: (data: Buffer) => { stderr: (data: Buffer) => {
stderr.push(data.toString(opts.encoding ?? 'utf8')) stderr.push(data)
stderrLength += data.length
} }
} }
} }
result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options) result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options)
result.stdout = stdout.join('') result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding)
result.stderr = stderr.join('') result.stderr = Buffer.concat(stderr, stderrLength).toString(encoding)
return result return result
} }
} }