pg-protocol
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -13,3 +13,5 @@ /// <reference types="node" /> | ||
export declare class Parser { | ||
private remainingBuffer; | ||
private buffer; | ||
private bufferLength; | ||
private bufferOffset; | ||
private reader; | ||
@@ -19,2 +21,3 @@ private mode; | ||
parse(buffer: Buffer, callback: MessageCallback): void; | ||
private mergeBuffer; | ||
private handlePacket; | ||
@@ -21,0 +24,0 @@ private parseReadyForQueryMessage; |
@@ -19,3 +19,5 @@ "use strict"; | ||
var _a, _b; | ||
this.remainingBuffer = emptyBuffer; | ||
this.buffer = emptyBuffer; | ||
this.bufferLength = 0; | ||
this.bufferOffset = 0; | ||
this.reader = new buffer_reader_1.BufferReader(); | ||
@@ -28,17 +30,13 @@ if (((_a = opts) === null || _a === void 0 ? void 0 : _a.mode) === 'binary') { | ||
parse(buffer, callback) { | ||
let combinedBuffer = buffer; | ||
if (this.remainingBuffer.byteLength) { | ||
combinedBuffer = Buffer.allocUnsafe(this.remainingBuffer.byteLength + buffer.byteLength); | ||
this.remainingBuffer.copy(combinedBuffer); | ||
buffer.copy(combinedBuffer, this.remainingBuffer.byteLength); | ||
} | ||
let offset = 0; | ||
while (offset + HEADER_LENGTH <= combinedBuffer.byteLength) { | ||
this.mergeBuffer(buffer); | ||
const bufferFullLength = this.bufferOffset + this.bufferLength; | ||
let offset = this.bufferOffset; | ||
while (offset + HEADER_LENGTH <= bufferFullLength) { | ||
// code is 1 byte long - it identifies the message type | ||
const code = combinedBuffer[offset]; | ||
const code = this.buffer[offset]; | ||
// length is 1 Uint32BE - it is the length of the message EXCLUDING the code | ||
const length = combinedBuffer.readUInt32BE(offset + CODE_LENGTH); | ||
const length = this.buffer.readUInt32BE(offset + CODE_LENGTH); | ||
const fullMessageLength = CODE_LENGTH + length; | ||
if (fullMessageLength + offset <= combinedBuffer.byteLength) { | ||
const message = this.handlePacket(offset + HEADER_LENGTH, code, length, combinedBuffer); | ||
if (fullMessageLength + offset <= bufferFullLength) { | ||
const message = this.handlePacket(offset + HEADER_LENGTH, code, length, this.buffer); | ||
callback(message); | ||
@@ -51,9 +49,48 @@ offset += fullMessageLength; | ||
} | ||
if (offset === combinedBuffer.byteLength) { | ||
this.remainingBuffer = emptyBuffer; | ||
if (offset === bufferFullLength) { | ||
// No more use for the buffer | ||
this.buffer = emptyBuffer; | ||
this.bufferLength = 0; | ||
this.bufferOffset = 0; | ||
} | ||
else { | ||
this.remainingBuffer = combinedBuffer.slice(offset); | ||
// Adjust the cursors of remainingBuffer | ||
this.bufferLength = bufferFullLength - offset; | ||
this.bufferOffset = offset; | ||
} | ||
} | ||
mergeBuffer(buffer) { | ||
if (this.bufferLength > 0) { | ||
const newLength = this.bufferLength + buffer.byteLength; | ||
const newFullLength = newLength + this.bufferOffset; | ||
if (newFullLength > this.buffer.byteLength) { | ||
// We can't concat the new buffer with the remaining one | ||
let newBuffer; | ||
if (newLength <= this.buffer.byteLength && this.bufferOffset >= this.bufferLength) { | ||
// We can move the relevant part to the beginning of the buffer instead of allocating a new buffer | ||
newBuffer = this.buffer; | ||
} | ||
else { | ||
// Allocate a new larger buffer | ||
let newBufferLength = this.buffer.byteLength * 2; | ||
while (newLength >= newBufferLength) { | ||
newBufferLength *= 2; | ||
} | ||
newBuffer = Buffer.allocUnsafe(newBufferLength); | ||
} | ||
// Move the remaining buffer to the new one | ||
this.buffer.copy(newBuffer, 0, this.bufferOffset, this.bufferOffset + this.bufferLength); | ||
this.buffer = newBuffer; | ||
this.bufferOffset = 0; | ||
} | ||
// Concat the new buffer with the remaining one | ||
buffer.copy(this.buffer, this.bufferOffset + this.bufferLength); | ||
this.bufferLength = newLength; | ||
} | ||
else { | ||
this.buffer = buffer; | ||
this.bufferOffset = 0; | ||
this.bufferLength = buffer.byteLength; | ||
} | ||
} | ||
handlePacket(offset, code, length, bytes) { | ||
@@ -60,0 +97,0 @@ switch (code) { |
{ | ||
"name": "pg-protocol", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "The postgres client/server binary protocol, implemented in TypeScript", | ||
@@ -25,3 +25,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "f3136a7d5d5498280924b3e06f47f8ce80dbe4e6" | ||
"gitHead": "dec892ed015af8844f1aa6a9475832c88693b464" | ||
} |
@@ -77,3 +77,5 @@ import { TransformOptions } from 'stream' | ||
export class Parser { | ||
private remainingBuffer: Buffer = emptyBuffer | ||
private buffer: Buffer = emptyBuffer | ||
private bufferLength: number = 0 | ||
private bufferOffset: number = 0 | ||
private reader = new BufferReader() | ||
@@ -90,20 +92,13 @@ private mode: Mode | ||
public parse(buffer: Buffer, callback: MessageCallback) { | ||
let combinedBuffer = buffer | ||
if (this.remainingBuffer.byteLength) { | ||
combinedBuffer = Buffer.allocUnsafe(this.remainingBuffer.byteLength + buffer.byteLength) | ||
this.remainingBuffer.copy(combinedBuffer) | ||
buffer.copy(combinedBuffer, this.remainingBuffer.byteLength) | ||
} | ||
let offset = 0 | ||
while (offset + HEADER_LENGTH <= combinedBuffer.byteLength) { | ||
this.mergeBuffer(buffer) | ||
const bufferFullLength = this.bufferOffset + this.bufferLength | ||
let offset = this.bufferOffset | ||
while (offset + HEADER_LENGTH <= bufferFullLength) { | ||
// code is 1 byte long - it identifies the message type | ||
const code = combinedBuffer[offset] | ||
const code = this.buffer[offset] | ||
// length is 1 Uint32BE - it is the length of the message EXCLUDING the code | ||
const length = combinedBuffer.readUInt32BE(offset + CODE_LENGTH) | ||
const length = this.buffer.readUInt32BE(offset + CODE_LENGTH) | ||
const fullMessageLength = CODE_LENGTH + length | ||
if (fullMessageLength + offset <= combinedBuffer.byteLength) { | ||
const message = this.handlePacket(offset + HEADER_LENGTH, code, length, combinedBuffer) | ||
if (fullMessageLength + offset <= bufferFullLength) { | ||
const message = this.handlePacket(offset + HEADER_LENGTH, code, length, this.buffer) | ||
callback(message) | ||
@@ -115,7 +110,44 @@ offset += fullMessageLength | ||
} | ||
if (offset === bufferFullLength) { | ||
// No more use for the buffer | ||
this.buffer = emptyBuffer | ||
this.bufferLength = 0 | ||
this.bufferOffset = 0 | ||
} else { | ||
// Adjust the cursors of remainingBuffer | ||
this.bufferLength = bufferFullLength - offset | ||
this.bufferOffset = offset | ||
} | ||
} | ||
if (offset === combinedBuffer.byteLength) { | ||
this.remainingBuffer = emptyBuffer | ||
private mergeBuffer(buffer: Buffer): void { | ||
if (this.bufferLength > 0) { | ||
const newLength = this.bufferLength + buffer.byteLength | ||
const newFullLength = newLength + this.bufferOffset | ||
if (newFullLength > this.buffer.byteLength) { | ||
// We can't concat the new buffer with the remaining one | ||
let newBuffer: Buffer | ||
if (newLength <= this.buffer.byteLength && this.bufferOffset >= this.bufferLength) { | ||
// We can move the relevant part to the beginning of the buffer instead of allocating a new buffer | ||
newBuffer = this.buffer | ||
} else { | ||
// Allocate a new larger buffer | ||
let newBufferLength = this.buffer.byteLength * 2 | ||
while (newLength >= newBufferLength) { | ||
newBufferLength *= 2 | ||
} | ||
newBuffer = Buffer.allocUnsafe(newBufferLength) | ||
} | ||
// Move the remaining buffer to the new one | ||
this.buffer.copy(newBuffer, 0, this.bufferOffset, this.bufferOffset + this.bufferLength) | ||
this.buffer = newBuffer | ||
this.bufferOffset = 0 | ||
} | ||
// Concat the new buffer with the remaining one | ||
buffer.copy(this.buffer, this.bufferOffset + this.bufferLength) | ||
this.bufferLength = newLength | ||
} else { | ||
this.remainingBuffer = combinedBuffer.slice(offset) | ||
this.buffer = buffer | ||
this.bufferOffset = 0 | ||
this.bufferLength = buffer.byteLength | ||
} | ||
@@ -122,0 +154,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
229144
4381