le-windows.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const stream = require('stream');
  3. const Transform = stream.Transform;
  4. /**
  5. * Ensures that only <CR><LF> sequences are used for linebreaks
  6. *
  7. * @param {Object} options Stream options
  8. */
  9. class LeWindows extends Transform {
  10. constructor(options) {
  11. super(options);
  12. // init Transform
  13. this.options = options || {};
  14. this.lastByte = false;
  15. }
  16. /**
  17. * Escapes dots
  18. */
  19. _transform(chunk, encoding, done) {
  20. let buf;
  21. let lastPos = 0;
  22. for (let i = 0, len = chunk.length; i < len; i++) {
  23. if (chunk[i] === 0x0a) {
  24. // \n
  25. if ((i && chunk[i - 1] !== 0x0d) || (!i && this.lastByte !== 0x0d)) {
  26. if (i > lastPos) {
  27. buf = chunk.slice(lastPos, i);
  28. this.push(buf);
  29. }
  30. this.push(Buffer.from('\r\n'));
  31. lastPos = i + 1;
  32. }
  33. }
  34. }
  35. if (lastPos && lastPos < chunk.length) {
  36. buf = chunk.slice(lastPos);
  37. this.push(buf);
  38. } else if (!lastPos) {
  39. this.push(chunk);
  40. }
  41. this.lastByte = chunk[chunk.length - 1];
  42. done();
  43. }
  44. }
  45. module.exports = LeWindows;