forked from lambci/node-custom-lambda
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.c
52 lines (44 loc) · 1.86 KB
/
bootstrap.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#ifndef NODE_MAJOR
#error Must pass NODE_MAJOR to the compiler (eg "10")
#define NODE_MAJOR ""
#endif
#define AWS_EXECUTION_ENV "AWS_Lambda_nodejs" NODE_MAJOR "_lambci"
#define NODE_PATH "/opt/nodejs/node" NODE_MAJOR "/node_modules:" \
"/opt/nodejs/node_modules:" \
"/var/runtime/node_modules:" \
"/var/runtime:" \
"/var/task"
#define NODE_OPTIONS "--experimental-loader=/opt/esm-loader-hook.mjs"
#define MIN_MEM_SIZE 128
#define ARG_BUF_SIZE 32
int main(void) {
setenv("AWS_EXECUTION_ENV", AWS_EXECUTION_ENV, true);
setenv("NODE_PATH", NODE_PATH, true);
const char *node_options = getenv("NODE_OPTIONS");
if (node_options != NULL) {
char custom_node_options[ARG_BUF_SIZE * 8];
snprintf(custom_node_options, ARG_BUF_SIZE * 8, "%s %s", NODE_OPTIONS, node_options);
setenv("NODE_OPTIONS", custom_node_options, true);
} else {
setenv("NODE_OPTIONS", NODE_OPTIONS, true);
}
const char *mem_size_str = getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
int mem_size = mem_size_str != NULL ? atoi(mem_size_str) : MIN_MEM_SIZE;
char max_semi_space_size[ARG_BUF_SIZE];
snprintf(max_semi_space_size, ARG_BUF_SIZE, "--max-semi-space-size=%d", mem_size * 5 / 100);
char max_old_space_size[ARG_BUF_SIZE];
snprintf(max_old_space_size, ARG_BUF_SIZE, "--max-old-space-size=%d", mem_size * 90 / 100);
execv("/opt/bin/node", (char *[]){
"node",
"--expose-gc",
max_semi_space_size,
max_old_space_size,
"/opt/bootstrap.js",
NULL});
perror("Could not execv");
return EXIT_FAILURE;
}