Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\"')

Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions packages/pg/test/integration/gh-issues/2862-tests.js
Original file line number Diff line number Diff line change
@@ -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()
})