NodePathFS.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodePathFS = void 0;
  4. const url_1 = require("url");
  5. const util_1 = require("util");
  6. const ProxiedFS_1 = require("./ProxiedFS");
  7. const path_1 = require("./path");
  8. /**
  9. * Adds support for file URLs and Buffers to the wrapped `baseFs`, but *not* inside the typings.
  10. *
  11. * Only exists for compatibility with Node's behavior.
  12. *
  13. * Automatically wraps all FS instances passed to `patchFs` & `extendFs`.
  14. *
  15. * Don't use it!
  16. */
  17. class NodePathFS extends ProxiedFS_1.ProxiedFS {
  18. constructor(baseFs) {
  19. super(path_1.npath);
  20. this.baseFs = baseFs;
  21. }
  22. mapFromBase(path) {
  23. return path;
  24. }
  25. mapToBase(path) {
  26. if (typeof path === `string`)
  27. return path;
  28. if (path instanceof url_1.URL)
  29. return (0, url_1.fileURLToPath)(path);
  30. if (Buffer.isBuffer(path)) {
  31. const str = path.toString();
  32. if (Buffer.byteLength(str) !== path.byteLength)
  33. throw new Error(`Non-utf8 buffers are not supported at the moment. Please upvote the following issue if you encounter this error: https://github.com/yarnpkg/berry/issues/4942`);
  34. return str;
  35. }
  36. throw new Error(`Unsupported path type: ${(0, util_1.inspect)(path)}`);
  37. }
  38. }
  39. exports.NodePathFS = NodePathFS;