faas-js-runtime
Advanced tools
Comparing version 0.6.0 to 0.7.0
#!/usr/bin/env node | ||
const path = require('path'); | ||
const runtime = require('../'); | ||
const { start } = require('../'); | ||
const pkg = require('../package.json'); | ||
@@ -17,2 +17,4 @@ | ||
.version(pkg.version) | ||
.option('--logLevel <logLevel>', 'change the log level of the function', 'warn') | ||
.option('--port <port>', 'change the port the runtime listens on', 8080) | ||
.arguments('<file>') | ||
@@ -24,6 +26,20 @@ .action(runServer); | ||
async function runServer(file) { | ||
const programOpts = program.opts(); | ||
try { | ||
const func = require(extractFullPath(file)); | ||
const server = await runtime(func); | ||
let server; | ||
let options = { | ||
logLevel: programOpts.logLevel || process.env.FUNC_LOG_LEVEL, | ||
port: programOpts.port || process.env.FUNC_PORT | ||
}; | ||
const filePath = extractFullPath(file); | ||
const code = require(filePath); | ||
if (typeof code === 'function') { | ||
server = await start(code, options); | ||
} else if (typeof code.handle === 'function') { | ||
server = await start(code.handle, options); | ||
} else { | ||
console.error(code); | ||
throw TypeError(`Cannot find Invokable function 'handle' in ${code}`); | ||
} | ||
ON_DEATH(_ => { | ||
@@ -33,18 +49,4 @@ server.close(); | ||
}); | ||
log(chalk.blue(` | ||
The server has started. | ||
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: 1.0' \\ | ||
http://localhost:8080`)); | ||
log(chalk.blue(`The server has started. http://localhost:8080`)); | ||
} catch (error) { | ||
log(chalk.redBright('Something went wrong')); | ||
log(chalk.red(error)); | ||
@@ -51,0 +53,0 @@ } |
@@ -5,2 +5,15 @@ # Changelog | ||
## [0.7.0](https://www.github.com/boson-project/faas-js-runtime/compare/v0.6.0...v0.7.0) (2021-05-24) | ||
### Features | ||
* add TypeScript type definitions ([#90](https://www.github.com/boson-project/faas-js-runtime/issues/90)) ([d43fa28](https://www.github.com/boson-project/faas-js-runtime/commit/d43fa28c0114ed7ef24a805f43c50b19cfe7a287)) | ||
* **cli:** pass --logLevel and --port from the cli or as env variables ([d6b32a3](https://www.github.com/boson-project/faas-js-runtime/commit/d6b32a3e32292112c531abe63d5ffbad9c00639e)) | ||
### Bug Fixes | ||
* change index.js to not have a default export ([#93](https://www.github.com/boson-project/faas-js-runtime/issues/93)) ([d5bfd68](https://www.github.com/boson-project/faas-js-runtime/commit/d5bfd68f4ee731f6a7170ff8978954ff8fd0c100)) | ||
## [0.6.0](https://www.github.com/boson-project/faas-js-runtime/compare/v0.5.1...v0.6.0) (2021-04-10) | ||
@@ -7,0 +20,0 @@ |
18
index.js
@@ -13,14 +13,5 @@ const qs = require('qs'); | ||
function start(func, port, cb, options) { | ||
switch (typeof port) { | ||
case 'function': | ||
options = cb; | ||
cb = port; | ||
port = DEFAULT_PORT; | ||
break; | ||
case 'undefined': | ||
port = DEFAULT_PORT; | ||
break; | ||
} | ||
const { logLevel = 'info' } = { ...options }; | ||
// Invoker | ||
function start(func, options) { | ||
const { logLevel = 'warn', port = DEFAULT_PORT } = { ...options }; | ||
@@ -60,3 +51,2 @@ const server = fastify({ logger: { level: logLevel } }); | ||
if (err) return reject(err); | ||
if (cb) cb(server.server); | ||
resolve(server.server); | ||
@@ -67,2 +57,2 @@ }); | ||
module.exports = exports = start; | ||
module.exports = exports = { start }; |
@@ -18,2 +18,3 @@ const { HTTP } = require('cloudevents'); | ||
request.fcontext.cloudevent = HTTP.toEvent(request); | ||
request.fcontext.cloudevent.validate(); | ||
} catch (err) { | ||
@@ -20,0 +21,0 @@ if (err.message.startsWith('invalid spec version')) { |
@@ -27,5 +27,5 @@ 'use strict'; | ||
module.exports = function(fastify, options, done) { | ||
fastify.get(readinessURL, callProtect); | ||
fastify.get(livenessURL, callProtect); | ||
fastify.get(readinessURL, { logLevel: 'warn' }, callProtect); | ||
fastify.get(livenessURL, { logLevel: 'warn' }, callProtect); | ||
done(); | ||
}; |
@@ -22,3 +22,3 @@ 'use strict'; | ||
// as the first parameter | ||
payload.response = await func(context, context.cloudevent.data); | ||
payload.response = await func(context, context.cloudevent); | ||
} else { | ||
@@ -25,0 +25,0 @@ // Invoke with context |
{ | ||
"name": "faas-js-runtime", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/openshift-cloud-functions/faas-js-runtime.git" | ||
"url": "https://github.com/boson-project/faas-js-runtime.git" | ||
}, | ||
@@ -11,4 +11,6 @@ "author": "Red Hat, Inc.", | ||
"scripts": { | ||
"lint": "eslint index.js lib/*.js bin/*.js", | ||
"test": "nyc tape test/*.js | tap-spec", | ||
"lint": "eslint --ignore-path .gitignore .", | ||
"test": "npm run test:source && npm run test:types", | ||
"test:source": "nyc tape test/*.js | tap-spec", | ||
"test:types": "tsd", | ||
"pretest": "npm run lint" | ||
@@ -18,2 +20,3 @@ }, | ||
"files": [ | ||
"index.d.ts", | ||
"index.js", | ||
@@ -23,6 +26,7 @@ "lib", | ||
], | ||
"types": "index.d.ts", | ||
"bin": "./bin/cli.js", | ||
"dependencies": { | ||
"chalk": "^4.1.0", | ||
"cloudevents": "^4.0.0", | ||
"cloudevents": "^4.0.2", | ||
"commander": "^6.1.0", | ||
@@ -35,8 +39,14 @@ "death": "^1.1.0", | ||
"devDependencies": { | ||
"@types/node": "^15.0.3", | ||
"@typescript-eslint/eslint-plugin": "^4.23.0", | ||
"@typescript-eslint/parser": "^4.23.0", | ||
"babel-eslint": "^10.1.0", | ||
"eslint": "^7.4.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"nyc": "^14.1.1", | ||
"supertest": "^4.0.2", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.11.0" | ||
"tape": "^4.11.0", | ||
"tsd": "^0.15.1", | ||
"typescript": "^4.2.4" | ||
}, | ||
@@ -47,3 +57,7 @@ "standardx": { | ||
] | ||
}, | ||
"tsd": { | ||
"directory": "test/types", | ||
"typingsFile": "index.d.ts" | ||
} | ||
} |
@@ -19,6 +19,29 @@ ## FaaS Node.js Runtime Framework | ||
TBD: What format should the function response be? Should it also be a Cloud | ||
Event? But where is the event emitted? To some Knative Channel? Much is | ||
still to be determined. | ||
### CLI | ||
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, | ||
```js | ||
// 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. | ||
```console | ||
npx faas-js-runtime ./index.js | ||
``` | ||
### Usage | ||
@@ -29,3 +52,3 @@ | ||
```js | ||
const framework = require('faas-js-runtime'); | ||
const { start } = require('faas-js-runtime'); | ||
const options = { | ||
@@ -39,3 +62,3 @@ // Pino is used as the logger implementation. Supported log levels are | ||
// My function directory is in ./function-dir | ||
framework(require(`${__dirname}/function-dir/`), server => { | ||
start(require(`${__dirname}/function-dir/`), server => { | ||
// The server is now listening on localhost:8080 | ||
@@ -42,0 +65,0 @@ // and the function will be invoked for each HTTP |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
34748
14
359
118
12
6
Updatedcloudevents@^4.0.2