util.js 9.4 KB

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