| 221 | } |
| 222 | |
| 223 | newClient( |
| 224 | urlOrQueryOptions?: string | Record<string, any>, |
| 225 | serverOptions?: MongoClientOptions |
| 226 | ): MongoClient { |
| 227 | const baseOptions: MongoClientOptions = this.compressor |
| 228 | ? { |
| 229 | compressors: this.compressor |
| 230 | } |
| 231 | : {}; |
| 232 | |
| 233 | serverOptions = Object.assign(baseOptions, getEnvironmentalOptions(), serverOptions); |
| 234 | |
| 235 | if (this.loggingEnabled && !Object.hasOwn(serverOptions, 'mongodbLogPath')) { |
| 236 | serverOptions = this.setupLogging(serverOptions); |
| 237 | } |
| 238 | |
| 239 | // Support MongoClient constructor form (url, options) for `newClient`. |
| 240 | if (typeof urlOrQueryOptions === 'string') { |
| 241 | if ('host' in serverOptions || 'port' in serverOptions) { |
| 242 | throw new Error(`Cannot use options to specify host/port, must be in ${urlOrQueryOptions}`); |
| 243 | } |
| 244 | |
| 245 | const newClient: MongoClient = new MongoClient(urlOrQueryOptions, serverOptions); |
| 246 | return newClient; |
| 247 | } |
| 248 | |
| 249 | const queryOptions = urlOrQueryOptions ?? {}; |
| 250 | |
| 251 | // Fall back. |
| 252 | let dbHost = queryOptions.host || this.options.host; |
| 253 | if (dbHost.indexOf('.sock') !== -1) { |
| 254 | dbHost = qs.escape(dbHost); |
| 255 | } |
| 256 | delete queryOptions.host; |
| 257 | const dbPort = queryOptions.port || this.options.port; |
| 258 | delete queryOptions.port; |
| 259 | |
| 260 | if (this.options.authMechanism && !serverOptions.authMechanism) { |
| 261 | Object.assign(queryOptions, { |
| 262 | authMechanism: this.options.authMechanism |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | if (this.options.authMechanismProperties && !serverOptions.authMechanismProperties) { |
| 267 | Object.assign(queryOptions, { |
| 268 | authMechanismProperties: convertToConnStringMap(this.options.authMechanismProperties) |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | if (this.options.replicaSet && !serverOptions.replicaSet) { |
| 273 | Object.assign(queryOptions, { replicaSet: this.options.replicaSet }); |
| 274 | } |
| 275 | |
| 276 | if (this.options.proxyURIParams) { |
| 277 | for (const [name, value] of Object.entries(this.options.proxyURIParams)) { |
| 278 | if (value) { |
| 279 | queryOptions[name] = value; |
| 280 | } |