Skip to content

Commit c010728

Browse files
committed
chore: add docstrings
1 parent 6536bba commit c010728

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/proxy/processors/push-action/parsePush.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ if (!fs.existsSync(dir)) {
1212
fs.mkdirSync(dir, { recursive: true });
1313
}
1414

15+
/**
16+
* Executes the parsing of a push request.
17+
* @param {*} req - The request object containing the push data.
18+
* @param {Action} action - The action object to be modified.
19+
* @return {Promise<Action>} The modified action object.
20+
*/
1521
async function exec(req: any, action: Action): Promise<Action> {
1622
const step = new Step('parsePackFile');
1723

@@ -67,6 +73,11 @@ async function exec(req: any, action: Action): Promise<Action> {
6773
return action;
6874
};
6975

76+
/**
77+
* Parses the commit data from the contents of a pack file.
78+
* @param {CommitContent[]} contents - The contents of the pack file.
79+
* @return {*} An array of commit data objects.
80+
*/
7081
const getCommitData = (contents: CommitContent[]) => {
7182
console.log({ contents });
7283
return lod
@@ -167,6 +178,11 @@ const getCommitData = (contents: CommitContent[]) => {
167178
.value();
168179
};
169180

181+
/**
182+
* Gets the metadata from a pack file.
183+
* @param {Buffer} buffer - The buffer containing the pack file data.
184+
* @return {Array} An array containing the metadata and the remaining buffer.
185+
*/
170186
const getPackMeta = (buffer: Buffer) => {
171187
const sig = buffer.slice(0, 4).toString('utf-8');
172188
const version = buffer.readUIntBE(4, 4);
@@ -181,6 +197,12 @@ const getPackMeta = (buffer: Buffer) => {
181197
return [meta, buffer.slice(12)];
182198
};
183199

200+
/**
201+
* Gets the contents of a pack file.
202+
* @param {Buffer} buffer - The buffer containing the pack file data.
203+
* @param {number} entries - The number of entries in the pack file.
204+
* @return {CommitContent[]} An array of commit content objects.
205+
*/
184206
const getContents = (buffer: Buffer | CommitContent[], entries: number) => {
185207
const contents = [];
186208

@@ -196,6 +218,11 @@ const getContents = (buffer: Buffer | CommitContent[], entries: number) => {
196218
return contents;
197219
};
198220

221+
/**
222+
* Converts an array of bits to an integer.
223+
* @param {boolean[]} bits - The array of bits.
224+
* @return {number} The integer value.
225+
*/
199226
const getInt = (bits: boolean[]) => {
200227
let strBits = '';
201228

@@ -207,6 +234,12 @@ const getInt = (bits: boolean[]) => {
207234
return parseInt(strBits, 2);
208235
};
209236

237+
/**
238+
* Gets the content of a pack file entry.
239+
* @param {number} item - The index of the entry.
240+
* @param {Buffer} buffer - The buffer containing the pack file data.
241+
* @return {Array} An array containing the content object and the next buffer.
242+
*/
210243
const getContent = (item: number, buffer: Buffer) => {
211244
// FIRST byte contains the type and some of the size of the file
212245
// a MORE flag -8th byte tells us if there is a subsequent byte
@@ -273,6 +306,11 @@ const getContent = (item: number, buffer: Buffer) => {
273306
return [result, nextBuffer];
274307
};
275308

309+
/**
310+
* Unzips the content of a buffer.
311+
* @param {Buffer} buf - The buffer containing the zipped content.
312+
* @return {Array} An array containing the unzipped content and the size of the deflated content.
313+
*/
276314
const unpack = (buf: Buffer) => {
277315
// Unzip the content
278316
const inflated = zlib.inflateSync(buf);
@@ -284,6 +322,11 @@ const unpack = (buf: Buffer) => {
284322
return [inflated.toString('utf8'), deflated.length];
285323
};
286324

325+
/**
326+
* Parses the packet lines from a buffer into an array of strings.
327+
* @param {Buffer} buffer - The buffer containing the packet data.
328+
* @return {string[]} An array of parsed lines.
329+
*/
287330
const parsePacketLines = (buffer: Buffer): string[] => {
288331
const lines: string[] = [];
289332
let offset = 0;

test/testParsePush.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const {
99
unpack
1010
} = require('../src/proxy/processors/push-action/parsePush');
1111

12+
/**
13+
* Creates a simplified sample PACK buffer for testing.
14+
* @param {number} numEntries - Number of entries in the PACK file.
15+
* @param {string} commitContent - Content of the commit object.
16+
* @param {number} type - Type of the object (1 for commit).
17+
* @return {Buffer} - The generated PACK buffer.
18+
*/
1219
function createSamplePackBuffer(numEntries = 1, commitContent = 'tree 123\nparent 456\nauthor A <a@a> 123 +0000\ncommitter C <c@c> 456 +0000\n\nmessage', type = 1) {
1320
const header = Buffer.alloc(12);
1421
header.write('PACK', 0, 4, 'utf-8'); // Signature
@@ -36,6 +43,12 @@ function createSamplePackBuffer(numEntries = 1, commitContent = 'tree 123\nparen
3643
return Buffer.concat([header, packContent, checksum]);
3744
}
3845

46+
/**
47+
* Creates a packet line buffer from an array of lines.
48+
* Each line is prefixed with its length in hex format, and the last line is a flush packet.
49+
* @param {string[]} lines - Array of lines to be included in the buffer.
50+
* @return {Buffer} - The generated buffer containing the packet lines.
51+
*/
3952
function createPacketLineBuffer(lines) {
4053
let buffer = Buffer.alloc(0);
4154
lines.forEach(line => {

0 commit comments

Comments
 (0)