Detalhes do pacote

@google-cloud/functions-framework

GoogleCloudPlatform3.7mApache-2.04.0.0

FaaS (Function as a service) framework for writing portable Node.js functions

readme (leia-me)

Functions Framework for Node.js

npm version npm downloads

Node unit CI Node lint CI Node conformace CI Security Scorecard

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.

Features

  • Spin up a local development server for quick testing
  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Portable between serverless platforms

Installation

Add the Functions Framework to your package.json file using npm.

npm install @google-cloud/functions-framework

Quickstarts

Quickstart: Hello, World on your local machine

  1. Create an index.js file with the following contents:

     exports.helloWorld = (req, res) => {
       res.send('Hello, World');
     };
  2. Run the following command:

     npx @google-cloud/functions-framework --target=helloWorld
  3. Open http://localhost:8080/ in your browser and see Hello, World.

Quickstart: Set up a new project

  1. Create a package.json file using npm init:

     npm init
  2. 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');
     });
  3. Now install the Functions Framework:

     npm install @google-cloud/functions-framework
  4. Add a start script to package.json, with configuration passed via command-line arguments:

       "scripts": {
         "start": "functions-framework --target=helloWorld"
       }
  5. Use npm start to start the built-in local development server:

     npm start
     ...
     Serving function...
     Function: helloWorld
     URL: http://localhost:8080/
  6. Send requests to this function using curl from another terminal window:

     curl localhost:8080
     # Output: Hello, World

Quickstart: Build a Deployable Container

  1. Install Docker and the pack tool.

  2. 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
  3. Start the built container:

     docker run --rm -p 8080:8080 my-first-function
     # Output: Serving function...
  4. Send requests to this function using curl from another terminal window:

     curl localhost:8080
     # Output: Hello, World!

Run your function on serverless platforms

Google Cloud Run functions

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.

Container environments based on KNative

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.

Configure the Functions Framework

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.
--ignored-routes IGNORED_ROUTES A route expression for requests that should not be routed the function. An empty 404 response will be returned. This is set to `/favicon.ico /robots.txtby default forhttp` functions.

You can set command-line flags in your package.json via the start script. For example:

  "scripts": {
    "start": "functions-framework --target=helloWorld"
  }

Enable Google Cloud Run functions Events

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.

Enable CloudEvents

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);
});

Advanced Docs

More advanced guides and docs can be found in the docs/ folder.

Contributing

Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.

changelog (log de mudanças)

Changelog

npm history

4.0.0 (2025-04-28)

⚠ BREAKING CHANGES

  • upgrade all dependencies
  • delete the undocumented "typed" function signature
  • drop support for old nodejs versions

Bug Fixes

  • delete the undocumented "typed" function signature (c0714e7)

Miscellaneous Chores

  • drop support for old nodejs versions (c0714e7)
  • upgrade all dependencies (c0714e7)

3.5.1 (2025-03-27)

Bug Fixes

  • correct handling of IGNORED_ROUTES set to empty (4799207)

3.5.0 (2025-03-27)

Features

  • adds a new ignored-routes config option (70f68e9)

Bug Fixes

  • deps: downgrade @types/express to v4 (d40a514)

3.4.6 (2025-03-04)

Bug Fixes

3.4.5 (2024-12-26)

Bug Fixes

  • fix trace context pattern, remove trace id and respect logging span id field. (#667) (0fb00a5)

3.4.4 (2024-12-19)

Bug Fixes

  • do not call render in the errorHandler (#665) (9206893)

3.4.3 (2024-12-12)

Bug Fixes

  • Fix typings for functions.cloudEvent to include callback. (#631) (47cd4c6)

3.4.2 (2024-07-22)

Bug Fixes

  • Don't set severity level for text log. (#625) (5bd82de)

3.4.1 (2024-07-09)

Bug Fixes

  • if execution id support requested for node 12, fail open and drop execution id support instead of crashing out (#618) (47003fd)
  • parse structured logs, and handle ANSI escape codes in logs (#620) (33a7266)

3.4.0 (2024-04-29)

Features

Bug Fixes

  • etag should not be set on any response (#571) (89a3abb)
  • framework fails to shutdown gracefully when exit code is a string (#545) (5355069)

3.3.0 (2023-06-28)

Features

  • introduce strongly typed function signature (#525) (65f3f34)

3.2.1 (2023-06-20)

Bug Fixes

  • module resolution returns early and accurate error messages on failure (#534) (51f20a4)

3.2.0 (2023-04-07)

Features

  • configure security score card action (#509) (1e63fd0)

Bug Fixes

  • make renovatebot's ignorePath and grouping of non-major updates work correctly (#505) (e4f3c57)
  • remove default fallback when target function does not exist (#497) (f3325d3)

3.1.3 (2022-11-17)

Bug Fixes

  • deps: bump minimist from 1.2.5 to 1.2.7 (#486) (df1e2ed)

3.1.2 (2022-06-06)

Bug Fixes

3.1.1 (2022-04-22)

Bug Fixes

  • deps: Move @types/express to dependencies (#457) (1682e07)
  • deps: update dependency cloudevents to v6 (#451) (33d6a5b)

3.1.0 (2022-03-31)

Features

  • Disable Express eTag response header for consistent FF behaviour across FF runtimes (#439) (07668f6)

Bug Fixes

  • deps: update dependency minimist to v1.2.6 [security] (#432) (32bb723)

3.0.0 (2022-02-15)

⚠ BREAKING CHANGES

  • send correct response code on exception (#427)

Bug Fixes

  • send correct response code on exception (#427) (5d996fe)

2.1.1 (2022-02-01)

Bug Fixes

2.1.0 (2021-12-07)

Features

  • add testing helpers and instructions (#392) (9c25913)
  • update to latest version of clouevents sdk (#402) (bd36243)

2.0.0 (2021-11-10)

⚠ BREAKING CHANGES

  • remove eventarc cloudevent types (#385)
  • update the build to use api-extractor types (#383)
  • use snake case for generated cloudevent files (#382)
  • rename cloudevent to CloudEvent (#379)
  • Add type annotation for Request.rawBody
  • move function wrapping earlier (#335)
  • This commit refactors the SignatureType enum into an array of strings declared as const. This allows the SignatureType type to be expressed as a union type, which works a bit better when parsing a users provided string.

Features

  • add google CloudEvent types (#376) (292ade9)
  • enable traceparent header for cloudevents (#336) (aba9cb4)
  • generate cloudevent types from googleapis/google-cloudevents (#371) (7617801)
  • introduce declarative function signatures (#347) (db1ba9e)
  • update the build to use api-extractor types (#383) (752c49c)

Bug Fixes

Reverts

Code Refactoring

1.10.1 (2021-09-20)

Bug Fixes

  • update publish to include directories (#328) (5fb44a2)

1.10.0 (2021-09-16)

Features

1.9.0 (2021-06-25)

Features

Bug Fixes

v1.8.0

06-04-2021 16:11 PDT

Features

  • Update event and cloudevent interfaces (#276)
  • Support local development with Pub/Sub emulator (#272)
  • Disable x-powered-by header (#223)

Bug Fixes

  • Allow killing process with CTRL+C (#264)
  • Do not pass numeric arguments to res.send (#242)
  • Fix cloudevent signature callbacks (#234)
  • Log function signature type (#228)
  • Export the public interfaces (#218)

Dependencies

  • update lodash to 4.17.21 (#284)
  • update hosted-git-info to 2.8.9 (#282)
  • update googlecloudplatform/functions-framework-conformance action to v0.3.9 (#271)
  • update typescript to v4.2.3 (#269)
  • update mocha to v8.3.2 (#268)
  • update @types/supertest to v2.0.11 (#267)
  • update @types/node to v11.15.50 (#266)
  • update supertest to v6 (#251)
  • update gts to v3 (#250)
  • update actions/setup-node action to v2 (#249)
  • update @types/minimist to v1.2.1 (#245)
  • update @types/express to v4.17.11 (#244)
  • update ini to 1.3.7 (#240)
  • update @types/mocha to v8.0.3 (#201)
  • update minimist to 1.2.5 (#259)

Documentation

  • Add buildpacks/docker quickstart (#212)
  • Mention express as the request/response parameters (#200)

Internal / Testing Changues

  • Updates to functions-framework-conformance action (#224, #236, #279, #280)
  • Split up invoker tests into separate integration test files (#281)
  • Enable eslint for tests (#275)
  • Add useful npm scripts (#274)
  • CI configuration updates (#219, #217)
  • Refactor: split invoker and router (#213)
  • Update renovate.json schedule (#210)

v1.7.1

08-10-2020 11:13 PDT

Implementation Changes

  • fix: Don't call function after 404 on /{robots.txt,favicon.ico} (#193)

New Features

Dependencies

  • chore(deps): update dependency mocha to v8.1.1 (#194)
  • chore: remove tslint.json (#190)

Documentation

Internal / Testing Changes

v1.7.0

08-06-2020 12:01 PDT

Implementation Changes

  • fix: do not send error when error code is fine (#187)

New Features

  • fix: add functions-framework-nodejs executable (#152)

Dependencies

  • chore(deps): use gts v2 (#186)
  • chore(deps): update dependency @types/express to v4.17.7 (#166)
  • chore(deps): update dependency @types/node to v11.15.20 (#172)
  • chore(deps): update dependency @types/supertest to v2.0.10 (#173)
  • chore(deps): update dependency mocha to v8 (#178)
  • chore(deps): update dependency supertest to v4 (#179)
  • chore(deps): automerge all but major updates (#183)
  • chore(deps): update dependency typescript to v3.9.7 (#176)
  • chore: make renovate not update as often (#170)
  • chore(deps): bump lodash from 4.17.14 to 4.17.19 (#156)
  • chore(deps): pin dependencies (#163)
  • chore(deps): add renovate.json (#65)

Documentation

  • docs: change badge to GitHub Actions (#180)
  • Fix typo: https -> http (#153)

Internal / Testing Changes

v1.6.0

06-17-2020 14:53 PDT

Implementation Changes

  • refactor: extract cloudevents functions, privateize file-global identifiers (#138)
  • refactor: Move the logic to load user's function to loader.ts (#136)
  • refactor: split files (#135)

New Features

  • feat: prototype cloudevent function signature type (#147)

Dependencies

Documentation

  • docs: remove incorrect pubsub docs (#145)
  • Fix PubSub Event Example (#141)

Internal / Testing Changes

  • Use CloudEvents v1.0 in CloudEventsContext and tests (#139)

v1.5.1

03-30-2020 11:05 PDT

Implementation Changes

  • fix: handle SIGINT in ErrorHandler (#126)

New Features

Dependencies

  • chore(deps): bump acorn from 5.7.3 to 5.7.4 (#124)
  • chore(deps): bump minimist from 1.2.0 to 1.2.2 (#123)
  • chore(deps): bump minimist from 1.2.2 to 1.2.3 (#128)

Documentation

Internal / Testing Changes

v1.5.0

03-06-2020 08:15 PST

Implementation Changes

  • Adjust path handling (#121)

New Features

Dependencies

Documentation

Internal / Testing Changes

v1.4.0

01-30-2020 11:54 PST

Implementation Changes

New Features

  • feat: add --dry-run option to load user's function but not start a server (#118)

Dependencies

Documentation

  • docs: Remove beta from gcloud run (#114)
  • Update README.md (#115)
  • docs: update pub sub docs with instructions that work (#109)
  • Fix double quotation from full-width character (#107)
  • docs: add video about FF (#102)
  • Changed curl command to include json content-type (#100)

Internal / Testing Changes

v1.3.2

09-13-2019 18:06 PDT

  • Revert "fixes #33: Only listen to functionTarget. (#81)"

Implementation Changes

New Features

Dependencies

Documentation

Internal / Testing Changes

v1.3.1

09-13-2019 10:00 PDT

Implementation Changes

  • fix: use empty string path when function source is not specified (#90)

New Features

Dependencies

Documentation

Internal / Testing Changes

v1.3.0

09-11-2019 10:17 PDT

Implementation Changes

  • fixes #33: Only listen to functionTarget. (#81)
  • fix: change the default function directory to the current working directory instead of root ('/') (#77)
  • Adding null check in catch block (#71)
  • fix: Adds types for superagent. Fixes #68 (#69)

New Features

  • Add a source flag/env option to add flexibility to execution path (#53)

Dependencies

Documentation

  • Docs Updates (#70)
  • Fix small typo on link (#79)
  • New section documentation on how to perform local PubSub testing (#76)
  • Docs: Add Docker Tutorial Doc (#58)

Internal / Testing Changes

  • chore: remove the converter