11import express from 'express' ;
22import http from 'http' ;
3- import getPort from 'getport ' ;
3+ import getPort from 'get-port ' ;
44import socketClusterServer from 'socketcluster-server' ;
5- import getOptions , { Options } from './options' ;
5+ import getOptions from './options' ;
66import routes from './routes' ;
77import createStore from './store' ;
88
9- // var LOG_LEVEL_NONE = 0;
10- const LOG_LEVEL_ERROR = 1 ;
9+ // const LOG_LEVEL_NONE = 0;
10+ // const LOG_LEVEL_ERROR = 1;
1111const LOG_LEVEL_WARN = 2 ;
1212const LOG_LEVEL_INFO = 3 ;
1313
14- export interface ExtendedOptions extends Options {
15- allowClientPublish : boolean ;
16- }
17-
18- export default function ( argv : { [ arg : string ] : any } ) : Promise < {
14+ export default async function ( argv : { [ arg : string ] : any } ) : Promise < {
1915 portAlreadyUsed ?: boolean ;
2016 listener : ( eventName : 'ready' ) => { once ( ) : Promise < void > } ;
2117} > {
@@ -25,131 +21,120 @@ export default function (argv: { [arg: string]: any }): Promise<{
2521 const port = options . port ;
2622 const logLevel =
2723 options . logLevel === undefined ? LOG_LEVEL_INFO : options . logLevel ;
28- return new Promise ( function ( resolve ) {
29- // Check port already used
30- getPort ( port , function ( err , p ) {
31- /* eslint-disable no-console */
32- if ( err ) {
33- if ( logLevel >= LOG_LEVEL_ERROR ) {
34- console . error ( err ) ;
35- }
36- return ;
37- }
38- if ( port !== p ) {
39- if ( logLevel >= LOG_LEVEL_WARN ) {
40- console . log ( `[ReduxDevTools] Server port ${ port } is already used.` ) ;
41- }
42- resolve ( {
43- portAlreadyUsed : true ,
44- listener : function ( eventName : 'ready' ) {
45- return {
46- once ( ) {
47- return Promise . resolve ( ) ;
48- } ,
49- } ;
24+ // Check port already used
25+ const p = await getPort ( { port } ) ;
26+ if ( port !== p ) {
27+ if ( logLevel >= LOG_LEVEL_WARN ) {
28+ console . log ( `[ReduxDevTools] Server port ${ port } is already used.` ) ;
29+ }
30+ return {
31+ portAlreadyUsed : true ,
32+ listener : function ( ) {
33+ return {
34+ once ( ) {
35+ return Promise . resolve ( ) ;
5036 } ,
51- } ) ;
52- } else {
53- if ( logLevel >= LOG_LEVEL_INFO ) {
54- console . log ( '[ReduxDevTools] Start server...' ) ;
55- console . log ( '-' . repeat ( 80 ) + '\n' ) ;
56- }
57- const httpServer = http . createServer ( ) ;
58- const agServer = socketClusterServer . attach ( httpServer , options ) ;
37+ } ;
38+ } ,
39+ } ;
40+ }
5941
60- const app = express ( ) ;
61- httpServer . on ( 'request' , app ) ;
62- const store = createStore ( options ) ;
63- app . use ( routes ( options , store , agServer ) ) ;
42+ if ( logLevel >= LOG_LEVEL_INFO ) {
43+ console . log ( '[ReduxDevTools] Start server...' ) ;
44+ console . log ( '-' . repeat ( 80 ) + '\n' ) ;
45+ }
46+ const httpServer = http . createServer ( ) ;
47+ const agServer = socketClusterServer . attach ( httpServer , options ) ;
6448
65- agServer . setMiddleware (
66- agServer . MIDDLEWARE_INBOUND ,
67- // eslint-disable-next-line @typescript-eslint/no-misused-promises
68- async ( middlewareStream ) => {
69- for await ( const action of middlewareStream ) {
70- if ( action . type === action . TRANSMIT ) {
71- const channel = action . receiver ;
72- const data = action . data ;
73- if (
74- channel . substring ( 0 , 3 ) === 'sc-' ||
75- channel === 'respond' ||
76- channel === 'log'
77- ) {
78- void agServer . exchange . transmitPublish ( channel , data ) ;
79- } else if ( channel === 'log-noid' ) {
80- void agServer . exchange . transmitPublish ( 'log' , {
81- id : action . socket . id ,
82- data : data ,
83- } ) ;
84- }
85- } else if ( action . type === action . SUBSCRIBE ) {
86- if ( action . channel === 'report' ) {
87- store
88- . list ( )
89- . then ( function ( data ) {
90- void agServer . exchange . transmitPublish ( 'report' , {
91- type : 'list' ,
92- data : data ,
93- } ) ;
94- } )
95- . catch ( function ( error ) {
96- console . error ( error ) ; // eslint-disable-line no-console
97- } ) ;
98- }
99- }
100- action . allow ( ) ;
101- }
102- }
103- ) ;
49+ const app = express ( ) ;
50+ httpServer . on ( 'request' , app ) ;
51+ const store = createStore ( options ) ;
52+ app . use ( routes ( options , store , agServer ) ) ;
10453
105- void ( async ( ) => {
106- for await ( const { socket } of agServer . listener ( 'connection' ) ) {
107- let channelToWatch : string , channelToEmit : string ;
108- void ( async ( ) => {
109- for await ( const request of socket . procedure ( 'login' ) ) {
110- const credentials = request . data ;
111- if ( credentials === 'master' ) {
112- channelToWatch = 'respond' ;
113- channelToEmit = 'log' ;
114- } else {
115- channelToWatch = 'log' ;
116- channelToEmit = 'respond' ;
117- }
118- request . end ( channelToWatch ) ;
119- }
120- } ) ( ) ;
121- void ( async ( ) => {
122- for await ( const request of socket . procedure ( 'getReport' ) ) {
123- const id = request . data as string ;
124- store
125- . get ( id )
126- . then ( function ( data ) {
127- request . end ( data ) ;
128- } )
129- . catch ( function ( error ) {
130- console . error ( error ) ; // eslint-disable-line no-console
131- } ) ;
132- }
133- } ) ( ) ;
134- void ( async ( ) => {
135- for await ( const data of socket . listener ( 'disconnect' ) ) {
136- const channel = agServer . exchange . channel ( 'sc-' + socket . id ) ;
137- channel . unsubscribe ( ) ;
138- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139- void agServer . exchange . transmitPublish ( channelToEmit ! , {
140- id : socket . id ,
141- type : 'DISCONNECTED' ,
54+ agServer . setMiddleware (
55+ agServer . MIDDLEWARE_INBOUND ,
56+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
57+ async ( middlewareStream ) => {
58+ for await ( const action of middlewareStream ) {
59+ if ( action . type === action . TRANSMIT ) {
60+ const channel = action . receiver ;
61+ const data = action . data ;
62+ if (
63+ channel . substring ( 0 , 3 ) === 'sc-' ||
64+ channel === 'respond' ||
65+ channel === 'log'
66+ ) {
67+ void agServer . exchange . transmitPublish ( channel , data ) ;
68+ } else if ( channel === 'log-noid' ) {
69+ void agServer . exchange . transmitPublish ( 'log' , {
70+ id : action . socket . id ,
71+ data : data ,
72+ } ) ;
73+ }
74+ } else if ( action . type === action . SUBSCRIBE ) {
75+ if ( action . channel === 'report' ) {
76+ store
77+ . list ( )
78+ . then ( function ( data ) {
79+ void agServer . exchange . transmitPublish ( 'report' , {
80+ type : 'list' ,
81+ data : data ,
14282 } ) ;
143- }
144- } ) ( ) ;
83+ } )
84+ . catch ( function ( error ) {
85+ console . error ( error ) ; // eslint-disable-line no-console
86+ } ) ;
14587 }
146- } ) ( ) ;
147-
148- httpServer . listen ( options . port ) ;
149- // @ts -expect-error Shouldn't there be a 'ready' event?
150- resolve ( agServer ) ;
88+ }
89+ action . allow ( ) ;
15190 }
152- /* eslint-enable no-console */
153- } ) ;
154- } ) ;
91+ }
92+ ) ;
93+
94+ void ( async ( ) => {
95+ for await ( const { socket } of agServer . listener ( 'connection' ) ) {
96+ let channelToWatch : string , channelToEmit : string ;
97+ void ( async ( ) => {
98+ for await ( const request of socket . procedure ( 'login' ) ) {
99+ const credentials = request . data ;
100+ if ( credentials === 'master' ) {
101+ channelToWatch = 'respond' ;
102+ channelToEmit = 'log' ;
103+ } else {
104+ channelToWatch = 'log' ;
105+ channelToEmit = 'respond' ;
106+ }
107+ request . end ( channelToWatch ) ;
108+ }
109+ } ) ( ) ;
110+ void ( async ( ) => {
111+ for await ( const request of socket . procedure ( 'getReport' ) ) {
112+ const id = request . data as string ;
113+ store
114+ . get ( id )
115+ . then ( function ( data ) {
116+ request . end ( data ) ;
117+ } )
118+ . catch ( function ( error ) {
119+ console . error ( error ) ; // eslint-disable-line no-console
120+ } ) ;
121+ }
122+ } ) ( ) ;
123+ void ( async ( ) => {
124+ for await ( const data of socket . listener ( 'disconnect' ) ) {
125+ const channel = agServer . exchange . channel ( 'sc-' + socket . id ) ;
126+ channel . unsubscribe ( ) ;
127+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
128+ void agServer . exchange . transmitPublish ( channelToEmit ! , {
129+ id : socket . id ,
130+ type : 'DISCONNECTED' ,
131+ } ) ;
132+ }
133+ } ) ( ) ;
134+ }
135+ } ) ( ) ;
136+
137+ httpServer . listen ( options . port ) ;
138+ // @ts -expect-error Shouldn't there be a 'ready' event?
139+ return agServer ;
155140}
0 commit comments