Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postgres-bytea

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postgres-bytea - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

decode-test.js

33

index.js
'use strict'
module.exports = function parseBytea (input) {
if (/^\\x/.test(input)) {
// new 'hex' style response (pg >9.0)
return Buffer.from(input.substr(2), 'hex')
}
var output = ''
var i = 0
while (i < input.length) {
if (input[i] !== '\\') {
output += input[i]
++i
} else {
if (/[0-7]{3}/.test(input.substr(i + 1, 3))) {
output += String.fromCharCode(parseInt(input.substr(i + 1, 3), 8))
i += 4
} else {
var backslashes = 1
while (i + backslashes < input.length && input[i + backslashes] === '\\') {
backslashes++
}
for (var k = 0; k < Math.floor(backslashes / 2); ++k) {
output += '\\'
}
i += Math.floor(backslashes / 2) * 2
}
}
}
return Buffer.from(output, 'binary')
}
exports = module.exports = require('./decode')
exports.Encoder = require('./encoder')
exports.Decoder = require('./decoder')
{
"name": "postgres-bytea",
"main": "index.js",
"version": "2.0.0",
"version": "3.0.0",
"description": "Postgres bytea parser",

@@ -17,3 +17,3 @@ "license": "MIT",

"scripts": {
"test": "standard && tape test.js"
"test": "standard && tape *.js"
},

@@ -26,11 +26,15 @@ "keywords": [

],
"dependencies": {},
"dependencies": {
"obuf": "~1.1.2"
},
"devDependencies": {
"tape": "^4.0.0",
"standard": "^12.0.1"
"concat-stream": "2.0.0",
"standard": "^14.0.0",
"stream-to-promise": "^3.0.0",
"tape": "^5.0.0",
"tape-promise": "4.0.0"
},
"files": [
"index.js",
"readme.md"
"*.js"
]
}
# postgres-bytea [![Build Status](https://travis-ci.org/bendrucker/postgres-bytea.svg?branch=master)](https://travis-ci.org/bendrucker/postgres-bytea) [![Greenkeeper badge](https://badges.greenkeeper.io/bendrucker/postgres-bytea.svg)](https://greenkeeper.io/)
> Postgres bytea parser
> Decode/encode Postgres bytea strings to Buffers

@@ -8,18 +8,52 @@

```sh
npm install postgres-bytea
```
$ npm install --save postgres-bytea
## Usage
### Decoding
To decode a bytea string into a buffer:
```js
const bytea = require('postgres-bytea')
// bytea hex format
bytea.decode('\\x1234') // <Buffer 12 34>
// bytea escape format
bytea.decode('\\000\\100\\200') // <Buffer 00 40 80>
```
The `decode` function supports both the hex format used in Postgres 9+ and the escape format used in Postgres 8 and earlier. It automatically detects the format from the incoming data.
## Usage
For backward compatibility, `decode` is also the default export from the package.
### Decoding (Stream)
To decode a bytea hex stream into binary:
```js
var bytea = require('postgres-bytea');
bytea('\\000\\100\\200')
//=> buffer
const bytea = require('postgres-bytea')
readable.pipe(new bytea.Decoder())
```
`Decoder` expects a double-escaped `\\x` prefix to allow reading from a `COPY TO` statement.
### Encoding (Stream)
```js
const bytea = require('postgres-bytea')
readable.pipe(new bytea.Encoder())
```
`Encoder` adds a double-escaped `\\x` prefix to allow writing to a `COPY FROM` statement.
## API
#### `bytea(input)` -> `buffer`
#### `bytea.decode(input)` -> `buffer`

@@ -33,4 +67,22 @@ ##### input

#### `new bytea.Decoder()` -> `stream.Transform`
Creates a bytea decoder stream that emits buffer chunks.
#### `new bytea.Encoder()` -> `stream.Transform`
Creates a bytea encoder stream that receives buffer chunks and emits them as bytea strings.
## Prefix Escaping
> The “hex” format encodes binary data as 2 hexadecimal digits per byte, most significant nibble first. The entire string is preceded by the sequence \x (to distinguish it from the escape format). In some contexts, the initial backslash may need to be escaped by doubling it (see Section 4.1.2.1).
>
> https://www.postgresql.org/docs/12/datatype-binary.html#id-1.5.7.12.9
A `SELECT` statement returns bytea values using the single-escaped `\x` prefix. The `COPY TO` and `COPY FROM` commands expect and return bytea values with the double-escaped `\\x` prefix.
`bytea.decode` expects the single-escaped prefix. The `Decoder` and `Encoder` streams expect the double-escaped prefix, since they are most useful in `COPY FROM` and `COPY TO` statements.
## License
MIT © [Ben Drucker](http://bendrucker.me)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc