Skip to content

Commit

Permalink
fix: remove fs.copyFile checks
Browse files Browse the repository at this point in the history
This exists in all of the node versions this module supports
  • Loading branch information
wraithgar committed Apr 27, 2022
1 parent e870016 commit 90776fd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
9 changes: 3 additions & 6 deletions lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Pipeline = require('minipass-pipeline')

const lstat = util.promisify(fs.lstat)
const readFile = util.promisify(fs.readFile)
const copyFile = util.promisify(fs.copyFile)

module.exports = read

Expand Down Expand Up @@ -90,12 +91,8 @@ function readStream (cache, integrity, opts = {}) {
return stream
}

let copyFile
if (fs.copyFile) {
module.exports.copy = copy
module.exports.copy.sync = copySync
copyFile = util.promisify(fs.copyFile)
}
module.exports.copy = copy
module.exports.copy.sync = copySync

function copy (cache, integrity, dest) {
return withContentSri(cache, integrity, (cpath, sri) => {
Expand Down
49 changes: 14 additions & 35 deletions lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
const Collect = require('minipass-collect')
const Minipass = require('minipass')
const Pipeline = require('minipass-pipeline')
const fs = require('fs')
const util = require('util')

const index = require('./entry-index')
const memo = require('./memoization')
const read = require('./content/read')

const writeFile = util.promisify(fs.writeFile)

function getData (cache, key, opts = {}) {
const { integrity, memoize, size } = opts
const memoized = memo.get(cache, key, opts)
Expand Down Expand Up @@ -209,42 +205,25 @@ function info (cache, key, opts = {}) {
module.exports.info = info

function copy (cache, key, dest, opts = {}) {
if (read.copy) {
return index.find(cache, key, opts).then((entry) => {
if (!entry) {
throw new index.NotFoundError(cache, key)
}
return read.copy(cache, entry.integrity, dest, opts)
.then(() => {
return {
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
})
})
}

return getData(cache, key, opts).then((res) => {
return writeFile(dest, res.data).then(() => {
return {
metadata: res.metadata,
size: res.size,
integrity: res.integrity,
}
})
return index.find(cache, key, opts).then((entry) => {
if (!entry) {
throw new index.NotFoundError(cache, key)
}
return read.copy(cache, entry.integrity, dest, opts)
.then(() => {
return {
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
})
})
}

module.exports.copy = copy

function copyByDigest (cache, key, dest, opts = {}) {
if (read.copy) {
return read.copy(cache, key, dest, opts).then(() => key)
}

return getDataByDigest(cache, key, opts).then((res) => {
return writeFile(dest, res).then(() => key)
})
return read.copy(cache, key, dest, opts).then(() => key)
}
module.exports.copy.byDigest = copyByDigest

Expand Down
10 changes: 2 additions & 8 deletions lib/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,8 @@ function putStream (cache, key, opts = {}) {
if (memoize && memoData) {
memo.put(cache, entry, memoData, opts)
}

if (integrity) {
pipeline.emit('integrity', integrity)
}

if (size) {
pipeline.emit('size', size)
}
pipeline.emit('integrity', integrity)
pipeline.emit('size', size)
})
}
},
Expand Down

0 comments on commit 90776fd

Please sign in to comment.