util.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.filenameToSteps = exports.resolve = exports.unixify = exports.isWin = void 0;
  4. exports.isFd = isFd;
  5. exports.validateFd = validateFd;
  6. exports.dataToBuffer = dataToBuffer;
  7. const pathModule = require("path");
  8. const buffer_1 = require("../internal/buffer");
  9. const process_1 = require("../process");
  10. const encoding_1 = require("../encoding");
  11. const constants_1 = require("../node/constants");
  12. exports.isWin = process_1.default.platform === 'win32';
  13. const resolveCrossPlatform = pathModule.resolve;
  14. const { sep } = pathModule.posix ? pathModule.posix : pathModule;
  15. const isSeparator = (str, i) => {
  16. let char = str[i];
  17. return i > 0 && (char === '/' || (exports.isWin && char === '\\'));
  18. };
  19. const removeTrailingSeparator = (str) => {
  20. let i = str.length - 1;
  21. if (i < 2)
  22. return str;
  23. while (isSeparator(str, i))
  24. i--;
  25. return str.substr(0, i + 1);
  26. };
  27. const normalizePath = (str, stripTrailing) => {
  28. if (typeof str !== 'string')
  29. throw new TypeError('expected a string');
  30. str = str.replace(/[\\\/]+/g, '/');
  31. if (stripTrailing !== false)
  32. str = removeTrailingSeparator(str);
  33. return str;
  34. };
  35. const unixify = (filepath, stripTrailing = true) => {
  36. if (exports.isWin) {
  37. filepath = normalizePath(filepath, stripTrailing);
  38. return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
  39. }
  40. return filepath;
  41. };
  42. exports.unixify = unixify;
  43. let resolve = (filename, base = process_1.default.cwd()) => resolveCrossPlatform(base, filename);
  44. exports.resolve = resolve;
  45. if (exports.isWin) {
  46. const _resolve = resolve;
  47. exports.resolve = resolve = (filename, base) => (0, exports.unixify)(_resolve(filename, base));
  48. }
  49. const filenameToSteps = (filename, base) => {
  50. const fullPath = resolve(filename, base);
  51. const fullPathSansSlash = fullPath.substring(1);
  52. if (!fullPathSansSlash)
  53. return [];
  54. return fullPathSansSlash.split(sep);
  55. };
  56. exports.filenameToSteps = filenameToSteps;
  57. function isFd(path) {
  58. return path >>> 0 === path;
  59. }
  60. function validateFd(fd) {
  61. if (!isFd(fd))
  62. throw TypeError(constants_1.ERRSTR.FD);
  63. }
  64. function dataToBuffer(data, encoding = encoding_1.ENCODING_UTF8) {
  65. if (buffer_1.Buffer.isBuffer(data))
  66. return data;
  67. else if (data instanceof Uint8Array)
  68. return (0, buffer_1.bufferFrom)(data);
  69. else
  70. return (0, buffer_1.bufferFrom)(String(data), encoding);
  71. }
  72. //# sourceMappingURL=util.js.map