Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@google-cloud/functions-framework
Advanced tools
FaaS (Function as a service) framework for writing portable Node.js functions
@google-cloud/functions-framework is a Node.js framework for writing, deploying, and running Google Cloud Functions locally. It allows developers to create HTTP functions and background functions that can be triggered by various Google Cloud services.
HTTP Function
This feature allows you to create an HTTP function that can be triggered by HTTP requests. The example code defines a simple HTTP function that responds with 'Hello, World!'.
const functions = require('@google-cloud/functions-framework');
functions.http('helloHttp', (req, res) => {
res.send('Hello, World!');
});
Background Function
This feature allows you to create a background function that can be triggered by events from Google Cloud services. The example code defines a background function that logs the received event.
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('helloBackground', (cloudEvent) => {
console.log('Received event:', cloudEvent);
});
Local Development
This feature allows you to run your Google Cloud Functions locally for development and testing. The example code shows how to set up a script in your package.json to start the functions framework targeting the 'helloHttp' function.
{
"scripts": {
"start": "functions-framework --target=helloHttp"
}
}
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Unlike @google-cloud/functions-framework, Express is not specifically designed for Google Cloud Functions but can be used to create HTTP servers and APIs.
The Serverless Framework is a toolkit for deploying and operating serverless architectures. It supports multiple cloud providers, including AWS, Azure, and Google Cloud. While @google-cloud/functions-framework is focused on Google Cloud Functions, Serverless Framework provides a more general solution for serverless deployments across different platforms.
Firebase Functions is a framework for deploying serverless functions on Firebase. It is tightly integrated with Firebase services and provides similar functionality to @google-cloud/functions-framework but is more focused on Firebase-specific use cases.
An open source FaaS (Function as a Service) framework based on Express for writing portable Node.js functions.
The Functions Framework lets you write lightweight functions that run in many different environments, including:
The framework allows you to go from:
/**
* Send "Hello, World!"
* @param req https://expressjs.com/en/api.html#req
* @param res https://expressjs.com/en/api.html#res
*/
exports.helloWorld = (req, res) => {
res.send('Hello, World!');
};
To:
curl http://my-url
# Output: Hello, World!
All without needing to worry about writing an HTTP server or complicated request handling logic.
Watch this video to learn more about the Node Functions Framework.
Add the Functions Framework to your package.json
file using npm
.
npm install @google-cloud/functions-framework
Create an index.js
file with the following contents:
exports.helloWorld = (req, res) => {
res.send('Hello, World');
};
Run the following command:
npx @google-cloud/functions-framework --target=helloWorld
Open http://localhost:8080/ in your browser and see Hello, World.
Create a package.json
file using npm init
:
npm init
Create an index.js
file with the following contents:
const functions = require('@google-cloud/functions-framework');
functions.http('helloWorld', (req, res) => {
res.send('Hello, World');
});
Now install the Functions Framework:
npm install @google-cloud/functions-framework
Add a start
script to package.json
, with configuration passed via
command-line arguments:
"scripts": {
"start": "functions-framework --target=helloWorld"
}
Use npm start
to start the built-in local development server:
npm start
...
Serving function...
Function: helloWorld
URL: http://localhost:8080/
Send requests to this function using curl
from another terminal window:
curl localhost:8080
# Output: Hello, World
Build a container from your function using the Functions buildpacks:
pack build \
--builder gcr.io/buildpacks/builder:v1 \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=helloWorld \
my-first-function
Start the built container:
docker run --rm -p 8080:8080 my-first-function
# Output: Serving function...
Send requests to this function using curl
from another terminal window:
curl localhost:8080
# Output: Hello, World!
The Node.JS runtime on Cloud Run functions utilizes the Node.JS Functions Framework. On Cloud Run functions, the Functions Framework is completely optional: if you don't add it to your package.json
, it will be
installed automatically. For
After you've written your function, you can simply deploy it from your local
machine using the gcloud
command-line tool.
Check out the Cloud Functions quickstart.
Cloud Run and Cloud Run for Anthos both implement the Knative Serving API. The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.
You can configure the Functions Framework using command-line flags or environment variables. If you specify both, the environment variable will be ignored.
Command-line flag | Environment variable | Description |
---|---|---|
--port | PORT | The port on which the Functions Framework listens for requests. Default: 8080 |
--target | FUNCTION_TARGET | The name of the exported function to be invoked in response to requests. Default: function |
--signature-type | FUNCTION_SIGNATURE_TYPE | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: http ; accepted values: http or event or cloudevent |
--source | FUNCTION_SOURCE | The path to the directory of your function. Default: cwd (the current working directory) |
--log-execution-id | LOG_EXECUTION_ID | Enables execution IDs in logs, either true or false . When not specified, default to disable. Requires Node.js 13.0.0 or later. |
You can set command-line flags in your package.json
via the start
script.
For example:
"scripts": {
"start": "functions-framework --target=helloWorld"
}
The Functions Framework can unmarshall incoming
Google Cloud Functions event payloads to data
and context
objects.
These will be passed as arguments to your function when it receives a request.
Note that your function must use the event
-style function signature:
exports.helloEvents = (data, context) => {
console.log(data);
console.log(context);
};
To enable automatic unmarshalling, set the function signature type to event
using a command-line flag or an environment variable. By default, the HTTP
signature will be used and automatic event unmarshalling will be disabled.
For more details on this signature type, check out the Google Cloud Functions documentation on background functions.
The Functions Framework can unmarshall incoming
CloudEvents payloads to a cloudevent
object.
It will be passed as an argument to your function when it receives a request.
Note that your function must use the cloudevent
-style function signature:
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('helloCloudEvents', (cloudevent) => {
console.log(cloudevent.specversion);
console.log(cloudevent.type);
console.log(cloudevent.source);
console.log(cloudevent.subject);
console.log(cloudevent.id);
console.log(cloudevent.time);
console.log(cloudevent.datacontenttype);
});
More advanced guides and docs can be found in the docs/
folder.
Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.
FAQs
FaaS (Function as a service) framework for writing portable Node.js functions
The npm package @google-cloud/functions-framework receives a total of 579,791 weekly downloads. As such, @google-cloud/functions-framework popularity was classified as popular.
We found that @google-cloud/functions-framework demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.