Skip to content

Commit 53814cb

Browse files
authored
Add context overwrite option (#240)
1 parent 5ad5685 commit 53814cb

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Executes a lambda given the `options` object, which is a dictionary where the ke
7272
| `callback`|optional, lambda third parameter [callback][1]. When left out a Promise is returned|
7373
| `onInvocationEnd`|optional. called once the invocation ended. useful when awslambda.streamifyResponse is used to distinguish between end of response stream and end of invocation. |
7474
| `clientContext`|optional, used to populated clientContext property of lambda second parameter (context)
75+
| `contextOverwrite`| optional, a function that overwrites the context object. It can get and overwrite the values of the context (such as awsRequestId). |
7576

7677
#### `lambdaLocal.setLogger(logger)`
7778
#### `lambdaLocal.getLogger()`

src/lambdalocal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ function _executeSync(opts) {
187187
timeoutMs = opts.timeoutMs || 3000,
188188
verboseLevel = opts.verboseLevel,
189189
callback = opts.callback,
190+
contextOverwrite = opts.contextOverwrite,
190191
onInvocationEnd = opts.onInvocationEnd,
191192
clientContext = null;
192193

@@ -304,6 +305,8 @@ function _executeSync(opts) {
304305

305306
var ctx = context.generate_context();
306307

308+
if(contextOverwrite) opts.contextOverwrite(ctx);
309+
307310
const executeLambdaFunc = lambdaFunc => {
308311
try {
309312
//load event

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,26 @@ describe("- Testing lambdalocal.js", function () {
301301
});
302302
});
303303
});
304+
describe('* Context overwrite', function () {
305+
it("Should overwrite context", function(cb){
306+
var lambdalocal = require(lambdalocal_path);
307+
lambdalocal.execute({
308+
event: require(path.join(__dirname, "./events/test-event.js")),
309+
lambdaPath: path.join(__dirname, "./functs/test-func.js"),
310+
lambdaHandler: functionName,
311+
contextOverwrite: function (ctx){
312+
ctx.awsRequestId = "test id";
313+
ctx.functionName = "test function";
314+
},
315+
callback: function (err, done) {
316+
assert.equal(done.result, "testvar");
317+
assert.equal(done.context.awsRequestId, "test id");
318+
assert.equal(done.context.functionName, "test function");
319+
cb();
320+
}
321+
});
322+
});
323+
});
304324
describe('* Nested Run', function () {
305325
it("Should handle nested calls properly (context singleton)", function (cb) {
306326
var lambdalocal = require(lambdalocal_path);

0 commit comments

Comments
 (0)