Socket
Socket
Sign inDemoInstall

fs-extra

Package Overview
Dependencies
3
Maintainers
3
Versions
96
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 9.1.0 to 10.0.0

lib/mkdirs/utils.js

12

lib/copy-sync/copy-sync.js

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

const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy')
const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
stat.checkParentPathsSync(src, srcStat, dest, 'copy')

@@ -34,3 +34,3 @@ return handleFilterAndCopy(destStat, src, dest, opts)

if (!fs.existsSync(destParent)) mkdirsSync(destParent)
return startCopy(destStat, src, dest, opts)
return getStats(destStat, src, dest, opts)
}

@@ -52,2 +52,5 @@

else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
throw new Error(`Unknown file: ${src}`)
}

@@ -105,5 +108,2 @@

if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
if (destStat && !destStat.isDirectory()) {
throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
return copyDir(src, dest, opts)

@@ -125,3 +125,3 @@ }

const destItem = path.join(dest, item)
const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy')
const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
return startCopy(destStat, srcItem, destItem, opts)

@@ -128,0 +128,0 @@ }

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

stat.checkPaths(src, dest, 'copy', (err, stats) => {
stat.checkPaths(src, dest, 'copy', opts, (err, stats) => {
if (err) return cb(err)

@@ -46,6 +46,6 @@ const { srcStat, destStat } = stats

if (err) return cb(err)
if (dirExists) return startCopy(destStat, src, dest, opts, cb)
if (dirExists) return getStats(destStat, src, dest, opts, cb)
mkdirs(destParent, err => {
if (err) return cb(err)
return startCopy(destStat, src, dest, opts, cb)
return getStats(destStat, src, dest, opts, cb)
})

@@ -77,2 +77,5 @@ })

else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts, cb)
else if (srcStat.isSocket()) return cb(new Error(`Cannot copy a socket file: ${src}`))
else if (srcStat.isFIFO()) return cb(new Error(`Cannot copy a FIFO pipe: ${src}`))
return cb(new Error(`Unknown file: ${src}`))
})

@@ -149,5 +152,2 @@ }

if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, cb)
if (destStat && !destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
}
return copyDir(src, dest, opts, cb)

@@ -182,3 +182,3 @@ }

const destItem = path.join(dest, item)
stat.checkPaths(srcItem, destItem, 'copy', (err, stats) => {
stat.checkPaths(srcItem, destItem, 'copy', opts, (err, stats) => {
if (err) return cb(err)

@@ -185,0 +185,0 @@ const { destStat } = stats

'use strict'
const u = require('universalify').fromCallback
const fs = require('graceful-fs')
const u = require('universalify').fromPromise
const fs = require('../fs')
const path = require('path')

@@ -9,20 +9,11 @@ const mkdir = require('../mkdirs')

const emptyDir = u(function emptyDir (dir, callback) {
callback = callback || function () {}
fs.readdir(dir, (err, items) => {
if (err) return mkdir.mkdirs(dir, callback)
const emptyDir = u(async function emptyDir (dir) {
let items
try {
items = await fs.readdir(dir)
} catch {
return mkdir.mkdirs(dir)
}
items = items.map(item => path.join(dir, item))
deleteItem()
function deleteItem () {
const item = items.pop()
if (!item) return callback()
remove.remove(item, err => {
if (err) return callback(err)
deleteItem()
})
}
})
return Promise.all(items.map(item => remove.remove(path.join(dir, item))))
})

@@ -29,0 +20,0 @@

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

const pathExists = require('../path-exists').pathExists
const { areIdentical } = require('../util/stat')

@@ -18,6 +19,4 @@ function createLink (srcpath, dstpath, callback) {

pathExists(dstpath, (err, destinationExists) => {
if (err) return callback(err)
if (destinationExists) return callback(null)
fs.lstat(srcpath, (err) => {
fs.lstat(dstpath, (_, dstStat) => {
fs.lstat(srcpath, (err, srcStat) => {
if (err) {

@@ -27,2 +26,3 @@ err.message = err.message.replace('lstat', 'ensureLink')

}
if (dstStat && areIdentical(srcStat, dstStat)) return callback(null)

@@ -43,7 +43,10 @@ const dir = path.dirname(dstpath)

function createLinkSync (srcpath, dstpath) {
const destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined
let dstStat
try {
dstStat = fs.lstatSync(dstpath)
} catch {}
try {
fs.lstatSync(srcpath)
const srcStat = fs.lstatSync(srcpath)
if (dstStat && areIdentical(srcStat, dstStat)) return
} catch (err) {

@@ -50,0 +53,0 @@ err.message = err.message.replace('lstat', 'ensureLink')

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

const path = require('path')
const fs = require('graceful-fs')
const fs = require('../fs')
const _mkdirs = require('../mkdirs')

@@ -21,2 +21,4 @@ const mkdirs = _mkdirs.mkdirs

const { areIdentical } = require('../util/stat')
function createSymlink (srcpath, dstpath, type, callback) {

@@ -26,18 +28,28 @@ callback = (typeof type === 'function') ? type : callback

pathExists(dstpath, (err, destinationExists) => {
fs.lstat(dstpath, (err, stats) => {
if (!err && stats.isSymbolicLink()) {
Promise.all([
fs.stat(srcpath),
fs.stat(dstpath)
]).then(([srcStat, dstStat]) => {
if (areIdentical(srcStat, dstStat)) return callback(null)
_createSymlink(srcpath, dstpath, type, callback)
})
} else _createSymlink(srcpath, dstpath, type, callback)
})
}
function _createSymlink (srcpath, dstpath, type, callback) {
symlinkPaths(srcpath, dstpath, (err, relative) => {
if (err) return callback(err)
if (destinationExists) return callback(null)
symlinkPaths(srcpath, dstpath, (err, relative) => {
srcpath = relative.toDst
symlinkType(relative.toCwd, type, (err, type) => {
if (err) return callback(err)
srcpath = relative.toDst
symlinkType(relative.toCwd, type, (err, type) => {
const dir = path.dirname(dstpath)
pathExists(dir, (err, dirExists) => {
if (err) return callback(err)
const dir = path.dirname(dstpath)
pathExists(dir, (err, dirExists) => {
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
mkdirs(dir, err => {
if (err) return callback(err)
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
mkdirs(dir, err => {
if (err) return callback(err)
fs.symlink(srcpath, dstpath, type, callback)
})
fs.symlink(srcpath, dstpath, type, callback)
})

@@ -50,4 +62,11 @@ })

function createSymlinkSync (srcpath, dstpath, type) {
const destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined
let stats
try {
stats = fs.lstatSync(dstpath)
} catch {}
if (stats && stats.isSymbolicLink()) {
const srcStat = fs.statSync(srcpath)
const dstStat = fs.statSync(dstpath)
if (areIdentical(srcStat, dstStat)) return
}

@@ -54,0 +73,0 @@ const relative = symlinkPathsSync(srcpath, dstpath)

@@ -50,11 +50,4 @@ 'use strict'

// Export all keys:
Object.keys(fs).forEach(key => {
if (key === 'promises') {
// fs.promises is a getter property that triggers ExperimentalWarning
// Don't re-export it here, the getter is defined in "lib/index.js"
return
}
exports[key] = fs[key]
})
// Export cloned fs:
Object.assign(exports, fs)

@@ -65,2 +58,3 @@ // Universalify async methods:

})
exports.realpath.native = u(fs.realpath.native)

@@ -128,6 +122,1 @@ // We differ from mz/fs in that we still ship the old, broken, fs.exists()

}
// fs.realpath.native only available in Node v9.2+
if (typeof fs.realpath.native === 'function') {
exports.realpath.native = u(fs.realpath.native)
}

@@ -19,10 +19,1 @@ 'use strict'

}
// Export fs.promises as a getter property so that we don't trigger
// ExperimentalWarning before fs.promises is actually accessed.
const fs = require('fs')
if (Object.getOwnPropertyDescriptor(fs, 'promises')) {
Object.defineProperty(module.exports, 'promises', {
get () { return fs.promises }
})
}

@@ -1,141 +0,27 @@

// Adapted from https://github.com/sindresorhus/make-dir
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'
const fs = require('../fs')
const path = require('path')
const atLeastNode = require('at-least-node')
const { checkPath } = require('./utils')
const useNativeRecursiveOption = atLeastNode('10.12.0')
// https://github.com/nodejs/node/issues/8987
// https://github.com/libuv/libuv/pull/1088
const checkPath = pth => {
if (process.platform === 'win32') {
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''))
if (pathHasInvalidWinCharacters) {
const error = new Error(`Path contains invalid characters: ${pth}`)
error.code = 'EINVAL'
throw error
}
}
}
const processOptions = options => {
const getMode = options => {
const defaults = { mode: 0o777 }
if (typeof options === 'number') options = { mode: options }
return { ...defaults, ...options }
if (typeof options === 'number') return options
return ({ ...defaults, ...options }).mode
}
const permissionError = pth => {
// This replicates the exception of `fs.mkdir` with native the
// `recusive` option when run on an invalid drive under Windows.
const error = new Error(`operation not permitted, mkdir '${pth}'`)
error.code = 'EPERM'
error.errno = -4048
error.path = pth
error.syscall = 'mkdir'
return error
}
module.exports.makeDir = async (dir, options) => {
checkPath(dir)
module.exports.makeDir = async (input, options) => {
checkPath(input)
options = processOptions(options)
if (useNativeRecursiveOption) {
const pth = path.resolve(input)
return fs.mkdir(pth, {
mode: options.mode,
recursive: true
})
}
const make = async pth => {
try {
await fs.mkdir(pth, options.mode)
} catch (error) {
if (error.code === 'EPERM') {
throw error
}
if (error.code === 'ENOENT') {
if (path.dirname(pth) === pth) {
throw permissionError(pth)
}
if (error.message.includes('null bytes')) {
throw error
}
await make(path.dirname(pth))
return make(pth)
}
try {
const stats = await fs.stat(pth)
if (!stats.isDirectory()) {
// This error is never exposed to the user
// it is caught below, and the original error is thrown
throw new Error('The path is not a directory')
}
} catch {
throw error
}
}
}
return make(path.resolve(input))
return fs.mkdir(dir, {
mode: getMode(options),
recursive: true
})
}
module.exports.makeDirSync = (input, options) => {
checkPath(input)
options = processOptions(options)
module.exports.makeDirSync = (dir, options) => {
checkPath(dir)
if (useNativeRecursiveOption) {
const pth = path.resolve(input)
return fs.mkdirSync(pth, {
mode: options.mode,
recursive: true
})
}
const make = pth => {
try {
fs.mkdirSync(pth, options.mode)
} catch (error) {
if (error.code === 'EPERM') {
throw error
}
if (error.code === 'ENOENT') {
if (path.dirname(pth) === pth) {
throw permissionError(pth)
}
if (error.message.includes('null bytes')) {
throw error
}
make(path.dirname(pth))
return make(pth)
}
try {
if (!fs.statSync(pth).isDirectory()) {
// This error is never exposed to the user
// it is caught below, and the original error is thrown
throw new Error('The path is not a directory')
}
} catch {
throw error
}
}
}
return make(path.resolve(input))
return fs.mkdirSync(dir, {
mode: getMode(options),
recursive: true
})
}

@@ -14,9 +14,16 @@ 'use strict'

const { srcStat } = stat.checkPathsSync(src, dest, 'move')
const { srcStat, isChangingCase = false } = stat.checkPathsSync(src, dest, 'move', opts)
stat.checkParentPathsSync(src, srcStat, dest, 'move')
mkdirpSync(path.dirname(dest))
return doRename(src, dest, overwrite)
if (!isParentRoot(dest)) mkdirpSync(path.dirname(dest))
return doRename(src, dest, overwrite, isChangingCase)
}
function doRename (src, dest, overwrite) {
function isParentRoot (dest) {
const parent = path.dirname(dest)
const parsedPath = path.parse(parent)
return parsedPath.root === parent
}
function doRename (src, dest, overwrite, isChangingCase) {
if (isChangingCase) return rename(src, dest, overwrite)
if (overwrite) {

@@ -23,0 +30,0 @@ removeSync(dest)

@@ -19,10 +19,11 @@ 'use strict'

stat.checkPaths(src, dest, 'move', (err, stats) => {
stat.checkPaths(src, dest, 'move', opts, (err, stats) => {
if (err) return cb(err)
const { srcStat } = stats
const { srcStat, isChangingCase = false } = stats
stat.checkParentPaths(src, srcStat, dest, 'move', err => {
if (err) return cb(err)
if (isParentRoot(dest)) return doRename(src, dest, overwrite, isChangingCase, cb)
mkdirp(path.dirname(dest), err => {
if (err) return cb(err)
return doRename(src, dest, overwrite, cb)
return doRename(src, dest, overwrite, isChangingCase, cb)
})

@@ -33,3 +34,10 @@ })

function doRename (src, dest, overwrite, cb) {
function isParentRoot (dest) {
const parent = path.dirname(dest)
const parsedPath = path.parse(parent)
return parsedPath.root === parent
}
function doRename (src, dest, overwrite, isChangingCase, cb) {
if (isChangingCase) return rename(src, dest, overwrite, cb)
if (overwrite) {

@@ -36,0 +44,0 @@ return remove(dest, err => {

'use strict'
const fs = require('graceful-fs')
const u = require('universalify').fromCallback
const rimraf = require('./rimraf')
function remove (path, callback) {
// Node 14.14.0+
if (fs.rm) return fs.rm(path, { recursive: true, force: true }, callback)
rimraf(path, callback)
}
function removeSync (path) {
// Node 14.14.0+
if (fs.rmSync) return fs.rmSync(path, { recursive: true, force: true })
rimraf.sync(path)
}
module.exports = {
remove: u(rimraf),
removeSync: rimraf.sync
remove: u(remove),
removeSync
}

@@ -6,12 +6,10 @@ 'use strict'

const util = require('util')
const atLeastNode = require('at-least-node')
const nodeSupportsBigInt = atLeastNode('10.5.0')
const stat = (file) => nodeSupportsBigInt ? fs.stat(file, { bigint: true }) : fs.stat(file)
const statSync = (file) => nodeSupportsBigInt ? fs.statSync(file, { bigint: true }) : fs.statSync(file)
function getStats (src, dest) {
function getStats (src, dest, opts) {
const statFunc = opts.dereference
? (file) => fs.stat(file, { bigint: true })
: (file) => fs.lstat(file, { bigint: true })
return Promise.all([
stat(src),
stat(dest).catch(err => {
statFunc(src),
statFunc(dest).catch(err => {
if (err.code === 'ENOENT') return null

@@ -23,7 +21,10 @@ throw err

function getStatsSync (src, dest) {
function getStatsSync (src, dest, opts) {
let destStat
const srcStat = statSync(src)
const statFunc = opts.dereference
? (file) => fs.statSync(file, { bigint: true })
: (file) => fs.lstatSync(file, { bigint: true })
const srcStat = statFunc(src)
try {
destStat = statSync(dest)
destStat = statFunc(dest)
} catch (err) {

@@ -36,9 +37,26 @@ if (err.code === 'ENOENT') return { srcStat, destStat: null }

function checkPaths (src, dest, funcName, cb) {
util.callbackify(getStats)(src, dest, (err, stats) => {
function checkPaths (src, dest, funcName, opts, cb) {
util.callbackify(getStats)(src, dest, opts, (err, stats) => {
if (err) return cb(err)
const { srcStat, destStat } = stats
if (destStat && areIdentical(srcStat, destStat)) {
return cb(new Error('Source and destination must not be the same.'))
if (destStat) {
if (areIdentical(srcStat, destStat)) {
const srcBaseName = path.basename(src)
const destBaseName = path.basename(dest)
if (funcName === 'move' &&
srcBaseName !== destBaseName &&
srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
return cb(null, { srcStat, destStat, isChangingCase: true })
}
return cb(new Error('Source and destination must not be the same.'))
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`))
}
}
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {

@@ -51,7 +69,24 @@ return cb(new Error(errMsg(src, dest, funcName)))

function checkPathsSync (src, dest, funcName) {
const { srcStat, destStat } = getStatsSync(src, dest)
if (destStat && areIdentical(srcStat, destStat)) {
throw new Error('Source and destination must not be the same.')
function checkPathsSync (src, dest, funcName, opts) {
const { srcStat, destStat } = getStatsSync(src, dest, opts)
if (destStat) {
if (areIdentical(srcStat, destStat)) {
const srcBaseName = path.basename(src)
const destBaseName = path.basename(dest)
if (funcName === 'move' &&
srcBaseName !== destBaseName &&
srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
return { srcStat, destStat, isChangingCase: true }
}
throw new Error('Source and destination must not be the same.')
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
throw new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
}
}
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {

@@ -71,3 +106,3 @@ throw new Error(errMsg(src, dest, funcName))

if (destParent === srcParent || destParent === path.parse(destParent).root) return cb()
const callback = (err, destStat) => {
fs.stat(destParent, { bigint: true }, (err, destStat) => {
if (err) {

@@ -81,5 +116,3 @@ if (err.code === 'ENOENT') return cb()

return checkParentPaths(src, srcStat, destParent, funcName, cb)
}
if (nodeSupportsBigInt) fs.stat(destParent, { bigint: true }, callback)
else fs.stat(destParent, callback)
})
}

@@ -93,3 +126,3 @@

try {
destStat = statSync(destParent)
destStat = fs.statSync(destParent, { bigint: true })
} catch (err) {

@@ -106,22 +139,3 @@ if (err.code === 'ENOENT') return

function areIdentical (srcStat, destStat) {
if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) {
if (nodeSupportsBigInt || destStat.ino < Number.MAX_SAFE_INTEGER) {
// definitive answer
return true
}
// Use additional heuristics if we can't use 'bigint'.
// Different 'ino' could be represented the same if they are >= Number.MAX_SAFE_INTEGER
// See issue 657
if (destStat.size === srcStat.size &&
destStat.mode === srcStat.mode &&
destStat.nlink === srcStat.nlink &&
destStat.atimeMs === srcStat.atimeMs &&
destStat.mtimeMs === srcStat.mtimeMs &&
destStat.ctimeMs === srcStat.ctimeMs &&
destStat.birthtimeMs === srcStat.birthtimeMs) {
// heuristic answer
return true
}
}
return false
return destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev
}

@@ -146,3 +160,4 @@

checkParentPathsSync,
isSrcSubdir
isSrcSubdir,
areIdentical
}
{
"name": "fs-extra",
"version": "9.1.0",
"version": "10.0.0",
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as recursive mkdir, copy, and remove.",
"engines": {
"node": ">=10"
"node": ">=12"
},

@@ -40,3 +40,2 @@ "homepage": "https://github.com/jprichardson/node-fs-extra",

"dependencies": {
"at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0",

@@ -47,2 +46,3 @@ "jsonfile": "^6.0.1",

"devDependencies": {
"at-least-node": "^1.0.0",
"coveralls": "^3.0.0",

@@ -49,0 +49,0 @@ "klaw": "^2.1.1",

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc