| 235 | } |
| 236 | |
| 237 | export function parseOptions( |
| 238 | uri: string, |
| 239 | mongoClient: MongoClient | MongoClientOptions | undefined = undefined, |
| 240 | options: MongoClientOptions = {} |
| 241 | ): MongoOptions { |
| 242 | if (mongoClient != null && !(mongoClient instanceof MongoClient)) { |
| 243 | options = mongoClient; |
| 244 | mongoClient = undefined; |
| 245 | } |
| 246 | |
| 247 | class="cm">// validate BSONOptions |
| 248 | if (options.useBigInt64 && typeof options.promoteLongs === class="st">'boolean' && !options.promoteLongs) { |
| 249 | throw new MongoAPIError(class="st">'Must request either bigint or Long for int64 deserialization'); |
| 250 | } |
| 251 | |
| 252 | if (options.useBigInt64 && typeof options.promoteValues === class="st">'boolean' && !options.promoteValues) { |
| 253 | throw new MongoAPIError(class="st">'Must request either bigint or Long for int64 deserialization'); |
| 254 | } |
| 255 | |
| 256 | const url = new ConnectionString(uri); |
| 257 | const { hosts, isSRV } = url; |
| 258 | |
| 259 | const mongoOptions = Object.create(null); |
| 260 | |
| 261 | mongoOptions.hosts = isSRV ? [] : hosts.map(HostAddress.fromString); |
| 262 | |
| 263 | const urlOptions = new CaseInsensitiveMap<unknown[]>(); |
| 264 | |
| 265 | if (url.pathname !== class="st">'/' && url.pathname !== class="st">'') { |
| 266 | const dbName = decodeURIComponent( |
| 267 | url.pathname[0] === class="st">'/' ? url.pathname.slice(1) : url.pathname |
| 268 | ); |
| 269 | if (dbName) { |
| 270 | urlOptions.set(class="st">'dbName', [dbName]); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (url.username !== class="st">'') { |
| 275 | const auth: Document = { |
| 276 | username: decodeURIComponent(url.username) |
| 277 | }; |
| 278 | |
| 279 | if (typeof url.password === class="st">'string') { |
| 280 | auth.password = decodeURIComponent(url.password); |
| 281 | } |
| 282 | |
| 283 | urlOptions.set(class="st">'auth', [auth]); |
| 284 | } |
| 285 | |
| 286 | for (const key of url.searchParams.keys()) { |
| 287 | const values = url.searchParams.getAll(key); |
| 288 | |
| 289 | const isReadPreferenceTags = /readPreferenceTags/i.test(key); |
| 290 | |
| 291 | if (!isReadPreferenceTags && values.length > 1) { |
| 292 | throw new MongoInvalidArgumentError( |
| 293 | `URI option class="st">"${key}" cannot appear more than once in the connection string` |
| 294 | ); |