4
4
5
5
"use strict" ;
6
6
7
- var async = require ( 'async' ) ;
8
- var fs = require ( 'fs' ) ;
9
- var path = require ( 'path' ) ;
10
- var uuid = require ( 'node-uuid' ) ;
11
- var mkdirp = require ( 'mkdirp' ) ;
12
- var clone = require ( 'clone' ) ;
13
-
14
- var isJSONFile = function ( f ) {
15
- return f . substr ( - 5 ) === ".json" ;
16
- } ;
7
+ import async from 'async' ;
8
+ import fs from 'fs' ;
9
+ import path from 'path' ;
10
+ import uuid from 'node-uuid' ;
11
+ import mkdirp from 'mkdirp' ;
12
+ import clone from 'clone' ;
17
13
18
- var removeFileExtension = function ( f ) {
19
- return f . split ( ".json" ) [ 0 ] ;
20
- } ;
14
+ const isJSONFile = f => f . substr ( - 5 ) === ".json" ;
21
15
22
- var getIDs = function ( a ) {
23
- return a . filter ( isJSONFile ) . map ( removeFileExtension ) ;
24
- } ;
16
+ const removeFileExtension = f => f . split ( ".json" ) [ 0 ] ;
25
17
26
- var readIDsSync = function ( d ) {
27
- return getIDs ( fs . readdirSync ( d ) ) ;
28
- } ;
18
+ const getIDs = a => a . filter ( isJSONFile ) . map ( removeFileExtension ) ;
29
19
30
- var readIDs = function ( d , cb ) {
31
- return fs . readdir ( d , function ( err , ids ) {
32
- return cb ( err , getIDs ( ids ) ) ;
33
- } ) ;
34
- } ;
20
+ const readIDsSync = d => getIDs ( fs . readdirSync ( d ) ) ;
35
21
36
- var canWrite = function ( stat ) {
37
- var group , owner ;
38
- owner = ( typeof process . getuid === "function" ? process . getuid ( ) : void 0 ) === stat . uid ;
39
- group = ( typeof process . getgid === "function" ? process . getgid ( ) : void 0 ) === stat . gid ;
22
+ const readIDs = ( d , cb ) => fs . readdir ( d , ( err , ids ) => {
23
+ cb ( err , getIDs ( ids ) ) ;
24
+ } ) ;
25
+
26
+ const canWrite = stat => {
27
+ const owner = ( typeof process . getuid === "function" ? process . getuid ( ) : void 0 ) === stat . uid ;
28
+ const group = ( typeof process . getgid === "function" ? process . getgid ( ) : void 0 ) === stat . gid ;
40
29
return owner && ( stat . mode & 128 ) || group && ( stat . mode & 16 ) || ( stat . mode & 2 ) ;
41
30
} ;
42
31
43
- var canWriteToFile = function ( file , cb ) {
44
- return fs . exists ( file , function ( e ) {
32
+ const canWriteToFile = ( file , cb ) => {
33
+ fs . exists ( file , ( e ) => {
45
34
if ( ! e ) {
46
35
return cb ( null ) ;
47
36
}
48
- return fs . stat ( file , function ( err , s ) {
37
+ fs . stat ( file , ( err , s ) => {
49
38
if ( err ) {
50
39
return cb ( err ) ;
51
40
}
52
41
if ( canWrite ( s ) ) {
53
- return cb ( null ) ;
42
+ cb ( null ) ;
54
43
} else {
55
- return cb ( new Error ( "File is protected" ) ) ;
44
+ cb ( new Error ( "File is protected" ) ) ;
56
45
}
57
46
} ) ;
58
47
} ) ;
59
48
} ;
60
49
61
- var canWriteToFileSync = function ( file ) {
50
+ const canWriteToFileSync = file => {
62
51
if ( ! fs . existsSync ( file ) ) {
63
52
return ;
64
53
}
@@ -69,55 +58,50 @@ var canWriteToFileSync = function(file) {
69
58
}
70
59
} ;
71
60
72
- var getObjectFromFileSync = function ( id ) {
73
- var e ;
61
+ const getObjectFromFileSync = function ( id ) {
74
62
try {
75
63
return JSON . parse ( fs . readFileSync ( this . _getFileName ( id ) , "utf8" ) ) ;
76
64
} catch ( error ) {
77
- e = error ;
78
- return e ;
65
+ return error ;
79
66
}
80
67
} ;
81
68
82
- var getObjectFromFile = function ( id , cb ) {
83
- return fs . readFile ( this . _getFileName ( id ) , "utf8" , function ( err , o ) {
84
- var e ;
69
+ const getObjectFromFile = function ( id , cb ) {
70
+ fs . readFile ( this . _getFileName ( id ) , "utf8" , ( err , o ) => {
85
71
if ( err ) {
86
72
return cb ( err ) ;
87
73
}
88
74
try {
89
- return cb ( null , JSON . parse ( o ) ) ;
75
+ cb ( null , JSON . parse ( o ) ) ;
90
76
} catch ( error ) {
91
- e = error ;
92
- return cb ( e ) ;
77
+ cb ( error ) ;
93
78
}
94
79
} ) ;
95
80
} ;
96
81
97
- var saveObjectToFile = function ( o , file , cb ) {
98
- var e , indent , json , tmpFileName ;
99
- indent = this . _pretty ? 2 : void 0 ;
82
+ const saveObjectToFile = function ( o , file , cb ) {
83
+ var json ;
84
+ const indent = this . _pretty ? 2 : void 0 ;
100
85
try {
101
86
json = JSON . stringify ( o , null , indent ) ;
102
87
} catch ( error ) {
103
- e = error ;
104
88
if ( cb != null ) {
105
- return cb ( e ) ;
89
+ return cb ( error ) ;
106
90
} else {
107
- return e ;
91
+ return error ;
108
92
}
109
93
}
110
- tmpFileName = file + uuid . v4 ( ) + ".tmp" ;
94
+ var tmpFileName = file + uuid . v4 ( ) + ".tmp" ;
111
95
if ( cb != null ) {
112
- return canWriteToFile ( file , function ( err ) {
96
+ canWriteToFile ( file , ( err ) => {
113
97
if ( err ) {
114
98
return cb ( err ) ;
115
99
}
116
- return fs . writeFile ( tmpFileName , json , 'utf8' , function ( err ) {
100
+ fs . writeFile ( tmpFileName , json , 'utf8' , ( err ) => {
117
101
if ( err ) {
118
102
return cb ( err ) ;
119
103
}
120
- return fs . rename ( tmpFileName , file , cb ) ;
104
+ fs . rename ( tmpFileName , file , cb ) ;
121
105
} ) ;
122
106
} ) ;
123
107
} else {
@@ -126,18 +110,15 @@ var saveObjectToFile = function(o, file, cb) {
126
110
fs . writeFileSync ( tmpFileName , json , 'utf8' ) ;
127
111
return fs . renameSync ( tmpFileName , file ) ;
128
112
} catch ( error ) {
129
- e = error ;
130
- return e ;
113
+ return error ;
131
114
}
132
115
}
133
116
} ;
134
117
135
- var id2fileName = function ( id , dir ) {
136
- return path . join ( dir , id + ".json" ) ;
137
- } ;
118
+ const id2fileName = ( id , dir ) => path . join ( dir , id + ".json" ) ;
138
119
139
- var save = function ( id , o , cb ) {
140
- var backup , data , done , file , k ;
120
+ const save = function ( id , o , cb ) {
121
+ var backup , k ;
141
122
if ( typeof id === "object" ) {
142
123
cb = o ;
143
124
o = id ;
@@ -146,7 +127,7 @@ var save = function(id, o, cb) {
146
127
if ( id == null ) {
147
128
id = uuid . v4 ( ) ;
148
129
}
149
- file = this . _getFileName ( id ) ;
130
+ const file = this . _getFileName ( id ) ;
150
131
o = clone ( o ) ;
151
132
if ( this . _saveId ) {
152
133
if ( ( typeof ( k = this . _saveId ) ) === 'string' && k . length > 0 ) {
@@ -155,46 +136,49 @@ var save = function(id, o, cb) {
155
136
o . id = id ;
156
137
}
157
138
}
158
- data = this . _single ? ( backup = this . _cache [ id ] , this . _cache [ id ] = o , this . _cache ) : o ;
159
- done = ( function ( _this ) {
160
- return function ( err ) {
139
+
140
+ const data = this . _single ? ( backup = this . _cache [ id ] , this . _cache [ id ] = o , this . _cache ) : o ;
141
+
142
+ const done = ( function ( _this ) {
143
+ return ( err ) => {
161
144
if ( err ) {
162
145
if ( _this . _single ) {
163
146
_this . _cache [ id ] = backup ;
164
147
}
165
148
if ( cb != null ) {
166
- return cb ( err ) ;
149
+ cb ( err ) ;
167
150
} else {
168
151
return err ;
169
152
}
170
153
} else {
171
154
_this . _cache [ id ] = o ;
172
155
if ( cb != null ) {
173
- return cb ( null , id ) ;
156
+ cb ( null , id ) ;
174
157
} else {
175
158
return id ;
176
159
}
177
160
}
178
161
} ;
179
162
} ) ( this ) ;
163
+
180
164
if ( this . _memory ) {
181
165
return done ( ) ;
182
166
} else {
183
167
if ( cb != null ) {
184
- return saveObjectToFile . call ( this , data , file , done ) ;
168
+ saveObjectToFile . call ( this , data , file , done ) ;
185
169
} else {
186
170
return done ( saveObjectToFile . call ( this , data , file ) ) ;
187
171
}
188
172
}
189
173
} ;
190
174
191
- var get = function ( id , cb ) {
192
- var done , err , o ;
175
+ const get = function ( id , cb ) {
176
+ var err , o ;
193
177
o = clone ( this . _cache [ id ] ) ;
194
178
if ( o != null ) {
195
179
return ( cb != null ? cb ( null , o ) : o ) ;
196
180
}
197
- done = ( function ( _this ) {
181
+ const done = ( function ( _this ) {
198
182
return function ( err , o ) {
199
183
var e , item ;
200
184
if ( err ) {
@@ -232,14 +216,14 @@ var get = function(id, cb) {
232
216
return done ( ( err ? o : void 0 ) , ( ! err ? o : void 0 ) ) ;
233
217
} ;
234
218
235
- var remove = function ( id , cb ) {
236
- var cacheBackup , done , e , err , file , notInCache , o ;
237
- file = this . _getFileName ( id ) ;
219
+ const remove = function ( id , cb ) {
220
+ var cacheBackup , e , err , notInCache , o ;
221
+ const file = this . _getFileName ( id ) ;
238
222
cacheBackup = this . _cache [ id ] ;
239
223
if ( cacheBackup == null ) {
240
224
notInCache = new Error ( id + " does not exist" ) ;
241
225
}
242
- done = ( function ( _this ) {
226
+ const done = ( function ( _this ) {
243
227
return function ( err ) {
244
228
if ( err ) {
245
229
_this . _cache [ id ] = cacheBackup ;
@@ -275,9 +259,9 @@ var remove = function(id, cb) {
275
259
}
276
260
} ;
277
261
278
- var Store = ( function ( ) {
279
- function Store ( name , opt ) {
280
- var fn ;
262
+ class Store {
263
+
264
+ constructor ( name , opt ) {
281
265
this . name = name != null ? name : 'store' ;
282
266
if ( opt == null ) {
283
267
opt = { } ;
@@ -299,7 +283,7 @@ var Store = (function() {
299
283
mkdirp . sync ( this . _dir ) ;
300
284
}
301
285
if ( this . _single ) {
302
- fn = this . _getFileName ( ) ;
286
+ const fn = this . _getFileName ( ) ;
303
287
if ( ! this . _memory ) {
304
288
if ( ! fs . existsSync ( fn ) ) {
305
289
if ( fs . writeFileSync ( fn , "{}" , 'utf8' ) ) {
@@ -311,45 +295,47 @@ var Store = (function() {
311
295
}
312
296
}
313
297
314
- Store . prototype . _getFileName = function ( id ) {
298
+ _getFileName ( id ) {
315
299
if ( this . _single ) {
316
300
return path . join ( this . _dir , ( path . basename ( this . name ) ) + ".json" ) ;
317
301
} else {
318
302
return id2fileName ( id , this . _dir ) ;
319
303
}
320
- } ;
304
+ }
321
305
322
- Store . prototype . save = function ( id , o , cb ) {
306
+
307
+
308
+ save ( id , o , cb ) {
323
309
if ( cb == null ) {
324
310
cb = function ( ) { } ;
325
311
}
326
312
return save . call ( this , id , o , cb ) ;
327
313
} ;
328
314
329
- Store . prototype . saveSync = function ( id , o ) {
315
+ saveSync ( id , o ) {
330
316
return save . call ( this , id , o ) ;
331
317
} ;
332
318
333
- Store . prototype . get = function ( id , cb ) {
319
+ get ( id , cb ) {
334
320
if ( cb == null ) {
335
321
cb = function ( ) { } ;
336
322
}
337
323
return get . call ( this , id , cb ) ;
338
324
} ;
339
325
340
- Store . prototype . getSync = function ( id ) {
326
+ getSync ( id ) {
341
327
return get . call ( this , id ) ;
342
328
} ;
343
329
344
- Store . prototype [ " delete" ] = function ( id , cb ) {
330
+ delete ( id , cb ) {
345
331
return remove . call ( this , id , cb ) ;
346
332
} ;
347
333
348
- Store . prototype . deleteSync = function ( id ) {
334
+ deleteSync ( id ) {
349
335
return remove . call ( this , id ) ;
350
336
} ;
351
337
352
- Store . prototype . all = function ( cb ) {
338
+ all ( cb ) {
353
339
if ( cb == null ) {
354
340
cb = function ( ) { } ;
355
341
}
@@ -392,7 +378,7 @@ var Store = (function() {
392
378
}
393
379
} ;
394
380
395
- Store . prototype . allSync = function ( ) {
381
+ allSync ( ) {
396
382
var db , f , i , item , len , objects , ref ;
397
383
if ( this . _memory ) {
398
384
return this . _cache ;
@@ -417,10 +403,8 @@ var Store = (function() {
417
403
}
418
404
return objects ;
419
405
}
420
- } ;
421
-
422
- return Store ;
406
+ }
423
407
424
- } ) ( ) ;
408
+ }
425
409
426
410
module . exports = Store ;
0 commit comments