Skip to content

Commit

Permalink
feat(read): add sync support to other internal read.js fns
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 8, 2018
1 parent 4b28ae3 commit fe638b6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
31 changes: 30 additions & 1 deletion lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,29 @@ function readStream (cache, integrity, opts) {
let copyFileAsync
if (fs.copyFile) {
module.exports.copy = copy
module.exports.copy.sync = copySync
copyFileAsync = BB.promisify(fs.copyFile)
}

function copy (cache, integrity, dest, opts) {
opts = ReadOpts(opts)
return withContentSri(cache, integrity, (cpath, sri) => {
return copyFileAsync(cpath, dest)
})
}

function copySync (cache, integrity, dest, opts) {
opts = ReadOpts(opts)
return withContentSriSync(cache, integrity, (cpath, sri) => {
return fs.copyFileSync(cpath, dest)
})
}

module.exports.hasContent = hasContent
function hasContent (cache, integrity) {
if (!integrity) { return BB.resolve(false) }
return withContentSri(cache, integrity, (cpath, sri) => {
return lstatAsync(cpath).then(stat => ({size: stat.size, sri}))
return lstatAsync(cpath).then(stat => ({size: stat.size, sri, stat}))
}).catch(err => {
if (err.code === 'ENOENT') { return false }
if (err.code === 'EPERM') {
Expand All @@ -99,6 +108,26 @@ function hasContent (cache, integrity) {
})
}

module.exports.hasContent.sync = hasContentSync
function hasContentSync (cache, integrity) {
if (!integrity) { return false }
return withContentSriSync(cache, integrity, (cpath, sri) => {
try {
const stat = fs.lstatSync(cpath)
return {size: stat.size, sri, stat}
} catch (err) {
if (err.code === 'ENOENT') { return false }
if (err.code === 'EPERM') {
if (process.platform !== 'win32') {
throw err
} else {
return false
}
}
}
})
}

function withContentSri (cache, integrity, fn) {
return BB.try(() => {
const sri = ssri.parse(integrity)
Expand Down
44 changes: 43 additions & 1 deletion test/content.read.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ test('read: errors if content size does not match size option', function (t) {
)
})

test('hasContent: returns { sri, size } when a cache file exists', function (t) {
test('hasContent: tests content existence', t => {
const fixture = new Tacks(CacheContent({
'sha1-deadbeef': ''
}))
Expand All @@ -156,6 +156,7 @@ test('hasContent: returns { sri, size } when a cache file exists', function (t)
.then(content => {
t.ok(content.sri, 'returned sri for this content')
t.equal(content.size, 0, 'returned the right size for this content')
t.ok(content.stat.isFile(), 'returned actual stat object')
}),
read.hasContent(CACHE, 'sha1-not-there')
.then(content => {
Expand All @@ -168,6 +169,28 @@ test('hasContent: returns { sri, size } when a cache file exists', function (t)
)
})

test('hasContent.sync: checks content existence synchronously', t => {
const fixture = new Tacks(CacheContent({
'sha1-deadbeef': ''
}))
fixture.create(CACHE)
const content = read.hasContent.sync(CACHE, 'sha1-deadbeef')
t.ok(content.sri, 'returned sri for this content')
t.equal(content.size, 0, 'returned the right size for this content')
t.ok(content.stat.isFile(), 'returned actual stat object')
t.equal(
read.hasContent.sync(CACHE, 'sha1-not-there'),
false,
'returned false for missing content'
)
t.equal(
read.hasContent.sync(CACHE, 'sha1-not-here sha1-also-not-here'),
false,
'multi-content hash failures work ok'
)
t.done()
})

test('copy: copies content to a destination path', {
skip: !fs.copyFile && 'Not supported on node versions without fs.copyFile'
}, t => {
Expand All @@ -184,3 +207,22 @@ test('copy: copies content to a destination path', {
t.deepEqual(data, CONTENT, 'file successfully copied')
})
})

test('copy.sync: copies content to a destination path synchronously', {
skip: !fs.copyFile && 'Not supported on node versions without fs.copyFile'
}, t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const DEST = path.join(CACHE, 'foobar-file')
const fixture = new Tacks(CacheContent({
[INTEGRITY]: CONTENT
}))
fixture.create(CACHE)
read.copy.sync(CACHE, INTEGRITY, DEST)
t.deepEqual(
fs.readFileSync(DEST),
CONTENT,
'file successfully copied'
)
t.done()
})

0 comments on commit fe638b6

Please sign in to comment.