Skip to content

Commit

Permalink
fix(serialization): normalize function stringification
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst committed Jun 6, 2018
1 parent e311056 commit 21eb0b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/bson/parser/calculate_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var Long = require('../long').Long,
DBRef = require('../db_ref').DBRef,
Binary = require('../binary').Binary;

const normalizedFunctionString = require('./utils').normalizedFunctionString;

// To ensure that 0.4 of node works correctly
var isDate = function isDate(d) {
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
Expand Down Expand Up @@ -224,7 +226,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
1 +
4 +
4 +
Buffer.byteLength(value.toString(), 'utf8') +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
Expand All @@ -233,7 +235,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(value.toString(), 'utf8') +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1
);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/bson/parser/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var writeIEEE754 = require('../float_parser').writeIEEE754,
MinKey = require('../min_key').MinKey,
Binary = require('../binary').Binary;

const normalizedFunctionString = require('./utils').normalizedFunctionString;

// try {
// var _Buffer = Uint8Array;
// } catch (e) {
Expand Down Expand Up @@ -450,7 +452,8 @@ var serializeFunction = function(buffer, key, value, index, checkKeys, depth, is
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
var functionString = value.toString();
var functionString = normalizedFunctionString(value);

// Write the string
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
// Write the size of the string to buffer
Expand Down
13 changes: 13 additions & 0 deletions lib/bson/parser/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

/**
* Normalizes our expected stringified form of a function across versions of node
* @param {Function} fn The function to stringify
*/
function normalizedFunctionString(fn) {
return fn.toString().replace('function(', 'function (');
}

module.exports = {
normalizedFunctionString
};

0 comments on commit 21eb0b0

Please sign in to comment.