No Description

index.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. var fs = require('fs')
  8. var origRealpath = fs.realpath
  9. var origRealpathSync = fs.realpathSync
  10. var version = process.version
  11. var ok = /^v[0-5]\./.test(version)
  12. var old = require('./old.js')
  13. function newError (er) {
  14. return er && er.syscall === 'realpath' && (
  15. er.code === 'ELOOP' ||
  16. er.code === 'ENOMEM' ||
  17. er.code === 'ENAMETOOLONG'
  18. )
  19. }
  20. function realpath (p, cache, cb) {
  21. if (ok) {
  22. return origRealpath(p, cache, cb)
  23. }
  24. if (typeof cache === 'function') {
  25. cb = cache
  26. cache = null
  27. }
  28. origRealpath(p, cache, function (er, result) {
  29. if (newError(er)) {
  30. old.realpath(p, cache, cb)
  31. } else {
  32. cb(er, result)
  33. }
  34. })
  35. }
  36. function realpathSync (p, cache) {
  37. if (ok) {
  38. return origRealpathSync(p, cache)
  39. }
  40. try {
  41. return origRealpathSync(p, cache)
  42. } catch (er) {
  43. if (newError(er)) {
  44. return old.realpathSync(p, cache)
  45. } else {
  46. throw er
  47. }
  48. }
  49. }
  50. function monkeypatch () {
  51. fs.realpath = realpath
  52. fs.realpathSync = realpathSync
  53. }
  54. function unmonkeypatch () {
  55. fs.realpath = origRealpath
  56. fs.realpathSync = origRealpathSync
  57. }