stream.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const stream_1 = require("stream");
  4. const fsStat = require("@nodelib/fs.stat");
  5. const fsWalk = require("@nodelib/fs.walk");
  6. const reader_1 = require("./reader");
  7. class ReaderStream extends reader_1.default {
  8. constructor() {
  9. super(...arguments);
  10. this._walkStream = fsWalk.walkStream;
  11. this._stat = fsStat.stat;
  12. }
  13. dynamic(root, options) {
  14. return this._walkStream(root, options);
  15. }
  16. static(patterns, options) {
  17. const filepaths = patterns.map(this._getFullEntryPath, this);
  18. const stream = new stream_1.PassThrough({ objectMode: true });
  19. stream._write = (index, _enc, done) => {
  20. return this._getEntry(filepaths[index], patterns[index], options)
  21. .then((entry) => {
  22. if (entry !== null && options.entryFilter(entry)) {
  23. stream.push(entry);
  24. }
  25. if (index === filepaths.length - 1) {
  26. stream.end();
  27. }
  28. done();
  29. })
  30. .catch(done);
  31. };
  32. for (let i = 0; i < filepaths.length; i++) {
  33. stream.write(i);
  34. }
  35. return stream;
  36. }
  37. _getEntry(filepath, pattern, options) {
  38. return this._getStat(filepath)
  39. .then((stats) => this._makeEntry(stats, pattern))
  40. .catch((error) => {
  41. if (options.errorFilter(error)) {
  42. return null;
  43. }
  44. throw error;
  45. });
  46. }
  47. _getStat(filepath) {
  48. return new Promise((resolve, reject) => {
  49. this._stat(filepath, this._fsStatSettings, (error, stats) => {
  50. return error === null ? resolve(stats) : reject(error);
  51. });
  52. });
  53. }
  54. }
  55. exports.default = ReaderStream;