From e9e8bf5bc209901b5adc3745f5b5aec8770cc3e4 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 18 Oct 2024 14:09:40 -0600 Subject: [PATCH] chore: add lint rule to prevent import from `bson` in driver src (#4285) --- .eslintrc.json | 6 ++++++ src/beta.ts | 2 +- src/bson.ts | 3 +++ src/cmap/auth/mongodb_oidc.ts | 3 +-- src/cmap/auth/mongodb_oidc/callback_workflow.ts | 2 +- src/cmap/auth/mongodb_oidc/command_builders.ts | 3 +-- src/cmap/auth/mongodb_oidc/human_callback_workflow.ts | 3 +-- src/cmap/auth/mongodb_oidc/machine_workflow.ts | 2 +- src/cmap/connection.ts | 9 +++++++-- src/cmap/wire_protocol/on_demand/document.ts | 3 +-- src/cmap/wire_protocol/responses.ts | 3 +-- src/cursor/client_bulk_write_cursor.ts | 3 +-- src/mongo_types.ts | 3 ++- src/operations/search_indexes/create.ts | 3 +-- src/operations/search_indexes/drop.ts | 3 +-- src/operations/search_indexes/update.ts | 3 +-- 16 files changed, 30 insertions(+), 24 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 984b7c98bf7..d9a6d9a9202 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -266,6 +266,12 @@ "patterns": [ "**/../lib/**", "mongodb-mock-server" + ], + "paths": [ + { + "name": "bson", + "message": "Import from the driver's bson.ts file instead." + } ] } ] diff --git a/src/beta.ts b/src/beta.ts index 5d1ddb9998c..05c60fa7069 100644 --- a/src/beta.ts +++ b/src/beta.ts @@ -1,4 +1,4 @@ -import { type Document } from 'bson'; +import { type Document } from './bson'; export * from './index'; diff --git a/src/bson.ts b/src/bson.ts index 260b60aba18..f9db54dc428 100644 --- a/src/bson.ts +++ b/src/bson.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-restricted-imports */ import { BSON, type DeserializeOptions, type SerializeOptions } from 'bson'; export { @@ -12,6 +13,7 @@ export { DBRef, Decimal128, deserialize, + type DeserializeOptions, Document, Double, EJSON, @@ -21,6 +23,7 @@ export { MaxKey, MinKey, ObjectId, + type ObjectIdLike, serialize, Timestamp, UUID diff --git a/src/cmap/auth/mongodb_oidc.ts b/src/cmap/auth/mongodb_oidc.ts index e44436b5ab9..d4b2a843aea 100644 --- a/src/cmap/auth/mongodb_oidc.ts +++ b/src/cmap/auth/mongodb_oidc.ts @@ -1,5 +1,4 @@ -import type { Document } from 'bson'; - +import type { Document } from '../../bson'; import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error'; import type { HandshakeDocument } from '../connect'; import type { Connection } from '../connection'; diff --git a/src/cmap/auth/mongodb_oidc/callback_workflow.ts b/src/cmap/auth/mongodb_oidc/callback_workflow.ts index 02c4993bd9b..afa1b96c78d 100644 --- a/src/cmap/auth/mongodb_oidc/callback_workflow.ts +++ b/src/cmap/auth/mongodb_oidc/callback_workflow.ts @@ -1,6 +1,6 @@ -import { type Document } from 'bson'; import { setTimeout } from 'timers/promises'; +import { type Document } from '../../../bson'; import { MongoMissingCredentialsError } from '../../../error'; import { ns } from '../../../utils'; import type { Connection } from '../../connection'; diff --git a/src/cmap/auth/mongodb_oidc/command_builders.ts b/src/cmap/auth/mongodb_oidc/command_builders.ts index 2c2256e4afc..da0c09c4c91 100644 --- a/src/cmap/auth/mongodb_oidc/command_builders.ts +++ b/src/cmap/auth/mongodb_oidc/command_builders.ts @@ -1,5 +1,4 @@ -import { Binary, BSON, type Document } from 'bson'; - +import { Binary, BSON, type Document } from '../../../bson'; import { type MongoCredentials } from '../mongo_credentials'; import { AuthMechanism } from '../providers'; diff --git a/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts b/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts index 13ac81a6be5..a162ce06f7a 100644 --- a/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts +++ b/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts @@ -1,5 +1,4 @@ -import { BSON } from 'bson'; - +import { BSON } from '../../../bson'; import { MONGODB_ERROR_CODES, MongoError, MongoOIDCError } from '../../../error'; import { Timeout, TimeoutError } from '../../../timeout'; import { type Connection } from '../../connection'; diff --git a/src/cmap/auth/mongodb_oidc/machine_workflow.ts b/src/cmap/auth/mongodb_oidc/machine_workflow.ts index 31b2e278e15..7a0fd96aefc 100644 --- a/src/cmap/auth/mongodb_oidc/machine_workflow.ts +++ b/src/cmap/auth/mongodb_oidc/machine_workflow.ts @@ -1,6 +1,6 @@ -import { type Document } from 'bson'; import { setTimeout } from 'timers/promises'; +import { type Document } from '../../../bson'; import { ns } from '../../../utils'; import type { Connection } from '../../connection'; import type { MongoCredentials } from '../mongo_credentials'; diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 0837c54d3fa..1ed62647a8a 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -1,8 +1,13 @@ -import { type DeserializeOptions } from 'bson'; import { type Readable, Transform, type TransformCallback } from 'stream'; import { clearTimeout, setTimeout } from 'timers'; -import { type BSONSerializeOptions, deserialize, type Document, type ObjectId } from '../bson'; +import { + type BSONSerializeOptions, + deserialize, + type DeserializeOptions, + type Document, + type ObjectId +} from '../bson'; import { type AutoEncrypter } from '../client-side-encryption/auto_encrypter'; import { CLOSE, diff --git a/src/cmap/wire_protocol/on_demand/document.ts b/src/cmap/wire_protocol/on_demand/document.ts index efc7e322888..98189b399c0 100644 --- a/src/cmap/wire_protocol/on_demand/document.ts +++ b/src/cmap/wire_protocol/on_demand/document.ts @@ -1,11 +1,10 @@ -import { type DeserializeOptions } from 'bson'; - import { Binary, type BSONElement, BSONError, BSONType, deserialize, + type DeserializeOptions, getBigInt64LE, getFloat64LE, getInt32LE, diff --git a/src/cmap/wire_protocol/responses.ts b/src/cmap/wire_protocol/responses.ts index 18afde92e72..a968bcb2061 100644 --- a/src/cmap/wire_protocol/responses.ts +++ b/src/cmap/wire_protocol/responses.ts @@ -1,9 +1,8 @@ -import { type DeserializeOptions } from 'bson'; - import { type BSONElement, type BSONSerializeOptions, BSONType, + type DeserializeOptions, type Document, Long, parseToElementsToArray, diff --git a/src/cursor/client_bulk_write_cursor.ts b/src/cursor/client_bulk_write_cursor.ts index 3a4e7eb99aa..69e166effca 100644 --- a/src/cursor/client_bulk_write_cursor.ts +++ b/src/cursor/client_bulk_write_cursor.ts @@ -1,5 +1,4 @@ -import { type Document } from 'bson'; - +import { type Document } from '../bson'; import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses'; import type { MongoClient } from '../mongo_client'; import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write'; diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 68f7d07c46d..be116b36997 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -1,15 +1,16 @@ -import type { BSONType, ObjectIdLike } from 'bson'; import { EventEmitter } from 'events'; import type { Binary, BSONRegExp, + BSONType, Decimal128, Document, Double, Int32, Long, ObjectId, + ObjectIdLike, Timestamp } from './bson'; import { type CommandStartedEvent } from './cmap/command_monitoring_events'; diff --git a/src/operations/search_indexes/create.ts b/src/operations/search_indexes/create.ts index 7e5e55d18d6..2ce66f4707e 100644 --- a/src/operations/search_indexes/create.ts +++ b/src/operations/search_indexes/create.ts @@ -1,5 +1,4 @@ -import type { Document } from 'bson'; - +import type { Document } from '../../bson'; import type { Collection } from '../../collection'; import type { Server } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; diff --git a/src/operations/search_indexes/drop.ts b/src/operations/search_indexes/drop.ts index 4e287cca012..ee9acdf850e 100644 --- a/src/operations/search_indexes/drop.ts +++ b/src/operations/search_indexes/drop.ts @@ -1,5 +1,4 @@ -import type { Document } from 'bson'; - +import type { Document } from '../../bson'; import type { Collection } from '../../collection'; import { MONGODB_ERROR_CODES, MongoServerError } from '../../error'; import type { Server } from '../../sdam/server'; diff --git a/src/operations/search_indexes/update.ts b/src/operations/search_indexes/update.ts index aad7f93536c..b6986da9410 100644 --- a/src/operations/search_indexes/update.ts +++ b/src/operations/search_indexes/update.ts @@ -1,5 +1,4 @@ -import type { Document } from 'bson'; - +import type { Document } from '../../bson'; import type { Collection } from '../../collection'; import type { Server } from '../../sdam/server'; import type { ClientSession } from '../../sessions';