-
-
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
14 changed files
with
531 additions
and
374 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
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,55 @@ | ||
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(); | ||
const pubSubId = json?.message?.message_id ?? 'unknown'; | ||
|
||
// 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: ${pubSubId}]`, | ||
); | ||
|
||
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) => { | ||
let data = {}; | ||
|
||
try { | ||
data = JSON.parse( | ||
Buffer.from(json.message.data, 'base64').toString(), | ||
); | ||
} catch (error) { | ||
logger.error( | ||
`Failed to parse message data [PubSub ID: ${pubSubId}]: ${error}`, | ||
); | ||
|
||
return reject(new Response(null, { status: 500 })); | ||
} | ||
|
||
const event = new MqMessageReceivedEvent({ | ||
id: json.message.message_id, | ||
subscriptionIdentifier: json.subscription, | ||
data, | ||
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: Record<string, unknown>; | ||
attributes: Record<string, string>; | ||
onAck: () => void; | ||
onNack: () => void; | ||
} | ||
|
||
export class MqMessageReceivedEvent { | ||
readonly id: string; | ||
readonly subscriptionIdentifier: string; | ||
readonly data: Record<string, unknown>; | ||
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.