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' ) ;
7
10
8
11
if ( ! fs . existsSync ( dir ) ) {
9
- fs . mkdirSync ( dir ) ;
12
+ fs . mkdirSync ( dir , { recursive : true } ) ;
10
13
}
11
14
12
- const exec = async ( req , action ) => {
15
+ const exec = async ( req : any , action : Action ) : Promise < Action > => {
13
16
const step = new Step ( 'parsePackFile' ) ;
14
17
15
18
try {
@@ -20,9 +23,9 @@ const exec = async (req, action) => {
20
23
const index = req . body . lastIndexOf ( 'PACK' ) ;
21
24
const buf = req . body . slice ( index ) ;
22
25
const [ meta , contentBuff ] = getPackMeta ( buf ) ;
23
- const contents = getContents ( contentBuff , meta . entries ) ;
26
+ const contents = getContents ( contentBuff as any , meta . entries as number ) ;
24
27
25
- action . commitData = getCommitData ( contents ) ;
28
+ action . commitData = getCommitData ( contents as any ) ;
26
29
27
30
if ( action . commitFrom === '0000000000000000000000000000000000000000' ) {
28
31
action . commitFrom = action . commitData [ action . commitData . length - 1 ] . parent ;
@@ -35,7 +38,7 @@ const exec = async (req, action) => {
35
38
step . content = {
36
39
meta : meta ,
37
40
} ;
38
- } catch ( e ) {
41
+ } catch ( e : any ) {
39
42
step . setError (
40
43
`Unable to parse push. Please contact an administrator for support: ${ e . toString ( 'utf-8' ) } ` ,
41
44
) ;
@@ -45,7 +48,7 @@ const exec = async (req, action) => {
45
48
return action ;
46
49
} ;
47
50
48
- const getCommitData = ( contents ) => {
51
+ const getCommitData = ( contents : CommitContent [ ] ) => {
49
52
console . log ( { contents } ) ;
50
53
return lod
51
54
. chain ( contents )
@@ -59,9 +62,13 @@ const getCommitData = (contents) => {
59
62
const parts = formattedContent . filter ( ( part ) => part . length > 0 ) ;
60
63
console . log ( { parts } ) ;
61
64
65
+ if ( ! parts || parts . length < 5 ) {
66
+ throw new Error ( 'Invalid commit data' ) ;
67
+ }
68
+
62
69
const tree = parts
63
70
. find ( ( t ) => t . split ( ' ' ) [ 0 ] === 'tree' )
64
- . replace ( 'tree' , '' )
71
+ ? .replace ( 'tree' , '' )
65
72
. trim ( ) ;
66
73
console . log ( { tree } ) ;
67
74
@@ -75,13 +82,13 @@ const getCommitData = (contents) => {
75
82
76
83
const author = parts
77
84
. find ( ( t ) => t . split ( ' ' ) [ 0 ] === 'author' )
78
- . replace ( 'author' , '' )
85
+ ? .replace ( 'author' , '' )
79
86
. trim ( ) ;
80
87
console . log ( { author } ) ;
81
88
82
89
const committer = parts
83
90
. find ( ( t ) => t . split ( ' ' ) [ 0 ] === 'committer' )
84
- . replace ( 'committer' , '' )
91
+ ? .replace ( 'committer' , '' )
85
92
. trim ( ) ;
86
93
console . log ( { committer } ) ;
87
94
@@ -93,36 +100,40 @@ const getCommitData = (contents) => {
93
100
. join ( ' ' ) ;
94
101
console . log ( { message } ) ;
95
102
96
- const commitTimestamp = committer . split ( ' ' ) . reverse ( ) [ 1 ] ;
103
+ const commitTimestamp = committer ? .split ( ' ' ) . reverse ( ) [ 1 ] ;
97
104
console . log ( { commitTimestamp } ) ;
98
105
99
- const authorEmail = author . split ( ' ' ) . reverse ( ) [ 2 ] . slice ( 1 , - 1 ) ;
106
+ const authorEmail = author ? .split ( ' ' ) . reverse ( ) [ 2 ] . slice ( 1 , - 1 ) ;
100
107
console . log ( { authorEmail } ) ;
101
108
102
109
console . log ( {
103
110
tree,
104
111
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 ( ) ,
107
114
commitTimestamp,
108
115
message,
109
116
authorEmail,
110
117
} ) ;
111
118
119
+ if ( ! tree || ! parent || ! author || ! committer || ! commitTimestamp || ! message || ! authorEmail ) {
120
+ throw new Error ( 'Invalid commit data' ) ;
121
+ }
122
+
112
123
return {
113
124
tree,
114
125
parent,
115
126
author : author . split ( '<' ) [ 0 ] . trim ( ) ,
116
127
committer : committer . split ( '<' ) [ 0 ] . trim ( ) ,
117
128
commitTimestamp,
118
129
message,
119
- authorEmail,
130
+ authorEmail : authorEmail ,
120
131
} ;
121
132
} )
122
133
. value ( ) ;
123
134
} ;
124
135
125
- const getPackMeta = ( buffer ) => {
136
+ const getPackMeta = ( buffer : Buffer ) => {
126
137
const sig = buffer . slice ( 0 , 4 ) . toString ( 'utf-8' ) ;
127
138
const version = buffer . readUIntBE ( 4 , 4 ) ;
128
139
const entries = buffer . readUIntBE ( 8 , 4 ) ;
@@ -136,13 +147,13 @@ const getPackMeta = (buffer) => {
136
147
return [ meta , buffer . slice ( 12 ) ] ;
137
148
} ;
138
149
139
- const getContents = ( buffer , entries ) => {
150
+ const getContents = ( buffer : Buffer | CommitContent [ ] , entries : number ) => {
140
151
const contents = [ ] ;
141
152
142
153
for ( let i = 0 ; i < entries ; i ++ ) {
143
154
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 ;
146
157
contents . push ( content ) ;
147
158
} catch ( e ) {
148
159
console . log ( e ) ;
@@ -151,7 +162,7 @@ const getContents = (buffer, entries) => {
151
162
return contents ;
152
163
} ;
153
164
154
- const getInt = ( bits ) => {
165
+ const getInt = ( bits : boolean [ ] ) => {
155
166
let strBits = '' ;
156
167
157
168
// eslint-disable-next-line guard-for-in
@@ -162,7 +173,7 @@ const getInt = (bits) => {
162
173
return parseInt ( strBits , 2 ) ;
163
174
} ;
164
175
165
- const getContent = ( item , buffer ) => {
176
+ const getContent = ( item : number , buffer : Buffer ) => {
166
177
// FIRST byte contains the type and some of the size of the file
167
178
// a MORE flag -8th byte tells us if there is a subsequent byte
168
179
// which holds the file size
@@ -175,7 +186,7 @@ const getContent = (item, buffer) => {
175
186
const type = getInt ( [ m . getBit ( 4 ) , m . getBit ( 5 ) , m . getBit ( 6 ) ] ) ;
176
187
177
188
// Object IDs if this is a deltatfied blob
178
- let objectRef = null ;
189
+ let objectRef : string | null = null ;
179
190
180
191
// If we have a more flag get the next
181
192
// 8 bytes
@@ -199,7 +210,7 @@ const getContent = (item, buffer) => {
199
210
}
200
211
201
212
// NOTE Size is the unziped size, not the zipped size
202
- size = getInt ( size ) ;
213
+ const intSize = getInt ( size ) ;
203
214
204
215
// Deltafied objectives have a 20 byte identifer
205
216
if ( type == 7 || type == 6 ) {
@@ -216,19 +227,19 @@ const getContent = (item, buffer) => {
216
227
item : item ,
217
228
value : byte ,
218
229
type : type ,
219
- size : size ,
230
+ size : intSize ,
220
231
deflatedSize : deflatedSize ,
221
232
objectRef : objectRef ,
222
233
content : content ,
223
234
} ;
224
235
225
236
// Move on by the zipped content size.
226
- const nextBuffer = contentBuffer . slice ( deflatedSize ) ;
237
+ const nextBuffer = contentBuffer . slice ( deflatedSize as number ) ;
227
238
228
239
return [ result , nextBuffer ] ;
229
240
} ;
230
241
231
- const unpack = ( buf ) => {
242
+ const unpack = ( buf : Buffer ) => {
232
243
// Unzip the content
233
244
const inflated = zlib . inflateSync ( buf ) ;
234
245
@@ -240,6 +251,9 @@ const unpack = (buf) => {
240
251
} ;
241
252
242
253
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