MCPcopy
hub / github.com/brianc/node-postgres / parse

Method parse

packages/pg-protocol/src/parser.ts:94–122  ·  view source on GitHub ↗
(buffer: Buffer, callback: MessageCallback)

Source from the content-addressed store, hash-verified

92 }
93
94 public parse(buffer: Buffer, callback: MessageCallback) {
95 this.mergeBuffer(buffer)
96 const bufferFullLength = this.bufferOffset + this.bufferLength
97 let offset = this.bufferOffset
98 while (offset + HEADER_LENGTH <= bufferFullLength) {
99 // code is 1 byte long - it identifies the message type
100 const code = this.buffer[offset]
101 // length is 1 Uint32BE - it is the length of the message EXCLUDING the code
102 const length = this.buffer.readUInt32BE(offset + CODE_LENGTH)
103 const fullMessageLength = CODE_LENGTH + length
104 if (fullMessageLength + offset <= bufferFullLength) {
105 const message = this.handlePacket(offset + HEADER_LENGTH, code, length, this.buffer)
106 callback(message)
107 offset += fullMessageLength
108 } else {
109 break
110 }
111 }
112 if (offset === bufferFullLength) {
113 // No more use for the buffer
114 this.buffer = emptyBuffer
115 this.bufferLength = 0
116 this.bufferOffset = 0
117 } else {
118 // Adjust the cursors of remainingBuffer
119 this.bufferLength = bufferFullLength - offset
120 this.bufferOffset = offset
121 }
122 }
123
124 private mergeBuffer(buffer: Buffer): void {
125 if (this.bufferLength > 0) {

Callers 5

parseFunction · 0.95
submitMethod · 0.45
pauses.tsFile · 0.45

Calls 3

mergeBufferMethod · 0.95
handlePacketMethod · 0.95
callbackFunction · 0.85

Tested by

no test coverage detected