src/buffer-writer.ts

node-postgres · packages/pg-protocol/src/buffer-writer.ts · typescript · MIT
database, Database / ORM

Efficient binary buffer writer for PostgreSQL protocol.

// Source: https://github.com/brianc/node-postgres | License: MIT | Copyright 2010 - 2021 Brian Carlson
// Provided via justclaw.it.com; keep this notice with the code.
//binary data writer tuned for encoding binary specific to the postgres binary protocol

export class Writer {
  public buffer: Buffer
  private offset: number = 5
  private headerPosition: number = 0
  constructor(private size = 256) {
    this.buffer = Buffer.allocUnsafe(size)
  }

  private ensure(size: number): void {
    const remaining = this.buffer.length - this.offset
    if (remaining < size) {
      const oldBuffer = this.buffer
      // exponential growth factor of around ~ 1.5
      // https://stackoverflow.com/questions/2269063/buffer-growth-strategy
      const newSize = oldBuffer.length + (oldBuffer.length >>

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key