Skip to content
Merged
11 changes: 9 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

machine:
node:
version: 6.12.3
version: 6.14.1

# Use for broader build-related configuration
general:
Expand All @@ -35,21 +35,25 @@ dependencies:
- yarn install
- yarn run lint
- samples test install -l=functions/background
- samples test install -l=functions/concepts
- samples test install -l=functions/datastore
- samples test install -l=functions/errorreporting
- samples test install -l=functions/gcs
- samples test install -l=functions/datastore
- samples test install -l=functions/helloworld
- samples test install -l=functions/http
- samples test install -l=functions/imagemagick
- samples test install -l=functions/log
- samples test install -l=functions/ocr/app
- samples test install -l=functions/pubsub
- samples test install -l=functions/sendgrid
- samples test install -l=functions/slack
- samples test install -l=functions/spanner
- samples test install -l=functions/tips
- samples test install -l=functions/uuid
cache_directories:
- ~/.cache/yarn
- functions/background/node_modules
- functions/concepts/node_modules
- functions/datastore/node_modules
- functions/errorreporting/node_modules
- functions/gcs/node_modules
Expand All @@ -62,6 +66,7 @@ dependencies:
- functions/sendgrid/node_modules
- functions/slack/node_modules
- functions/spanner/node_modules
- functions/tips/node_modules
- functions/uuid/node_modules

# Run your tests
Expand All @@ -70,6 +75,7 @@ test:
- functions-emulator config set projectId $GCLOUD_PROJECT && functions-emulator start && cd functions/datastore && npm run system-test && functions-emulator stop
- functions-emulator config set projectId $GCLOUD_PROJECT && functions-emulator start && cd functions/helloworld && npm run test && functions-emulator stop
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/background/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/concepts/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/gcs/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/http/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/imagemagick/test/**/*.test.js'
Expand All @@ -79,6 +85,7 @@ test:
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/slack/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/spanner/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/uuid/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/tips/test/**/*.test.js'
post:
- nyc report --reporter=lcov > coverage.lcov && codecov || true
deployment:
Expand Down
175 changes: 175 additions & 0 deletions functions/concepts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* HTTP Cloud Function that demonstrates
* how to catch errors of different types.
*
* @param {Object} req Cloud Function request context.
* @param {Object} req.body Cloud Function request context body.
* @param {String} req.body.topic The Cloud Pub/Sub topic to publish to.
* @param {Object} res Cloud Function response context.
*/
exports.errorTypes = (req, res) => {
// [START functions_concepts_error_object]
try {
// Throw an Error object (to simulate a GCP API failure)
throw new Error('Error object!');
} catch (err) {
// err is already an Error object
console.error(err);
}
// [END functions_concepts_error_object]

const someCondition = !!req.body.throwAsString;

/* eslint-disable no-throw-literal */
// [START functions_concepts_error_unknown]
try {
// Throw an unknown error type
if (someCondition) {
throw 'Error string!';
} else {
throw new Error('Error object!');
}
} catch (err) {
// Determine the error type
if (err instanceof Error) {
console.error(err);
} else {
console.error(new Error(err));
}
}
// [END functions_concepts_error_unknown]
/* eslint-enable no-throw-literal */

res.end();
};

// [START functions_concepts_stateless]
// Global variable, but only shared within function instance.
let count = 0;

/**
* HTTP Cloud Function that counts how many times
* it is executed within a specific instance.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.executionCount = (req, res) => {
count++;

// Note: the total function invocation count across
// all instances may not be equal to this value!
res.send(`Instance execution count: ${count}`);
};
// [END functions_concepts_stateless]

// [START functions_concepts_after_response]
/**
* HTTP Cloud Function that may not completely
* execute due to early HTTP response
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.afterResponse = (req, res) => {
res.end();

// This statement may not execute
console.log('Function complete!');
};
// [END functions_concepts_after_response]

// [START functions_concepts_after_timeout]
/**
* HTTP Cloud Function that may not completely
* execute due to function execution timeout
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.afterTimeout = (req, res) => {
setTimeout(() => {
// May not execute if function's timeout is <2 minutes
console.log('Function running...');
res.end();
}, 120000); // 2 minute delay
};
// [END functions_concepts_after_timeout]

// [START functions_concepts_filesystem]
const fs = require('fs');

/**
* HTTP Cloud Function that lists files in the function directory
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.listFiles = (req, res) => {
fs.readdir(__dirname, (err, files) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
console.log('Files', files);
res.sendStatus(200);
}
});
};
// [END functions_concepts_filesystem]

// [START functions_concepts_modules]
const path = require('path');
const loadedModule = require(path.join(__dirname, 'loadable.js'));

/**
* HTTP Cloud Function that runs a function loaded from another Node.js file
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.runLoadedModule = (req, res) => {
console.log(`Loaded function from file ${loadedModule.getFileName()}`);
res.end();
};
// [END functions_concepts_modules]

// [START functions_concepts_requests]
const request = require('request');

/**
* HTTP Cloud Function that makes an HTTP request
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.makeRequest = (req, res) => {
// The URL to send the request to
const url = 'https://example.com';

request(url, (err, response) => {
if (!err && response.statusCode === 200) {
res.sendStatus(200);
} else {
res.sendStatus(500);
}
});
};
// [END functions_concepts_requests]
22 changes: 22 additions & 0 deletions functions/concepts/loadable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START functions_sample_module]
exports.getFileName = () => {
return __filename;
};
// [END functions_sample_module]
26 changes: 26 additions & 0 deletions functions/concepts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "nodejs-docs-samples-functions-concepts",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"lint": "repo-tools lint"
},
"dependencies": {
"request": "^2.85.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.2.5",
"ava": "^0.25.0",
"sinon": "^4.5.0",
"supertest": "^3.0.0"
}
}
46 changes: 46 additions & 0 deletions functions/concepts/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const sample = require(`../`);

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);

test(`should demonstrate error type behavior`, (t) => {
const objError = new Error('Error object!');
const strError = new Error('Error string!');

const req = { body:
{ throwAsString: true }
};
const res = { end: sinon.stub() };

// Test throwing both objects and strings
sample.errorTypes(req, res);
t.deepEqual(console.error.getCall(0).args, [objError]);
t.deepEqual(console.error.getCall(1).args, [strError]);

// Test throwing objects only
req.body.throwAsString = false;
sample.errorTypes(req, res);
t.deepEqual(console.error.getCall(2).args, [objError]);
t.deepEqual(console.error.getCall(3).args, [objError]);
});
16 changes: 16 additions & 0 deletions functions/helloworld/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
2 changes: 1 addition & 1 deletion functions/helloworld/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Google, Inc.
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
Loading