Skip to content

Commit d7c09b7

Browse files
authored
Allow functions to deploy to multiple regions (firebase#375)
* Reverts 3d2adbe, allows region() to accept multiple regions * switch to loadash difference * adds ` * changelog
1 parent 9325a68 commit d7c09b7

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
feature - support for multiple regions on functions by passing extra region strings to functions.regions()
12
fixed - validation on instance name for realtime database triggers

spec/function-builder.spec.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ describe('FunctionBuilder', () => {
4242
expect(fn.__trigger.regions).to.deep.equal(['us-east1']);
4343
});
4444

45-
// it('should allow multiple regions to be set', () => {
46-
// let fn = functions
47-
// .region('my-region', 'my-other-region')
48-
// .auth.user()
49-
// .onCreate(user => user);
50-
51-
// expect(fn.__trigger.regions).to.deep.equal([
52-
// 'my-region',
53-
// 'my-other-region',
54-
// ]);
55-
// });
45+
it('should allow multiple regions to be set', () => {
46+
let fn = functions
47+
.region('us-east1', 'us-central1')
48+
.auth.user()
49+
.onCreate(user => user);
50+
51+
expect(fn.__trigger.regions).to.deep.equal([
52+
'us-east1',
53+
'us-central1',
54+
]);
55+
});
5656

5757
it('should allow runtime options to be set', () => {
5858
let fn = functions
@@ -135,6 +135,28 @@ describe('FunctionBuilder', () => {
135135
timeoutSeconds: 500,
136136
} as any);
137137
}).to.throw(Error);
138+
139+
expect(() => {
140+
return functions.region('unsupported', 'us-east1');
141+
}).to.throw(Error);
142+
143+
expect(() => {
144+
return functions.region('unsupported', 'us-east1').runWith({
145+
timeoutSeconds: 500,
146+
} as any);
147+
}).to.throw(Error);
148+
});
149+
150+
it('should throw an error if user chooses no region when using .region()', () => {
151+
expect(() => {
152+
return functions.region();
153+
}).to.throw(Error);
154+
155+
expect(() => {
156+
return functions.region().runWith({
157+
timeoutSeconds: 500,
158+
} as any);
159+
}).to.throw(Error);
138160
});
139161

140162
});

src/function-builder.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,27 @@ import { CloudFunction, EventContext, Runnable, TriggerAnnotated } from './cloud
3636

3737
/**
3838
* Configure the regions that the function is deployed to.
39-
* @param region Region string.
40-
* For example: `functions.region('us-east1')`
39+
* @param regions One of more region strings.
40+
* For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
4141
*/
42-
export function region(region: string) {
42+
export function region(...regions: string[]) {
43+
if (!regions.length) {
44+
throw new Error(
45+
"You must specify at least one region"
46+
);
47+
}
4348
if (
44-
!_.includes(
45-
['us-central1', 'us-east1', 'europe-west1', 'asia-northeast1'],
46-
region
47-
)
49+
_.difference(
50+
regions,
51+
['us-central1', 'us-east1', 'europe-west1', 'asia-northeast1']
52+
).length
4853
) {
4954
throw new Error(
5055
"The only valid regions are 'us-central1', 'us-east1', 'europe-west1', and 'asia-northeast1'"
5156
);
5257
}
53-
return new FunctionBuilder({ regions: [region] });
58+
return new FunctionBuilder({ regions });
5459
}
55-
5660
/**
5761
* Configure runtime options for the function.
5862
* @param runtimeOptions Object with 2 optional fields:
@@ -96,11 +100,11 @@ export class FunctionBuilder {
96100

97101
/**
98102
* Configure the regions that the function is deployed to.
99-
* @param region Region string.
100-
* For example: `functions.region('us-east1')`
103+
* @param regions One or more region strings.
104+
* For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
101105
*/
102-
region = (region: string) => {
103-
this.options.regions = [region];
106+
region = (...regions: string[]) => {
107+
this.options.regions = regions;
104108
return this;
105109
};
106110

0 commit comments

Comments
 (0)