stream-converter.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. // =======================================================================================================
  6. // StreamConverter
  7. //
  8. // convert between encoding schemes in a stream
  9. // Work in Progress - Will complete this at some point
  10. var jconv;
  11. var StreamConverter = /*#__PURE__*/function () {
  12. function StreamConverter(inner, options) {
  13. _classCallCheck(this, StreamConverter);
  14. this.inner = inner;
  15. options = options || {};
  16. this.innerEncoding = (options.innerEncoding || 'UTF8').toUpperCase();
  17. this.outerEncoding = (options.outerEncoding || 'UTF8').toUpperCase();
  18. this.innerBOM = options.innerBOM || null;
  19. this.outerBOM = options.outerBOM || null;
  20. this.writeStarted = false;
  21. }
  22. _createClass(StreamConverter, [{
  23. key: "convertInwards",
  24. value: function convertInwards(data) {
  25. if (data) {
  26. if (typeof data === 'string') {
  27. data = Buffer.from(data, this.outerEncoding);
  28. }
  29. if (this.innerEncoding !== this.outerEncoding) {
  30. data = jconv.convert(data, this.outerEncoding, this.innerEncoding);
  31. }
  32. }
  33. return data;
  34. }
  35. }, {
  36. key: "convertOutwards",
  37. value: function convertOutwards(data) {
  38. if (typeof data === 'string') {
  39. data = Buffer.from(data, this.innerEncoding);
  40. }
  41. if (this.innerEncoding !== this.outerEncoding) {
  42. data = jconv.convert(data, this.innerEncoding, this.outerEncoding);
  43. }
  44. return data;
  45. }
  46. }, {
  47. key: "addListener",
  48. value: function addListener(event, handler) {
  49. this.inner.addListener(event, handler);
  50. }
  51. }, {
  52. key: "removeListener",
  53. value: function removeListener(event, handler) {
  54. this.inner.removeListener(event, handler);
  55. }
  56. }, {
  57. key: "write",
  58. value: function write(data, encoding, callback) {
  59. if (encoding instanceof Function) {
  60. callback = encoding;
  61. encoding = undefined;
  62. }
  63. if (!this.writeStarted) {
  64. // if inner encoding has BOM, write it now
  65. if (this.innerBOM) {
  66. this.inner.write(this.innerBOM);
  67. } // if outer encoding has BOM, delete it now
  68. if (this.outerBOM) {
  69. if (data.length <= this.outerBOM.length) {
  70. if (callback) {
  71. callback();
  72. }
  73. return;
  74. }
  75. var bomless = Buffer.alloc(data.length - this.outerBOM.length);
  76. data.copy(bomless, 0, this.outerBOM.length, data.length);
  77. data = bomless;
  78. }
  79. this.writeStarted = true;
  80. }
  81. this.inner.write(this.convertInwards(data), encoding ? this.innerEncoding : undefined, callback);
  82. }
  83. }, {
  84. key: "read",
  85. value: function read() {// TBD
  86. }
  87. }, {
  88. key: "pipe",
  89. value: function pipe(destination, options) {
  90. var reverseConverter = new StreamConverter(destination, {
  91. innerEncoding: this.outerEncoding,
  92. outerEncoding: this.innerEncoding,
  93. innerBOM: this.outerBOM,
  94. outerBOM: this.innerBOM
  95. });
  96. this.inner.pipe(reverseConverter, options);
  97. }
  98. }, {
  99. key: "close",
  100. value: function close() {
  101. this.inner.close();
  102. }
  103. }, {
  104. key: "on",
  105. value: function on(type, callback) {
  106. var _this = this;
  107. switch (type) {
  108. case 'data':
  109. this.inner.on('data', function (chunk) {
  110. callback(_this.convertOutwards(chunk));
  111. });
  112. return this;
  113. default:
  114. this.inner.on(type, callback);
  115. return this;
  116. }
  117. }
  118. }, {
  119. key: "once",
  120. value: function once(type, callback) {
  121. this.inner.once(type, callback);
  122. }
  123. }, {
  124. key: "end",
  125. value: function end(chunk, encoding, callback) {
  126. this.inner.end(this.convertInwards(chunk), this.innerEncoding, callback);
  127. }
  128. }, {
  129. key: "emit",
  130. value: function emit(type, value) {
  131. this.inner.emit(type, value);
  132. }
  133. }]);
  134. return StreamConverter;
  135. }();
  136. module.exports = StreamConverter;
  137. //# sourceMappingURL=stream-converter.js.map