Skip to content

Commit 409f9c5

Browse files
committed
Move getGlobal into separate file to remove circular dep
1 parent 9e4dfaf commit 409f9c5

File tree

7 files changed

+47
-30
lines changed

7 files changed

+47
-30
lines changed

common/api-review/util.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ export const getDefaults: () => FirebaseDefaults | undefined;
228228
// @public
229229
export const getExperimentalSetting: <T extends ExperimentalKey>(name: T) => FirebaseDefaults[`_${T}`];
230230

231-
// Warning: (ae-missing-release-tag) "getGlobal" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
232-
//
233231
// @public
234232
export function getGlobal(): typeof globalThis;
235233

packages/util/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ export * from './src/uuid';
4242
export * from './src/exponential_backoff';
4343
export * from './src/formatters';
4444
export * from './src/compat';
45+
export * from './src/global';

packages/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ export * from './src/uuid';
3737
export * from './src/exponential_backoff';
3838
export * from './src/formatters';
3939
export * from './src/compat';
40+
export * from './src/global';

packages/util/src/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { base64Decode } from './crypt';
19-
import { getGlobal } from './environment';
19+
import { getGlobal } from './global';
2020

2121
/**
2222
* Keys for experimental properties on the `FirebaseDefaults` object.

packages/util/src/environment.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,3 @@ export function areCookiesEnabled(): boolean {
201201
}
202202
return true;
203203
}
204-
205-
/**
206-
* Polyfill for `globalThis` object.
207-
* @returns the `globalThis` object for the given environment.
208-
*/
209-
export function getGlobal(): typeof globalThis {
210-
if (typeof self !== 'undefined') {
211-
return self;
212-
}
213-
if (typeof window !== 'undefined') {
214-
return window;
215-
}
216-
if (typeof global !== 'undefined') {
217-
return global;
218-
}
219-
throw new Error('Unable to locate global object.');
220-
}

packages/util/src/global.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Polyfill for `globalThis` object.
20+
* @returns the `globalThis` object for the given environment.
21+
* @public
22+
*/
23+
export function getGlobal(): typeof globalThis {
24+
if (typeof self !== 'undefined') {
25+
return self;
26+
}
27+
if (typeof window !== 'undefined') {
28+
return window;
29+
}
30+
if (typeof global !== 'undefined') {
31+
return global;
32+
}
33+
throw new Error('Unable to locate global object.');
34+
}

packages/util/test/defaults.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import {
2121
getDefaultEmulatorHost,
2222
getDefaultEmulatorHostnameAndPort
2323
} from '../src/defaults';
24-
import * as environment from '../src/environment';
24+
import * as global from '../src/global';
2525

2626
use(sinonChai);
2727

2828
describe('getDefaultEmulatorHost', () => {
2929
after(() => {
30-
delete environment.getGlobal().__FIREBASE_DEFAULTS__;
30+
delete global.getGlobal().__FIREBASE_DEFAULTS__;
3131
});
3232

3333
context('with no config', () => {
@@ -68,7 +68,7 @@ describe('getDefaultEmulatorHost', () => {
6868
context('with no config and something unexpected throws', () => {
6969
let consoleInfoStub: SinonStub;
7070
before(() => {
71-
stub(environment, 'getGlobal').throws(new Error('getGlobal threw!'));
71+
stub(global, 'getGlobal').throws(new Error('getGlobal threw!'));
7272
consoleInfoStub = stub(console, 'info');
7373
});
7474
after(() => {
@@ -83,7 +83,7 @@ describe('getDefaultEmulatorHost', () => {
8383

8484
context('with global config not listing the emulator', () => {
8585
before(() => {
86-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
86+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
8787
emulatorHosts: {
8888
/* no firestore */
8989
database: '127.0.0.1:8080'
@@ -98,7 +98,7 @@ describe('getDefaultEmulatorHost', () => {
9898

9999
context('with IPv4 hostname in global config', () => {
100100
before(() => {
101-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
101+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
102102
emulatorHosts: {
103103
firestore: '127.0.0.1:8080'
104104
}
@@ -112,7 +112,7 @@ describe('getDefaultEmulatorHost', () => {
112112

113113
context('with quoted IPv6 hostname in global config', () => {
114114
before(() => {
115-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
115+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
116116
emulatorHosts: {
117117
firestore: '[::1]:8080'
118118
}
@@ -127,7 +127,7 @@ describe('getDefaultEmulatorHost', () => {
127127

128128
describe('getDefaultEmulatorHostnameAndPort', () => {
129129
after(() => {
130-
delete environment.getGlobal().__FIREBASE_DEFAULTS__;
130+
delete global.getGlobal().__FIREBASE_DEFAULTS__;
131131
});
132132

133133
context('with no config', () => {
@@ -138,7 +138,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
138138

139139
context('with global config not listing the emulator', () => {
140140
before(() => {
141-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
141+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
142142
emulatorHosts: {
143143
/* no firestore */
144144
database: '127.0.0.1:8080'
@@ -153,7 +153,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
153153

154154
context('with IPv4 hostname in global config', () => {
155155
before(() => {
156-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
156+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
157157
emulatorHosts: {
158158
firestore: '127.0.0.1:8080'
159159
}
@@ -170,7 +170,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
170170

171171
context('with quoted IPv6 hostname in global config', () => {
172172
before(() => {
173-
environment.getGlobal().__FIREBASE_DEFAULTS__ = {
173+
global.getGlobal().__FIREBASE_DEFAULTS__ = {
174174
emulatorHosts: {
175175
firestore: '[::1]:8080'
176176
}

0 commit comments

Comments
 (0)