utils.js 10 KB

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