Skip to content

Commit 0322c1b

Browse files
authored
Implement useEmulator for Functions (#3906)
1 parent 7156eb3 commit 0322c1b

File tree

9 files changed

+86
-20
lines changed

9 files changed

+86
-20
lines changed

.changeset/silver-books-reply.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'firebase': minor
3+
'@firebase/functions-exp': minor
4+
'@firebase/functions': minor
5+
'@firebase/functions-types': minor
6+
---
7+
8+
Add a useEmulator(host, port) method to Cloud Functions

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ export function getFunctions(
5656
}
5757

5858
/**
59-
* Changes this instance to point to a Cloud Functions emulator running
60-
* locally. See https://firebase.google.com/docs/functions/local-emulator
59+
* Modify this instance to communicate with the Cloud Functions emulator.
6160
*
62-
* @param origin - The origin of the local emulator, such as
63-
* "http://localhost:5005".
61+
* Note: this must be called before this instance has been used to do any operations.
62+
*
63+
* @param host The emulator host (ex: localhost)
64+
* @param port The emulator port (ex: 5001)
6465
* @public
6566
*/
6667
export function useFunctionsEmulator(
6768
functionsInstance: Functions,
68-
origin: string
69+
host: string,
70+
port: number
6971
): void {
70-
_useFunctionsEmulator(functionsInstance as FunctionsService, origin);
72+
_useFunctionsEmulator(functionsInstance as FunctionsService, host, port);
7173
}
7274

7375
/**

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Firebase Functions > Service', () => {
4141

4242
it('can use emulator', () => {
4343
service = createTestService(app);
44-
useFunctionsEmulator(service, 'http://localhost:5005');
44+
useFunctionsEmulator(service, 'localhost', 5005);
4545
assert.equal(
4646
service._url('foo'),
4747
'http://localhost:5005/my-project/us-central1/foo'
@@ -58,7 +58,7 @@ describe('Firebase Functions > Service', () => {
5858

5959
it('correctly sets region with emulator', () => {
6060
service = createTestService(app, 'my-region');
61-
useFunctionsEmulator(service, 'http://localhost:5005');
61+
useFunctionsEmulator(service, 'localhost', 5005);
6262
assert.equal(
6363
service._url('foo'),
6464
'http://localhost:5005/my-project/my-region/foo'
@@ -72,7 +72,7 @@ describe('Firebase Functions > Service', () => {
7272

7373
it('prefers emulator to custom domain', () => {
7474
const service = createTestService(app, 'https://mydomain.com');
75-
useFunctionsEmulator(service, 'http://localhost:5005');
75+
useFunctionsEmulator(service, 'localhost', 5005);
7676
assert.equal(
7777
service._url('foo'),
7878
'http://localhost:5005/my-project/us-central1/foo'

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,20 @@ export class FunctionsService implements _FirebaseService {
133133
}
134134

135135
/**
136-
* Changes this instance to point to a Cloud Functions emulator running
137-
* locally. See https://firebase.google.com/docs/functions/local-emulator
136+
* Modify this instance to communicate with the Cloud Functions emulator.
138137
*
139-
* @param origin - The origin of the local emulator, such as
140-
* "http://localhost:5005".
138+
* Note: this must be called before this instance has been used to do any operations.
139+
*
140+
* @param host The emulator host (ex: localhost)
141+
* @param port The emulator port (ex: 5001)
141142
* @public
142143
*/
143144
export function useFunctionsEmulator(
144145
functionsInstance: FunctionsService,
145-
origin: string
146+
host: string,
147+
port: number
146148
): void {
147-
functionsInstance.emulatorOrigin = origin;
149+
functionsInstance.emulatorOrigin = `http://${host}:${port}`;
148150
}
149151

150152
/**

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ export function createTestService(
6464
);
6565
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
6666
if (useEmulator) {
67+
const url = new URL(process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN!);
6768
useFunctionsEmulator(
6869
functions,
69-
process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN!
70-
);
70+
url.hostname,
71+
Number.parseInt(url.port, 10));
7172
}
7273
return functions;
7374
}

packages/firebase/index.d.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1799,10 +1799,22 @@ declare namespace firebase.functions {
17991799
*/
18001800
export class Functions {
18011801
private constructor();
1802+
1803+
/**
1804+
* Modify this instance to communicate with the Cloud Functions emulator.
1805+
*
1806+
* Note: this must be called before this instance has been used to do any operations.
1807+
*
1808+
* @param host The emulator host (ex: localhost)
1809+
* @param port The emulator port (ex: 5001)
1810+
*/
1811+
useEmulator(host: string, port: number): void;
1812+
18021813
/**
18031814
* Changes this instance to point to a Cloud Functions emulator running
18041815
* locally. See https://firebase.google.com/docs/functions/local-emulator
1805-
*
1816+
*
1817+
* @deprecated Prefer the useEmulator(host, port) method.
18061818
* @param origin The origin of the local emulator, such as
18071819
* "http://localhost:5005".
18081820
*/

packages/functions-types/index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ export class FirebaseFunctions {
5353
*/
5454
httpsCallable(name: string, options?: HttpsCallableOptions): HttpsCallable;
5555

56+
/**
57+
* Modify this instance to communicate with the Cloud Functions emulator.
58+
*
59+
* Note: this must be called before this instance has been used to do any operations.
60+
*
61+
* @param host The emulator host (ex: localhost)
62+
* @param port The emulator port (ex: 5001)
63+
*/
64+
useEmulator(host: string, port: number): void;
65+
5666
/**
5767
* Changes this instance to point to a Cloud Functions emulator running
5868
* locally. See https://firebase.google.com/docs/functions/local-emulator
5969
*
70+
* @deprecated Prefer the useEmulator(host, port) method.
6071
* @param origin The origin of the local emulator, such as
6172
* "http://localhost:5005".
6273
*/

packages/functions/src/api/service.ts

+13
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,23 @@ export class Service implements FirebaseFunctions, FirebaseService {
150150
return `https://${this.region}-${projectId}.cloudfunctions.net/${name}`;
151151
}
152152

153+
/**
154+
* Modify this instance to communicate with the Cloud Functions emulator.
155+
*
156+
* Note: this must be called before this instance has been used to do any operations.
157+
*
158+
* @param host The emulator host (ex: localhost)
159+
* @param port The emulator port (ex: 5001)
160+
*/
161+
useEmulator(host: string, port: number): void {
162+
this.emulatorOrigin = `http://${host}:${port}`;
163+
}
164+
153165
/**
154166
* Changes this instance to point to a Cloud Functions emulator running
155167
* locally. See https://firebase.google.com/docs/functions/local-emulator
156168
*
169+
* @deprecated Prefer the useEmulator(host, port) method.
157170
* @param origin The origin of the local emulator, such as
158171
* "http://localhost:5005".
159172
*/

packages/functions/test/service.test.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ describe('Firebase Functions > Service', () => {
3333
);
3434
});
3535

36-
it('can use emulator', () => {
36+
it('can use emulator (deprecated)', () => {
3737
service.useFunctionsEmulator('http://localhost:5005');
3838
assert.equal(
3939
service._url('foo'),
4040
'http://localhost:5005/my-project/us-central1/foo'
4141
);
4242
});
43+
44+
it('can use emulator', () => {
45+
service.useEmulator('localhost', 5005);
46+
assert.equal(
47+
service._url('foo'),
48+
'http://localhost:5005/my-project/us-central1/foo'
49+
);
50+
});
4351
});
4452

4553
describe('custom region/domain constructor', () => {
@@ -62,13 +70,22 @@ describe('Firebase Functions > Service', () => {
6270
assert.equal(service._url('foo'), 'https://mydomain.com/foo');
6371
});
6472

65-
it('prefers emulator to custom domain', () => {
73+
it('prefers emulator to custom domain (deprecated)', () => {
6674
const service = createTestService(app, 'https://mydomain.com');
6775
service.useFunctionsEmulator('http://localhost:5005');
6876
assert.equal(
6977
service._url('foo'),
7078
'http://localhost:5005/my-project/us-central1/foo'
7179
);
7280
});
81+
82+
it('prefers emulator to custom domain', () => {
83+
const service = createTestService(app, 'https://mydomain.com');
84+
service.useEmulator('localhost', 5005);
85+
assert.equal(
86+
service._url('foo'),
87+
'http://localhost:5005/my-project/us-central1/foo'
88+
);
89+
});
7390
});
7491
});

0 commit comments

Comments
 (0)