Skip to content

Commit ea83bf5

Browse files
author
Thomas Reggi
authored
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
1 parent c18ba71 commit ea83bf5

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Starting with Angular 6, Angular CLI removed the shim for `global` and other nod
193193
Parse an Extended JSON string, constructing the JavaScript value or object described by that
194194
string.
195195

196-
**Example**
196+
**Example**
197197
```js
198198
const { EJSON } = require('bson');
199199
const text = '{ "int32": { "$numberInt": "10" } }';
@@ -221,7 +221,7 @@ Converts a BSON document to an Extended JSON string, optionally replacing values
221221
function is specified or optionally including only the specified properties if a replacer array
222222
is specified.
223223

224-
**Example**
224+
**Example**
225225
```js
226226
const { EJSON } = require('bson');
227227
const Int32 = require('mongodb').Int32;
@@ -278,7 +278,7 @@ Sets the size of the internal serialization buffer.
278278

279279
Serialize a Javascript object.
280280

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

284284
### serializeWithBufferAndIndex(object, buffer)
@@ -294,7 +294,7 @@ Serialize a Javascript object.
294294

295295
Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
296296

297-
**Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
297+
**Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
298298
<a name="deserialize"></a>
299299

300300
### deserialize(buffer)
@@ -304,7 +304,6 @@ Serialize a Javascript object using a predefined Buffer and index into the buffe
304304
| buffer | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
305305
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
306306
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
307-
| [options.cacheFunctionsCrc32] | <code>Object</code> | <code>false</code> | use a crc32 code for caching, otherwise use the string of the function. |
308307
| [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 |
309308
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
310309
| [options.promoteValues] | <code>Object</code> | <code>false</code> | 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
314313

315314
Deserialize data as BSON.
316315

317-
**Returns**: <code>Object</code> - returns the deserialized Javascript Object.
316+
**Returns**: <code>Object</code> - returns the deserialized Javascript Object.
318317
<a name="calculateObjectSize"></a>
319318

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

328327
Calculate the bson size for a passed in Javascript object.
329328

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

333332
### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])
@@ -342,7 +341,6 @@ Calculate the bson size for a passed in Javascript object.
342341
| [options] | <code>Object</code> | | additional options used for the deserialization. |
343342
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
344343
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
345-
| [options.cacheFunctionsCrc32] | <code>Object</code> | <code>false</code> | use a crc32 code for caching, otherwise use the string of the function. |
346344
| [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 |
347345
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
348346
| [options.promoteValues] | <code>Object</code> | <code>false</code> | 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.
351349

352350
Deserialize stream data as BSON documents.
353351

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

356354
## FAQ
357355

src/parser/deserializer.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export interface DeserializeOptions {
2121
evalFunctions?: boolean;
2222
/** cache evaluated functions for reuse. */
2323
cacheFunctions?: boolean;
24-
/** use a crc32 code for caching, otherwise use the string of the function. */
24+
/**
25+
* use a crc32 code for caching, otherwise use the string of the function.
26+
* @deprecated this option to use the crc32 function never worked as intended
27+
* due to the fact that the crc32 function itself was never implemented.
28+
* */
2529
cacheFunctionsCrc32?: boolean;
2630
/** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */
2731
promoteLongs?: boolean;
@@ -98,15 +102,6 @@ function deserializeObject(
98102
) {
99103
const evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
100104
const cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
101-
const cacheFunctionsCrc32 =
102-
options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
103-
104-
let crc32;
105-
if (!cacheFunctionsCrc32) {
106-
crc32 = null;
107-
} else {
108-
crc32 = (v: string) => v; // FIXME(NODE-2770): This is a bug, hashing function is missing.
109-
}
110105

111106
const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
112107

@@ -499,9 +494,8 @@ function deserializeObject(
499494
if (evalFunctions) {
500495
// If we have cache enabled let's look for the md5 of the function in the cache
501496
if (cacheFunctions) {
502-
const hash = cacheFunctionsCrc32 && crc32 ? crc32(functionString) : functionString;
503497
// Got to do this to avoid V8 deoptimizing the call due to finding eval
504-
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
498+
object[name] = isolateEval(functionString, functionCache, object);
505499
} else {
506500
object[name] = isolateEval(functionString);
507501
}
@@ -568,9 +562,8 @@ function deserializeObject(
568562
if (evalFunctions) {
569563
// If we have cache enabled let's look for the md5 of the function in the cache
570564
if (cacheFunctions) {
571-
const hash = cacheFunctionsCrc32 && crc32 ? crc32(functionString) : functionString;
572565
// Got to do this to avoid V8 deoptimizing the call due to finding eval
573-
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
566+
object[name] = isolateEval(functionString, functionCache, object);
574567
} else {
575568
object[name] = isolateEval(functionString);
576569
}
@@ -654,26 +647,17 @@ function deserializeObject(
654647
*
655648
* @internal
656649
*/
657-
function isolateEvalWithHash(
658-
functionCache: { [hash: string]: Function },
659-
hash: string,
650+
function isolateEval(
660651
functionString: string,
661-
object: Document
652+
functionCache?: { [hash: string]: Function },
653+
object?: Document
662654
) {
655+
if (!functionCache) return new Function(functionString);
663656
// Check for cache hit, eval if missing and return cached function
664-
if (functionCache[hash] == null) {
665-
functionCache[hash] = new Function(functionString);
657+
if (functionCache[functionString] == null) {
658+
functionCache[functionString] = new Function(functionString);
666659
}
667660

668661
// Set the object
669-
return functionCache[hash].bind(object);
670-
}
671-
672-
/**
673-
* Ensure eval is isolated.
674-
*
675-
* @internal
676-
*/
677-
function isolateEval(functionString: string): Function {
678-
return new Function(functionString);
662+
return functionCache[functionString].bind(object);
679663
}

0 commit comments

Comments
 (0)