Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
fix(fs): make a few more things optimistic
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Nov 9, 2018
1 parent d06362b commit fd5542f
Showing 1 changed file with 67 additions and 48 deletions.
115 changes: 67 additions & 48 deletions lib/node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,23 @@ function overrideNode () {
}

patchFn('lstatSync', lstatSync => p => {
const resolved = pkglock.resolve(p)
if (resolved == null) {
try {
return lstatSync(p)
} else if (!resolved) {
return notFoundError(p)
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
notFoundError(p)
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
} else {
const resolved = pkglock.resolve(p)
if (!resolved) {
return notFoundError(p)
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
notFoundError(p)
}
return stats
}
}
return stats
}
})

Expand All @@ -129,17 +135,23 @@ function overrideNode () {
})

patchFn('statSync', statSync => p => {
const resolved = pkglock.resolve(p)
if (resolved == null) {
try {
return statSync(p)
} else if (!resolved) {
return notFoundError(p)
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
notFoundError(p)
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
} else {
const resolved = pkglock.resolve(p)
if (!resolved) {
return notFoundError(p)
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
notFoundError(p)
}
return stats
}
}
return stats
}
})

Expand All @@ -161,47 +173,54 @@ function overrideNode () {
})

patchFn('statSyncNoException', statSyncNoException => p => {
const resolved = pkglock.resolve(p)
if (resolved == null) {
return statSyncNoException(p)
} else if (!resolved) {
return false
const ret = statSyncNoException(p)
if (ret) {
return ret
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
const resolved = pkglock.resolve(p)
if (!resolved) {
return false
} else {
const stats = pkglock.statSync(resolved)
if (!stats) {
return false
}
return stats
}
return stats
}
})

patchFn('realpathSync', realpathSync => function (p) {
const resolved = pkglock.resolve(p)
if (resolved == null) {
return realpathSync.apply(this, arguments)
} else if (!resolved) {
return notFoundError(p)
} else {
return resolved.resolvedPath
patchFn('realpathSync', realpathSync => (p, ...args) => {
try {
return realpathSync(p, ...args)
} catch (err) {
if (err.code !== 'ENOENT') { throw err }
const resolved = pkglock.resolve(p)
if (!resolved) {
return notFoundError(p)
} else {
return resolved.resolvedPath
}
}
})

patchFn('realpath', realpath => function (p, cache, cb) {
const resolved = pkglock.resolve(p)
if (resolved == null) {
return realpath.apply(this, arguments)
}
if (typeof cache === 'function') {
cb = cache
cache = void 0
}

process.nextTick(() => {
if (resolved) {
cb(null, resolved.resolvedPath)
} else {
notFoundError(p, cb)
realpath(p, cache, (err, rp) => {
if (err && err.code !== 'ENOENT') { return cb(err) }
if (!err) { return cb(null, rp) }
const resolved = pkglock.resolve(p)
if (typeof cache === 'function') {
cb = cache
cache = void 0
}

process.nextTick(() => {
if (resolved) {
cb(null, resolved.resolvedPath)
} else {
notFoundError(p, cb)
}
})
})
})

Expand Down

0 comments on commit fd5542f

Please sign in to comment.