-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathinvoker.ts
287 lines (272 loc) · 9.23 KB
/
invoker.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Node.js server that runs user's code on HTTP request. HTTP response is sent
// once user's function has completed.
// The server accepts following HTTP requests:
// - POST '/*' for executing functions (only for servers handling functions
// with non-HTTP trigger).
// - ANY (all methods) '/*' for executing functions (only for servers handling
// functions with HTTP trigger).
// eslint-disable-next-line node/no-deprecated-api
import * as domain from 'domain';
import * as express from 'express';
import * as http from 'http';
import {FUNCTION_STATUS_HEADER_FIELD} from './types';
import {sendCrashResponse} from './logger';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloudevents';
import {
HttpFunction,
EventFunction,
EventFunctionWithCallback,
CloudEventFunction,
CloudEventFunctionWithCallback,
} from './functions';
// We optionally annotate the express Request with a rawBody field.
// Express leaves the Express namespace open to allow merging of new fields.
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Express {
export interface Request {
rawBody?: Buffer;
}
}
}
/**
* Response object for the most recent request.
* Used for sending errors to the user.
*/
let latestRes: express.Response | null = null;
export const setLatestRes = (res: express.Response) => {
latestRes = res;
};
/**
* Sends back a response to the incoming request.
* @param result Output from function execution.
* @param err Error from function execution.
* @param res Express response object.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function sendResponse(result: any, err: Error | null, res: express.Response) {
if (err) {
res.set(FUNCTION_STATUS_HEADER_FIELD, 'error');
// Sending error message back is fine for Pub/Sub-based functions as they do
// not reach the caller anyway.
res.send(err.message);
return;
}
if (typeof result === 'undefined' || result === null) {
res.sendStatus(204); // No Content
} else if (typeof result === 'number') {
// This isn't technically compliant but numbers otherwise cause us to set
// the status code to that number instead of sending the number as a body.
res.json(result);
} else {
try {
res.send(result);
} catch (sendErr) {
// If a customer passes a non-serializeable object (e.g. one with a cycle)
// then res.send will throw. Customers don't always put a lot of thought
// into the return value because it's currently only used for
// CallFunction. The most sane resolution here is to succeed the function
// (this was the customer's clear intent) but send a 204 (NO CONTENT) and
// log an error message explaining why their content wasn't sent.
console.error('Error serializing return value: ' + sendErr.toString());
res.sendStatus(204); // No Content
}
}
}
/**
* Wraps the provided function into an Express handler function with additional
* instrumentation logic.
* @param execute Runs user's function.
* @return An Express handler function.
*/
export function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
return (req: express.Request, res: express.Response) => {
const d = domain.create();
// Catch unhandled errors originating from this request.
d.on('error', err => {
if (res.locals.functionExecutionFinished) {
console.error(`Exception from a finished function: ${err}`);
} else {
res.locals.functionExecutionFinished = true;
sendCrashResponse({err, res});
}
});
d.run(() => {
process.nextTick(() => {
execute(req, res);
});
});
};
}
/**
* Wraps cloudevent function (or cloudevent function with callback) in HTTP
* function signature.
* @param userFunction User's function.
* @return HTTP function which wraps the provided event function.
*/
export function wrapCloudEventFunction(
userFunction: CloudEventFunction | CloudEventFunctionWithCallback
): HttpFunction {
return (req: express.Request, res: express.Response) => {
const callback = process.domain.bind(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(err: Error | null, result: any) => {
if (res.locals.functionExecutionFinished) {
console.log('Ignoring extra callback call');
} else {
res.locals.functionExecutionFinished = true;
if (err) {
console.error(err.stack);
}
sendResponse(result, err, res);
}
}
);
let cloudevent = req.body;
if (isBinaryCloudEvent(req)) {
cloudevent = getBinaryCloudEventContext(req);
cloudevent.data = req.body;
}
// Callback style if user function has more than 1 argument.
if (userFunction!.length > 1) {
const fn = userFunction as CloudEventFunctionWithCallback;
return fn(cloudevent, callback);
}
const fn = userFunction as CloudEventFunction;
Promise.resolve()
.then(() => {
const result = fn(cloudevent);
return result;
})
.then(
result => {
callback(null, result);
},
err => {
callback(err, undefined);
}
);
};
}
/**
* Wraps event function (or event function with callback) in HTTP function
* signature.
* @param userFunction User's function.
* @return HTTP function which wraps the provided event function.
*/
export function wrapEventFunction(
userFunction: EventFunction | EventFunctionWithCallback
): HttpFunction {
return (req: express.Request, res: express.Response) => {
const event = req.body;
const callback = process.domain.bind(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(err: Error | null, result: any) => {
if (res.locals.functionExecutionFinished) {
console.log('Ignoring extra callback call');
} else {
res.locals.functionExecutionFinished = true;
if (err) {
console.error(err.stack);
}
sendResponse(result, err, res);
}
}
);
let data = event.data;
let context = event.context;
if (isBinaryCloudEvent(req)) {
// Support CloudEvents in binary content mode, with data being the whole
// request body and context attributes retrieved from request headers.
data = event;
context = getBinaryCloudEventContext(req);
} else if (context === undefined) {
// Support legacy events and CloudEvents in structured content mode, with
// context properties represented as event top-level properties.
// Context is everything but data.
context = event;
// Clear the property before removing field so the data object
// is not deleted.
context.data = undefined;
delete context.data;
}
// Callback style if user function has more than 2 arguments.
if (userFunction!.length > 2) {
const fn = userFunction as EventFunctionWithCallback;
return fn(data, context, callback);
}
const fn = userFunction as EventFunction;
Promise.resolve()
.then(() => {
const result = fn(data, context);
return result;
})
.then(
result => {
callback(null, result);
},
err => {
callback(err, undefined);
}
);
};
}
// Use an exit code which is unused by Node.js:
// https://nodejs.org/api/process.html#process_exit_codes
const killInstance = process.exit.bind(process, 16);
/**
* Enables registration of error handlers.
* @param server HTTP server which invokes user's function.
* @constructor
*/
export class ErrorHandler {
constructor(private readonly server: http.Server) {
this.server = server;
}
/**
* Registers handlers for uncaught exceptions and other unhandled errors.
*/
register() {
process.on('uncaughtException', err => {
console.error('Uncaught exception');
sendCrashResponse({err, res: latestRes, callback: killInstance});
});
process.on('unhandledRejection', err => {
console.error('Unhandled rejection');
sendCrashResponse({err, res: latestRes, callback: killInstance});
});
process.on('exit', code => {
sendCrashResponse({
err: new Error(`Process exited with code ${code}`),
res: latestRes,
silent: code === 0,
});
});
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal as NodeJS.Signals, () => {
sendCrashResponse({
err: new Error(`Received ${signal}`),
res: latestRes,
silent: true,
callback: () => {
// eslint-disable-next-line no-process-exit
process.exit();
},
});
});
});
}
}