Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 10x 10x 10x 10x | import logger from "../../utils/logger"; const PUSHER_KEY = process.env.CLIENT_PUSHER_KEY || "ff9f18c208855d77a152"; const PUSHER_CLUSTER = "mt1"; type Channel = { subscribe: (channelName: string) => unknown; unsubscribe: (channelName: string) => unknown; bind: ( event: string, callback: (data: Record<string, unknown>) => void ) => unknown; }; type PusherClient = { subscribe: (channelName: string) => Channel; unsubscribe: (channelName: string) => unknown; bind: ( event: string, callback: (data: Record<string, unknown>) => void ) => unknown; }; type TChunkedTriggerData = { id: string; index: number; chunk: string; final: boolean; }; export type TriggerData = { appName: string; clientId: number; payload: Record<string, unknown>; originalPayload: Record<string, unknown>; metadata: { id: string; connectionId: string; triggerName: string; triggerData: string; triggerConfig: Record<string, unknown>; connection: { id: string; integrationId: string; clientUniqueUserId: string; status: string; }; }; }; export class PusherUtils { static pusherClient: PusherClient; static getPusherClient(baseURL: string, apiKey: string): PusherClient { Iif (!PusherUtils.pusherClient) { // Dynamic import not available, using require for now // TODO: Update to use dynamic import when available // eslint-disable-next-line @typescript-eslint/no-require-imports const PusherClient = require("pusher-js"); PusherUtils.pusherClient = new PusherClient(PUSHER_KEY, { cluster: PUSHER_CLUSTER, channelAuthorization: { endpoint: `${baseURL}/api/v1/client/auth/pusher_auth`, headers: { "x-api-key": apiKey, }, transport: "ajax", }, }); } return PusherUtils.pusherClient; } /** * Subscribes to a Pusher channel and binds an event to a callback function. * @param {string} channelName - The name of the channel to subscribe to. * @param {string} event - The event to bind to the channel. * @param {(data: Record<string, unknown>) => void} fn - The callback function to execute when the event is triggered. * @returns {PusherClient} The Pusher client instance. */ static async subscribe( channelName: string, event: string, fn: (data: Record<string, unknown>) => void ): Promise<void> { try { await PusherUtils.pusherClient.subscribe(channelName).bind(event, fn); } catch (error) { logger.error( `Error subscribing to ${channelName} with event ${event}: ${error}` ); } } /** * Unsubscribes from a Pusher channel. * @param {string} channelName - The name of the channel to unsubscribe from. * @returns {void} */ static async unsubscribe(channelName: string): Promise<void> { PusherUtils.pusherClient.unsubscribe(channelName); } /** * Binds an event to a channel with support for chunked messages. * @param {PusherClient} channel - The Pusher channel to bind the event to. * @param {string} event - The event to bind to the channel. * @param {(data: unknown) => void} callback - The callback function to execute when the event is triggered. */ private static bindWithChunking( channel: PusherClient, event: string, callback: (data: Record<string, unknown>) => void ): void { channel.bind(event, callback); // Allow normal unchunked events. // Now the chunked variation. Allows arbitrarily long messages. const events: { [key: string]: { chunks: string[]; receivedFinal: boolean }; } = {}; channel.bind("chunked-" + event, (data) => { const typedData = data as TChunkedTriggerData; Iif (!events.hasOwnProperty(typedData.id)) { events[typedData.id] = { chunks: [], receivedFinal: false }; } const ev = events[typedData.id]; ev.chunks[typedData.index] = typedData.chunk; Iif (typedData.final) ev.receivedFinal = true; Iif ( ev.receivedFinal && ev.chunks.length === Object.keys(ev.chunks).length ) { callback(JSON.parse(ev.chunks.join(""))); delete events[typedData.id]; } }); } /** * Subscribes to a trigger channel for a client and handles chunked data. * @param {string} clientId - The unique identifier for the client subscribing to the events. * @param {(data: TriggerData) => void} fn - The callback function to execute when trigger data is received. */ static triggerSubscribe( clientId: string, fn: (data: TriggerData) => void ): void { const channel = PusherUtils.pusherClient.subscribe( `private-${clientId}_triggers` ); PusherUtils.bindWithChunking( channel as PusherClient, "trigger_to_client", fn as (data: unknown) => void ); logger.info( `Subscribed to triggers. You should start receiving events now.` ); } static triggerUnsubscribe(clientId: string): void { PusherUtils.pusherClient.unsubscribe(`${clientId}_triggers`); } } |