Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

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