4
4
5
5
'use strict' ;
6
6
7
- var fs = require ( 'fs' ) ;
8
- var storj = require ( 'storj-lib' ) ;
9
- var bitcore = storj . deps . bitcore ;
10
- var du = require ( 'du' ) ;
11
- var diskspace = require ( 'fd-diskspace' ) . diskSpace ;
12
- var assert = require ( 'assert' ) ;
7
+ const fs = require ( 'fs' ) ;
8
+ const storj = require ( 'storj-lib' ) ;
9
+ const { bitcore} = storj . deps ;
10
+ const du = require ( 'du' ) ;
11
+ const diskspace = require ( 'fd-diskspace' ) . diskSpace ;
12
+ const assert = require ( 'assert' ) ;
13
+ const bytes = require ( 'bytes' ) ;
13
14
14
15
/**
15
16
* Validate the given payout address
16
17
* @param {String } address
17
18
*/
18
- module . exports . _isValidPayoutAddress = function ( address ) {
19
+ exports . _isValidPayoutAddress = function ( address ) {
19
20
return bitcore . Address . isValid ( address ) ||
20
21
bitcore . Address . isValid ( address , bitcore . Networks . testnet ) ;
21
22
} ;
@@ -24,33 +25,28 @@ module.exports._isValidPayoutAddress = function(address) {
24
25
* Validate the given dataserv directory
25
26
* @param {String } directory
26
27
*/
27
- module . exports . _isValidDirectory = function ( directory ) {
28
+ exports . _isValidDirectory = function ( directory ) {
28
29
return this . existsSync ( directory ) ;
29
30
} ;
30
31
31
32
/**
32
33
* Validate the given size
33
34
* @param {String } size
34
35
*/
35
- module . exports . _isValidSize = function ( size ) {
36
+ exports . _isValidSize = function ( size ) {
36
37
return Number ( size ) > 0 && typeof size !== 'undefined' ;
37
38
} ;
38
39
39
40
/**
40
41
* Validates a given tab config
41
42
* @param {Object } config
42
43
*/
43
- module . exports . validate = function ( config ) {
44
+ exports . validate = function ( config ) {
44
45
assert ( this . _isValidPayoutAddress ( config . paymentAddress ) ,
45
46
'Invalid payout address' ) ;
46
47
assert ( this . _isValidDirectory ( config . storagePath ) , 'Invalid directory' ) ;
47
- assert ( this . _isValidSize ( config . storageAllocationSize ) ,
48
+ assert ( this . _isValidSize ( bytes . parse ( config . storageAllocation ) ) ,
48
49
'Invalid storage size' ) ;
49
-
50
- if ( ! this . existsSync ( config . storagePath ) ) {
51
- fs . mkdirSync ( config . storagePath ) ;
52
- }
53
-
54
50
assert ( this . _isValidDirectory ( config . storagePath ) ,
55
51
'Could not create Shard Directory' ) ;
56
52
} ;
@@ -59,21 +55,18 @@ module.exports.validate = function(config) {
59
55
* Validates the space being allocated exists
60
56
* @param {Object } config
61
57
*/
62
- module . exports . validateAllocation = function ( conf , callback ) {
58
+ exports . validateAllocation = function ( conf , callback ) {
63
59
const self = this ;
64
60
65
61
self . getFreeSpace ( conf . storagePath , function ( err , free ) {
66
- var allocatedSpace = self . manualConvert ( {
67
- size : conf . storageAllocationSize ,
68
- unit : conf . storageAllocationUnit
69
- } , 'B' , 0 ) ;
62
+ var allocatedSpace = bytes . parse ( conf . storageAllocation ) ;
70
63
71
64
self . getDirectorySize ( conf . storagePath , function ( err , usedSpaceBytes ) {
72
65
if ( err ) {
73
66
return callback ( err ) ;
74
67
}
75
68
76
- if ( allocatedSpace . size > free + usedSpaceBytes ) {
69
+ if ( allocatedSpace > free + usedSpaceBytes ) {
77
70
return callback ( new Error ( 'Invalid storage size' ) ) ;
78
71
}
79
72
@@ -86,7 +79,7 @@ module.exports.validateAllocation = function(conf, callback) {
86
79
* Check if file exists
87
80
* @param {String } file - Path to file
88
81
*/
89
- module . exports . existsSync = function ( file ) {
82
+ exports . existsSync = function ( file ) {
90
83
try {
91
84
fs . statSync ( file ) ;
92
85
} catch ( err ) {
@@ -96,84 +89,13 @@ module.exports.existsSync = function(file) {
96
89
return true ;
97
90
} ;
98
91
99
- /**
100
- * Converts to a reasonable unit of bytes
101
- * @param {Object } bytes
102
- * @param {Number } precision
103
- */
104
- module . exports . autoConvert = function ( object , precision ) {
105
- /*jshint maxcomplexity:10 */
106
- var kilobyte = 1000 ;
107
- var megabyte = kilobyte * 1000 ;
108
- var gigabyte = megabyte * 1000 ;
109
- var terabyte = gigabyte * 1000 ;
110
-
111
- var byteobject = ( this . manualConvert ( object , 'B' ) ) ;
112
- var bytes = byteobject . size ;
113
-
114
- if ( ( bytes >= kilobyte ) && ( bytes < megabyte ) ) {
115
- return this . manualConvert ( byteobject , 'KB' , ( precision || 1 ) ) ;
116
- } else if ( ( bytes >= megabyte ) && ( bytes < gigabyte ) ) {
117
- return this . manualConvert ( byteobject , 'MB' , ( precision || 2 ) ) ;
118
- } else if ( ( bytes >= gigabyte ) && ( bytes < terabyte ) ) {
119
- return this . manualConvert ( byteobject , 'GB' , ( precision || 3 ) ) ;
120
- } else if ( bytes >= terabyte ) {
121
- return this . manualConvert ( byteobject , 'TB' , ( precision || 4 ) ) ;
122
- }
123
-
124
- return byteobject ;
125
- } ;
126
-
127
- /**
128
- * Converts units of bytes to other units
129
- * @param {Object } object to be converted
130
- * @param {String } Unit Object will be converted to
131
- */
132
- module . exports . manualConvert = function ( object , unit , precision ) {
133
- const table = {
134
- 'B' : 0 ,
135
- 'KB' : 1 ,
136
- 'MB' : 2 ,
137
- 'GB' : 3 ,
138
- 'TB' : 4
139
- } ;
140
-
141
- precision = ( ! precision ) ? ( table [ unit ] ? table [ unit ] : 6 ) : precision ;
142
- let diff = table [ object . unit ] - table [ unit ] ;
143
-
144
- if ( diff < 0 ) {
145
- return {
146
- size : ( object . size / Math . pow ( 1000 , Math . abs ( diff ) ) ) . toFixed ( precision ) ,
147
- unit : unit
148
- } ;
149
- } else if ( diff > 0 ) {
150
- return {
151
- size : ( object . size * Math . pow ( 1000 , Math . abs ( diff ) ) ) . toFixed ( precision ) ,
152
- unit : unit
153
- } ;
154
- } else {
155
- return object ;
156
- }
157
- } ;
158
-
159
- /**
160
- * find the difference between two file sizes
161
- * @param {Object } object
162
- * @param {Object } object
163
- */
164
- module . exports . subtract = function ( object1 , object2 ) {
165
- const bytes1 = this . manualConvert ( object1 , 'B' ) ;
166
- const bytes2 = this . manualConvert ( object2 , 'B' ) ;
167
- let difference = bytes1 . size - bytes2 . size ;
168
- return this . autoConvert ( { size : difference , unit : 'B' } ) ;
169
- } ;
170
-
171
92
/**
172
93
* Recursively determines the size of a directory
173
94
* @param {String } dir - Directory to traverse
174
95
* @param {Function } callback
175
96
*/
176
- module . exports . getDirectorySize = function ( dir , callback ) {
97
+ exports . getDirectorySize = function ( dir , callback ) {
98
+ /* istanbul ignore next */
177
99
du ( dir , {
178
100
filter : function ( f ) {
179
101
return f . indexOf ( 'contracts.db' ) !== - 1 ||
@@ -183,35 +105,35 @@ module.exports.getDirectorySize = function(dir, callback) {
183
105
} ;
184
106
185
107
/**
186
- * get free space on disk of path
108
+ * Get free space on disk of path
187
109
* @param {String } path
188
110
*/
189
- module . exports . getFreeSpace = function ( path , callback ) {
190
- var self = this ;
191
-
192
- if ( ! this . existsSync ( path ) ) {
111
+ /* istanbul ignore next */
112
+ exports . getFreeSpace = function ( path , callback ) {
113
+ if ( ! exports . existsSync ( path ) ) {
193
114
return callback ( null , 0 ) ;
194
115
}
195
116
196
117
diskspace ( function ( err , result ) {
197
- /*jshint maxcomplexity:10 */
118
+ /* jshint maxcomplexity:10 */
198
119
if ( err ) {
199
120
return callback ( err ) ;
200
121
}
201
122
202
- var free = 0 ;
123
+ let free = 0 ;
203
124
204
- for ( var disk in result . disks ) {
205
- var diskDrive = disk ;
125
+ for ( let disk in result . disks ) {
126
+ let diskDrive = disk ;
206
127
128
+ /* istanbul ignore if */
207
129
if ( process . platform === 'win32' ) {
208
130
diskDrive += ':\\' ;
209
131
}
210
132
211
- if ( self . existsSync ( diskDrive ) ) {
133
+ if ( exports . existsSync ( diskDrive ) ) {
212
134
if ( fs . statSync ( path ) . dev === fs . statSync ( diskDrive ) . dev ) {
213
- // The `df` command on linux returns KB by default, so we need to
214
- // convert to bytes.
135
+ // NB: The `df` command on gnu+ linux returns KB by default
136
+ // NB: so we need to convert to bytes.
215
137
free = process . platform === 'win32' ?
216
138
result . disks [ disk ] . free :
217
139
result . disks [ disk ] . free * 1000 ;
@@ -223,17 +145,3 @@ module.exports.getFreeSpace = function(path, callback) {
223
145
} ) ;
224
146
} ;
225
147
226
- exports . getLastLines = function ( filename , lines , callback ) {
227
- let chunk = '' ;
228
- let size = Math . max ( 0 , fs . statSync ( filename ) . size - ( lines * 200 ) ) ;
229
-
230
- fs . createReadStream ( filename , { start : size } )
231
- . on ( 'data' , function ( data ) {
232
- chunk += data . toString ( ) ;
233
- } )
234
- . on ( 'end' , function ( ) {
235
- chunk = chunk . split ( '\n' ) . slice ( - ( lines + 1 ) ) ;
236
- chunk . pop ( ) ;
237
- callback ( chunk ) ;
238
- } ) ;
239
- } ;
0 commit comments