All files / packages/core/src https-adapter.ts

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

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                                                                           
import type { Certificate } from '@https-enable/mkcert'
import type { AppType, MiddlewareType, SamePortOptions, ServerInstance, ServerOptions } from './type'
import { samePortSSL } from './same-port-ssl'
 
export abstract class HttpsAdapter<App extends AppType, Middleware = MiddlewareType> {
  public serverInstance: ServerInstance | null = null
 
  constructor(public app?: App | null) {}
 
  /**
   * 创建框架特定的中间件
   */
  abstract createMiddleware?: (options: ServerOptions) => Middleware
 
  /**
   * 重置证书时通知
   */
  abstract onCertRenewed?: (certificate: Certificate) => any
 
  /** 特殊后端框架可以使用这个自定义返回 app */
  abstract init?: () => Promise<App>
 
  /**
   * 启动 HTTPS 服务(可选,部分框架需要自定义逻辑)
   */
  async createServer(options: SamePortOptions, app?: App) {
    let tempApp = app ?? this.app
    if (this.init && typeof this.init === 'function') {
      tempApp = await this.init()
    }
    if (!tempApp && !this.app) {
      throw new Error('`app` is no found')
    }
    this.serverInstance = await samePortSSL((tempApp ?? this.app)!, options)
    return this.serverInstance
  }
}