Skip to content

refactor(NODE-6610): replace feature flag symbols with internal properties #4354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ export function parseOptions(

const mongoOptions = Object.create(null);

// Feature flags
for (const flag of Object.getOwnPropertySymbols(options)) {
if (FEATURE_FLAGS.has(flag)) {
mongoOptions[flag] = options[flag];
}
}

mongoOptions.hosts = isSRV ? [] : hosts.map(HostAddress.fromString);

const urlOptions = new CaseInsensitiveMap<unknown[]>();
Expand Down Expand Up @@ -515,12 +508,11 @@ export function parseOptions(
);
}

const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
mongoOptions[loggerFeatureFlag] = mongoOptions[loggerFeatureFlag] ?? false;
mongoOptions.__enableMongoLogger = mongoOptions.__enableMongoLogger ?? false;

let loggerEnvOptions: MongoLoggerEnvOptions = {};
let loggerClientOptions: MongoLoggerMongoClientOptions = {};
if (mongoOptions[loggerFeatureFlag]) {
if (mongoOptions.__enableMongoLogger) {
loggerEnvOptions = {
MONGODB_LOG_COMMAND: process.env.MONGODB_LOG_COMMAND,
MONGODB_LOG_TOPOLOGY: process.env.MONGODB_LOG_TOPOLOGY,
Expand All @@ -530,7 +522,7 @@ export function parseOptions(
MONGODB_LOG_ALL: process.env.MONGODB_LOG_ALL,
MONGODB_LOG_MAX_DOCUMENT_LENGTH: process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH,
MONGODB_LOG_PATH: process.env.MONGODB_LOG_PATH,
...mongoOptions[Symbol.for('@@mdb.internalLoggerConfig')]
...mongoOptions.__internalLoggerConfig
};
loggerClientOptions = {
mongodbLogPath: mongoOptions.mongodbLogPath,
Expand Down Expand Up @@ -1317,21 +1309,14 @@ export const OPTIONS = {
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogMaxDocumentLength: { type: 'uint' }
mongodbLogMaxDocumentLength: { type: 'uint' },
__enableMongoLogger: { type: 'boolean' },
__skipPingOnConnect: { type: 'boolean' },
__internalLoggerConfig: { type: 'record' }
} as Record<keyof MongoClientOptions, OptionDescriptor>;

export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
Object.entries(OPTIONS)
.filter(([, descriptor]) => descriptor.default != null)
.map(([k, d]) => [k, d.default])
);

/**
* Set of permitted feature flags
* @internal
*/
export const FEATURE_FLAGS = new Set([
Symbol.for('@@mdb.skipPingOnConnect'),
Symbol.for('@@mdb.enableMongoLogger'),
Symbol.for('@@mdb.internalLoggerConfig')
]);
16 changes: 12 additions & 4 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
type LogComponentSeveritiesClientOptions,
type MongoDBLogWritable,
MongoLogger,
type MongoLoggerEnvOptions,
type MongoLoggerOptions,
SeverityLevel
} from './mongo_logger';
Expand Down Expand Up @@ -299,7 +300,11 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
mongodbLogMaxDocumentLength?: number;

/** @internal */
[featureFlag: symbol]: any;
__skipPingOnConnect?: boolean;
/** @internal */
__internalLoggerConfig?: MongoLoggerEnvOptions;
/** @internal */
__enableMongoLogger?: boolean;
}

/** @public */
Expand Down Expand Up @@ -1006,9 +1011,6 @@ export interface MongoOptions
tlsCRLFile?: string;
tlsCertificateKeyFile?: string;

/** @internal */
[featureFlag: symbol]: any;

/**
* @internal
* TODO: NODE-5671 - remove internal flag
Expand All @@ -1020,4 +1022,10 @@ export interface MongoOptions
*/
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
timeoutMS?: number;
/** @internal */
__skipPingOnConnect?: boolean;
/** @internal */
__internalLoggerConfig?: Document;
/** @internal */
__enableMongoLogger?: boolean;
}
4 changes: 2 additions & 2 deletions src/operations/execute_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async function autoConnect(client: MongoClient): Promise<Topology> {
if (client.s.hasBeenClosed) {
throw new MongoNotConnectedError('Client must be connected before running operations');
}
client.s.options[Symbol.for('@@mdb.skipPingOnConnect')] = true;
client.s.options.__skipPingOnConnect = true;
try {
await client.connect();
if (client.topology == null) {
Expand All @@ -141,7 +141,7 @@ async function autoConnect(client: MongoClient): Promise<Topology> {
}
return client.topology;
} finally {
delete client.s.options[Symbol.for('@@mdb.skipPingOnConnect')];
delete client.s.options.__skipPingOnConnect;
}
}
return client.topology;
Expand Down
9 changes: 4 additions & 5 deletions src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MongoCredentials } from '../cmap/auth/mongo_credentials';
import type { ConnectionEvents } from '../cmap/connection';
import type { ConnectionPoolEvents } from '../cmap/connection_pool';
import type { ClientMetadata } from '../cmap/handshake/client_metadata';
import { DEFAULT_OPTIONS, FEATURE_FLAGS } from '../connection_string';
import { DEFAULT_OPTIONS } from '../connection_string';
import {
CLOSE,
CONNECT,
Expand Down Expand Up @@ -153,7 +153,7 @@ export interface TopologyOptions extends BSONSerializeOptions, ServerOptions {
serverMonitoringMode: ServerMonitoringMode;
/** MongoDB server API version */
serverApi?: ServerApi;
[featureFlag: symbol]: any;
__skipPingOnConnect?: boolean;
}

/** @public */
Expand Down Expand Up @@ -251,8 +251,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
// Options should only be undefined in tests, MongoClient will always have defined options
options = options ?? {
hosts: [HostAddress.fromString('localhost:27017')],
...Object.fromEntries(DEFAULT_OPTIONS.entries()),
...Object.fromEntries(FEATURE_FLAGS.entries())
...Object.fromEntries(DEFAULT_OPTIONS.entries())
};

if (typeof seeds === 'string') {
Expand Down Expand Up @@ -466,7 +465,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
readPreferenceServerSelector(readPreference),
selectServerOptions
);
const skipPingOnConnect = this.s.options[Symbol.for('@@mdb.skipPingOnConnect')] === true;
const skipPingOnConnect = this.s.options.__skipPingOnConnect === true;
if (!skipPingOnConnect && this.s.credentials) {
await server.command(ns('admin.$cmd'), { ping: 1 }, { timeoutContext });
stateTransition(this, STATE_CONNECTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CapturingMongoClient extends MongoClient {
commandStartedEvents: Array<CommandStartedEvent> = [];
clientsCreated = 0;
constructor(url: string, options: MongoClientOptions = {}) {
options = { ...options, monitorCommands: true, [Symbol.for('@@mdb.skipPingOnConnect')]: true };
options = { ...options, monitorCommands: true, __skipPingOnConnect: true };
if (process.env.MONGODB_API_VERSION) {
options.serverApi = process.env.MONGODB_API_VERSION as MongoClientOptions['serverApi'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { expect } from 'chai';
import { DEFAULT_MAX_DOCUMENT_LENGTH, type Document } from '../../mongodb';

describe('Command Logging and Monitoring Prose Tests', function () {
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
const ELLIPSES_LENGTH = 3;
let client;
let writable;
Expand Down Expand Up @@ -40,7 +39,7 @@ describe('Command Logging and Monitoring Prose Tests', function () {
client = this.configuration.newClient(
{},
{
[loggerFeatureFlag]: true,
__enableMongoLogger: true,
mongodbLogPath: writable,
mongodbLogComponentSeverities: {
command: 'debug'
Expand Down Expand Up @@ -124,7 +123,7 @@ describe('Command Logging and Monitoring Prose Tests', function () {
client = this.configuration.newClient(
{},
{
[loggerFeatureFlag]: true,
__enableMongoLogger: true,
mongodbLogPath: writable,
mongodbLogComponentSeverities: {
command: 'debug'
Expand Down Expand Up @@ -181,7 +180,7 @@ describe('Command Logging and Monitoring Prose Tests', function () {
client = this.configuration.newClient(
{},
{
[loggerFeatureFlag]: true,
__enableMongoLogger: true,
mongodbLogPath: writable,
mongodbLogComponentSeverities: {
command: 'debug'
Expand Down
168 changes: 0 additions & 168 deletions test/integration/node-specific/feature_flags.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/integration/node-specific/ipv6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('IPv6 Addresses', () => {

ipv6Hosts = this.configuration.options.hostAddresses.map(({ port }) => `[::1]:${port}`);
client = this.configuration.newClient(`mongodb://${ipv6Hosts.join(',')}/test`, {
[Symbol.for('@@mdb.skipPingOnConnect')]: true,
__skipPingOnConnect: true,
maxPoolSize: 1
});
});
Expand Down
Loading
Loading