The postgres client/server binary protocol, implemented in TypeScript
npx @tessl/cli install tessl/npm-pg-protocol@1.10.0pg-protocol is a low-level TypeScript library that implements the PostgreSQL client/server binary protocol. It provides streaming parsing capabilities and message serialization utilities that serve as the foundation for higher-level PostgreSQL client libraries in the node-postgres ecosystem.
npm install pg-protocolimport { parse, serialize, DatabaseError } from "pg-protocol";For CommonJS:
const { parse, serialize, DatabaseError } = require("pg-protocol");import { parse, serialize, DatabaseError } from "pg-protocol";
import { Readable } from "stream";
// Parse incoming protocol data from a stream
const callback = (message) => {
console.log('Received message:', message.name);
if (message.name === 'error') {
console.error('Database error:', message);
}
};
// Example with a readable stream containing protocol data
const stream = new Readable();
parse(stream, callback);
// Serialize outbound messages for sending to PostgreSQL server
const queryBuffer = serialize.query("SELECT * FROM users WHERE name = 'john'");
const parseBuffer = serialize.parse({ text: "SELECT * FROM users WHERE id = $1" });
const bindBuffer = serialize.bind({ values: [25] });
const executeBuffer = serialize.execute();pg-protocol is designed around several key components:
parse function provides streaming protocol parsing with an internal Parser classserialize object contains functions for creating all protocol message typesDatabaseError class with detailed PostgreSQL error informationStream-based parsing of PostgreSQL wire protocol messages with callback-based message handling. Supports the complete PostgreSQL protocol specification.
function parse(
stream: NodeJS.ReadableStream,
callback: MessageCallback
): Promise<void>;
type MessageCallback = (msg: BackendMessage) => void;Complete set of functions for creating PostgreSQL protocol messages. Covers authentication, queries, prepared statements, and administrative operations.
const serialize: {
startup(opts: Record<string, string>): Buffer;
query(text: string): Buffer;
parse(query: ParseOpts): Buffer;
bind(config?: BindOpts): Buffer;
execute(config?: ExecOpts): Buffer;
// ... additional serialization functions
};
interface ParseOpts {
name?: string;
types?: number[];
text: string;
}
interface BindOpts {
portal?: string;
binary?: boolean;
statement?: string;
values?: any[];
valueMapper?: ValueMapper;
}Comprehensive TypeScript types and classes representing all PostgreSQL protocol messages, from authentication to data transfer.
interface BackendMessage {
name: MessageName;
length: number;
}
type MessageName =
| 'parseComplete' | 'bindComplete' | 'closeComplete'
| 'dataRow' | 'commandComplete' | 'readyForQuery'
| 'error' | 'notice' | 'authenticationOk'
// ... additional message types
class DatabaseError extends Error {
severity?: string;
code?: string;
detail?: string;
hint?: string;
// ... additional error fields
}type Mode = 'text' | 'binary';
type ValueMapper = (param: any, index: number) => any;
interface ExecOpts {
portal?: string;
rows?: number;
}