readlines.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. const fs = require('fs');
  3. /**
  4. * @class
  5. */
  6. class LineByLine {
  7. constructor(file, options) {
  8. options = options || {};
  9. if (!options.readChunk) options.readChunk = 1024;
  10. if (!options.newLineCharacter) {
  11. options.newLineCharacter = 0x0a; //linux line ending
  12. } else {
  13. options.newLineCharacter = options.newLineCharacter.charCodeAt(0);
  14. }
  15. if (typeof file === 'number') {
  16. this.fd = file;
  17. } else {
  18. this.fd = fs.openSync(file, 'r');
  19. }
  20. this.options = options;
  21. this.newLineCharacter = options.newLineCharacter;
  22. this.reset();
  23. }
  24. _searchInBuffer(buffer, hexNeedle) {
  25. let found = -1;
  26. for (let i = 0; i <= buffer.length; i++) {
  27. let b_byte = buffer[i];
  28. if (b_byte === hexNeedle) {
  29. found = i;
  30. break;
  31. }
  32. }
  33. return found;
  34. }
  35. reset() {
  36. this.eofReached = false;
  37. this.linesCache = [];
  38. this.fdPosition = 0;
  39. }
  40. close() {
  41. fs.closeSync(this.fd);
  42. this.fd = null;
  43. }
  44. _extractLines(buffer) {
  45. let line;
  46. const lines = [];
  47. let bufferPosition = 0;
  48. let lastNewLineBufferPosition = 0;
  49. while (true) {
  50. let bufferPositionValue = buffer[bufferPosition++];
  51. if (bufferPositionValue === this.newLineCharacter) {
  52. line = buffer.slice(lastNewLineBufferPosition, bufferPosition);
  53. lines.push(line);
  54. lastNewLineBufferPosition = bufferPosition;
  55. } else if (bufferPositionValue === undefined) {
  56. break;
  57. }
  58. }
  59. let leftovers = buffer.slice(lastNewLineBufferPosition, bufferPosition);
  60. if (leftovers.length) {
  61. lines.push(leftovers);
  62. }
  63. return lines;
  64. };
  65. _readChunk(lineLeftovers) {
  66. let totalBytesRead = 0;
  67. let bytesRead;
  68. const buffers = [];
  69. do {
  70. const readBuffer = new Buffer(this.options.readChunk);
  71. bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk, this.fdPosition);
  72. totalBytesRead = totalBytesRead + bytesRead;
  73. this.fdPosition = this.fdPosition + bytesRead;
  74. buffers.push(readBuffer);
  75. } while (bytesRead && this._searchInBuffer(buffers[buffers.length-1], this.options.newLineCharacter) === -1);
  76. let bufferData = Buffer.concat(buffers);
  77. if (bytesRead < this.options.readChunk) {
  78. this.eofReached = true;
  79. bufferData = bufferData.slice(0, totalBytesRead);
  80. }
  81. if (totalBytesRead) {
  82. this.linesCache = this._extractLines(bufferData);
  83. if (lineLeftovers) {
  84. this.linesCache[0] = Buffer.concat([lineLeftovers, this.linesCache[0]]);
  85. }
  86. }
  87. return totalBytesRead;
  88. }
  89. next() {
  90. if (!this.fd) return false;
  91. let line = false;
  92. if (this.eofReached && this.linesCache.length === 0) {
  93. return line;
  94. }
  95. let bytesRead;
  96. if (!this.linesCache.length) {
  97. bytesRead = this._readChunk();
  98. }
  99. if (this.linesCache.length) {
  100. line = this.linesCache.shift();
  101. const lastLineCharacter = line[line.length-1];
  102. if (lastLineCharacter !== this.newLineCharacter) {
  103. bytesRead = this._readChunk(line);
  104. if (bytesRead) {
  105. line = this.linesCache.shift();
  106. }
  107. }
  108. }
  109. if (this.eofReached && this.linesCache.length === 0) {
  110. this.close();
  111. }
  112. if (line && line[line.length-1] === this.newLineCharacter) {
  113. line = line.slice(0, line.length-1);
  114. }
  115. return line;
  116. }
  117. }
  118. module.exports = LineByLine;