Skip to content

Commit

Permalink
Add senderAppId param to broadcaster
Browse files Browse the repository at this point in the history
  • Loading branch information
gabiruuuuu committed Feb 11, 2022
1 parent da1916d commit 381e0ef
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
4 changes: 3 additions & 1 deletion node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ declare global {

interface State extends RecorderState {
payload: unknown[]
appId: string
senderAppId: string
clientAppId: string
}

interface BroadcasterEventContext extends EventContext<Clients> {
body: {
eventId: string
payload: unknown[]
senderAppId: string
clientAppId: string
}
}
Expand Down
3 changes: 2 additions & 1 deletion node/middlewares/broadcaster/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export async function broadcast(
) {
const {
clients: { events },
body: { payload, clientAppId },
body: { payload, senderAppId, clientAppId },
} = ctx

payload.forEach((row) => {
events.sendEvent(clientAppId, eventKey, {
data: row,
senderAppId,
})
})

Expand Down
3 changes: 2 additions & 1 deletion node/middlewares/broadcaster/splitPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function splitPayload(
) {
const {
clients: { events },
body: { payload, clientAppId },
body: { payload, senderAppId, clientAppId },
} = ctx

if (payload.length > ARRAY_SIZE_TARGET) {
Expand All @@ -27,6 +27,7 @@ export async function splitPayload(
events.sendEvent(thisAppId, eventKey, {
eventId,
payload: chunk,
senderAppId,
clientAppId,
})
}
Expand Down
9 changes: 6 additions & 3 deletions node/middlewares/notify/parseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import {
MAXIMUM_FILE_SIZE,
MAXIMUM_FILE_SIZE_STRING,
} from '../../utils/constants'
import { asyncBusboyWrapper } from '../../utils/parsing'
import { asyncBusboyWrapper, senderAppIdFromHeaders } from '../../utils/parsing'

export async function parseFile(ctx: Context, next: () => Promise<unknown>) {
const {
req,
vtex: { logger },
} = ctx

const senderAppId = senderAppIdFromHeaders(req.headers)

const {
fields: { appId },
fields: { appId: clientAppId },
files: [file],
} = await asyncBusboyWrapper<NotifyInputParameters>(req)

Expand Down Expand Up @@ -55,7 +57,8 @@ export async function parseFile(ctx: Context, next: () => Promise<unknown>) {
const payload = utils.sheet_to_json(sheet)

ctx.state.payload = payload
ctx.state.appId = appId ?? ''
ctx.state.senderAppId = senderAppId
ctx.state.clientAppId = clientAppId ?? ''
} catch (error) {
logger.error(error.message)

Expand Down
3 changes: 2 additions & 1 deletion node/middlewares/notify/startEventChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

export async function startEventChain(ctx: Context) {
const {
state: { payload, appId: clientAppId },
state: { payload, senderAppId, clientAppId },
clients: { events },
} = ctx

Expand All @@ -16,6 +16,7 @@ export async function startEventChain(ctx: Context) {
events.sendEvent(thisAppId, eventKey, {
eventId,
payload,
senderAppId,
clientAppId,
})

Expand Down
6 changes: 6 additions & 0 deletions node/utils/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export async function asyncBusboyWrapper<T>(req: IncomingMessage): Promise<T> {

return (result as unknown) as T
}

export function senderAppIdFromHeaders(headers: IncomingMessage['headers']) {
const appIdWithVersion = headers['x-vtex-caller'] as string

return appIdWithVersion.split('@')[0]
}

0 comments on commit 381e0ef

Please sign in to comment.