-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
239 lines (217 loc) · 6.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const qs = require('qs');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const requestHandler = require('./lib/request-handler');
const eventHandler = require('./lib/event-handler');
const Context = require('./lib/context');
const shutdown = require('death')({ uncaughtException: true });
const fastifyRawBody = require('fastify-raw-body');
const { isPromise } = require('./lib/utils');
// HTTP framework
const fastify = require('fastify');
// Default log level
const LOG_LEVEL = 'warn';
// Default port
const PORT = 8080;
// Don't Include Raw body by default
const INCLUDE_RAW = false;
/**
* Starts the provided Function. If the function is a module, it will be
* inspected for init, shutdown, cors, liveness, and readiness functions and those
* will be used to configure the server. If it's a function, it will be used
* directly.
*
* @param {Object | function} func The function to start (see the Function type)
* @param {*} options Options to configure the server
* @param {string} options.logLevel The log level to use
* @param {number} options.port The port to listen on
* @returns {Promise<http.Server>} The server that was started
*/
async function start(func, options) {
options = options || {};
if (typeof func === 'function') {
return __start(func, options);
}
if (typeof func.handle !== 'function') {
throw new TypeError('Function must export a handle function');
}
if (typeof func.init === 'function') {
const initRet = func.init();
if (isPromise(initRet)) {
await initRet;
}
}
if (typeof func.shutdown === 'function') {
options.shutdown = func.shutdown;
}
if (typeof func.liveness === 'function') {
options.liveness = func.liveness;
}
if (typeof func.readiness === 'function') {
options.readiness = func.readiness;
}
if (typeof func.cors === 'function') {
options.cors = func.cors;
}
return __start(func.handle, options);
}
/**
* Internal function to start the server. This is used by the start function.
*
* @param {function} func - The function to start
* @param {*} options - Options to configure the server
* @param {string} options.logLevel - The log level to use
* @param {number} options.port - The port to listen on
* @returns {Promise<http.Server>} The server that was started
*/
async function __start(func, options) {
// Load a func.yaml file if it exists
const config = loadConfig(options);
// Create and configure the server for the default behavior
const server = initializeServer(config);
// Configures the server to handle incoming requests to the function itself,
// and also to other endpoints such as telemetry and liveness/readiness
requestHandler(server, { func, funcConfig: config });
// Start the server
try {
await server.listen({
port: config.port,
host: '::',
});
return server.server;
} catch (err) {
console.error('Error starting server', err);
process.exit(1);
}
}
/**
* Creates and configures the HTTP server to handle incoming CloudEvents,
* and initializes the Context object.
* @param {Config} config - The configuration object for port and logLevel
* @returns {FastifyInstance} The Fastify server that was created
*/
function initializeServer(config) {
const server = fastify({
logger: {
level: config.logLevel,
formatters: {
bindings: bindings => ({
pid: bindings.pid,
hostname: bindings.hostname,
node_version: process.version,
}),
},
},
});
if (config.includeRaw) {
server.register(fastifyRawBody, {
field: 'rawBody',
global: true,
encoding: 'utf8',
runFirst: false,
});
}
// Give the Function an opportunity to clean up before the process exits
shutdown(async _ => {
if (typeof config.shutdown === 'function') {
const shutdownRet = config.shutdown();
if (isPromise(shutdownRet)) {
await shutdownRet;
}
}
server.close();
process.exit(0);
});
// Add a parser for application/x-www-form-urlencoded
server.addContentTypeParser(
'application/x-www-form-urlencoded',
function(_, payload, done) {
var body = '';
payload.on('data', data => (body += data));
payload.on('end', _ => {
try {
const parsed = qs.parse(body);
done(null, parsed);
} catch (e) {
done(e);
}
});
payload.on('error', done);
}
);
// Add a parser for everything else - parse it as a buffer and
// let this framework's router handle it
server.addContentTypeParser(
'*',
{ parseAs: 'buffer' },
function(req, body, done) {
try {
done(null, body);
} catch (err) {
err.statusCode = 500;
done(err, undefined);
}
}
);
// Initialize the invocation context
// This is passed as a parameter to the function when it's invoked
server.decorateRequest('fcontext');
server.addHook('preHandler', (req, reply, done) => {
req.fcontext = new Context(req);
done();
});
// Evaluates the incoming request, parsing any CloudEvents and attaching
// to the request's `fcontext`
eventHandler(server);
return server;
}
/**
* loadConfig() loads a func.yaml file if it exists, allowing it to take precedence over the default options
*
* @param {Object} options Server options
* @param {String} options.config Path to a func.yaml file
* @param {String} options.logLevel Log level - one of 'fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent'
* @param {number} options.port Port to listen on
* @returns {Config} Configuration object
*/
function loadConfig(options) {
const opts = { ...options, ...readFuncYaml(options.config) };
opts.logLevel = opts.logLevel || LOG_LEVEL;
opts.port = opts.port || PORT;
opts.includeRaw = opts.includeRaw || INCLUDE_RAW;
return opts;
}
/**
* Reads a func.yaml file at path and returns it as a JS object
* @param {string} fileOrDirPath - the path to the func.yaml file or the directory containing it
* @returns {object} the parsed func.yaml file
*/
function readFuncYaml(fileOrDirPath) {
if (!fileOrDirPath) fileOrDirPath = './';
let baseDir;
let maybeDir = fs.statSync(fileOrDirPath);
if (maybeDir.isDirectory()) {
baseDir = fileOrDirPath;
} else {
maybeDir = fs.statSync(path.dirname(fileOrDirPath));
if (maybeDir.isDirectory()) {
baseDir = fileOrDirPath;
}
}
if (baseDir) {
const yamlFile = path.join(baseDir, 'func.yaml');
const maybeYaml = fs.statSync(yamlFile, { throwIfNoEntry: false });
if (!!maybeYaml && maybeYaml.isFile()) {
try {
return yaml.load(fs.readFileSync(yamlFile, 'utf8'));
} catch (err) {
console.warn(err);
}
}
}
}
module.exports = exports = {
start,
defaults: { LOG_LEVEL, PORT, INCLUDE_RAW },
};