@@ -28,14 +28,14 @@ export class RpcServer implements Printable {
2828 return server ;
2929 } ;
3030
31- public static readonly startWithDefaults = ( opts : RpcServerStartOpts ) => {
31+ public static readonly startWithDefaults = ( opts : RpcServerStartOpts ) : RpcServer => {
3232 const port = opts . port ?? 8080 ;
3333 const logger = opts . logger ?? console ;
3434 const server = http . createServer ( ) ;
3535 const http1Server = new Http1Server ( {
3636 server,
3737 } ) ;
38- const rpcServer = RpcServer . create ( {
38+ const rpcServer = new RpcServer ( {
3939 caller : opts . caller ,
4040 http1 : http1Server ,
4141 logger,
@@ -47,6 +47,7 @@ export class RpcServer implements Printable {
4747 if ( typeof host === 'object' ) host = ( host as any ) . address ;
4848 logger . log ( { msg : 'SERVER_STARTED' , host, port} ) ;
4949 } ) ;
50+ return rpcServer ;
5051 } ;
5152
5253 public readonly http1 : Http1Server ;
@@ -71,6 +72,24 @@ export class RpcServer implements Printable {
7172 this . http1 . enableHttpPing ( ) ;
7273 }
7374
75+ public enableCors ( ) : void {
76+ this . http1 . route ( {
77+ method : 'OPTIONS' ,
78+ path : '/{::\n}' ,
79+ handler : ( ctx ) => {
80+ const res = ctx . res ;
81+ res . writeHead ( 200 , 'OK' , {
82+ 'Access-Control-Allow-Origin' : '*' ,
83+ 'Access-Control-Allow-Credentials' : 'true' ,
84+ // 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
85+ // 'Access-Control-Allow-Headers': 'Content-Type',
86+ // 'Access-Control-Max-Age': '86400',
87+ } ) ;
88+ res . end ( ) ;
89+ } ,
90+ } ) ;
91+ }
92+
7493 public enableHttpRpc ( path : string = '/rpc' ) : void {
7594 const batchProcessor = this . batchProcessor ;
7695 const logger = this . opts . logger ?? console ;
@@ -161,16 +180,15 @@ export class RpcServer implements Printable {
161180 }
162181
163182 public enableDefaults ( ) : void {
164- // this.enableCors();
183+ this . enableCors ( ) ;
165184 this . enableHttpPing ( ) ;
166185 this . enableHttpRpc ( ) ;
167- // this.enableWsRpc();
168- // this.startRouting();
186+ this . enableWsRpc ( ) ;
169187 }
170188
171189 // ---------------------------------------------------------------- Printable
172190
173191 public toString ( tab : string = '' ) : string {
174- return `${ this . constructor . name } ` + printTree ( tab , [ ( tab ) => `HTTP/1.1 ${ this . http1 . toString ( tab ) } ` ] ) ;
192+ return `${ this . constructor . name } ` + printTree ( tab , [ ( tab ) => this . http1 . toString ( tab ) ] ) ;
175193 }
176194}
0 commit comments