Skip to content

Commit 57d11bc

Browse files
claudiahdzisaacs
authored andcommittedFeb 17, 2020
feat: remove figgy-pudding
BREAKING CHANGE: drop figgy-pudding and use canonical option names.
1 parent a36bb4b commit 57d11bc

File tree

8 files changed

+85
-136
lines changed

8 files changed

+85
-136
lines changed
 

‎get.js

+28-39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
22

33
const util = require('util')
4-
5-
const figgyPudding = require('figgy-pudding')
64
const fs = require('fs')
75
const index = require('./lib/entry-index')
86
const memo = require('./lib/memoization')
@@ -14,25 +12,19 @@ const Pipeline = require('minipass-pipeline')
1412

1513
const writeFile = util.promisify(fs.writeFile)
1614

17-
const GetOpts = figgyPudding({
18-
integrity: {},
19-
memoize: {},
20-
size: {}
21-
})
22-
2315
module.exports = function get (cache, key, opts) {
2416
return getData(false, cache, key, opts)
2517
}
2618
module.exports.byDigest = function getByDigest (cache, digest, opts) {
2719
return getData(true, cache, digest, opts)
2820
}
2921

30-
function getData (byDigest, cache, key, opts) {
31-
opts = GetOpts(opts)
22+
function getData (byDigest, cache, key, opts = {}) {
23+
const { integrity, memoize, size } = opts
3224
const memoized = byDigest
3325
? memo.get.byDigest(cache, key, opts)
3426
: memo.get(cache, key, opts)
35-
if (memoized && opts.memoize !== false) {
27+
if (memoized && memoize !== false) {
3628
return Promise.resolve(
3729
byDigest
3830
? memoized
@@ -50,23 +42,23 @@ function getData (byDigest, cache, key, opts) {
5042
throw new index.NotFoundError(cache, key)
5143
}
5244
return read(cache, byDigest ? key : entry.integrity, {
53-
integrity: opts.integrity,
54-
size: opts.size
45+
integrity,
46+
size
5547
})
5648
.then((data) =>
5749
byDigest
5850
? data
5951
: {
52+
data,
6053
metadata: entry.metadata,
61-
data: data,
6254
size: entry.size,
6355
integrity: entry.integrity
6456
}
6557
)
6658
.then((res) => {
67-
if (opts.memoize && byDigest) {
59+
if (memoize && byDigest) {
6860
memo.put.byDigest(cache, key, res, opts)
69-
} else if (opts.memoize) {
61+
} else if (memoize) {
7062
memo.put(cache, entry, res.data, opts)
7163
}
7264
return res
@@ -82,12 +74,12 @@ module.exports.sync.byDigest = function getByDigest (cache, digest, opts) {
8274
return getDataSync(true, cache, digest, opts)
8375
}
8476

85-
function getDataSync (byDigest, cache, key, opts) {
86-
opts = GetOpts(opts)
77+
function getDataSync (byDigest, cache, key, opts = {}) {
78+
const { integrity, memoize, size } = opts
8779
const memoized = byDigest
8880
? memo.get.byDigest(cache, key, opts)
8981
: memo.get(cache, key, opts)
90-
if (memoized && opts.memoize !== false) {
82+
if (memoized && memoize !== false) {
9183
return byDigest
9284
? memoized
9385
: {
@@ -102,8 +94,8 @@ function getDataSync (byDigest, cache, key, opts) {
10294
throw new index.NotFoundError(cache, key)
10395
}
10496
const data = read.sync(cache, byDigest ? key : entry.integrity, {
105-
integrity: opts.integrity,
106-
size: opts.size
97+
integrity: integrity,
98+
size: size
10799
})
108100
const res = byDigest
109101
? data
@@ -113,9 +105,9 @@ function getDataSync (byDigest, cache, key, opts) {
113105
size: entry.size,
114106
integrity: entry.integrity
115107
}
116-
if (opts.memoize && byDigest) {
108+
if (memoize && byDigest) {
117109
memo.put.byDigest(cache, key, res, opts)
118-
} else if (opts.memoize) {
110+
} else if (memoize) {
119111
memo.put(cache, entry, res.data, opts)
120112
}
121113
return res
@@ -134,10 +126,10 @@ const getMemoizedStream = (memoized) => {
134126
return stream
135127
}
136128

137-
function getStream (cache, key, opts) {
138-
opts = GetOpts(opts)
129+
function getStream (cache, key, opts = {}) {
130+
const { memoize, size } = opts
139131
const memoized = memo.get(cache, key, opts)
140-
if (memoized && opts.memoize !== false) {
132+
if (memoized && memoize !== false) {
141133
return getMemoizedStream(memoized)
142134
}
143135

@@ -160,12 +152,10 @@ function getStream (cache, key, opts) {
160152
const src = read.readStream(
161153
cache,
162154
entry.integrity,
163-
opts.concat({
164-
size: opts.size == null ? entry.size : opts.size
165-
})
155+
{ ...opts, size: typeof size !== 'number' ? entry.size : size }
166156
)
167157

168-
if (opts.memoize) {
158+
if (memoize) {
169159
const memoStream = new Collect.PassThrough()
170160
memoStream.on('collect', data => memo.put(cache, entry, data, opts))
171161
stream.unshift(memoStream)
@@ -179,16 +169,16 @@ function getStream (cache, key, opts) {
179169

180170
module.exports.stream.byDigest = getStreamDigest
181171

182-
function getStreamDigest (cache, integrity, opts) {
183-
opts = GetOpts(opts)
172+
function getStreamDigest (cache, integrity, opts = {}) {
173+
const { memoize } = opts
184174
const memoized = memo.get.byDigest(cache, integrity, opts)
185-
if (memoized && opts.memoize !== false) {
175+
if (memoized && memoize !== false) {
186176
const stream = new Minipass()
187177
stream.end(memoized)
188178
return stream
189179
} else {
190180
const stream = read.readStream(cache, integrity, opts)
191-
if (!opts.memoize) {
181+
if (!memoize) {
192182
return stream
193183
}
194184
const memoStream = new Collect.PassThrough()
@@ -204,10 +194,10 @@ function getStreamDigest (cache, integrity, opts) {
204194

205195
module.exports.info = info
206196

207-
function info (cache, key, opts) {
208-
opts = GetOpts(opts)
197+
function info (cache, key, opts = {}) {
198+
const { memoize } = opts
209199
const memoized = memo.get(cache, key, opts)
210-
if (memoized && opts.memoize !== false) {
200+
if (memoized && memoize !== false) {
211201
return Promise.resolve(memoized.entry)
212202
} else {
213203
return index.find(cache, key)
@@ -228,8 +218,7 @@ function cpDigest (cache, digest, dest, opts) {
228218

229219
module.exports.copy.byDigest = cpDigest
230220

231-
function copy (byDigest, cache, key, dest, opts) {
232-
opts = GetOpts(opts)
221+
function copy (byDigest, cache, key, dest, opts = {}) {
233222
if (read.copy) {
234223
return (byDigest
235224
? Promise.resolve(null)

‎lib/content/read.js

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

33
const util = require('util')
44

5-
const figgyPudding = require('figgy-pudding')
65
const fs = require('fs')
76
const fsm = require('fs-minipass')
87
const ssri = require('ssri')
@@ -12,21 +11,17 @@ const Pipeline = require('minipass-pipeline')
1211
const lstat = util.promisify(fs.lstat)
1312
const readFile = util.promisify(fs.readFile)
1413

15-
const ReadOpts = figgyPudding({
16-
size: {}
17-
})
18-
1914
module.exports = read
2015

2116
const MAX_SINGLE_READ_SIZE = 64 * 1024 * 1024
22-
function read (cache, integrity, opts) {
23-
opts = ReadOpts(opts)
17+
function read (cache, integrity, opts = {}) {
18+
const { size } = opts
2419
return withContentSri(cache, integrity, (cpath, sri) => {
2520
// get size
2621
return lstat(cpath).then(stat => ({ stat, cpath, sri }))
2722
}).then(({ stat, cpath, sri }) => {
28-
if (typeof opts.size === 'number' && stat.size !== opts.size) {
29-
throw sizeError(opts.size, stat.size)
23+
if (typeof size === 'number' && stat.size !== size) {
24+
throw sizeError(size, stat.size)
3025
}
3126
if (stat.size > MAX_SINGLE_READ_SIZE) {
3227
return readPipeline(cpath, stat.size, sri, new Pipeline()).concat()
@@ -57,12 +52,12 @@ const readPipeline = (cpath, size, sri, stream) => {
5752

5853
module.exports.sync = readSync
5954

60-
function readSync (cache, integrity, opts) {
61-
opts = ReadOpts(opts)
55+
function readSync (cache, integrity, opts = {}) {
56+
const { size } = opts
6257
return withContentSriSync(cache, integrity, (cpath, sri) => {
6358
const data = fs.readFileSync(cpath)
64-
if (typeof opts.size === 'number' && opts.size !== data.length) {
65-
throw sizeError(opts.size, data.length)
59+
if (typeof size === 'number' && size !== data.length) {
60+
throw sizeError(size, data.length)
6661
}
6762

6863
if (ssri.checkData(data, sri)) {
@@ -76,16 +71,15 @@ function readSync (cache, integrity, opts) {
7671
module.exports.stream = readStream
7772
module.exports.readStream = readStream
7873

79-
function readStream (cache, integrity, opts) {
80-
opts = ReadOpts(opts)
81-
74+
function readStream (cache, integrity, opts = {}) {
75+
const { size } = opts
8276
const stream = new Pipeline()
8377
withContentSri(cache, integrity, (cpath, sri) => {
8478
// just lstat to ensure it exists
8579
return lstat(cpath).then((stat) => ({ stat, cpath, sri }))
8680
}).then(({ stat, cpath, sri }) => {
87-
if (typeof opts.size === 'number' && opts.size !== stat.size) {
88-
return stream.emit('error', sizeError(opts.size, stat.size))
81+
if (typeof size === 'number' && size !== stat.size) {
82+
return stream.emit('error', sizeError(size, stat.size))
8983
}
9084
readPipeline(cpath, stat.size, sri, stream)
9185
}, er => stream.emit('error', er))
@@ -100,15 +94,13 @@ if (fs.copyFile) {
10094
copyFile = util.promisify(fs.copyFile)
10195
}
10296

103-
function copy (cache, integrity, dest, opts) {
104-
opts = ReadOpts(opts)
97+
function copy (cache, integrity, dest) {
10598
return withContentSri(cache, integrity, (cpath, sri) => {
10699
return copyFile(cpath, dest)
107100
})
108101
}
109102

110-
function copySync (cache, integrity, dest, opts) {
111-
opts = ReadOpts(opts)
103+
function copySync (cache, integrity, dest) {
112104
return withContentSriSync(cache, integrity, (cpath, sri) => {
113105
return fs.copyFileSync(cpath, dest)
114106
})

‎lib/content/write.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ const writeFile = util.promisify(fs.writeFile)
2020

2121
module.exports = write
2222

23-
function write (cache, data, opts) {
24-
opts = opts || {}
25-
if (opts.algorithms && opts.algorithms.length > 1) {
23+
function write (cache, data, opts = {}) {
24+
const { algorithms, size, integrity } = opts
25+
if (algorithms && algorithms.length > 1) {
2626
throw new Error('opts.algorithms only supports a single algorithm for now')
2727
}
28-
if (typeof opts.size === 'number' && data.length !== opts.size) {
29-
return Promise.reject(sizeError(opts.size, data.length))
28+
if (typeof size === 'number' && data.length !== size) {
29+
return Promise.reject(sizeError(size, data.length))
3030
}
31-
const sri = ssri.fromData(data, {
32-
algorithms: opts.algorithms
33-
})
34-
if (opts.integrity && !ssri.checkData(data, opts.integrity, opts)) {
35-
return Promise.reject(checksumError(opts.integrity, sri))
31+
const sri = ssri.fromData(data, { algorithms })
32+
if (integrity && !ssri.checkData(data, integrity, opts)) {
33+
return Promise.reject(checksumError(integrity, sri))
3634
}
3735

3836
return disposer(makeTmp(cache, opts), makeTmpDisposer,
@@ -90,8 +88,7 @@ class CacacheWriteStream extends Flush {
9088
}
9189
}
9290

93-
function writeStream (cache, opts) {
94-
opts = opts || {}
91+
function writeStream (cache, opts = {}) {
9592
return new CacacheWriteStream(cache, opts)
9693
}
9794

‎lib/entry-index.js

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

55
const crypto = require('crypto')
6-
const figgyPudding = require('figgy-pudding')
76
const fs = require('fs')
87
const Minipass = require('minipass')
98
const path = require('path')
@@ -26,22 +25,17 @@ module.exports.NotFoundError = class NotFoundError extends Error {
2625
}
2726
}
2827

29-
const IndexOpts = figgyPudding({
30-
metadata: {},
31-
size: {}
32-
})
33-
3428
module.exports.insert = insert
3529

36-
function insert (cache, key, integrity, opts) {
37-
opts = IndexOpts(opts)
30+
function insert (cache, key, integrity, opts = {}) {
31+
const { metadata, size } = opts
3832
const bucket = bucketPath(cache, key)
3933
const entry = {
4034
key,
4135
integrity: integrity && ssri.stringify(integrity),
4236
time: Date.now(),
43-
size: opts.size,
44-
metadata: opts.metadata
37+
size,
38+
metadata
4539
}
4640
return fixOwner
4741
.mkdirfix(cache, path.dirname(bucket))
@@ -75,15 +69,15 @@ function insert (cache, key, integrity, opts) {
7569

7670
module.exports.insert.sync = insertSync
7771

78-
function insertSync (cache, key, integrity, opts) {
79-
opts = IndexOpts(opts)
72+
function insertSync (cache, key, integrity, opts = {}) {
73+
const { metadata, size } = opts
8074
const bucket = bucketPath(cache, key)
8175
const entry = {
8276
key,
8377
integrity: integrity && ssri.stringify(integrity),
8478
time: Date.now(),
85-
size: opts.size,
86-
metadata: opts.metadata
79+
size,
80+
metadata
8781
}
8882
fixOwner.mkdirfix.sync(cache, path.dirname(bucket))
8983
const stringified = JSON.stringify(entry)

‎lib/util/tmp.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22

33
const util = require('util')
44

5-
const figgyPudding = require('figgy-pudding')
65
const fixOwner = require('./fix-owner')
76
const path = require('path')
87
const rimraf = util.promisify(require('rimraf'))
98
const uniqueFilename = require('unique-filename')
109
const { disposer } = require('./disposer')
1110

12-
const TmpOpts = figgyPudding({
13-
tmpPrefix: {}
14-
})
15-
1611
module.exports.mkdir = mktmpdir
1712

18-
function mktmpdir (cache, opts) {
19-
opts = TmpOpts(opts)
20-
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
13+
function mktmpdir (cache, opts = {}) {
14+
const { tmpPrefix } = opts
15+
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), tmpPrefix)
2116
return fixOwner.mkdirfix(cache, tmpTarget).then(() => {
2217
return tmpTarget
2318
})
@@ -28,10 +23,8 @@ module.exports.withTmp = withTmp
2823
function withTmp (cache, opts, cb) {
2924
if (!cb) {
3025
cb = opts
31-
opts = null
26+
opts = {}
3227
}
33-
opts = TmpOpts(opts)
34-
3528
return disposer(mktmpdir(cache, opts), rimraf, cb)
3629
}
3730

‎lib/verify.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const util = require('util')
44

55
const pMap = require('p-map')
66
const contentPath = require('./content/path')
7-
const figgyPudding = require('figgy-pudding')
87
const fixOwner = require('./util/fix-owner')
98
const fs = require('fs')
109
const fsm = require('fs-minipass')
@@ -22,20 +21,16 @@ const truncate = util.promisify(fs.truncate)
2221
const writeFile = util.promisify(fs.writeFile)
2322
const readFile = util.promisify(fs.readFile)
2423

25-
const VerifyOpts = figgyPudding({
26-
concurrency: {
27-
default: 20
28-
},
29-
filter: {},
30-
log: {
31-
default: { silly () {} }
32-
}
24+
const verifyOpts = (opts) => ({
25+
concurrency: 20,
26+
log: { silly () {} },
27+
...opts
3328
})
3429

3530
module.exports = verify
3631

3732
function verify (cache, opts) {
38-
opts = VerifyOpts(opts)
33+
opts = verifyOpts(opts)
3934
opts.log.silly('verify', 'verifying cache at', cache)
4035

4136
const steps = [

‎package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@
5858
],
5959
"license": "ISC",
6060
"dependencies": {
61-
"chownr": "^1.1.4",
62-
"figgy-pudding": "^3.5.1",
63-
"fs-minipass": "^2.1.0",
64-
"glob": "^7.1.6",
61+
"chownr": "^1.1.2",
62+
"fs-minipass": "^2.0.0",
63+
"glob": "^7.1.4",
6564
"infer-owner": "^1.0.4",
6665
"lru-cache": "^5.1.1",
6766
"minipass": "^3.1.1",

‎put.js

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
'use strict'
22

3-
const figgyPudding = require('figgy-pudding')
43
const index = require('./lib/entry-index')
54
const memo = require('./lib/memoization')
65
const write = require('./lib/content/write')
76
const Flush = require('minipass-flush')
87
const { PassThrough } = require('minipass-collect')
98
const Pipeline = require('minipass-pipeline')
109

11-
const PutOpts = figgyPudding({
12-
algorithms: {
13-
default: ['sha512']
14-
},
15-
integrity: {},
16-
memoize: {},
17-
metadata: {},
18-
pickAlgorithm: {},
19-
size: {},
20-
tmpPrefix: {},
21-
single: {},
22-
sep: {},
23-
error: {},
24-
strict: {}
10+
const putOpts = (opts) => ({
11+
algorithms: ['sha512'],
12+
...opts
2513
})
2614

2715
module.exports = putData
2816

29-
function putData (cache, key, data, opts) {
30-
opts = PutOpts(opts)
17+
function putData (cache, key, data, opts = {}) {
18+
const { memoize } = opts
19+
opts = putOpts(opts)
3120
return write(cache, data, opts).then((res) => {
3221
return index
33-
.insert(cache, key, res.integrity, opts.concat({ size: res.size }))
22+
.insert(cache, key, res.integrity, { ...opts, size: res.size })
3423
.then((entry) => {
35-
if (opts.memoize) {
24+
if (memoize) {
3625
memo.put(cache, entry, data, opts)
3726
}
3827
return res.integrity
@@ -42,16 +31,17 @@ function putData (cache, key, data, opts) {
4231

4332
module.exports.stream = putStream
4433

45-
function putStream (cache, key, opts) {
46-
opts = PutOpts(opts)
34+
function putStream (cache, key, opts = {}) {
35+
const { memoize } = opts
36+
opts = putOpts(opts)
4737
let integrity
4838
let size
4939

5040
let memoData
5141
const pipeline = new Pipeline()
5242
// first item in the pipeline is the memoizer, because we need
5343
// that to end first and get the collected data.
54-
if (opts.memoize) {
44+
if (memoize) {
5545
const memoizer = new PassThrough().on('collect', data => {
5646
memoData = data
5747
})
@@ -75,9 +65,9 @@ function putStream (cache, key, opts) {
7565
pipeline.push(new Flush({
7666
flush () {
7767
return index
78-
.insert(cache, key, integrity, opts.concat({ size }))
68+
.insert(cache, key, integrity, { ...opts, size })
7969
.then((entry) => {
80-
if (opts.memoize && memoData) {
70+
if (memoize && memoData) {
8171
memo.put(cache, entry, memoData, opts)
8272
}
8373
if (integrity) {

0 commit comments

Comments
 (0)
Please sign in to comment.