Skip to content

Commit 33d4eed

Browse files
authoredApr 9, 2018
feat(opts): use figgy-pudding for opts (#128)
1 parent 529f347 commit 33d4eed

File tree

9 files changed

+862
-800
lines changed

9 files changed

+862
-800
lines changed
 

‎get.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const BB = require('bluebird')
44

5+
const figgyPudding = require('figgy-pudding')
56
const fs = require('fs')
67
const index = require('./lib/entry-index')
78
const memo = require('./lib/memoization')
@@ -10,14 +11,20 @@ const pipeline = require('mississippi').pipeline
1011
const read = require('./lib/content/read')
1112
const through = require('mississippi').through
1213

14+
const GetOpts = figgyPudding({
15+
integrity: {},
16+
memoize: {},
17+
size: {}
18+
})
19+
1320
module.exports = function get (cache, key, opts) {
1421
return getData(false, cache, key, opts)
1522
}
1623
module.exports.byDigest = function getByDigest (cache, digest, opts) {
1724
return getData(true, cache, digest, opts)
1825
}
1926
function getData (byDigest, cache, key, opts) {
20-
opts = opts || {}
27+
opts = GetOpts(opts)
2128
const memoized = (
2229
byDigest
2330
? memo.get.byDigest(cache, key, opts)
@@ -58,7 +65,7 @@ function getData (byDigest, cache, key, opts) {
5865

5966
module.exports.stream = getStream
6067
function getStream (cache, key, opts) {
61-
opts = opts || {}
68+
opts = GetOpts(opts)
6269
let stream = through()
6370
const memoized = memo.get(cache, key, opts)
6471
if (memoized && opts.memoize !== false) {
@@ -91,7 +98,6 @@ function getStream (cache, key, opts) {
9198
} else {
9299
memoStream = through()
93100
}
94-
opts.size = opts.size == null ? entry.size : opts.size
95101
stream.emit('metadata', entry.metadata)
96102
stream.emit('integrity', entry.integrity)
97103
stream.emit('size', entry.size)
@@ -101,7 +107,9 @@ function getStream (cache, key, opts) {
101107
ev === 'size' && cb(entry.size)
102108
})
103109
pipe(
104-
read.readStream(cache, entry.integrity, opts),
110+
read.readStream(cache, entry.integrity, opts.concat({
111+
size: opts.size == null ? entry.size : opts.size
112+
})),
105113
memoStream,
106114
stream
107115
)
@@ -111,7 +119,7 @@ function getStream (cache, key, opts) {
111119

112120
module.exports.stream.byDigest = getStreamDigest
113121
function getStreamDigest (cache, integrity, opts) {
114-
opts = opts || {}
122+
opts = GetOpts(opts)
115123
const memoized = memo.get.byDigest(cache, integrity, opts)
116124
if (memoized && opts.memoize !== false) {
117125
const stream = through()
@@ -143,7 +151,7 @@ function getStreamDigest (cache, integrity, opts) {
143151

144152
module.exports.info = info
145153
function info (cache, key, opts) {
146-
opts = opts || {}
154+
opts = GetOpts(opts)
147155
const memoized = memo.get(cache, key, opts)
148156
if (memoized && opts.memoize !== false) {
149157
return BB.resolve(memoized.entry)
@@ -161,7 +169,7 @@ module.exports.copy.byDigest = function cpDigest (cache, digest, dest, opts) {
161169
return copy(true, cache, digest, dest, opts)
162170
}
163171
function copy (byDigest, cache, key, dest, opts) {
164-
opts = opts || {}
172+
opts = GetOpts(opts)
165173
if (read.copy) {
166174
return (
167175
byDigest ? BB.resolve(null) : index.find(cache, key, opts)

‎lib/content/read.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const BB = require('bluebird')
44

55
const contentPath = require('./path')
6+
const figgyPudding = require('figgy-pudding')
67
const fs = require('graceful-fs')
78
const PassThrough = require('stream').PassThrough
89
const pipe = BB.promisify(require('mississippi').pipe)
@@ -11,9 +12,13 @@ const Y = require('../util/y.js')
1112

1213
BB.promisifyAll(fs)
1314

15+
const ReadOpts = figgyPudding({
16+
size: {}
17+
})
18+
1419
module.exports = read
1520
function read (cache, integrity, opts) {
16-
opts = opts || {}
21+
opts = ReadOpts(opts)
1722
return pickContentSri(cache, integrity).then(content => {
1823
const sri = content.sri
1924
const cpath = contentPath(cache, sri)
@@ -32,7 +37,7 @@ function read (cache, integrity, opts) {
3237
module.exports.stream = readStream
3338
module.exports.readStream = readStream
3439
function readStream (cache, integrity, opts) {
35-
opts = opts || {}
40+
opts = ReadOpts(opts)
3641
const stream = new PassThrough()
3742
pickContentSri(
3843
cache, integrity
@@ -56,7 +61,7 @@ if (fs.copyFile) {
5661
module.exports.copy = copy
5762
}
5863
function copy (cache, integrity, dest, opts) {
59-
opts = opts || {}
64+
opts = ReadOpts(opts)
6065
return pickContentSri(cache, integrity).then(content => {
6166
const sri = content.sri
6267
const cpath = contentPath(cache, sri)

‎lib/content/write.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function write (cache, data, opts) {
2828
if (typeof opts.size === 'number' && data.length !== opts.size) {
2929
return BB.reject(sizeError(opts.size, data.length))
3030
}
31-
const sri = ssri.fromData(data, opts)
31+
const sri = ssri.fromData(data, {
32+
algorithms: opts.algorithms
33+
})
3234
if (opts.integrity && !ssri.checkData(data, opts.integrity, opts)) {
3335
return BB.reject(checksumError(opts.integrity, sri))
3436
}

‎lib/entry-index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const BB = require('bluebird')
44

55
const contentPath = require('./content/path')
66
const crypto = require('crypto')
7+
const figgyPudding = require('figgy-pudding')
78
const fixOwner = require('./util/fix-owner')
89
const fs = require('graceful-fs')
910
const hashToSegments = require('./util/hash-to-segments')
@@ -29,9 +30,16 @@ module.exports.NotFoundError = class NotFoundError extends Error {
2930
}
3031
}
3132

33+
const IndexOpts = figgyPudding({
34+
metadata: {},
35+
size: {},
36+
uid: {},
37+
gid: {}
38+
})
39+
3240
module.exports.insert = insert
3341
function insert (cache, key, integrity, opts) {
34-
opts = opts || {}
42+
opts = IndexOpts(opts)
3543
const bucket = bucketPath(cache, key)
3644
const entry = {
3745
key,

‎lib/util/tmp.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
const BB = require('bluebird')
44

5+
const figgyPudding = require('figgy-pudding')
56
const fixOwner = require('./fix-owner')
67
const path = require('path')
78
const rimraf = BB.promisify(require('rimraf'))
89
const uniqueFilename = require('unique-filename')
910

11+
const TmpOpts = figgyPudding({
12+
tmpPrefix: {},
13+
uid: {},
14+
gid: {}
15+
})
16+
1017
module.exports.mkdir = mktmpdir
1118
function mktmpdir (cache, opts) {
12-
opts = opts || {}
19+
opts = TmpOpts(opts)
1320
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
1421
return fixOwner.mkdirfix(tmpTarget, opts.uid, opts.gid).then(() => {
1522
return tmpTarget
@@ -22,11 +29,12 @@ function withTmp (cache, opts, cb) {
2229
cb = opts
2330
opts = null
2431
}
25-
opts = opts || {}
32+
opts = TmpOpts(opts)
2633
return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb)
2734
}
2835

2936
module.exports.fix = fixtmpdir
3037
function fixtmpdir (cache, opts) {
38+
opts = TmpOpts(opts)
3139
return fixOwner(path.join(cache, 'tmp'), opts.uid, opts.gid)
3240
}

‎lib/verify.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const BB = require('bluebird')
44

55
const contentPath = require('./content/path')
6+
const figgyPudding = require('figgy-pudding')
67
const finished = BB.promisify(require('mississippi').finished)
78
const fixOwner = require('./util/fix-owner')
89
const fs = require('graceful-fs')
@@ -14,10 +15,22 @@ const ssri = require('ssri')
1415

1516
BB.promisifyAll(fs)
1617

18+
const VerifyOpts = figgyPudding({
19+
concurrency: {
20+
default: 20
21+
},
22+
filter: {},
23+
log: {
24+
default: { silly () {} }
25+
},
26+
uid: {},
27+
gid: {}
28+
})
29+
1730
module.exports = verify
1831
function verify (cache, opts) {
19-
opts = opts || {}
20-
opts.log && opts.log.silly('verify', 'verifying cache at', cache)
32+
opts = VerifyOpts(opts)
33+
opts.log.silly('verify', 'verifying cache at', cache)
2134
return BB.reduce([
2235
markStartTime,
2336
fixPerms,
@@ -40,7 +53,7 @@ function verify (cache, opts) {
4053
})
4154
}, {}).tap(stats => {
4255
stats.runTime.total = stats.endTime - stats.startTime
43-
opts.log && opts.log.silly('verify', 'verification finished for', cache, 'in', `${stats.runTime.total}ms`)
56+
opts.log.silly('verify', 'verification finished for', cache, 'in', `${stats.runTime.total}ms`)
4457
})
4558
}
4659

@@ -53,7 +66,7 @@ function markEndTime (cache, opts) {
5366
}
5467

5568
function fixPerms (cache, opts) {
56-
opts.log && opts.log.silly('verify', 'fixing cache permissions')
69+
opts.log.silly('verify', 'fixing cache permissions')
5770
return fixOwner.mkdirfix(cache, opts.uid, opts.gid).then(() => {
5871
// TODO - fix file permissions too
5972
return fixOwner.chownr(cache, opts.uid, opts.gid)
@@ -70,11 +83,11 @@ function fixPerms (cache, opts) {
7083
// 5. If content is not marked as live, rimraf it.
7184
//
7285
function garbageCollect (cache, opts) {
73-
opts.log && opts.log.silly('verify', 'garbage collecting content')
86+
opts.log.silly('verify', 'garbage collecting content')
7487
const indexStream = index.lsStream(cache)
7588
const liveContent = new Set()
7689
indexStream.on('data', entry => {
77-
if (opts && opts.filter && !opts.filter(entry)) { return }
90+
if (opts.filter && !opts.filter(entry)) { return }
7891
liveContent.add(entry.integrity.toString())
7992
})
8093
return finished(indexStream).then(() => {
@@ -117,7 +130,7 @@ function garbageCollect (cache, opts) {
117130
})
118131
})
119132
}
120-
}, {concurrency: opts.concurrency || 20}))
133+
}, {concurrency: opts.concurrency}))
121134
})
122135
})
123136
}
@@ -141,7 +154,7 @@ function verifyContent (filepath, sri) {
141154
}
142155

143156
function rebuildIndex (cache, opts) {
144-
opts.log && opts.log.silly('verify', 'rebuilding index')
157+
opts.log.silly('verify', 'rebuilding index')
145158
return index.ls(cache).then(entries => {
146159
const stats = {
147160
missingContent: 0,
@@ -153,7 +166,7 @@ function rebuildIndex (cache, opts) {
153166
if (entries.hasOwnProperty(k)) {
154167
const hashed = index._hashKey(k)
155168
const entry = entries[k]
156-
const excluded = opts && opts.filter && !opts.filter(entry)
169+
const excluded = opts.filter && !opts.filter(entry)
157170
excluded && stats.rejectedEntries++
158171
if (buckets[hashed] && !excluded) {
159172
buckets[hashed].push(entry)
@@ -170,7 +183,7 @@ function rebuildIndex (cache, opts) {
170183
}
171184
return BB.map(Object.keys(buckets), key => {
172185
return rebuildBucket(cache, buckets[key], stats, opts)
173-
}, {concurrency: opts.concurrency || 20}).then(() => stats)
186+
}, {concurrency: opts.concurrency}).then(() => stats)
174187
})
175188
}
176189

@@ -195,13 +208,13 @@ function rebuildBucket (cache, bucket, stats, opts) {
195208
}
196209

197210
function cleanTmp (cache, opts) {
198-
opts.log && opts.log.silly('verify', 'cleaning tmp directory')
211+
opts.log.silly('verify', 'cleaning tmp directory')
199212
return rimraf(path.join(cache, 'tmp'))
200213
}
201214

202215
function writeVerifile (cache, opts) {
203216
const verifile = path.join(cache, '_lastverified')
204-
opts.log && opts.log.silly('verify', 'writing verifile to ' + verifile)
217+
opts.log.silly('verify', 'writing verifile to ' + verifile)
205218
return fs.writeFileAsync(verifile, '' + (+(new Date())))
206219
}
207220

‎package-lock.json

+771-766
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"dependencies": {
6161
"bluebird": "^3.5.1",
6262
"chownr": "^1.0.1",
63+
"figgy-pudding": "^3.1.0",
6364
"glob": "^7.1.2",
6465
"graceful-fs": "^4.1.11",
6566
"lru-cache": "^4.1.2",

‎put.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
'use strict'
22

3+
const figgyPudding = require('figgy-pudding')
34
const index = require('./lib/entry-index')
45
const memo = require('./lib/memoization')
56
const write = require('./lib/content/write')
67
const to = require('mississippi').to
78

9+
const PutOpts = figgyPudding({
10+
algorithms: {
11+
default: ['sha512']
12+
},
13+
integrity: {},
14+
memoize: {},
15+
metadata: {},
16+
size: {},
17+
tmpPrefix: {},
18+
uid: {},
19+
gid: {}
20+
})
21+
822
module.exports = putData
923
function putData (cache, key, data, opts) {
10-
opts = opts || {}
24+
opts = PutOpts(opts)
1125
return write(cache, data, opts).then(res => {
12-
// TODO - stop modifying opts
13-
opts.size = res.size
14-
return index.insert(cache, key, res.integrity, opts).then(entry => {
26+
return index.insert(
27+
cache, key, res.integrity, opts.concat({size: res.size})
28+
).then(entry => {
1529
if (opts.memoize) {
1630
memo.put(cache, entry, data, opts)
1731
}
@@ -22,7 +36,7 @@ function putData (cache, key, data, opts) {
2236

2337
module.exports.stream = putStream
2438
function putStream (cache, key, opts) {
25-
opts = opts || {}
39+
opts = PutOpts(opts)
2640
let integrity
2741
let size
2842
const contentStream = write.stream(
@@ -45,9 +59,7 @@ function putStream (cache, key, opts) {
4559
})
4660
}, cb => {
4761
contentStream.end(() => {
48-
// TODO - stop modifying `opts`
49-
opts.size = size
50-
index.insert(cache, key, integrity, opts).then(entry => {
62+
index.insert(cache, key, integrity, opts.concat({size})).then(entry => {
5163
if (opts.memoize) {
5264
memo.put(cache, entry, Buffer.concat(memoData, memoTotal), opts)
5365
}

0 commit comments

Comments
 (0)
Please sign in to comment.