UNPKG

5.61 kBTypeScriptView Raw
1/**
2 * This file is part of the @egodigital/egoose distribution.
3 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
4 *
5 * @egodigital/egoose is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.
8 *
9 * @egodigital/egoose is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17/// <reference types="node" />
18import { Predicate } from '../index';
19import * as events from 'events';
20import * as http from 'http';
21import * as https from 'https';
22import * as ws from 'ws';
23/**
24 * Context of a client verifier.
25 */
26export interface WebSocketHostClientVerifierContext {
27 /**
28 * Indicates if connection is secure or not.
29 */
30 isSecure: boolean;
31 /**
32 * The request context.
33 */
34 request: http.IncomingMessage;
35}
36/**
37 * Verifies a remote client.
38 *
39 * @param {WebSocketHostClientVerifierContext} context The context.
40 *
41 * @return {boolean|PromiseLike<boolean>} The result, that indicates, if client is valid or not.
42 */
43export declare type WebSocketHostClientVerifier = (context: WebSocketHostClientVerifierContext) => boolean | PromiseLike<boolean>;
44/**
45 * Options for a 'WebSocketHost'.
46 */
47export interface WebSocketHostOptions {
48 /**
49 * The custom TCP port. Default: 5979
50 */
51 port?: number;
52 /**
53 * A factory function, that creates a custom server instance.
54 */
55 serverFactory?: WebSocketHostServerFactory;
56 /**
57 * An optional function to verify a client.
58 */
59 verifyClient?: WebSocketHostClientVerifier;
60}
61/**
62 * Describes a factory function, that creates a custom server instance.
63 *
64 * @return {WebSocketHostServerFactoryResult|PromiseLike<WebSocketHostServerFactoryResult>} The result with the new server instance.
65 */
66export declare type WebSocketHostServerFactory = () => WebSocketHostServerFactoryResult | PromiseLike<WebSocketHostServerFactoryResult>;
67/**
68 * A possible result of 'WebSocketHostServerFactory'.
69 */
70export declare type WebSocketHostServerFactoryResult = http.Server | https.Server;
71/**
72 * A web socket message.
73 */
74export interface WebSocketMessage<TData = any> {
75 /**
76 * The data.
77 */
78 data?: TData;
79 /**
80 * The type.
81 */
82 type: string;
83}
84/**
85 * Possible values for 'onType' first argument.
86 */
87export declare type WebSocketOnTypeCheckArgument = RegExp | string | Predicate<string>;
88/**
89 * An event for an 'onType' event.
90 *
91 * @param {TData} data The data.
92 * @param {string} type The type.
93 */
94export declare type WebSocketOnTypeEventListener<TData = any> = (data: TData, type: string) => void;
95/**
96 * A web socket host.
97 */
98export declare class WebSocketHost extends events.EventEmitter {
99 readonly options?: WebSocketHostOptions;
100 private _server;
101 /**
102 * Initializes a new instance of that class.
103 *
104 * @param {WebSocketHostOptions} [options] Custom options for the host.
105 */
106 constructor(options?: WebSocketHostOptions);
107 /**
108 * Gets if the server is currently running or not.
109 */
110 readonly isRunning: boolean;
111 /**
112 * Starts the host.
113 *
114 * @return {Promise<boolean>} The promise that indicates if operation was succesfull or not.
115 */
116 start(): Promise<boolean>;
117 /**
118 * Stops the host.
119 *
120 * @return {Promise<boolean>} The promise that indicates if operation was succesfull or not.
121 */
122 stop(): Promise<boolean>;
123}
124/**
125 * A web socket client.
126 */
127export declare class WebSocketClient extends events.EventEmitter {
128 readonly host: WebSocketHost;
129 readonly socket: ws;
130 /**
131 * Initializes a new instance of that class.
132 *
133 * @param {WebSocketHost} host The underlying host.
134 * @param {ws} socket The underlying socket.
135 */
136 constructor(host: WebSocketHost, socket: ws);
137 /**
138 * Initializes the instance.
139 */
140 init(): void;
141 /**
142 * Registers an event listener for a message type.
143 *
144 * @param {RegExp} pattern The Regex pattern, that checks the type.
145 * @param {WebSocketOnTypeEventListener<TData>} listener The listener.
146 *
147 * @return {Function} The "real" event listener.
148 */
149 onType<TData = any>(pattern: RegExp, listener: WebSocketOnTypeEventListener<TData>): Function;
150 /**
151 * Registers an event listener for a message type.
152 *
153 * @param {string} type The type.
154 * @param {WebSocketOnTypeEventListener<TData>} listener The listener.
155 *
156 * @return {Function} The "real" event listener.
157 */
158 onType<TData = any>(type: string, listener: WebSocketOnTypeEventListener<TData>): Function;
159 /**
160 * Registers an event listener for a message type.
161 *
162 * @param {Predicate<string>} predicate The predicate, that checks the type.
163 * @param {WebSocketOnTypeEventListener<TData>} listener The listener.
164 *
165 * @return {Function} The "real" event listener.
166 */
167 onType<TData = any>(predicate: Predicate<string>, listener: WebSocketOnTypeEventListener<TData>): Function;
168 /**
169 * Sends a message to the remote client.
170 *
171 * @param {string} type The type.
172 * @param {any} [data] The data to send.
173 */
174 send(type: string, data?: any): Promise<void>;
175}