1
1
'use strict'
2
2
3
- const BB = require ( 'bluebird ' )
3
+ const util = require ( 'util ' )
4
4
5
5
const contentPath = require ( './content/path' )
6
6
const crypto = require ( 'crypto' )
@@ -15,9 +15,9 @@ const Y = require('./util/y.js')
15
15
16
16
const indexV = require ( '../package.json' ) [ 'cache-version' ] . index
17
17
18
- const appendFile = BB . promisify ( fs . appendFile )
19
- const readFile = BB . promisify ( fs . readFile )
20
- const readdir = BB . promisify ( fs . readdir )
18
+ const appendFile = util . promisify ( fs . appendFile )
19
+ const readFile = util . promisify ( fs . readFile )
20
+ const readdir = util . promisify ( fs . readdir )
21
21
22
22
module . exports . NotFoundError = class NotFoundError extends Error {
23
23
constructor ( cache , key ) {
@@ -34,6 +34,7 @@ const IndexOpts = figgyPudding({
34
34
} )
35
35
36
36
module . exports . insert = insert
37
+
37
38
function insert ( cache , key , integrity , opts ) {
38
39
opts = IndexOpts ( opts )
39
40
const bucket = bucketPath ( cache , key )
@@ -76,6 +77,7 @@ function insert (cache, key, integrity, opts) {
76
77
}
77
78
78
79
module . exports . insert . sync = insertSync
80
+
79
81
function insertSync ( cache , key , integrity , opts ) {
80
82
opts = IndexOpts ( opts )
81
83
const bucket = bucketPath ( cache , key )
@@ -102,6 +104,7 @@ function insertSync (cache, key, integrity, opts) {
102
104
}
103
105
104
106
module . exports . find = find
107
+
105
108
function find ( cache , key ) {
106
109
const bucket = bucketPath ( cache , key )
107
110
return bucketEntries ( bucket ) . then ( ( entries ) => {
@@ -122,6 +125,7 @@ function find (cache, key) {
122
125
}
123
126
124
127
module . exports . find . sync = findSync
128
+
125
129
function findSync ( cache , key ) {
126
130
const bucket = bucketPath ( cache , key )
127
131
try {
@@ -142,55 +146,72 @@ function findSync (cache, key) {
142
146
}
143
147
144
148
module . exports . delete = del
149
+
145
150
function del ( cache , key , opts ) {
146
151
return insert ( cache , key , null , opts )
147
152
}
148
153
149
154
module . exports . delete . sync = delSync
155
+
150
156
function delSync ( cache , key , opts ) {
151
157
return insertSync ( cache , key , null , opts )
152
158
}
153
159
154
160
module . exports . lsStream = lsStream
161
+
155
162
function lsStream ( cache ) {
156
163
const indexDir = bucketDir ( cache )
157
164
const stream = from . obj ( )
158
165
159
166
// "/cachename/*"
160
- readdirOrEmpty ( indexDir ) . map ( bucket => {
161
- const bucketPath = path . join ( indexDir , bucket )
162
-
163
- // "/cachename/<bucket 0xFF>/*"
164
- return readdirOrEmpty ( bucketPath ) . map ( subbucket => {
165
- const subbucketPath = path . join ( bucketPath , subbucket )
166
-
167
- // "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
168
- return readdirOrEmpty ( subbucketPath ) . map ( entry => {
169
- const getKeyToEntry = bucketEntries (
170
- path . join ( subbucketPath , entry )
171
- ) . reduce ( ( acc , entry ) => {
172
- acc . set ( entry . key , entry )
173
- return acc
174
- } , new Map ( ) )
175
-
176
- return getKeyToEntry . then ( ( reduced ) => {
177
- for ( let entry of reduced . values ( ) ) {
178
- const formatted = formatEntry ( cache , entry )
179
- formatted && stream . push ( formatted )
180
- }
181
- } ) . catch ( ( err ) => { if ( err . code === 'ENOENT' ) { return undefined } throw err } )
182
- } )
167
+ readdirOrEmpty ( indexDir )
168
+ . then ( ( buckets ) => {
169
+ return Promise . all ( buckets . map ( ( bucket ) => {
170
+ const bucketPath = path . join ( indexDir , bucket )
171
+
172
+ // "/cachename/<bucket 0xFF>/*"
173
+ return readdirOrEmpty ( bucketPath ) . then ( ( subbuckets ) => {
174
+ return Promise . all ( subbuckets . map ( ( subbucket ) => {
175
+ const subbucketPath = path . join ( bucketPath , subbucket )
176
+
177
+ // "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
178
+ return readdirOrEmpty ( subbucketPath ) . then ( ( entries ) => {
179
+ return Promise . all ( entries . map ( ( entry ) => {
180
+ const getKeyToEntry = bucketEntries (
181
+ path . join ( subbucketPath , entry )
182
+ ) . then ( ( entries ) => {
183
+ return entries . reduce ( ( acc , entry ) => {
184
+ acc . set ( entry . key , entry )
185
+ return acc
186
+ } , new Map ( ) )
187
+ } )
188
+
189
+ return getKeyToEntry . then ( ( reduced ) => {
190
+ for ( let entry of reduced . values ( ) ) {
191
+ const formatted = formatEntry ( cache , entry )
192
+ formatted && stream . push ( formatted )
193
+ }
194
+ } ) . catch ( ( err ) => {
195
+ if ( err . code === 'ENOENT' ) { return undefined }
196
+ throw err
197
+ } )
198
+ } ) )
199
+ } )
200
+ } ) )
201
+ } )
202
+ } ) )
203
+ } )
204
+ . then ( ( ) => {
205
+ stream . push ( null )
206
+ } , err => {
207
+ stream . emit ( 'error' , err )
183
208
} )
184
- } ) . then ( ( ) => {
185
- stream . push ( null )
186
- } , err => {
187
- stream . emit ( 'error' , err )
188
- } )
189
209
190
210
return stream
191
211
}
192
212
193
213
module . exports . ls = ls
214
+
194
215
function ls ( cache ) {
195
216
return new Promise ( ( resolve , reject ) => {
196
217
lsStream ( cache ) . on ( 'error' , reject ) . pipe ( concat ( entries => {
@@ -238,11 +259,13 @@ function _bucketEntries (data, filter) {
238
259
}
239
260
240
261
module . exports . _bucketDir = bucketDir
262
+
241
263
function bucketDir ( cache ) {
242
264
return path . join ( cache , `index-v${ indexV } ` )
243
265
}
244
266
245
267
module . exports . _bucketPath = bucketPath
268
+
246
269
function bucketPath ( cache , key ) {
247
270
const hashed = hashKey ( key )
248
271
return path . join . apply ( path , [ bucketDir ( cache ) ] . concat (
@@ -251,11 +274,13 @@ function bucketPath (cache, key) {
251
274
}
252
275
253
276
module . exports . _hashKey = hashKey
277
+
254
278
function hashKey ( key ) {
255
279
return hash ( key , 'sha256' )
256
280
}
257
281
258
282
module . exports . _hashEntry = hashEntry
283
+
259
284
function hashEntry ( str ) {
260
285
return hash ( str , 'sha1' )
261
286
}
0 commit comments