-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
313 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Context } from 'hono'; | ||
|
||
import type { HonoContextVariables } from '../../../app'; | ||
import { EVENT_MQ_MESSAGE_RECEIVED } from '../../../constants'; | ||
import { MqMessageReceivedEvent } from '../../../events/mq-message-received-event'; | ||
|
||
export async function handleMessageAction( | ||
ctx: Context<{ Variables: HonoContextVariables }>, | ||
): Promise<Response> { | ||
const logger = ctx.get('logger'); | ||
const eventBus = ctx.get('eventBus'); | ||
|
||
const json = await ctx.req.json(); | ||
|
||
// If no listeners are attached, we should not process the message | ||
if (eventBus.listenerCount(EVENT_MQ_MESSAGE_RECEIVED) === 0) { | ||
logger.info( | ||
`No event listeners attached to [${EVENT_MQ_MESSAGE_RECEIVED}], nacking incoming message [PubSub ID: ${json.message.message_id}]`, | ||
); | ||
|
||
return new Response(null, { status: 429 }); | ||
} | ||
|
||
// Return a promise that will eventually resolve or reject when the | ||
// message is ack'd or nack'd | ||
return await new Promise<Response>((resolve, reject) => { | ||
const event = new MqMessageReceivedEvent({ | ||
id: json.message.message_id, | ||
subscriptionIdentifier: json.subscription, | ||
data: JSON.parse( | ||
Buffer.from(json.message.data, 'base64').toString(), | ||
), | ||
attributes: json.message.attributes, | ||
onAck: () => resolve(new Response(null, { status: 200 })), | ||
onNack: () => reject(new Response(null, { status: 500 })), | ||
}); | ||
|
||
eventBus.emit(EVENT_MQ_MESSAGE_RECEIVED, event); | ||
|
||
// TODO: Handle timeout | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
interface MqMessageReceivedEventOptions { | ||
id: string; | ||
subscriptionIdentifier: string; | ||
data: string; | ||
attributes: Record<string, string>; | ||
onAck: () => void; | ||
onNack: () => void; | ||
} | ||
|
||
export class MqMessageReceivedEvent { | ||
readonly id: string; | ||
readonly subscriptionIdentifier: string; | ||
readonly data: string; | ||
readonly attributes: Record<string, string>; | ||
private onAck: () => void; | ||
private onNack: () => void; | ||
|
||
constructor({ | ||
id, | ||
subscriptionIdentifier, | ||
data, | ||
attributes, | ||
onAck, | ||
onNack, | ||
}: MqMessageReceivedEventOptions) { | ||
this.id = id; | ||
this.subscriptionIdentifier = subscriptionIdentifier; | ||
this.data = data; | ||
this.attributes = attributes; | ||
this.onAck = onAck; | ||
this.onNack = onNack; | ||
} | ||
|
||
ack() { | ||
this.onAck(); | ||
} | ||
|
||
nack() { | ||
this.onNack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.