Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android, emulator): add firebase.json config element to bypass localhost remap #5852

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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