utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. const fsystem = require("fs");
  2. const pth = require("path");
  3. const Constants = require("./constants");
  4. const Errors = require("./errors");
  5. const isWin = typeof process === "object" && "win32" === process.platform;
  6. const is_Obj = (obj) => typeof obj === "object" && obj !== null;
  7. // generate CRC32 lookup table
  8. const crcTable = new Uint32Array(256).map((t, c) => {
  9. for (let k = 0; k < 8; k++) {
  10. if ((c & 1) !== 0) {
  11. c = 0xedb88320 ^ (c >>> 1);
  12. } else {
  13. c >>>= 1;
  14. }
  15. }
  16. return c >>> 0;
  17. });
  18. // UTILS functions
  19. function Utils(opts) {
  20. this.sep = pth.sep;
  21. this.fs = fsystem;
  22. if (is_Obj(opts)) {
  23. // custom filesystem
  24. if (is_Obj(opts.fs) && typeof opts.fs.statSync === "function") {
  25. this.fs = opts.fs;
  26. }
  27. }
  28. }
  29. module.exports = Utils;
  30. // INSTANTIABLE functions
  31. Utils.prototype.makeDir = function (/*String*/ folder) {
  32. const self = this;
  33. // Sync - make directories tree
  34. function mkdirSync(/*String*/ fpath) {
  35. let resolvedPath = fpath.split(self.sep)[0];
  36. fpath.split(self.sep).forEach(function (name) {
  37. if (!name || name.substr(-1, 1) === ":") return;
  38. resolvedPath += self.sep + name;
  39. var stat;
  40. try {
  41. stat = self.fs.statSync(resolvedPath);
  42. } catch (e) {
  43. if (e.message && e.message.startsWith('ENOENT')) {
  44. self.fs.mkdirSync(resolvedPath);
  45. } else {
  46. throw e;
  47. }
  48. }
  49. if (stat && stat.isFile()) throw Errors.FILE_IN_THE_WAY(`"${resolvedPath}"`);
  50. });
  51. }
  52. mkdirSync(folder);
  53. };
  54. Utils.prototype.writeFileTo = function (/*String*/ path, /*Buffer*/ content, /*Boolean*/ overwrite, /*Number*/ attr) {
  55. const self = this;
  56. if (self.fs.existsSync(path)) {
  57. if (!overwrite) return false; // cannot overwrite
  58. var stat = self.fs.statSync(path);
  59. if (stat.isDirectory()) {
  60. return false;
  61. }
  62. }
  63. var folder = pth.dirname(path);
  64. if (!self.fs.existsSync(folder)) {
  65. self.makeDir(folder);
  66. }
  67. var fd;
  68. try {
  69. fd = self.fs.openSync(path, "w", 0o666); // 0666
  70. } catch (e) {
  71. self.fs.chmodSync(path, 0o666);
  72. fd = self.fs.openSync(path, "w", 0o666);
  73. }
  74. if (fd) {
  75. try {
  76. self.fs.writeSync(fd, content, 0, content.length, 0);
  77. } finally {
  78. self.fs.closeSync(fd);
  79. }
  80. }
  81. self.fs.chmodSync(path, attr || 0o666);
  82. return true;
  83. };
  84. Utils.prototype.writeFileToAsync = function (/*String*/ path, /*Buffer*/ content, /*Boolean*/ overwrite, /*Number*/ attr, /*Function*/ callback) {
  85. if (typeof attr === "function") {
  86. callback = attr;
  87. attr = undefined;
  88. }
  89. const self = this;
  90. self.fs.exists(path, function (exist) {
  91. if (exist && !overwrite) return callback(false);
  92. self.fs.stat(path, function (err, stat) {
  93. if (exist && stat.isDirectory()) {
  94. return callback(false);
  95. }
  96. var folder = pth.dirname(path);
  97. self.fs.exists(folder, function (exists) {
  98. if (!exists) self.makeDir(folder);
  99. self.fs.open(path, "w", 0o666, function (err, fd) {
  100. if (err) {
  101. self.fs.chmod(path, 0o666, function () {
  102. self.fs.open(path, "w", 0o666, function (err, fd) {
  103. self.fs.write(fd, content, 0, content.length, 0, function () {
  104. self.fs.close(fd, function () {
  105. self.fs.chmod(path, attr || 0o666, function () {
  106. callback(true);
  107. });
  108. });
  109. });
  110. });
  111. });
  112. } else if (fd) {
  113. self.fs.write(fd, content, 0, content.length, 0, function () {
  114. self.fs.close(fd, function () {
  115. self.fs.chmod(path, attr || 0o666, function () {
  116. callback(true);
  117. });
  118. });
  119. });
  120. } else {
  121. self.fs.chmod(path, attr || 0o666, function () {
  122. callback(true);
  123. });
  124. }
  125. });
  126. });
  127. });
  128. });
  129. };
  130. Utils.prototype.findFiles = function (/*String*/ path) {
  131. const self = this;
  132. function findSync(/*String*/ dir, /*RegExp*/ pattern, /*Boolean*/ recursive) {
  133. if (typeof pattern === "boolean") {
  134. recursive = pattern;
  135. pattern = undefined;
  136. }
  137. let files = [];
  138. self.fs.readdirSync(dir).forEach(function (file) {
  139. const path = pth.join(dir, file);
  140. const stat = self.fs.statSync(path);
  141. if (!pattern || pattern.test(path)) {
  142. files.push(pth.normalize(path) + (stat.isDirectory() ? self.sep : ""));
  143. }
  144. if (stat.isDirectory() && recursive) files = files.concat(findSync(path, pattern, recursive));
  145. });
  146. return files;
  147. }
  148. return findSync(path, undefined, true);
  149. };
  150. /**
  151. * Callback for showing if everything was done.
  152. *
  153. * @callback filelistCallback
  154. * @param {Error} err - Error object
  155. * @param {string[]} list - was request fully completed
  156. */
  157. /**
  158. *
  159. * @param {string} dir
  160. * @param {filelistCallback} cb
  161. */
  162. Utils.prototype.findFilesAsync = function (dir, cb) {
  163. const self = this;
  164. let results = [];
  165. self.fs.readdir(dir, function (err, list) {
  166. if (err) return cb(err);
  167. let list_length = list.length;
  168. if (!list_length) return cb(null, results);
  169. list.forEach(function (file) {
  170. file = pth.join(dir, file);
  171. self.fs.stat(file, function (err, stat) {
  172. if (err) return cb(err);
  173. if (stat) {
  174. results.push(pth.normalize(file) + (stat.isDirectory() ? self.sep : ""));
  175. if (stat.isDirectory()) {
  176. self.findFilesAsync(file, function (err, res) {
  177. if (err) return cb(err);
  178. results = results.concat(res);
  179. if (!--list_length) cb(null, results);
  180. });
  181. } else {
  182. if (!--list_length) cb(null, results);
  183. }
  184. }
  185. });
  186. });
  187. });
  188. };
  189. Utils.prototype.getAttributes = function () {};
  190. Utils.prototype.setAttributes = function () {};
  191. // STATIC functions
  192. // crc32 single update (it is part of crc32)
  193. Utils.crc32update = function (crc, byte) {
  194. return crcTable[(crc ^ byte) & 0xff] ^ (crc >>> 8);
  195. };
  196. Utils.crc32 = function (buf) {
  197. if (typeof buf === "string") {
  198. buf = Buffer.from(buf, "utf8");
  199. }
  200. let len = buf.length;
  201. let crc = ~0;
  202. for (let off = 0; off < len; ) crc = Utils.crc32update(crc, buf[off++]);
  203. // xor and cast as uint32 number
  204. return ~crc >>> 0;
  205. };
  206. Utils.methodToString = function (/*Number*/ method) {
  207. switch (method) {
  208. case Constants.STORED:
  209. return "STORED (" + method + ")";
  210. case Constants.DEFLATED:
  211. return "DEFLATED (" + method + ")";
  212. default:
  213. return "UNSUPPORTED (" + method + ")";
  214. }
  215. };
  216. /**
  217. * removes ".." style path elements
  218. * @param {string} path - fixable path
  219. * @returns string - fixed filepath
  220. */
  221. Utils.canonical = function (/*string*/ path) {
  222. if (!path) return "";
  223. // trick normalize think path is absolute
  224. const safeSuffix = pth.posix.normalize("/" + path.split("\\").join("/"));
  225. return pth.join(".", safeSuffix);
  226. };
  227. /**
  228. * fix file names in achive
  229. * @param {string} path - fixable path
  230. * @returns string - fixed filepath
  231. */
  232. Utils.zipnamefix = function (path) {
  233. if (!path) return "";
  234. // trick normalize think path is absolute
  235. const safeSuffix = pth.posix.normalize("/" + path.split("\\").join("/"));
  236. return pth.posix.join(".", safeSuffix);
  237. };
  238. /**
  239. *
  240. * @param {Array} arr
  241. * @param {function} callback
  242. * @returns
  243. */
  244. Utils.findLast = function (arr, callback) {
  245. if (!Array.isArray(arr)) throw new TypeError("arr is not array");
  246. const len = arr.length >>> 0;
  247. for (let i = len - 1; i >= 0; i--) {
  248. if (callback(arr[i], i, arr)) {
  249. return arr[i];
  250. }
  251. }
  252. return void 0;
  253. };
  254. // make abolute paths taking prefix as root folder
  255. Utils.sanitize = function (/*string*/ prefix, /*string*/ name) {
  256. prefix = pth.resolve(pth.normalize(prefix));
  257. var parts = name.split("/");
  258. for (var i = 0, l = parts.length; i < l; i++) {
  259. var path = pth.normalize(pth.join(prefix, parts.slice(i, l).join(pth.sep)));
  260. if (path.indexOf(prefix) === 0) {
  261. return path;
  262. }
  263. }
  264. return pth.normalize(pth.join(prefix, pth.basename(name)));
  265. };
  266. // converts buffer, Uint8Array, string types to buffer
  267. Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input, /* function */ encoder) {
  268. if (Buffer.isBuffer(input)) {
  269. return input;
  270. } else if (input instanceof Uint8Array) {
  271. return Buffer.from(input);
  272. } else {
  273. // expect string all other values are invalid and return empty buffer
  274. return typeof input === "string" ? encoder(input) : Buffer.alloc(0);
  275. }
  276. };
  277. Utils.readBigUInt64LE = function (/*Buffer*/ buffer, /*int*/ index) {
  278. const lo = buffer.readUInt32LE(index);
  279. const hi = buffer.readUInt32LE(index + 4);
  280. return hi * 0x100000000 + lo;
  281. };
  282. Utils.fromDOS2Date = function (val) {
  283. return new Date(((val >> 25) & 0x7f) + 1980, Math.max(((val >> 21) & 0x0f) - 1, 0), Math.max((val >> 16) & 0x1f, 1), (val >> 11) & 0x1f, (val >> 5) & 0x3f, (val & 0x1f) << 1);
  284. };
  285. Utils.fromDate2DOS = function (val) {
  286. let date = 0;
  287. let time = 0;
  288. if (val.getFullYear() > 1979) {
  289. date = (((val.getFullYear() - 1980) & 0x7f) << 9) | ((val.getMonth() + 1) << 5) | val.getDate();
  290. time = (val.getHours() << 11) | (val.getMinutes() << 5) | (val.getSeconds() >> 1);
  291. }
  292. return (date << 16) | time;
  293. };
  294. Utils.isWin = isWin; // Do we have windows system
  295. Utils.crcTable = crcTable;