From 81ae3c3417adbbe6fe54947e7ae9d8f3f4f5b296 Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Wed, 13 May 2026 13:10:22 -0700 Subject: [PATCH 1/2] cleanup: `arrayString` code style --- packages/pg/lib/utils.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index e23a55e9a..33103d246 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -18,14 +18,14 @@ function arrayString(val) { let result = '{' for (let i = 0; i < val.length; i++) { if (i > 0) { - result = result + ',' + result += ',' } - if (val[i] === null || typeof val[i] === 'undefined') { - result = result + 'NULL' - } else if (Array.isArray(val[i])) { - result = result + arrayString(val[i]) - } else if (ArrayBuffer.isView(val[i])) { - let item = val[i] + let 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)) { const buf = Buffer.from(item.buffer, item.byteOffset, item.byteLength) if (buf.length === item.byteLength) { @@ -36,10 +36,10 @@ function arrayString(val) { } result += '\\\\x' + item.toString('hex') } else { - result += escapeElement(prepareValue(val[i])) + result += escapeElement(prepareValue(item)) } } - result = result + '}' + result += '}' return result } From e20ed400c0e1492ea171c576d8850e9e60cd33dc Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Wed, 13 May 2026 13:18:06 -0700 Subject: [PATCH 2/2] cleanup: Move `Buffer.from` Node 4 compatibility code to common function --- packages/pg/lib/utils.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 33103d246..1bbdebaf9 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -11,6 +11,12 @@ function escapeElement(elementRepresentation) { return '"' + escaped + '"' } +// Node.js v4 does not support those Buffer.from params +const bufferFrom = + Buffer.from(new Uint8Array(1).buffer, 0, 0).length === 0 + ? Buffer.from + : (arrayBuffer, byteOffset, length) => Buffer.from(arrayBuffer).slice(byteOffset, byteOffset + length) + // convert a JS array to a postgres array literal // uses comma separator so won't work for types like box that use // a different array separator. @@ -27,12 +33,7 @@ function arrayString(val) { result += arrayString(item) } else if (ArrayBuffer.isView(item)) { if (!(item instanceof Buffer)) { - const buf = Buffer.from(item.buffer, item.byteOffset, item.byteLength) - if (buf.length === item.byteLength) { - item = buf - } else { - item = buf.slice(item.byteOffset, item.byteOffset + item.byteLength) - } + item = bufferFrom(item.buffer, item.byteOffset, item.byteLength) } result += '\\\\x' + item.toString('hex') } else { @@ -57,11 +58,7 @@ const prepareValue = function (val, seen) { return val } if (ArrayBuffer.isView(val)) { - const buf = Buffer.from(val.buffer, val.byteOffset, val.byteLength) - if (buf.length === val.byteLength) { - return buf - } - return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params + return bufferFrom(val.buffer, val.byteOffset, val.byteLength) } if (isDate(val)) { if (defaults.parseInputDatesAsUTC) {