diff --git a/.gitignore b/.gitignore index 893bde713..ac51ae0c5 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ lib .nyc_output/ coverage/ *.d.ts +!test/register-bson.d.ts *.tgz docs/public diff --git a/api-extractor.json b/api-extractor.json index 165a7260e..77da83f70 100644 --- a/api-extractor.json +++ b/api-extractor.json @@ -1,6 +1,6 @@ { "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", - "mainEntryPointFilePath": "/lib/bson.d.ts", + "mainEntryPointFilePath": "/lib/index.d.ts", "apiReport": { "enabled": false }, diff --git a/bower.json b/bower.json deleted file mode 100644 index 74aa6eaab..000000000 --- a/bower.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "bson", - "description": "A bson parser for node.js and the browser", - "keywords": [ - "mongodb", - "bson", - "parser" - ], - "author": "Christian Amor Kvalheim ", - "main": "./dist/bson.js", - "license": "Apache-2.0", - "moduleType": [ - "globals", - "node" - ], - "ignore": [ - "**/.*", - "alternate_parsers", - "benchmarks", - "bower_components", - "node_modules", - "test", - "tools" - ], - "version": "4.7.0" -} diff --git a/docs/upgrade-to-v5.md b/docs/upgrade-to-v5.md index 4fa30e48a..34713d2d1 100644 --- a/docs/upgrade-to-v5.md +++ b/docs/upgrade-to-v5.md @@ -179,3 +179,9 @@ EJSON.parse("...", { strict: false }); /* migrate to */ EJSON.parse("...", { r // stringify EJSON.stringify({}, { strict: true }); /* migrate to */ EJSON.stringify({}, { relaxed: false }); EJSON.stringify({}, { strict: false }); /* migrate to */ EJSON.stringify({}, { relaxed: true }); + +### The BSON default export has been removed. + +* If you import BSON commonjs style `const BSON = require('bson')` then the `BSON.default` property is no longer present. +* If you import BSON esmodule style `import BSON from 'bson'` then this code will crash upon loading. **TODO: This is not the case right now but it will be after NODE-4713.** + * This error will throw: `SyntaxError: The requested module 'bson' does not provide an export named 'default'`. diff --git a/etc/benchmarks/lib_runner.mjs b/etc/benchmarks/lib_runner.mjs index 5e5080e09..f0cea7c5b 100644 --- a/etc/benchmarks/lib_runner.mjs +++ b/etc/benchmarks/lib_runner.mjs @@ -59,7 +59,7 @@ export async function getLibs() { const hash = stdout.trim(); return { name: 'local', - lib: await import('../../lib/bson.js'), + lib: await import('../../lib/index.js'), version: hash }; })(), diff --git a/package.json b/package.json index c90afc7b5..1d1a5a7f1 100644 --- a/package.json +++ b/package.json @@ -80,10 +80,10 @@ "config": { "native": false }, - "main": "lib/bson.js", + "main": "lib/index.js", "module": "dist/bson.esm.js", "browser": { - "./lib/bson.js": "./dist/bson.browser.umd.js", + "./lib/index.js": "./dist/bson.browser.umd.js", "./dist/bson.esm.js": "./dist/bson.browser.esm.js" }, "engines": { diff --git a/rollup.config.mjs b/rollup.config.mjs index e42462533..9d85b8d86 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -32,7 +32,7 @@ const tsConfig = { tsconfig: false, include: ['src/**/*'] }; -const input = 'src/bson.ts'; +const input = 'src/index.ts'; const plugins = (options = { browser: false }) => { return [ diff --git a/src/bson.ts b/src/bson.ts index 89f49dfe3..fd012a332 100644 --- a/src/bson.ts +++ b/src/bson.ts @@ -3,13 +3,11 @@ import { Code } from './code'; import { DBRef } from './db_ref'; import { Decimal128 } from './decimal128'; import { Double } from './double'; -import { EJSON } from './extended_json'; import { Int32 } from './int_32'; import { Long } from './long'; import { MaxKey } from './max_key'; import { MinKey } from './min_key'; import { ObjectId } from './objectid'; -import { BSONError, BSONTypeError } from './error'; import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size'; // Parts of the parser import { deserialize as internalDeserialize, DeserializeOptions } from './parser/deserializer'; @@ -247,39 +245,3 @@ export function deserializeStream( // Return object containing end index of parsing and list of documents return index; } - -/** - * BSON default export - * @deprecated Please use named exports - * @privateRemarks - * We want to someday deprecate the default export, - * so none of the new TS types are being exported on the default - * @public - */ -const BSON = { - Binary, - Code, - DBRef, - Decimal128, - Double, - Int32, - Long, - UUID, - Map, - MaxKey, - MinKey, - ObjectId, - BSONRegExp, - BSONSymbol, - Timestamp, - EJSON, - setInternalBufferSize, - serialize, - serializeWithBufferAndIndex, - deserialize, - calculateObjectSize, - deserializeStream, - BSONError, - BSONTypeError -}; -export default BSON; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..5ef415756 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,19 @@ +import * as BSON from './bson'; + +// Export all named properties from BSON to support +// import { ObjectId, serialize } from 'bson'; +// const { ObjectId, serialize } = require('bson'); +export * from './bson'; + +// Export BSON as a namespace to support: +// import { BSON } from 'bson'; +// const { BSON } = require('bson'); +export { BSON }; + +// BSON does **NOT** have a default export + +// The following will crash in es module environments +// import BSON from 'bson'; + +// The following will work as expected, BSON as a namespace of all the APIs (BSON.ObjectId, BSON.serialize) +// const BSON = require('bson'); diff --git a/test/node/exports.test.ts b/test/node/exports.test.ts index 161e55065..12c6807b8 100644 --- a/test/node/exports.test.ts +++ b/test/node/exports.test.ts @@ -1,3 +1,4 @@ +import { expect } from 'chai'; import * as BSON from '../register-bson'; import { sorted, byStrings } from './tools/utils'; @@ -30,7 +31,7 @@ const EXPECTED_EXPORTS = [ 'deserialize', 'calculateObjectSize', 'deserializeStream', - 'default' + 'BSON' ]; const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize']; diff --git a/test/register-bson.d.ts b/test/register-bson.d.ts new file mode 100644 index 000000000..3bdc94cd1 --- /dev/null +++ b/test/register-bson.d.ts @@ -0,0 +1 @@ +export * from '../src/index' diff --git a/test/register-bson.js b/test/register-bson.js index a62bd5a61..d48cd05ce 100644 --- a/test/register-bson.js +++ b/test/register-bson.js @@ -42,7 +42,7 @@ let BSON; if (web) { BSON = loadBSONWithGlobal().BSON; } else { - BSON = require('../src/bson'); + BSON = require('../src/index'); } // Some mocha tests need to know the environment for instanceof assertions or be skipped