|
| 1 | +// Copyright (c) Alex Ellis 2021. All rights reserved. |
| 2 | +// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +"use strict" |
| 6 | + |
| 7 | +const express = require('express') |
| 8 | +const app = express() |
| 9 | +const handler = require('./function/handler'); |
| 10 | +const bodyParser = require('body-parser') |
| 11 | + |
| 12 | +const defaultMaxSize = '100kb' // body-parser default |
| 13 | + |
| 14 | +app.disable('x-powered-by'); |
| 15 | + |
| 16 | +const rawLimit = process.env.MAX_RAW_SIZE || defaultMaxSize |
| 17 | +const jsonLimit = process.env.MAX_JSON_SIZE || defaultMaxSize |
| 18 | + |
| 19 | +app.use(function addDefaultContentType(req, res, next) { |
| 20 | + // When no content-type is given, the body element is set to |
| 21 | + // nil, and has been a source of contention for new users. |
| 22 | + |
| 23 | + if(!req.headers['content-type']) { |
| 24 | + req.headers['content-type'] = "text/plain" |
| 25 | + } |
| 26 | + next() |
| 27 | +}) |
| 28 | + |
| 29 | +if (process.env.RAW_BODY === 'true') { |
| 30 | + app.use(bodyParser.raw({ type: '*/*' , limit: rawLimit })) |
| 31 | +} else { |
| 32 | + app.use(bodyParser.text({ type : "text/*" })); |
| 33 | + app.use(bodyParser.json({ limit: jsonLimit})); |
| 34 | + app.use(bodyParser.urlencoded({ extended: true })); |
| 35 | +} |
| 36 | + |
| 37 | +const isArray = (a) => { |
| 38 | + return (!!a) && (a.constructor === Array); |
| 39 | +}; |
| 40 | + |
| 41 | +const isObject = (a) => { |
| 42 | + return (!!a) && (a.constructor === Object); |
| 43 | +}; |
| 44 | + |
| 45 | +class FunctionEvent { |
| 46 | + constructor(req) { |
| 47 | + this.body = req.body; |
| 48 | + this.headers = req.headers; |
| 49 | + this.method = req.method; |
| 50 | + this.query = req.query; |
| 51 | + this.path = req.path; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class FunctionContext { |
| 56 | + constructor(cb) { |
| 57 | + this.statusCode = 200; |
| 58 | + this.cb = cb; |
| 59 | + this.headerValues = {}; |
| 60 | + this.cbCalled = 0; |
| 61 | + } |
| 62 | + |
| 63 | + status(statusCode) { |
| 64 | + if(!statusCode) { |
| 65 | + return this.statusCode; |
| 66 | + } |
| 67 | + |
| 68 | + this.statusCode = statusCode; |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + headers(value) { |
| 73 | + if(!value) { |
| 74 | + return this.headerValues; |
| 75 | + } |
| 76 | + |
| 77 | + this.headerValues = value; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + succeed(value) { |
| 82 | + let err; |
| 83 | + this.cbCalled++; |
| 84 | + this.cb(err, value); |
| 85 | + } |
| 86 | + |
| 87 | + fail(value) { |
| 88 | + let message; |
| 89 | + if(this.status() == "200") { |
| 90 | + this.status(500) |
| 91 | + } |
| 92 | + |
| 93 | + this.cbCalled++; |
| 94 | + this.cb(value, message); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +const middleware = async (req, res) => { |
| 99 | + const cb = (err, functionResult) => { |
| 100 | + if (err) { |
| 101 | + console.error(err); |
| 102 | + |
| 103 | + return res.status(fnContext.status()) |
| 104 | + .send(err.toString ? err.toString() : err); |
| 105 | + } |
| 106 | + |
| 107 | + if(isArray(functionResult) || isObject(functionResult)) { |
| 108 | + res.set(fnContext.headers()) |
| 109 | + .status(fnContext.status()).send(JSON.stringify(functionResult)); |
| 110 | + } else { |
| 111 | + res.set(fnContext.headers()) |
| 112 | + .status(fnContext.status()) |
| 113 | + .send(functionResult); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + const fnEvent = new FunctionEvent(req); |
| 118 | + const fnContext = new FunctionContext(cb); |
| 119 | + |
| 120 | + Promise.resolve(handler(fnEvent, fnContext, cb)) |
| 121 | + .then(res => { |
| 122 | + if(!fnContext.cbCalled) { |
| 123 | + fnContext.succeed(res); |
| 124 | + } |
| 125 | + }) |
| 126 | + .catch(e => { |
| 127 | + cb(e); |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +app.post('/*', middleware); |
| 132 | +app.get('/*', middleware); |
| 133 | +app.patch('/*', middleware); |
| 134 | +app.put('/*', middleware); |
| 135 | +app.delete('/*', middleware); |
| 136 | +app.options('/*', middleware); |
| 137 | + |
| 138 | +const port = process.env.http_port || 3000; |
| 139 | + |
| 140 | +app.listen(port, () => { |
| 141 | + console.log(`node18 listening on port: ${port}`) |
| 142 | +}); |
| 143 | + |
| 144 | + |
0 commit comments