@vercel/fun 1.2.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/deferred.js +1 -2
- package/dist/src/deferred.js.map +1 -1
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.js +88 -96
- package/dist/src/index.js.map +1 -1
- package/dist/src/install-node.d.ts +0 -1
- package/dist/src/install-node.js +32 -44
- package/dist/src/install-node.js.map +1 -1
- package/dist/src/install-python.d.ts +0 -1
- package/dist/src/install-python.js +16 -33
- package/dist/src/install-python.js.map +1 -1
- package/dist/src/providers/docker/index.js +1 -1
- package/dist/src/providers/docker/index.js.map +1 -1
- package/dist/src/providers/native/index.d.ts +0 -1
- package/dist/src/providers/native/index.js +133 -128
- package/dist/src/providers/native/index.js.map +1 -1
- package/dist/src/runtime-server.d.ts +0 -1
- package/dist/src/runtime-server.js +115 -129
- package/dist/src/runtime-server.js.map +1 -1
- package/dist/src/runtimes/go1.x/filename.js +1 -2
- package/dist/src/runtimes/go1.x/filename.js.map +1 -1
- package/dist/src/runtimes/go1.x/index.js +27 -39
- package/dist/src/runtimes/go1.x/index.js.map +1 -1
- package/dist/src/runtimes/nodejs/bootstrap.js +99 -124
- package/dist/src/runtimes/nodejs/bootstrap.js.map +1 -1
- package/dist/src/runtimes/nodejs10.x/index.js +6 -18
- package/dist/src/runtimes/nodejs10.x/index.js.map +1 -1
- package/dist/src/runtimes/nodejs12.x/index.js +6 -18
- package/dist/src/runtimes/nodejs12.x/index.js.map +1 -1
- package/dist/src/runtimes/nodejs14.x/index.js +6 -18
- package/dist/src/runtimes/nodejs14.x/index.js.map +1 -1
- package/dist/src/runtimes/nodejs6.10/index.js +6 -18
- package/dist/src/runtimes/nodejs6.10/index.js.map +1 -1
- package/dist/src/runtimes/nodejs8.10/index.js +6 -18
- package/dist/src/runtimes/nodejs8.10/index.js.map +1 -1
- package/dist/src/runtimes/python/bootstrap.js.map +1 -1
- package/dist/src/runtimes/python2.7/index.js +6 -18
- package/dist/src/runtimes/python2.7/index.js.map +1 -1
- package/dist/src/runtimes/python3/bootstrap.js.map +1 -1
- package/dist/src/runtimes/python3/index.js +3 -15
- package/dist/src/runtimes/python3/index.js.map +1 -1
- package/dist/src/runtimes/python3.6/index.js +6 -18
- package/dist/src/runtimes/python3.6/index.js.map +1 -1
- package/dist/src/runtimes/python3.7/index.js +6 -18
- package/dist/src/runtimes/python3.7/index.js.map +1 -1
- package/dist/src/runtimes.js +129 -136
- package/dist/src/runtimes.js.map +1 -1
- package/dist/src/types.d.ts +0 -2
- package/dist/src/unzip.d.ts +0 -2
- package/dist/src/unzip.js +65 -78
- package/dist/src/unzip.js.map +1 -1
- package/package.json +5 -7
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -26,6 +17,10 @@ const isWin = process.platform === 'win32';
|
|
|
26
17
|
const debug = (0, debug_1.default)('@vercel/fun:providers/native');
|
|
27
18
|
const treeKill = (0, node_util_1.promisify)(tree_kill_1.default);
|
|
28
19
|
class NativeProvider {
|
|
20
|
+
pool;
|
|
21
|
+
lambda;
|
|
22
|
+
params;
|
|
23
|
+
runtimeApis;
|
|
29
24
|
constructor(fn, params) {
|
|
30
25
|
const factory = {
|
|
31
26
|
create: this.createProcess.bind(this),
|
|
@@ -54,73 +49,87 @@ class NativeProvider {
|
|
|
54
49
|
console.error('factoryDestroyError', { err });
|
|
55
50
|
});
|
|
56
51
|
}
|
|
57
|
-
createProcess() {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
52
|
+
async createProcess() {
|
|
53
|
+
const { runtime, params, region, version, extractedDir } = this.lambda;
|
|
54
|
+
const binDir = (0, node_path_1.join)(runtime.cacheDir, 'bin');
|
|
55
|
+
const bootstrapFile = runtime.name === 'executable' ? 'executable' : 'bootstrap';
|
|
56
|
+
const bootstrap = (0, node_path_1.join)(runtime.cacheDir, isWin ? 'bootstrap.js' : bootstrapFile);
|
|
57
|
+
const server = new runtime_server_1.RuntimeServer(this.lambda);
|
|
58
|
+
await (0, async_listen_1.default)(server, 0, '127.0.0.1');
|
|
59
|
+
const { port } = server.address();
|
|
60
|
+
debug('Creating process %o', bootstrap);
|
|
61
|
+
const taskDir = (0, node_path_1.resolve)(extractedDir || params.Code.Directory);
|
|
62
|
+
const functionName = params.FunctionName || (0, node_path_1.basename)(taskDir);
|
|
63
|
+
const logGroupName = `aws/lambda/${functionName}`;
|
|
64
|
+
const logStreamName = `2019/01/12/[${version}]${(0, node_crypto_1.randomUUID)().replace(/\-/g, '')}`;
|
|
65
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
|
|
66
|
+
const env = {
|
|
67
|
+
// Non-reserved env vars (can overwrite with params)
|
|
68
|
+
PATH: `${binDir}${node_path_1.delimiter}${process.env.PATH}`,
|
|
69
|
+
LANG: 'en_US.UTF-8',
|
|
70
|
+
// User env vars
|
|
71
|
+
...(params.Environment && params.Environment.Variables),
|
|
72
|
+
// Restricted env vars
|
|
73
|
+
_HANDLER: params.Handler,
|
|
74
|
+
AWS_REGION: region,
|
|
75
|
+
AWS_ACCESS_KEY_ID: params.AccessKeyId,
|
|
76
|
+
AWS_SECRET_ACCESS_KEY: params.SecretAccessKey,
|
|
77
|
+
AWS_DEFAULT_REGION: region,
|
|
78
|
+
AWS_EXECUTION_ENV: `AWS_Lambda_${params.Runtime}`,
|
|
79
|
+
AWS_LAMBDA_FUNCTION_NAME: functionName,
|
|
80
|
+
AWS_LAMBDA_FUNCTION_VERSION: version,
|
|
81
|
+
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: String(params.MemorySize || 128),
|
|
82
|
+
AWS_LAMBDA_RUNTIME_API: `127.0.0.1:${port}`,
|
|
83
|
+
AWS_LAMBDA_LOG_GROUP_NAME: logGroupName,
|
|
84
|
+
AWS_LAMBDA_LOG_STREAM_NAME: logStreamName,
|
|
85
|
+
LAMBDA_RUNTIME_DIR: runtime.cacheDir,
|
|
86
|
+
LAMBDA_TASK_ROOT: taskDir,
|
|
87
|
+
TZ: ':UTC'
|
|
88
|
+
};
|
|
89
|
+
let bin = bootstrap;
|
|
90
|
+
const args = [];
|
|
91
|
+
if (isWin) {
|
|
92
|
+
args.push(bootstrap);
|
|
93
|
+
bin = process.execPath;
|
|
94
|
+
}
|
|
95
|
+
const proc = (0, child_process_1.spawn)(bin, args, {
|
|
96
|
+
env,
|
|
97
|
+
cwd: taskDir,
|
|
98
|
+
stdio: ['ignore', 'inherit', 'inherit']
|
|
99
|
+
});
|
|
100
|
+
this.runtimeApis.set(proc, server);
|
|
101
|
+
proc.on('exit', async (code, signal) => {
|
|
102
|
+
debug('Process (pid=%o) exited with code %o, signal %o', proc.pid, code, signal);
|
|
103
|
+
const server = this.runtimeApis.get(proc);
|
|
104
|
+
if (server) {
|
|
105
|
+
debug('Shutting down Runtime API for %o', proc.pid);
|
|
106
|
+
server.close();
|
|
107
|
+
this.runtimeApis.delete(proc);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
debug('No Runtime API server associated with process %o. This SHOULD NOT happen!', proc.pid);
|
|
82
111
|
}
|
|
83
|
-
const proc = (0, child_process_1.spawn)(bin, args, {
|
|
84
|
-
env,
|
|
85
|
-
cwd: taskDir,
|
|
86
|
-
stdio: ['ignore', 'inherit', 'inherit']
|
|
87
|
-
});
|
|
88
|
-
this.runtimeApis.set(proc, server);
|
|
89
|
-
proc.on('exit', (code, signal) => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
debug('Process (pid=%o) exited with code %o, signal %o', proc.pid, code, signal);
|
|
91
|
-
const server = this.runtimeApis.get(proc);
|
|
92
|
-
if (server) {
|
|
93
|
-
debug('Shutting down Runtime API for %o', proc.pid);
|
|
94
|
-
server.close();
|
|
95
|
-
this.runtimeApis.delete(proc);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
debug('No Runtime API server associated with process %o. This SHOULD NOT happen!', proc.pid);
|
|
99
|
-
}
|
|
100
|
-
}));
|
|
101
|
-
return proc;
|
|
102
112
|
});
|
|
113
|
+
return proc;
|
|
103
114
|
}
|
|
104
|
-
destroyProcess(proc) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
async destroyProcess(proc) {
|
|
116
|
+
try {
|
|
117
|
+
// Unfreeze the process first so it is able to process the `SIGTERM`
|
|
118
|
+
// signal and exit cleanly (clean up child processes, etc.)
|
|
119
|
+
this.unfreezeProcess(proc);
|
|
120
|
+
debug('Stopping process %o', proc.pid);
|
|
121
|
+
await treeKill(proc.pid);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
// ESRCH means that the process ID no longer exists, which is fine
|
|
125
|
+
// in this case since we're shutting down the process anyways
|
|
126
|
+
if (err.code === 'ESRCH' || /not found/i.test(err.message)) {
|
|
127
|
+
debug('Got error stopping process %o: %s', proc.pid, err.message);
|
|
112
128
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// in this case since we're shutting down the process anyways
|
|
116
|
-
if (err.code === 'ESRCH' || /not found/i.test(err.message)) {
|
|
117
|
-
debug('Got error stopping process %o: %s', proc.pid, err.message);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
throw err;
|
|
121
|
-
}
|
|
129
|
+
else {
|
|
130
|
+
throw err;
|
|
122
131
|
}
|
|
123
|
-
}
|
|
132
|
+
}
|
|
124
133
|
}
|
|
125
134
|
freezeProcess(proc) {
|
|
126
135
|
// `SIGSTOP` is not supported on Windows
|
|
@@ -136,65 +145,61 @@ class NativeProvider {
|
|
|
136
145
|
process.kill(proc.pid, 'SIGCONT');
|
|
137
146
|
}
|
|
138
147
|
}
|
|
139
|
-
invoke(params) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return initError;
|
|
155
|
-
}
|
|
156
|
-
debug('Lambda is initialized for process %o', proc.pid);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
// The lambda process is being re-used for a subsequent
|
|
160
|
-
// invocation, so unfreeze the process first
|
|
161
|
-
this.unfreezeProcess(proc);
|
|
148
|
+
async invoke(params) {
|
|
149
|
+
let result;
|
|
150
|
+
const proc = await this.pool.acquire();
|
|
151
|
+
const server = this.runtimeApis.get(proc);
|
|
152
|
+
if (server.initDeferred) {
|
|
153
|
+
// The lambda process has just booted up, so wait for the
|
|
154
|
+
// initialization API call to come in before proceeding
|
|
155
|
+
debug('Waiting for init on process %o', proc.pid);
|
|
156
|
+
const initError = await server.initDeferred.promise;
|
|
157
|
+
if (initError) {
|
|
158
|
+
debug('Lambda got initialization error on process %o', proc.pid);
|
|
159
|
+
// An error happend during initialization, so remove the
|
|
160
|
+
// process from the pool and return the error to the caller
|
|
161
|
+
await this.pool.destroy(proc);
|
|
162
|
+
return initError;
|
|
162
163
|
}
|
|
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
|
-
|
|
164
|
+
debug('Lambda is initialized for process %o', proc.pid);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// The lambda process is being re-used for a subsequent
|
|
168
|
+
// invocation, so unfreeze the process first
|
|
169
|
+
this.unfreezeProcess(proc);
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
result = await server.invoke(params);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
result = {
|
|
176
|
+
StatusCode: 200,
|
|
177
|
+
FunctionError: 'Unhandled',
|
|
178
|
+
ExecutedVersion: '$LATEST',
|
|
179
|
+
// TODO: make this into a `server.createError()` function
|
|
180
|
+
Payload: JSON.stringify({
|
|
181
|
+
errorMessage: err.message
|
|
182
|
+
})
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (result.FunctionError === 'Unhandled') {
|
|
186
|
+
// An "Unhandled" error means either init error or the process
|
|
187
|
+
// exited before sending the response. In either case, the process
|
|
188
|
+
// is unhealthy and needs to be removed from the pool
|
|
189
|
+
await this.pool.destroy(proc);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
// Either a successful response, or a "Handled" error.
|
|
193
|
+
// The process may be re-used for the next invocation.
|
|
194
|
+
this.freezeProcess(proc);
|
|
195
|
+
await this.pool.release(proc);
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
191
198
|
}
|
|
192
|
-
destroy() {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.pool.clear();
|
|
197
|
-
});
|
|
199
|
+
async destroy() {
|
|
200
|
+
debug('Draining pool');
|
|
201
|
+
await this.pool.drain();
|
|
202
|
+
this.pool.clear();
|
|
198
203
|
}
|
|
199
204
|
}
|
|
200
205
|
exports.default = NativeProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/providers/native/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/providers/native/index.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,6CAAiD;AAEjD,kDAAgC;AAChC,yCAAsC;AAEtC,gEAAkC;AAClC,0DAAkC;AAClC,+CAAgD;AAChD,yCAA+D;AAC/D,iDAAoD;AACpD,yDAAqD;AASrD,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAC3C,MAAM,KAAK,GAAG,IAAA,eAAW,EAAC,8BAA8B,CAAC,CAAC;AAC1D,MAAM,QAAQ,GAAG,IAAA,qBAAS,EAAC,mBAAS,CAAC,CAAC;AAEtC,MAAqB,cAAc;IAC1B,IAAI,CAAqB;IACzB,MAAM,CAAS;IACf,MAAM,CAAe;IACrB,WAAW,CAAuC;IAE1D,YAAY,EAAU,EAAE,MAAoB;QAC3C,MAAM,OAAO,GAAG;YACf,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC;YACrC,OAAO,EAAE,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC;SACvC,CAAC;QACF,MAAM,I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CAAC;YACP,sDAAsD;YACtD,sDAAsD;YACtD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YACzB,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/B,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAED,KAAK,CAAC,OAAO;QACZ,KAAK,CAAC,eAAe,CAAC,CAAC;QACvB,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACnB,CAAC;CACD;AAjOD,iCAiOC"}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -28,6 +19,13 @@ function send404(res) {
|
|
|
28
19
|
res.end();
|
|
29
20
|
}
|
|
30
21
|
class RuntimeServer extends node_http_1.Server {
|
|
22
|
+
version;
|
|
23
|
+
initDeferred;
|
|
24
|
+
resultDeferred;
|
|
25
|
+
nextDeferred;
|
|
26
|
+
invokeDeferred;
|
|
27
|
+
lambda;
|
|
28
|
+
currentRequestId;
|
|
31
29
|
constructor(fn) {
|
|
32
30
|
super();
|
|
33
31
|
this.version = '2018-06-01';
|
|
@@ -43,141 +41,129 @@ class RuntimeServer extends node_http_1.Server {
|
|
|
43
41
|
this.resultDeferred = null;
|
|
44
42
|
this.currentRequestId = (0, node_crypto_1.randomUUID)();
|
|
45
43
|
}
|
|
46
|
-
serve(req, res) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
async serve(req, res) {
|
|
45
|
+
debug('%s %s', req.method, req.url);
|
|
46
|
+
const result = matchFn((0, node_url_1.parse)(req.url).pathname);
|
|
47
|
+
if (!result) {
|
|
48
|
+
return send404(res);
|
|
49
|
+
}
|
|
50
|
+
const { version, subject, target, action } = result.params;
|
|
51
|
+
if (this.version !== version) {
|
|
52
|
+
debug('Invalid API version, expected %o but got %o', this.version, version);
|
|
53
|
+
return send404(res);
|
|
54
|
+
}
|
|
55
|
+
// Routing logic
|
|
56
|
+
if (subject === 'invocation') {
|
|
57
|
+
if (target === 'next') {
|
|
58
|
+
return this.handleNextInvocation(req, res);
|
|
57
59
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
return this.
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
// Assume it's an "AwsRequestId"
|
|
65
|
-
if (action === 'response') {
|
|
66
|
-
return this.handleInvocationResponse(req, res, target);
|
|
67
|
-
}
|
|
68
|
-
else if (action === 'error') {
|
|
69
|
-
return this.handleInvocationError(req, res, target);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
return send404(res);
|
|
73
|
-
}
|
|
60
|
+
else {
|
|
61
|
+
// Assume it's an "AwsRequestId"
|
|
62
|
+
if (action === 'response') {
|
|
63
|
+
return this.handleInvocationResponse(req, res, target);
|
|
74
64
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (target === 'error') {
|
|
78
|
-
return this.handleInitializationError(req, res);
|
|
65
|
+
else if (action === 'error') {
|
|
66
|
+
return this.handleInvocationError(req, res, target);
|
|
79
67
|
}
|
|
80
68
|
else {
|
|
81
69
|
return send404(res);
|
|
82
70
|
}
|
|
83
71
|
}
|
|
72
|
+
}
|
|
73
|
+
else if (subject === 'init') {
|
|
74
|
+
if (target === 'error') {
|
|
75
|
+
return this.handleInitializationError(req, res);
|
|
76
|
+
}
|
|
84
77
|
else {
|
|
85
78
|
return send404(res);
|
|
86
79
|
}
|
|
87
|
-
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return send404(res);
|
|
83
|
+
}
|
|
88
84
|
}
|
|
89
|
-
handleNextInvocation(req, res) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
yield finish;
|
|
114
|
-
});
|
|
85
|
+
async handleNextInvocation(req, res) {
|
|
86
|
+
const { initDeferred } = this;
|
|
87
|
+
if (initDeferred) {
|
|
88
|
+
debug('Runtime successfully initialized');
|
|
89
|
+
this.initDeferred = null;
|
|
90
|
+
initDeferred.resolve();
|
|
91
|
+
}
|
|
92
|
+
this.invokeDeferred = (0, deferred_1.createDeferred)();
|
|
93
|
+
this.resultDeferred = (0, deferred_1.createDeferred)();
|
|
94
|
+
this.nextDeferred.resolve();
|
|
95
|
+
this.nextDeferred = null;
|
|
96
|
+
debug('Waiting for the `invoke()` function to be called');
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
req.setTimeout(0); // disable default 2 minute socket timeout
|
|
99
|
+
const params = await this.invokeDeferred.promise;
|
|
100
|
+
// TODO: use dynamic values from lambda params
|
|
101
|
+
const deadline = 5000;
|
|
102
|
+
const functionArn = 'arn:aws:lambda:us-west-1:977805900156:function:nate-dump';
|
|
103
|
+
res.setHeader('Lambda-Runtime-Aws-Request-Id', this.currentRequestId);
|
|
104
|
+
res.setHeader('Lambda-Runtime-Invoked-Function-Arn', functionArn);
|
|
105
|
+
res.setHeader('Lambda-Runtime-Deadline-Ms', String(deadline));
|
|
106
|
+
const finish = (0, once_1.default)(res, 'finish');
|
|
107
|
+
res.end(params.Payload);
|
|
108
|
+
await finish;
|
|
115
109
|
}
|
|
116
|
-
handleInvocationResponse(req, res, requestId) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
this.resetInvocationState();
|
|
133
|
-
});
|
|
110
|
+
async handleInvocationResponse(req, res, requestId) {
|
|
111
|
+
// `RequestResponse` = 200
|
|
112
|
+
// `Event` = 202
|
|
113
|
+
// `DryRun` = 204
|
|
114
|
+
const statusCode = 200;
|
|
115
|
+
const payload = {
|
|
116
|
+
StatusCode: statusCode,
|
|
117
|
+
ExecutedVersion: '$LATEST',
|
|
118
|
+
Payload: await (0, micro_1.text)(req, { limit: '6mb' })
|
|
119
|
+
};
|
|
120
|
+
res.statusCode = 202;
|
|
121
|
+
const finish = (0, once_1.default)(res, 'finish');
|
|
122
|
+
res.end();
|
|
123
|
+
await finish;
|
|
124
|
+
this.resultDeferred.resolve(payload);
|
|
125
|
+
this.resetInvocationState();
|
|
134
126
|
}
|
|
135
|
-
handleInvocationError(req, res, requestId) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
this.resetInvocationState();
|
|
150
|
-
});
|
|
127
|
+
async handleInvocationError(req, res, requestId) {
|
|
128
|
+
const statusCode = 200;
|
|
129
|
+
const payload = {
|
|
130
|
+
StatusCode: statusCode,
|
|
131
|
+
FunctionError: 'Handled',
|
|
132
|
+
ExecutedVersion: '$LATEST',
|
|
133
|
+
Payload: await (0, micro_1.text)(req, { limit: '6mb' })
|
|
134
|
+
};
|
|
135
|
+
res.statusCode = 202;
|
|
136
|
+
const finish = (0, once_1.default)(res, 'finish');
|
|
137
|
+
res.end();
|
|
138
|
+
await finish;
|
|
139
|
+
this.resultDeferred.resolve(payload);
|
|
140
|
+
this.resetInvocationState();
|
|
151
141
|
}
|
|
152
|
-
handleInitializationError(req, res) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.initDeferred.resolve(payload);
|
|
166
|
-
});
|
|
142
|
+
async handleInitializationError(req, res) {
|
|
143
|
+
const statusCode = 200;
|
|
144
|
+
const payload = {
|
|
145
|
+
StatusCode: statusCode,
|
|
146
|
+
FunctionError: 'Unhandled',
|
|
147
|
+
ExecutedVersion: '$LATEST',
|
|
148
|
+
Payload: await (0, micro_1.text)(req, { limit: '6mb' })
|
|
149
|
+
};
|
|
150
|
+
res.statusCode = 202;
|
|
151
|
+
const finish = (0, once_1.default)(res, 'finish');
|
|
152
|
+
res.end();
|
|
153
|
+
await finish;
|
|
154
|
+
this.initDeferred.resolve(payload);
|
|
167
155
|
}
|
|
168
|
-
invoke(params = { InvocationType: 'RequestResponse' }) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return result;
|
|
180
|
-
});
|
|
156
|
+
async invoke(params = { InvocationType: 'RequestResponse' }) {
|
|
157
|
+
if (this.nextDeferred) {
|
|
158
|
+
debug('Waiting for `next` invocation request from runtime');
|
|
159
|
+
await this.nextDeferred.promise;
|
|
160
|
+
}
|
|
161
|
+
if (!params.Payload) {
|
|
162
|
+
params.Payload = '{}';
|
|
163
|
+
}
|
|
164
|
+
this.invokeDeferred.resolve(params);
|
|
165
|
+
const result = await this.resultDeferred.promise;
|
|
166
|
+
return result;
|
|
181
167
|
}
|
|
182
168
|
close(callback) {
|
|
183
169
|
const deferred = this.initDeferred || this.resultDeferred;
|