Skip to content

Commit

Permalink
feat: allow users to disable automatic host mapping (#11962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyokone committed Dec 28, 2023
1 parent 3eaae47 commit 13c1ce3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> useAuthEmulator(String host, int port) async {
Future<void> 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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,17 @@ 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<void> useStorageEmulator(String host, int port) async {
Future<void> useStorageEmulator(String host, int port,
{bool automaticHostMapping = true}) async {
assert(host.isNotEmpty);
assert(!port.isNegative);

String mappedHost = host;

// 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';
Expand Down

0 comments on commit 13c1ce3

Please sign in to comment.