diff --git a/src/utils/trimEndChar.ts b/src/utils/trimEndChar.ts new file mode 100644 index 0000000..8e708f0 --- /dev/null +++ b/src/utils/trimEndChar.ts @@ -0,0 +1,12 @@ +export function trimEndChar(str: string, char: string): string { + if (!char || char.length !== 1) { + throw new Error("trimEndChar expects a single character.") + } + + let end = str.length + while (end > 0 && str[end - 1] === char) { + end-- + } + + return str.slice(0, end) +} diff --git a/src/wire/parseRefsAdResponse.ts b/src/wire/parseRefsAdResponse.ts index dff9a0b..55248e7 100644 --- a/src/wire/parseRefsAdResponse.ts +++ b/src/wire/parseRefsAdResponse.ts @@ -1,7 +1,8 @@ import { Buffer } from 'buffer' import { EmptyServerResponseError } from '../errors/EmptyServerResponseError' -import { ParseError } from '../errors/ParseError' import { GitPktLine } from '../models/GitPktLine' +import { ParseError } from '../errors/ParseError' +import { trimEndChar } from '../utils/trimEndChar' /** @internal */ export type RemoteHTTPV1 = { @@ -89,7 +90,7 @@ export async function parseRefsAdResponse( } function splitAndAssert(line: string, sep: string, expected: string) { - const split = line.trim().split(sep) + const split = trimEndChar(line.trim(), '\x00').split(sep) if (split.length !== 2) { throw new ParseError(expected, line) } diff --git a/tests/utils-trimEndChar.test.ts b/tests/utils-trimEndChar.test.ts new file mode 100644 index 0000000..f339fe3 Binary files /dev/null and b/tests/utils-trimEndChar.test.ts differ