Dir.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Dir = void 0;
  4. const util_1 = require("./node/util");
  5. const Dirent_1 = require("./Dirent");
  6. /**
  7. * A directory stream, like `fs.Dir`.
  8. */
  9. class Dir {
  10. constructor(link, options) {
  11. this.link = link;
  12. this.options = options;
  13. this.iteratorInfo = [];
  14. this.path = link.getParentPath();
  15. this.iteratorInfo.push(link.children[Symbol.iterator]());
  16. }
  17. wrapAsync(method, args, callback) {
  18. (0, util_1.validateCallback)(callback);
  19. setImmediate(() => {
  20. let result;
  21. try {
  22. result = method.apply(this, args);
  23. }
  24. catch (err) {
  25. callback(err);
  26. return;
  27. }
  28. callback(null, result);
  29. });
  30. }
  31. isFunction(x) {
  32. return typeof x === 'function';
  33. }
  34. promisify(obj, fn) {
  35. return (...args) => new Promise((resolve, reject) => {
  36. if (this.isFunction(obj[fn])) {
  37. obj[fn].bind(obj)(...args, (error, result) => {
  38. if (error)
  39. reject(error);
  40. resolve(result);
  41. });
  42. }
  43. else {
  44. reject('Not a function');
  45. }
  46. });
  47. }
  48. closeBase() { }
  49. readBase(iteratorInfo) {
  50. let done;
  51. let value;
  52. let name;
  53. let link;
  54. do {
  55. do {
  56. ({ done, value } = iteratorInfo[iteratorInfo.length - 1].next());
  57. if (!done) {
  58. [name, link] = value;
  59. }
  60. else {
  61. break;
  62. }
  63. } while (name === '.' || name === '..');
  64. if (done) {
  65. iteratorInfo.pop();
  66. if (iteratorInfo.length === 0) {
  67. break;
  68. }
  69. else {
  70. done = false;
  71. }
  72. }
  73. else {
  74. if (this.options.recursive && link.children.size) {
  75. iteratorInfo.push(link.children[Symbol.iterator]());
  76. }
  77. return Dirent_1.default.build(link, this.options.encoding);
  78. }
  79. } while (!done);
  80. return null;
  81. }
  82. closeBaseAsync(callback) {
  83. this.wrapAsync(this.closeBase, [], callback);
  84. }
  85. close(callback) {
  86. if (typeof callback === 'function') {
  87. this.closeBaseAsync(callback);
  88. }
  89. else {
  90. return this.promisify(this, 'closeBaseAsync')();
  91. }
  92. }
  93. closeSync() {
  94. this.closeBase();
  95. }
  96. readBaseAsync(callback) {
  97. this.wrapAsync(this.readBase, [this.iteratorInfo], callback);
  98. }
  99. read(callback) {
  100. if (typeof callback === 'function') {
  101. this.readBaseAsync(callback);
  102. }
  103. else {
  104. return this.promisify(this, 'readBaseAsync')();
  105. }
  106. }
  107. readSync() {
  108. return this.readBase(this.iteratorInfo);
  109. }
  110. [Symbol.asyncIterator]() {
  111. const iteratorInfo = [];
  112. const _this = this;
  113. iteratorInfo.push(_this.link.children[Symbol.iterator]());
  114. // auxiliary object so promisify() can be used
  115. const o = {
  116. readBaseAsync(callback) {
  117. _this.wrapAsync(_this.readBase, [iteratorInfo], callback);
  118. },
  119. };
  120. return {
  121. async next() {
  122. const dirEnt = await _this.promisify(o, 'readBaseAsync')();
  123. if (dirEnt !== null) {
  124. return { done: false, value: dirEnt };
  125. }
  126. else {
  127. return { done: true, value: undefined };
  128. }
  129. },
  130. [Symbol.asyncIterator]() {
  131. throw new Error('Not implemented');
  132. },
  133. };
  134. }
  135. }
  136. exports.Dir = Dir;
  137. //# sourceMappingURL=Dir.js.map