From 71026a1ed8ea832efd7a5799f90fed3d1a7b2e68 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 19 Oct 2023 14:43:41 +0200 Subject: [PATCH 1/4] feat: bump to BSON 6.2.0, make string quoting consistent MONGOSH-1452 --- package-lock.json | 32 +++---- packages/browser-runtime-core/package.json | 2 +- packages/cli-repl/src/format-output.ts | 2 +- packages/e2e-tests/test/e2e-bson.spec.ts | 87 +++++++++++-------- .../node-runtime-worker-thread/package.json | 2 +- packages/service-provider-core/package.json | 2 +- .../src/printable-bson.spec.ts | 26 +++--- .../src/printable-bson.ts | 27 ++---- packages/shell-api/package.json | 2 +- packages/snippet-manager/package.json | 2 +- 10 files changed, 96 insertions(+), 88 deletions(-) diff --git a/package-lock.json b/package-lock.json index a5eddbc42..8aad209bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12269,9 +12269,9 @@ } }, "node_modules/bson": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/bson/-/bson-6.1.0.tgz", - "integrity": "sha512-yiQ3KxvpVoRpx1oD1uPz4Jit9tAVTJgjdmjDKtUErkOoL9VNoF8Dd58qtAOL5E40exx2jvAT9sqdRSK/r+SHlA==", + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.2.0.tgz", + "integrity": "sha512-ID1cI+7bazPDyL9wYy9GaQ8gEEohWvcUl/Yf0dIdutJxnmInEEyCsb4awy/OiBfall7zBA179Pahi3vCdFze3Q==", "engines": { "node": ">=16.20.1" } @@ -30945,7 +30945,7 @@ "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/tsconfig-mongosh": "^1.0.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "prettier": "^2.8.8", @@ -31423,7 +31423,7 @@ "@mongosh/service-provider-core": "0.0.0-dev.0", "@mongosh/service-provider-server": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mocha": "^10.2.0", @@ -31442,7 +31442,7 @@ "dependencies": { "@aws-sdk/credential-providers": "^3.347.1", "@mongosh/errors": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "mongodb": "^6.0.0", "mongodb-build-info": "^1.6.2" }, @@ -31510,7 +31510,7 @@ "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/tsconfig-mongosh": "^1.0.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mongodb": "^6.0.0", @@ -31551,7 +31551,7 @@ "@mongosh/errors": "0.0.0-dev.0", "@mongosh/shell-api": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "cross-spawn": "^7.0.3", "escape-string-regexp": "^4.0.0", "joi": "^17.4.0", @@ -37872,7 +37872,7 @@ "@mongosh/shell-api": "0.0.0-dev.0", "@mongosh/shell-evaluator": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "prettier": "^2.8.8", @@ -38223,7 +38223,7 @@ "@mongosh/service-provider-core": "0.0.0-dev.0", "@mongosh/service-provider-server": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "interruptor": "^1.0.1", @@ -38242,7 +38242,7 @@ "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/tsconfig-mongosh": "^1.0.0", "@mongosh/errors": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mongodb": "^6.0.0", @@ -38286,7 +38286,7 @@ "@mongosh/i18n": "0.0.0-dev.0", "@mongosh/service-provider-core": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mongodb": "^6.0.0", @@ -38322,7 +38322,7 @@ "@types/cross-spawn": "^6.0.2", "@types/node-fetch": "^2.6.4", "@types/tar": "^4.0.4", - "bson": "^6.1.0", + "bson": "^6.2.0", "cross-spawn": "^7.0.3", "depcheck": "^1.4.3", "escape-string-regexp": "^4.0.0", @@ -42047,9 +42047,9 @@ } }, "bson": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/bson/-/bson-6.1.0.tgz", - "integrity": "sha512-yiQ3KxvpVoRpx1oD1uPz4Jit9tAVTJgjdmjDKtUErkOoL9VNoF8Dd58qtAOL5E40exx2jvAT9sqdRSK/r+SHlA==" + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.2.0.tgz", + "integrity": "sha512-ID1cI+7bazPDyL9wYy9GaQ8gEEohWvcUl/Yf0dIdutJxnmInEEyCsb4awy/OiBfall7zBA179Pahi3vCdFze3Q==" }, "buffer": { "version": "4.9.2", diff --git a/packages/browser-runtime-core/package.json b/packages/browser-runtime-core/package.json index 25a338df8..184afcdc6 100644 --- a/packages/browser-runtime-core/package.json +++ b/packages/browser-runtime-core/package.json @@ -42,7 +42,7 @@ "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/tsconfig-mongosh": "^1.0.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "prettier": "^2.8.8", diff --git a/packages/cli-repl/src/format-output.ts b/packages/cli-repl/src/format-output.ts index 8f94e3c1e..a7f0543d3 100644 --- a/packages/cli-repl/src/format-output.ts +++ b/packages/cli-repl/src/format-output.ts @@ -344,7 +344,7 @@ function dateInspect(this: Date, depth: number, options: any): string { if (isNaN(this.valueOf())) { return options.stylize('Invalid Date', 'date'); } - return `ISODate("${this.toISOString()}")`; + return `ISODate('${this.toISOString()}')`; } function inspect(output: any, options: FormatOptions): any { diff --git a/packages/e2e-tests/test/e2e-bson.spec.ts b/packages/e2e-tests/test/e2e-bson.spec.ts index 904cf32da..30e4227e4 100644 --- a/packages/e2e-tests/test/e2e-bson.spec.ts +++ b/packages/e2e-tests/test/e2e-bson.spec.ts @@ -32,20 +32,21 @@ describe('BSON e2e', function () { afterEach(TestShell.cleanup); describe('printed BSON', function () { const outputDoc = { - ObjectId: 'ObjectId("5f16b8bebe434dc98cdfc9ca")', - DBRef1: 'DBRef("a", ObjectId("5f16b8bebe434dc98cdfc9cb"), \'db\')', - DBRef2: "DBRef(\"a\", '5f16b8bebe434dc98cdfc9cb', 'db')", - DBRef3: "DBRef(\"a\", { x: '5f16b8bebe434dc98cdfc9cb' }, 'db')", + ObjectId: "ObjectId('5f16b8bebe434dc98cdfc9ca')", + DBRef1: "DBRef('a', ObjectId('5f16b8bebe434dc98cdfc9cb'), 'db')", + DBRef2: "DBRef('a', '5f16b8bebe434dc98cdfc9cb', 'db')", + DBRef3: "DBRef('a', { x: '5f16b8bebe434dc98cdfc9cb' }, 'db')", MinKey: 'MinKey()', MaxKey: 'MaxKey()', NumberInt: 'Int32(32)', - NumberLong: 'Long("64")', + NumberLong: "Long('64')", Timestamp: 'Timestamp({ t: 100, i: 1 })', - Symbol: 'abc', - Code: 'Code("abc")', - NumberDecimal: 'Decimal128("1")', - BinData: 'Binary.createFromBase64("MTIzNA==", 128)', - ISODate: 'ISODate("2021-05-04T15:49:33.000Z")', + Symbol: "BSONSymbol('abc')", + SymbolRawValue: "'abc'", + Code: "Code('abc')", + NumberDecimal: "Decimal128('1')", + BinData: "Binary.createFromBase64('MTIzNA==', 128)", + ISODate: "ISODate('2021-05-04T15:49:33.000Z')", RegExp: '/match/', }; it('Entire doc prints when returned from the server', async function () { @@ -83,12 +84,16 @@ describe('BSON e2e', function () { expect(output).to.include(outputDoc.MinKey); expect(output).to.include(outputDoc.MaxKey); expect(output).to.include(outputDoc.Timestamp); - expect(output).to.include(outputDoc.Symbol); + expect(output).to.include(outputDoc.SymbolRawValue); expect(output).to.include(outputDoc.Code); expect(output).to.include(outputDoc.NumberDecimal); expect(output).to.include(outputDoc.BinData); expect(output).to.include(outputDoc.ISODate); expect(output).to.include(outputDoc.RegExp); + const unpromotedOutput = await shell.executeLine( + 'db.test.findOne({}, {}, { promoteValues: false })' + ); + expect(unpromotedOutput).to.include(outputDoc.Symbol); shell.assertNoErrors(); }); it('Entire doc prints when created by user', async function () { @@ -125,12 +130,22 @@ describe('BSON e2e', function () { expect(output).to.include(outputDoc.RegExp); shell.assertNoErrors(); }); + it('Entire doc equals itself when being re-evaluated', async function () { + const input = Object.entries(outputDoc) + .map(([key, value]) => `${key}: ${value}`) + .join(','); + const firstOutput = await shell.executeLine(`a = ({${input}})`); + const printedObject = firstOutput.match(/^\{[\s\S]+\}$/m)?.[0]; + await shell.executeLine(`b = ${printedObject}`); + await shell.executeLine(`assert.deepStrictEqual(a, b)`); + shell.assertNoErrors(); + }); it('ObjectId prints when returned from the server', async function () { const value = new bson.ObjectId('5f16b8bebe434dc98cdfc9ca'); await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'ObjectId("5f16b8bebe434dc98cdfc9ca")' + "ObjectId('5f16b8bebe434dc98cdfc9ca')" ); shell.assertNoErrors(); }); @@ -142,7 +157,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'DBRef("coll", ObjectId("5f16b8bebe434dc98cdfc9ca"))' + "DBRef('coll', ObjectId('5f16b8bebe434dc98cdfc9ca'))" ); shell.assertNoErrors(); }); @@ -169,7 +184,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'Long("64")' + "Long('64')" ); shell.assertNoErrors(); }); @@ -178,7 +193,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'Long("345678654321234561")' + "Long('345678654321234561')" ); shell.assertNoErrors(); }); @@ -196,7 +211,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'Code("abc")' + "Code('abc')" ); shell.assertNoErrors(); }); @@ -205,7 +220,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'Decimal128("1")' + "Decimal128('1')" ); shell.assertNoErrors(); }); @@ -215,7 +230,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'Binary.createFromBase64("MTIzNA==", 128)' + "Binary.createFromBase64('MTIzNA==', 128)" ); shell.assertNoErrors(); }); @@ -224,7 +239,7 @@ describe('BSON e2e', function () { await shell.executeLine(`use ${dbName}`); await db.collection('test').insertOne({ value: value }); expect(await shell.executeLine('db.test.findOne().value')).to.include( - 'ISODate("2021-05-04T15:49:33.000Z")' + "ISODate('2021-05-04T15:49:33.000Z')" ); shell.assertNoErrors(); }); @@ -245,16 +260,16 @@ describe('BSON e2e', function () { await shell.executeLine( 'db.test.findOne({}, {}, { bsonRegExp: true }).value' ) - ).to.include(String.raw`BSONRegExp("(?-i)A\"A_", "im")`); + ).to.include(String.raw`BSONRegExp('(?-i)A"A_', 'im')`); shell.assertNoErrors(); }); it('ObjectId prints when created by user', async function () { - const value = 'ObjectId("5f16b8bebe434dc98cdfc9ca")'; + const value = "ObjectId('5f16b8bebe434dc98cdfc9ca')"; expect(await shell.executeLine(value)).to.include(value); shell.assertNoErrors(); }); it('DBRef prints when created by user', async function () { - const value = 'DBRef("coll", ObjectId("5f16b8bebe434dc98cdfc9ca"))'; + const value = "DBRef('coll', ObjectId('5f16b8bebe434dc98cdfc9ca'))"; expect(await shell.executeLine(value)).to.include(value); shell.assertNoErrors(); }); @@ -275,13 +290,13 @@ describe('BSON e2e', function () { }); it('NumberLong prints when created by user', async function () { const value = 'NumberLong("64")'; - expect(await shell.executeLine(value)).to.include('Long("64")'); + expect(await shell.executeLine(value)).to.include("Long('64')"); shell.assertNoErrors(); }); it('NumberLong prints when created by user (> MAX_SAFE_INTEGER)', async function () { const value = 'NumberLong("345678654321234561")'; expect(await shell.executeLine(value)).to.include( - 'Long("345678654321234561")' + "Long('345678654321234561')" ); shell.assertNoErrors(); }); @@ -299,54 +314,56 @@ describe('BSON e2e', function () { }); it('Symbol prints when created by user', async function () { const value = 'new BSONSymbol("symbol")'; - expect(await shell.executeLine(value)).to.include('"symbol"'); + expect(await shell.executeLine(value)).to.include("BSONSymbol('symbol')"); shell.assertNoErrors(); }); it('Code prints when created by user', async function () { const value = 'new Code("abc")'; - expect(await shell.executeLine(value)).to.include('Code("abc")'); + expect(await shell.executeLine(value)).to.include("Code('abc')"); shell.assertNoErrors(); }); it('Code with scope prints when created by user', async function () { const value = 'new Code("abc", { s: 1 })'; - expect(await shell.executeLine(value)).to.include('Code("abc", {"s":1})'); + expect(await shell.executeLine(value)).to.include( + "Code('abc', { s: 1 })" + ); shell.assertNoErrors(); }); it('Decimal128 prints when created by user', async function () { const value = 'NumberDecimal("100")'; - expect(await shell.executeLine(value)).to.include('Decimal128("100")'); + expect(await shell.executeLine(value)).to.include("Decimal128('100')"); shell.assertNoErrors(); }); // NOTE this is a slight change from the old shell, since the old shell just // printed the raw input, while this one converts it to a string. it('BinData prints when created by user', async function () { - const value = 'BinData(128, "MTIzNA==")'; + const value = "BinData(128, 'MTIzNA==')"; expect(await shell.executeLine(value)).to.include( - 'Binary.createFromBase64("MTIzNA==", 128)' + "Binary.createFromBase64('MTIzNA==', 128)" ); shell.assertNoErrors(); }); it('BinData prints as UUID when created by user as such', async function () { - const value = 'UUID("01234567-89ab-cdef-0123-456789abcdef")'; + const value = "UUID('01234567-89ab-cdef-0123-456789abcdef')"; expect(await shell.executeLine(value)).to.include(value); shell.assertNoErrors(); }); it('BinData prints as MD5 when created by user as such', async function () { - const value = 'MD5("0123456789abcdef0123456789abcdef")'; + const value = "MD5('0123456789abcdef0123456789abcdef')"; expect(await shell.executeLine(value)).to.include(value); shell.assertNoErrors(); }); it('BinData prints as BinData when created as invalid UUID', async function () { const value = 'UUID("abcdef")'; expect(await shell.executeLine(value)).to.include( - 'Binary.createFromBase64("q83v", 4)' + "Binary.createFromBase64('q83v', 4)" ); shell.assertNoErrors(); }); it('ISODate prints when created by user', async function () { const value = 'ISODate("2021-05-04T15:49:33.000Z")'; expect(await shell.executeLine(value)).to.include( - 'ISODate("2021-05-04T15:49:33.000Z")' + "ISODate('2021-05-04T15:49:33.000Z')" ); shell.assertNoErrors(); }); @@ -358,7 +375,7 @@ describe('BSON e2e', function () { it('BSONRegExp prints when created by user', async function () { const value = 'BSONRegExp(`(?-i)A"A_`, "im")'; expect(await shell.executeLine(value)).to.include( - String.raw`BSONRegExp("(?-i)A\"A_", "im")` + String.raw`BSONRegExp('(?-i)A"A_', 'im')` ); shell.assertNoErrors(); }); diff --git a/packages/node-runtime-worker-thread/package.json b/packages/node-runtime-worker-thread/package.json index 814915591..ab1683b8b 100644 --- a/packages/node-runtime-worker-thread/package.json +++ b/packages/node-runtime-worker-thread/package.json @@ -42,7 +42,7 @@ "@mongosh/service-provider-core": "0.0.0-dev.0", "@mongosh/service-provider-server": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mocha": "^10.2.0", diff --git a/packages/service-provider-core/package.json b/packages/service-provider-core/package.json index d386efa2c..9afa421d3 100644 --- a/packages/service-provider-core/package.json +++ b/packages/service-provider-core/package.json @@ -45,7 +45,7 @@ "dependencies": { "@aws-sdk/credential-providers": "^3.347.1", "@mongosh/errors": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "mongodb": "^6.0.0", "mongodb-build-info": "^1.6.2" }, diff --git a/packages/service-provider-core/src/printable-bson.spec.ts b/packages/service-provider-core/src/printable-bson.spec.ts index a6a21a602..d52059bcf 100644 --- a/packages/service-provider-core/src/printable-bson.spec.ts +++ b/packages/service-provider-core/src/printable-bson.spec.ts @@ -10,7 +10,7 @@ describe('BSON printers', function () { it('formats ObjectIds correctly', function () { expect(inspect(new bson.ObjectId('5fa5694f88211043b23c7f11'))).to.equal( - 'ObjectId("5fa5694f88211043b23c7f11")' + "ObjectId('5fa5694f88211043b23c7f11')" ); }); @@ -19,12 +19,12 @@ describe('BSON printers', function () { inspect( new bson.DBRef('a', new bson.ObjectId('5f16b8bebe434dc98cdfc9cb'), 'db') ) - ).to.equal('DBRef("a", ObjectId("5f16b8bebe434dc98cdfc9cb"), \'db\')'); + ).to.equal("DBRef('a', ObjectId('5f16b8bebe434dc98cdfc9cb'), 'db')"); expect(inspect(new bson.DBRef('a', 'foo' as any, 'db'))).to.equal( - "DBRef(\"a\", 'foo', 'db')" + "DBRef('a', 'foo', 'db')" ); expect(inspect(new bson.DBRef('a', { x: 1 } as any, 'db'))).to.equal( - 'DBRef("a", { x: 1 }, \'db\')' + "DBRef('a', { x: 1 }, 'db')" ); }); @@ -38,18 +38,18 @@ describe('BSON printers', function () { }); it('formats NumberLong correctly', function () { - expect(inspect(bson.Long.fromString('64'))).to.equal('Long("64")'); + expect(inspect(bson.Long.fromString('64'))).to.equal("Long('64')"); }); it('formats unsigned NumberLong correctly', function () { expect(inspect(bson.Long.fromString('64', true))).to.equal( - 'Long("64", true)' + "Long('64', true)" ); }); it('formats NumberDecimal correctly', function () { expect(inspect(bson.Decimal128.fromString('1'))).to.equal( - 'Decimal128("1")' + "Decimal128('1')" ); }); @@ -60,16 +60,16 @@ describe('BSON printers', function () { }); it('formats Symbol correctly', function () { - expect(inspect(new bson.BSONSymbol('abc'))).to.equal('BSONSymbol("abc")'); + expect(inspect(new bson.BSONSymbol('abc'))).to.equal("BSONSymbol('abc')"); }); it('formats Code correctly', function () { - expect(inspect(new bson.Code('abc'))).to.equal('Code("abc")'); + expect(inspect(new bson.Code('abc'))).to.equal("Code('abc')"); }); it('formats BSONRegExp correctly', function () { expect(inspect(new bson.BSONRegExp('(?-i)AA_', 'im'))).to.equal( - 'BSONRegExp("(?-i)AA_", "im")' + "BSONRegExp('(?-i)AA_', 'im')" ); }); @@ -81,7 +81,7 @@ describe('BSON printers', function () { 4 ) ) - ).to.equal('UUID("01234567-89ab-cdef-0123-456789abcdef")'); + ).to.equal("UUID('01234567-89ab-cdef-0123-456789abcdef')"); }); it('formats MD5s correctly', function () { @@ -92,12 +92,12 @@ describe('BSON printers', function () { 5 ) ) - ).to.equal('MD5("0123456789abcdef0123456789abcdef")'); + ).to.equal("MD5('0123456789abcdef0123456789abcdef')"); }); it('formats any other value with the new format using createfromBase64', function () { expect( inspect(bson.Binary.createFromBase64('SGVsbG8sIFdvcmxkIQo=')) - ).to.equal('Binary.createFromBase64("SGVsbG8sIFdvcmxkIQo=", 0)'); + ).to.equal("Binary.createFromBase64('SGVsbG8sIFdvcmxkIQo=', 0)"); }); }); diff --git a/packages/service-provider-core/src/printable-bson.ts b/packages/service-provider-core/src/printable-bson.ts index 3d1a0cf4e..7a9a18187 100644 --- a/packages/service-provider-core/src/printable-bson.ts +++ b/packages/service-provider-core/src/printable-bson.ts @@ -24,24 +24,14 @@ function makeClasslessInspect(className: K) { }; } +const binaryInspect = makeClasslessInspect('Binary'); export const bsonStringifiers: Record< BSONClassKey | 'ObjectID', (this: any, depth: any, options: any) => string > = { ObjectId: makeClasslessInspect('ObjectId'), ObjectID: makeClasslessInspect('ObjectId'), - DBRef: function ( - this: typeof BSON.DBRef.prototype, - depth: any, - options: any - ): string { - return ( - `DBRef("${this.collection}", ` + - inspect(this.oid, options) + // The driver's inspect() does not account for non-ObjectID oid values - (this.db ? `, ${inspect(this.db, options)}` : '') + - ')' - ); - }, + DBRef: makeClasslessInspect('DBRef'), MaxKey: makeClasslessInspect('MaxKey'), MinKey: makeClasslessInspect('MinKey'), Timestamp: makeClasslessInspect('Timestamp'), @@ -52,11 +42,14 @@ export const bsonStringifiers: Record< Long: makeClasslessInspect('Long'), Double: makeClasslessInspect('Double'), BSONRegExp: makeClasslessInspect('BSONRegExp'), - Binary: function (this: typeof BSON.Binary.prototype): string { + Binary: function ( + this: typeof BSON.Binary.prototype, + ...args: any[] + ): string { const hexString = this.toString('hex'); switch (this.sub_type) { case BSON.Binary.SUBTYPE_MD5: - return `MD5("${hexString}")`; + return `MD5('${hexString}')`; case BSON.Binary.SUBTYPE_UUID: if (hexString.length === 32) { // Format '0123456789abcdef0123456789abcdef' into @@ -65,14 +58,12 @@ export const bsonStringifiers: Record< .exec(hexString)! .slice(1, 6) .join('-'); - return `UUID("${asUUID}")`; + return `UUID('${asUUID}')`; } // In case somebody did something weird and used an UUID with a // non-standard length, fall through. default: - return `Binary.createFromBase64("${this.toString('base64')}", ${ - this.sub_type - })`; + return binaryInspect.apply(this, args); } }, }; diff --git a/packages/shell-api/package.json b/packages/shell-api/package.json index b3b4703e3..bf83f80c4 100644 --- a/packages/shell-api/package.json +++ b/packages/shell-api/package.json @@ -52,7 +52,7 @@ "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/tsconfig-mongosh": "^1.0.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "depcheck": "^1.4.3", "eslint": "^7.25.0", "mongodb": "^6.0.0", diff --git a/packages/snippet-manager/package.json b/packages/snippet-manager/package.json index 5ea4a9bbb..9d3f29405 100644 --- a/packages/snippet-manager/package.json +++ b/packages/snippet-manager/package.json @@ -38,7 +38,7 @@ "@mongosh/errors": "0.0.0-dev.0", "@mongosh/shell-api": "0.0.0-dev.0", "@mongosh/types": "0.0.0-dev.0", - "bson": "^6.1.0", + "bson": "^6.2.0", "cross-spawn": "^7.0.3", "escape-string-regexp": "^4.0.0", "joi": "^17.4.0", From 4f0e884b70b679482c079048a424ecd6d8c597fe Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 19 Oct 2023 16:06:46 +0200 Subject: [PATCH 2/4] fixup: lint and other test fixups --- packages/cli-repl/src/cli-repl.spec.ts | 6 +++--- packages/cli-repl/src/format-output.spec.ts | 2 +- packages/cli-repl/src/mongosh-repl.spec.ts | 2 +- .../node-runtime-worker-thread/src/worker-runtime.spec.ts | 2 +- packages/service-provider-core/src/printable-bson.ts | 1 - 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 623739832..b479ad351 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -1575,7 +1575,7 @@ describe('CliRepl', function () { cliRepl, await testServer.connectionString() ); - expect(output).to.match(/Inserted: ObjectId\("[a-z0-9]{24}"\)/); + expect(output).to.match(/Inserted: ObjectId\('[a-z0-9]{24}'\)/); expect(exitCode).to.equal(0); }); @@ -1596,7 +1596,7 @@ describe('CliRepl', function () { cliRepl, await testServer.connectionString() ); - expect(output).to.match(/Inserted: ObjectId\("[a-z0-9]{24}"\)/); + expect(output).to.match(/Inserted: ObjectId\('[a-z0-9]{24}'\)/); expect(exitCode).to.equal(0); }); @@ -1614,7 +1614,7 @@ describe('CliRepl', function () { cliRepl = new CliRepl(cliReplOptions); await cliRepl.start(await testServer.connectionString(), {}); - expect(output).to.match(/Inserted: ObjectId\("[a-z0-9]{24}"\)/); + expect(output).to.match(/Inserted: ObjectId\('[a-z0-9]{24}'\)/); expect(exitCode).to.equal(null); input.write( diff --git a/packages/cli-repl/src/format-output.spec.ts b/packages/cli-repl/src/format-output.spec.ts index 9963e1738..76f9e530a 100644 --- a/packages/cli-repl/src/format-output.spec.ts +++ b/packages/cli-repl/src/format-output.spec.ts @@ -30,7 +30,7 @@ for (const colors of [false, true]) { context('when the result is a date', function () { it('returns the inspection', function () { expect(format({ value: new Date(1234567890000) })).to.include( - 'ISODate("2009-02-13T23:31:30.000Z")' + "ISODate('2009-02-13T23:31:30.000Z')" ); expect(format({ value: new Date(NaN) })).to.include('Invalid Date'); }); diff --git a/packages/cli-repl/src/mongosh-repl.spec.ts b/packages/cli-repl/src/mongosh-repl.spec.ts index 7f356ac27..3c92e72c0 100644 --- a/packages/cli-repl/src/mongosh-repl.spec.ts +++ b/packages/cli-repl/src/mongosh-repl.spec.ts @@ -274,7 +274,7 @@ describe('MongoshNodeRepl', function () { it('prints Date objects using the ISODate constructor variant', async function () { input.write('new Date(1620143373000)\n'); await waitEval(bus); - expect(output).to.include('ISODate("2021-05-04T15:49:33.000Z")'); + expect(output).to.include("ISODate('2021-05-04T15:49:33.000Z')"); }); it('handles a long series of errors', async function () { diff --git a/packages/node-runtime-worker-thread/src/worker-runtime.spec.ts b/packages/node-runtime-worker-thread/src/worker-runtime.spec.ts index 701d9ef36..feb286204 100644 --- a/packages/node-runtime-worker-thread/src/worker-runtime.spec.ts +++ b/packages/node-runtime-worker-thread/src/worker-runtime.spec.ts @@ -559,7 +559,7 @@ describe('worker', function () { expect(evalListener.onPrint).to.have.been.calledWith([ { - printable: 'ObjectId("62a209b0c7dc31e23ab9da45")', + printable: "ObjectId('62a209b0c7dc31e23ab9da45')", source: undefined, type: 'InspectResult', }, diff --git a/packages/service-provider-core/src/printable-bson.ts b/packages/service-provider-core/src/printable-bson.ts index 7a9a18187..d402ce53d 100644 --- a/packages/service-provider-core/src/printable-bson.ts +++ b/packages/service-provider-core/src/printable-bson.ts @@ -1,5 +1,4 @@ import { bson as BSON } from './bson-export'; -import { inspect } from 'util'; const inspectCustom = Symbol.for('nodejs.util.inspect.custom'); type BSONClassKey = (typeof BSON)[Exclude< keyof typeof BSON, From a2d7df596cfcb09fc500a0cec8d9c54211dd38b0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 30 Oct 2023 11:54:54 +0100 Subject: [PATCH 3/4] fixup: one more e2e test --- packages/e2e-tests/test/e2e.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/test/e2e.spec.ts b/packages/e2e-tests/test/e2e.spec.ts index 48c477324..aedf4789e 100644 --- a/packages/e2e-tests/test/e2e.spec.ts +++ b/packages/e2e-tests/test/e2e.spec.ts @@ -664,7 +664,7 @@ describe('e2e', function () { it('allows calling convertShardKeyToHashed() as a global function', async function () { expect( await shell.executeLine('convertShardKeyToHashed({foo:"bar"})') - ).to.include('Long("4975617422686807705")'); + ).to.include("Long('4975617422686807705')"); }); it('rewrites async properly for a complex $function', async function () { From 1c45ffa0ebc5d0fc4285b87d1a4c23482cd063d2 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 30 Oct 2023 14:20:56 +0100 Subject: [PATCH 4/4] fixup: browser-repl package as well --- packages/browser-repl/src/components/utils/inspect.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser-repl/src/components/utils/inspect.spec.ts b/packages/browser-repl/src/components/utils/inspect.spec.ts index 54bd1a479..d4b0b541e 100644 --- a/packages/browser-repl/src/components/utils/inspect.spec.ts +++ b/packages/browser-repl/src/components/utils/inspect.spec.ts @@ -43,7 +43,7 @@ describe('inspect', function () { it('inspects UUID', function () { expect( inspect(bson.Binary.createFromBase64('YWJjZGVmZ2hpa2xtbm9wcQ==', 4)) - ).to.equal('UUID("61626364-6566-6768-696b-6c6d6e6f7071")'); + ).to.equal("UUID('61626364-6566-6768-696b-6c6d6e6f7071')"); }); it('inspects nested ObjectId', function () { @@ -57,7 +57,7 @@ describe('inspect', function () { inspect({ p: bson.Binary.createFromBase64('YWJjZGVmZ2hpa2xtbm9wcQ==', 4), }) - ).to.equal('{ p: UUID("61626364-6566-6768-696b-6c6d6e6f7071") }'); + ).to.equal("{ p: UUID('61626364-6566-6768-696b-6c6d6e6f7071') }"); }); it('does not require BSON types to be instances of the current bson library', function () {