Skip to content

Commit

Permalink
chore: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Mar 10, 2022
1 parent a7aa492 commit 32ac8a2
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 134 deletions.
47 changes: 31 additions & 16 deletions lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ function read (cache, integrity, opts = {}) {
// get size
return lstat(cpath).then(stat => ({ stat, cpath, sri }))
}).then(({ stat, cpath, sri }) => {
if (typeof size === 'number' && stat.size !== size)
if (typeof size === 'number' && stat.size !== size) {
throw sizeError(size, stat.size)
}

if (stat.size > MAX_SINGLE_READ_SIZE)
if (stat.size > MAX_SINGLE_READ_SIZE) {
return readPipeline(cpath, stat.size, sri, new Pipeline()).concat()
}

return readFile(cpath, null).then((data) => {
if (!ssri.checkData(data, sri))
if (!ssri.checkData(data, sri)) {
throw integrityError(sri, cpath)
}

return data
})
Expand All @@ -55,11 +58,13 @@ function readSync (cache, integrity, opts = {}) {
const { size } = opts
return withContentSriSync(cache, integrity, (cpath, sri) => {
const data = fs.readFileSync(cpath)
if (typeof size === 'number' && size !== data.length)
if (typeof size === 'number' && size !== data.length) {
throw sizeError(size, data.length)
}

if (ssri.checkData(data, sri))
if (ssri.checkData(data, sri)) {
return data
}

throw integrityError(sri, cpath)
})
Expand All @@ -75,8 +80,9 @@ function readStream (cache, integrity, opts = {}) {
// just lstat to ensure it exists
return lstat(cpath).then((stat) => ({ stat, cpath, sri }))
}).then(({ stat, cpath, sri }) => {
if (typeof size === 'number' && size !== stat.size)
if (typeof size === 'number' && size !== stat.size) {
return stream.emit('error', sizeError(size, stat.size))
}

readPipeline(cpath, stat.size, sri, stream)
}, er => stream.emit('error', er))
Expand Down Expand Up @@ -106,45 +112,51 @@ function copySync (cache, integrity, dest) {
module.exports.hasContent = hasContent

function hasContent (cache, integrity) {
if (!integrity)
if (!integrity) {
return Promise.resolve(false)
}

return withContentSri(cache, integrity, (cpath, sri) => {
return lstat(cpath).then((stat) => ({ size: stat.size, sri, stat }))
}).catch((err) => {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT') {
return false
}

if (err.code === 'EPERM') {
/* istanbul ignore else */
if (process.platform !== 'win32')
if (process.platform !== 'win32') {
throw err
else
} else {
return false
}
}
})
}

module.exports.hasContent.sync = hasContentSync

function hasContentSync (cache, integrity) {
if (!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')
if (err.code === 'ENOENT') {
return false
}

if (err.code === 'EPERM') {
/* istanbul ignore else */
if (process.platform !== 'win32')
if (process.platform !== 'win32') {
throw err
else
} else {
return false
}
}
}
})
Expand Down Expand Up @@ -180,13 +192,15 @@ function withContentSri (cache, integrity, fn) {
.then((results) => {
// Return the first non error if it is found
const result = results.find((r) => !(r instanceof Error))
if (result)
if (result) {
return result
}

// Throw the No matching content found error
const enoentError = results.find((r) => r.code === 'ENOENT')
if (enoentError)
if (enoentError) {
throw enoentError
}

// Throw generic error
throw results.find((r) => r instanceof Error)
Expand Down Expand Up @@ -228,6 +242,7 @@ function withContentSriSync (cache, integrity, fn) {
}

function sizeError (expected, found) {
/* eslint-disable-next-line max-len */
const err = new Error(`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
err.expected = expected
err.found = found
Expand Down
5 changes: 3 additions & 2 deletions lib/content/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module.exports = rm
function rm (cache, integrity) {
return hasContent(cache, integrity).then((content) => {
// ~pretty~ sure we can't end up with a content lacking sri, but be safe
if (content && content.sri)
if (content && content.sri) {
return rimraf(contentPath(cache, content.sri)).then(() => true)
else
} else {
return false
}
})
}
13 changes: 9 additions & 4 deletions lib/content/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ module.exports = write

function write (cache, data, opts = {}) {
const { algorithms, size, integrity } = opts
if (algorithms && algorithms.length > 1)
if (algorithms && algorithms.length > 1) {
throw new Error('opts.algorithms only supports a single algorithm for now')
}

if (typeof size === 'number' && data.length !== size)
if (typeof size === 'number' && data.length !== size) {
return Promise.reject(sizeError(size, data.length))
}

const sri = ssri.fromData(data, algorithms ? { algorithms } : {})
if (integrity && !ssri.checkData(data, integrity, opts))
if (integrity && !ssri.checkData(data, integrity, opts)) {
return Promise.reject(checksumError(integrity, sri))
}

return disposer(makeTmp(cache, opts), makeTmpDisposer,
(tmp) => {
Expand Down Expand Up @@ -149,8 +152,9 @@ function makeTmp (cache, opts) {
}

function makeTmpDisposer (tmp) {
if (tmp.moved)
if (tmp.moved) {
return Promise.resolve()
}

return rimraf(tmp.target)
}
Expand All @@ -171,6 +175,7 @@ function moveToDestination (tmp, cache, sri, opts) {
}

function sizeError (expected, found) {
/* eslint-disable-next-line max-len */
const err = new Error(`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
err.expected = expected
err.found = found
Expand Down
62 changes: 40 additions & 22 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ async function compact (cache, key, matchFn, opts = {}) {
// if the integrity is null and no validateEntry is provided, we break
// as we consider the null integrity to be a deletion of everything
// that came before it.
if (entry.integrity === null && !opts.validateEntry)
if (entry.integrity === null && !opts.validateEntry) {
break
}

// if this entry is valid, and it is either the first entry or
// the newEntries array doesn't already include an entry that
// matches this one based on the provided matchFn, then we add
// it to the beginning of our list
if ((!opts.validateEntry || opts.validateEntry(entry) === true) &&
(newEntries.length === 0 ||
!newEntries.find((oldEntry) => matchFn(oldEntry, entry))))
!newEntries.find((oldEntry) => matchFn(oldEntry, entry)))) {
newEntries.unshift(entry)
}
}

const newIndex = '\n' + newEntries.map((entry) => {
Expand All @@ -78,8 +80,9 @@ async function compact (cache, key, matchFn, opts = {}) {
}

const teardown = async (tmp) => {
if (!tmp.moved)
if (!tmp.moved) {
return rimraf(tmp.target)
}
}

const write = async (tmp) => {
Expand All @@ -92,8 +95,9 @@ async function compact (cache, key, matchFn, opts = {}) {
try {
await fixOwner.chownr(cache, bucket)
} catch (err) {
if (err.code !== 'ENOENT')
if (err.code !== 'ENOENT') {
throw err
}
}
}

Expand Down Expand Up @@ -136,8 +140,9 @@ function insert (cache, key, integrity, opts = {}) {
})
.then(() => fixOwner.chownr(cache, bucket))
.catch((err) => {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT') {
return undefined
}

throw err
// There's a class of race conditions that happen when things get deleted
Expand Down Expand Up @@ -169,8 +174,9 @@ function insertSync (cache, key, integrity, opts = {}) {
try {
fixOwner.chownr.sync(cache, bucket)
} catch (err) {
if (err.code !== 'ENOENT')
if (err.code !== 'ENOENT') {
throw err
}
}
return formatEntry(cache, entry)
}
Expand All @@ -182,17 +188,19 @@ function find (cache, key) {
return bucketEntries(bucket)
.then((entries) => {
return entries.reduce((latest, next) => {
if (next && next.key === key)
if (next && next.key === key) {
return formatEntry(cache, next)
else
} else {
return latest
}
}, null)
})
.catch((err) => {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT') {
return null
else
} else {
throw err
}
})
}

Expand All @@ -202,24 +210,27 @@ function findSync (cache, key) {
const bucket = bucketPath(cache, key)
try {
return bucketEntriesSync(bucket).reduce((latest, next) => {
if (next && next.key === key)
if (next && next.key === key) {
return formatEntry(cache, next)
else
} else {
return latest
}
}, null)
} catch (err) {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT') {
return null
else
} else {
throw err
}
}
}

module.exports.delete = del

function del (cache, key, opts = {}) {
if (!opts.removeFully)
if (!opts.removeFully) {
return insert(cache, key, null, opts)
}

const bucket = bucketPath(cache, key)
return rimraf(bucket)
Expand All @@ -228,8 +239,9 @@ function del (cache, key, opts = {}) {
module.exports.delete.sync = delSync

function delSync (cache, key, opts = {}) {
if (!opts.removeFully)
if (!opts.removeFully) {
return insertSync(cache, key, null, opts)
}

const bucket = bucketPath(cache, key)
return rimraf.sync(bucket)
Expand Down Expand Up @@ -263,12 +275,14 @@ function lsStream (cache) {
// reduced is a map of key => entry
for (const entry of reduced.values()) {
const formatted = formatEntry(cache, entry)
if (formatted)
if (formatted) {
stream.write(formatted)
}
}
}).catch(err => {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT') {
return undefined
}
throw err
})
})
Expand Down Expand Up @@ -312,8 +326,9 @@ function bucketEntriesSync (bucket, filter) {
function _bucketEntries (data, filter) {
const entries = []
data.split('\n').forEach((entry) => {
if (!entry)
if (!entry) {
return
}

const pieces = entry.split('\t')
if (!pieces[1] || hashEntry(pieces[1]) !== pieces[0]) {
Expand All @@ -328,8 +343,9 @@ function _bucketEntries (data, filter) {
// Entry is corrupted!
return
}
if (obj)
if (obj) {
entries.push(obj)
}
})
return entries
}
Expand Down Expand Up @@ -371,8 +387,9 @@ function hash (str, digest) {

function formatEntry (cache, entry, keepAll) {
// Treat null digests as deletions. They'll shadow any previous entries.
if (!entry.integrity && !keepAll)
if (!entry.integrity && !keepAll) {
return null
}

return {
key: entry.key,
Expand All @@ -386,8 +403,9 @@ function formatEntry (cache, entry, keepAll) {

function readdirOrEmpty (dir) {
return readdir(dir).catch((err) => {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR')
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
return []
}

throw err
})
Expand Down
Loading

0 comments on commit 32ac8a2

Please sign in to comment.