vadimpronin / guacamole-lite

Node.js library for creating Guacamole-compatible servers. Guacamole is a RDP/VNC/SSH/Telnet client for HTML5 browsers.
Apache License 2.0
247 stars 78 forks source link

No typescritp support #50

Open vandelpavel opened 1 year ago

vandelpavel commented 1 year ago

In order to make guacamole more TS friendly we've created the following declaration file based on how the guacamole-lite app has been built.

// guacamole-lite.d.ts

// See: https://stackoverflow.com/a/49936686/7931540
type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends Array<infer U>
    ? Array<DeepPartial<U>>
    : T[P] extends ReadonlyArray<infer U>
    ? ReadonlyArray<DeepPartial<U>>
    : DeepPartial<T[P]>;
};

declare module "guacamole-lite" {
  // See packages/client/models/guacamole.ts for other guacamole related types which should be served by "guacamole-lite"

  /**
   * This should have next type structure: https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback [1]
   * But guacamole-lite is using a really old version(1.1.2) of ws(now 8.13.0)
   */
  export interface WsServerOptions {
    port?: number;
  }

  /*
   * This configuration object is used inside the guacamole-lite server to establish the connection with the guacd using the following overload of `Net.connect`
   * function connect(port: number, host?: string, connectionListener?: () => void): Socket;
   * See: https://github.com/vadimpronin/guacamole-lite/blob/d2f4ce1ec1847781ca3667066c9e286624d8e432/lib/GuacdClient.js#L23
   */
  export interface GuacdConnectOptions {
    port?: number;
    host?: string;
  }

  enum GuacamoleLogLevel {
    QUIET = 0,
    ERRORS = 10,
    NORMAL = 20,
    VERBOSE = 30,
    DEBUG = 40,
  }

  /**
   * This configuration object comes from this lines of code used inside the Server.js
   * See: https://github.com/vadimpronin/guacamole-lite/blob/d2f4ce1ec1847781ca3667066c9e286624d8e432/lib/Server.js#L34-L107
   */
  interface __GuacamoleClientOptions {
    maxInactivityTime: number;
    log: {
      level: GuacamoleLogLevel;
      stdLog: typeof console.log;
      errorLog: typeof console.error;
    };

    crypt: {
      cypher: string;
    };

    connectionDefaultSettings: {
      rdp: {
        args: string;
        port: string;
        width: number;
        height: number;
        dpi: number;
      };
      vnc: {
        args: string;
        port: string;
        width: number;
        height: number;
        dpi: number;
      };
      ssh: {
        args: string;
        port: number;
        width: number;
        height: number;
        dpi: number;
      };
      telnet: {
        args: string;
        port: number;
        width: number;
        height: number;
        dpi: number;
      };
    };

    allowedUnencryptedConnectionSettings: {
      rdp: string[];
      vnc: string[];
      ssh: string[];
      telnet: string[];
    };
  }

  export type GuacamoleClientOptions = DeepPartial<__GuacamoleClientOptions> & {
    crypt: {
      key: string;
    };
  };

  export default class GuacamoleLite {
    constructor(
      websocketOptions: WsServerOptions,
      guacdOptions: GuacdConnectOptions,
      clientOptions: GuacamoleClientOptions
    );
  }
}