Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / readdirp / stream-api.js
1 'use strict';
2
3 var si =  require('set-immediate-shim');
4 var stream = require('readable-stream');
5 var util = require('util');
6
7 var Readable = stream.Readable;
8
9 module.exports = ReaddirpReadable;
10
11 util.inherits(ReaddirpReadable, Readable);
12
13 function ReaddirpReadable (opts) {
14   if (!(this instanceof ReaddirpReadable)) return new ReaddirpReadable(opts);
15
16   opts = opts || {};
17
18   opts.objectMode = true;
19   Readable.call(this, opts);
20
21   // backpressure not implemented at this point
22   this.highWaterMark = Infinity;
23
24   this._destroyed = false;
25   this._paused = false;
26   this._warnings = [];
27   this._errors = [];
28
29   this._pauseResumeErrors();
30 }
31
32 var proto = ReaddirpReadable.prototype;
33
34 proto._pauseResumeErrors = function () {
35   var self = this;
36   self.on('pause', function () { self._paused = true });
37   self.on('resume', function () {
38     if (self._destroyed) return;
39     self._paused = false;
40
41     self._warnings.forEach(function (err) { self.emit('warn', err) });
42     self._warnings.length = 0;
43
44     self._errors.forEach(function (err) { self.emit('error', err) });
45     self._errors.length = 0;
46   })
47 }
48
49 // called for each entry
50 proto._processEntry = function (entry) {
51   if (this._destroyed) return;
52   this.push(entry);
53 }
54
55 proto._read = function () { }
56
57 proto.destroy = function () {
58   // when stream is destroyed it will emit nothing further, not even errors or warnings
59   this.push(null);
60   this.readable = false;
61   this._destroyed = true;
62   this.emit('close');
63 }
64
65 proto._done = function () {
66   this.push(null);
67 }
68
69 // we emit errors and warnings async since we may handle errors like invalid args
70 // within the initial event loop before any event listeners subscribed
71 proto._handleError = function (err) {
72   var self = this;
73   si(function () {
74     if (self._paused) return self._warnings.push(err);
75     if (!self._destroyed) self.emit('warn', err);
76   });
77 }
78
79 proto._handleFatalError = function (err) {
80   var self = this;
81   si(function () {
82     if (self._paused) return self._errors.push(err);
83     if (!self._destroyed) self.emit('error', err);
84   });
85 }
86
87 function createStreamAPI () {
88   var stream = new ReaddirpReadable();
89
90   return {
91       stream           :  stream
92     , processEntry     :  stream._processEntry.bind(stream)
93     , done             :  stream._done.bind(stream)
94     , handleError      :  stream._handleError.bind(stream)
95     , handleFatalError :  stream._handleFatalError.bind(stream)
96   };
97 }
98
99 module.exports = createStreamAPI;