Skip to content

Commit 3be8dc0

Browse files
committed
Merge branch 'NODE-4711-rm-eval' of github.com:mongodb/js-bson into NODE-4711-rm-eval
2 parents 2562947 + 390642e commit 3be8dc0

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

docs/upgrade-to-v5.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,30 @@ new Timestamp({ t: 2, i: 0xFFFF_FFFF + 1 });
166166
// Will throw, both fields need to be less than or equal to the unsigned int32 max value
167167
```
168168

169-
### `Code` only stores a string
169+
### Extended JSON `strict` flag removed
170170

171-
The `Code` class only stores stringified javascript.
172-
It can still be constructed from a javascript function.
171+
Extended JSON `parse` and `stringify` APIs no longer support the `strict` option, please use the `relaxed` option instead.
172+
173+
**Note** that the `relaxed` setting is the inverse of `strict`. See the following migration example:
174+
175+
```typescript
176+
// parse
177+
EJSON.parse("...", { strict: true }); /* migrate to */ EJSON.parse("...", { relaxed: false });
178+
EJSON.parse("...", { strict: false }); /* migrate to */ EJSON.parse("...", { relaxed: true });
179+
// stringify
180+
EJSON.stringify({}, { strict: true }); /* migrate to */ EJSON.stringify({}, { relaxed: false });
181+
EJSON.stringify({}, { strict: false }); /* migrate to */ EJSON.stringify({}, { relaxed: true });
182+
```
183+
184+
### `class Code` always converts `.code` to string
185+
186+
The `Code` class still supports the same constructor arguments as before.
187+
It will now convert the first argument to a string before saving it to the code property, see the following:
173188

174189
```typescript
175190
const myCode = new Code(function iLoveJavascript() { console.log('I love javascript') });
176191
// myCode.code === "function iLoveJavascript() { console.log('I love javascript') }"
192+
// typeof myCode.code === 'string'
177193
```
178194

179195
### `BSON.deserialize()` only returns `Code` instances
@@ -194,3 +210,4 @@ const iLoveJavascript = new Function(`return ${result.iLoveJavascript.code}`)();
194210
iLoveJavascript();
195211
// prints "I love javascript"
196212
// iLoveJavascript.name === "iLoveJavascript"
213+
```

src/extended_json.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@ export namespace EJSON {
359359
legacy?: boolean;
360360
/** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
361361
relaxed?: boolean;
362-
/**
363-
* Disable Extended JSON's `relaxed` mode, which attempts to return BSON types where possible, rather than native JS types
364-
* @deprecated Please use the relaxed property instead
365-
*/
366-
strict?: boolean;
367362
}
368363

369364
/**
@@ -383,19 +378,13 @@ export namespace EJSON {
383378
* ```
384379
*/
385380
export function parse(text: string, options?: EJSON.Options): SerializableTypes {
386-
const finalOptions = Object.assign({}, { relaxed: true, legacy: false }, options);
387-
388-
// relaxed implies not strict
389-
if (typeof finalOptions.relaxed === 'boolean') finalOptions.strict = !finalOptions.relaxed;
390-
if (typeof finalOptions.strict === 'boolean') finalOptions.relaxed = !finalOptions.strict;
391-
392381
return JSON.parse(text, (key, value) => {
393382
if (key.indexOf('\x00') !== -1) {
394383
throw new BSONError(
395384
`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`
396385
);
397386
}
398-
return deserializeValue(value, finalOptions);
387+
return deserializeValue(value, { relaxed: true, legacy: false, ...options });
399388
});
400389
}
401390

test/node/extended_json.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ describe('Extended JSON', function () {
8686
expect(json).to.equal(EJSON.stringify(doc, null, 0, { relaxed: false }));
8787
});
8888

89-
it('should correctly deserialize using the default relaxed mode', function () {
90-
// Deserialize the document using non strict mode
89+
it('should correctly deserialize using the default relaxed mode (relaxed=true)', function () {
90+
// Deserialize the document using relaxed=true mode
9191
let doc1 = EJSON.parse(EJSON.stringify(doc, null, 0));
9292

9393
// Validate the values
@@ -96,7 +96,7 @@ describe('Extended JSON', function () {
9696
expect(0x19000000000000).to.equal(doc1.longNumberIntFit);
9797
expect(19007199250000000).to.equal(doc1.doubleNumberIntFit);
9898

99-
// Deserialize the document using strict mode
99+
// Deserialize the document using relaxed=false
100100
doc1 = EJSON.parse(EJSON.stringify(doc, null, 0), { relaxed: false });
101101

102102
// Validate the values
@@ -117,9 +117,10 @@ describe('Extended JSON', function () {
117117
const text = EJSON.stringify(doc1, null, 0, { relaxed: false });
118118
expect(text).to.equal('{"int32":{"$numberInt":"10"}}');
119119

120-
// Deserialize the json in strict and non strict mode
120+
// Deserialize the json in relaxed=false mode
121121
let doc2 = EJSON.parse(text, { relaxed: false });
122122
expect(doc2.int32._bsontype).to.equal('Int32');
123+
// Deserialize the json in relaxed=true mode
123124
doc2 = EJSON.parse(text);
124125
expect(doc2.int32).to.equal(10);
125126
});
@@ -589,7 +590,7 @@ Converting circular structure to EJSON:
589590
});
590591

591592
context('when serializing date', function () {
592-
context('when using strict mode', function () {
593+
context('when using relaxed=false mode', function () {
593594
it('stringifies $date with with ISO-8601 string', function () {
594595
const date = new Date(1452124800000);
595596
const doc = { field: date };
@@ -667,7 +668,7 @@ Converting circular structure to EJSON:
667668
});
668669

669670
context('when deserializing date', function () {
670-
context('when using strict mode', function () {
671+
context('when using relaxed=false mode', function () {
671672
it('parses $date with with ISO-8601 string', function () {
672673
const date = new Date(1452124800000);
673674
const doc = { field: date };
@@ -679,7 +680,7 @@ Converting circular structure to EJSON:
679680
});
680681
});
681682

682-
context('when using relaxed mode', function () {
683+
context('when using relaxed=true mode', function () {
683684
it('parses $date number with millis since epoch', function () {
684685
const date = new Date(1452124800000);
685686
const doc = { field: date };

0 commit comments

Comments
 (0)