index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const Transform = require('stream').Transform;
  3. /**
  4. * Encodes a Buffer into a base64 encoded string
  5. *
  6. * @param {Buffer} buffer Buffer to convert
  7. * @returns {String} base64 encoded string
  8. */
  9. function encode(buffer) {
  10. if (typeof buffer === 'string') {
  11. buffer = Buffer.from(buffer, 'utf-8');
  12. }
  13. return buffer.toString('base64');
  14. }
  15. /**
  16. * Adds soft line breaks to a base64 string
  17. *
  18. * @param {String} str base64 encoded string that might need line wrapping
  19. * @param {Number} [lineLength=76] Maximum allowed length for a line
  20. * @returns {String} Soft-wrapped base64 encoded string
  21. */
  22. function wrap(str, lineLength) {
  23. str = (str || '').toString();
  24. lineLength = lineLength || 76;
  25. if (str.length <= lineLength) {
  26. return str;
  27. }
  28. let result = [];
  29. let pos = 0;
  30. let chunkLength = lineLength * 1024;
  31. while (pos < str.length) {
  32. let wrappedLines = str
  33. .substr(pos, chunkLength)
  34. .replace(new RegExp('.{' + lineLength + '}', 'g'), '$&\r\n')
  35. .trim();
  36. result.push(wrappedLines);
  37. pos += chunkLength;
  38. }
  39. return result.join('\r\n').trim();
  40. }
  41. /**
  42. * Creates a transform stream for encoding data to base64 encoding
  43. *
  44. * @constructor
  45. * @param {Object} options Stream options
  46. * @param {Number} [options.lineLength=76] Maximum lenght for lines, set to false to disable wrapping
  47. */
  48. class Encoder extends Transform {
  49. constructor(options) {
  50. super();
  51. // init Transform
  52. this.options = options || {};
  53. if (this.options.lineLength !== false) {
  54. this.options.lineLength = this.options.lineLength || 76;
  55. }
  56. this._curLine = '';
  57. this._remainingBytes = false;
  58. this.inputBytes = 0;
  59. this.outputBytes = 0;
  60. }
  61. _transform(chunk, encoding, done) {
  62. if (encoding !== 'buffer') {
  63. chunk = Buffer.from(chunk, encoding);
  64. }
  65. if (!chunk || !chunk.length) {
  66. return setImmediate(done);
  67. }
  68. this.inputBytes += chunk.length;
  69. if (this._remainingBytes && this._remainingBytes.length) {
  70. chunk = Buffer.concat([this._remainingBytes, chunk], this._remainingBytes.length + chunk.length);
  71. this._remainingBytes = false;
  72. }
  73. if (chunk.length % 3) {
  74. this._remainingBytes = chunk.slice(chunk.length - (chunk.length % 3));
  75. chunk = chunk.slice(0, chunk.length - (chunk.length % 3));
  76. } else {
  77. this._remainingBytes = false;
  78. }
  79. let b64 = this._curLine + encode(chunk);
  80. if (this.options.lineLength) {
  81. b64 = wrap(b64, this.options.lineLength);
  82. // remove last line as it is still most probably incomplete
  83. let lastLF = b64.lastIndexOf('\n');
  84. if (lastLF < 0) {
  85. this._curLine = b64;
  86. b64 = '';
  87. } else if (lastLF === b64.length - 1) {
  88. this._curLine = '';
  89. } else {
  90. this._curLine = b64.substr(lastLF + 1);
  91. b64 = b64.substr(0, lastLF + 1);
  92. }
  93. }
  94. if (b64) {
  95. this.outputBytes += b64.length;
  96. this.push(Buffer.from(b64, 'ascii'));
  97. }
  98. setImmediate(done);
  99. }
  100. _flush(done) {
  101. if (this._remainingBytes && this._remainingBytes.length) {
  102. this._curLine += encode(this._remainingBytes);
  103. }
  104. if (this._curLine) {
  105. this._curLine = wrap(this._curLine, this.options.lineLength);
  106. this.outputBytes += this._curLine.length;
  107. this.push(this._curLine, 'ascii');
  108. this._curLine = '';
  109. }
  110. done();
  111. }
  112. }
  113. // expose to the world
  114. module.exports = {
  115. encode,
  116. wrap,
  117. Encoder
  118. };