Skip to content

Commit 4c4b2d1

Browse files
authored
Remove isomorphic-fetch dependency (firebase#3782)
1 parent bd85ee7 commit 4c4b2d1

File tree

13 files changed

+84
-81
lines changed

13 files changed

+84
-81
lines changed

packages-exp/functions-exp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@firebase/functions-types-exp": "0.0.800",
5555
"@firebase/messaging-types": "0.5.0",
5656
"@firebase/util": "0.3.2",
57-
"isomorphic-fetch": "2.2.1",
57+
"node-fetch": "2.6.1",
5858
"tslib": "^1.11.1"
5959
},
6060
"nyc": {

packages-exp/functions-exp/src/config.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,31 @@ import { FUNCTIONS_TYPE } from './constants';
2727

2828
export const DEFAULT_REGION = 'us-central1';
2929

30-
const factory: InstanceFactory<'functions'> = (
31-
container: ComponentContainer,
32-
region?: string
33-
) => {
34-
// Dependencies
35-
const app = container.getProvider('app-exp').getImmediate();
36-
const authProvider = container.getProvider('auth-internal');
37-
const messagingProvider = container.getProvider('messaging');
30+
export function registerFunctions(fetchImpl: typeof fetch): void {
31+
const factory: InstanceFactory<'functions'> = (
32+
container: ComponentContainer,
33+
region?: string
34+
) => {
35+
// Dependencies
36+
const app = container.getProvider('app-exp').getImmediate();
37+
const authProvider = container.getProvider('auth-internal');
38+
const messagingProvider = container.getProvider('messaging');
3839

39-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40-
return new FunctionsService(app, authProvider, messagingProvider, region);
41-
};
42-
43-
export function registerFunctions(): void {
44-
const namespaceExports = {
45-
// no-inline
46-
Functions: FunctionsService
40+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41+
return new FunctionsService(
42+
app,
43+
authProvider,
44+
messagingProvider,
45+
region,
46+
fetchImpl
47+
);
4748
};
4849

4950
_registerComponent(
50-
new Component(FUNCTIONS_TYPE, factory, ComponentType.PUBLIC)
51-
.setServiceProps(namespaceExports)
52-
.setMultipleInstances(true)
51+
new Component(
52+
FUNCTIONS_TYPE,
53+
factory,
54+
ComponentType.PUBLIC
55+
).setMultipleInstances(true)
5356
);
5457
}

packages-exp/functions-exp/src/index.node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
*/
1717
import { registerVersion } from '@firebase/app-exp';
1818
import { registerFunctions } from './config';
19-
import 'isomorphic-fetch';
19+
import nodeFetch from 'node-fetch';
2020

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

2323
export * from './api';
2424

25-
registerFunctions();
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
registerFunctions(nodeFetch as any);
2627
registerVersion(name, version, 'node');

packages-exp/functions-exp/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ import { name, version } from '../package.json';
2121

2222
export * from './api';
2323

24-
registerFunctions();
24+
registerFunctions(fetch);
2525
registerVersion(name, version);

packages-exp/functions-exp/src/service.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export class FunctionsService implements _FirebaseService {
8484
readonly app: FirebaseApp,
8585
authProvider: Provider<FirebaseAuthInternalName>,
8686
messagingProvider: Provider<FirebaseMessagingName>,
87-
readonly region: string = DEFAULT_REGION
87+
readonly region: string = DEFAULT_REGION,
88+
readonly fetchImpl: typeof fetch
8889
) {
8990
this.contextProvider = new ContextProvider(authProvider, messagingProvider);
9091
// Cancels all ongoing requests when resolved.
@@ -155,13 +156,14 @@ export function httpsCallable(
155156
async function postJSON(
156157
url: string,
157158
body: unknown,
158-
headers: Headers
159+
headers: { [key: string]: string },
160+
fetchImpl: typeof fetch
159161
): Promise<HttpResponse> {
160-
headers.append('Content-Type', 'application/json');
162+
headers['Content-Type'] = 'application/json';
161163

162164
let response: Response;
163165
try {
164-
response = await fetch(url, {
166+
response = await fetchImpl(url, {
165167
method: 'POST',
166168
body: JSON.stringify(body),
167169
headers
@@ -206,20 +208,20 @@ async function call(
206208
const body = { data };
207209

208210
// Add a header for the authToken.
209-
const headers = new Headers();
211+
const headers: { [key: string]: string } = {};
210212
const context = await functionsInstance.contextProvider.getContext();
211213
if (context.authToken) {
212-
headers.append('Authorization', 'Bearer ' + context.authToken);
214+
headers['Authorization'] = 'Bearer ' + context.authToken;
213215
}
214216
if (context.messagingToken) {
215-
headers.append('Firebase-Instance-ID-Token', context.messagingToken);
217+
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
216218
}
217219

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

221223
const response = await Promise.race([
222-
postJSON(url, body, headers),
224+
postJSON(url, body, headers, functionsInstance.fetchImpl),
223225
failAfter(timeout),
224226
functionsInstance.cancelAllRequests
225227
]);

packages-exp/functions-exp/test/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2121
import { FirebaseMessagingName } from '@firebase/messaging-types';
2222
import { FunctionsService } from '../src/service';
2323
import { useFunctionsEmulator } from '../src/api';
24+
import nodeFetch from 'node-fetch';
2425

2526
export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
2627
options = {
@@ -52,11 +53,14 @@ export function createTestService(
5253
new ComponentContainer('test')
5354
)
5455
): FunctionsService {
56+
const fetchImpl: typeof fetch =
57+
typeof window !== 'undefined' ? fetch.bind(window) : (nodeFetch as any);
5558
const functions = new FunctionsService(
5659
app,
5760
authProvider,
5861
messagingProvider,
59-
region
62+
region,
63+
fetchImpl
6064
);
6165
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
6266
if (useEmulator) {

packages/functions/index.node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import firebase from '@firebase/app';
1818
import { _FirebaseNamespace } from '@firebase/app-types/private';
1919
import { registerFunctions } from './src/config';
20-
import 'isomorphic-fetch';
20+
import nodeFetch from 'node-fetch';
2121

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

24-
registerFunctions(firebase as _FirebaseNamespace);
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25+
registerFunctions(firebase as _FirebaseNamespace, nodeFetch as any);
2526
firebase.registerVersion(name, version, 'node');

packages/functions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { registerFunctions } from './src/config';
2121

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

24-
registerFunctions(firebase as _FirebaseNamespace);
24+
registerFunctions(firebase as _FirebaseNamespace, fetch);
2525
firebase.registerVersion(name, version);
2626

2727
declare module '@firebase/app-types' {

packages/functions/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"browser": "dist/index.cjs.js",
88
"module": "dist/index.esm.js",
99
"esm2017": "dist/index.esm2017.js",
10-
"files": ["dist"],
10+
"files": [
11+
"dist"
12+
],
1113
"scripts": {
1214
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1315
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
@@ -45,10 +47,10 @@
4547
},
4648
"typings": "dist/index.d.ts",
4749
"dependencies": {
50+
"@firebase/component": "0.1.19",
4851
"@firebase/functions-types": "0.3.17",
4952
"@firebase/messaging-types": "0.5.0",
50-
"@firebase/component": "0.1.19",
51-
"isomorphic-fetch": "2.2.1",
53+
"node-fetch": "2.6.1",
5254
"tslib": "^1.11.1"
5355
},
5456
"nyc": {

packages/functions/src/api/service.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export class Service implements FirebaseFunctions, FirebaseService {
9696
private app_: FirebaseApp,
9797
authProvider: Provider<FirebaseAuthInternalName>,
9898
messagingProvider: Provider<FirebaseMessagingName>,
99-
private region_: string = 'us-central1'
99+
private region_: string = 'us-central1',
100+
readonly fetchImpl: typeof fetch
100101
) {
101102
this.contextProvider = new ContextProvider(authProvider, messagingProvider);
102103
// Cancels all ongoing requests when resolved.
@@ -162,13 +163,13 @@ export class Service implements FirebaseFunctions, FirebaseService {
162163
private async postJSON(
163164
url: string,
164165
body: {},
165-
headers: Headers
166+
headers: { [key: string]: string }
166167
): Promise<HttpResponse> {
167-
headers.append('Content-Type', 'application/json');
168+
headers['Content-Type'] = 'application/json';
168169

169170
let response: Response;
170171
try {
171-
response = await fetch(url, {
172+
response = await this.fetchImpl(url, {
172173
method: 'POST',
173174
body: JSON.stringify(body),
174175
headers
@@ -212,13 +213,13 @@ export class Service implements FirebaseFunctions, FirebaseService {
212213
const body = { data };
213214

214215
// Add a header for the authToken.
215-
const headers = new Headers();
216+
const headers: { [key: string]: string } = {};
216217
const context = await this.contextProvider.getContext();
217218
if (context.authToken) {
218-
headers.append('Authorization', 'Bearer ' + context.authToken);
219+
headers['Authorization'] = 'Bearer ' + context.authToken;
219220
}
220221
if (context.instanceIdToken) {
221-
headers.append('Firebase-Instance-ID-Token', context.instanceIdToken);
222+
headers['Firebase-Instance-ID-Token'] = context.instanceIdToken;
222223
}
223224

224225
// Default timeout to 70s, but let the options override it.

0 commit comments

Comments
 (0)