declare type AnyFunction = (...args: any[]) => any; declare type AnyObject = Record; export declare const channelGenerator: (controller: string, event: string, type: ChannelTypes) => string; declare type ChannelTypes = 'c' | 'i' | 's'; declare type ClientFunctionReturnType = Promise>>; export declare function createIpcClient(...args: ConstructorParameters): IpcClientController & IpcClientControllerProxy; export declare function createIpcServer(...args: ConstructorParameters): IpcServerController; /** * The error handler defaults to using the [serialize-error]{@link https://www.npmjs.com/package/serialize-error} * package to serialize and deserialize error objects. * * The serialize method must be defined in the main process and the * deserialize method must be defined in the renderer process. */ export declare const ErrorHandler: ErrorHandlerInterface; declare interface ErrorHandlerInterface { serialize(error: any): object; deserialize(errorObject: any): Error; } declare type ExactType = [T] extends [U] ? ([U] extends [T] ? true : false) : false; declare type FunctionParameters = T extends AnyFunction ? Parameters : any[]; declare type FunctionProperties = T extends AnyObject ? Extract>, string> : string; declare type FunctionPropertyNames = { [K in keyof T]: T[K] extends AnyFunction ? K : never; }[keyof T]; export declare interface InitOptions { /** * Auto register `IpcClientController`, default is `true`. * * If you do not use it, you need to manually call {@link IpcClientController.register} in the preload script. * * If you want to strictly control which IPC each renderer can use, you can disable auto register. */ autoRegister?: boolean; /** * If the value is greater than 0, use `contextBridge.exposeInIsolatedWorld` to * expose the global object in isolated world. */ isolatedWorldId?: number; } /** * Controller used in the renderer process */ export declare class IpcClientController { #private; /** * @param name {string} Controller name */ constructor(name: string); /** * Controller name */ get name(): string; /** * Use `ipcRenderer.invoke()` to call the function defined by `IpcServerController.handle()`. */ invoke>(name: K, ...args: FunctionParameters): ClientFunctionReturnType; postMessage>(event: K, message: any, transfer?: MessagePort[]): void; /** * Removes event listeners added using `on()` and `once()`. * * If the `listener` parameter is not provided, all listeners for * the corresponding event will be removed. */ off>(event: K, listener?: RendererEventListener): void; /** * Adds a specific client event listener, where the first parameter of * the listener is of type `Electron.IpcRendererEvent`. */ on>(event: K, listener: RendererEventListener): void; /** * Like `on()`, but the listener is removed after it is triggered once. * * @see {on} */ once>(event: K, listener: RendererEventListener): void; /** * Uses `ipcRenderer.send()` to send to the server event listeners added with `IpcServerController.on()`. */ send>(event: K, ...args: FunctionParameters): void; /** * If auto register is not enabled, use this method in the * **preload script** to manually register the controller. */ register(): void; /** * Can use this method in the **preload script** to unregister the controller */ unregister(): void; } declare type IpcClientControllerProxy = { [K in FunctionProperties as `\$${K}`]: T[K] extends AnyFunction ? (...args: FunctionParameters) => ClientFunctionReturnType : never; }; declare type IpcEventListener = (event: T, ...args: FunctionParameters) => ExactType extends true ? ReturnType : void; /** * Controller used in the main process */ export declare class IpcServerController { #private; /** * `IpcServerController` calls the trust handler when it receives a call or event. * If the trust handler returns false, an exception is thrown to the renderer process: "*Blocked by trust handler.*". * * * @see {trustHandler} */ static TrustHandler: TrustHandlerFunc>; /** * Used to specify which renderers the `IpcServerController.send` method sends events to. * by default, it does not send to any renderers. * * @see {webContentsGetter} */ static WebContentsGetter: WebContentsGetterFunc | undefined; /** * `Electron.IpcMain` used by all IpcServerController instances. * * @see {Electron.IpcMain} */ static IpcMain: Electron.IpcMain | undefined; /** * `Electron.IpcMain` used by this instance. * * @see {Electron.IpcMain} * @see {IpcServerController.IpcMain} */ ipcMain: Electron.IpcMain | undefined; /** * This controller specific trust handler. * * @see {TrustHandler} */ trustHandler: TrustHandlerFunc | undefined; /** * This controller specific webContents getter. * * @see {WebContentsGetter} */ webContentsGetter: WebContentsGetterFunc | undefined; /** * @param name {string} Controller name */ constructor(name: string); /** * Controller name */ get name(): string; /** * Use the `handle()` to set all properties of an object as function handlers, with the * property names being used as function names. * * If the value is set to `undefined` or if the property has been set previously, the * existing function handler will be removed using `removeHandler()` first. * * @see {handle} * @see {removeHandler} */ set handlers(value: Partial | undefined); /** * Uses `IpcMain.handle()` to add a specific function handler. */ handle>(name: K, handler: Functions[K]): void; /** * Like `handle()`, it will just pass the event object. * * @see {handle} */ handleWithEvent>(name: K, handler: MainInvokeEventHandler): void; /** * Uses `IpcMain.removeHandler()` to remove a specific function handler. */ removeHandler>(name: K): void; /** * Removes event listeners added using `on()` and `once()`. * * If the `listener` parameter is not provided, all listeners for * the corresponding event will be removed. */ off>(event: K, listener?: MainEventListener): void; /** * Adds a specific server event listener, where the first parameter of * the listener is of type `Electron.IpcMainEvent`. */ on>(event: K, listener: MainEventListener): void; /** * Like `on()`, but the listener is removed after it is triggered once. * * @see {on} */ once>(event: K, listener: MainEventListener): void; /** * Uses `WebContents.send()` to send to the server event listeners added with `IpcClientController.on()`. */ send>(event: K, ...args: FunctionParameters): void; /** * Like `send()`, but use `WebContents.sendToFrame()`. * * @see {send} */ sendToFrame>(frameId: number | [number, number], event: K, ...args: FunctionParameters): void; } declare type MainEventListener = IpcEventListener; declare type MainInvokeEventHandler = IpcEventListener; /** * Initialize the required global objects (must be called in the preload script). */ export declare function preloadInit(contextBridge: Electron.ContextBridge, ipcRenderer: Electron.IpcRenderer, options?: InitOptions): void; declare type RendererEventListener = IpcEventListener; declare type TrustHandlerFunc> = { (controller: Controller, name: Parameters[0], type: 'event', event: Electron.IpcMainEvent): Promise | boolean; (controller: Controller, name: Parameters[0], type: 'invoke', event: Electron.IpcMainInvokeEvent): Promise | boolean; }; declare type WebContentsGetterFunc = () => Promise | Electron.WebContents[]; export { }