import * as http from "http" ; import { AddressInfo } from "net" ; import * as util from "util" ; import * as core from "@fluidframework/server-services-core" ; import * as socketIo from "./socketIoServer" ; export type RequestListener = ( request : http . IncomingMessage , response : http . ServerResponse ) => void ; export class HttpServer implements core . IHttpServer { constructor ( private readonly server ) { } public async close ( ) { await util . promisify ( ( ( callback ) => this . server . close ( callback ) ) as any ) ( ) ; } public listen ( port ) { this . server . listen ( port ) ; } public on ( event , listener ) { this . server . on ( event , listener ) ; } public address ( ) { return this . server . address ( ) as AddressInfo ; } } export class WebServer implements core . IWebServer { constructor ( public httpServer , public webSocketServer ) { } /**
     * Closes the web server
     */ public async close ( ) { await Promise . all ( [ this . httpServer . close ( ) , this . webSocketServer ? this . webSocketServer . close ( ) : Promise . resolve ( ) ] ) ; } } export interface IHttpServerConfig { /**
     * The number of milliseconds of inactivity before a socket is presumed to have timed out.
     * A value of 0 will disable the timeout behavior on incoming connections.
     * Default: 0 (disabled)
     */ connectionTimeoutMs ? : number ; } const defaultHttpServerConfig = { connectionTimeoutMs : 0 } ; const createAndConfigureHttpServer = ( requestListener , httpServerConfig ) : http . Server => { const server = http . createServer ( requestListener ) ; server . timeout = httpServerConfig ?. connectionTimeoutMs ?? defaultHttpServerConfig . connectionTimeoutMs ; return server ; } ; export class SocketIoWebServerFactory implements core . IWebServerFactory { constructor ( private readonly redisConfig , private readonly socketIoAdapterConfig ? , private readonly httpServerConfig ? ) { } public create ( requestListener ) { const server = createAndConfigureHttpServer ( requestListener , this . httpServerConfig ) ; const httpServer = new HttpServer ( server ) ; const socketIoServer = socketIo . create ( this . redisConfig , server , this . socketIoAdapterConfig ) ; return new WebServer ( httpServer , socketIoServer ) ; } } export class BasicWebServerFactory implements core . IWebServerFactory { constructor ( private readonly httpServerConfig ? ) { } public create ( requestListener ) { const server = createAndConfigureHttpServer ( requestListener , this . httpServerConfig ) ; const httpServer = new HttpServer ( server ) ; return new WebServer ( httpServer , null ) ; } }