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

View File

@ -360,7 +360,7 @@ export class GitCommandManager {
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 env = {}
@ -368,26 +368,30 @@ export class GitCommandManager {
env[key] = process.env[key]
}
const stdout: string[] = []
const stderr: string[] = []
const stdout: Buffer[] = []
let stdoutLength = 0
const stderr: Buffer[] = []
let stderrLength = 0
const options = {
cwd: this.workingDirectory,
env,
ignoreReturnCode: opts.allowAllExitCodes ?? false,
ignoreReturnCode: allowAllExitCodes,
listeners: {
stdout: (data: Buffer) => {
stdout.push(data.toString(opts.encoding ?? 'utf8'))
stdout.push(data)
stdoutLength += data.length
},
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.stdout = stdout.join('')
result.stderr = stderr.join('')
result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding)
result.stderr = Buffer.concat(stderr, stderrLength).toString(encoding)
return result
}
}