streams.js 3.3 KB

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