Skip to content

Commit d3b5de8

Browse files
committed
Refactor project structure to support node
1 parent 3fa6502 commit d3b5de8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+602
-422
lines changed

packages-exp/auth-exp/index.node.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,26 @@
2222
* just use index.browser.ts
2323
*/
2424

25+
import * as fetchImpl from 'node-fetch';
26+
27+
import { registerVersion } from '@firebase/app-exp';
28+
import { name, version } from './package.json';
2529
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
2630
import { ClientPlatform } from './src/core/util/version';
31+
import { FetchProvider } from './src/core/util/fetch_provider';
32+
33+
// Initialize the fetch polyfill, the types are slightly off so just cast and hope for the best
34+
FetchProvider.initialize(
35+
(fetchImpl.default as unknown) as typeof fetch,
36+
(fetchImpl.Headers as unknown) as typeof Headers,
37+
(fetchImpl.Response as unknown) as typeof Response
38+
);
2739

2840
// Core functionality shared by all clients
2941
export * from './src';
3042

3143
export const initializeAuth = _initializeAuthForClientPlatform(
3244
ClientPlatform.NODE
3345
);
46+
47+
registerVersion(name, version, 'node');

packages-exp/auth-exp/index.rn.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
* just use index.ts
2323
*/
2424

25+
import { registerVersion } from '@firebase/app-exp';
2526
import { AsyncStorage } from 'react-native';
27+
import { name, version } from './package.json';
2628
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
27-
import { getReactNativePersistence } from './src/core/persistence/react_native';
29+
import { getReactNativePersistence } from './src/platform_react_native/persistence/react_native';
2830
import { ClientPlatform } from './src/core/util/version';
2931

3032
// Core functionality shared by all clients
@@ -37,3 +39,5 @@ export const reactNativeLocalPersistence = getReactNativePersistence(
3739
export const initializeAuth = _initializeAuthForClientPlatform(
3840
ClientPlatform.REACT_NATIVE
3941
);
42+
43+
registerVersion(name, version, 'rn');

packages-exp/auth-exp/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { registerVersion } from '@firebase/app-exp';
19+
import { name, version } from './package.json';
1820
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
1921
import { ClientPlatform } from './src/core/util/version';
2022

@@ -23,35 +25,43 @@ export * from './src';
2325

2426
// Additional DOM dependend functionality
2527

26-
// core/persistence
28+
// persistence
2729
export {
2830
browserLocalPersistence,
2931
browserSessionPersistence
30-
} from './src/core/persistence/browser';
32+
} from './src/platform_browser/persistence/browser';
33+
export { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db';
3134

32-
// core/strategies
35+
// providers
36+
export { PhoneAuthProvider } from './src/platform_browser/providers/phone';
37+
38+
// strategies
3339
export {
3440
signInWithPhoneNumber,
3541
linkWithPhoneNumber,
3642
reauthenticateWithPhoneNumber,
3743
updatePhoneNumber
38-
} from './src/core/strategies/phone';
44+
} from './src/platform_browser/strategies/phone';
3945
export {
4046
signInWithPopup,
4147
linkWithPopup,
4248
reauthenticateWithPopup
43-
} from './src/core/strategies/popup';
49+
} from './src/platform_browser/strategies/popup';
4450
export {
4551
signInWithRedirect,
4652
linkWithRedirect,
4753
reauthenticateWithRedirect,
4854
getRedirectResult
49-
} from './src/core/strategies/redirect';
55+
} from './src/platform_browser/strategies/redirect';
5056

51-
// platform_browser
5257
export { RecaptchaVerifier } from './src/platform_browser/recaptcha/recaptcha_verifier';
5358
export { browserPopupRedirectResolver } from './src/platform_browser/popup_redirect';
5459

60+
// MFA
61+
export { PhoneMultiFactorGenerator } from './src/platform_browser/mfa/assertions/phone';
62+
5563
export const initializeAuth = _initializeAuthForClientPlatform(
5664
ClientPlatform.BROWSER
5765
);
66+
67+
registerVersion(name, version);

packages-exp/auth-exp/index.webworker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { registerVersion } from '@firebase/app-exp';
19+
import { name, version } from './package.json';
1820
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
1921
import { ClientPlatform } from './src/core/util/version';
2022

@@ -24,3 +26,5 @@ export * from './src';
2426
export const initializeAuth = _initializeAuthForClientPlatform(
2527
ClientPlatform.WORKER
2628
);
29+
30+
registerVersion(name, version, 'webworker');

packages-exp/auth-exp/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
"build:internal": "rollup -c rollup.config.internal.js",
2323
"build:release": "rollup -c rollup.config.release.js && yarn api-report && yarn typings:public",
2424
"dev": "rollup -c -w",
25-
"test": "yarn type-check && run-p lint test:browser",
25+
"test": "yarn type-check && run-p lint test:browser test:node",
2626
"test:browser": "karma start --single-run",
2727
"test:browser:unit": "karma start --single-run --unit",
2828
"test:browser:integration": "karma start --single-run --integration",
2929
"test:browser:debug": "karma start --auto-watch",
30-
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha src/**/*.test.* --opts ../../config/mocha.node.opts",
30+
"test:browser:unit:debug": "karma start --auto-watch --unit",
31+
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'src/!(platform_browser|platform_react_native)/**/*.test.ts' --file index.node.ts --config ../../config/mocharc.node.js",
3132
"type-check": "tsc -p . --noEmit",
3233
"prepare": "yarn build",
3334
"typings:public": "node ./use_typings.js --public",

packages-exp/auth-exp/rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import strip from '@rollup/plugin-strip';
1919
import typescriptPlugin from 'rollup-plugin-typescript2';
20+
import json from 'rollup-plugin-json';
2021
import typescript from 'typescript';
2122
import pkg from './package.json';
2223

@@ -28,6 +29,7 @@ const deps = Object.keys(
2829
* Common plugins for all builds
2930
*/
3031
const commonPlugins = [
32+
json(),
3133
strip({
3234
functions: ['debugAssert.*']
3335
})

packages-exp/auth-exp/src/api/authentication/token.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { querystring } from '@firebase/util';
2121

2222
import { _performFetchWithErrorHandling, HttpMethod } from '../';
2323
import { AuthCore } from '../../model/auth';
24+
import { FetchProvider } from '../../core/util/fetch_provider';
2425

2526
export const _ENDPOINT = 'v1/token';
2627
const GRANT_TYPE = 'refresh_token';
@@ -52,7 +53,7 @@ export async function requestStsToken(
5253
const { apiScheme, tokenApiHost, apiKey, sdkClientVersion } = auth.config;
5354
const url = `${apiScheme}://${tokenApiHost}/${_ENDPOINT}`;
5455

55-
return fetch(`${url}?key=${apiKey}`, {
56+
return FetchProvider.fetch()(`${url}?key=${apiKey}`, {
5657
method: HttpMethod.POST,
5758
headers: {
5859
'X-Client-Version': sdkClientVersion,

packages-exp/auth-exp/src/api/index.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { assert, expect, use } from 'chai';
1919
import * as chaiAsPromised from 'chai-as-promised';
20-
import { SinonStub, stub, useFakeTimers } from 'sinon';
20+
import { useFakeTimers } from 'sinon';
2121

2222
import { FirebaseError } from '@firebase/util';
2323

@@ -207,19 +207,11 @@ describe('api/_performApiRequest', () => {
207207
});
208208

209209
context('with network issues', () => {
210-
let fetchStub: SinonStub;
211-
212-
beforeEach(() => {
213-
fetchStub = stub(self, 'fetch');
214-
});
215-
216-
afterEach(() => {
217-
fetchStub.restore();
218-
});
210+
afterEach(mockFetch.tearDown);
219211

220212
it('should handle timeouts', async () => {
221213
const clock = useFakeTimers();
222-
fetchStub.callsFake(() => {
214+
mockFetch.setUpWithOverride(() => {
223215
return new Promise<never>(() => null);
224216
});
225217
const promise = _performApiRequest<typeof request, never>(
@@ -234,7 +226,7 @@ describe('api/_performApiRequest', () => {
234226
});
235227

236228
it('should handle network failure', async () => {
237-
fetchStub.callsFake(() => {
229+
mockFetch.setUpWithOverride(() => {
238230
return new Promise<never>((_, reject) =>
239231
reject(new Error('network error'))
240232
);

packages-exp/auth-exp/src/api/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { AuthCore } from '../model/auth';
2727
import { IdTokenResponse, TaggedWithTokenResponse } from '../model/id_token';
2828
import { IdTokenMfaResponse } from './authentication/mfa';
2929
import { ServerError, ServerErrorMap, SERVER_ERROR_MAP } from './errors';
30+
import { FetchProvider } from '../core/util/fetch_provider';
3031

3132
export enum HttpMethod {
3233
POST = 'POST',
@@ -88,15 +89,15 @@ export async function _performApiRequest<T, V>(
8889
...params
8990
}).slice(1);
9091

91-
const headers = new Headers();
92+
const headers = new (FetchProvider.headers())();
9293
headers.set(HttpHeader.CONTENT_TYPE, 'application/json');
9394
headers.set(HttpHeader.X_CLIENT_VERSION, auth.config.sdkClientVersion);
9495

9596
if (auth.languageCode) {
9697
headers.set(HttpHeader.X_FIREBASE_LOCALE, auth.languageCode);
9798
}
9899

99-
return fetch(
100+
return FetchProvider.fetch()(
100101
`${auth.config.apiScheme}://${auth.config.apiHost}${path}?${query}`,
101102
{
102103
method,

0 commit comments

Comments
 (0)