Skip to content

Commit 5e768fd

Browse files
committed
Fix escaping Date objects from foreign isolates
fixes #67
1 parent 2ab4901 commit 5e768fd

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix escaping `Date` objects from foreign isolates
5+
16
2.3.2 / 2020-04-15
27
==================
38

lib/SqlString.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
4040
case 'boolean': return (val) ? 'true' : 'false';
4141
case 'number': return val + '';
4242
case 'object':
43-
if (val instanceof Date) {
43+
if (Object.prototype.toString.call(val) === '[object Date]') {
4444
return SqlString.dateToString(val, timeZone || 'local');
4545
} else if (Array.isArray(val)) {
4646
return SqlString.arrayToList(val, timeZone);

test/unit/test-SqlString.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var assert = require('assert');
22
var SqlString = require('../../');
33
var test = require('utest');
4+
var vm = require('vm');
45

56
test('SqlString.escapeId', {
67
'value is quoted': function() {
@@ -222,6 +223,14 @@ test('SqlString.escape', {
222223
assert.strictEqual(string, 'NULL');
223224
},
224225

226+
'dates from other isolates are converted': function() {
227+
var expected = '2012-05-07 11:42:03.002';
228+
var date = vm.runInNewContext('new Date(2012, 4, 7, 11, 42, 3, 2)');
229+
var string = SqlString.escape(date);
230+
231+
assert.strictEqual(string, "'" + expected + "'");
232+
},
233+
225234
'buffers are converted to hex': function() {
226235
var buffer = new Buffer([0, 1, 254, 255]);
227236
var string = SqlString.escape(buffer);

0 commit comments

Comments
 (0)