react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / iconv-lite / lib / streams.js
81169 views"use strict"12var Transform = require("stream").Transform;345// == Exports ==================================================================6module.exports = function(iconv) {78// Additional Public API.9iconv.encodeStream = function encodeStream(encoding, options) {10return new IconvLiteEncoderStream(iconv.getEncoder(encoding, options), options);11}1213iconv.decodeStream = function decodeStream(encoding, options) {14return new IconvLiteDecoderStream(iconv.getDecoder(encoding, options), options);15}1617iconv.supportsStreams = true;181920// Not published yet.21iconv.IconvLiteEncoderStream = IconvLiteEncoderStream;22iconv.IconvLiteDecoderStream = IconvLiteDecoderStream;23iconv._collect = IconvLiteDecoderStream.prototype.collect;24};252627// == Encoder stream =======================================================28function IconvLiteEncoderStream(conv, options) {29this.conv = conv;30options = options || {};31options.decodeStrings = false; // We accept only strings, so we don't need to decode them.32Transform.call(this, options);33}3435IconvLiteEncoderStream.prototype = Object.create(Transform.prototype, {36constructor: { value: IconvLiteEncoderStream }37});3839IconvLiteEncoderStream.prototype._transform = function(chunk, encoding, done) {40if (typeof chunk != 'string')41return done(new Error("Iconv encoding stream needs strings as its input."));42try {43var res = this.conv.write(chunk);44if (res && res.length) this.push(res);45done();46}47catch (e) {48done(e);49}50}5152IconvLiteEncoderStream.prototype._flush = function(done) {53try {54var res = this.conv.end();55if (res && res.length) this.push(res);56done();57}58catch (e) {59done(e);60}61}6263IconvLiteEncoderStream.prototype.collect = function(cb) {64var chunks = [];65this.on('error', cb);66this.on('data', function(chunk) { chunks.push(chunk); });67this.on('end', function() {68cb(null, Buffer.concat(chunks));69});70return this;71}727374// == Decoder stream =======================================================75function IconvLiteDecoderStream(conv, options) {76this.conv = conv;77options = options || {};78options.encoding = this.encoding = 'utf8'; // We output strings.79Transform.call(this, options);80}8182IconvLiteDecoderStream.prototype = Object.create(Transform.prototype, {83constructor: { value: IconvLiteDecoderStream }84});8586IconvLiteDecoderStream.prototype._transform = function(chunk, encoding, done) {87if (!Buffer.isBuffer(chunk))88return done(new Error("Iconv decoding stream needs buffers as its input."));89try {90var res = this.conv.write(chunk);91if (res && res.length) this.push(res, this.encoding);92done();93}94catch (e) {95done(e);96}97}9899IconvLiteDecoderStream.prototype._flush = function(done) {100try {101var res = this.conv.end();102if (res && res.length) this.push(res, this.encoding);103done();104}105catch (e) {106done(e);107}108}109110IconvLiteDecoderStream.prototype.collect = function(cb) {111var res = '';112this.on('error', cb);113this.on('data', function(chunk) { res += chunk; });114this.on('end', function() {115cb(null, res);116});117return this;118}119120121122