Skip to content

Commit 1320c10

Browse files
committed
fix(serialization): normalize function stringification
A non-default option for serialization allows for the automatically stringifying javascript functions. Function.prototype.toString has changed between versions of node, so we need to normalize so as to not potentially break compatibility with earlier expectations. This should have marginal performance effects only in the case where users are actually using this functionality. NODE-1499
1 parent 20f764c commit 1320c10

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/bson/parser/calculate_size.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var Long = require('../long').Long,
1313
DBRef = require('../db_ref').DBRef,
1414
Binary = require('../binary').Binary;
1515

16+
var normalizedFunctionString = require('./utils').normalizedFunctionString;
17+
1618
// To ensure that 0.4 of node works correctly
1719
var isDate = function isDate(d) {
1820
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
@@ -221,7 +223,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
221223
1 +
222224
4 +
223225
4 +
224-
Buffer.byteLength(value.toString(), 'utf8') +
226+
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
225227
1 +
226228
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
227229
);
@@ -230,7 +232,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
230232
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
231233
1 +
232234
4 +
233-
Buffer.byteLength(value.toString(), 'utf8') +
235+
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
234236
1
235237
);
236238
}

lib/bson/parser/serializer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var writeIEEE754 = require('../float_parser').writeIEEE754,
66
Map = require('../map'),
77
Binary = require('../binary').Binary;
88

9+
const normalizedFunctionString = require('./utils').normalizedFunctionString;
10+
911
// try {
1012
// var _Buffer = Uint8Array;
1113
// } catch (e) {
@@ -443,7 +445,8 @@ var serializeFunction = function(buffer, key, value, index, checkKeys, depth, is
443445
index = index + numberOfWrittenBytes;
444446
buffer[index++] = 0;
445447
// Function string
446-
var functionString = value.toString();
448+
var functionString = normalizedFunctionString(value);
449+
447450
// Write the string
448451
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
449452
// Write the size of the string to buffer

lib/bson/parser/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
/**
4+
* Normalizes our expected stringified form of a function across versions of node
5+
* @param {Function} fn The function to stringify
6+
*/
7+
function normalizedFunctionString(fn) {
8+
return fn.toString().replace('function(', 'function (');
9+
}
10+
11+
module.exports = {
12+
normalizedFunctionString
13+
};

0 commit comments

Comments
 (0)