Skip to content
This repository was archived by the owner on Jul 3, 2019. It is now read-only.

Commit bad6c49

Browse files
Alexander Plavinskizkat
Alexander Plavinski
authored andcommitted
feat(read): change hasContent to return {sri, size} (#88)
Fixes #87 BREAKING CHANGE: hasContent now returns an object with `{sri, size}` instead of `sri`. Use `result.sri` anywhere that needed the old return value.
1 parent a9130de commit bad6c49

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ cacache.get.info(cachePath, 'my-thing').then(console.log)
297297

298298
#### <a name="get-hasContent"></a> `> cacache.get.hasContent(cache, integrity) -> Promise`
299299

300-
Looks up a [Subresource Integrity hash](#integrity) in the cache. If content
301-
exists for this `integrity`, it will return the specific single integrity hash
302-
that was found. If no content exists for this integrity, it will return `false`.
300+
Looks up a [Subresource Integrity hash](#integrity) in the cache. If content
301+
exists for this `integrity`, it will return an object, with the specific single integrity hash
302+
that was found in `sri` key, and the size of the found content as `size`. If no content exists for this integrity, it will return `false`.
303303

304304
##### Fields
305305

@@ -316,10 +316,13 @@ cacache.get.hasContent(cachePath, 'sha256-MUSTVERIFY+ALL/THINGS==').then(console
316316

317317
// Output
318318
{
319-
source: 'sha256-MUSTVERIFY+ALL/THINGS==',
320-
algorithm: 'sha256',
321-
digest: 'MUSTVERIFY+ALL/THINGS==',
322-
options: []
319+
sri: {
320+
source: 'sha256-MUSTVERIFY+ALL/THINGS==',
321+
algorithm: 'sha256',
322+
digest: 'MUSTVERIFY+ALL/THINGS==',
323+
options: []
324+
},
325+
size: 9001
323326
}
324327

325328
cacache.get.hasContent(cachePath, 'sha521-NOT+IN/CACHE==').then(console.log)
@@ -397,7 +400,7 @@ for inserted data. Can use any algorithm listed in `crypto.getHashes()` or
397400
`'omakase'`/`'お任せします'` to pick a random hash algorithm on each insertion. You
398401
may also use any anagram of `'modnar'` to use this feature.
399402

400-
Currently only supports one algorithm at a time (i.e., an array length of
403+
Currently only supports one algorithm at a time (i.e., an array length of
401404
exactly `1`). Has no effect if `opts.integrity` is present.
402405

403406
##### `opts.uid`/`opts.gid`

lib/content/read.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ BB.promisifyAll(fs)
1313
module.exports = read
1414
function read (cache, integrity, opts) {
1515
opts = opts || {}
16-
return pickContentSri(cache, integrity).then(sri => {
16+
return pickContentSri(cache, integrity).then(content => {
17+
const sri = content.sri
1718
const cpath = contentPath(cache, sri)
1819
return fs.readFileAsync(cpath, null).then(data => {
1920
if (typeof opts.size === 'number' && opts.size !== data.length) {
@@ -34,7 +35,8 @@ function readStream (cache, integrity, opts) {
3435
const stream = new PassThrough()
3536
pickContentSri(
3637
cache, integrity
37-
).then(sri => {
38+
).then(content => {
39+
const sri = content.sri
3840
return pipe(
3941
fs.createReadStream(contentPath(cache, sri)),
4042
ssri.integrityStream({
@@ -52,34 +54,33 @@ function readStream (cache, integrity, opts) {
5254
module.exports.hasContent = hasContent
5355
function hasContent (cache, integrity) {
5456
if (!integrity) { return BB.resolve(false) }
55-
return pickContentSri(cache, integrity, true)
57+
return pickContentSri(cache, integrity)
5658
.catch({code: 'ENOENT'}, () => false)
5759
.catch({code: 'EPERM'}, err => {
5860
if (process.platform !== 'win32') {
5961
throw err
6062
} else {
6163
return false
6264
}
63-
}).then(sri => sri || false)
65+
}).then(content => {
66+
if (!content.sri) return false
67+
return ({ sri: content.sri, size: content.stat.size })
68+
})
6469
}
6570

6671
module.exports._pickContentSri = pickContentSri
67-
function pickContentSri (cache, integrity, checkFs) {
72+
function pickContentSri (cache, integrity) {
6873
const sri = ssri.parse(integrity)
6974
// If `integrity` has multiple entries, pick the first digest
7075
// with available local data.
7176
const algo = sri.pickAlgorithm()
7277
const digests = sri[algo]
7378
if (digests.length <= 1) {
7479
const cpath = contentPath(cache, digests[0])
75-
if (checkFs) {
76-
return fs.lstatAsync(cpath).then(() => digests[0])
77-
} else {
78-
return BB.resolve(digests[0])
79-
}
80+
return fs.lstatAsync(cpath).then(stat => ({ sri: digests[0], stat }))
8081
} else {
8182
return BB.any(sri[sri.pickAlgorithm()].map(meta => {
82-
return pickContentSri(cache, meta, true)
83+
return pickContentSri(cache, meta)
8384
}))
8485
}
8586
}

lib/content/rm.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const rimraf = BB.promisify(require('rimraf'))
88

99
module.exports = rm
1010
function rm (cache, integrity) {
11-
return hasContent(cache, integrity).then(sri => {
11+
return hasContent(cache, integrity).then(content => {
12+
const sri = content.sri
1213
if (sri) {
1314
return rimraf(contentPath(cache, sri))
1415
}

test/content.read.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ test('read: errors if content size does not match size option', function (t) {
132132
)
133133
})
134134

135-
test('hasContent: returns true when a cache file exists', function (t) {
135+
test('hasContent: returns { sri, size } when a cache file exists', function (t) {
136136
const fixture = new Tacks(CacheContent({
137137
'sha1-deadbeef': ''
138138
}))
139139
fixture.create(CACHE)
140140
return BB.join(
141-
read.hasContent(CACHE, 'sha1-deadbeef').then(bool => {
142-
t.ok(bool, 'returned true for existing content')
141+
read.hasContent(CACHE, 'sha1-deadbeef').then(content => {
142+
t.ok(content.sri, 'returned sri for this content')
143+
t.equal(content.size, 0, 'returned the right size for this content')
143144
}),
144-
read.hasContent(CACHE, 'sha1-not-there').then(bool => {
145-
t.equal(bool, false, 'returned false for missing content')
145+
read.hasContent(CACHE, 'sha1-not-there').then(content => {
146+
t.equal(content, false, 'returned false for missing content')
146147
})
147148
)
148149
})

0 commit comments

Comments
 (0)