Skip to content

Commit ddf3f5f

Browse files
author
Hübner Krisztián
authored
feat(android, emulator): add firebase.json config element to bypass localhost remap (#5852)
* add flag * Remove unnecessary log, simplify mock object * Add flag to documentation
1 parent 232d860 commit ddf3f5f

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

jest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jest.doMock('react-native', () => {
2828
options: {},
2929
},
3030
],
31+
FIREBASE_RAW_JSON: '{}',
3132
addListener: jest.fn(),
3233
eventsAddListener: jest.fn(),
3334
eventsNotifyReady: jest.fn(),

packages/app/firebase-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105
"android_background_activity_names": {
106106
"description": "The names (as returned by `getShortClassName()` of Activities used outside the context of react native.\nThese are ignored when determining if the app is in foreground for purposes of calling javascript background handlers",
107107
"type": "array"
108+
},
109+
"android_bypass_emulator_url_remap": {
110+
"description": "On android devices, the urls of firebase emulators will be remapped from localhost to 10.0.2.2. If you don't need this behaviour set this fleg to `true`.",
111+
"type": "boolean"
108112
}
109113
}
110114
}

packages/auth/lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,19 +360,22 @@ class FirebaseAuthModule extends FirebaseModule {
360360
}
361361

362362
let _url = url;
363-
if (isAndroid && _url) {
363+
const androidBypassEmulatorUrlRemap =
364+
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
365+
this.firebaseJson.android_bypass_emulator_url_remap;
366+
if (!androidBypassEmulatorUrlRemap && isAndroid && _url) {
364367
if (_url.startsWith('http://localhost')) {
365368
_url = _url.replace('http://localhost', 'http://10.0.2.2');
366369
// eslint-disable-next-line no-console
367370
console.log(
368-
'Mapping auth host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices.',
371+
'Mapping auth host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices. You can bypass this behaviour with "android_bypass_emulator_url_remap" flag.',
369372
);
370373
}
371374
if (_url.startsWith('http://127.0.0.1')) {
372375
_url = _url.replace('http://127.0.0.1', 'http://10.0.2.2');
373376
// eslint-disable-next-line no-console
374377
console.log(
375-
'Mapping auth host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices.',
378+
'Mapping auth host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices. You can bypass this behaviour with "android_bypass_emulator_url_remap" flag.',
376379
);
377380
}
378381
}

packages/firestore/lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ class FirebaseFirestoreModule extends FirebaseModule {
101101
throw new Error('firebase.firestore().useEmulator() takes a non-empty host and port');
102102
}
103103
let _host = host;
104-
if (isAndroid && _host) {
104+
const androidBypassEmulatorUrlRemap =
105+
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
106+
this.firebaseJson.android_bypass_emulator_url_remap;
107+
if (!androidBypassEmulatorUrlRemap && isAndroid && _host) {
105108
if (_host === 'localhost' || _host === '127.0.0.1') {
106109
_host = '10.0.2.2';
107110
// eslint-disable-next-line no-console
108111
console.log(
109-
'Mapping firestore host to "10.0.2.2" for android emulators. Use real IP on real devices.',
112+
'Mapping firestore host to "10.0.2.2" for android emulators. Use real IP on real devices. You can bypass this behaviour with "android_bypass_emulator_url_remap" flag.',
110113
);
111114
}
112115
}

packages/functions/lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,22 @@ class FirebaseFunctionsModule extends FirebaseModule {
9393

9494
useFunctionsEmulator(origin) {
9595
let _origin = origin;
96-
if (isAndroid && _origin) {
96+
const androidBypassEmulatorUrlRemap =
97+
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
98+
this.firebaseJson.android_bypass_emulator_url_remap;
99+
if (!androidBypassEmulatorUrlRemap && isAndroid && _origin) {
97100
if (_origin.startsWith('http://localhost')) {
98101
_origin = _origin.replace('http://localhost', 'http://10.0.2.2');
99102
// eslint-disable-next-line no-console
100103
console.log(
101-
'Mapping functions host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices.',
104+
'Mapping functions host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices. You can bypass this behaviour with "android_bypass_emulator_url_remap" flag.',
102105
);
103106
}
104107
if (_origin.startsWith('http://127.0.0.1')) {
105108
_origin = _origin.replace('http://127.0.0.1', 'http://10.0.2.2');
106109
// eslint-disable-next-line no-console
107110
console.log(
108-
'Mapping functions host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices.',
111+
'Mapping functions host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices. You can bypass this behaviour with "android_bypass_emulator_url_remap" flag.',
109112
);
110113
}
111114
}

tests/firebase.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
"android_task_executor_maximum_pool_size": 10,
3636
"android_task_executor_keep_alive_seconds": 3,
37+
"android_bypass_emulator_url_remap": false,
3738

3839
"rnfirebase_json_testing_string": "abc",
3940
"rnfirebase_json_testing_boolean_false": false,

0 commit comments

Comments
 (0)