• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1module.exports = realpath
2realpath.realpath = realpath
3realpath.sync = realpathSync
4realpath.realpathSync = realpathSync
5realpath.monkeypatch = monkeypatch
6realpath.unmonkeypatch = unmonkeypatch
7
8var fs = require('fs')
9var origRealpath = fs.realpath
10var origRealpathSync = fs.realpathSync
11
12var version = process.version
13var ok = /^v[0-5]\./.test(version)
14var old = require('./old.js')
15
16function newError (er) {
17  return er && er.syscall === 'realpath' && (
18    er.code === 'ELOOP' ||
19    er.code === 'ENOMEM' ||
20    er.code === 'ENAMETOOLONG'
21  )
22}
23
24function 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
42function 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
58function monkeypatch () {
59  fs.realpath = realpath
60  fs.realpathSync = realpathSync
61}
62
63function unmonkeypatch () {
64  fs.realpath = origRealpath
65  fs.realpathSync = origRealpathSync
66}
67