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