util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.unixify = exports.getWriteSyncArgs = exports.getWriteArgs = exports.bufToUint8 = exports.isWin = void 0;
  4. exports.promisify = promisify;
  5. exports.validateCallback = validateCallback;
  6. exports.modeToNumber = modeToNumber;
  7. exports.nullCheck = nullCheck;
  8. exports.pathToFilename = pathToFilename;
  9. exports.createError = createError;
  10. exports.genRndStr6 = genRndStr6;
  11. exports.flagsToNumber = flagsToNumber;
  12. exports.isFd = isFd;
  13. exports.validateFd = validateFd;
  14. exports.streamToBuffer = streamToBuffer;
  15. exports.dataToBuffer = dataToBuffer;
  16. exports.bufferToEncoding = bufferToEncoding;
  17. exports.isReadableStream = isReadableStream;
  18. const constants_1 = require("./constants");
  19. const errors = require("../internal/errors");
  20. const buffer_1 = require("../internal/buffer");
  21. const encoding_1 = require("../encoding");
  22. const buffer_2 = require("../internal/buffer");
  23. const queueMicrotask_1 = require("../queueMicrotask");
  24. exports.isWin = process.platform === 'win32';
  25. function promisify(fs, fn, getResult = input => input) {
  26. return (...args) => new Promise((resolve, reject) => {
  27. fs[fn].bind(fs)(...args, (error, result) => {
  28. if (error)
  29. return reject(error);
  30. return resolve(getResult(result));
  31. });
  32. });
  33. }
  34. function validateCallback(callback) {
  35. if (typeof callback !== 'function')
  36. throw TypeError(constants_1.ERRSTR.CB);
  37. return callback;
  38. }
  39. function _modeToNumber(mode, def) {
  40. if (typeof mode === 'number')
  41. return mode;
  42. if (typeof mode === 'string')
  43. return parseInt(mode, 8);
  44. if (def)
  45. return modeToNumber(def);
  46. return undefined;
  47. }
  48. function modeToNumber(mode, def) {
  49. const result = _modeToNumber(mode, def);
  50. if (typeof result !== 'number' || isNaN(result))
  51. throw new TypeError(constants_1.ERRSTR.MODE_INT);
  52. return result;
  53. }
  54. function nullCheck(path, callback) {
  55. if (('' + path).indexOf('\u0000') !== -1) {
  56. const er = new Error('Path must be a string without null bytes');
  57. er.code = 'ENOENT';
  58. if (typeof callback !== 'function')
  59. throw er;
  60. (0, queueMicrotask_1.default)(() => {
  61. callback(er);
  62. });
  63. return false;
  64. }
  65. return true;
  66. }
  67. function getPathFromURLPosix(url) {
  68. if (url.hostname !== '') {
  69. throw new errors.TypeError('ERR_INVALID_FILE_URL_HOST', process.platform);
  70. }
  71. const pathname = url.pathname;
  72. for (let n = 0; n < pathname.length; n++) {
  73. if (pathname[n] === '%') {
  74. const third = pathname.codePointAt(n + 2) | 0x20;
  75. if (pathname[n + 1] === '2' && third === 102) {
  76. throw new errors.TypeError('ERR_INVALID_FILE_URL_PATH', 'must not include encoded / characters');
  77. }
  78. }
  79. }
  80. return decodeURIComponent(pathname);
  81. }
  82. function pathToFilename(path) {
  83. if (typeof path !== 'string' && !buffer_1.Buffer.isBuffer(path)) {
  84. try {
  85. if (!(path instanceof require('url').URL))
  86. throw new TypeError(constants_1.ERRSTR.PATH_STR);
  87. }
  88. catch (err) {
  89. throw new TypeError(constants_1.ERRSTR.PATH_STR);
  90. }
  91. path = getPathFromURLPosix(path);
  92. }
  93. const pathString = String(path);
  94. nullCheck(pathString);
  95. // return slash(pathString);
  96. return pathString;
  97. }
  98. const ENOENT = 'ENOENT';
  99. const EBADF = 'EBADF';
  100. const EINVAL = 'EINVAL';
  101. const EPERM = 'EPERM';
  102. const EPROTO = 'EPROTO';
  103. const EEXIST = 'EEXIST';
  104. const ENOTDIR = 'ENOTDIR';
  105. const EMFILE = 'EMFILE';
  106. const EACCES = 'EACCES';
  107. const EISDIR = 'EISDIR';
  108. const ENOTEMPTY = 'ENOTEMPTY';
  109. const ENOSYS = 'ENOSYS';
  110. const ERR_FS_EISDIR = 'ERR_FS_EISDIR';
  111. const ERR_OUT_OF_RANGE = 'ERR_OUT_OF_RANGE';
  112. function formatError(errorCode, func = '', path = '', path2 = '') {
  113. let pathFormatted = '';
  114. if (path)
  115. pathFormatted = ` '${path}'`;
  116. if (path2)
  117. pathFormatted += ` -> '${path2}'`;
  118. switch (errorCode) {
  119. case ENOENT:
  120. return `ENOENT: no such file or directory, ${func}${pathFormatted}`;
  121. case EBADF:
  122. return `EBADF: bad file descriptor, ${func}${pathFormatted}`;
  123. case EINVAL:
  124. return `EINVAL: invalid argument, ${func}${pathFormatted}`;
  125. case EPERM:
  126. return `EPERM: operation not permitted, ${func}${pathFormatted}`;
  127. case EPROTO:
  128. return `EPROTO: protocol error, ${func}${pathFormatted}`;
  129. case EEXIST:
  130. return `EEXIST: file already exists, ${func}${pathFormatted}`;
  131. case ENOTDIR:
  132. return `ENOTDIR: not a directory, ${func}${pathFormatted}`;
  133. case EISDIR:
  134. return `EISDIR: illegal operation on a directory, ${func}${pathFormatted}`;
  135. case EACCES:
  136. return `EACCES: permission denied, ${func}${pathFormatted}`;
  137. case ENOTEMPTY:
  138. return `ENOTEMPTY: directory not empty, ${func}${pathFormatted}`;
  139. case EMFILE:
  140. return `EMFILE: too many open files, ${func}${pathFormatted}`;
  141. case ENOSYS:
  142. return `ENOSYS: function not implemented, ${func}${pathFormatted}`;
  143. case ERR_FS_EISDIR:
  144. return `[ERR_FS_EISDIR]: Path is a directory: ${func} returned EISDIR (is a directory) ${path}`;
  145. case ERR_OUT_OF_RANGE:
  146. return `[ERR_OUT_OF_RANGE]: value out of range, ${func}${pathFormatted}`;
  147. default:
  148. return `${errorCode}: error occurred, ${func}${pathFormatted}`;
  149. }
  150. }
  151. function createError(errorCode, func = '', path = '', path2 = '', Constructor = Error) {
  152. const error = new Constructor(formatError(errorCode, func, path, path2));
  153. error.code = errorCode;
  154. if (path) {
  155. error.path = path;
  156. }
  157. return error;
  158. }
  159. function genRndStr6() {
  160. const str = (Math.random() + 1).toString(36).substring(2, 8);
  161. if (str.length === 6)
  162. return str;
  163. else
  164. return genRndStr6();
  165. }
  166. function flagsToNumber(flags) {
  167. if (typeof flags === 'number')
  168. return flags;
  169. if (typeof flags === 'string') {
  170. const flagsNum = constants_1.FLAGS[flags];
  171. if (typeof flagsNum !== 'undefined')
  172. return flagsNum;
  173. }
  174. // throw new TypeError(formatError(ERRSTR_FLAG(flags)));
  175. throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'flags', flags);
  176. }
  177. function isFd(path) {
  178. return path >>> 0 === path;
  179. }
  180. function validateFd(fd) {
  181. if (!isFd(fd))
  182. throw TypeError(constants_1.ERRSTR.FD);
  183. }
  184. function streamToBuffer(stream) {
  185. const chunks = [];
  186. return new Promise((resolve, reject) => {
  187. stream.on('data', chunk => chunks.push(chunk));
  188. stream.on('end', () => resolve(buffer_1.Buffer.concat(chunks)));
  189. stream.on('error', reject);
  190. });
  191. }
  192. function dataToBuffer(data, encoding = encoding_1.ENCODING_UTF8) {
  193. if (buffer_1.Buffer.isBuffer(data))
  194. return data;
  195. else if (data instanceof Uint8Array)
  196. return (0, buffer_2.bufferFrom)(data);
  197. else
  198. return (0, buffer_2.bufferFrom)(String(data), encoding);
  199. }
  200. const bufToUint8 = (buf) => new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
  201. exports.bufToUint8 = bufToUint8;
  202. const getWriteArgs = (fd, a, b, c, d, e) => {
  203. validateFd(fd);
  204. let offset = 0;
  205. let length;
  206. let position = null;
  207. let encoding;
  208. let callback;
  209. const tipa = typeof a;
  210. const tipb = typeof b;
  211. const tipc = typeof c;
  212. const tipd = typeof d;
  213. if (tipa !== 'string') {
  214. if (tipb === 'function') {
  215. callback = b;
  216. }
  217. else if (tipc === 'function') {
  218. offset = b | 0;
  219. callback = c;
  220. }
  221. else if (tipd === 'function') {
  222. offset = b | 0;
  223. length = c;
  224. callback = d;
  225. }
  226. else {
  227. offset = b | 0;
  228. length = c;
  229. position = d;
  230. callback = e;
  231. }
  232. }
  233. else {
  234. if (tipb === 'function') {
  235. callback = b;
  236. }
  237. else if (tipc === 'function') {
  238. position = b;
  239. callback = c;
  240. }
  241. else if (tipd === 'function') {
  242. position = b;
  243. encoding = c;
  244. callback = d;
  245. }
  246. }
  247. const buf = dataToBuffer(a, encoding);
  248. if (tipa !== 'string') {
  249. if (typeof length === 'undefined')
  250. length = buf.length;
  251. }
  252. else {
  253. offset = 0;
  254. length = buf.length;
  255. }
  256. const cb = validateCallback(callback);
  257. return [fd, tipa === 'string', buf, offset, length, position, cb];
  258. };
  259. exports.getWriteArgs = getWriteArgs;
  260. const getWriteSyncArgs = (fd, a, b, c, d) => {
  261. validateFd(fd);
  262. let encoding;
  263. let offset;
  264. let length;
  265. let position;
  266. const isBuffer = typeof a !== 'string';
  267. if (isBuffer) {
  268. offset = (b || 0) | 0;
  269. length = c;
  270. position = d;
  271. }
  272. else {
  273. position = b;
  274. encoding = c;
  275. }
  276. const buf = dataToBuffer(a, encoding);
  277. if (isBuffer) {
  278. if (typeof length === 'undefined') {
  279. length = buf.length;
  280. }
  281. }
  282. else {
  283. offset = 0;
  284. length = buf.length;
  285. }
  286. return [fd, buf, offset || 0, length, position];
  287. };
  288. exports.getWriteSyncArgs = getWriteSyncArgs;
  289. function bufferToEncoding(buffer, encoding) {
  290. if (!encoding || encoding === 'buffer')
  291. return buffer;
  292. else
  293. return buffer.toString(encoding);
  294. }
  295. function isReadableStream(stream) {
  296. return (stream !== null &&
  297. typeof stream === 'object' &&
  298. typeof stream.pipe === 'function' &&
  299. typeof stream.on === 'function' &&
  300. stream.readable === true);
  301. }
  302. const isSeparator = (str, i) => {
  303. let char = str[i];
  304. return i > 0 && (char === '/' || (exports.isWin && char === '\\'));
  305. };
  306. const removeTrailingSeparator = (str) => {
  307. let i = str.length - 1;
  308. if (i < 2)
  309. return str;
  310. while (isSeparator(str, i))
  311. i--;
  312. return str.substr(0, i + 1);
  313. };
  314. const normalizePath = (str, stripTrailing) => {
  315. if (typeof str !== 'string')
  316. throw new TypeError('expected a string');
  317. str = str.replace(/[\\\/]+/g, '/');
  318. if (stripTrailing !== false)
  319. str = removeTrailingSeparator(str);
  320. return str;
  321. };
  322. const unixify = (filepath, stripTrailing = true) => {
  323. if (exports.isWin) {
  324. filepath = normalizePath(filepath, stripTrailing);
  325. return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
  326. }
  327. return filepath;
  328. };
  329. exports.unixify = unixify;
  330. //# sourceMappingURL=util.js.map