Skip to content

Commit

Permalink
feat(android, emulator): add firebase.json config element to bypass l…
Browse files Browse the repository at this point in the history
…ocalhost remap (#5852)

* add flag
* Remove unnecessary log, simplify mock object
* Add flag to documentation
  • Loading branch information
stchristian authored Nov 16, 2021
1 parent 232d860 commit ddf3f5f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jest.doMock('react-native', () => {
options: {},
},
],
FIREBASE_RAW_JSON: '{}',
addListener: jest.fn(),
eventsAddListener: jest.fn(),
eventsNotifyReady: jest.fn(),
Expand Down
4 changes: 4 additions & 0 deletions packages/app/firebase-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
"android_background_activity_names": {
"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",
"type": "array"
},
"android_bypass_emulator_url_remap": {
"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`.",
"type": "boolean"
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,19 +360,22 @@ class FirebaseAuthModule extends FirebaseModule {
}

let _url = url;
if (isAndroid && _url) {
const androidBypassEmulatorUrlRemap =
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
this.firebaseJson.android_bypass_emulator_url_remap;
if (!androidBypassEmulatorUrlRemap && isAndroid && _url) {
if (_url.startsWith('http://localhost')) {
_url = _url.replace('http://localhost', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping auth host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices.',
'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.',
);
}
if (_url.startsWith('http://127.0.0.1')) {
_url = _url.replace('http://127.0.0.1', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping auth host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices.',
'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.',
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/firestore/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ class FirebaseFirestoreModule extends FirebaseModule {
throw new Error('firebase.firestore().useEmulator() takes a non-empty host and port');
}
let _host = host;
if (isAndroid && _host) {
const androidBypassEmulatorUrlRemap =
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
this.firebaseJson.android_bypass_emulator_url_remap;
if (!androidBypassEmulatorUrlRemap && isAndroid && _host) {
if (_host === 'localhost' || _host === '127.0.0.1') {
_host = '10.0.2.2';
// eslint-disable-next-line no-console
console.log(
'Mapping firestore host to "10.0.2.2" for android emulators. Use real IP on real devices.',
'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.',
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/functions/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,22 @@ class FirebaseFunctionsModule extends FirebaseModule {

useFunctionsEmulator(origin) {
let _origin = origin;
if (isAndroid && _origin) {
const androidBypassEmulatorUrlRemap =
typeof this.firebaseJson.android_bypass_emulator_url_remap === 'boolean' &&
this.firebaseJson.android_bypass_emulator_url_remap;
if (!androidBypassEmulatorUrlRemap && isAndroid && _origin) {
if (_origin.startsWith('http://localhost')) {
_origin = _origin.replace('http://localhost', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping functions host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices.',
'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.',
);
}
if (_origin.startsWith('http://127.0.0.1')) {
_origin = _origin.replace('http://127.0.0.1', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping functions host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices.',
'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.',
);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

"android_task_executor_maximum_pool_size": 10,
"android_task_executor_keep_alive_seconds": 3,
"android_bypass_emulator_url_remap": false,

"rnfirebase_json_testing_string": "abc",
"rnfirebase_json_testing_boolean_false": false,
Expand Down

1 comment on commit ddf3f5f

@vercel
Copy link

@vercel vercel bot commented on ddf3f5f Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.