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