You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.3 KiB

4 years ago
  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. }