From ea83bf5200f4a936692f710063941ba802386da4 Mon Sep 17 00:00:00 2001 From: Thomas Reggi Date: Fri, 18 Sep 2020 12:57:37 -0400 Subject: [PATCH] fix: deprecate cacheFunctionsCrc32 The `crc32` function was never implemented, as a result this property never worked as intended. Deprecates the `cacheFunctionsCrc32` property from deserialize options, and removes it from documentation. NODE-2770 --- README.md | 16 ++++++-------- src/parser/deserializer.ts | 44 ++++++++++++-------------------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index f7115cf3..cd7242fd 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ Starting with Angular 6, Angular CLI removed the shim for `global` and other nod Parse an Extended JSON string, constructing the JavaScript value or object described by that string. -**Example** +**Example** ```js const { EJSON } = require('bson'); const text = '{ "int32": { "$numberInt": "10" } }'; @@ -221,7 +221,7 @@ Converts a BSON document to an Extended JSON string, optionally replacing values function is specified or optionally including only the specified properties if a replacer array is specified. -**Example** +**Example** ```js const { EJSON } = require('bson'); const Int32 = require('mongodb').Int32; @@ -278,7 +278,7 @@ Sets the size of the internal serialization buffer. Serialize a Javascript object. -**Returns**: Buffer - returns the Buffer object containing the serialized object. +**Returns**: Buffer - returns the Buffer object containing the serialized object. ### serializeWithBufferAndIndex(object, buffer) @@ -294,7 +294,7 @@ Serialize a Javascript object. Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization. -**Returns**: Number - returns the index pointing to the last written byte in the buffer. +**Returns**: Number - returns the index pointing to the last written byte in the buffer. ### deserialize(buffer) @@ -304,7 +304,6 @@ Serialize a Javascript object using a predefined Buffer and index into the buffe | buffer | Buffer | | the buffer containing the serialized set of BSON documents. | | [options.evalFunctions] | Object | false | evaluate functions in the BSON document scoped to the object deserialized. | | [options.cacheFunctions] | Object | false | cache evaluated functions for reuse. | -| [options.cacheFunctionsCrc32] | Object | false | use a crc32 code for caching, otherwise use the string of the function. | | [options.promoteLongs] | Object | true | when deserializing a Long will fit it into a Number if it's smaller than 53 bits | | [options.promoteBuffers] | Object | false | when deserializing a Binary will return it as a node.js Buffer instance. | | [options.promoteValues] | Object | false | when deserializing will promote BSON values to their Node.js closest equivalent types. | @@ -314,7 +313,7 @@ Serialize a Javascript object using a predefined Buffer and index into the buffe Deserialize data as BSON. -**Returns**: Object - returns the deserialized Javascript Object. +**Returns**: Object - returns the deserialized Javascript Object. ### calculateObjectSize(object) @@ -327,7 +326,7 @@ Deserialize data as BSON. Calculate the bson size for a passed in Javascript object. -**Returns**: Number - returns the number of bytes the BSON object will take up. +**Returns**: Number - returns the number of bytes the BSON object will take up. ### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options]) @@ -342,7 +341,6 @@ Calculate the bson size for a passed in Javascript object. | [options] | Object | | additional options used for the deserialization. | | [options.evalFunctions] | Object | false | evaluate functions in the BSON document scoped to the object deserialized. | | [options.cacheFunctions] | Object | false | cache evaluated functions for reuse. | -| [options.cacheFunctionsCrc32] | Object | false | use a crc32 code for caching, otherwise use the string of the function. | | [options.promoteLongs] | Object | true | when deserializing a Long will fit it into a Number if it's smaller than 53 bits | | [options.promoteBuffers] | Object | false | when deserializing a Binary will return it as a node.js Buffer instance. | | [options.promoteValues] | Object | false | when deserializing will promote BSON values to their Node.js closest equivalent types. | @@ -351,7 +349,7 @@ Calculate the bson size for a passed in Javascript object. Deserialize stream data as BSON documents. -**Returns**: Number - returns the next index in the buffer after deserialization **x** numbers of documents. +**Returns**: Number - returns the next index in the buffer after deserialization **x** numbers of documents. ## FAQ diff --git a/src/parser/deserializer.ts b/src/parser/deserializer.ts index 6bbabba1..d9b145e3 100644 --- a/src/parser/deserializer.ts +++ b/src/parser/deserializer.ts @@ -21,7 +21,11 @@ export interface DeserializeOptions { evalFunctions?: boolean; /** cache evaluated functions for reuse. */ cacheFunctions?: boolean; - /** use a crc32 code for caching, otherwise use the string of the function. */ + /** + * use a crc32 code for caching, otherwise use the string of the function. + * @deprecated this option to use the crc32 function never worked as intended + * due to the fact that the crc32 function itself was never implemented. + * */ cacheFunctionsCrc32?: boolean; /** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */ promoteLongs?: boolean; @@ -98,15 +102,6 @@ function deserializeObject( ) { const evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions']; const cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions']; - const cacheFunctionsCrc32 = - options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32']; - - let crc32; - if (!cacheFunctionsCrc32) { - crc32 = null; - } else { - crc32 = (v: string) => v; // FIXME(NODE-2770): This is a bug, hashing function is missing. - } const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw']; @@ -499,9 +494,8 @@ function deserializeObject( if (evalFunctions) { // If we have cache enabled let's look for the md5 of the function in the cache if (cacheFunctions) { - const hash = cacheFunctionsCrc32 && crc32 ? crc32(functionString) : functionString; // Got to do this to avoid V8 deoptimizing the call due to finding eval - object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + object[name] = isolateEval(functionString, functionCache, object); } else { object[name] = isolateEval(functionString); } @@ -568,9 +562,8 @@ function deserializeObject( if (evalFunctions) { // If we have cache enabled let's look for the md5 of the function in the cache if (cacheFunctions) { - const hash = cacheFunctionsCrc32 && crc32 ? crc32(functionString) : functionString; // Got to do this to avoid V8 deoptimizing the call due to finding eval - object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + object[name] = isolateEval(functionString, functionCache, object); } else { object[name] = isolateEval(functionString); } @@ -654,26 +647,17 @@ function deserializeObject( * * @internal */ -function isolateEvalWithHash( - functionCache: { [hash: string]: Function }, - hash: string, +function isolateEval( functionString: string, - object: Document + functionCache?: { [hash: string]: Function }, + object?: Document ) { + if (!functionCache) return new Function(functionString); // Check for cache hit, eval if missing and return cached function - if (functionCache[hash] == null) { - functionCache[hash] = new Function(functionString); + if (functionCache[functionString] == null) { + functionCache[functionString] = new Function(functionString); } // Set the object - return functionCache[hash].bind(object); -} - -/** - * Ensure eval is isolated. - * - * @internal - */ -function isolateEval(functionString: string): Function { - return new Function(functionString); + return functionCache[functionString].bind(object); }