Skip to content

Refactor project structure to support node (auth-next) #3579

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

Merged
merged 1 commit into from
Aug 10, 2020
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
14 changes: 14 additions & 0 deletions packages-exp/auth-exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@
* just use index.browser.ts
*/

import * as fetchImpl from 'node-fetch';

import { registerVersion } from '@firebase/app-exp';
import { name, version } from './package.json';
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
import { ClientPlatform } from './src/core/util/version';
import { FetchProvider } from './src/core/util/fetch_provider';

// Initialize the fetch polyfill, the types are slightly off so just cast and hope for the best
FetchProvider.initialize(
(fetchImpl.default as unknown) as typeof fetch,
(fetchImpl.Headers as unknown) as typeof Headers,
(fetchImpl.Response as unknown) as typeof Response
);

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

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.NODE
);

registerVersion(name, version, 'node');
6 changes: 5 additions & 1 deletion packages-exp/auth-exp/index.rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
* just use index.ts
*/

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

// Core functionality shared by all clients
Expand All @@ -37,3 +39,5 @@ export const reactNativeLocalPersistence = getReactNativePersistence(
export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.REACT_NATIVE
);

registerVersion(name, version, 'rn');
24 changes: 17 additions & 7 deletions packages-exp/auth-exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

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

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

// Additional DOM dependend functionality

// core/persistence
// persistence
export {
browserLocalPersistence,
browserSessionPersistence
} from './src/core/persistence/browser';
} from './src/platform_browser/persistence/browser';
export { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db';

// core/strategies
// providers
export { PhoneAuthProvider } from './src/platform_browser/providers/phone';

// strategies
export {
signInWithPhoneNumber,
linkWithPhoneNumber,
reauthenticateWithPhoneNumber,
updatePhoneNumber
} from './src/core/strategies/phone';
} from './src/platform_browser/strategies/phone';
export {
signInWithPopup,
linkWithPopup,
reauthenticateWithPopup
} from './src/core/strategies/popup';
} from './src/platform_browser/strategies/popup';
export {
signInWithRedirect,
linkWithRedirect,
reauthenticateWithRedirect,
getRedirectResult
} from './src/core/strategies/redirect';
} from './src/platform_browser/strategies/redirect';

// platform_browser
export { RecaptchaVerifier } from './src/platform_browser/recaptcha/recaptcha_verifier';
export { browserPopupRedirectResolver } from './src/platform_browser/popup_redirect';

// MFA
export { PhoneMultiFactorGenerator } from './src/platform_browser/mfa/assertions/phone';

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.BROWSER
);

registerVersion(name, version);
4 changes: 4 additions & 0 deletions packages-exp/auth-exp/index.webworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

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

Expand All @@ -24,3 +26,5 @@ export * from './src';
export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.WORKER
);

registerVersion(name, version, 'webworker');
7 changes: 4 additions & 3 deletions packages-exp/auth-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
"main": "dist/index.node.cjs.js",
"main-esm2017": "dist/index.node.esm2017.js",
"react-native": "dist/index.rn.esm2017.js",
"react-native": "dist/index.rn.cjs.js",
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"esm2017": "dist/index.esm2017.js",
Expand All @@ -22,12 +22,13 @@
"build:internal": "rollup -c rollup.config.internal.js",
"build:release": "rollup -c rollup.config.release.js && yarn api-report && yarn typings:public",
"dev": "rollup -c -w",
"test": "yarn type-check && run-p lint test:browser",
"test": "yarn type-check && run-p lint test:browser test:node",
"test:browser": "karma start --single-run",
"test:browser:unit": "karma start --single-run --unit",
"test:browser:integration": "karma start --single-run --integration",
"test:browser:debug": "karma start --auto-watch",
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha src/**/*.test.* --opts ../../config/mocha.node.opts",
"test:browser:unit:debug": "karma start --auto-watch --unit",
"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",
"type-check": "tsc -p . --noEmit",
"prepare": "yarn build",
"typings:public": "node ./use_typings.js --public",
Expand Down
2 changes: 2 additions & 0 deletions packages-exp/auth-exp/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import strip from '@rollup/plugin-strip';
import typescriptPlugin from 'rollup-plugin-typescript2';
import json from 'rollup-plugin-json';
import typescript from 'typescript';
import pkg from './package.json';

Expand All @@ -28,6 +29,7 @@ const deps = Object.keys(
* Common plugins for all builds
*/
const commonPlugins = [
json(),
strip({
functions: ['debugAssert.*']
})
Expand Down
3 changes: 2 additions & 1 deletion packages-exp/auth-exp/src/api/authentication/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { querystring } from '@firebase/util';

import { _performFetchWithErrorHandling, HttpMethod } from '../';
import { AuthCore } from '../../model/auth';
import { FetchProvider } from '../../core/util/fetch_provider';

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

return fetch(`${url}?key=${apiKey}`, {
return FetchProvider.fetch()(`${url}?key=${apiKey}`, {
method: HttpMethod.POST,
headers: {
'X-Client-Version': sdkClientVersion,
Expand Down
16 changes: 4 additions & 12 deletions packages-exp/auth-exp/src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

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

Expand Down Expand Up @@ -207,19 +207,11 @@ describe('api/_performApiRequest', () => {
});

context('with network issues', () => {
let fetchStub: SinonStub;

beforeEach(() => {
fetchStub = stub(self, 'fetch');
});

afterEach(() => {
fetchStub.restore();
});
afterEach(mockFetch.tearDown);

it('should handle timeouts', async () => {
const clock = useFakeTimers();
fetchStub.callsFake(() => {
mockFetch.setUpWithOverride(() => {
return new Promise<never>(() => null);
});
const promise = _performApiRequest<typeof request, never>(
Expand All @@ -234,7 +226,7 @@ describe('api/_performApiRequest', () => {
});

it('should handle network failure', async () => {
fetchStub.callsFake(() => {
mockFetch.setUpWithOverride(() => {
return new Promise<never>((_, reject) =>
reject(new Error('network error'))
);
Expand Down
5 changes: 3 additions & 2 deletions packages-exp/auth-exp/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { AuthCore } from '../model/auth';
import { IdTokenResponse, TaggedWithTokenResponse } from '../model/id_token';
import { IdTokenMfaResponse } from './authentication/mfa';
import { ServerError, ServerErrorMap, SERVER_ERROR_MAP } from './errors';
import { FetchProvider } from '../core/util/fetch_provider';

export enum HttpMethod {
POST = 'POST',
Expand Down Expand Up @@ -88,15 +89,15 @@ export async function _performApiRequest<T, V>(
...params
}).slice(1);

const headers = new Headers();
const headers = new (FetchProvider.headers())();
headers.set(HttpHeader.CONTENT_TYPE, 'application/json');
headers.set(HttpHeader.X_CLIENT_VERSION, auth.config.sdkClientVersion);

if (auth.languageCode) {
headers.set(HttpHeader.X_FIREBASE_LOCALE, auth.languageCode);
}

return fetch(
return FetchProvider.fetch()(
`${auth.config.apiScheme}://${auth.config.apiHost}${path}?${query}`,
{
method,
Expand Down
Loading