Skip to content

Commit ff852c5

Browse files
author
Lauren Long
committed
Merge branch 'master' of github.com:FirebasePrivate/firebase-functions
2 parents d9fc8a6 + 9df27f4 commit ff852c5

File tree

3 files changed

+131
-80
lines changed

3 files changed

+131
-80
lines changed

spec/providers/pubsub.spec.ts

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -311,45 +311,83 @@ describe('Pubsub Functions', () => {
311311

312312
describe('handler namespace', () => {
313313
describe('#onPublish', () => {
314-
it('should return an empty trigger', () => {
315-
const result = functions.handler.pubsub.topic.onPublish(() => null);
316-
expect(result.__trigger).to.deep.equal({});
317-
});
314+
describe('#topic', () => {
315+
it('should return an empty trigger', () => {
316+
const result = functions.handler.pubsub.topic.onPublish(() => null);
317+
expect(result.__trigger).to.deep.equal({});
318+
});
318319

319-
it('should properly handle a new-style event', () => {
320-
const raw = new Buffer('{"hello":"world"}', 'utf8').toString('base64');
321-
const event = {
322-
data: {
323-
data: raw,
324-
attributes: {
325-
foo: 'bar',
320+
it('should properly handle a new-style event', () => {
321+
const raw = new Buffer('{"hello":"world"}', 'utf8').toString(
322+
'base64'
323+
);
324+
const event = {
325+
data: {
326+
data: raw,
327+
attributes: {
328+
foo: 'bar',
329+
},
326330
},
327-
},
328-
context: {
329-
eventId: '70172329041928',
330-
timestamp: '2018-04-09T07:56:12.975Z',
331-
eventType: 'google.pubsub.topic.publish',
332-
resource: {
333-
service: 'pubsub.googleapis.com',
334-
name: 'projects/project1/topics/toppy',
331+
context: {
332+
eventId: '70172329041928',
333+
timestamp: '2018-04-09T07:56:12.975Z',
334+
eventType: 'google.pubsub.topic.publish',
335+
resource: {
336+
service: 'pubsub.googleapis.com',
337+
name: 'projects/project1/topics/toppy',
338+
},
335339
},
336-
},
337-
};
338-
339-
const result = functions.handler.pubsub.topic.onPublish((data) => {
340-
return {
341-
raw: data.data,
342-
json: data.json,
343-
attributes: data.attributes,
344340
};
345-
});
346341

347-
return expect(
348-
result(event.data, event.context)
349-
).to.eventually.deep.equal({
350-
raw,
351-
json: { hello: 'world' },
352-
attributes: { foo: 'bar' },
342+
const result = functions.handler.pubsub.topic.onPublish((data) => {
343+
return {
344+
raw: data.data,
345+
json: data.json,
346+
attributes: data.attributes,
347+
};
348+
});
349+
350+
return expect(
351+
result(event.data, event.context)
352+
).to.eventually.deep.equal({
353+
raw,
354+
json: { hello: 'world' },
355+
attributes: { foo: 'bar' },
356+
});
357+
});
358+
});
359+
describe('#schedule', () => {
360+
it('should return an empty trigger', () => {
361+
const result = functions.handler.pubsub.schedule.onRun(() => null);
362+
expect(result.__trigger).to.deep.equal({});
363+
});
364+
it('should return a handler with a proper event context', () => {
365+
const raw = new Buffer('{"hello":"world"}', 'utf8').toString(
366+
'base64'
367+
);
368+
const event = {
369+
data: {
370+
data: raw,
371+
attributes: {
372+
foo: 'bar',
373+
},
374+
},
375+
context: {
376+
eventId: '70172329041928',
377+
timestamp: '2018-04-09T07:56:12.975Z',
378+
eventType: 'google.pubsub.topic.publish',
379+
resource: {
380+
service: 'pubsub.googleapis.com',
381+
name: 'projects/project1/topics/toppy',
382+
},
383+
},
384+
};
385+
const result = functions.handler.pubsub.schedule.onRun(
386+
(context) => context.eventId
387+
);
388+
return expect(result(event.data, event.context)).to.eventually.equal(
389+
'70172329041928'
390+
);
353391
});
354392
});
355393
});

src/handler-builder.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ export class HandlerBuilder {
201201
get topic() {
202202
return new pubsub.TopicBuilder(() => null, {});
203203
},
204+
/**
205+
* Handle periodic events triggered by Cloud Scheduler.
206+
*/
207+
get schedule() {
208+
return new pubsub.ScheduleBuilder(() => null, {});
209+
},
204210
};
205211
}
206212

src/providers/pubsub.ts

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,52 +63,6 @@ export function _topicWithOptions(
6363
}, options);
6464
}
6565

66-
export function schedule(schedule: string): ScheduleBuilder {
67-
return _scheduleWithOptions(schedule, {});
68-
}
69-
70-
export class ScheduleBuilder {
71-
/** @hidden */
72-
constructor(private options: DeploymentOptions) {}
73-
74-
retryConfig(config: ScheduleRetryConfig): ScheduleBuilder {
75-
this.options.schedule.retryConfig = config;
76-
return this;
77-
}
78-
79-
timeZone(timeZone: string): ScheduleBuilder {
80-
this.options.schedule.timeZone = timeZone;
81-
return this;
82-
}
83-
84-
onRun(handler: (context: EventContext) => PromiseLike<any> | any) {
85-
const triggerResource = () => {
86-
if (!process.env.GCLOUD_PROJECT) {
87-
throw new Error('process.env.GCLOUD_PROJECT is not set.');
88-
}
89-
return `projects/${process.env.GCLOUD_PROJECT}/topics`; // The CLI will append the correct topic name based on region and function name
90-
};
91-
const cloudFunction = makeCloudFunction({
92-
contextOnlyHandler: handler,
93-
provider,
94-
service,
95-
triggerResource,
96-
eventType: 'topic.publish',
97-
options: this.options,
98-
labels: { 'deployment-scheduled': 'true' },
99-
});
100-
return cloudFunction;
101-
}
102-
}
103-
104-
/** @hidden */
105-
export function _scheduleWithOptions(
106-
schedule: string,
107-
options: DeploymentOptions
108-
): ScheduleBuilder {
109-
return new ScheduleBuilder({ ...options, schedule: { schedule } });
110-
}
111-
11266
/**
11367
* The Google Cloud Pub/Sub topic builder.
11468
*
@@ -144,6 +98,59 @@ export class TopicBuilder {
14498
}
14599
}
146100

101+
export function schedule(schedule: string): ScheduleBuilder {
102+
return _scheduleWithOptions(schedule, {});
103+
}
104+
105+
/** @hidden */
106+
export function _scheduleWithOptions(
107+
schedule: string,
108+
options: DeploymentOptions
109+
): ScheduleBuilder {
110+
const triggerResource = () => {
111+
if (!process.env.GCLOUD_PROJECT) {
112+
throw new Error('process.env.GCLOUD_PROJECT is not set.');
113+
}
114+
// The CLI will append the correct topic name based on region and function name
115+
return `projects/${process.env.GCLOUD_PROJECT}/topics`;
116+
};
117+
return new ScheduleBuilder(triggerResource, {
118+
...options,
119+
schedule: { schedule },
120+
});
121+
}
122+
123+
export class ScheduleBuilder {
124+
/** @hidden */
125+
constructor(
126+
private triggerResource: () => string,
127+
private options: DeploymentOptions
128+
) {}
129+
130+
retryConfig(config: ScheduleRetryConfig): ScheduleBuilder {
131+
this.options.schedule.retryConfig = config;
132+
return this;
133+
}
134+
135+
timeZone(timeZone: string): ScheduleBuilder {
136+
this.options.schedule.timeZone = timeZone;
137+
return this;
138+
}
139+
140+
onRun(handler: (context: EventContext) => PromiseLike<any> | any) {
141+
const cloudFunction = makeCloudFunction({
142+
contextOnlyHandler: handler,
143+
provider,
144+
service,
145+
triggerResource: this.triggerResource,
146+
eventType: 'topic.publish',
147+
options: this.options,
148+
labels: { 'deployment-scheduled': 'true' },
149+
});
150+
return cloudFunction;
151+
}
152+
}
153+
147154
/**
148155
* Interface representing a Google Cloud Pub/Sub message.
149156
*

0 commit comments

Comments
 (0)