Repository navigation
Expand file tree
/
Copy pathbootstrap.js
More file actions
403 lines (356 loc) · 10.5 KB
/
Copy pathbootstrap.js
File metadata and controls
403 lines (356 loc) · 10.5 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
'use strict';
var fs = require('fs');
var net = require('net');
// var http = require('http');
var http_common = require('_http_common');
var events = require('events');
var HTTPParser = process.binding('http_parser').HTTPParser;
var oldlog = console.log
console.log = console.error
// Some notes on the semantics of the succeed(), fail() and done() methods.
// Tests are the source of truth!
// First call wins in terms of deciding the result of the function. BUT,
// subsequent calls also log. Further, code execution does not stop, even where
// for done(), the docs say that the "function terminates". It seems though
// that further cycles of the event loop do not run. For example:
// index.handler = function(event, context) {
// context.fail("FAIL")
// process.nextTick(function() {
// console.log("This does not get logged")
// })
// console.log("This does get logged")
// }
// on the other hand:
// index.handler = function(event, context) {
// process.nextTick(function() {
// console.log("This also gets logged")
// context.fail("FAIL")
// })
// console.log("This does get logged")
// }
//
// The same is true for context.succeed() and done() captures the semantics of
// both. It seems this is implemented simply by having process.nextTick() cause
// process.exit() or similar, because the following:
// exports.handler = function(event, context) {
// process.nextTick(function() {console.log("This gets logged")})
// process.nextTick(function() {console.log("This also gets logged")})
// context.succeed("END")
// process.nextTick(function() {console.log("This does not get logged")})
// };
//
// So the context object needs to have some sort of hidden boolean that is only
// flipped once, by the first call, and dictates the behavior on the next tick.
//
// In addition, the response behaviour depends on the invocation type. If we
// are to only support the async type, succeed() must return a 202 response
// code, not sure how to do this.
//
// Only the first 256kb, followed by a truncation message, should be logged.
//
// Also, the error log is always in a json literal
// { "errorMessage": "<message>" }
var Context = function() {
var concluded = false;
var contextSelf = this;
// The succeed, fail and done functions are public, but access a private
// member (concluded). Hence this ugly nested definition.
this.succeed = function(result) {
if (concluded) {
return
}
// We have to process the result before we can conclude, because otherwise
// we have to fail. This means NO EARLY RETURNS from this function without
// review!
if (result === undefined) {
result = null
}
var failed = false;
try {
// Output result to log
oldlog(JSON.stringify(result));
} catch(e) {
// Set X-Amz-Function-Error: Unhandled header
console.log("Unable to stringify body as json: " + e);
failed = true;
}
// FIXME(nikhil): Return 202 or 200 based on invocation type and set response
// to result. Should probably be handled externally by the runner/swapi.
// OK, everything good.
concluded = true;
process.nextTick(function() { process.exit(failed ? 1 : 0) })
}
this.fail = function(error) {
if (concluded) {
return
}
concluded = true
process.nextTick(function() { process.exit(1) })
if (error === undefined) {
error = null
}
// FIXME(nikhil): Truncated log of error, plus non-truncated response body
var errstr = "fail() called with argument but a problem was encountered while converting it to a to string";
// The semantics of fail() are weird. If the error is something that can be
// converted to a string, the log output wraps the string in a JSON literal
// with key "errorMessage". If toString() fails, then the output is only
// the error string.
try {
if (error === null) {
errstr = null
} else {
errstr = error.toString()
}
oldlog(JSON.stringify({"errorMessage": errstr }))
} catch(e) {
// Set X-Amz-Function-Error: Unhandled header
oldlog(errstr)
}
}
this.done = function() {
var error = arguments[0];
var result = arguments[1];
if (error) {
contextSelf.fail(error)
} else {
contextSelf.succeed(result)
}
}
var plannedEnd = Date.now() + (getTimeoutInSeconds() * 1000);
this.getRemainingTimeInMillis = function() {
return Math.max(plannedEnd - Date.now(), 0);
}
}
function getTimeoutInSeconds() {
var t = parseInt(getEnv("TASK_TIMEOUT"));
if (Number.isNaN(t)) {
return 3600;
}
return t;
}
var getEnv = function(name) {
return process.env[name] || "";
}
var makeCtx = function() {
var fnname = getEnv("AWS_LAMBDA_FUNCTION_NAME");
// FIXME(nikhil): Generate UUID.
var taskID = getEnv("TASK_ID");
var mem = getEnv("TASK_MAXRAM").toLowerCase();
var bytes = 300 * 1024 * 1024;
var scale = { 'b': 1, 'k': 1024, 'm': 1024*1024, 'g': 1024*1024*1024 };
// We don't bother validating too much, if the last character is not a number
// and not in the scale table we just return a default value.
// We use slice instead of indexing so that we always get an empty string,
// instead of undefined.
if (mem.slice(-1).match(/[0-9]/)) {
var a = parseInt(mem);
if (!Number.isNaN(a)) {
bytes = a;
}
} else {
var rem = parseInt(mem.slice(0, -1));
if (!Number.isNaN(rem)) {
var multiplier = scale[mem.slice(-1)];
if (multiplier) {
bytes = rem * multiplier
}
}
}
var memoryMB = bytes / (1024 * 1024);
var ctx = new Context();
Object.defineProperties(ctx, {
"functionName": {
value: fnname,
enumerable: true,
},
"functionVersion": {
value: "$LATEST",
enumerable: true,
},
"invokedFunctionArn": {
// FIXME(nikhil): Should be filled in.
value: "",
enumerable: true,
},
"memoryLimitInMB": {
// Sigh, yes it is a string.
value: ""+memoryMB,
enumerable: true,
},
"awsRequestId": {
value: taskID,
enumerable: true,
},
"logGroupName": {
// FIXME(nikhil): Should be filled in.
value: "",
enumerable: true,
},
"logStreamName": {
// FIXME(nikhil): Should be filled in.
value: "",
enumerable: true,
},
"identity": {
// FIXME(nikhil): Should be filled in.
value: null,
enumerable: true,
},
"clientContext": {
// FIXME(nikhil): Should be filled in.
value: null,
enumerable: true,
},
});
return ctx;
}
var setEnvFromHeader = function () {
var headerPrefix = "CONFIG_";
var newEnvVars = {};
for (var key in process.env) {
if (key.indexOf(headerPrefix) == 0) {
newEnvVars[key.slice(headerPrefix.length)] = process.env[key];
}
}
for (var key in newEnvVars) {
process.env[key] = newEnvVars[key];
}
}
// for http hot functions
function freeParser(parser){
if (parser) {
parser.onIncoming = null;
parser.socket = null;
http_common.parsers.free(parser);
parser = null;
}
};
// parses http requests
function parse(socket){
var emitter = new events.EventEmitter();
var parser = http_common.parsers.alloc();
parser.reinitialize(HTTPParser.REQUEST);
parser.socket = socket;
parser.maxHeaderPairs = 2000;
parser.onIncoming = function(req){
emitter.emit('request', req);
};
socket.on('data', function(buffer){
var ret = parser.execute(buffer, 0, buffer.length);
if(ret instanceof Error){
emitter.emit('error');
freeParser(parser);
}
});
socket.once('close', function(){
freeParser(parser);
});
return emitter;
};
function run() {
setEnvFromHeader();
var path = process.env["PAYLOAD_FILE"]; // if we allow a mounted file, this is used. Could safely be removed.
var stream = process.stdin;
if (path) {
try {
stream = fs.createReadStream(path);
} catch(e) {
console.error("bootstrap: Error opening payload file", e)
process.exit(1);
}
}
// First, check format (ie: hot functions)
var format = process.env["FORMAT"];
if (format == "http"){
// var parser = httpSocketSetup(stream)
// init parser
var parser = parse(stream);
let i = 0;
parser.on('request', function(req){
// Got parsed HTTP object
// console.error("REQUEST", req)
i++;
console.error("REQUEST:", i)
handleRequest(req);
});
parser.on('error', function(e){
// Not HTTP data
console.error("INVALID HTTP DATA!", e)
});
} else {
// default
handleRequest(stream);
}
}
function handleRequest(stream) {
var input = "";
stream.setEncoding('utf8');
stream.on('data', function(chunk) {
input += chunk;
});
stream.on('error', function(err) {
console.error("bootstrap: Error reading payload stream", err);
process.exit(1);
});
stream.on('end', function() {
var payload = {}
try {
if (input.length > 0) {
payload = JSON.parse(input);
}
} catch(e) {
console.error("bootstrap: Error parsing JSON", e);
process.exit(1);
}
handlePayload(payload)
})
}
function handlePayload(payload) {
if (process.argv.length > 2) {
var handler = process.argv[2];
var parts = handler.split('.');
// FIXME(nikhil): Error checking.
var script = parts[0];
var entry = parts[1];
var started = false;
try {
var mod = require('./'+script);
var func = mod[entry];
if (func === undefined) {
oldlog("Handler '" + entry + "' missing on module '" + script + "'");
return;
}
if (typeof func !== 'function') {
throw "TypeError: " + (typeof func) + " is not a function";
}
started = true;
var cback
// RUN THE FUNCTION:
mod[entry](payload, makeCtx(), functionCallback)
} catch(e) {
if (typeof e === 'string') {
oldlog(e)
} else {
oldlog(e.message)
}
if (!started) {
oldlog("Process exited before completing request\n")
}
}
} else {
console.error("bootstrap: No script specified")
process.exit(1);
}
}
function functionCallback(err, result) {
if (err != null) {
// then user returned error and we should respond with error
// http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html
oldlog(JSON.stringify({"errorMessage": errstr }))
return
}
if (result != null) {
oldlog(JSON.stringify(result))
}
}
run()