diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index ba51c82c8..c362e454c 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -26,18 +26,14 @@ function arrayString(val) { if (i > 0) { result += ',' } - let item = val[i] + const item = val[i] if (item == null) { result += 'NULL' } else if (Array.isArray(item)) { result += arrayString(item) - } else if (ArrayBuffer.isView(item)) { - if (!(item instanceof Buffer)) { - item = Buffer.from(item.buffer, item.byteOffset, item.byteLength) - } - result += '\\\\x' + item.toString('hex') } else { - result += escapeElement(prepareValue(item)) + const prepared = prepareValue(item) + result += prepared instanceof Buffer ? '\\\\x' + prepared.toString('hex') : escapeElement(prepared) } } result += '}' diff --git a/packages/pg/test/integration/client/custom-binary-array-tests.js b/packages/pg/test/integration/client/custom-binary-array-tests.js new file mode 100644 index 000000000..7515c2d3b --- /dev/null +++ b/packages/pg/test/integration/client/custom-binary-array-tests.js @@ -0,0 +1,45 @@ +'use strict' +const helper = require('./test-helper') +const assert = require('assert') +const suite = new helper.Suite() + +suite.test('custom binary array elements round-trip through bytea arrays', async function () { + const client = new helper.pg.Client(helper.config) + await client.connect() + try { + const bytes = Uint8Array.from({ length: 256 }, (_, i) => i) + const cases = [ + [Buffer.from(bytes), Buffer.from(bytes)], + [Buffer.alloc(0), Buffer.alloc(0)], + [bytes.subarray(127, 130), Buffer.from([127, 128, 129])], + [new DataView(bytes.buffer, 254, 2), Buffer.from([254, 255])], + ] + for (const [value, expected] of cases) { + let calls = 0 + const customType = { + toPostgres: function () { + calls++ + return value + }, + } + const result = await client.query('SELECT $1::bytea[] AS value', [[customType, value, null]]) + assert.deepStrictEqual(result.rows[0].value, [expected, expected, null]) + assert.strictEqual(calls, 1) + } + } finally { + await client.end() + } +}) + +suite.test('chained custom binary elements round-trip through nested bytea arrays', async function () { + const client = new helper.pg.Client(helper.config) + await client.connect() + try { + const bytes = Buffer.from([0, 128, 255]) + const customType = { toPostgres: () => ({ toPostgres: () => bytes }) } + const result = await client.query('SELECT $1::bytea[] AS value', [[[customType], [bytes]]]) + assert.deepStrictEqual(result.rows[0].value, [[bytes], [bytes]]) + } finally { + await client.end() + } +}) diff --git a/packages/pg/test/unit/utils-tests.js b/packages/pg/test/unit/utils-tests.js index ff0d92944..c5f9e102e 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -220,6 +220,39 @@ test('prepareValue: Uint8Array array prepared properly', function () { assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}') }) +test('prepareValue: direct and custom binary array elements prepared properly', function () { + const cases = [ + [Buffer.from([0, 128, 255]), '0080ff'], + [Buffer.alloc(0), ''], + [new Uint8Array([1, 128, 255, 2]).subarray(1, 3), '80ff'], + [new DataView(Uint8Array.from([1, 128, 255, 2]).buffer, 1, 2), '80ff'], + ] + + for (const [value, hex] of cases) { + let calls = 0 + const customType = { + toPostgres: function () { + calls++ + return value + }, + } + assert.strictEqual(utils.prepareValue([value, customType]), `{\\\\x${hex},\\\\x${hex}}`) + assert.strictEqual(calls, 1) + } +}) + +test('prepareValue: chained custom binary elements in nested arrays prepared properly', function () { + const buffer = Buffer.from([0, 128, 255]) + const customType = { toPostgres: () => ({ toPostgres: () => buffer }) } + assert.strictEqual( + utils.prepareValue([ + [customType, null], + [buffer, undefined], + ]), + '{{\\\\x0080ff,NULL},{\\\\x0080ff,NULL}}' + ) +}) + test('prepareValue: objects with complex toPostgres prepared properly', function () { const customType = { toPostgres: function () {