{"library":"postgres-bytea","title":"Postgres Bytea Parser","description":"The `postgres-bytea` library provides robust functionality for parsing and encoding PostgreSQL `bytea` binary strings within Node.js applications. It supports both the modern 'hex' format (prefixed with `\\x`) used in PostgreSQL 9.0 and later, as well as the older 'escape' format from PostgreSQL 8 and earlier, automatically detecting the input format. The current stable version is 3.0.0. While specific release cadence is not explicitly stated, the package appears actively maintained given recent NPM activity. Its key differentiation lies in its dual approach to `bytea` handling: a direct `decode` function for quick conversions and stream-based `Decoder` and `Encoder` classes for handling larger data volumes efficiently, particularly useful with PostgreSQL's `COPY` commands.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install postgres-bytea"],"cli":null},"imports":["import { decode } from 'postgres-bytea'","import { Decoder } from 'postgres-bytea'","import { Encoder } from 'postgres-bytea'","import bytea from 'postgres-bytea'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { decode, Encoder } from 'postgres-bytea';\nimport { pipeline } from 'stream/promises';\nimport { Buffer } from 'buffer';\n\nasync function runByteaExample() {\n  // Example of a PostgreSQL bytea hex string (e.g., from a SELECT query)\n  const hexByteaString = '\\\\x48656c6c6f20506f7374677265735141'; // 'Hello PostgresQA'\n\n  // 1. Decoding a bytea string to a Buffer\n  const decodedBuffer = decode(hexByteaString);\n  console.log('Decoded Buffer:', decodedBuffer); // <Buffer 48 65 6c 6c 6f 20 50 6f 73 74 67 72 65 73 51 41>\n  console.log('Decoded String:', decodedBuffer.toString('utf8')); // 'Hello PostgresQA'\n\n  // Example binary data to encode\n  const originalData = Buffer.from('Binary data for PostgreSQL');\n  let encodedBytea = '';\n\n  // 2. Encoding a Buffer to a bytea string using a stream\n  const encoder = new Encoder();\n  encoder.on('data', (chunk) => {\n    encodedBytea += chunk.toString('utf8');\n  });\n\n  await pipeline(Buffer.from(originalData), encoder);\n\n  console.log('Original Data:', originalData.toString());\n  console.log('Encoded Bytea (from stream):', encodedBytea);\n\n  // Expected encoded format for COPY TO/FROM would be '\\\\x...' or escape format\n  // The Encoder stream emits chunks in the double-escaped hex format (e.g., \\\\x4269...)\n  // if bytea_output is 'hex' and it's used with COPY operations.\n  // The direct decode function expects single-escaped hex (\\x...).\n\n  // Verify decoding of the streamed output (adjusting for single vs double escaping)\n  const cleanedEncodedBytea = encodedBytea.replace(/\\\\\\\\x/, '\\\\x'); // Change \\\\x to \\x for decode function\n  const reDecodedBuffer = decode(cleanedEncodedBytea);\n  console.log('Re-decoded String (from stream output):', reDecodedBuffer.toString('utf8'));\n}","lang":"typescript","description":"This quickstart demonstrates how to decode a PostgreSQL `bytea` hex string into a Node.js Buffer and how to encode a Buffer back into a `bytea` string using the stream API. It highlights the expected string formats for direct `decode` and stream-based `Encoder`/`Decoder` operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}