Skip to content

Commit 4c4b2d1

Browse files
authored
Remove isomorphic-fetch dependency (#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.

packages/functions/src/config.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,24 @@ import { _FirebaseNamespace } from '@firebase/app-types/private';
2828
*/
2929
const FUNCTIONS_TYPE = 'functions';
3030

31-
function factory(container: ComponentContainer, region?: string): Service {
32-
// Dependencies
33-
const app = container.getProvider('app').getImmediate();
34-
const authProvider = container.getProvider('auth-internal');
35-
const messagingProvider = container.getProvider('messaging');
36-
37-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
38-
return new Service(app, authProvider, messagingProvider, region);
39-
}
40-
41-
export function registerFunctions(instance: _FirebaseNamespace): void {
31+
export function registerFunctions(
32+
instance: _FirebaseNamespace,
33+
fetchImpl: typeof fetch
34+
): void {
4235
const namespaceExports = {
4336
// no-inline
4437
Functions: Service
4538
};
39+
40+
function factory(container: ComponentContainer, region?: string): Service {
41+
// Dependencies
42+
const app = container.getProvider('app').getImmediate();
43+
const authProvider = container.getProvider('auth-internal');
44+
const messagingProvider = container.getProvider('messaging');
45+
46+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
return new Service(app, authProvider, messagingProvider, region, fetchImpl);
48+
}
4649
instance.INTERNAL.registerComponent(
4750
new Component(FUNCTIONS_TYPE, factory, ComponentType.PUBLIC)
4851
.setServiceProps(namespaceExports)

packages/functions/test/utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Provider, ComponentContainer } from '@firebase/component';
2020
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2121
import { FirebaseMessagingName } from '@firebase/messaging-types';
2222
import { Service } from '../src/api/service';
23+
import nodeFetch from 'node-fetch';
2324

2425
export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
2526
options = {
@@ -52,7 +53,15 @@ export function createTestService(
5253
new ComponentContainer('test')
5354
)
5455
): Service {
55-
const functions = new Service(app, authProvider, messagingProvider, region);
56+
const fetchImpl: typeof fetch =
57+
typeof window !== 'undefined' ? fetch.bind(window) : (nodeFetch as any);
58+
const functions = new Service(
59+
app,
60+
authProvider,
61+
messagingProvider,
62+
region,
63+
fetchImpl
64+
);
5665
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
5766
if (useEmulator) {
5867
functions.useFunctionsEmulator(

yarn.lock

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8934,7 +8934,7 @@ is-stream-ended@^0.1.4:
89348934
resolved "https://registry.npmjs.org/is-stream-ended/-/is-stream-ended-0.1.4.tgz#f50224e95e06bce0e356d440a4827cd35b267eda"
89358935
integrity sha512-xj0XPvmr7bQFTvirqnFr50o0hQIh6ZItDqloxt5aJrR4NQsYeSsyFQERYGCAzfindAcnKjINnwEEgLx4IqVzQw==
89368936

8937-
is-stream@^1.0.0, is-stream@^1.0.1, is-stream@^1.1.0:
8937+
is-stream@^1.0.0, is-stream@^1.1.0:
89388938
version "1.1.0"
89398939
resolved "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
89408940
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=
@@ -9075,14 +9075,6 @@ isobject@^3.0.0, isobject@^3.0.1:
90759075
resolved "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
90769076
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=
90779077

9078-
9079-
version "2.2.1"
9080-
resolved "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
9081-
integrity sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=
9082-
dependencies:
9083-
node-fetch "^1.0.1"
9084-
whatwg-fetch ">=0.10.0"
9085-
90869078
isstream@~0.1.2:
90879079
version "0.1.2"
90889080
resolved "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
@@ -11100,14 +11092,6 @@ [email protected], node-fetch@^2.2.0, node-fetch@^2.3.0, node-fetch@^2.5.0, node-
1110011092
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
1110111093
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
1110211094

11103-
node-fetch@^1.0.1:
11104-
version "1.7.3"
11105-
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
11106-
integrity sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==
11107-
dependencies:
11108-
encoding "^0.1.11"
11109-
is-stream "^1.0.1"
11110-
1111111095
node-forge@^0.10.0:
1111211096
version "0.10.0"
1111311097
resolved "https://registry.npmjs.org/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
@@ -14361,7 +14345,6 @@ symbol-observable@^1.1.0:
1436114345

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

1436714350
table@^5.2.3:
@@ -15670,7 +15653,6 @@ websocket-extensions@>=0.1.1:
1567015653

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

15687-
whatwg-fetch@>=0.10.0:
15688-
version "3.4.1"
15689-
resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.4.1.tgz#e5f871572d6879663fa5674c8f833f15a8425ab3"
15690-
integrity sha512-sofZVzE1wKwO+EYPbWfiwzaKovWiZXf4coEzjGP9b2GBVgQRLQUZ2QcuPpQExGDAW5GItpEm6Tl4OU5mywnAoQ==
15691-
1569215669
whatwg-mimetype@^2.3.0:
1569315670
version "2.3.0"
1569415671
resolved "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"

0 commit comments

Comments
 (0)