11'use strict' ;
22
33const { node, npm } = require ( './dependencies.js' ) ;
4- const { http, https, worker } = node ;
4+ const { http, https } = node ;
55const { common, ws } = npm ;
6-
7- const Semaphore = require ( './semaphore .js' ) ;
6+ const { Channel } = require ( './channel.js' ) ;
7+ const { application } = require ( './application .js' ) ;
88
99const SHUTDOWN_TIMEOUT = 5000 ;
1010const SHORT_TIMEOUT = 500 ;
@@ -19,61 +19,46 @@ const receiveBody = async (req) => {
1919} ;
2020
2121class Server {
22- constructor ( config , { application , Channel } ) {
22+ constructor ( config ) {
2323 this . config = config ;
24- this . application = application ;
25- this . Channel = Channel ;
2624 this . channels = new Map ( ) ;
27- const { host, balancer, protocol, ports, concurrency, queue } = config ;
28- this . semaphore = new Semaphore ( concurrency , queue . size , queue . timeout ) ;
29- const { threadId } = worker ;
30- this . balancer = balancer && threadId === 1 ;
31- const skipBalancer = balancer ? 1 : 0 ;
32- this . port = this . balancer ? balancer : ports [ threadId - skipBalancer - 1 ] ;
33- const transport = protocol === 'http' || this . balancer ? http : https ;
25+ const { port, host, protocol, cert } = config ;
26+ this . port = port ;
27+ this . host = host ;
28+ this . protocol = protocol ;
29+ const transport = protocol === 'http' ? http : https ;
3430 const listener = this . listener . bind ( this ) ;
35- this . server = transport . createServer ( { ... application . cert } , listener ) ;
31+ this . server = transport . createServer ( cert , listener ) ;
3632 this . ws = new ws . Server ( { server : this . server } ) ;
3733 this . ws . on ( 'connection' , async ( connection , req ) => {
3834 const channel = await new Channel ( req , null , connection , application ) ;
3935 connection . on ( 'message' , ( data ) => {
4036 channel . message ( data ) ;
4137 } ) ;
4238 } ) ;
43- this . protocol = protocol ;
44- this . host = host ;
4539 this . server . listen ( this . port , host ) ;
4640 }
4741
4842 async listener ( req , res ) {
49- const { channels, Channel } = this ;
5043 let finished = false ;
5144 const { url, connection } = req ;
52- const channel = await new Channel ( req , res , null , this . application ) ;
53- channels . set ( connection , channel ) ;
45+ const channel = await new Channel ( req , res , null , application ) ;
46+ this . channels . set ( connection , channel ) ;
5447
5548 const timer = setTimeout ( ( ) => {
5649 if ( finished ) return ;
5750 finished = true ;
58- channels . delete ( connection ) ;
51+ this . channels . delete ( connection ) ;
5952 channel . error ( 504 ) ;
6053 } , LONG_RESPONSE ) ;
6154
6255 res . on ( 'close' , ( ) => {
6356 if ( finished ) return ;
6457 finished = true ;
6558 clearTimeout ( timer ) ;
66- channels . delete ( connection ) ;
59+ this . channels . delete ( connection ) ;
6760 } ) ;
6861
69- if ( this . balancer ) {
70- const host = common . parseHost ( req . headers . host ) ;
71- const port = common . sample ( this . config . ports ) ;
72- const { protocol } = this . config ;
73- channel . redirect ( `${ protocol } ://${ host } :${ port } /` ) ;
74- return ;
75- }
76-
7762 if ( url . startsWith ( '/api' ) ) this . request ( channel ) ;
7863 else channel . static ( ) ;
7964 }
@@ -117,7 +102,7 @@ class Server {
117102
118103 async close ( ) {
119104 this . server . close ( ( err ) => {
120- if ( err ) this . application . logger . error ( err . stack ) ;
105+ if ( err ) application . logger . error ( err . stack ) ;
121106 } ) ;
122107 if ( this . channels . size === 0 ) {
123108 await common . timeout ( SHORT_TIMEOUT ) ;
0 commit comments