| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | module.exports = realpath |
| 2 | realpath.realpath = realpath |
| 3 | realpath.sync = realpathSync |
| 4 | realpath.realpathSync = realpathSync |
| 5 | realpath.monkeypatch = monkeypatch |
| 6 | realpath.unmonkeypatch = unmonkeypatch |
| 7 | |
| 8 | var fs = require('fs') |
| 9 | var origRealpath = fs.realpath |
| 10 | var origRealpathSync = fs.realpathSync |
| 11 | |
| 12 | var version = process.version |
| 13 | var ok = /^v[0-5]\./.test(version) |
| 14 | var old = require('./old.js') |
| 15 | |
| 16 | function newError (er) { |
| 17 | return er && er.syscall === 'realpath' && ( |
| 18 | er.code === 'ELOOP' || |
| 19 | er.code === 'ENOMEM' || |
| 20 | er.code === 'ENAMETOOLONG' |
| 21 | ) |
| 22 | } |
| 23 | |
| 24 | function realpath (p, cache, cb) { |
| 25 | if (ok) { |
| 26 | return origRealpath(p, cache, cb) |
| 27 | } |
| 28 | |
| 29 | if (typeof cache === 'function') { |
| 30 | cb = cache |
| 31 | cache = null |
| 32 | } |
| 33 | origRealpath(p, cache, function (er, result) { |
| 34 | if (newError(er)) { |
| 35 | old.realpath(p, cache, cb) |
| 36 | } else { |
| 37 | cb(er, result) |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | function realpathSync (p, cache) { |
| 43 | if (ok) { |
| 44 | return origRealpathSync(p, cache) |
| 45 | } |
| 46 | |
| 47 | try { |
| 48 | return origRealpathSync(p, cache) |
| 49 | } catch (er) { |
| 50 | if (newError(er)) { |
| 51 | return old.realpathSync(p, cache) |
| 52 | } else { |
| 53 | throw er |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | function monkeypatch () { |
| 59 | fs.realpath = realpath |
| 60 | fs.realpathSync = realpathSync |
| 61 | } |
| 62 | |
| 63 | function unmonkeypatch () { |
| 64 | fs.realpath = origRealpath |
| 65 | fs.realpathSync = origRealpathSync |
| 66 | } |