Skip to content

Commit e3ee26a

Browse files
committed
refactor(ts): refactor parsePush action into TS
1 parent aa2993a commit e3ee26a

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

src/proxy/processors/push-action/parsePush.js renamed to src/proxy/processors/push-action/parsePush.ts

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
const zlib = require('zlib');
2-
const fs = require('fs');
3-
const lod = require('lodash');
4-
const BitMask = require('bit-mask');
5-
const Step = require('../../actions').Step;
6-
const dir = './.tmp/';
1+
import { Action, Step } from '../../actions';
2+
import zlib from 'zlib';
3+
import fs from 'fs';
4+
import path from 'path';
5+
import lod from 'lodash';
6+
import { CommitContent } from '../types';
7+
const BitMask = require('bit-mask') as any;
8+
9+
const dir = path.resolve(__dirname, './.tmp');
710

811
if (!fs.existsSync(dir)) {
9-
fs.mkdirSync(dir);
12+
fs.mkdirSync(dir, { recursive: true });
1013
}
1114

12-
const exec = async (req, action) => {
15+
const exec = async (req: any, action: Action): Promise<Action> => {
1316
const step = new Step('parsePackFile');
1417

1518
try {
@@ -20,9 +23,9 @@ const exec = async (req, action) => {
2023
const index = req.body.lastIndexOf('PACK');
2124
const buf = req.body.slice(index);
2225
const [meta, contentBuff] = getPackMeta(buf);
23-
const contents = getContents(contentBuff, meta.entries);
26+
const contents = getContents(contentBuff as any, meta.entries as number);
2427

25-
action.commitData = getCommitData(contents);
28+
action.commitData = getCommitData(contents as any);
2629

2730
if (action.commitFrom === '0000000000000000000000000000000000000000') {
2831
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
@@ -35,7 +38,7 @@ const exec = async (req, action) => {
3538
step.content = {
3639
meta: meta,
3740
};
38-
} catch (e) {
41+
} catch (e: any) {
3942
step.setError(
4043
`Unable to parse push. Please contact an administrator for support: ${e.toString('utf-8')}`,
4144
);
@@ -45,7 +48,7 @@ const exec = async (req, action) => {
4548
return action;
4649
};
4750

48-
const getCommitData = (contents) => {
51+
const getCommitData = (contents: CommitContent[]) => {
4952
console.log({ contents });
5053
return lod
5154
.chain(contents)
@@ -59,9 +62,13 @@ const getCommitData = (contents) => {
5962
const parts = formattedContent.filter((part) => part.length > 0);
6063
console.log({ parts });
6164

65+
if (!parts || parts.length < 5) {
66+
throw new Error('Invalid commit data');
67+
}
68+
6269
const tree = parts
6370
.find((t) => t.split(' ')[0] === 'tree')
64-
.replace('tree', '')
71+
?.replace('tree', '')
6572
.trim();
6673
console.log({ tree });
6774

@@ -75,13 +82,13 @@ const getCommitData = (contents) => {
7582

7683
const author = parts
7784
.find((t) => t.split(' ')[0] === 'author')
78-
.replace('author', '')
85+
?.replace('author', '')
7986
.trim();
8087
console.log({ author });
8188

8289
const committer = parts
8390
.find((t) => t.split(' ')[0] === 'committer')
84-
.replace('committer', '')
91+
?.replace('committer', '')
8592
.trim();
8693
console.log({ committer });
8794

@@ -93,36 +100,40 @@ const getCommitData = (contents) => {
93100
.join(' ');
94101
console.log({ message });
95102

96-
const commitTimestamp = committer.split(' ').reverse()[1];
103+
const commitTimestamp = committer?.split(' ').reverse()[1];
97104
console.log({ commitTimestamp });
98105

99-
const authorEmail = author.split(' ').reverse()[2].slice(1, -1);
106+
const authorEmail = author?.split(' ').reverse()[2].slice(1, -1);
100107
console.log({ authorEmail });
101108

102109
console.log({
103110
tree,
104111
parent,
105-
author: author.split('<')[0].trim(),
106-
committer: committer.split('<')[0].trim(),
112+
author: author?.split('<')[0].trim(),
113+
committer: committer?.split('<')[0].trim(),
107114
commitTimestamp,
108115
message,
109116
authorEmail,
110117
});
111118

119+
if (!tree || !parent || !author || !committer || !commitTimestamp || !message || !authorEmail) {
120+
throw new Error('Invalid commit data');
121+
}
122+
112123
return {
113124
tree,
114125
parent,
115126
author: author.split('<')[0].trim(),
116127
committer: committer.split('<')[0].trim(),
117128
commitTimestamp,
118129
message,
119-
authorEmail,
130+
authorEmail: authorEmail,
120131
};
121132
})
122133
.value();
123134
};
124135

125-
const getPackMeta = (buffer) => {
136+
const getPackMeta = (buffer: Buffer) => {
126137
const sig = buffer.slice(0, 4).toString('utf-8');
127138
const version = buffer.readUIntBE(4, 4);
128139
const entries = buffer.readUIntBE(8, 4);
@@ -136,13 +147,13 @@ const getPackMeta = (buffer) => {
136147
return [meta, buffer.slice(12)];
137148
};
138149

139-
const getContents = (buffer, entries) => {
150+
const getContents = (buffer: Buffer | CommitContent[], entries: number) => {
140151
const contents = [];
141152

142153
for (let i = 0; i < entries; i++) {
143154
try {
144-
const [content, nextBuffer] = getContent(i, buffer);
145-
buffer = nextBuffer;
155+
const [content, nextBuffer] = getContent(i, buffer as Buffer);
156+
buffer = nextBuffer as Buffer;
146157
contents.push(content);
147158
} catch (e) {
148159
console.log(e);
@@ -151,7 +162,7 @@ const getContents = (buffer, entries) => {
151162
return contents;
152163
};
153164

154-
const getInt = (bits) => {
165+
const getInt = (bits: boolean[]) => {
155166
let strBits = '';
156167

157168
// eslint-disable-next-line guard-for-in
@@ -162,7 +173,7 @@ const getInt = (bits) => {
162173
return parseInt(strBits, 2);
163174
};
164175

165-
const getContent = (item, buffer) => {
176+
const getContent = (item: number, buffer: Buffer) => {
166177
// FIRST byte contains the type and some of the size of the file
167178
// a MORE flag -8th byte tells us if there is a subsequent byte
168179
// which holds the file size
@@ -175,7 +186,7 @@ const getContent = (item, buffer) => {
175186
const type = getInt([m.getBit(4), m.getBit(5), m.getBit(6)]);
176187

177188
// Object IDs if this is a deltatfied blob
178-
let objectRef = null;
189+
let objectRef: string | null = null;
179190

180191
// If we have a more flag get the next
181192
// 8 bytes
@@ -199,7 +210,7 @@ const getContent = (item, buffer) => {
199210
}
200211

201212
// NOTE Size is the unziped size, not the zipped size
202-
size = getInt(size);
213+
const intSize = getInt(size);
203214

204215
// Deltafied objectives have a 20 byte identifer
205216
if (type == 7 || type == 6) {
@@ -216,19 +227,19 @@ const getContent = (item, buffer) => {
216227
item: item,
217228
value: byte,
218229
type: type,
219-
size: size,
230+
size: intSize,
220231
deflatedSize: deflatedSize,
221232
objectRef: objectRef,
222233
content: content,
223234
};
224235

225236
// Move on by the zipped content size.
226-
const nextBuffer = contentBuffer.slice(deflatedSize);
237+
const nextBuffer = contentBuffer.slice(deflatedSize as number);
227238

228239
return [result, nextBuffer];
229240
};
230241

231-
const unpack = (buf) => {
242+
const unpack = (buf: Buffer) => {
232243
// Unzip the content
233244
const inflated = zlib.inflateSync(buf);
234245

@@ -240,6 +251,9 @@ const unpack = (buf) => {
240251
};
241252

242253
exec.displayName = 'parsePush.exec';
243-
exports.exec = exec;
244-
exports.getPackMeta = getPackMeta;
245-
exports.unpack = unpack;
254+
255+
export {
256+
exec,
257+
getPackMeta,
258+
unpack
259+
};

0 commit comments

Comments
 (0)