Skip to content

Commit 7781972

Browse files
Copilotcharmander
andcommitted
Deprecate serializing invalid Dates
Co-authored-by: charmander <1889843+charmander@users.noreply.github.com>
1 parent c5e8c9a commit 7781972

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

packages/pg/lib/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
'use strict'
22

33
const defaults = require('./defaults')
4+
const nodeUtils = require('util')
45

56
const { isDate } = require('util/types')
67

8+
const invalidDateDeprecationNotice = nodeUtils.deprecate(
9+
() => {},
10+
'Sending an invalid date to Postgres is deprecated and will throw an error in the next major version of pg. Ensure any Date object passed as a query parameter is valid.',
11+
'PG_INVALID_DATE'
12+
)
13+
714
function escapeElement(elementRepresentation) {
815
const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
916

@@ -54,6 +61,9 @@ const prepareValue = function (val, seen) {
5461
return Buffer.from(val.buffer, val.byteOffset, val.byteLength)
5562
}
5663
if (isDate(val)) {
64+
if (isNaN(val.getTime())) {
65+
invalidDateDeprecationNotice()
66+
}
5767
if (defaults.parseInputDatesAsUTC) {
5868
return dateToStringUTC(val)
5969
} else {

packages/pg/test/unit/utils-tests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ test('prepareValues: 1 BC date prepared properly', function () {
8989
helper.resetTimezoneOffset()
9090
})
9191

92+
test('prepareValue: invalid date emits deprecation warning', function () {
93+
const warningSeen = new Promise((resolve) => {
94+
const onWarning = (warning) => {
95+
if (warning.code === 'PG_INVALID_DATE') {
96+
process.removeListener('warning', onWarning)
97+
resolve()
98+
}
99+
}
100+
process.on('warning', onWarning)
101+
})
102+
103+
const out = utils.prepareValue(new Date(NaN))
104+
assert.strictEqual(out, '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN')
105+
106+
return warningSeen
107+
})
108+
92109
test('prepareValues: undefined prepared properly', function () {
93110
const out = utils.prepareValue(void 0)
94111
assert.strictEqual(out, null)

0 commit comments

Comments
 (0)