22/* eslint-disable no-restricted-syntax */
33import EventEmitter from 'eventemitter3' ;
44import fetch , { Response } from 'node-fetch' ;
5- import PQueue from " p-queue" ;
5+ import PQueue from ' p-queue' ;
66
7- import { ScannerEvents } from " ../ScannerTypes" ;
8- import { DispatcherResponse } from " ./DispatcherResponse" ;
9- import { ScannerCfg } from " ../ScannerCfg" ;
10- import { GlobalControllerAborter } from " ./GlobalControllerAborter" ;
7+ import { ScannerEvents } from ' ../ScannerTypes' ;
8+ import { DispatcherResponse } from ' ./DispatcherResponse' ;
9+ import { ScannerCfg } from ' ../ScannerCfg' ;
10+ import { GlobalControllerAborter } from ' ./GlobalControllerAborter' ;
1111import { DispatchableItem } from './DispatchableItem' ;
1212import { HttpsProxyAgent } from 'https-proxy-agent' ;
1313import { HttpProxyAgent } from 'http-proxy-agent' ;
@@ -35,26 +35,34 @@ export class Dispatcher extends EventEmitter {
3535 constructor ( scannerCfg = new ScannerCfg ( ) ) {
3636 super ( ) ;
3737 this . scannerCfg = scannerCfg ;
38- if ( this . scannerCfg . CONCURRENCY_LIMIT > MAX_CONCURRENT_REQUEST )
38+ if ( this . scannerCfg . CONCURRENCY_LIMIT > MAX_CONCURRENT_REQUEST )
3939 this . scannerCfg . CONCURRENCY_LIMIT = MAX_CONCURRENT_REQUEST ;
4040
4141 this . init ( ) ;
4242 }
4343
4444 init ( ) {
45-
4645 //Loads proxy from SDK config, if not, loads from env variables, if not, leave empty
4746 this . proxyAgent = null ;
4847 this . caCert = null ;
4948
50- const proxyAddr = this . scannerCfg . PROXY || process . env . https_proxy || process . env . HTTPS_PROXY || process . env . http_proxy || process . env . HTTP_PROXY || null ;
51- const caCertPath = this . scannerCfg . CA_CERT || process . env . NODE_EXTRA_CA_CERTS
49+ const proxyAddr =
50+ this . scannerCfg . PROXY ||
51+ process . env . https_proxy ||
52+ process . env . HTTPS_PROXY ||
53+ process . env . http_proxy ||
54+ process . env . HTTP_PROXY ||
55+ null ;
56+ const caCertPath =
57+ this . scannerCfg . CA_CERT || process . env . NODE_EXTRA_CA_CERTS ;
5258
5359 if ( caCertPath ) Utils . loadCaCertFromFile ( caCertPath ) ;
54- else if ( this . scannerCfg . IGNORE_CERT_ERRORS || proxyAddr ) process . env . NODE_TLS_REJECT_UNAUTHORIZED = "0" ;
60+ else if ( this . scannerCfg . IGNORE_CERT_ERRORS || proxyAddr )
61+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
5562
5663 if ( proxyAddr ) {
57- if ( this . scannerCfg . API_URL . trim ( ) . startsWith ( 'https' ) ) this . proxyAgent = new HttpsProxyAgent ( proxyAddr ) ;
64+ if ( this . scannerCfg . API_URL . trim ( ) . startsWith ( 'https' ) )
65+ this . proxyAgent = new HttpsProxyAgent ( proxyAddr ) ;
5866 else this . proxyAgent = new HttpProxyAgent ( proxyAddr ) ;
5967 }
6068
@@ -68,7 +76,11 @@ export class Dispatcher extends EventEmitter {
6876 } ) ;
6977
7078 this . pQueue . on ( 'next' , ( ) => {
71- if ( ( this . pQueue . size + this . pQueue . pending ) < this . scannerCfg . DISPATCHER_QUEUE_SIZE_MIN_LIMIT && ! this . queueMinLimitReached ) {
79+ if (
80+ this . pQueue . size + this . pQueue . pending <
81+ this . scannerCfg . DISPATCHER_QUEUE_SIZE_MIN_LIMIT &&
82+ ! this . queueMinLimitReached
83+ ) {
7284 this . emit ( ScannerEvents . DISPATCHER_QUEUE_SIZE_MIN_LIMIT ) ;
7385 this . queueMinLimitReached = true ;
7486 this . queueMaxLimitReached = false ;
@@ -95,7 +107,8 @@ export class Dispatcher extends EventEmitter {
95107 this . pQueue . add ( ( ) => this . dispatch ( item ) ) ;
96108
97109 if (
98- this . pQueue . size + this . pQueue . pending >= this . scannerCfg . DISPATCHER_QUEUE_SIZE_MAX_LIMIT &&
110+ this . pQueue . size + this . pQueue . pending >=
111+ this . scannerCfg . DISPATCHER_QUEUE_SIZE_MAX_LIMIT &&
99112 ! this . queueMaxLimitReached
100113 ) {
101114 this . emit ( ScannerEvents . DISPATCHER_QUEUE_SIZE_MAX_LIMIT ) ;
@@ -109,26 +122,35 @@ export class Dispatcher extends EventEmitter {
109122 }
110123
111124 emitNoDispatchedItem ( disptItem ) {
112- this . emit ( ScannerEvents . DISPATCHER_LOG , `[ SCANNER ]: WFP content sended to many times. Some files won't be scanned` ) ;
125+ this . emit (
126+ ScannerEvents . DISPATCHER_LOG ,
127+ `[ SCANNER ]: WFP content sended to many times. Some files won't be scanned`
128+ ) ;
113129 this . emit ( ScannerEvents . DISPATCHER_ITEM_NO_DISPATCHED , disptItem ) ;
114130 }
115131
116132 errorHandler ( error : Error , disptItem : DispatchableItem , response : string ) {
117133 if ( ! this . globalAbortController . isAborting ( ) ) {
118-
119134 if ( error . name === 'AbortError' ) {
120- error . message = `Timeout reached for packet with request ID ${ disptItem . uuid } . Enqueuing again.`
135+ error . message = `Timeout reached for packet with request ID ${ disptItem . uuid } . Enqueuing again.` ;
121136 error . name = 'TIMEOUT' ;
122137 }
123138
124139 if ( this . recoverableErrors . has ( error . name ) ) {
125140 disptItem . increaseErrorCounter ( ) ;
126- if ( disptItem . getErrorCounter ( ) >= this . scannerCfg . MAX_RETRIES_FOR_RECOVERABLES_ERRORS ) {
141+ if (
142+ disptItem . getErrorCounter ( ) >=
143+ this . scannerCfg . MAX_RETRIES_FOR_RECOVERABLES_ERRORS
144+ ) {
127145 this . emitNoDispatchedItem ( disptItem ) ;
128- if ( this . scannerCfg . ABORT_ON_MAX_RETRIES ) this . emitUnrecoberableError ( error , disptItem , response ) ;
146+ if ( this . scannerCfg . ABORT_ON_MAX_RETRIES )
147+ this . emitUnrecoberableError ( error , disptItem , response ) ;
129148 return ;
130149 }
131- this . emit ( ScannerEvents . DISPATCHER_LOG , `[ SCANNER ]: Recoverable error happened sending WFP content to server. Reason: ${ error } ` ) ;
150+ this . emit (
151+ ScannerEvents . DISPATCHER_LOG ,
152+ `[ SCANNER ]: Recoverable error happened sending WFP content to server. Reason: ${ error } `
153+ ) ;
132154 this . dispatchItem ( disptItem ) ;
133155 return ;
134156 }
@@ -138,17 +160,23 @@ export class Dispatcher extends EventEmitter {
138160
139161 async dispatch ( item : DispatchableItem ) {
140162 const timeoutController = this . globalAbortController . getAbortController ( ) ;
141- const timeoutId = setTimeout ( ( ) => timeoutController . abort ( ) , this . scannerCfg . TIMEOUT ) ;
163+ const timeoutId = setTimeout (
164+ ( ) => timeoutController . abort ( ) ,
165+ this . scannerCfg . TIMEOUT
166+ ) ;
142167 let plain_response : string ;
143168 try {
144169 this . emit ( ScannerEvents . DISPATCHER_WFP_SENDED ) ;
145170 const response = await fetch ( this . scannerCfg . API_URL , {
146171 agent : this . proxyAgent ,
147172 method : 'post' ,
148173 body : item . getForm ( ) ,
149- headers : { 'User-Agent' : this . scannerCfg . CLIENT_TIMESTAMP ? this . scannerCfg . CLIENT_TIMESTAMP : `scanoss-js/v${ Utils . getPackageVersion ( ) } ` ,
150- 'X-Session' : this . scannerCfg . API_KEY ,
151- 'x-request-id' : item . uuid ,
174+ headers : {
175+ 'User-Agent' : this . scannerCfg . CLIENT_TIMESTAMP
176+ ? this . scannerCfg . CLIENT_TIMESTAMP
177+ : `scanoss-js/v${ Utils . getPackageVersion ( ) } ` ,
178+ 'X-Session' : this . scannerCfg . API_KEY ,
179+ 'x-request-id' : item . uuid ,
152180 } ,
153181 signal : timeoutController . signal ,
154182 } ) ;
@@ -158,22 +186,27 @@ export class Dispatcher extends EventEmitter {
158186
159187 if ( ! response . ok ) {
160188 plain_response = await response . text ( ) ;
161- const err = new Error ( `\nHTTP Status code: ${ response . status } \nServer Response:\n${ plain_response } \n` ) ;
189+ const err = new Error (
190+ `\nHTTP Status code: ${ response . status } \nServer Response:\n${ plain_response } \n`
191+ ) ;
162192 err . name = 'HTTP_ERROR' ;
163193 throw err ;
164194 }
165195
166- const dataAsText = await response . text ( ) ;
167- const dataAsObj = JSON . parse ( dataAsText ) ;
196+ plain_response = await response . text ( ) ;
197+ const dataAsObj = JSON . parse ( plain_response ) ;
168198
169- const dispatcherResponse = new DispatcherResponse ( dataAsObj , item . getFingerprintPackage ( ) . getContent ( ) ) ;
199+ const dispatcherResponse = new DispatcherResponse (
200+ dataAsObj ,
201+ item . getFingerprintPackage ( ) . getContent ( )
202+ ) ;
170203 this . emit ( ScannerEvents . DISPATCHER_NEW_DATA , dispatcherResponse ) ;
171204 return Promise . resolve ( ) ;
172205 } catch ( e ) {
173- clearTimeout ( timeoutId ) ;
174- this . globalAbortController . removeAbortController ( timeoutController ) ;
175- this . errorHandler ( e , item , plain_response ) ;
176- return Promise . resolve ( ) ;
206+ clearTimeout ( timeoutId ) ;
207+ this . globalAbortController . removeAbortController ( timeoutController ) ;
208+ this . errorHandler ( e , item , plain_response ) ;
209+ return Promise . resolve ( ) ;
177210 }
178211 }
179212}
0 commit comments