Change instanceof(Date) to util.types.isDate(Date) (#2862)

* change instanceof to isDate

* use both methods to check for valid Date

* add test for PR 2862

* use only isDate(date) in place of instanceof Date

* Extend compatibility of `isDate` use back to Node 8

* Clean up test

---------

Co-authored-by: Charmander <~@charmander.me>
Reviewed-by: Charmander <~@charmander.me>
This commit is contained in:
Ryan Staples 2025-05-26 18:37:13 -04:00 committed by GitHub
parent 9cf2184d09
commit 3e7bd2f681
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -2,6 +2,9 @@
const defaults = require('./defaults')
const util = require('util')
const { isDate } = util.types || util // Node 8 doesn't have `util.types`
function escapeElement(elementRepresentation) {
const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
@ -60,7 +63,7 @@ const prepareValue = function (val, seen) {
}
return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params
}
if (val instanceof Date) {
if (isDate(val)) {
if (defaults.parseInputDatesAsUTC) {
return dateToStringUTC(val)
} else {

View File

@ -0,0 +1,23 @@
'use strict'
const helper = require('../test-helper')
const assert = require('assert')
const vm = require('vm')
const suite = new helper.Suite()
suite.testAsync('Handle date objects as Date', async () => {
const crossRealmDate = await vm.runInNewContext('new Date()')
assert(!(crossRealmDate instanceof Date))
const date = new Date(crossRealmDate.getTime())
const client = new helper.pg.Client()
await client.connect()
await client.query('CREATE TEMP TABLE foo(bar timestamptz, bar2 timestamptz)')
await client.query('INSERT INTO foo(bar, bar2) VALUES($1, $2)', [date, crossRealmDate])
const results = await client.query('SELECT * FROM foo')
const row = results.rows[0]
assert.deepStrictEqual(row.bar, date)
assert.deepStrictEqual(row.bar2, date)
await client.end()
})