Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
faas-js-runtime
Advanced tools
A Node.js framework for executing arbitrary functions in response to HTTP or cloud events
This module provides a Node.js framework for executing a function that
exists in a user provided directory path as an index.js
file. The
directory may also contain an optional package.json
file which can
be used to declare runtime dependencies for the function.
The function is loaded, and then invoked for incoming HTTP requests
at localhost:8080
. The incoming request may be a
Cloud Event or
just a simple HTTP GET request. In either case, the function will receive
a Context
object instance that has a cloudevent
property. For a raw HTTP
request, the incoming request is converted to a Cloud Event.
The invoked user function can be async
but that is not required.
The easiest way to get started is to use the CLI. You can call it with the path to any JavaScript file which has a default export that is a function. For example,
// index.js
function handle(context) {
const event = context.cloudevent;
// business logic
return {
statusCode: 200,
statusMessage: 'OK'
}
}
module.exports = handle;
You can expose this function as an HTTP endpoint at localhost:8080
with the CLI.
npx faas-js-runtime ./index.js
In my current working directory, I have an index.js
file like this.
const { start } = require('faas-js-runtime');
const options = {
// Pino is used as the logger implementation. Supported log levels are
// documented at this link:
// https://github.com/pinojs/pino/blob/master/docs/api.md#options
logLevel: 'info'
}
// My function directory is in ./function-dir
start(require(`${__dirname}/function-dir/`), server => {
// The server is now listening on localhost:8080
// and the function will be invoked for each HTTP
// request to this endpoint.
console.log('Server listening');
// Whenever you want to shutdown the framework
server.close();
}, options);
In ./function-dir
, there is an index.js
file that looks
like this.
module.exports = async function myFunction(context) {
const ret = 'This is a test function for Node.js FaaS. Success.';
return new Promise((resolve, reject) => {
setTimeout(_ => {
context.log.info('sending response to client')
resolve(ret);
}, 500);
});
};
You can use curl
to POST
to the endpoint:
$ curl -X POST -d 'hello=world' \
-H'Content-type: application/x-www-form-urlencoded' http://localhost:8080
You can use curl
to POST
JSON data to the endpoint:
$ curl -X POST -d '{"hello": "world"}' \
-H'Content-type: application/json' \
http://localhost:8080
You can use curl
to POST
an event to the endpoint:
$ curl -X POST -d '{"hello": "world"}' \
-H'Content-type: application/json' \
-H'Ce-id: 1' \
-H'Ce-source: cloud-event-example' \
-H'Ce-type: dev.knative.example' \
-H'Ce-specversion: 0.2' \
http://localhost:8080
You can see this in action, executing the function at test/fixtures/async
by running node hack/run.js
.
Just run npm test
.
FAQs
A Node.js framework for executing arbitrary functions in response to HTTP or cloud events
The npm package faas-js-runtime receives a total of 2,518 weekly downloads. As such, faas-js-runtime popularity was classified as popular.
We found that faas-js-runtime 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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.