From 13c1ce333b8cd113241a1f7ac07181c1c76194bc Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Thu, 28 Dec 2023 07:46:55 +0100 Subject: [PATCH] feat: allow users to disable automatic host mapping (#11962) --- .../cloud_firestore/lib/src/firestore.dart | 10 ++++++++-- .../cloud_functions/lib/src/firebase_functions.dart | 6 ++++-- .../firebase_auth/lib/src/firebase_auth.dart | 6 ++++-- .../firebase_storage/lib/src/firebase_storage.dart | 6 ++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart b/packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart index 01c538c8df12..1350814c8339 100644 --- a/packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart +++ b/packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart @@ -121,7 +121,12 @@ class FirebaseFirestore extends FirebasePluginPlatform { /// /// Note: Must be called immediately, prior to accessing FirebaseFirestore methods. /// Do not use with production credentials as emulator traffic is not encrypted. - void useFirestoreEmulator(String host, int port, {bool sslEnabled = false}) { + void useFirestoreEmulator( + String host, + int port, { + bool sslEnabled = false, + bool automaticHostMapping = true, + }) { if (kIsWeb) { // use useEmulator() API for web as settings are set immediately unlike native platforms try { @@ -140,7 +145,8 @@ class FirebaseFirestore extends FirebasePluginPlatform { String mappedHost = host; // Android considers localhost as 10.0.2.2 - automatically handle this for users. if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) { - if (mappedHost == 'localhost' || mappedHost == '127.0.0.1') { + if ((mappedHost == 'localhost' || mappedHost == '127.0.0.1') && + automaticHostMapping) { // ignore: avoid_print print('Mapping Firestore Emulator host "$mappedHost" to "10.0.2.2".'); mappedHost = '10.0.2.2'; diff --git a/packages/cloud_functions/cloud_functions/lib/src/firebase_functions.dart b/packages/cloud_functions/cloud_functions/lib/src/firebase_functions.dart index 8cc1a2b71533..26468321f791 100644 --- a/packages/cloud_functions/cloud_functions/lib/src/firebase_functions.dart +++ b/packages/cloud_functions/cloud_functions/lib/src/firebase_functions.dart @@ -100,11 +100,13 @@ class FirebaseFunctions extends FirebasePluginPlatform { /// /// Set the [host] of the local emulator, such as "localhost" /// Set the [port] of the local emulator, such as "5001" (port 5001 is default for functions package) - void useFunctionsEmulator(String host, int port) { + void useFunctionsEmulator(String host, int port, + {bool automaticHostMapping = true}) { String mappedHost = host; // Android considers localhost as 10.0.2.2 - automatically handle this for users. if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) { - if (mappedHost == 'localhost' || mappedHost == '127.0.0.1') { + if ((mappedHost == 'localhost' || mappedHost == '127.0.0.1') && + automaticHostMapping) { // ignore: avoid_print print('Mapping Functions Emulator host "$mappedHost" to "10.0.2.2".'); mappedHost = '10.0.2.2'; diff --git a/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart b/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart index d9c365010c1f..3e1292cda194 100644 --- a/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart +++ b/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart @@ -126,11 +126,13 @@ class FirebaseAuth extends FirebasePluginPlatform { /// /// Note: Must be called immediately, prior to accessing auth methods. /// Do not use with production credentials as emulator traffic is not encrypted. - Future useAuthEmulator(String host, int port) async { + Future useAuthEmulator(String host, int port, + {bool automaticHostMapping = true}) async { String mappedHost = host; if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) { - if (mappedHost == 'localhost' || mappedHost == '127.0.0.1') { + if ((mappedHost == 'localhost' || mappedHost == '127.0.0.1') && + automaticHostMapping) { // ignore: avoid_print print('Mapping Auth Emulator host "$mappedHost" to "10.0.2.2".'); mappedHost = '10.0.2.2'; diff --git a/packages/firebase_storage/firebase_storage/lib/src/firebase_storage.dart b/packages/firebase_storage/firebase_storage/lib/src/firebase_storage.dart index d8521b3b335d..e603a8d03834 100644 --- a/packages/firebase_storage/firebase_storage/lib/src/firebase_storage.dart +++ b/packages/firebase_storage/firebase_storage/lib/src/firebase_storage.dart @@ -184,7 +184,8 @@ class FirebaseStorage extends FirebasePluginPlatform { /// /// Note: Must be called immediately, prior to accessing storage methods. /// Do not use with production credentials as emulator traffic is not encrypted. - Future useStorageEmulator(String host, int port) async { + Future useStorageEmulator(String host, int port, + {bool automaticHostMapping = true}) async { assert(host.isNotEmpty); assert(!port.isNegative); @@ -192,7 +193,8 @@ class FirebaseStorage extends FirebasePluginPlatform { // Android considers localhost as 10.0.2.2 - automatically handle this for users. if (defaultTargetPlatform == TargetPlatform.android && !kIsWeb) { - if (mappedHost == 'localhost' || mappedHost == '127.0.0.1') { + if ((mappedHost == 'localhost' || mappedHost == '127.0.0.1') && + automaticHostMapping) { // ignore: avoid_print print('Mapping Storage Emulator host "$mappedHost" to "10.0.2.2".'); mappedHost = '10.0.2.2';