Skip to content

Commit 3e7bd2f

Browse files
Sraymancharmander
andauthored
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 <[email protected]> Reviewed-by: Charmander <[email protected]>
1 parent 9cf2184 commit 3e7bd2f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/pg/lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const defaults = require('./defaults')
44

5+
const util = require('util')
6+
const { isDate } = util.types || util // Node 8 doesn't have `util.types`
7+
58
function escapeElement(elementRepresentation) {
69
const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
710

@@ -60,7 +63,7 @@ const prepareValue = function (val, seen) {
6063
}
6164
return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params
6265
}
63-
if (val instanceof Date) {
66+
if (isDate(val)) {
6467
if (defaults.parseInputDatesAsUTC) {
6568
return dateToStringUTC(val)
6669
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const helper = require('../test-helper')
4+
const assert = require('assert')
5+
const vm = require('vm')
6+
7+
const suite = new helper.Suite()
8+
9+
suite.testAsync('Handle date objects as Date', async () => {
10+
const crossRealmDate = await vm.runInNewContext('new Date()')
11+
assert(!(crossRealmDate instanceof Date))
12+
const date = new Date(crossRealmDate.getTime())
13+
const client = new helper.pg.Client()
14+
await client.connect()
15+
16+
await client.query('CREATE TEMP TABLE foo(bar timestamptz, bar2 timestamptz)')
17+
await client.query('INSERT INTO foo(bar, bar2) VALUES($1, $2)', [date, crossRealmDate])
18+
const results = await client.query('SELECT * FROM foo')
19+
const row = results.rows[0]
20+
assert.deepStrictEqual(row.bar, date)
21+
assert.deepStrictEqual(row.bar2, date)
22+
await client.end()
23+
})

0 commit comments

Comments
 (0)