Skip to content

Commit 21eb0b0

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 e311056 commit 21eb0b0

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/bson/parser/calculate_size.js

+4-2
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+
const 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]';
@@ -224,7 +226,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
224226
1 +
225227
4 +
226228
4 +
227-
Buffer.byteLength(value.toString(), 'utf8') +
229+
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
228230
1 +
229231
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
230232
);
@@ -233,7 +235,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
233235
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
234236
1 +
235237
4 +
236-
Buffer.byteLength(value.toString(), 'utf8') +
238+
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
237239
1
238240
);
239241
}

lib/bson/parser/serializer.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var writeIEEE754 = require('../float_parser').writeIEEE754,
66
MinKey = require('../min_key').MinKey,
77
Binary = require('../binary').Binary;
88

9+
const normalizedFunctionString = require('./utils').normalizedFunctionString;
10+
911
// try {
1012
// var _Buffer = Uint8Array;
1113
// } catch (e) {
@@ -450,7 +452,8 @@ var serializeFunction = function(buffer, key, value, index, checkKeys, depth, is
450452
index = index + numberOfWrittenBytes;
451453
buffer[index++] = 0;
452454
// Function string
453-
var functionString = value.toString();
455+
var functionString = normalizedFunctionString(value);
456+
454457
// Write the string
455458
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
456459
// Write the size of the string to buffer

lib/bson/parser/utils.js

+13
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)