index.js 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const fs = require('fs');
  3. const promisify = require('util.promisify');
  4. const promisiedFsRealpath = promisify(fs.realpath);
  5. function realpath(filepath) {
  6. if (typeof fs.realpath.native === 'function') {
  7. return promisify(fs.realpath.native)(filepath);
  8. }
  9. const fsBinding = process.binding('fs');
  10. if (fsBinding.realpath) {
  11. return new Promise((resolve, reject) => {
  12. try {
  13. resolve(fsBinding.realpath(filepath, 'utf8'));
  14. } catch (e) {
  15. reject(e);
  16. }
  17. });
  18. }
  19. return promisiedFsRealpath(filepath);
  20. }
  21. function realpathSync(filepath) {
  22. if (typeof fs.realpathSync.native === 'function') {
  23. return fs.realpathSync.native(filepath);
  24. }
  25. const fsBinding = process.binding('fs');
  26. if (fsBinding.realpath) {
  27. try {
  28. return fsBinding.realpath(filepath, 'utf8');
  29. } catch (err) {
  30. /* Probably RAM-disk on windows. */
  31. }
  32. }
  33. return fs.realpathSync(filepath);
  34. }
  35. module.exports = realpath;
  36. module.exports.sync = realpathSync;