Skip to content

Commit 9007e55

Browse files
committed
Breaking change: rename pg_types (parse, deparse)=>(decode, encode), (parser, deparser)=>(rowReader, rowWriter)
1 parent 2fa66fe commit 9007e55

9 files changed

+28
-28
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
deparser: require('./lib/deparser'),
3-
parser: require('./lib/parser'),
2+
rowReader: require('./lib/rowReader'),
3+
rowWriter: require('./lib/rowWriter'),
44
transform: require('./lib/transform'),
55
}

lib/pg_types.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const array_send = function (atype, buf, value) {
119119
const flat = flatten(value)
120120
const len = flat.length
121121
for (let i = 0; i < len; i++) {
122-
deparse(buf, atype, flat[i])
122+
encode(buf, atype, flat[i])
123123
}
124124
}
125125
// note the type is not necessary here because it is embedded inside the
@@ -160,7 +160,7 @@ const array_recv = function (buf) {
160160
for (let i = 0; i < len; i++) {
161161
const fieldLen = buf.readUInt32BE(offset)
162162
offset += UInt32Len
163-
flat.push(parse(buf.slice(offset, offset + fieldLen), type))
163+
flat.push(decode(buf.slice(offset, offset + fieldLen), type))
164164
offset += fieldLen
165165
}
166166
let size
@@ -199,7 +199,7 @@ const types = {
199199
_timestamptz: { oid: 1185, send: array_send.bind(null, 'timestamptz'), recv: array_recv },
200200
}
201201

202-
function deparse(buf, type, value) {
202+
function encode(buf, type, value) {
203203
// Add a UInt32 placeholder for the field length
204204
buf.word32be(0)
205205
const lenField = buf.words[buf.words.length - 1]
@@ -219,7 +219,7 @@ function deparse(buf, type, value) {
219219
return buf
220220
}
221221

222-
function parse(buf, type) {
222+
function decode(buf, type) {
223223
return types[type].recv(buf)
224224
}
225225

@@ -229,6 +229,6 @@ function flatten(arr) {
229229

230230
module.exports = {
231231
types: types,
232-
deparse: deparse,
233-
parse: parse,
232+
encode: encode,
233+
decode: decode,
234234
}

lib/parser.js renamed to lib/rowReader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (txt, options) {
1010

1111
const { Transform } = require('stream')
1212
const BP = require('bufferput')
13-
const { parse } = require('./pg_types')
13+
const { decode } = require('./pg_types')
1414
const BufferList = require('bl/BufferList')
1515

1616
const PG_HEADER = 0
@@ -105,7 +105,7 @@ class CopyStream extends Transform {
105105

106106
if (PG_FIELD_END === this._state) {
107107
if (this._fieldBuffer && this.mapping) {
108-
this._fieldBuffer = parse(this._fieldBuffer.slice(), this.mapping[this._fieldIndex].type)
108+
this._fieldBuffer = decode(this._fieldBuffer.slice(), this.mapping[this._fieldIndex].type)
109109
}
110110
if (this.mapping) {
111111
this._row[this.mapping[this._fieldIndex].key] = this._fieldBuffer

lib/deparser.js renamed to lib/rowWriter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function (txt, options) {
1111
const { Transform } = require('stream')
1212

1313
const BufferPut = require('bufferput')
14-
const { deparse } = require('./pg_types')
14+
const { encode } = require('./pg_types')
1515

1616
class CopyStream extends Transform {
1717
constructor(options = {}) {
@@ -53,7 +53,7 @@ class CopyStream extends Transform {
5353
let vec
5454
for (i = 0; i < fieldCount; i++) {
5555
vec = chunk[i]
56-
deparse(buf, vec.type, vec.value)
56+
encode(buf, vec.type, vec.value)
5757
}
5858

5959
this.push(buf.buffer())

lib/transform.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const through2 = require('through2')
22
const MultiFork = require('multi-fork')
33

4-
const parser = require('./parser')
5-
const deparser = require('./deparser')
4+
const rowReader = require('./rowReader')
5+
const rowWriter = require('./rowWriter')
66

77
const shift = function () {
88
return through2.obj(function (row, _, cb) {
@@ -17,7 +17,7 @@ module.exports = function (opt) {
1717
const { transform } = opt
1818
const copyIns = opt.targets
1919

20-
const first = parser({ mapping: mapping })
20+
const first = rowReader({ mapping: mapping })
2121
const n = copyIns.length
2222
let f = n
2323
const finish = function () {
@@ -32,7 +32,7 @@ module.exports = function (opt) {
3232
const M = new MultiFork(n, { classifier: classifier })
3333
for (let i = 0; i < n; i++) {
3434
copyIns[i].on('finish', finish)
35-
M.streams[i].pipe(shift()).pipe(deparser()).pipe(copyIns[i])
35+
M.streams[i].pipe(shift()).pipe(rowWriter()).pipe(copyIns[i])
3636
}
3737
first.pipe(transform).pipe(M)
3838
return first

test/parse.js renamed to test/decode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert')
44
const util = require('util')
55

66
const pgtypes = require('../lib/pg_types')
7-
const { parse } = pgtypes
7+
const { decode } = pgtypes
88

99
const samples = require('./samples')
1010

@@ -23,15 +23,15 @@ function flatten(arr) {
2323

2424
describe('decode', () => {
2525
samples.forEach(function (s) {
26-
it(`parse type ${s.t}: ${util.inspect(s.v)}`, async () => {
26+
it(`decode type ${s.t}: ${util.inspect(s.v)}`, async () => {
2727
const buf = s.r
2828
const isNull = buf.readInt32BE(0)
2929
const UInt32Len = 4
3030
let type = s.t
3131
if (isNull === -1) {
3232
assert.equal(buf.length, UInt32Len, 'A "null" binary buffer should be 0xffffffff')
3333
} else {
34-
let result = parse(buf.slice(UInt32Len), s.t)
34+
let result = decode(buf.slice(UInt32Len), s.t)
3535
let expected = s.v
3636

3737
let results = [result]
@@ -66,7 +66,7 @@ describe('decode', () => {
6666
assert.equal(
6767
result,
6868
expected,
69-
s.t + ': parsed value is incorrect for ' + s.t + ' expected ' + expected + ', got ' + result
69+
s.t + ': decoded value is incorrect for ' + s.t + ' expected ' + expected + ', got ' + result
7070
)
7171
}
7272
}

test/deparse.js renamed to test/encode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ const assert = require('assert')
22
const util = require('util')
33

44
const pgtypes = require('../lib/pg_types')
5-
const { deparse } = pgtypes
5+
const { encode } = pgtypes
66

77
const BP = require('bufferput')
88
const samples = require('./samples')
99

1010
describe('encode', () => {
1111
samples.forEach(function (s) {
1212
it(`encode type ${s.t}: ${util.inspect(s.v)}`, async () => {
13-
const buf = deparse(new BP(), s.t, s.v).buffer()
13+
const buf = encode(new BP(), s.t, s.v).buffer()
1414
const eq = buf.equals(s.r)
1515
assert(
1616
eq,

test/copy-out.js renamed to test/rowReader.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const assert = require('assert')
22
const pg = require('pg')
3-
const { parser } = require('../')
3+
const { rowReader } = require('../')
44
const { to: copyTo } = require('pg-copy-streams')
55
const through2 = require('through2')
66
const concat = require('concat-stream')
@@ -48,7 +48,7 @@ describe('integration test - copyOut', () => {
4848

4949
const sql = 'COPY plug TO STDOUT BINARY'
5050
const copyOut = client.query(copyTo(sql))
51-
const p = parser({ objectMode: true, mapping: mapping })
51+
const p = rowReader({ objectMode: true, mapping: mapping })
5252

5353
idx = 0
5454
const pipeline = copyOut.pipe(p).pipe(
@@ -113,7 +113,7 @@ describe('integration test - copyOut', () => {
113113
assert.deepEqual(arr[0].c1, Buffer.alloc(Math.pow(2, power), '-'))
114114
done()
115115
}
116-
const p = parser({ objectMode: true, mapping: [{ key: 'c1', type: 'bytea' }] })
116+
const p = rowReader({ objectMode: true, mapping: [{ key: 'c1', type: 'bytea' }] })
117117
p.on('error', (err) => {
118118
client.end()
119119
done(err)

test/copy-in.js renamed to test/rowWriter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const assert = require('assert')
22
const util = require('util')
33
const pg = require('pg')
4-
const { deparser } = require('../')
4+
const { rowWriter } = require('../')
55
const { from: copyFrom } = require('pg-copy-streams')
66

77
const getClient = function () {
@@ -16,7 +16,7 @@ describe('integration test - copyIn', () => {
1616
client.query('CREATE TEMP TABLE plug (col1 text)')
1717
const sql = 'COPY plug FROM STDIN BINARY'
1818
const copyIn = client.query(copyFrom(sql))
19-
const encoder = deparser({ objectMode: true })
19+
const encoder = rowWriter({ objectMode: true })
2020
encoder.pipe(copyIn)
2121
const cleanup = (err) => {
2222
done(err)
@@ -74,7 +74,7 @@ describe('integration test - copyIn', () => {
7474
client.query('CREATE TEMP TABLE plug (col1 ' + coltype + ')')
7575
const sql = 'COPY plug FROM STDIN BINARY'
7676
const copyIn = client.query(copyFrom(sql))
77-
const encoder = deparser({ objectMode: true })
77+
const encoder = rowWriter({ objectMode: true })
7878
encoder.pipe(copyIn)
7979
copyIn.on('finish', () => {
8080
const sql = 'SELECT col1::text FROM plug'

0 commit comments

Comments
 (0)