Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

async.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const events_1 = require("events");
  4. const fsScandir = require("@nodelib/fs.scandir");
  5. const fastq = require("fastq");
  6. const common = require("./common");
  7. const reader_1 = require("./reader");
  8. class AsyncReader extends reader_1.default {
  9. constructor(_root, _settings) {
  10. super(_root, _settings);
  11. this._settings = _settings;
  12. this._scandir = fsScandir.scandir;
  13. this._emitter = new events_1.EventEmitter();
  14. this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
  15. this._isFatalError = false;
  16. this._isDestroyed = false;
  17. this._queue.drain = () => {
  18. if (!this._isFatalError) {
  19. this._emitter.emit('end');
  20. }
  21. };
  22. }
  23. read() {
  24. this._isFatalError = false;
  25. this._isDestroyed = false;
  26. setImmediate(() => {
  27. this._pushToQueue(this._root, this._settings.basePath);
  28. });
  29. return this._emitter;
  30. }
  31. destroy() {
  32. if (this._isDestroyed) {
  33. throw new Error('The reader is already destroyed');
  34. }
  35. this._isDestroyed = true;
  36. this._queue.killAndDrain();
  37. }
  38. onEntry(callback) {
  39. this._emitter.on('entry', callback);
  40. }
  41. onError(callback) {
  42. this._emitter.once('error', callback);
  43. }
  44. onEnd(callback) {
  45. this._emitter.once('end', callback);
  46. }
  47. _pushToQueue(directory, base) {
  48. const queueItem = { directory, base };
  49. this._queue.push(queueItem, (error) => {
  50. if (error !== null) {
  51. this._handleError(error);
  52. }
  53. });
  54. }
  55. _worker(item, done) {
  56. this._scandir(item.directory, this._settings.fsScandirSettings, (error, entries) => {
  57. if (error !== null) {
  58. return done(error, undefined);
  59. }
  60. for (const entry of entries) {
  61. this._handleEntry(entry, item.base);
  62. }
  63. done(null, undefined);
  64. });
  65. }
  66. _handleError(error) {
  67. if (!common.isFatalError(this._settings, error)) {
  68. return;
  69. }
  70. this._isFatalError = true;
  71. this._isDestroyed = true;
  72. this._emitter.emit('error', error);
  73. }
  74. _handleEntry(entry, base) {
  75. if (this._isDestroyed || this._isFatalError) {
  76. return;
  77. }
  78. const fullpath = entry.path;
  79. if (base !== undefined) {
  80. entry.path = common.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  81. }
  82. if (common.isAppliedFilter(this._settings.entryFilter, entry)) {
  83. this._emitEntry(entry);
  84. }
  85. if (entry.dirent.isDirectory() && common.isAppliedFilter(this._settings.deepFilter, entry)) {
  86. this._pushToQueue(fullpath, entry.path);
  87. }
  88. }
  89. _emitEntry(entry) {
  90. this._emitter.emit('entry', entry);
  91. }
  92. }
  93. exports.default = AsyncReader;