Skip to content

Commit 427c085

Browse files
authored
src: extract runtime framework into its own module (#29)
The runtime framework can be developed tested and published independent of the runtime image by pulling the source out of this repository and publishing it in the npm repository at https://www.npmjs.com/package/@redhat/faas-js-runtime. The framework source has been copied to another repository at https://github.com/openshift-cloud-functions/faas-js-runtime. Fixes: #28 build: bump image tag to 0.0.2 The 0.0.2 version of the runtime does not install dependencies when the framework spins up. That is the responsibility of the user. In our case, the assemble script does the npm install for any user functions. The application entry point at index.js now passes a function instead of a path to a function to the framework.
1 parent 0cc2d67 commit 427c085

File tree

6 files changed

+13
-109
lines changed

6 files changed

+13
-109
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
IMAGE_TAG = 0.0.1
1+
IMAGE_TAG = 0.0.2
22
IMAGE_NAME = oscf/js-runtime
33
DOCKER_IMAGE = docker.io/$(IMAGE_NAME):$(IMAGE_TAG)
44
QUAY_IMAGE = quay.io/$(IMAGE_NAME):$(IMAGE_TAG)
5-
TEST_IMAGE = $(IMAGE_NAME):candidate
5+
TEST_IMAGE = $(IMAGE_NAME):$(IMAGE_TAG)-candidate
66

77
.PHONY: build test clean
88

run-test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test_probe () {
3131

3232
test_http () {
3333
echo "Testing runtime HTTP function"
34-
EXPECTED='This is the test function for Node.js FaaS. Success.'
34+
EXPECTED='{"message":"This is the test function for Node.js FaaS. Success."}'
3535
RESPONSE=$(curl http://localhost:8080)
3636

3737
if [ "${RESPONSE}" == "${EXPECTED}" ] ; then
@@ -44,7 +44,7 @@ test_http () {
4444

4545
test_events () {
4646
echo "Testing cloud events"
47-
EXPECTED='Hello eventing!'
47+
EXPECTED='{"message":"Hello eventing!"}'
4848
RESPONSE=$(curl -d "{\"message\": \"Hello eventing!\"}" \
4949
-H "ce-type: dev.knative.cronjob.event" \
5050
-H "ce-id: 115fccc4-d65e-4e2d-a669-8f39b0672f6e" \

src/index.js

+5-52
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,10 @@
11
'use strict';
22

3-
const http = require('http');
4-
const Context = require('./lib/context');
5-
const eventHandler = require('./lib/event-handler');
6-
const protection = require('overload-protection');
3+
const path = require('path');
4+
const framework = require('@redhat/faas-js-runtime');
75

8-
// Default LIVENESS/READINESS urls
9-
const READINESS_URL = '/health';
10-
const LIVENESS_URL = '/health';
6+
console.log(`Executing in ${__dirname}`);
117

12-
// load the user function
13-
const func = require('../usr');
14-
15-
// Configure protect for liveness/readiness probes
16-
const protectCfg = {
17-
production: process.env.NODE_ENV === 'production',
18-
maxHeapUsedBytes: 0, // Maximum heap used threshold (0 to disable) [default 0]
19-
maxRssBytes: 0, // Maximum rss size threshold (0 to disable) [default 0]
20-
errorPropagationMode: false // Don't propagate error to callback, if something is wrong just reply with correct status code
21-
};
22-
const readinessURL = process.env.READINESS_URL || READINESS_URL;
23-
const livenessURL = process.env.LIVENESS_URL || LIVENESS_URL;
24-
const protect = protection('http', protectCfg);
25-
26-
// listen for incoming requests
27-
const server = http.createServer((req, res) => {
28-
const context = new Context(req, res);
29-
// Check if health path
30-
if (req.url === readinessURL || req.url === livenessURL) {
31-
protect(req, res, () => res.end("OK"))
32-
} else if (('ce-type' in req.headers) && req.headers['ce-type'].startsWith('dev.knative')) {
33-
eventHandler(req, res)
34-
.then(event => {
35-
context.cloudevent = event;
36-
res.end(func(context))
37-
})
38-
.catch(err => {
39-
// TODO: This should do some better error handling. What should the caller get?
40-
console.error(err);
41-
res.end(err);
42-
});
43-
} else {
44-
res.end(func(context));
45-
}
46-
});
47-
48-
server.on('clientError', (err, socket) => {
49-
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
50-
console.log(err);
8+
framework(require(path.join(`${__dirname}`, '..', 'usr')), 8080, server => {
9+
console.log('FaaS framework initialized.');
5110
});
52-
53-
server.on('listening', _ => {
54-
console.log(`Server listening on port ${server.address().port}`);
55-
});
56-
57-
server.listen(8080);

src/lib/context.js

-8
This file was deleted.

src/lib/event-handler.js

-42
This file was deleted.

src/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"name": "faas-js-runtime",
2+
"name": "js-runtime",
33
"version": "1.0.0",
44
"dependencies": {
5-
"overload-protection": "^1.1.0",
6-
"cloudevents-sdk": "v0.3.1"
5+
"@redhat/faas-js-runtime": "0.0.2",
6+
"cloudevents-sdk": "v0.3.1",
7+
"overload-protection": "^1.1.0"
78
}
89
}

0 commit comments

Comments
 (0)