Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion collectInvocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
export default function collectInvocation(event, context, handlerType) {
const ctx = {};
if (context) {
for (const [key, value] of Object.entries(context)) {
const handle = (key, value) => {
if (typeof value === 'function') {
try {
ctx[key] = value.call(context);
Expand All @@ -24,6 +24,20 @@ export default function collectInvocation(event, context, handlerType) {
} else {
ctx[key] = value;
}
};

for (const [key, value] of Object.entries(context)) {
handle(key, value);
}

const proto = Object.getPrototypeOf(context);
if (proto && proto !== Object.prototype) {
for (const key of Reflect.ownKeys(proto)) {
if (key === 'constructor' || Object.prototype.hasOwnProperty.call(ctx, key)) {
continue;
}
handle(key, context[key]);
}
}
}
return { event, context: ctx, handlerType };
Expand Down
24 changes: 24 additions & 0 deletions tests/collectInvocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ describe('collectInvocation', () => {
handlerType: 'test'
});
});

test('captures prototype methods', () => {
const event = {};
class ProtoCtx {
protoMethod() { return 'proto'; }
protoFail() { throw new Error('oops'); }
}
const context = new ProtoCtx();
context.awsRequestId = 'req-2';
context.own = () => 'own';

const result = collectInvocation(event, context, 'test');

expect(result).toEqual({
event,
context: {
awsRequestId: 'req-2',
own: 'own',
protoMethod: 'proto',
protoFail: 'oops'
},
handlerType: 'test'
});
});
});
Loading