token-stream-parser.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Parser = void 0;
  6. var _events = require("events");
  7. var _streamParser = _interopRequireDefault(require("./stream-parser"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /*
  10. Buffers are thrown at the parser (by calling addBuffer).
  11. Tokens are parsed from the buffer until there are no more tokens in
  12. the buffer, or there is just a partial token left.
  13. If there is a partial token left over, then it is kept until another
  14. buffer is added, which should contain the remainder of the partial
  15. token, along with (perhaps) more tokens.
  16. The partial token and the new buffer are concatenated, and the token
  17. parsing resumes.
  18. */
  19. class Parser extends _events.EventEmitter {
  20. constructor(debug, colMetadata, options) {
  21. super();
  22. this.debug = debug;
  23. this.colMetadata = colMetadata;
  24. this.options = options;
  25. this.parser = new _streamParser.default(this.debug, this.colMetadata, this.options);
  26. this.parser.on('data', token => {
  27. if (token.event) {
  28. this.emit(token.event, token);
  29. }
  30. });
  31. this.parser.on('drain', () => {
  32. this.emit('drain');
  33. });
  34. }
  35. // Returns false to apply backpressure.
  36. addBuffer(buffer) {
  37. return this.parser.write(buffer);
  38. } // Writes an end-of-message (EOM) marker into the parser transform input
  39. // queue. StreamParser will emit a 'data' event with an 'endOfMessage'
  40. // pseudo token when the EOM marker has passed through the transform stream.
  41. // Returns false to apply backpressure.
  42. addEndOfMessageMarker() {
  43. return this.parser.write(this.parser.endOfMessageMarker);
  44. }
  45. isEnd() {
  46. return this.parser.buffer.length === this.parser.position;
  47. } // Temporarily suspends the token stream parser transform from emitting events.
  48. pause() {
  49. this.parser.pause();
  50. } // Resumes the token stream parser transform.
  51. resume() {
  52. this.parser.resume();
  53. }
  54. }
  55. exports.Parser = Parser;