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

Method mergeBuffer

packages/pg-protocol/src/parser.ts:124–155  ·  view source on GitHub ↗
(buffer: Buffer)

Source from the content-addressed store, hash-verified

122 }
123
124 private mergeBuffer(buffer: Buffer): void {
125 if (this.bufferLength > 0) {
126 const newLength = this.bufferLength + buffer.byteLength
127 const newFullLength = newLength + this.bufferOffset
128 if (newFullLength > this.buffer.byteLength) {
129 // We can't concat the new buffer with the remaining one
130 let newBuffer: Buffer
131 if (newLength <= this.buffer.byteLength && this.bufferOffset >= this.bufferLength) {
132 // We can move the relevant part to the beginning of the buffer instead of allocating a new buffer
133 newBuffer = this.buffer
134 } else {
135 // Allocate a new larger buffer
136 let newBufferLength = this.buffer.byteLength * 2
137 while (newLength >= newBufferLength) {
138 newBufferLength *= 2
139 }
140 newBuffer = Buffer.allocUnsafe(newBufferLength)
141 }
142 // Move the remaining buffer to the new one
143 this.buffer.copy(newBuffer, 0, this.bufferOffset, this.bufferOffset + this.bufferLength)
144 this.buffer = newBuffer
145 this.bufferOffset = 0
146 }
147 // Concat the new buffer with the remaining one
148 buffer.copy(this.buffer, this.bufferOffset + this.bufferLength)
149 this.bufferLength = newLength
150 } else {
151 this.buffer = buffer
152 this.bufferOffset = 0
153 this.bufferLength = buffer.byteLength
154 }
155 }
156
157 private handlePacket(offset: number, code: number, length: number, bytes: Buffer): BackendMessage {
158 const { reader } = this

Callers 1

parseMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected