Initial commit
This commit is contained in:
19
node_modules/postgres-bytea/decode-test.js
generated
vendored
Normal file
19
node_modules/postgres-bytea/decode-test.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('tape')
|
||||
const decode = require('./')
|
||||
|
||||
test('decode', (t) => {
|
||||
t.test('pg <9 escape format', (t) => {
|
||||
const buffer = Buffer.from([102, 111, 111, 0, 128, 92, 255])
|
||||
t.ok(buffer.equals(decode('foo\\000\\200\\\\\\377')))
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('pg >=9 hex format', (t) => {
|
||||
t.ok(decode('\\x1234').equals(Buffer.from([0x12, 0x34])))
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
40
node_modules/postgres-bytea/decode.js
generated
vendored
Normal file
40
node_modules/postgres-bytea/decode.js
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = byteaToBinary
|
||||
|
||||
function byteaToBinary (input) {
|
||||
if (/^\\x/.test(input)) {
|
||||
return byteaHexFormatToBinary(input)
|
||||
}
|
||||
return byteaEscapeFormatToBinary(input)
|
||||
}
|
||||
|
||||
function byteaHexFormatToBinary (input) {
|
||||
return Buffer.from(input.substr(2), 'hex')
|
||||
}
|
||||
|
||||
function byteaEscapeFormatToBinary (input) {
|
||||
let output = ''
|
||||
let 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 {
|
||||
let backslashes = 1
|
||||
while (i + backslashes < input.length && input[i + backslashes] === '\\') {
|
||||
backslashes++
|
||||
}
|
||||
for (let k = 0; k < Math.floor(backslashes / 2); ++k) {
|
||||
output += '\\'
|
||||
}
|
||||
i += Math.floor(backslashes / 2) * 2
|
||||
}
|
||||
}
|
||||
}
|
||||
return Buffer.from(output, 'binary')
|
||||
}
|
||||
29
node_modules/postgres-bytea/decoder-test.js
generated
vendored
Normal file
29
node_modules/postgres-bytea/decoder-test.js
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('tape-promise').default(require('tape'))
|
||||
|
||||
const { Readable } = require('stream')
|
||||
const streamToPromise = require('stream-to-promise')
|
||||
const Decoder = require('./decoder')
|
||||
|
||||
test('decoder', (t) => {
|
||||
t.test('input cuts at chunk boundary', async (t) => {
|
||||
const input = [...Buffer.from('\\\\x616263').values()]
|
||||
|
||||
for (let i = 1; i < input.length; i++) {
|
||||
const result = await streamToPromise(Readable.from([input.slice(0, i), input.slice(i)].map(Buffer.from)).pipe(new Decoder()))
|
||||
t.equal(result.toString(), 'abc', `i=${i}`)
|
||||
}
|
||||
})
|
||||
|
||||
t.test('fails if not prefixed with \\\\x', async (t) => {
|
||||
const dest = new Decoder()
|
||||
const promise = streamToPromise(dest)
|
||||
|
||||
dest.write(Buffer.from('616263'))
|
||||
|
||||
await t.rejects(promise, /prefix/)
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
56
node_modules/postgres-bytea/decoder.js
generated
vendored
Normal file
56
node_modules/postgres-bytea/decoder.js
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
|
||||
const BufferList = require('obuf')
|
||||
const { Transform } = require('stream')
|
||||
|
||||
const State = {
|
||||
READ_PREFIX: 1,
|
||||
READ_DATA: 2
|
||||
}
|
||||
|
||||
class ByteaDecoder extends Transform {
|
||||
constructor () {
|
||||
super()
|
||||
this._incomingChunks = new BufferList()
|
||||
this._state = State.READ_PREFIX
|
||||
}
|
||||
|
||||
_transform (chunk, encoding, callback) {
|
||||
this._incomingChunks.push(chunk)
|
||||
|
||||
while (true) {
|
||||
if (this._state === State.READ_PREFIX) {
|
||||
if (this._incomingChunks.has(3)) {
|
||||
const prefix = this._incomingChunks.take(3)
|
||||
const prefixString = prefix.toString()
|
||||
if (prefixString !== '\\\\x') {
|
||||
return this.emit('error', new Error(`Expected double-escaped postgres bytea hex format prefix, received: '${prefixString}'`))
|
||||
}
|
||||
this._state = State.READ_DATA
|
||||
continue
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (this._state === State.READ_DATA) {
|
||||
if (this._incomingChunks.size >= 2) {
|
||||
// two hex characters are needed to parse a byte. read even number of chars, and let remainder roll over
|
||||
let evenChunk
|
||||
const isEvenLength = this._incomingChunks.size % 2 === 0
|
||||
if (isEvenLength) {
|
||||
evenChunk = this._incomingChunks.take(this._incomingChunks.size)
|
||||
} else {
|
||||
evenChunk = this._incomingChunks.take(this._incomingChunks.size - 1)
|
||||
}
|
||||
this.push(Buffer.from(evenChunk.toString(), 'hex'))
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ByteaDecoder
|
||||
23
node_modules/postgres-bytea/encoder-test.js
generated
vendored
Normal file
23
node_modules/postgres-bytea/encoder-test.js
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('tape-promise').default(require('tape'))
|
||||
const { Readable } = require('stream')
|
||||
const streamToPromise = require('stream-to-promise')
|
||||
const Encoder = require('./encoder')
|
||||
|
||||
test('encoder', (t) => {
|
||||
t.test('empty input gives empty result', async (t) => {
|
||||
const result = await streamToPromise(Readable.from([]).pipe(new Encoder()))
|
||||
t.equal(result.toString(), '\\\\x')
|
||||
})
|
||||
|
||||
t.test('input cuts at chunk boundary multiple ways', async (t) => {
|
||||
const input = [0x12, 0x34, 0x56]
|
||||
|
||||
for (let i = 1; i < input.length; i++) {
|
||||
const result = await streamToPromise(Readable.from([input.slice(0, i), input.slice(i)].map(Buffer.from)).pipe(new Encoder()))
|
||||
t.equal(result.toString(), '\\\\x123456')
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
17
node_modules/postgres-bytea/encoder.js
generated
vendored
Normal file
17
node_modules/postgres-bytea/encoder.js
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const { Transform } = require('stream')
|
||||
|
||||
class ByteaEncoder extends Transform {
|
||||
constructor () {
|
||||
super()
|
||||
this.push('\\\\x')
|
||||
}
|
||||
|
||||
_transform (chunk, encoding, callback) {
|
||||
this.push(chunk.toString('hex'))
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ByteaEncoder
|
||||
6
node_modules/postgres-bytea/index.js
generated
vendored
Normal file
6
node_modules/postgres-bytea/index.js
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
exports = module.exports = require('./decode')
|
||||
|
||||
exports.Encoder = require('./encoder')
|
||||
exports.Decoder = require('./decoder')
|
||||
21
node_modules/postgres-bytea/license
generated
vendored
Normal file
21
node_modules/postgres-bytea/license
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Ben Drucker <bvdrucker@gmail.com> (bendrucker.me)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
38
node_modules/postgres-bytea/package.json
generated
vendored
Normal file
38
node_modules/postgres-bytea/package.json
generated
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "postgres-bytea",
|
||||
"main": "index.js",
|
||||
"version": "3.0.0",
|
||||
"description": "Postgres bytea parser",
|
||||
"license": "MIT",
|
||||
"repository": "bendrucker/postgres-bytea",
|
||||
"author": {
|
||||
"name": "Ben Drucker",
|
||||
"email": "bvdrucker@gmail.com",
|
||||
"url": "bendrucker.me"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "standard && tape *.js"
|
||||
},
|
||||
"keywords": [
|
||||
"bytea",
|
||||
"postgres",
|
||||
"binary",
|
||||
"parser"
|
||||
],
|
||||
"dependencies": {
|
||||
"obuf": "~1.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"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": [
|
||||
"*.js"
|
||||
]
|
||||
}
|
||||
86
node_modules/postgres-bytea/readme.md
generated
vendored
Normal file
86
node_modules/postgres-bytea/readme.md
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
# postgres-bytea [](https://travis-ci.org/bendrucker/postgres-bytea) [](https://greenkeeper.io/)
|
||||
|
||||
> Decode/encode Postgres bytea strings to Buffers
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install 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.
|
||||
|
||||
For backward compatibility, `decode` is also the default export from the package.
|
||||
|
||||
### Decoding (Stream)
|
||||
|
||||
To decode a bytea hex stream into binary:
|
||||
|
||||
```js
|
||||
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.decode(input)` -> `buffer`
|
||||
|
||||
##### input
|
||||
|
||||
*Required*
|
||||
Type: `string`
|
||||
|
||||
A Postgres bytea binary string.
|
||||
|
||||
#### `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)
|
||||
Reference in New Issue
Block a user