Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / iconv-lite / lib / streams.js
1 var Transform = require("stream").Transform;
2
3
4 // == Exports ==================================================================
5 module.exports = function(iconv) {
6     
7     // Additional Public API.
8     iconv.encodeStream = function encodeStream(encoding, options) {
9         return new IconvLiteEncoderStream(iconv.getCodec(encoding).encoder(options), options);
10     }
11
12     iconv.decodeStream = function decodeStream(encoding, options) {
13         return new IconvLiteDecoderStream(iconv.getCodec(encoding).decoder(options), options);
14     }
15
16     iconv.supportsStreams = true;
17
18
19     // Not published yet.
20     iconv.IconvLiteEncoderStream = IconvLiteEncoderStream;
21     iconv.IconvLiteDecoderStream = IconvLiteDecoderStream;
22     iconv._collect = IconvLiteDecoderStream.prototype.collect;
23 };
24
25
26 // == Encoder stream =======================================================
27 function IconvLiteEncoderStream(conv, options) {
28     this.conv = conv;
29     options = options || {};
30     options.decodeStrings = false; // We accept only strings, so we don't need to decode them.
31     Transform.call(this, options);
32 }
33
34 IconvLiteEncoderStream.prototype = Object.create(Transform.prototype, {
35     constructor: { value: IconvLiteEncoderStream }
36 });
37
38 IconvLiteEncoderStream.prototype._transform = function(chunk, encoding, done) {
39     if (typeof chunk != 'string')
40         return done(new Error("Iconv encoding stream needs strings as its input."));
41     try {
42         var res = this.conv.write(chunk);
43         if (res && res.length) this.push(res);
44         done();
45     }
46     catch (e) {
47         done(e);
48     }
49 }
50
51 IconvLiteEncoderStream.prototype._flush = function(done) {
52     try {
53         var res = this.conv.end();
54         if (res && res.length) this.push(res);
55         done();
56     }
57     catch (e) {
58         done(e);
59     }
60 }
61
62 IconvLiteEncoderStream.prototype.collect = function(cb) {
63     var chunks = [];
64     this.on('error', cb);
65     this.on('data', function(chunk) { chunks.push(chunk); });
66     this.on('end', function() {
67         cb(null, Buffer.concat(chunks));
68     });
69     return this;
70 }
71
72
73 // == Decoder stream =======================================================
74 function IconvLiteDecoderStream(conv, options) {
75     this.conv = conv;
76     options = options || {};
77     options.encoding = this.encoding = 'utf8'; // We output strings.
78     Transform.call(this, options);
79 }
80
81 IconvLiteDecoderStream.prototype = Object.create(Transform.prototype, {
82     constructor: { value: IconvLiteDecoderStream }
83 });
84
85 IconvLiteDecoderStream.prototype._transform = function(chunk, encoding, done) {
86     if (!Buffer.isBuffer(chunk))
87         return done(new Error("Iconv decoding stream needs buffers as its input."));
88     try {
89         var res = this.conv.write(chunk);
90         if (res && res.length) this.push(res, this.encoding);
91         done();
92     }
93     catch (e) {
94         done(e);
95     }
96 }
97
98 IconvLiteDecoderStream.prototype._flush = function(done) {
99     try {
100         var res = this.conv.end();
101         if (res && res.length) this.push(res, this.encoding);                
102         done();
103     }
104     catch (e) {
105         done(e);
106     }
107 }
108
109 IconvLiteDecoderStream.prototype.collect = function(cb) {
110     var res = '';
111     this.on('error', cb);
112     this.on('data', function(chunk) { res += chunk; });
113     this.on('end', function() {
114         cb(null, res);
115     });
116     return this;
117 }
118