Skip to content

Commit

Permalink
Add context overwrite option (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazoking authored Apr 13, 2024
1 parent 5ad5685 commit 53814cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Executes a lambda given the `options` object, which is a dictionary where the ke
| `callback`|optional, lambda third parameter [callback][1]. When left out a Promise is returned|
| `onInvocationEnd`|optional. called once the invocation ended. useful when awslambda.streamifyResponse is used to distinguish between end of response stream and end of invocation. |
| `clientContext`|optional, used to populated clientContext property of lambda second parameter (context)
| `contextOverwrite`| optional, a function that overwrites the context object. It can get and overwrite the values of the context (such as awsRequestId). |

#### `lambdaLocal.setLogger(logger)`
#### `lambdaLocal.getLogger()`
Expand Down
3 changes: 3 additions & 0 deletions src/lambdalocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function _executeSync(opts) {
timeoutMs = opts.timeoutMs || 3000,
verboseLevel = opts.verboseLevel,
callback = opts.callback,
contextOverwrite = opts.contextOverwrite,
onInvocationEnd = opts.onInvocationEnd,
clientContext = null;

Expand Down Expand Up @@ -304,6 +305,8 @@ function _executeSync(opts) {

var ctx = context.generate_context();

if(contextOverwrite) opts.contextOverwrite(ctx);

const executeLambdaFunc = lambdaFunc => {
try {
//load event
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@ describe("- Testing lambdalocal.js", function () {
});
});
});
describe('* Context overwrite', function () {
it("Should overwrite context", function(cb){
var lambdalocal = require(lambdalocal_path);
lambdalocal.execute({
event: require(path.join(__dirname, "./events/test-event.js")),
lambdaPath: path.join(__dirname, "./functs/test-func.js"),
lambdaHandler: functionName,
contextOverwrite: function (ctx){
ctx.awsRequestId = "test id";
ctx.functionName = "test function";
},
callback: function (err, done) {
assert.equal(done.result, "testvar");
assert.equal(done.context.awsRequestId, "test id");
assert.equal(done.context.functionName, "test function");
cb();
}
});
});
});
describe('* Nested Run', function () {
it("Should handle nested calls properly (context singleton)", function (cb) {
var lambdalocal = require(lambdalocal_path);
Expand Down

0 comments on commit 53814cb

Please sign in to comment.