Skip to content

Commit

Permalink
Merge branch 'NODE-4711-rm-eval' of github.com:mongodb/js-bson into N…
Browse files Browse the repository at this point in the history
…ODE-4711-rm-eval
  • Loading branch information
nbbeeken committed Dec 9, 2022
2 parents 2562947 + 390642e commit 3be8dc0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
23 changes: 20 additions & 3 deletions docs/upgrade-to-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,30 @@ new Timestamp({ t: 2, i: 0xFFFF_FFFF + 1 });
// Will throw, both fields need to be less than or equal to the unsigned int32 max value
```

### `Code` only stores a string
### Extended JSON `strict` flag removed

The `Code` class only stores stringified javascript.
It can still be constructed from a javascript function.
Extended JSON `parse` and `stringify` APIs no longer support the `strict` option, please use the `relaxed` option instead.

**Note** that the `relaxed` setting is the inverse of `strict`. See the following migration example:

```typescript
// parse
EJSON.parse("...", { strict: true }); /* migrate to */ EJSON.parse("...", { relaxed: false });
EJSON.parse("...", { strict: false }); /* migrate to */ EJSON.parse("...", { relaxed: true });
// stringify
EJSON.stringify({}, { strict: true }); /* migrate to */ EJSON.stringify({}, { relaxed: false });
EJSON.stringify({}, { strict: false }); /* migrate to */ EJSON.stringify({}, { relaxed: true });
```

### `class Code` always converts `.code` to string

The `Code` class still supports the same constructor arguments as before.
It will now convert the first argument to a string before saving it to the code property, see the following:

```typescript
const myCode = new Code(function iLoveJavascript() { console.log('I love javascript') });
// myCode.code === "function iLoveJavascript() { console.log('I love javascript') }"
// typeof myCode.code === 'string'
```

### `BSON.deserialize()` only returns `Code` instances
Expand All @@ -194,3 +210,4 @@ const iLoveJavascript = new Function(`return ${result.iLoveJavascript.code}`)();
iLoveJavascript();
// prints "I love javascript"
// iLoveJavascript.name === "iLoveJavascript"
```
13 changes: 1 addition & 12 deletions src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,6 @@ export namespace EJSON {
legacy?: boolean;
/** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
relaxed?: boolean;
/**
* Disable Extended JSON's `relaxed` mode, which attempts to return BSON types where possible, rather than native JS types
* @deprecated Please use the relaxed property instead
*/
strict?: boolean;
}

/**
Expand All @@ -383,19 +378,13 @@ export namespace EJSON {
* ```
*/
export function parse(text: string, options?: EJSON.Options): SerializableTypes {
const finalOptions = Object.assign({}, { relaxed: true, legacy: false }, options);

// relaxed implies not strict
if (typeof finalOptions.relaxed === 'boolean') finalOptions.strict = !finalOptions.relaxed;
if (typeof finalOptions.strict === 'boolean') finalOptions.relaxed = !finalOptions.strict;

return JSON.parse(text, (key, value) => {
if (key.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`
);
}
return deserializeValue(value, finalOptions);
return deserializeValue(value, { relaxed: true, legacy: false, ...options });
});
}

Expand Down
15 changes: 8 additions & 7 deletions test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ describe('Extended JSON', function () {
expect(json).to.equal(EJSON.stringify(doc, null, 0, { relaxed: false }));
});

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

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

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

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

// Deserialize the json in strict and non strict mode
// Deserialize the json in relaxed=false mode
let doc2 = EJSON.parse(text, { relaxed: false });
expect(doc2.int32._bsontype).to.equal('Int32');
// Deserialize the json in relaxed=true mode
doc2 = EJSON.parse(text);
expect(doc2.int32).to.equal(10);
});
Expand Down Expand Up @@ -589,7 +590,7 @@ Converting circular structure to EJSON:
});

context('when serializing date', function () {
context('when using strict mode', function () {
context('when using relaxed=false mode', function () {
it('stringifies $date with with ISO-8601 string', function () {
const date = new Date(1452124800000);
const doc = { field: date };
Expand Down Expand Up @@ -667,7 +668,7 @@ Converting circular structure to EJSON:
});

context('when deserializing date', function () {
context('when using strict mode', function () {
context('when using relaxed=false mode', function () {
it('parses $date with with ISO-8601 string', function () {
const date = new Date(1452124800000);
const doc = { field: date };
Expand All @@ -679,7 +680,7 @@ Converting circular structure to EJSON:
});
});

context('when using relaxed mode', function () {
context('when using relaxed=true mode', function () {
it('parses $date number with millis since epoch', function () {
const date = new Date(1452124800000);
const doc = { field: date };
Expand Down

0 comments on commit 3be8dc0

Please sign in to comment.