Parser.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. const Utf8Stream = require('../utils/Utf8Stream');
  3. class JsonlParser extends Utf8Stream {
  4. static make(options) {
  5. return new JsonlParser(options);
  6. }
  7. static checkedParse(input, reviver, errorIndicator) {
  8. try {
  9. return JSON.parse(input, reviver);
  10. } catch (error) {
  11. if (typeof errorIndicator == 'function') return errorIndicator(error, input, reviver);
  12. }
  13. return errorIndicator;
  14. }
  15. constructor(options) {
  16. super(Object.assign({}, options, {readableObjectMode: true}));
  17. this._rest = '';
  18. this._counter = 0;
  19. this._reviver = options && options.reviver;
  20. this._errorIndicator = options && options.errorIndicator;
  21. if (options && options.checkErrors) {
  22. this._processBuffer = this._checked_processBuffer;
  23. this._flush = this._checked_flush;
  24. }
  25. if (options && 'errorIndicator' in options) {
  26. this._processBuffer = this._suppressed_processBuffer;
  27. this._flush = this._suppressed_flush;
  28. }
  29. }
  30. _processBuffer(callback) {
  31. const lines = this._buffer.split('\n');
  32. this._rest += lines[0];
  33. if (lines.length > 1) {
  34. this._rest && this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
  35. this._rest = lines.pop();
  36. for (let i = 1; i < lines.length; ++i) {
  37. lines[i] && this.push({key: this._counter++, value: JSON.parse(lines[i], this._reviver)});
  38. }
  39. }
  40. this._buffer = '';
  41. callback(null);
  42. }
  43. _flush(callback) {
  44. super._flush(error => {
  45. if (error) return callback(error);
  46. if (this._rest) {
  47. this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
  48. this._rest = '';
  49. }
  50. callback(null);
  51. });
  52. }
  53. _suppressed_processBuffer(callback) {
  54. const lines = this._buffer.split('\n');
  55. this._rest += lines[0];
  56. if (lines.length > 1) {
  57. if (this._rest) {
  58. const value = JsonlParser.checkedParse(this._rest, this._reviver, this._errorIndicator);
  59. value !== undefined && this.push({key: this._counter++, value});
  60. }
  61. this._rest = lines.pop();
  62. for (let i = 1; i < lines.length; ++i) {
  63. if (!lines[i]) continue;
  64. const value = JsonlParser.checkedParse(lines[i], this._reviver, this._errorIndicator);
  65. value !== undefined && this.push({key: this._counter++, value});
  66. }
  67. }
  68. this._buffer = '';
  69. callback(null);
  70. }
  71. _suppressed_flush(callback) {
  72. super._flush(error => {
  73. if (error) return callback(error);
  74. if (this._rest) {
  75. const value = JsonlParser.checkedParse(this._rest, this._reviver, this._errorIndicator);
  76. value !== undefined && this.push({key: this._counter++, value});
  77. this._rest = '';
  78. }
  79. callback(null);
  80. });
  81. }
  82. _checked_processBuffer(callback) {
  83. const lines = this._buffer.split('\n');
  84. this._rest += lines[0];
  85. if (lines.length > 1) {
  86. try {
  87. this._rest && this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
  88. this._rest = lines.pop();
  89. for (let i = 1; i < lines.length; ++i) {
  90. lines[i] && this.push({key: this._counter++, value: JSON.parse(lines[i], this._reviver)});
  91. }
  92. } catch (cbErr) {
  93. this._buffer = '';
  94. callback(cbErr);
  95. return;
  96. }
  97. }
  98. this._buffer = '';
  99. callback(null);
  100. }
  101. _checked_flush(callback) {
  102. super._flush(error => {
  103. if (error) return callback(error);
  104. if (this._rest) {
  105. try {
  106. this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
  107. } catch (cbErr) {
  108. this._rest = '';
  109. callback(cbErr);
  110. return;
  111. }
  112. this._rest = '';
  113. }
  114. callback(null);
  115. });
  116. }
  117. }
  118. JsonlParser.parser = JsonlParser.make;
  119. JsonlParser.make.Constructor = JsonlParser;
  120. module.exports = JsonlParser;