11import * as http from 'http' ;
2+ import * as https from 'https' ;
3+ import type * as tls from 'tls' ;
24import type * as net from 'net' ;
35import { Writer } from '@jsonjoy.com/util/lib/buffers/Writer' ;
46import { Codecs } from '@jsonjoy.com/json-pack/lib/codecs/Codecs' ;
@@ -64,13 +66,55 @@ export interface Http1ServerOpts {
6466 writer ?: Writer ;
6567}
6668
69+ export type Http1CreateServerOpts = Http1CreateHttpServerOpts | Http1CreateHttpsServerOpts ;
70+
71+ export interface Http1CreateHttpServerOpts {
72+ tls ?: false ;
73+ conf ?: http . ServerOptions ;
74+ }
75+
76+ export interface Http1CreateHttpsServerOpts {
77+ tls : true ;
78+ conf ?: https . ServerOptions ;
79+ secureContext ?: ( ) => Promise < tls . SecureContextOptions > ;
80+
81+ /**
82+ * If specified, and the `secureContext` is also specified, will be used to
83+ * refresh the secure context every `secureContextRefreshInterval` milliseconds.
84+ */
85+ secureContextRefreshInterval ?: number ;
86+ }
87+
6788export class Http1Server implements Printable {
68- public static start ( opts : http . ServerOptions = { } , port = 8000 ) : Http1Server {
69- const rawServer = http . createServer ( opts ) ;
70- rawServer . listen ( port ) ;
71- const server = new Http1Server ( { server : rawServer } ) ;
72- return server ;
73- }
89+ public static create = async ( opts : Http1CreateServerOpts = { } ) : Promise < http . Server | https . Server > => {
90+ if ( opts . tls ) {
91+ const { secureContext, secureContextRefreshInterval} = opts ;
92+ const server = https . createServer ( {
93+ ...( secureContext ? await secureContext ( ) : { } ) ,
94+ ...opts . conf ,
95+ } ) ;
96+ if ( secureContext && secureContextRefreshInterval ) {
97+ const timer = setInterval ( ( ) => {
98+ try {
99+ secureContext ( )
100+ . then ( ( context ) => {
101+ server . setSecureContext ( context ) ;
102+ } )
103+ . catch ( ( error ) => {
104+ console . error ( 'Failed to update secure context:' , error ) ;
105+ } ) ;
106+ } catch ( error ) {
107+ console . error ( 'Failed to update secure context:' , error ) ;
108+ }
109+ } , secureContextRefreshInterval ) ;
110+ server . once ( 'close' , ( ) => {
111+ clearInterval ( timer ) ;
112+ } ) ;
113+ }
114+ return server ;
115+ }
116+ return http . createServer ( opts . conf || { } ) ;
117+ } ;
74118
75119 public readonly codecs : RpcCodecs ;
76120 public readonly server : http . Server ;
@@ -82,7 +126,7 @@ export class Http1Server implements Printable {
82126 this . wsEncoder = new WsFrameEncoder ( writer ) ;
83127 }
84128
85- public start ( ) : void {
129+ public async start ( ) : Promise < void > {
86130 const server = this . server ;
87131 this . httpMatcher = this . httpRouter . compile ( ) ;
88132 this . wsMatcher = this . wsRouter . compile ( ) ;
@@ -93,6 +137,11 @@ export class Http1Server implements Printable {
93137 } ) ;
94138 }
95139
140+ public async stop ( ) : Promise < void > {
141+ const server = this . server ;
142+ server . removeAllListeners ( ) ;
143+ }
144+
96145 // ------------------------------------------------------------- HTTP routing
97146
98147 public onnotfound : Http1NotFoundHandler = ( res ) => {
0 commit comments