@@ -272,23 +272,44 @@ function addAttributes<AppModelType, DbModelType extends DocumentData>(
272
272
} ;
273
273
274
274
if ( typeof settings . host === 'string' ) {
275
- if ( settings . host . startsWith ( '[' ) && settings . host . endsWith ( ']' ) ) {
276
- // Handling IPv6 addresses
277
- attributes [ ATTR_SERVER_ADDRESS ] = settings . host . slice ( 1 , - 1 ) ;
275
+ let address : string | undefined ;
276
+ let port : string | undefined ;
277
+
278
+ if ( settings . host . startsWith ( '[' ) ) {
279
+ if ( settings . host . endsWith ( ']' ) ) {
280
+ // Theres no port, just the address
281
+ address = settings . host . slice ( 1 , - 1 ) ;
282
+ } else {
283
+ // Handling IPv6 addresses with port
284
+ const lastColonIndex = settings . host . lastIndexOf ( ':' ) ;
285
+ if ( lastColonIndex !== - 1 ) {
286
+ address = settings . host . slice ( 1 , lastColonIndex ) ;
287
+ port = settings . host . slice ( lastColonIndex + 1 ) ;
288
+ }
289
+ }
278
290
} else {
279
- // Handling IPv4 addresses
280
- attributes [ ATTR_SERVER_ADDRESS ] = settings . host ;
291
+ if ( settings . host . includes ( '::' ) ) {
292
+ // Handling IPv6 addresses with port
293
+ const parts = settings . host . split ( ':' ) ;
294
+ address = parts . slice ( 0 , - 1 ) . join ( ':' ) ;
295
+ port = parts [ parts . length - 1 ] ;
296
+ } else if ( settings . host . includes ( ':' ) ) {
297
+ // Handling IPv4 addresses with port
298
+ const parts = settings . host . split ( ':' ) ;
299
+ address = parts [ 0 ] ;
300
+ port = parts [ 1 ] ;
301
+ } else {
302
+ // Handling IPv4 addresses without port
303
+ address = settings . host ;
304
+ }
281
305
}
282
306
283
- if ( settings . host . includes ( ':' ) ) {
284
- // Split the host by ':' to get the address and port
285
- // This will handle both IPv4 and IPv6 addresses correctly
286
- // It will split at the last colon
287
- const lastColonIndex = settings . host . lastIndexOf ( ':' ) ;
288
- if ( lastColonIndex !== - 1 ) {
289
- attributes [ ATTR_SERVER_ADDRESS ] = settings . host . slice ( 0 , lastColonIndex ) ;
290
- attributes [ ATTR_SERVER_PORT ] = Number ( settings . host . slice ( lastColonIndex + 1 ) ) ;
291
- }
307
+ if ( address ) {
308
+ attributes [ ATTR_SERVER_ADDRESS ] = address ;
309
+ }
310
+
311
+ if ( port !== undefined ) {
312
+ attributes [ ATTR_SERVER_PORT ] = Number ( port ) ;
292
313
}
293
314
}
294
315
0 commit comments