forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpktLineParser.ts
More file actions
38 lines (32 loc) · 1.43 KB
/
pktLineParser.ts
File metadata and controls
38 lines (32 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { PACKET_SIZE } from './constants';
/**
* Parses the packet lines from a buffer into an array of strings.
* Also returns the offset immediately following the parsed lines (including the flush packet).
* @param {Buffer} buffer - The buffer containing the packet data.
* @return {[string[], number]} An array containing the parsed lines and the offset after the last parsed line/flush packet.
*/
export const parsePacketLines = (buffer: Buffer): [string[], number] => {
const lines: string[] = [];
let offset = 0;
while (offset + PACKET_SIZE <= buffer.length) {
const lengthHex = buffer.toString('utf8', offset, offset + PACKET_SIZE);
const length = Number(`0x${lengthHex}`);
// Prevent non-hex characters from causing issues
if (isNaN(length) || length < 0) {
throw new Error(`Invalid packet line length ${lengthHex} at offset ${offset}`);
}
// length of 0 indicates flush packet (0000)
if (length === 0) {
offset += PACKET_SIZE; // Include length of the flush packet
break;
}
// Make sure we don't read past the end of the buffer
if (offset + length > buffer.length) {
throw new Error(`Invalid packet line length ${lengthHex} at offset ${offset}`);
}
const line = buffer.toString('utf8', offset + PACKET_SIZE, offset + length);
lines.push(line);
offset += length; // Move offset to the start of the next line's length prefix
}
return [lines, offset];
};