patchFs.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = patchFs;
  6. var _lists = require("./util/lists");
  7. function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  8. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  9. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  10. function patchFs(vol) {
  11. var fs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : require('fs');
  12. var bkp = {};
  13. var patch = function patch(key, newValue) {
  14. bkp[key] = fs[key];
  15. fs[key] = newValue;
  16. };
  17. var patchMethod = function patchMethod(key) {
  18. return patch(key, vol[key].bind(vol));
  19. };
  20. var _iterator = _createForOfIteratorHelper(_lists.fsProps),
  21. _step;
  22. try {
  23. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  24. var prop = _step.value;
  25. if (typeof vol[prop] !== 'undefined') patch(prop, vol[prop]);
  26. }
  27. } catch (err) {
  28. _iterator.e(err);
  29. } finally {
  30. _iterator.f();
  31. }
  32. if (typeof vol.StatWatcher === 'function') {
  33. patch('StatWatcher', vol.FSWatcher.bind(null, vol));
  34. }
  35. if (typeof vol.FSWatcher === 'function') {
  36. patch('FSWatcher', vol.StatWatcher.bind(null, vol));
  37. }
  38. if (typeof vol.ReadStream === 'function') {
  39. patch('ReadStream', vol.ReadStream.bind(null, vol));
  40. }
  41. if (typeof vol.WriteStream === 'function') {
  42. patch('WriteStream', vol.WriteStream.bind(null, vol));
  43. }
  44. if (typeof vol._toUnixTimestamp === 'function') patchMethod('_toUnixTimestamp');
  45. var _iterator2 = _createForOfIteratorHelper(_lists.fsAsyncMethods),
  46. _step2;
  47. try {
  48. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  49. var method = _step2.value;
  50. if (typeof vol[method] === 'function') patchMethod(method);
  51. }
  52. } catch (err) {
  53. _iterator2.e(err);
  54. } finally {
  55. _iterator2.f();
  56. }
  57. var _iterator3 = _createForOfIteratorHelper(_lists.fsSyncMethods),
  58. _step3;
  59. try {
  60. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  61. var _method = _step3.value;
  62. if (typeof vol[_method] === 'function') patchMethod(_method);
  63. }
  64. } catch (err) {
  65. _iterator3.e(err);
  66. } finally {
  67. _iterator3.f();
  68. }
  69. return function unpatch() {
  70. for (var key in bkp) {
  71. fs[key] = bkp[key];
  72. }
  73. };
  74. }
  75. ;