|
| 1 | +import type { ConsolaInstance } from 'consola' |
| 2 | +import Echo from 'laravel-echo' |
| 3 | +import type { Channel, ChannelAuthorizationCallback, Options } from 'pusher-js' |
| 4 | +import type { ChannelAuthorizationData } from 'pusher-js/types/src/core/auth/options' |
| 5 | +import type { Authentication, ModuleOptions } from '../types/options' |
| 6 | +import { createFetchClient } from './http' |
| 7 | +import { createError, type NuxtApp } from '#app' |
| 8 | + |
| 9 | +/** |
| 10 | + * Creates an authorizer function for the Echo instance. |
| 11 | + * @param app The Nuxt application instance |
| 12 | + * @param authentication The authentication options |
| 13 | + * @param logger The logger instance |
| 14 | + */ |
| 15 | +function createAuthorizer( |
| 16 | + app: NuxtApp, |
| 17 | + authentication: Required<Authentication>, |
| 18 | + logger: ConsolaInstance |
| 19 | +) { |
| 20 | + const client = createFetchClient(app, authentication, logger) |
| 21 | + |
| 22 | + return (channel: Channel, _: Options) => { |
| 23 | + return { |
| 24 | + authorize: (socketId: string, callback: ChannelAuthorizationCallback) => { |
| 25 | + const payload = JSON.stringify({ |
| 26 | + socket_id: socketId, |
| 27 | + channel_name: channel.name, |
| 28 | + }) |
| 29 | + |
| 30 | + client<ChannelAuthorizationData>(authentication.authEndpoint, { |
| 31 | + method: 'post', |
| 32 | + body: payload, |
| 33 | + }) |
| 34 | + .then((response: ChannelAuthorizationData) => |
| 35 | + callback(null, response) |
| 36 | + ) |
| 37 | + .catch((error: Error | null) => callback(error, null)) |
| 38 | + }, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Prepares the options for the Echo instance. |
| 45 | + * Returns Pusher or Reverb options based on the broadcaster. |
| 46 | + * @param app The Nuxt application instance |
| 47 | + * @param config The module options |
| 48 | + * @param logger The logger instance |
| 49 | + */ |
| 50 | +function prepareEchoOptions(app: NuxtApp, config: ModuleOptions, logger: ConsolaInstance) { |
| 51 | + const forceTLS = config.scheme === 'https' |
| 52 | + const additionalOptions = config.properties || {} |
| 53 | + |
| 54 | + const authorizer = config.authentication |
| 55 | + ? createAuthorizer( |
| 56 | + app, |
| 57 | + config.authentication as Required<Authentication>, |
| 58 | + logger |
| 59 | + ) |
| 60 | + : undefined |
| 61 | + |
| 62 | + // Create a Pusher instance |
| 63 | + if (config.broadcaster === 'pusher') { |
| 64 | + if (!forceTLS) { |
| 65 | + throw createError('Pusher requires a secure connection (schema: "https")') |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + broadcaster: config.broadcaster, |
| 70 | + key: config.key, |
| 71 | + cluster: config.cluster, |
| 72 | + forceTLS, |
| 73 | + authorizer, |
| 74 | + ...additionalOptions, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Create a Reverb instance |
| 79 | + return { |
| 80 | + broadcaster: config.broadcaster, |
| 81 | + key: config.key, |
| 82 | + wsHost: config.host, |
| 83 | + wsPort: config.port, |
| 84 | + wssPort: config.port, |
| 85 | + forceTLS, |
| 86 | + enabledTransports: config.transports, |
| 87 | + authorizer, |
| 88 | + ...additionalOptions, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns a new instance of Echo with configured authentication. |
| 94 | + * @param app The Nuxt application instance |
| 95 | + * @param config The module options |
| 96 | + * @param logger The logger instance |
| 97 | + */ |
| 98 | +export function createEcho(app: NuxtApp, config: ModuleOptions, logger: ConsolaInstance) { |
| 99 | + const options = prepareEchoOptions(app, config, logger) |
| 100 | + |
| 101 | + return new Echo(options) |
| 102 | +} |
0 commit comments