Socket
Socket
Sign inDemoInstall

fs-extra

Package Overview
Dependencies
Maintainers
2
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.3 to 5.0.0

16

CHANGELOG.md

@@ -0,1 +1,17 @@

5.0.0 / 2017-12-11
------------------
Significant refactor of `copy()` & `copySync()`, including breaking changes. No changes to other functions in this release.
Huge thanks to **[@manidlou](https://github.com/manidlou)** for doing most of the work on this release.
- The `filter` option can no longer be a RegExp (must be a function). This was deprecated since fs-extra v1.0.0. [#512](https://github.com/jprichardson/node-fs-extra/pull/512)
- `copy()`'s `filter` option can now be a function that returns a Promise. [#518](https://github.com/jprichardson/node-fs-extra/pull/518)
- `copy()` & `copySync()` now use `fs.copyFile()`/`fs.copyFileSync()` in environments that support it (currently Node 8.5.0+). Older Node versions still get the old implementation. [#505](https://github.com/jprichardson/node-fs-extra/pull/505)
- Don't allow copying a directory into itself. [#83](https://github.com/jprichardson/node-fs-extra/issues/83)
- Handle copying between identical files. [#198](https://github.com/jprichardson/node-fs-extra/issues/198)
- Error out when copying an empty folder to a path that already exists. [#464](https://github.com/jprichardson/node-fs-extra/issues/464)
- Don't create `dest`'s parent if the `filter` function aborts the `copy()` operation. [#517](https://github.com/jprichardson/node-fs-extra/pull/517)
- Fix `writeStream` not being closed if there was an error in `copy()`. [#516](https://github.com/jprichardson/node-fs-extra/pull/516)
4.0.3 / 2017-12-05

@@ -2,0 +18,0 @@ ------------------

2

docs/copy-sync.md

@@ -12,3 +12,3 @@ # copySync(src, dest, [options])

- `preserveTimestamps` `<boolean>`: will set last modification and access times to the ones of the original source files, default is `false`.
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude.

@@ -15,0 +15,0 @@ ## Example:

@@ -12,3 +12,3 @@ # copy(src, dest, [options, callback])

- `preserveTimestamps` `<boolean>`: will set last modification and access times to the ones of the original source files, default is `false`.
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. Can also return a `Promise` that resolves to `true` or `false` (or pass in an `async` function).
- `callback` `<Function>`

@@ -15,0 +15,0 @@

@@ -5,24 +5,19 @@ 'use strict'

const path = require('path')
const copyFileSync = require('./copy-file-sync')
const mkdir = require('../mkdirs')
const mkdirpSync = require('../mkdirs').mkdirsSync
const utimesSync = require('../util/utimes.js').utimesMillisSync
function copySync (src, dest, options) {
if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
const notExist = Symbol('notExist')
const existsReg = Symbol('existsReg')
function copySync (src, dest, opts) {
if (typeof opts === 'function') {
opts = {filter: opts}
}
options = options || {}
options.recursive = !!options.recursive
opts = opts || {}
opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
// default to true for now
options.clobber = 'clobber' in options ? !!options.clobber : true
// overwrite falls back to clobber
options.overwrite = 'overwrite' in options ? !!options.overwrite : options.clobber
options.dereference = 'dereference' in options ? !!options.dereference : false
options.preserveTimestamps = 'preserveTimestamps' in options ? !!options.preserveTimestamps : false
options.filter = options.filter || function () { return true }
// Warn about using preserveTimestamps on 32-bit node:
if (options.preserveTimestamps && process.arch === 'ia32') {
// Warn about using preserveTimestamps on 32-bit node
if (opts.preserveTimestamps && process.arch === 'ia32') {
console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n

@@ -32,33 +27,185 @@ see https://github.com/jprichardson/node-fs-extra/issues/269`)

const stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src)
const destFolder = path.dirname(dest)
const destFolderExists = fs.existsSync(destFolder)
let performCopy = false
src = path.resolve(src)
dest = path.resolve(dest)
if (options.filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
performCopy = options.filter.test(src)
} else if (typeof options.filter === 'function') performCopy = options.filter(src, dest)
// don't allow src and dest to be the same
if (src === dest) throw new Error('Source and destination must not be the same.')
if (stats.isFile() && performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.errorOnExist,
preserveTimestamps: options.preserveTimestamps
})
} else if (stats.isDirectory() && performCopy) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
const contents = fs.readdirSync(src)
contents.forEach(content => {
const opts = options
opts.recursive = true
copySync(path.join(src, content), path.join(dest, content), opts)
})
} else if (options.recursive && stats.isSymbolicLink() && performCopy) {
const srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
if (opts.filter && !opts.filter(src, dest)) return
const destParent = path.dirname(dest)
if (!fs.existsSync(destParent)) mkdirpSync(destParent)
return startCopy(src, dest, opts)
}
function startCopy (src, dest, opts) {
if (opts.filter && !opts.filter(src, dest)) return
return getStats(src, dest, opts)
}
function getStats (src, dest, opts) {
const statSync = opts.dereference ? fs.statSync : fs.lstatSync
const st = statSync(src)
if (st.isDirectory()) return onDir(st, src, dest, opts)
else if (st.isFile() ||
st.isCharacterDevice() ||
st.isBlockDevice()) return onFile(st, src, dest, opts)
else if (st.isSymbolicLink()) return onLink(src, dest, opts)
}
function onFile (srcStat, src, dest, opts) {
const resolvedPath = checkDest(dest)
if (resolvedPath === notExist) {
return copyFile(srcStat, src, dest, opts)
} else if (resolvedPath === existsReg) {
return mayCopyFile(srcStat, src, dest, opts)
} else {
if (src === resolvedPath) return
return mayCopyFile(srcStat, src, dest, opts)
}
}
function mayCopyFile (srcStat, src, dest, opts) {
if (opts.overwrite) {
fs.unlinkSync(dest)
return copyFile(srcStat, src, dest, opts)
} else if (opts.errorOnExist) {
throw new Error(`'${dest}' already exists`)
}
}
function copyFile (srcStat, src, dest, opts) {
if (typeof fs.copyFileSync === 'function') {
fs.copyFileSync(src, dest)
fs.chmodSync(dest, srcStat.mode)
if (opts.preserveTimestamps) {
return utimesSync(dest, srcStat.atime, srcStat.mtime)
}
return
}
return copyFileFallback(srcStat, src, dest, opts)
}
function copyFileFallback (srcStat, src, dest, opts) {
const BUF_LENGTH = 64 * 1024
const _buff = require('../util/buffer')(BUF_LENGTH)
const fdr = fs.openSync(src, 'r')
const fdw = fs.openSync(dest, 'w', srcStat.mode)
let bytesRead = 1
let pos = 0
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
fs.writeSync(fdw, _buff, 0, bytesRead)
pos += bytesRead
}
if (opts.preserveTimestamps) fs.futimesSync(fdw, srcStat.atime, srcStat.mtime)
fs.closeSync(fdr)
fs.closeSync(fdw)
}
function onDir (srcStat, src, dest, opts) {
const resolvedPath = checkDest(dest)
if (resolvedPath === notExist) {
if (isSrcSubdir(src, dest)) {
throw new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)
}
return mkDirAndCopy(srcStat, src, dest, opts)
} else if (resolvedPath === existsReg) {
if (isSrcSubdir(src, dest)) {
throw new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)
}
return mayCopyDir(src, dest, opts)
} else {
if (src === resolvedPath) return
return copyDir(src, dest, opts)
}
}
function mayCopyDir (src, dest, opts) {
if (!fs.statSync(dest).isDirectory()) {
throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
return copyDir(src, dest, opts)
}
function mkDirAndCopy (srcStat, src, dest, opts) {
fs.mkdirSync(dest, srcStat.mode)
fs.chmodSync(dest, srcStat.mode)
return copyDir(src, dest, opts)
}
function copyDir (src, dest, opts) {
fs.readdirSync(src).forEach(item => {
startCopy(path.join(src, item), path.join(dest, item), opts)
})
}
function onLink (src, dest, opts) {
let resolvedSrcPath = fs.readlinkSync(src)
if (opts.dereference) {
resolvedSrcPath = path.resolve(process.cwd(), resolvedSrcPath)
}
let resolvedDestPath = checkDest(dest)
if (resolvedDestPath === notExist || resolvedDestPath === existsReg) {
// if dest already exists, fs throws error anyway,
// so no need to guard against it here.
return fs.symlinkSync(resolvedSrcPath, dest)
} else {
if (opts.dereference) {
resolvedDestPath = path.resolve(process.cwd(), resolvedDestPath)
}
if (resolvedDestPath === resolvedSrcPath) return
// prevent copy if src is a subdir of dest since unlinking
// dest in this case would result in removing src contents
// and therefore a broken symlink would be created.
if (fs.statSync(dest).isDirectory() && isSrcSubdir(resolvedDestPath, resolvedSrcPath)) {
throw new Error(`Cannot overwrite '${resolvedDestPath}' with '${resolvedSrcPath}'.`)
}
return copyLink(resolvedSrcPath, dest)
}
}
function copyLink (resolvedSrcPath, dest) {
fs.unlinkSync(dest)
return fs.symlinkSync(resolvedSrcPath, dest)
}
// check if dest exists and/or is a symlink
function checkDest (dest) {
let resolvedPath
try {
resolvedPath = fs.readlinkSync(dest)
} catch (err) {
if (err.code === 'ENOENT') return notExist
// dest exists and is a regular file or directory, Windows may throw UNKNOWN error
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return existsReg
throw err
}
return resolvedPath // dest exists and is a symlink
}
// return true if dest is a subdir of src, otherwise false.
// extract dest base dir and check if that is the same as src basename
function isSrcSubdir (src, dest) {
const baseDir = dest.split(path.dirname(src) + path.sep)[1]
if (baseDir) {
const destBasename = baseDir.split(path.sep)[0]
if (destBasename) {
return src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src)
}
return false
}
return false
}
module.exports = copySync

@@ -5,18 +5,25 @@ 'use strict'

const path = require('path')
const ncp = require('./ncp')
const mkdir = require('../mkdirs')
const mkdirp = require('../mkdirs').mkdirs
const pathExists = require('../path-exists').pathExists
const utimes = require('../util/utimes').utimesMillis
function copy (src, dest, options, callback) {
if (typeof options === 'function' && !callback) {
callback = options
options = {}
} else if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
const notExist = Symbol('notExist')
const existsReg = Symbol('existsReg')
function copy (src, dest, opts, cb) {
if (typeof opts === 'function' && !cb) {
cb = opts
opts = {}
} else if (typeof opts === 'function') {
opts = {filter: opts}
}
callback = callback || function () {}
options = options || {}
// Warn about using preserveTimestamps on 32-bit node:
if (options.preserveTimestamps && process.arch === 'ia32') {
cb = cb || function () {}
opts = opts || {}
opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
// Warn about using preserveTimestamps on 32-bit node
if (opts.preserveTimestamps && process.arch === 'ia32') {
console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n

@@ -26,31 +33,234 @@ see https://github.com/jprichardson/node-fs-extra/issues/269`)

src = path.resolve(src)
dest = path.resolve(dest)
// don't allow src and dest to be the same
const basePath = process.cwd()
const currentPath = path.resolve(basePath, src)
const targetPath = path.resolve(basePath, dest)
if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
if (src === dest) return cb(new Error('Source and destination must not be the same.'))
fs.lstat(src, (err, stats) => {
if (err) return callback(err)
if (opts.filter) return handleFilter(checkParentDir, src, dest, opts, cb)
return checkParentDir(src, dest, opts, cb)
}
let dir = null
if (stats.isDirectory()) {
const parts = dest.split(path.sep)
parts.pop()
dir = parts.join(path.sep)
function checkParentDir (src, dest, opts, cb) {
const destParent = path.dirname(dest)
pathExists(destParent, (err, dirExists) => {
if (err) return cb(err)
if (dirExists) return startCopy(src, dest, opts, cb)
mkdirp(destParent, err => {
if (err) return cb(err)
return startCopy(src, dest, opts, cb)
})
})
}
function startCopy (src, dest, opts, cb) {
if (opts.filter) return handleFilter(getStats, src, dest, opts, cb)
return getStats(src, dest, opts, cb)
}
function handleFilter (onInclude, src, dest, opts, cb) {
Promise.resolve(opts.filter(src, dest))
.then(include => {
if (include) return onInclude(src, dest, opts, cb)
return cb()
}, error => cb(error))
}
function getStats (src, dest, opts, cb) {
const stat = opts.dereference ? fs.stat : fs.lstat
stat(src, (err, st) => {
if (err) return cb(err)
if (st.isDirectory()) return onDir(st, src, dest, opts, cb)
else if (st.isFile() ||
st.isCharacterDevice() ||
st.isBlockDevice()) return onFile(st, src, dest, opts, cb)
else if (st.isSymbolicLink()) return onLink(src, dest, opts, cb)
})
}
function onFile (srcStat, src, dest, opts, cb) {
checkDest(dest, (err, resolvedPath) => {
if (err) return cb(err)
if (resolvedPath === notExist) {
return copyFile(srcStat, src, dest, opts, cb)
} else if (resolvedPath === existsReg) {
return mayCopyFile(srcStat, src, dest, opts, cb)
} else {
dir = path.dirname(dest)
if (src === resolvedPath) return cb()
return mayCopyFile(srcStat, src, dest, opts, cb)
}
})
}
pathExists(dir, (err, dirExists) => {
if (err) return callback(err)
if (dirExists) return ncp(src, dest, options, callback)
mkdir.mkdirs(dir, err => {
if (err) return callback(err)
ncp(src, dest, options, callback)
})
function mayCopyFile (srcStat, src, dest, opts, cb) {
if (opts.overwrite) {
fs.unlink(dest, err => {
if (err) return cb(err)
return copyFile(srcStat, src, dest, opts, cb)
})
} else if (opts.errorOnExist) {
return cb(new Error(`'${dest}' already exists`))
} else return cb()
}
function copyFile (srcStat, src, dest, opts, cb) {
if (typeof fs.copyFile === 'function') {
return fs.copyFile(src, dest, err => {
if (err) return cb(err)
return setDestModeAndTimestamps(srcStat, dest, opts, cb)
})
}
return copyFileFallback(srcStat, src, dest, opts, cb)
}
function copyFileFallback (srcStat, src, dest, opts, cb) {
const rs = fs.createReadStream(src)
rs.on('error', err => cb(err))
.once('open', () => {
const ws = fs.createWriteStream(dest, { mode: srcStat.mode })
ws.on('error', err => cb(err))
.on('open', () => rs.pipe(ws))
.once('close', () => setDestModeAndTimestamps(srcStat, dest, opts, cb))
})
}
function setDestModeAndTimestamps (srcStat, dest, opts, cb) {
fs.chmod(dest, srcStat.mode, err => {
if (err) return cb(err)
if (opts.preserveTimestamps) {
return utimes(dest, srcStat.atime, srcStat.mtime, cb)
}
return cb()
})
}
function onDir (srcStat, src, dest, opts, cb) {
checkDest(dest, (err, resolvedPath) => {
if (err) return cb(err)
if (resolvedPath === notExist) {
if (isSrcSubdir(src, dest)) {
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`))
}
return mkDirAndCopy(srcStat, src, dest, opts, cb)
} else if (resolvedPath === existsReg) {
if (isSrcSubdir(src, dest)) {
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`))
}
return mayCopyDir(src, dest, opts, cb)
} else {
if (src === resolvedPath) return cb()
return copyDir(src, dest, opts, cb)
}
})
}
function mayCopyDir (src, dest, opts, cb) {
fs.stat(dest, (err, st) => {
if (err) return cb(err)
if (!st.isDirectory()) {
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
}
return copyDir(src, dest, opts, cb)
})
}
function mkDirAndCopy (srcStat, src, dest, opts, cb) {
fs.mkdir(dest, srcStat.mode, err => {
if (err) return cb(err)
fs.chmod(dest, srcStat.mode, err => {
if (err) return cb(err)
return copyDir(src, dest, opts, cb)
})
})
}
function copyDir (src, dest, opts, cb) {
fs.readdir(src, (err, items) => {
if (err) return cb(err)
return copyDirItems(items, src, dest, opts, cb)
})
}
function copyDirItems (items, src, dest, opts, cb) {
const item = items.pop()
if (!item) return cb()
startCopy(path.join(src, item), path.join(dest, item), opts, err => {
if (err) return cb(err)
return copyDirItems(items, src, dest, opts, cb)
})
}
function onLink (src, dest, opts, cb) {
fs.readlink(src, (err, resolvedSrcPath) => {
if (err) return cb(err)
if (opts.dereference) {
resolvedSrcPath = path.resolve(process.cwd(), resolvedSrcPath)
}
checkDest(dest, (err, resolvedDestPath) => {
if (err) return cb(err)
if (resolvedDestPath === notExist || resolvedDestPath === existsReg) {
// if dest already exists, fs throws error anyway,
// so no need to guard against it here.
return fs.symlink(resolvedSrcPath, dest, cb)
} else {
if (opts.dereference) {
resolvedDestPath = path.resolve(process.cwd(), resolvedDestPath)
}
if (resolvedDestPath === resolvedSrcPath) return cb()
// prevent copy if src is a subdir of dest since unlinking
// dest in this case would result in removing src contents
// and therefore a broken symlink would be created.
fs.stat(dest, (err, st) => {
if (err) return cb(err)
if (st.isDirectory() && isSrcSubdir(resolvedDestPath, resolvedSrcPath)) {
return cb(new Error(`Cannot overwrite '${resolvedDestPath}' with '${resolvedSrcPath}'.`))
}
return copyLink(resolvedSrcPath, dest, cb)
})
}
})
})
}
function copyLink (resolvedSrcPath, dest, cb) {
fs.unlink(dest, err => {
if (err) return cb(err)
return fs.symlink(resolvedSrcPath, dest, cb)
})
}
// check if dest exists and/or is a symlink
function checkDest (dest, cb) {
fs.readlink(dest, (err, resolvedPath) => {
if (err) {
if (err.code === 'ENOENT') return cb(null, notExist)
// dest exists and is a regular file or directory, Windows may throw UNKNOWN error.
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return cb(null, existsReg)
return cb(err)
}
return cb(null, resolvedPath) // dest exists and is a symlink
})
}
// return true if dest is a subdir of src, otherwise false.
// extract dest base dir and check if that is the same as src basename
function isSrcSubdir (src, dest) {
const baseDir = dest.split(path.dirname(src) + path.sep)[1]
if (baseDir) {
const destBasename = baseDir.split(path.sep)[0]
if (destBasename) {
return src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src)
}
return false
}
return false
}
module.exports = copy

@@ -11,3 +11,3 @@ 'use strict'

const fs = require('graceful-fs')
const ncp = require('../copy/ncp')
const copy = require('../copy/copy')
const path = require('path')

@@ -137,10 +137,10 @@ const remove = require('../remove').remove

if (err) return callback(err)
startNcp()
startCopy()
})
} else {
startNcp()
startCopy()
}
function startNcp () {
ncp(src, dest, options, err => {
function startCopy () {
copy(src, dest, options, err => {
if (err) return callback(err)

@@ -147,0 +147,0 @@ remove(src, callback)

@@ -67,2 +67,8 @@ 'use strict'

function utimesMillisSync (path, atime, mtime) {
const fd = fs.openSync(path, 'r+')
fs.futimesSync(fd, atime, mtime)
return fs.closeSync(fd)
}
module.exports = {

@@ -72,3 +78,4 @@ hasMillisRes,

timeRemoveMillis,
utimesMillis
utimesMillis,
utimesMillisSync
}
{
"name": "fs-extra",
"version": "4.0.3",
"version": "5.0.0",
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/jprichardson/node-fs-extra",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc