streams.js 3.3 KB

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