Skip to content

Commit

Permalink
fix: deprecate cacheFunctionsCrc32
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Thomas Reggi authored Sep 18, 2020
1 parent c18ba71 commit ea83bf5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -278,7 +278,7 @@ Sets the size of the internal serialization buffer.

Serialize a Javascript object.

**Returns**: <code>Buffer</code> - returns the Buffer object containing the serialized object.
**Returns**: <code>Buffer</code> - returns the Buffer object containing the serialized object.
<a name="serializeWithBufferAndIndex"></a>

### serializeWithBufferAndIndex(object, buffer)
Expand All @@ -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**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
**Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
<a name="deserialize"></a>

### deserialize(buffer)
Expand All @@ -304,7 +304,6 @@ Serialize a Javascript object using a predefined Buffer and index into the buffe
| buffer | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
| [options.cacheFunctionsCrc32] | <code>Object</code> | <code>false</code> | use a crc32 code for caching, otherwise use the string of the function. |
| [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
Expand All @@ -314,7 +313,7 @@ Serialize a Javascript object using a predefined Buffer and index into the buffe

Deserialize data as BSON.

**Returns**: <code>Object</code> - returns the deserialized Javascript Object.
**Returns**: <code>Object</code> - returns the deserialized Javascript Object.
<a name="calculateObjectSize"></a>

### calculateObjectSize(object)
Expand All @@ -327,7 +326,7 @@ Deserialize data as BSON.

Calculate the bson size for a passed in Javascript object.

**Returns**: <code>Number</code> - returns the number of bytes the BSON object will take up.
**Returns**: <code>Number</code> - returns the number of bytes the BSON object will take up.
<a name="deserializeStream"></a>

### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])
Expand All @@ -342,7 +341,6 @@ Calculate the bson size for a passed in Javascript object.
| [options] | <code>Object</code> | | additional options used for the deserialization. |
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
| [options.cacheFunctionsCrc32] | <code>Object</code> | <code>false</code> | use a crc32 code for caching, otherwise use the string of the function. |
| [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
Expand All @@ -351,7 +349,7 @@ Calculate the bson size for a passed in Javascript object.

Deserialize stream data as BSON documents.

**Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.
**Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.

## FAQ

Expand Down
44 changes: 14 additions & 30 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

0 comments on commit ea83bf5

Please sign in to comment.