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

Commit fd5542f

Browse files
committed
fix(fs): make a few more things optimistic
1 parent d06362b commit fd5542f

File tree

1 file changed

+67
-48
lines changed

1 file changed

+67
-48
lines changed

lib/node/fs.js

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,23 @@ function overrideNode () {
9797
}
9898

9999
patchFn('lstatSync', lstatSync => p => {
100-
const resolved = pkglock.resolve(p)
101-
if (resolved == null) {
100+
try {
102101
return lstatSync(p)
103-
} else if (!resolved) {
104-
return notFoundError(p)
105-
} else {
106-
const stats = pkglock.statSync(resolved)
107-
if (!stats) {
108-
notFoundError(p)
102+
} catch (err) {
103+
if (err.code !== 'ENOENT') {
104+
throw err
105+
} else {
106+
const resolved = pkglock.resolve(p)
107+
if (!resolved) {
108+
return notFoundError(p)
109+
} else {
110+
const stats = pkglock.statSync(resolved)
111+
if (!stats) {
112+
notFoundError(p)
113+
}
114+
return stats
115+
}
109116
}
110-
return stats
111117
}
112118
})
113119

@@ -129,17 +135,23 @@ function overrideNode () {
129135
})
130136

131137
patchFn('statSync', statSync => p => {
132-
const resolved = pkglock.resolve(p)
133-
if (resolved == null) {
138+
try {
134139
return statSync(p)
135-
} else if (!resolved) {
136-
return notFoundError(p)
137-
} else {
138-
const stats = pkglock.statSync(resolved)
139-
if (!stats) {
140-
notFoundError(p)
140+
} catch (err) {
141+
if (err.code !== 'ENOENT') {
142+
throw err
143+
} else {
144+
const resolved = pkglock.resolve(p)
145+
if (!resolved) {
146+
return notFoundError(p)
147+
} else {
148+
const stats = pkglock.statSync(resolved)
149+
if (!stats) {
150+
notFoundError(p)
151+
}
152+
return stats
153+
}
141154
}
142-
return stats
143155
}
144156
})
145157

@@ -161,47 +173,54 @@ function overrideNode () {
161173
})
162174

163175
patchFn('statSyncNoException', statSyncNoException => p => {
164-
const resolved = pkglock.resolve(p)
165-
if (resolved == null) {
166-
return statSyncNoException(p)
167-
} else if (!resolved) {
168-
return false
176+
const ret = statSyncNoException(p)
177+
if (ret) {
178+
return ret
169179
} else {
170-
const stats = pkglock.statSync(resolved)
171-
if (!stats) {
180+
const resolved = pkglock.resolve(p)
181+
if (!resolved) {
172182
return false
183+
} else {
184+
const stats = pkglock.statSync(resolved)
185+
if (!stats) {
186+
return false
187+
}
188+
return stats
173189
}
174-
return stats
175190
}
176191
})
177192

178-
patchFn('realpathSync', realpathSync => function (p) {
179-
const resolved = pkglock.resolve(p)
180-
if (resolved == null) {
181-
return realpathSync.apply(this, arguments)
182-
} else if (!resolved) {
183-
return notFoundError(p)
184-
} else {
185-
return resolved.resolvedPath
193+
patchFn('realpathSync', realpathSync => (p, ...args) => {
194+
try {
195+
return realpathSync(p, ...args)
196+
} catch (err) {
197+
if (err.code !== 'ENOENT') { throw err }
198+
const resolved = pkglock.resolve(p)
199+
if (!resolved) {
200+
return notFoundError(p)
201+
} else {
202+
return resolved.resolvedPath
203+
}
186204
}
187205
})
188206

189207
patchFn('realpath', realpath => function (p, cache, cb) {
190-
const resolved = pkglock.resolve(p)
191-
if (resolved == null) {
192-
return realpath.apply(this, arguments)
193-
}
194-
if (typeof cache === 'function') {
195-
cb = cache
196-
cache = void 0
197-
}
198-
199-
process.nextTick(() => {
200-
if (resolved) {
201-
cb(null, resolved.resolvedPath)
202-
} else {
203-
notFoundError(p, cb)
208+
realpath(p, cache, (err, rp) => {
209+
if (err && err.code !== 'ENOENT') { return cb(err) }
210+
if (!err) { return cb(null, rp) }
211+
const resolved = pkglock.resolve(p)
212+
if (typeof cache === 'function') {
213+
cb = cache
214+
cache = void 0
204215
}
216+
217+
process.nextTick(() => {
218+
if (resolved) {
219+
cb(null, resolved.resolvedPath)
220+
} else {
221+
notFoundError(p, cb)
222+
}
223+
})
205224
})
206225
})
207226

0 commit comments

Comments
 (0)