@@ -34,18 +34,21 @@ class Header {
34
34
this . atime = null
35
35
this . ctime = null
36
36
37
- if ( Buffer . isBuffer ( data ) )
37
+ if ( Buffer . isBuffer ( data ) ) {
38
38
this . decode ( data , off || 0 , ex , gex )
39
- else if ( data )
39
+ } else if ( data ) {
40
40
this . set ( data )
41
+ }
41
42
}
42
43
43
44
decode ( buf , off , ex , gex ) {
44
- if ( ! off )
45
+ if ( ! off ) {
45
46
off = 0
47
+ }
46
48
47
- if ( ! buf || ! ( buf . length >= off + 512 ) )
49
+ if ( ! buf || ! ( buf . length >= off + 512 ) ) {
48
50
throw new Error ( 'need 512 bytes for header' )
51
+ }
49
52
50
53
this . path = decString ( buf , off , 100 )
51
54
this . mode = decNumber ( buf , off + 100 , 8 )
@@ -62,18 +65,21 @@ class Header {
62
65
63
66
// old tar versions marked dirs as a file with a trailing /
64
67
this [ TYPE ] = decString ( buf , off + 156 , 1 )
65
- if ( this [ TYPE ] === '' )
68
+ if ( this [ TYPE ] === '' ) {
66
69
this [ TYPE ] = '0'
67
- if ( this [ TYPE ] === '0' && this . path . substr ( - 1 ) === '/' )
70
+ }
71
+ if ( this [ TYPE ] === '0' && this . path . slice ( - 1 ) === '/' ) {
68
72
this [ TYPE ] = '5'
73
+ }
69
74
70
75
// tar implementations sometimes incorrectly put the stat(dir).size
71
76
// as the size in the tarball, even though Directory entries are
72
77
// not able to have any body at all. In the very rare chance that
73
78
// it actually DOES have a body, we weren't going to do anything with
74
79
// it anyway, and it'll just be a warning about an invalid header.
75
- if ( this [ TYPE ] === '5' )
80
+ if ( this [ TYPE ] === '5' ) {
76
81
this . size = 0
82
+ }
77
83
78
84
this . linkpath = decString ( buf , off + 157 , 100 )
79
85
if ( buf . slice ( off + 257 , off + 265 ) . toString ( ) === 'ustar\u000000' ) {
@@ -87,32 +93,37 @@ class Header {
87
93
this . path = prefix + '/' + this . path
88
94
} else {
89
95
const prefix = decString ( buf , off + 345 , 130 )
90
- if ( prefix )
96
+ if ( prefix ) {
91
97
this . path = prefix + '/' + this . path
98
+ }
92
99
this . atime = decDate ( buf , off + 476 , 12 )
93
100
this . ctime = decDate ( buf , off + 488 , 12 )
94
101
}
95
102
}
96
103
97
104
let sum = 8 * 0x20
98
- for ( let i = off ; i < off + 148 ; i ++ )
105
+ for ( let i = off ; i < off + 148 ; i ++ ) {
99
106
sum += buf [ i ]
107
+ }
100
108
101
- for ( let i = off + 156 ; i < off + 512 ; i ++ )
109
+ for ( let i = off + 156 ; i < off + 512 ; i ++ ) {
102
110
sum += buf [ i ]
111
+ }
103
112
104
113
this . cksumValid = sum === this . cksum
105
- if ( this . cksum === null && sum === 8 * 0x20 )
114
+ if ( this . cksum === null && sum === 8 * 0x20 ) {
106
115
this . nullBlock = true
116
+ }
107
117
}
108
118
109
119
[ SLURP ] ( ex , global ) {
110
120
for ( const k in ex ) {
111
121
// we slurp in everything except for the path attribute in
112
122
// a global extended header, because that's weird.
113
123
if ( ex [ k ] !== null && ex [ k ] !== undefined &&
114
- ! ( global && k === 'path' ) )
124
+ ! ( global && k === 'path' ) ) {
115
125
this [ k ] = ex [ k ]
126
+ }
116
127
}
117
128
}
118
129
@@ -122,11 +133,13 @@ class Header {
122
133
off = 0
123
134
}
124
135
125
- if ( ! off )
136
+ if ( ! off ) {
126
137
off = 0
138
+ }
127
139
128
- if ( ! ( buf . length >= off + 512 ) )
140
+ if ( ! ( buf . length >= off + 512 ) ) {
129
141
throw new Error ( 'need 512 bytes for header' )
142
+ }
130
143
131
144
const prefixSize = this . ctime || this . atime ? 130 : 155
132
145
const split = splitPrefix ( this . path || '' , prefixSize )
@@ -148,20 +161,22 @@ class Header {
148
161
this . needPax = encNumber ( buf , off + 329 , 8 , this . devmaj ) || this . needPax
149
162
this . needPax = encNumber ( buf , off + 337 , 8 , this . devmin ) || this . needPax
150
163
this . needPax = encString ( buf , off + 345 , prefixSize , prefix ) || this . needPax
151
- if ( buf [ off + 475 ] !== 0 )
164
+ if ( buf [ off + 475 ] !== 0 ) {
152
165
this . needPax = encString ( buf , off + 345 , 155 , prefix ) || this . needPax
153
- else {
166
+ } else {
154
167
this . needPax = encString ( buf , off + 345 , 130 , prefix ) || this . needPax
155
168
this . needPax = encDate ( buf , off + 476 , 12 , this . atime ) || this . needPax
156
169
this . needPax = encDate ( buf , off + 488 , 12 , this . ctime ) || this . needPax
157
170
}
158
171
159
172
let sum = 8 * 0x20
160
- for ( let i = off ; i < off + 148 ; i ++ )
173
+ for ( let i = off ; i < off + 148 ; i ++ ) {
161
174
sum += buf [ i ]
175
+ }
162
176
163
- for ( let i = off + 156 ; i < off + 512 ; i ++ )
177
+ for ( let i = off + 156 ; i < off + 512 ; i ++ ) {
164
178
sum += buf [ i ]
179
+ }
165
180
166
181
this . cksum = sum
167
182
encNumber ( buf , off + 148 , 8 , this . cksum )
@@ -172,8 +187,9 @@ class Header {
172
187
173
188
set ( data ) {
174
189
for ( const i in data ) {
175
- if ( data [ i ] !== null && data [ i ] !== undefined )
190
+ if ( data [ i ] !== null && data [ i ] !== undefined ) {
176
191
this [ i ] = data [ i ]
192
+ }
177
193
}
178
194
}
179
195
@@ -186,10 +202,11 @@ class Header {
186
202
}
187
203
188
204
set type ( type ) {
189
- if ( types . code . has ( type ) )
205
+ if ( types . code . has ( type ) ) {
190
206
this [ TYPE ] = types . code . get ( type )
191
- else
207
+ } else {
192
208
this [ TYPE ] = type
209
+ }
193
210
}
194
211
}
195
212
@@ -200,34 +217,33 @@ const splitPrefix = (p, prefixSize) => {
200
217
let ret
201
218
const root = pathModule . parse ( p ) . root || '.'
202
219
203
- if ( Buffer . byteLength ( pp ) < pathSize )
220
+ if ( Buffer . byteLength ( pp ) < pathSize ) {
204
221
ret = [ pp , prefix , false ]
205
- else {
222
+ } else {
206
223
// first set prefix to the dir, and path to the base
207
224
prefix = pathModule . dirname ( pp )
208
225
pp = pathModule . basename ( pp )
209
226
210
227
do {
211
- // both fit!
212
228
if ( Buffer . byteLength ( pp ) <= pathSize &&
213
- Buffer . byteLength ( prefix ) <= prefixSize )
229
+ Buffer . byteLength ( prefix ) <= prefixSize ) {
230
+ // both fit!
214
231
ret = [ pp , prefix , false ]
215
-
216
- // prefix fits in prefix, but path doesn't fit in path
217
- else if ( Buffer . byteLength ( pp ) > pathSize &&
218
- Buffer . byteLength ( prefix ) <= prefixSize )
219
- ret = [ pp . substr ( 0 , pathSize - 1 ) , prefix , true ]
220
-
221
- else {
232
+ } else if ( Buffer . byteLength ( pp ) > pathSize &&
233
+ Buffer . byteLength ( prefix ) <= prefixSize ) {
234
+ // prefix fits in prefix, but path doesn't fit in path
235
+ ret = [ pp . slice ( 0 , pathSize - 1 ) , prefix , true ]
236
+ } else {
222
237
// make path take a bit from prefix
223
238
pp = pathModule . join ( pathModule . basename ( prefix ) , pp )
224
239
prefix = pathModule . dirname ( prefix )
225
240
}
226
241
} while ( prefix !== root && ! ret )
227
242
228
243
// at this point, found no resolution, just truncate
229
- if ( ! ret )
230
- ret = [ p . substr ( 0 , pathSize - 1 ) , '' , true ]
244
+ if ( ! ret ) {
245
+ ret = [ p . slice ( 0 , pathSize - 1 ) , '' , true ]
246
+ }
231
247
}
232
248
return ret
233
249
}
0 commit comments