Skip to content

Commit f6afa45

Browse files
authored
Revamp environment check for firebase app (#1757)
* rewrite environment check for firebase app * [AUTOMATED]: Prettier Code Styling
1 parent 4a8faad commit f6afa45

File tree

4 files changed

+82
-99
lines changed

4 files changed

+82
-99
lines changed

packages/app/index.ts

+33-25
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,53 @@
1717

1818
import { FirebaseNamespace } from '@firebase/app-types';
1919
import { createFirebaseNamespace } from './src/firebaseNamespace';
20+
import { isNode, isBrowser } from '@firebase/util';
21+
import { Logger } from '@firebase/logger';
2022

21-
// Node detection logic from: https://github.com/iliakan/detect-node/
22-
let isNode = false;
23-
try {
24-
isNode =
25-
Object.prototype.toString.call(global.process) === '[object process]';
26-
} catch (e) {}
27-
28-
isNode &&
29-
console.warn(`
30-
Warning: This is a browser-targeted Firebase bundle but it appears it is being
31-
run in a Node environment. If running in a Node environment, make sure you
32-
are using the bundle specified by the "main" field in package.json.
33-
34-
If you are using Webpack, you can specify "main" as the first item in
35-
"resolve.mainFields":
36-
https://webpack.js.org/configuration/resolve/#resolvemainfields
37-
38-
If using Rollup, use the rollup-plugin-node-resolve plugin and set "module"
39-
to false and "main" to true:
40-
https://github.com/rollup/rollup-plugin-node-resolve
41-
`);
23+
const logger = new Logger('@firebase/app');
4224

4325
// Firebase Lite detection
44-
if (self && 'firebase' in self) {
45-
console.warn(`
26+
if (isBrowser() && 'firebase' in self) {
27+
logger.warn(`
4628
Warning: Firebase is already defined in the global scope. Please make sure
4729
Firebase library is only loaded once.
4830
`);
4931

5032
const sdkVersion = ((self as any).firebase as FirebaseNamespace).SDK_VERSION;
5133
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
52-
console.warn(`
34+
logger.warn(`
5335
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
5436
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
5537
`);
5638
}
5739
}
5840

59-
export const firebase = createFirebaseNamespace();
41+
const firebaseNamespace = createFirebaseNamespace();
42+
const initializeApp = firebaseNamespace.initializeApp;
43+
44+
firebaseNamespace.initializeApp = function() {
45+
// Environment check before initializing app
46+
// Do the check in initializeApp, so people have a chance to disable it by setting logLevel
47+
// in @firebase/logger
48+
if (isNode()) {
49+
logger.warn(`
50+
Warning: This is a browser-targeted Firebase bundle but it appears it is being
51+
run in a Node environment. If running in a Node environment, make sure you
52+
are using the bundle specified by the "main" field in package.json.
53+
54+
If you are using Webpack, you can specify "main" as the first item in
55+
"resolve.mainFields":
56+
https://webpack.js.org/configuration/resolve/#resolvemainfields
57+
58+
If using Rollup, use the rollup-plugin-node-resolve plugin and set "module"
59+
to false and "main" to true:
60+
https://github.com/rollup/rollup-plugin-node-resolve
61+
`);
62+
}
63+
64+
return initializeApp.apply(undefined, arguments);
65+
};
66+
67+
export const firebase = firebaseNamespace;
6068

6169
export default firebase;

packages/app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"@firebase/app-types": "0.4.0",
2828
"@firebase/util": "0.2.14",
29+
"@firebase/logger": "0.1.13",
2930
"tslib": "1.9.3",
3031
"dom-storage": "2.1.0",
3132
"xmlhttprequest": "1.8.0"

packages/util/index.ts

+15-64
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,18 @@
1515
* limitations under the License.
1616
*/
1717

18-
export { assert, assertionError } from './src/assert';
19-
export { base64, base64Decode, base64Encode } from './src/crypt';
20-
export { CONSTANTS } from './src/constants';
21-
export { deepCopy, deepExtend, patchProperty } from './src/deepCopy';
22-
export { Deferred } from './src/deferred';
23-
export {
24-
getUA,
25-
isMobileCordova,
26-
isNodeSdk,
27-
isReactNative
28-
} from './src/environment';
29-
export {
30-
ErrorFactory,
31-
ErrorMap,
32-
FirebaseError,
33-
StringLike
34-
} from './src/errors';
35-
export { jsonEval, stringify } from './src/json';
36-
export {
37-
decode,
38-
isAdmin,
39-
issuedAtTime,
40-
isValidFormat,
41-
isValidTimestamp
42-
} from './src/jwt';
43-
export {
44-
clone,
45-
contains,
46-
every,
47-
extend,
48-
findKey,
49-
findValue,
50-
forEach,
51-
getAnyKey,
52-
getCount,
53-
getValues,
54-
isEmpty,
55-
isNonNullObject,
56-
map,
57-
safeGet
58-
} from './src/obj';
59-
export { querystring, querystringDecode } from './src/query';
60-
export { Sha1 } from './src/sha1';
61-
export {
62-
async,
63-
CompleteFn,
64-
createSubscribe,
65-
ErrorFn,
66-
Executor,
67-
NextFn,
68-
Observable,
69-
Observer,
70-
PartialObserver,
71-
Subscribe,
72-
Unsubscribe
73-
} from './src/subscribe';
74-
export {
75-
errorPrefix,
76-
validateArgCount,
77-
validateCallback,
78-
validateContextObject,
79-
validateNamespace
80-
} from './src/validation';
81-
export { stringLength, stringToByteArray } from './src/utf8';
18+
export * from './src/assert';
19+
export * from './src/crypt';
20+
export * from './src/constants';
21+
export * from './src/deepCopy';
22+
export * from './src/deferred';
23+
export * from './src/environment';
24+
export * from './src/errors';
25+
export * from './src/json';
26+
export * from './src/jwt';
27+
export * from './src/obj';
28+
export * from './src/query';
29+
export * from './src/sha1';
30+
export * from './src/subscribe';
31+
export * from './src/validation';
32+
export * from './src/utf8';

packages/util/src/environment.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { CONSTANTS } from './constants';
2121
* Returns navigator.userAgent string or '' if it's not defined.
2222
* @return {string} user agent string
2323
*/
24-
export const getUA = function() {
24+
export function getUA(): string {
2525
if (
2626
typeof navigator !== 'undefined' &&
2727
typeof navigator['userAgent'] === 'string'
@@ -30,7 +30,7 @@ export const getUA = function() {
3030
} else {
3131
return '';
3232
}
33-
};
33+
}
3434

3535
/**
3636
* Detect Cordova / PhoneGap / Ionic frameworks on a mobile device.
@@ -40,30 +40,53 @@ export const getUA = function() {
4040
*
4141
* @return {boolean} isMobileCordova
4242
*/
43-
export const isMobileCordova = function() {
43+
export function isMobileCordova(): boolean {
4444
return (
4545
typeof window !== 'undefined' &&
4646
!!(window['cordova'] || window['phonegap'] || window['PhoneGap']) &&
4747
/ios|iphone|ipod|ipad|android|blackberry|iemobile/i.test(getUA())
4848
);
49-
};
49+
}
50+
51+
/**
52+
* Detect Node.js.
53+
*
54+
* @return {boolean} True if Node.js environment is detected.
55+
* Node detection logic from: https://github.com/iliakan/detect-node/
56+
*/
57+
export function isNode(): boolean {
58+
try {
59+
return (
60+
Object.prototype.toString.call(global.process) === '[object process]'
61+
);
62+
} catch (e) {
63+
return false;
64+
}
65+
}
66+
67+
/**
68+
* Detect Browser Environment
69+
*/
70+
export function isBrowser(): boolean {
71+
return typeof window !== 'undefined';
72+
}
5073

5174
/**
5275
* Detect React Native.
5376
*
5477
* @return {boolean} True if ReactNative environment is detected.
5578
*/
56-
export const isReactNative = function() {
79+
export function isReactNative(): boolean {
5780
return (
5881
typeof navigator === 'object' && navigator['product'] === 'ReactNative'
5982
);
60-
};
83+
}
6184

6285
/**
63-
* Detect Node.js.
86+
* Detect whether the current SDK build is the Node version.
6487
*
65-
* @return {boolean} True if Node.js environment is detected.
88+
* @return {boolean} True if it's the Node SDK build.
6689
*/
67-
export const isNodeSdk = function() {
90+
export function isNodeSdk(): boolean {
6891
return CONSTANTS.NODE_CLIENT === true || CONSTANTS.NODE_ADMIN === true;
69-
};
92+
}

0 commit comments

Comments
 (0)