Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,36 @@ 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)
Comment on lines +14 to +18
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm p sure we don't support node v4 anymore 😛

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I had left it that way intentionally, but are you okay with cleanup in pg 8 dropping support for Node <16? I can push an update to this and some other things in that case.


// 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.
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) {
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 {
result += escapeElement(prepareValue(val[i]))
result += escapeElement(prepareValue(item))
}
}
result = result + '}'
result += '}'
return result
}

Expand All @@ -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) {
Expand Down
Loading