Skip to content

Commit 65047d0

Browse files
committed
Support Node 8 Google Cloud Functions Runtime (firebase#203)
1 parent 01665ef commit 65047d0

File tree

10 files changed

+277
-141
lines changed

10 files changed

+277
-141
lines changed

integration_test/functions/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
"build": "./node_modules/.bin/tsc"
66
},
77
"dependencies": {
8-
"@google-cloud/pubsub": "^0.6.0",
9-
"@types/lodash": "^4.14.41",
10-
"firebase": "^4.9.1",
8+
"@google-cloud/pubsub": "~0.19.0",
9+
"@types/google-cloud__pubsub": "^0.18.0",
10+
"@types/lodash": "~4.14.41",
11+
"firebase": "~5.2.0",
1112
"firebase-admin": "~5.12.1",
1213
"firebase-functions": "./firebase-functions.tgz",
13-
"lodash": "^4.17.2"
14+
"lodash": "~4.17.2"
1415
},
1516
"main": "lib/index.js",
1617
"devDependencies": {
1718
"typescript": "~2.8.3"
1819
},
20+
"engines": {
21+
"node": "8"
22+
},
1923
"private": true
2024
}

integration_test/functions/src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import * as https from 'https';
44
import * as admin from 'firebase-admin';
55
import { Request, Response } from 'express';
66

7+
import * as PubSub from '@google-cloud/pubsub';
8+
const pubsub = PubSub();
9+
710
export * from './pubsub-tests';
811
export * from './database-tests';
912
export * from './auth-tests';
@@ -14,7 +17,6 @@ const numTests = Object.keys(exports).length; // Assumption: every exported func
1417
import 'firebase-functions'; // temporary shim until process.env.FIREBASE_CONFIG available natively in GCF(BUG 63586213)
1518
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
1619
firebase.initializeApp(firebaseConfig);
17-
console.log('initializing admin');
1820
admin.initializeApp();
1921

2022
// TODO(klimt): Get rid of this once the JS client SDK supports callable triggers.
@@ -45,22 +47,22 @@ function callHttpsTrigger(name: string, data: any) {
4547

4648
export const integrationTests: any = functions.https.onRequest(
4749
(req: Request, resp: Response) => {
48-
let pubsub: any = require('@google-cloud/pubsub')();
49-
5050
const testId = firebase
5151
.database()
5252
.ref()
5353
.push().key;
5454
return Promise.all([
5555
// A database write to trigger the Firebase Realtime Database tests.
56-
// The database write happens without admin privileges, so that the triggered function's "event.data.ref" also
57-
// doesn't have admin privileges.
56+
// The database write happens without admin privileges.
5857
firebase
5958
.database()
6059
.ref(`dbTests/${testId}/start`)
6160
.set({ '.sv': 'timestamp' }),
6261
// A Pub/Sub publish to trigger the Cloud Pub/Sub tests.
63-
pubsub.topic('pubsubTests').publish({ testId }),
62+
pubsub
63+
.topic('pubsubTests')
64+
.publisher()
65+
.publish(Buffer.from(JSON.stringify({ testId }))),
6466
// A user creation to trigger the Firebase Auth user creation tests.
6567
admin
6668
.auth()

integration_test/functions/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"lib": ["es6"],
3+
"lib": ["es6", "dom"],
44
"module": "commonjs",
55
"target": "es6",
66
"noImplicitAny": false,

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
"build": "node_modules/.bin/tsc -p tsconfig.release.json",
88
"build:pack": "rm -rf lib && npm install && node_modules/.bin/tsc -p tsconfig.release.json && npm pack",
99
"build:release": "npm install --production && npm install typescript firebase-admin && node_modules/.bin/tsc -p tsconfig.release.json",
10-
"lint": "node_modules/.bin/tslint src/{**/*,*}.ts spec/{**/*,*}.ts integration_test/functions/src/{**/*,*}.ts",
1110
"format": "prettier --write '**/*.ts'",
1211
"pretest": "node_modules/.bin/tsc && cp -r spec/fixtures .tmp/spec",
1312
"test": "npm run mocha",
1413
"mocha": "mocha .tmp/spec/index.spec.js",
15-
"posttest": "npm run lint && rm -rf .tmp",
14+
"posttest": "npm run format && rm -rf .tmp",
1615
"postinstall": "node ./upgrade-warning"
1716
},
1817
"repository": {
@@ -48,8 +47,7 @@
4847
"nock": "^9.0.0",
4948
"prettier": "^1.13.7",
5049
"sinon": "^1.17.4",
51-
"typescript": "~2.8.3",
52-
"tslint": "^3.15.1"
50+
"typescript": "~2.8.3"
5351
},
5452
"peerDependencies": {
5553
"firebase-admin": "~5.12.1"

spec/cloud-functions.spec.ts

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ describe('makeCloudFunction', () => {
3838
service: 'service',
3939
triggerResource: () => 'resource',
4040
handler: () => null,
41+
legacyEventType: 'providers/provider/eventTypes/event',
4142
};
4243

4344
it('should put a __trigger on the returned CloudFunction', () => {
44-
let cf = makeCloudFunction(cloudFunctionArgs);
45+
let cf = makeCloudFunction({
46+
provider: 'mock.provider',
47+
eventType: 'mock.event',
48+
service: 'service',
49+
triggerResource: () => 'resource',
50+
handler: () => null,
51+
});
4552
expect(cf.__trigger).to.deep.equal({
4653
eventTrigger: {
4754
eventType: 'mock.provider.mock.event',
@@ -51,6 +58,17 @@ describe('makeCloudFunction', () => {
5158
});
5259
});
5360

61+
it('should have legacy event type in __trigger if provided', () => {
62+
let cf = makeCloudFunction(cloudFunctionArgs);
63+
expect(cf.__trigger).to.deep.equal({
64+
eventTrigger: {
65+
eventType: 'providers/provider/eventTypes/event',
66+
resource: 'resource',
67+
service: 'service',
68+
},
69+
});
70+
});
71+
5472
it('should construct the right context for legacy event format', () => {
5573
let args: any = _.assign({}, cloudFunctionArgs, {
5674
handler: (data: any, context: EventContext) => context,
@@ -105,6 +123,39 @@ describe('makeCloudFunction', () => {
105123
params: {},
106124
});
107125
});
126+
127+
it('should handle Node 8 function signature', () => {
128+
let args: any = _.assign({}, cloudFunctionArgs, {
129+
handler: (data: any, context: EventContext) => {
130+
return { data, context };
131+
},
132+
});
133+
let cf = makeCloudFunction(args);
134+
let testContext = {
135+
eventId: '00000',
136+
timestamp: '2016-11-04T21:29:03.496Z',
137+
eventType: 'provider.event',
138+
resource: {
139+
service: 'provider',
140+
name: 'resource',
141+
},
142+
};
143+
let testData = 'data';
144+
145+
return expect(cf(testData, testContext)).to.eventually.deep.equal({
146+
data: 'data',
147+
context: {
148+
eventId: '00000',
149+
timestamp: '2016-11-04T21:29:03.496Z',
150+
eventType: 'provider.event',
151+
resource: {
152+
service: 'provider',
153+
name: 'resource',
154+
},
155+
params: {},
156+
},
157+
});
158+
});
108159
});
109160

110161
describe('makeParams', () => {
@@ -114,13 +165,14 @@ describe('makeParams', () => {
114165
service: 'service',
115166
triggerResource: () => 'projects/_/instances/pid/ref/{foo}/nested/{bar}',
116167
handler: (data, context) => context.params,
168+
legacyEventType: 'legacyEvent',
117169
};
118170
const cf = makeCloudFunction(args);
119171

120172
it('should construct params from the event resource of legacy events', () => {
121173
const testEvent: LegacyEvent = {
122174
resource: 'projects/_/instances/pid/ref/a/nested/b',
123-
eventType: 'event',
175+
eventType: 'legacyEvent',
124176
data: 'data',
125177
};
126178

@@ -160,14 +212,14 @@ describe('makeAuth and makeAuthType', () => {
160212
handler: (data, context) => {
161213
return {
162214
auth: context.auth,
163-
authMode: context.authType,
215+
authType: context.authType,
164216
};
165217
},
166218
};
167219
let cf = makeCloudFunction(args);
168220

169221
it('should construct correct auth and authType for admin user', () => {
170-
const testEvent: LegacyEvent = {
222+
const testEvent = {
171223
data: 'data',
172224
auth: {
173225
admin: true,
@@ -176,12 +228,12 @@ describe('makeAuth and makeAuthType', () => {
176228

177229
return expect(cf(testEvent)).to.eventually.deep.equal({
178230
auth: undefined,
179-
authMode: 'ADMIN',
231+
authType: 'ADMIN',
180232
});
181233
});
182234

183235
it('should construct correct auth and authType for unauthenticated user', () => {
184-
const testEvent: LegacyEvent = {
236+
const testEvent = {
185237
data: 'data',
186238
auth: {
187239
admin: false,
@@ -190,7 +242,7 @@ describe('makeAuth and makeAuthType', () => {
190242

191243
return expect(cf(testEvent)).to.eventually.deep.equal({
192244
auth: null,
193-
authMode: 'UNAUTHENTICATED',
245+
authType: 'UNAUTHENTICATED',
194246
});
195247
});
196248

@@ -216,7 +268,7 @@ describe('makeAuth and makeAuthType', () => {
216268
sub: 'user',
217269
},
218270
},
219-
authMode: 'USER',
271+
authType: 'USER',
220272
});
221273
});
222274
});

0 commit comments

Comments
 (0)