Skip to content

Commit b7f35a0

Browse files
authored
Initial Firebase Functions (#948)
1 parent 7e65885 commit b7f35a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3786
-6
lines changed

FirebaseFunctions.podspec

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Be sure to run `pod lib lint FirebaseFunctions.podspec' to ensure this is a
3+
# valid spec before submitting.
4+
#
5+
# Any lines starting with a # are optional, but their use is encouraged
6+
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
7+
#
8+
9+
Pod::Spec.new do |s|
10+
s.name = 'FirebaseFunctions'
11+
s.version = '1.0.0'
12+
s.summary = 'Cloud Functions for Firebase iOS SDK.'
13+
14+
s.description = <<-DESC
15+
iOS SDK for Cloud Functions for Firebase.
16+
DESC
17+
18+
s.homepage = 'https://developers.google.com/'
19+
s.authors = 'Google, Inc.'
20+
s.source = { :git => 'https://github.com/TBD/FirebaseFunctions.git', :tag => s.version.to_s }
21+
22+
s.ios.deployment_target = '8.0'
23+
24+
s.cocoapods_version = '>= 1.4.0'
25+
s.static_framework = true
26+
s.prefix_header_file = false
27+
28+
s.source_files = 'Functions/FirebaseFunctions/**/*'
29+
s.public_header_files = 'Functions/FirebaseFunctions/Public/*.h'
30+
31+
s.dependency 'FirebaseCore', '~> 4.0'
32+
s.dependency 'GTMSessionFetcher/Core', '~> 1.1'
33+
end

Functions/.clang-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BasedOnStyle: Google
2+
ColumnLimit: 100
3+
BinPackParameters: false
4+
AllowAllParametersOfDeclarationOnNextLine: true
5+
ObjCSpaceBeforeProtocolList: false
6+
SpacesInContainerLiterals: true
7+
PointerAlignment: Right

Functions/Backend/index.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const assert = require('assert');
2+
const functions = require('firebase-functions');
3+
4+
exports.dataTest = functions.https.onRequest((request, response) => {
5+
assert.deepEqual(request.body, {
6+
data: {
7+
bool: true,
8+
int: 2,
9+
long: {
10+
value: '3',
11+
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
12+
},
13+
string: 'four',
14+
array: [5, 6],
15+
'null': null,
16+
}
17+
});
18+
response.send({
19+
data: {
20+
message: 'stub response',
21+
code: 42,
22+
long: {
23+
value: '420',
24+
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
25+
},
26+
}
27+
});
28+
});
29+
30+
exports.scalarTest = functions.https.onRequest((request, response) => {
31+
assert.deepEqual(request.body, { data: 17 });
32+
response.send({ data: 76 });
33+
});
34+
35+
exports.tokenTest = functions.https.onRequest((request, response) => {
36+
assert.equal('Bearer token', request.get('Authorization'));
37+
assert.deepEqual(request.body, { data: {} });
38+
response.send({ data: {} });
39+
});
40+
41+
exports.instanceIdTest = functions.https.onRequest((request, response) => {
42+
assert.equal(request.get('Firebase-Instance-ID-Token'), 'iid');
43+
assert.deepEqual(request.body, { data: {} });
44+
response.send({ data: {} });
45+
});
46+
47+
exports.nullTest = functions.https.onRequest((request, response) => {
48+
assert.deepEqual(request.body, { data: null });
49+
response.send({ data: null });
50+
});
51+
52+
exports.missingResultTest = functions.https.onRequest((request, response) => {
53+
assert.deepEqual(request.body, { data: null });
54+
response.send({});
55+
});
56+
57+
exports.unhandledErrorTest = functions.https.onRequest((request, response) => {
58+
// Fail in a way that the client shouldn't see.
59+
throw 'nope';
60+
});
61+
62+
exports.unknownErrorTest = functions.https.onRequest((request, response) => {
63+
// Send an http error with a body with an explicit code.
64+
response.status(400).send({
65+
error: {
66+
status: 'THIS_IS_NOT_VALID',
67+
message: 'this should be ignored',
68+
},
69+
});
70+
});
71+
72+
exports.explicitErrorTest = functions.https.onRequest((request, response) => {
73+
// Send an http error with a body with an explicit code.
74+
// Note that eventually the SDK will have a helper to automatically return
75+
// the appropriate http status code for an error.
76+
response.status(400).send({
77+
error: {
78+
status: 'OUT_OF_RANGE',
79+
message: 'explicit nope',
80+
details: {
81+
start: 10,
82+
end: 20,
83+
long: {
84+
value: '30',
85+
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
86+
},
87+
},
88+
},
89+
});
90+
});
91+
92+
exports.httpErrorTest = functions.https.onRequest((request, response) => {
93+
// Send an http error with no body.
94+
response.status(400).send();
95+
});

Functions/Backend/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"dependencies": {
5+
"firebase-admin": "~4.2.1",
6+
"firebase-functions": "^0.5.7"
7+
},
8+
"private": true,
9+
"devDependencies": {
10+
"@google-cloud/functions-emulator": "^1.0.0-alpha.21"
11+
}
12+
}

Functions/Backend/start.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
#
3+
# Sets up a project with the functions CLI and starts a backend to run
4+
# integration tests against.
5+
6+
set -e
7+
8+
# Get the absolute path to the directory containing this script.
9+
SCRIPT_DIR="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)"
10+
TEMP_DIR="$(mktemp -d -t firebase-functions)"
11+
echo "Creating functions in ${TEMP_DIR}"
12+
13+
# Set up the functions directory.
14+
cp "${SCRIPT_DIR}/index.js" "${TEMP_DIR}/"
15+
cp "${SCRIPT_DIR}/package.json" "${TEMP_DIR}/"
16+
cd "${TEMP_DIR}"
17+
npm install
18+
19+
# Start the server.
20+
FUNCTIONS_BIN="./node_modules/.bin/functions"
21+
"${FUNCTIONS_BIN}" config set projectId functions-integration-test
22+
"${FUNCTIONS_BIN}" config set supervisorPort 5005
23+
"${FUNCTIONS_BIN}" config set region us-central1
24+
"${FUNCTIONS_BIN}" config set verbose true
25+
"${FUNCTIONS_BIN}" restart
26+
"${FUNCTIONS_BIN}" deploy dataTest --trigger-http
27+
"${FUNCTIONS_BIN}" deploy scalarTest --trigger-http
28+
"${FUNCTIONS_BIN}" deploy tokenTest --trigger-http
29+
"${FUNCTIONS_BIN}" deploy instanceIdTest --trigger-http
30+
"${FUNCTIONS_BIN}" deploy nullTest --trigger-http
31+
"${FUNCTIONS_BIN}" deploy missingResultTest --trigger-http
32+
"${FUNCTIONS_BIN}" deploy unhandledErrorTest --trigger-http
33+
"${FUNCTIONS_BIN}" deploy unknownErrorTest --trigger-http
34+
"${FUNCTIONS_BIN}" deploy explicitErrorTest --trigger-http
35+
"${FUNCTIONS_BIN}" deploy httpErrorTest --trigger-http
36+
37+
# Wait for the user to tell us to stop the server.
38+
echo "Functions emulator now running in ${TEMP_DIR}."
39+
read -n 1 -p "*** Press any key to stop the server. ***"
40+
echo "\nStopping the emulator..."
41+
"${FUNCTIONS_BIN}" stop

Functions/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# v1.0.0
2+
- Initial public release

0 commit comments

Comments
 (0)