generated from vtex-apps/service-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from vtex-apps/feature/event-broadcaster
Feature/event broadcaster
- Loading branch information
Showing
16 changed files
with
318 additions
and
14 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,12 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema#", | ||
"title": "Event Registry", | ||
"type": "object", | ||
"properties": { | ||
"hasBeenProcessed": { | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": ["hasBeenProcessed"], | ||
"v-cache": false | ||
} |
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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
import { IOClients } from '@vtex/api' | ||
import { masterDataFor } from '@vtex/clients' | ||
import type { EventRegistry } from 'vtex.spreadsheet-event-broadcaster' | ||
|
||
export class Clients extends IOClients {} | ||
export class Clients extends IOClients { | ||
public get eventRegistry() { | ||
return this.getOrSet( | ||
'eventRegistry', | ||
masterDataFor<EventRegistry>('eventRegistry') | ||
) | ||
} | ||
} |
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,20 @@ | ||
import { OUTPUT_EVENT_KEY as eventKey } from '../../utils/constants' | ||
|
||
export async function broadcast( | ||
ctx: BroadcasterEventContext, | ||
next: () => Promise<unknown> | ||
) { | ||
const { | ||
clients: { events }, | ||
body: { payload, senderAppId, clientAppId }, | ||
} = ctx | ||
|
||
payload.forEach((row) => { | ||
events.sendEvent(clientAppId, eventKey, { | ||
data: row, | ||
senderAppId, | ||
}) | ||
}) | ||
|
||
await next() | ||
} |
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,39 @@ | ||
import { v4 as uuid } from 'uuid' | ||
|
||
import { | ||
APP_ID as thisAppId, | ||
EVENT_CHAIN_KEY as eventKey, | ||
ARRAY_SIZE_TARGET, | ||
ARRAY_SPLIT_FACTOR, | ||
} from '../../utils/constants' | ||
|
||
export async function splitPayload( | ||
ctx: BroadcasterEventContext, | ||
next: () => Promise<unknown> | ||
) { | ||
const { | ||
clients: { events }, | ||
body: { payload, senderAppId, clientAppId }, | ||
} = ctx | ||
|
||
if (payload.length > ARRAY_SIZE_TARGET) { | ||
const chunkSize = Math.ceil(payload.length / ARRAY_SPLIT_FACTOR) | ||
|
||
for (let i = 0; i < payload.length; i += chunkSize) { | ||
const eventId = uuid() | ||
|
||
const chunk = payload.slice(i, i + chunkSize) | ||
|
||
events.sendEvent(thisAppId, eventKey, { | ||
eventId, | ||
payload: chunk, | ||
senderAppId, | ||
clientAppId, | ||
}) | ||
} | ||
|
||
return | ||
} | ||
|
||
await next() | ||
} |
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,19 @@ | ||
import { EventRegistryService } from '../../services/eventRegistry' | ||
|
||
export async function verifyUniqueness( | ||
ctx: BroadcasterEventContext, | ||
next: () => Promise<unknown> | ||
) { | ||
const { | ||
clients: { eventRegistry }, | ||
body: { eventId }, | ||
} = ctx | ||
|
||
const registryService = new EventRegistryService(eventRegistry) | ||
|
||
const isUnique = await registryService.isEventUnique(eventId) | ||
|
||
if (!isUnique) return | ||
|
||
await next() | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"@vtex/clients": "^2.19.4", | ||
"async-busboy": "^1.1.0", | ||
"co-body": "^6.0.0", | ||
"get-stream": "^6.0.1", | ||
|
@@ -17,7 +18,8 @@ | |
"@vtex/api": "6.45.6", | ||
"@vtex/test-tools": "^1.0.0", | ||
"@vtex/tsconfig": "^0.5.6", | ||
"typescript": "3.9.7" | ||
"typescript": "3.9.7", | ||
"vtex.spreadsheet-event-broadcaster": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/_types/react" | ||
}, | ||
"scripts": { | ||
"lint": "tsc --noEmit --pretty" | ||
|
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,26 @@ | ||
import type { MasterDataEntity } from '@vtex/clients' | ||
import type { EventRegistry } from 'vtex.spreadsheet-event-broadcaster' | ||
|
||
import { MASTER_DATA_NO_CHANGES_RESPONSE_STATUS } from '../utils/constants' | ||
|
||
export class EventRegistryService { | ||
private client: MasterDataEntity<EventRegistry> | ||
constructor(client: MasterDataEntity<EventRegistry>) { | ||
this.client = client | ||
} | ||
|
||
public async isEventUnique(eventId: string) { | ||
try { | ||
await this.client.saveOrUpdate({ | ||
id: eventId, | ||
hasBeenProcessed: true, | ||
}) | ||
} catch (error) { | ||
if (error?.response?.status === MASTER_DATA_NO_CHANGES_RESPONSE_STATUS) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
export const APP_ID = '[email protected]' | ||
export const APP_ID = 'vtex.spreadsheet-event-broadcaster' | ||
|
||
export const EVENT_CHAIN_KEY = 'event.chain' | ||
|
||
export const OUTPUT_EVENT_KEY = 'row.output' | ||
|
||
export const MASTER_DATA_NO_CHANGES_RESPONSE_STATUS = 304 | ||
|
||
const _1GB = 1024 ** 3 | ||
|
||
export const MAXIMUM_FILE_SIZE = _1GB | ||
|
||
export const MAXIMUM_FILE_SIZE_STRING = '1GB' | ||
|
||
export const ARRAY_SIZE_TARGET = 1000 | ||
|
||
export const ARRAY_SPLIT_FACTOR = 100 |
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.