UNPKG

1.54 kBTypeScriptView Raw
1/// <reference types="node" />
2import express from 'express';
3import { Server as HttpServer } from 'http';
4import WebSocket from 'ws';
5import { Handle } from '@theintern/common';
6import Node from './executors/Node';
7import { Message } from './channels/Base';
8export interface Context {
9 readonly stopped: boolean;
10 readonly basePath: string;
11 readonly executor: Node;
12 handleMessage(message: Message): Promise<any>;
13}
14export default class Server implements ServerProperties {
15 readonly executor: Node;
16 basePath: string;
17 port: number;
18 runInSync: boolean;
19 socketPort: number;
20 get stopped(): boolean;
21 protected _app: express.Express | undefined;
22 protected _httpServer: HttpServer | undefined;
23 protected _sessions: {
24 [id: string]: {
25 listeners: ServerListener[];
26 };
27 } | undefined;
28 protected _wsServer: WebSocket.Server | undefined;
29 protected _wsPingTimers: NodeJS.Timer[];
30 constructor(options: ServerOptions);
31 start(): Promise<void>;
32 stop(): Promise<any[]>;
33 subscribe(sessionId: string, listener: ServerListener): Handle;
34 private _getSession;
35 private _handleMessage;
36 private _handleWebSocket;
37 private _publish;
38}
39export interface ServerProperties {
40 basePath: string;
41 executor: Node;
42 port: number;
43 runInSync: boolean;
44 socketPort: number;
45}
46export interface ServerListener {
47 (name: string, data?: any): void;
48}
49export declare type ServerOptions = Partial<ServerProperties> & {
50 executor: Node;
51};