UNPKG

event-source-hook

Version:

Easily intercept, modify, and simulate EventSource server-sent events.

53 lines (52 loc) 2.75 kB
import { HookCreateFunction, HookedEventSource, HookEventFunction, HookUrlFunction } from "./interfaces"; /** * Create a spoofed `EventSource` object for hook. Swapped with the native `EventSource`. * @constructor */ declare function HookedEventSource(url: string | URL, eventSourceInitDict?: EventSourceInit): HookedEventSource; /** Library `event-source-hook` static class that provides `EventSource` hooking utilities. */ declare class ESHook { private static _urlHook; private static _createHook; private static _eventHook; /** Hook function invoked just before a connection is established. */ static get urlHook(): HookUrlFunction | null; /** Hook function invoked just before a connection is established. */ static set urlHook(func: HookUrlFunction | null); /** Hook function invoked just before a connection is established. */ static get createHook(): HookCreateFunction | null; /** Hook function invoked just before a connection is established. */ static set createHook(func: HookCreateFunction | null); /** Hook function invoked just before an event is received on any connection. */ static get eventHook(): HookEventFunction | null; /** Hook function invoked just before an event is received on any connection. */ static set eventHook(func: HookEventFunction | null); /** Is the provided event hook function async? */ static get isEventHookAsync(): boolean; /** Is the native `EventSource` spoofed by the library? */ static get enabled(): boolean; /** * Swap the native `EventSource` constructor with a spoofed one. * Any opened connections will be spoofed by the library while enabled. */ static enable(): void; /** * Swap the native `EventSource` constructor back in. * Any opened connections while disabled will not be spoofed by the library even after re-enabling. * @note Hook event function will still be called while disabled. */ static disable(): void; /** * Reset all hooks function to none. */ static resetHooks(): void; /** * Simulate a received event. It will be handled as if it was an genuine event received from the server. * @note `simulated` and `isTrusted` properties are set to `true` on the `MessageEvent` object. * @param eventSource - Connection where the event should be received. * @param type - Event type. Use `message` if you want a non-typed event (default type). * @param options - Options to be passed to the event (such as data). */ static simulate(eventSource: EventSource | HookedEventSource, type: string, options?: MessageEventInit): void; } export default ESHook;