forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackContext.js
146 lines (133 loc) · 4.18 KB
/
CallbackContext.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
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
const BeforeExitListener = require('./BeforeExitListener.js');
const { structuredConsole } = require('./LogPatch');
/**
* Build the callback function and the part of the context which exposes
* the succeed/fail/done callbacks.
* @param client {RuntimeClient}
* The RAPID client used to post results/errors.
* @param id {string}
* The invokeId for the current invocation.
* @param scheduleNext {function}
* A function which takes no params and immediately schedules the next
* iteration of the invoke loop.
*/
function _rawCallbackContext(client, id, scheduleNext) {
const postError = (err, callback) => {
structuredConsole.logError('Invoke Error', err);
client.postInvocationError(err, id, callback);
};
let isCompleteInvoked = false;
const complete = (result, callback) => {
if (isCompleteInvoked) {
console.error(
'Invocation has already been reported as done. Cannot call complete more than once per invocation.',
);
return;
}
isCompleteInvoked = true;
client.postInvocationResponse(result, id, callback);
};
let waitForEmptyEventLoop = true;
const callback = function (err, result) {
BeforeExitListener.reset();
if (err !== undefined && err !== null) {
postError(err, scheduleNext);
} else {
if (!waitForEmptyEventLoop) {
complete(result, scheduleNext);
} else {
BeforeExitListener.set(() => {
setImmediate(() => {
complete(result, scheduleNext);
});
});
}
}
};
const done = (err, result) => {
BeforeExitListener.reset();
if (err !== undefined && err !== null) {
postError(err, scheduleNext);
} else {
complete(result, scheduleNext);
}
};
const succeed = (result) => {
done(null, result);
};
const fail = (err) => {
if (err === undefined || err === null) {
done('handled');
} else {
done(err, null);
}
};
const callbackContext = {
get callbackWaitsForEmptyEventLoop() {
return waitForEmptyEventLoop;
},
set callbackWaitsForEmptyEventLoop(value) {
waitForEmptyEventLoop = value;
},
succeed: succeed,
fail: fail,
done: done,
};
return [
callback,
callbackContext,
function () {
isCompleteInvoked = true;
},
];
}
/**
* Wraps the callback and context so that only the first call to any callback
* succeeds.
* @param callback {function}
* the node-style callback function that was previously generated but not
* yet wrapped.
* @param callbackContext {object}
* The previously generated callbackContext object that contains
* getter/setters for the contextWaitsForEmptyeventLoop flag and the
* succeed/fail/done functions.
* @return [callback, context]
*/
function _wrappedCallbackContext(callback, callbackContext, markCompleted) {
let finished = false;
const onlyAllowFirstCall = function (toWrap) {
return function () {
if (!finished) {
toWrap.apply(null, arguments);
finished = true;
}
};
};
callbackContext.succeed = onlyAllowFirstCall(callbackContext.succeed);
callbackContext.fail = onlyAllowFirstCall(callbackContext.fail);
callbackContext.done = onlyAllowFirstCall(callbackContext.done);
return [onlyAllowFirstCall(callback), callbackContext, markCompleted];
}
/**
* Construct the base-context object which includes the required flags and
* callback methods for the Node programming model.
* @param client {RAPIDClient}
* The RAPID client used to post results/errors.
* @param id {string}
* The invokeId for the current invocation.
* @param scheduleNext {function}
* A function which takes no params and immediately schedules the next
* iteration of the invoke loop.
* @return [callback, context]
* The same function and context object, but wrapped such that only the
* first call to any function will be successful. All subsequent calls are
* a no-op.
*/
module.exports.build = function (client, id, scheduleNext) {
let rawCallbackContext = _rawCallbackContext(client, id, scheduleNext);
return _wrappedCallbackContext(...rawCallbackContext);
};