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
3 changes: 2 additions & 1 deletion src/receivers/AwsLambdaReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default class AwsLambdaReceiver implements Receiver {

// Setup ack timeout warning
let isAcknowledged = false;
setTimeout(() => {
const noAckTimeoutId = setTimeout(() => {
if (!isAcknowledged) {
this.logger.error(
'An incoming event was not acknowledged within 3 seconds. ' +
Expand All @@ -172,6 +172,7 @@ export default class AwsLambdaReceiver implements Receiver {
throw new ReceiverMultipleAckError();
}
isAcknowledged = true;
clearTimeout(noAckTimeoutId);
if (typeof response === 'undefined' || response == null) {
storedResponse = '';
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/receivers/HTTPResponseAck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ describe('HTTPResponseAck', async () => {
done();
}, 2);
});
it('should not trigger unhandledRequestHandler if acknowledged', (done) => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const spy = sinon.spy();
// eslint-disable-next-line no-new
const responseAck = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: false,
unhandledRequestTimeoutMillis: 1,
unhandledRequestHandler: spy,
httpRequest,
httpResponse,
});
responseAck.ack();
setTimeout(() => {
assert(spy.notCalled);
done();
}, 2);
});
it('should throw an error if a bound Ack invocation was already acknowledged', async () => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
Expand Down
8 changes: 7 additions & 1 deletion src/receivers/HTTPResponseAck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class HTTPResponseAck {

private httpResponse: ServerResponse;

private noAckTimeoutId?: NodeJS.Timeout;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public storedResponse: any | string | undefined;

Expand All @@ -43,11 +45,12 @@ export class HTTPResponseAck {
this.httpRequest = args.httpRequest;
this.httpResponse = args.httpResponse;
this.storedResponse = undefined;
this.noAckTimeoutId = undefined;
this.init();
}

private init(): HTTPResponseAck {
setTimeout(() => {
this.noAckTimeoutId = setTimeout(() => {
if (!this.isAcknowledged) {
this.unhandledRequestHandler({
logger: this.logger,
Expand Down Expand Up @@ -84,5 +87,8 @@ export class HTTPResponseAck {

public ack(): void {
this.isAcknowledged = true;
if (this.noAckTimeoutId) {
clearTimeout(this.noAckTimeoutId);
}
}
}