From 778197246a643c2d0cc7cb61f1fb64fc1c9a22dc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Aug 2026 03:00:16 +0000 Subject: [PATCH] Deprecate serializing invalid `Date`s Co-authored-by: charmander <1889843+charmander@users.noreply.github.com> --- packages/pg/lib/utils.js | 10 ++++++++++ packages/pg/test/unit/utils-tests.js | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 638b43970..4649405a8 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -1,9 +1,16 @@ 'use strict' const defaults = require('./defaults') +const nodeUtils = require('util') const { isDate } = require('util/types') +const invalidDateDeprecationNotice = nodeUtils.deprecate( + () => {}, + '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.', + 'PG_INVALID_DATE' +) + function escapeElement(elementRepresentation) { const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"') @@ -54,6 +61,9 @@ const prepareValue = function (val, seen) { return Buffer.from(val.buffer, val.byteOffset, val.byteLength) } if (isDate(val)) { + if (isNaN(val.getTime())) { + invalidDateDeprecationNotice() + } if (defaults.parseInputDatesAsUTC) { return dateToStringUTC(val) } else { diff --git a/packages/pg/test/unit/utils-tests.js b/packages/pg/test/unit/utils-tests.js index 5f75f6c2d..edb01f414 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -89,6 +89,23 @@ test('prepareValues: 1 BC date prepared properly', function () { helper.resetTimezoneOffset() }) +test('prepareValue: invalid date emits deprecation warning', function () { + const warningSeen = new Promise((resolve) => { + const onWarning = (warning) => { + if (warning.code === 'PG_INVALID_DATE') { + process.removeListener('warning', onWarning) + resolve() + } + } + process.on('warning', onWarning) + }) + + const out = utils.prepareValue(new Date(NaN)) + assert.strictEqual(out, '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN') + + return warningSeen +}) + test('prepareValues: undefined prepared properly', function () { const out = utils.prepareValue(void 0) assert.strictEqual(out, null)