All files / packages/core/dist index.d.mts

0% Statements 0/0
0% Branches 1/1
0% Functions 1/1
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                                                                                                                                                                                                                                                                                                                                       
import { Certificate, Prettify as Prettify$1, CreateOptions, CertificatePath, VerifyOptions } from '@https-enable/mkcert';
import { Prettify, MakeOnlyRequired } from '@https-enable/types';
import http$1 from 'node:http';
import http2 from 'node:http2';
import https from 'node:https';
import { EventEmitter } from 'node:events';
import * as http from 'http';
 
interface CertificateEvents {
    'cert-renewed': [Certificate];
}
type HttpsEnablerEvents = {
    error: [any];
} & CertificateEvents;
 
/**
 *
 * @param app
 * @param options
 * @link https://gist.github.com/Coolchickenguy/a424ab0f4d32f024b39cd8cdd2b912ae
 */
declare function samePortSSL(app: AppType, options: SamePortOptions): Promise<{
    isAlive: boolean;
    /**
     * Kill the server
     * @returns A promise that resolves when the server ends
     */
    kill(): Promise<void>;
    /**
     * Change the server options
     * @param newOptions The new server options
     * @param newOptions.cert cert content
     * @param newOptions.key key content
     */
    refresh(newOptions: {
        cert: string;
        key: string;
    }): Promise<http$1.Server<typeof http$1.IncomingMessage, typeof http$1.ServerResponse> | undefined>;
}>;
type ServerInstance = Prettify<Awaited<ReturnType<typeof samePortSSL>>>;
 
type CertificateManagerOptions = Prettify$1<MakeOnlyRequired<CreateOptions, 'validity' | 'domains'> & CertificatePath & { cache?: boolean }>
 
type ServerOptions = Prettify$1<VerifyOptions>
type SamePortOptions = Prettify$1<ServerOptions & Certificate &
  {
  /**
   * http 301 重定向至 https
   * @default false
   */
    redirect?: boolean
  }>
 
type RawServerBase = http$1.Server | https.Server | http2.Http2Server | http2.Http2SecureServer
 
/**
 * The default request type based on the server type. Utilizes generic constraining.
 */
type RawRequestDefaultExpression<
  RawServer extends RawServerBase = RawServerDefault,
> = RawServer extends http$1.Server | https.Server ? http$1.IncomingMessage
  : RawServer extends http2.Http2Server | http2.Http2SecureServer ? http2.Http2ServerRequest
    : never
 
/**
 * The default reply type based on the server type. Utilizes generic constraining.
 */
type RawReplyDefaultExpression<
  RawServer extends RawServerBase = RawServerDefault,
> = RawServer extends http$1.Server | https.Server ? http$1.ServerResponse
  : RawServer extends http2.Http2Server | http2.Http2SecureServer ? http2.Http2ServerResponse
    : never
 
type AppType = http$1.RequestListener<typeof http$1.IncomingMessage, typeof http$1.ServerResponse> | ((req: RawRequestDefaultExpression<RawServerBase>, res: RawReplyDefaultExpression<RawServerBase>) => any)
type MiddlewareType = any
 
declare class CertificateManager extends EventEmitter<CertificateEvents> {
    protected options: Prettify<CreateOptions & {
        cache?: boolean;
    }>;
    protected pathOptions: CertificatePath;
    currentCert: Certificate | null;
    constructor(options: CertificateManagerOptions);
    /**
     * 初始化证书(生成或加载现有)
     */
    initialize(force?: boolean): Promise<Certificate | null>;
    validCert(): Promise<{
        match: boolean;
        message: string;
    } | null>;
    /**
     * 确保当前证书有效
     * @description 无效证书则自动重新创建
     */
    ensureValidCert(): Promise<void>;
    private generateNewCert;
    private loadExistingCert;
}
 
declare abstract class HttpsAdapter<App extends AppType, Middleware = MiddlewareType> {
    app?: (App | null) | undefined;
    serverInstance: ServerInstance | null;
    constructor(app?: (App | null) | undefined);
    /**
     * 创建框架特定的中间件
     */
    abstract createMiddleware?: (options: ServerOptions) => Middleware;
    /**
     * 重置证书时通知
     */
    abstract onCertRenewed?: (certificate: Certificate) => any;
    /** 特殊后端框架可以使用这个自定义返回 app */
    abstract init?: () => Promise<App>;
    /**
     * 启动 HTTPS 服务(可选,部分框架需要自定义逻辑)
     */
    createServer(options: SamePortOptions, app?: App): Promise<{
        isAlive: boolean;
        kill: () => Promise<void>;
        refresh: (newOptions: {
            cert: string;
            key: string;
        }) => Promise<http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | undefined>;
    }>;
}
 
declare class HttpsEnabler<App extends AppType, Middleware = MiddlewareType> extends EventEmitter<HttpsEnablerEvents> {
    adapter: HttpsAdapter<App, Middleware>;
    protected options: Prettify<ServerOptions & {
        /**
         * http 301 重定向至 https
         */
        redirect?: boolean;
    }>;
    protected certificateOptions: CertificateManagerOptions;
    protected certManager: CertificateManager;
    constructor(config: {
        adapter: typeof HttpsEnabler.prototype.adapter;
        options: typeof HttpsEnabler.prototype.options;
        certificateOptions: typeof HttpsEnabler.prototype.certificateOptions;
    });
    init(): Promise<void>;
    /**
     * 生成框架特定的中间件
     */
    middleware(): Middleware | undefined;
    refresh(options: Certificate): Promise<http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | undefined> | undefined;
    kill(): Promise<void> | undefined;
    /**
     * 启动 HTTPS 服务
     */
    startServer(app?: App): Promise<{
        key: string;
        cert: string;
        host: string;
        port: number | string;
        rejectUnauthorized?: boolean | undefined;
        redirect?: boolean | undefined;
    }>;
}
 
export { type AppType, CertificateManager, type CertificateManagerOptions, HttpsAdapter, HttpsEnabler, type MiddlewareType, type RawReplyDefaultExpression, type RawRequestDefaultExpression, type RawServerBase, type SamePortOptions, type ServerInstance, type ServerOptions, samePortSSL };