UNPKG

@boringnode/transmit

Version:

A framework agnostic Server-Sent-Event library

62 lines (61 loc) 2.09 kB
import { Stream } from './stream.js'; import type { IncomingMessage, ServerResponse } from 'node:http'; import type { AccessCallback } from './types/main.js'; interface OnConnectParams<Context> { uid: string; context?: Context; } interface OnDisconnectParams<Context> { uid: string; context?: Context; } interface OnSubscribeParams<Context> { uid: string; channel: string; context: Context; } interface OnUnsubscribeParams<Context> { uid: string; channel: string; context: Context; } export interface CreateStreamParams<Context> { uid: string; request: IncomingMessage; response: ServerResponse; context: Context; injectResponseHeaders?: Record<string, any>; onConnect?: (params: OnConnectParams<Context>) => void; onDisconnect?: (params: OnDisconnectParams<Context>) => void; } export interface SubscribeParams<Context> { uid: string; channel: string; context?: Context; skipAuthorization?: boolean; onSubscribe?: (params: OnSubscribeParams<Context>) => void; } export interface UnsubscribeParams<Context> { uid: string; channel: string; context?: Context; onUnsubscribe?: (params: OnUnsubscribeParams<Context>) => void; } export declare class StreamManager<Context extends unknown> { #private; constructor(); createStream({ uid, context, request, response, injectResponseHeaders, onConnect, onDisconnect, }: CreateStreamParams<Context>): Stream; subscribe({ uid, channel, context, skipAuthorization, onSubscribe, }: SubscribeParams<Context>): Promise<boolean>; unsubscribe({ uid, channel, context, onUnsubscribe }: UnsubscribeParams<Context>): Promise<boolean>; authorize<Params extends Record<string, string>>(channel: string, callback: AccessCallback<Context, Params>): void; verifyAccess(channel: string, context: Context): Promise<boolean>; /** * Get all subscribers */ getAllSubscribers(): Map<Stream, Set<string>>; /** * Find all subscribers to a channel */ findByChannel(channel: string): Set<Stream>; } export {};