stream-converter.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // =======================================================================================================
  2. // StreamConverter
  3. //
  4. // convert between encoding schemes in a stream
  5. // Work in Progress - Will complete this at some point
  6. let jconv;
  7. class StreamConverter {
  8. constructor(inner, options) {
  9. this.inner = inner;
  10. options = options || {};
  11. this.innerEncoding = (options.innerEncoding || 'UTF8').toUpperCase();
  12. this.outerEncoding = (options.outerEncoding || 'UTF8').toUpperCase();
  13. this.innerBOM = options.innerBOM || null;
  14. this.outerBOM = options.outerBOM || null;
  15. this.writeStarted = false;
  16. }
  17. convertInwards(data) {
  18. if (data) {
  19. if (typeof data === 'string') {
  20. data = Buffer.from(data, this.outerEncoding);
  21. }
  22. if (this.innerEncoding !== this.outerEncoding) {
  23. data = jconv.convert(data, this.outerEncoding, this.innerEncoding);
  24. }
  25. }
  26. return data;
  27. }
  28. convertOutwards(data) {
  29. if (typeof data === 'string') {
  30. data = Buffer.from(data, this.innerEncoding);
  31. }
  32. if (this.innerEncoding !== this.outerEncoding) {
  33. data = jconv.convert(data, this.innerEncoding, this.outerEncoding);
  34. }
  35. return data;
  36. }
  37. addListener(event, handler) {
  38. this.inner.addListener(event, handler);
  39. }
  40. removeListener(event, handler) {
  41. this.inner.removeListener(event, handler);
  42. }
  43. write(data, encoding, callback) {
  44. if (encoding instanceof Function) {
  45. callback = encoding;
  46. encoding = undefined;
  47. }
  48. if (!this.writeStarted) {
  49. // if inner encoding has BOM, write it now
  50. if (this.innerBOM) {
  51. this.inner.write(this.innerBOM);
  52. }
  53. // if outer encoding has BOM, delete it now
  54. if (this.outerBOM) {
  55. if (data.length <= this.outerBOM.length) {
  56. if (callback) {
  57. callback();
  58. }
  59. return;
  60. }
  61. const bomless = Buffer.alloc(data.length - this.outerBOM.length);
  62. data.copy(bomless, 0, this.outerBOM.length, data.length);
  63. data = bomless;
  64. }
  65. this.writeStarted = true;
  66. }
  67. this.inner.write(
  68. this.convertInwards(data),
  69. encoding ? this.innerEncoding : undefined,
  70. callback
  71. );
  72. }
  73. read() {
  74. // TBD
  75. }
  76. pipe(destination, options) {
  77. const reverseConverter = new StreamConverter(destination, {
  78. innerEncoding: this.outerEncoding,
  79. outerEncoding: this.innerEncoding,
  80. innerBOM: this.outerBOM,
  81. outerBOM: this.innerBOM,
  82. });
  83. this.inner.pipe(reverseConverter, options);
  84. }
  85. close() {
  86. this.inner.close();
  87. }
  88. on(type, callback) {
  89. switch (type) {
  90. case 'data':
  91. this.inner.on('data', chunk => {
  92. callback(this.convertOutwards(chunk));
  93. });
  94. return this;
  95. default:
  96. this.inner.on(type, callback);
  97. return this;
  98. }
  99. }
  100. once(type, callback) {
  101. this.inner.once(type, callback);
  102. }
  103. end(chunk, encoding, callback) {
  104. this.inner.end(this.convertInwards(chunk), this.innerEncoding, callback);
  105. }
  106. emit(type, value) {
  107. this.inner.emit(type, value);
  108. }
  109. }
  110. module.exports = StreamConverter;