File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11'use strict'
22
33const defaults = require ( './defaults' )
4+ const nodeUtils = require ( 'util' )
45
56const { 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+
714function 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 {
Original file line number Diff line number Diff 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+
92109test ( 'prepareValues: undefined prepared properly' , function ( ) {
93110 const out = utils . prepareValue ( void 0 )
94111 assert . strictEqual ( out , null )
You can’t perform that action at this time.
0 commit comments