Skip to content

Remove isomorphic-fetch dependency #3782

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 5 commits into from
Sep 21, 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
2 changes: 1 addition & 1 deletion packages-exp/functions-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@firebase/functions-types-exp": "0.0.800",
"@firebase/messaging-types": "0.5.0",
"@firebase/util": "0.3.2",
"isomorphic-fetch": "2.2.1",
"node-fetch": "2.6.1",
"tslib": "^1.11.1"
},
"nyc": {
Expand Down
41 changes: 22 additions & 19 deletions packages-exp/functions-exp/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,31 @@ import { FUNCTIONS_TYPE } from './constants';

export const DEFAULT_REGION = 'us-central1';

const factory: InstanceFactory<'functions'> = (
container: ComponentContainer,
region?: string
) => {
// Dependencies
const app = container.getProvider('app-exp').getImmediate();
const authProvider = container.getProvider('auth-internal');
const messagingProvider = container.getProvider('messaging');
export function registerFunctions(fetchImpl: typeof fetch): void {
const factory: InstanceFactory<'functions'> = (
container: ComponentContainer,
region?: string
) => {
// Dependencies
const app = container.getProvider('app-exp').getImmediate();
const authProvider = container.getProvider('auth-internal');
const messagingProvider = container.getProvider('messaging');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new FunctionsService(app, authProvider, messagingProvider, region);
};

export function registerFunctions(): void {
const namespaceExports = {
// no-inline
Functions: FunctionsService
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new FunctionsService(
app,
authProvider,
messagingProvider,
region,
fetchImpl
);
};

_registerComponent(
new Component(FUNCTIONS_TYPE, factory, ComponentType.PUBLIC)
.setServiceProps(namespaceExports)
.setMultipleInstances(true)
new Component(
FUNCTIONS_TYPE,
factory,
ComponentType.PUBLIC
).setMultipleInstances(true)
);
}
5 changes: 3 additions & 2 deletions packages-exp/functions-exp/src/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
*/
import { registerVersion } from '@firebase/app-exp';
import { registerFunctions } from './config';
import 'isomorphic-fetch';
import nodeFetch from 'node-fetch';

import { name, version } from '../package.json';

export * from './api';

registerFunctions();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerFunctions(nodeFetch as any);
registerVersion(name, version, 'node');
2 changes: 1 addition & 1 deletion packages-exp/functions-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import { name, version } from '../package.json';

export * from './api';

registerFunctions();
registerFunctions(fetch);
registerVersion(name, version);
18 changes: 10 additions & 8 deletions packages-exp/functions-exp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class FunctionsService implements _FirebaseService {
readonly app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>,
readonly region: string = DEFAULT_REGION
readonly region: string = DEFAULT_REGION,
readonly fetchImpl: typeof fetch
) {
this.contextProvider = new ContextProvider(authProvider, messagingProvider);
// Cancels all ongoing requests when resolved.
Expand Down Expand Up @@ -155,13 +156,14 @@ export function httpsCallable(
async function postJSON(
url: string,
body: unknown,
headers: Headers
headers: { [key: string]: string },
fetchImpl: typeof fetch
): Promise<HttpResponse> {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';

let response: Response;
try {
response = await fetch(url, {
response = await fetchImpl(url, {
method: 'POST',
body: JSON.stringify(body),
headers
Expand Down Expand Up @@ -206,20 +208,20 @@ async function call(
const body = { data };

// Add a header for the authToken.
const headers = new Headers();
const headers: { [key: string]: string } = {};
const context = await functionsInstance.contextProvider.getContext();
if (context.authToken) {
headers.append('Authorization', 'Bearer ' + context.authToken);
headers['Authorization'] = 'Bearer ' + context.authToken;
}
if (context.messagingToken) {
headers.append('Firebase-Instance-ID-Token', context.messagingToken);
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
}

// Default timeout to 70s, but let the options override it.
const timeout = options.timeout || 70000;

const response = await Promise.race([
postJSON(url, body, headers),
postJSON(url, body, headers, functionsInstance.fetchImpl),
failAfter(timeout),
functionsInstance.cancelAllRequests
]);
Expand Down
6 changes: 5 additions & 1 deletion packages-exp/functions-exp/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';
import { FunctionsService } from '../src/service';
import { useFunctionsEmulator } from '../src/api';
import nodeFetch from 'node-fetch';

export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
options = {
Expand Down Expand Up @@ -52,11 +53,14 @@ export function createTestService(
new ComponentContainer('test')
)
): FunctionsService {
const fetchImpl: typeof fetch =
typeof window !== 'undefined' ? fetch.bind(window) : (nodeFetch as any);
const functions = new FunctionsService(
app,
authProvider,
messagingProvider,
region
region,
fetchImpl
);
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
if (useEmulator) {
Expand Down
5 changes: 3 additions & 2 deletions packages/functions/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import firebase from '@firebase/app';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import { registerFunctions } from './src/config';
import 'isomorphic-fetch';
import nodeFetch from 'node-fetch';

import { name, version } from './package.json';

registerFunctions(firebase as _FirebaseNamespace);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerFunctions(firebase as _FirebaseNamespace, nodeFetch as any);
firebase.registerVersion(name, version, 'node');
2 changes: 1 addition & 1 deletion packages/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { registerFunctions } from './src/config';

import { name, version } from './package.json';

registerFunctions(firebase as _FirebaseNamespace);
registerFunctions(firebase as _FirebaseNamespace, fetch);
firebase.registerVersion(name, version);

declare module '@firebase/app-types' {
Expand Down
8 changes: 5 additions & 3 deletions packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"esm2017": "dist/index.esm2017.js",
"files": ["dist"],
"files": [
"dist"
],
"scripts": {
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
Expand Down Expand Up @@ -45,10 +47,10 @@
},
"typings": "dist/index.d.ts",
"dependencies": {
"@firebase/component": "0.1.19",
"@firebase/functions-types": "0.3.17",
"@firebase/messaging-types": "0.5.0",
"@firebase/component": "0.1.19",
"isomorphic-fetch": "2.2.1",
"node-fetch": "2.6.1",
"tslib": "^1.11.1"
},
"nyc": {
Expand Down
15 changes: 8 additions & 7 deletions packages/functions/src/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export class Service implements FirebaseFunctions, FirebaseService {
private app_: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>,
private region_: string = 'us-central1'
private region_: string = 'us-central1',
readonly fetchImpl: typeof fetch
) {
this.contextProvider = new ContextProvider(authProvider, messagingProvider);
// Cancels all ongoing requests when resolved.
Expand Down Expand Up @@ -162,13 +163,13 @@ export class Service implements FirebaseFunctions, FirebaseService {
private async postJSON(
url: string,
body: {},
headers: Headers
headers: { [key: string]: string }
): Promise<HttpResponse> {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';

let response: Response;
try {
response = await fetch(url, {
response = await this.fetchImpl(url, {
method: 'POST',
body: JSON.stringify(body),
headers
Expand Down Expand Up @@ -212,13 +213,13 @@ export class Service implements FirebaseFunctions, FirebaseService {
const body = { data };

// Add a header for the authToken.
const headers = new Headers();
const headers: { [key: string]: string } = {};
const context = await this.contextProvider.getContext();
if (context.authToken) {
headers.append('Authorization', 'Bearer ' + context.authToken);
headers['Authorization'] = 'Bearer ' + context.authToken;
}
if (context.instanceIdToken) {
headers.append('Firebase-Instance-ID-Token', context.instanceIdToken);
headers['Firebase-Instance-ID-Token'] = context.instanceIdToken;
}

// Default timeout to 70s, but let the options override it.
Expand Down
25 changes: 14 additions & 11 deletions packages/functions/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ import { _FirebaseNamespace } from '@firebase/app-types/private';
*/
const FUNCTIONS_TYPE = 'functions';

function factory(container: ComponentContainer, region?: string): Service {
// Dependencies
const app = container.getProvider('app').getImmediate();
const authProvider = container.getProvider('auth-internal');
const messagingProvider = container.getProvider('messaging');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Service(app, authProvider, messagingProvider, region);
}

export function registerFunctions(instance: _FirebaseNamespace): void {
export function registerFunctions(
instance: _FirebaseNamespace,
fetchImpl: typeof fetch
): void {
const namespaceExports = {
// no-inline
Functions: Service
};

function factory(container: ComponentContainer, region?: string): Service {
// Dependencies
const app = container.getProvider('app').getImmediate();
const authProvider = container.getProvider('auth-internal');
const messagingProvider = container.getProvider('messaging');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Service(app, authProvider, messagingProvider, region, fetchImpl);
}
instance.INTERNAL.registerComponent(
new Component(FUNCTIONS_TYPE, factory, ComponentType.PUBLIC)
.setServiceProps(namespaceExports)
Expand Down
11 changes: 10 additions & 1 deletion packages/functions/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Provider, ComponentContainer } from '@firebase/component';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';
import { Service } from '../src/api/service';
import nodeFetch from 'node-fetch';

export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
options = {
Expand Down Expand Up @@ -52,7 +53,15 @@ export function createTestService(
new ComponentContainer('test')
)
): Service {
const functions = new Service(app, authProvider, messagingProvider, region);
const fetchImpl: typeof fetch =
typeof window !== 'undefined' ? fetch.bind(window) : (nodeFetch as any);
const functions = new Service(
app,
authProvider,
messagingProvider,
region,
fetchImpl
);
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
if (useEmulator) {
functions.useFunctionsEmulator(
Expand Down
25 changes: 1 addition & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8934,7 +8934,7 @@ is-stream-ended@^0.1.4:
resolved "https://registry.npmjs.org/is-stream-ended/-/is-stream-ended-0.1.4.tgz#f50224e95e06bce0e356d440a4827cd35b267eda"
integrity sha512-xj0XPvmr7bQFTvirqnFr50o0hQIh6ZItDqloxt5aJrR4NQsYeSsyFQERYGCAzfindAcnKjINnwEEgLx4IqVzQw==

is-stream@^1.0.0, is-stream@^1.0.1, is-stream@^1.1.0:
is-stream@^1.0.0, is-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=
Expand Down Expand Up @@ -9075,14 +9075,6 @@ isobject@^3.0.0, isobject@^3.0.1:
resolved "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=

[email protected]:
version "2.2.1"
resolved "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
integrity sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=
dependencies:
node-fetch "^1.0.1"
whatwg-fetch ">=0.10.0"

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -11100,14 +11092,6 @@ [email protected], node-fetch@^2.2.0, node-fetch@^2.3.0, node-fetch@^2.5.0, node-
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-fetch@^1.0.1:
version "1.7.3"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
integrity sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==
dependencies:
encoding "^0.1.11"
is-stream "^1.0.1"

node-forge@^0.10.0:
version "0.10.0"
resolved "https://registry.npmjs.org/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
Expand Down Expand Up @@ -14361,7 +14345,6 @@ symbol-observable@^1.1.0:

"sync-promise@git+https://github.com/brettz9/sync-promise.git#full-sync-missing-promise-features":
version "1.0.1"
uid "25845a49a00aa2d2c985a5149b97c86a1fcdc75a"
resolved "git+https://github.com/brettz9/sync-promise.git#25845a49a00aa2d2c985a5149b97c86a1fcdc75a"

table@^5.2.3:
Expand Down Expand Up @@ -15670,7 +15653,6 @@ websocket-extensions@>=0.1.1:

"websql@git+https://github.com/brettz9/node-websql.git#configurable-secure2":
version "1.0.0"
uid "5149bc0763376ca757fc32dc74345ada0467bfbb"
resolved "git+https://github.com/brettz9/node-websql.git#5149bc0763376ca757fc32dc74345ada0467bfbb"
dependencies:
argsarray "^0.0.1"
Expand All @@ -15684,11 +15666,6 @@ [email protected]:
resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==

whatwg-fetch@>=0.10.0:
version "3.4.1"
resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.4.1.tgz#e5f871572d6879663fa5674c8f833f15a8425ab3"
integrity sha512-sofZVzE1wKwO+EYPbWfiwzaKovWiZXf4coEzjGP9b2GBVgQRLQUZ2QcuPpQExGDAW5GItpEm6Tl4OU5mywnAoQ==

whatwg-mimetype@^2.3.0:
version "2.3.0"
resolved "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
Expand Down