index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict';
  2. const Transform = require('stream').Transform;
  3. /**
  4. * Encodes a Buffer into a Quoted-Printable encoded string
  5. *
  6. * @param {Buffer} buffer Buffer to convert
  7. * @returns {String} Quoted-Printable encoded string
  8. */
  9. function encode(buffer) {
  10. if (typeof buffer === 'string') {
  11. buffer = Buffer.from(buffer, 'utf-8');
  12. }
  13. // usable characters that do not need encoding
  14. let ranges = [
  15. // https://tools.ietf.org/html/rfc2045#section-6.7
  16. [0x09], // <TAB>
  17. [0x0a], // <LF>
  18. [0x0d], // <CR>
  19. [0x20, 0x3c], // <SP>!"#$%&'()*+,-./0123456789:;
  20. [0x3e, 0x7e] // >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
  21. ];
  22. let result = '';
  23. let ord;
  24. for (let i = 0, len = buffer.length; i < len; i++) {
  25. ord = buffer[i];
  26. // if the char is in allowed range, then keep as is, unless it is a WS in the end of a line
  27. if (checkRanges(ord, ranges) && !((ord === 0x20 || ord === 0x09) && (i === len - 1 || buffer[i + 1] === 0x0a || buffer[i + 1] === 0x0d))) {
  28. result += String.fromCharCode(ord);
  29. continue;
  30. }
  31. result += '=' + (ord < 0x10 ? '0' : '') + ord.toString(16).toUpperCase();
  32. }
  33. return result;
  34. }
  35. /**
  36. * Adds soft line breaks to a Quoted-Printable string
  37. *
  38. * @param {String} str Quoted-Printable encoded string that might need line wrapping
  39. * @param {Number} [lineLength=76] Maximum allowed length for a line
  40. * @returns {String} Soft-wrapped Quoted-Printable encoded string
  41. */
  42. function wrap(str, lineLength) {
  43. str = (str || '').toString();
  44. lineLength = lineLength || 76;
  45. if (str.length <= lineLength) {
  46. return str;
  47. }
  48. let pos = 0;
  49. let len = str.length;
  50. let match, code, line;
  51. let lineMargin = Math.floor(lineLength / 3);
  52. let result = '';
  53. // insert soft linebreaks where needed
  54. while (pos < len) {
  55. line = str.substr(pos, lineLength);
  56. if ((match = line.match(/\r\n/))) {
  57. line = line.substr(0, match.index + match[0].length);
  58. result += line;
  59. pos += line.length;
  60. continue;
  61. }
  62. if (line.substr(-1) === '\n') {
  63. // nothing to change here
  64. result += line;
  65. pos += line.length;
  66. continue;
  67. } else if ((match = line.substr(-lineMargin).match(/\n.*?$/))) {
  68. // truncate to nearest line break
  69. line = line.substr(0, line.length - (match[0].length - 1));
  70. result += line;
  71. pos += line.length;
  72. continue;
  73. } else if (line.length > lineLength - lineMargin && (match = line.substr(-lineMargin).match(/[ \t.,!?][^ \t.,!?]*$/))) {
  74. // truncate to nearest space
  75. line = line.substr(0, line.length - (match[0].length - 1));
  76. } else if (line.match(/[=][\da-f]{0,2}$/i)) {
  77. // push incomplete encoding sequences to the next line
  78. if ((match = line.match(/[=][\da-f]{0,1}$/i))) {
  79. line = line.substr(0, line.length - match[0].length);
  80. }
  81. // ensure that utf-8 sequences are not split
  82. while (line.length > 3 && line.length < len - pos && !line.match(/^(?:=[\da-f]{2}){1,4}$/i) && (match = line.match(/[=][\da-f]{2}$/gi))) {
  83. code = parseInt(match[0].substr(1, 2), 16);
  84. if (code < 128) {
  85. break;
  86. }
  87. line = line.substr(0, line.length - 3);
  88. if (code >= 0xc0) {
  89. break;
  90. }
  91. }
  92. }
  93. if (pos + line.length < len && line.substr(-1) !== '\n') {
  94. if (line.length === lineLength && line.match(/[=][\da-f]{2}$/i)) {
  95. line = line.substr(0, line.length - 3);
  96. } else if (line.length === lineLength) {
  97. line = line.substr(0, line.length - 1);
  98. }
  99. pos += line.length;
  100. line += '=\r\n';
  101. } else {
  102. pos += line.length;
  103. }
  104. result += line;
  105. }
  106. return result;
  107. }
  108. /**
  109. * Helper function to check if a number is inside provided ranges
  110. *
  111. * @param {Number} nr Number to check for
  112. * @param {Array} ranges An Array of allowed values
  113. * @returns {Boolean} True if the value was found inside allowed ranges, false otherwise
  114. */
  115. function checkRanges(nr, ranges) {
  116. for (let i = ranges.length - 1; i >= 0; i--) {
  117. if (!ranges[i].length) {
  118. continue;
  119. }
  120. if (ranges[i].length === 1 && nr === ranges[i][0]) {
  121. return true;
  122. }
  123. if (ranges[i].length === 2 && nr >= ranges[i][0] && nr <= ranges[i][1]) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. /**
  130. * Creates a transform stream for encoding data to Quoted-Printable encoding
  131. *
  132. * @constructor
  133. * @param {Object} options Stream options
  134. * @param {Number} [options.lineLength=76] Maximum lenght for lines, set to false to disable wrapping
  135. */
  136. class Encoder extends Transform {
  137. constructor(options) {
  138. super();
  139. // init Transform
  140. this.options = options || {};
  141. if (this.options.lineLength !== false) {
  142. this.options.lineLength = this.options.lineLength || 76;
  143. }
  144. this._curLine = '';
  145. this.inputBytes = 0;
  146. this.outputBytes = 0;
  147. }
  148. _transform(chunk, encoding, done) {
  149. let qp;
  150. if (encoding !== 'buffer') {
  151. chunk = Buffer.from(chunk, encoding);
  152. }
  153. if (!chunk || !chunk.length) {
  154. return done();
  155. }
  156. this.inputBytes += chunk.length;
  157. if (this.options.lineLength) {
  158. qp = this._curLine + encode(chunk);
  159. qp = wrap(qp, this.options.lineLength);
  160. qp = qp.replace(/(^|\n)([^\n]*)$/, (match, lineBreak, lastLine) => {
  161. this._curLine = lastLine;
  162. return lineBreak;
  163. });
  164. if (qp) {
  165. this.outputBytes += qp.length;
  166. this.push(qp);
  167. }
  168. } else {
  169. qp = encode(chunk);
  170. this.outputBytes += qp.length;
  171. this.push(qp, 'ascii');
  172. }
  173. done();
  174. }
  175. _flush(done) {
  176. if (this._curLine) {
  177. this.outputBytes += this._curLine.length;
  178. this.push(this._curLine, 'ascii');
  179. }
  180. done();
  181. }
  182. }
  183. // expose to the world
  184. module.exports = {
  185. encode,
  186. wrap,
  187. Encoder
  188. };