2
2
/* eslint-disable no-restricted-syntax */
3
3
import EventEmitter from 'eventemitter3' ;
4
4
import fetch , { Response } from 'node-fetch' ;
5
- import PQueue from " p-queue" ;
5
+ import PQueue from ' p-queue' ;
6
6
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' ;
11
11
import { DispatchableItem } from './DispatchableItem' ;
12
12
import { HttpsProxyAgent } from 'https-proxy-agent' ;
13
13
import { HttpProxyAgent } from 'http-proxy-agent' ;
@@ -35,26 +35,34 @@ export class Dispatcher extends EventEmitter {
35
35
constructor ( scannerCfg = new ScannerCfg ( ) ) {
36
36
super ( ) ;
37
37
this . scannerCfg = scannerCfg ;
38
- if ( this . scannerCfg . CONCURRENCY_LIMIT > MAX_CONCURRENT_REQUEST )
38
+ if ( this . scannerCfg . CONCURRENCY_LIMIT > MAX_CONCURRENT_REQUEST )
39
39
this . scannerCfg . CONCURRENCY_LIMIT = MAX_CONCURRENT_REQUEST ;
40
40
41
41
this . init ( ) ;
42
42
}
43
43
44
44
init ( ) {
45
-
46
45
//Loads proxy from SDK config, if not, loads from env variables, if not, leave empty
47
46
this . proxyAgent = null ;
48
47
this . caCert = null ;
49
48
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 ;
52
58
53
59
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' ;
55
62
56
63
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 ) ;
58
66
else this . proxyAgent = new HttpProxyAgent ( proxyAddr ) ;
59
67
}
60
68
@@ -68,7 +76,11 @@ export class Dispatcher extends EventEmitter {
68
76
} ) ;
69
77
70
78
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
+ ) {
72
84
this . emit ( ScannerEvents . DISPATCHER_QUEUE_SIZE_MIN_LIMIT ) ;
73
85
this . queueMinLimitReached = true ;
74
86
this . queueMaxLimitReached = false ;
@@ -95,7 +107,8 @@ export class Dispatcher extends EventEmitter {
95
107
this . pQueue . add ( ( ) => this . dispatch ( item ) ) ;
96
108
97
109
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 &&
99
112
! this . queueMaxLimitReached
100
113
) {
101
114
this . emit ( ScannerEvents . DISPATCHER_QUEUE_SIZE_MAX_LIMIT ) ;
@@ -109,26 +122,35 @@ export class Dispatcher extends EventEmitter {
109
122
}
110
123
111
124
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
+ ) ;
113
129
this . emit ( ScannerEvents . DISPATCHER_ITEM_NO_DISPATCHED , disptItem ) ;
114
130
}
115
131
116
132
errorHandler ( error : Error , disptItem : DispatchableItem , response : string ) {
117
133
if ( ! this . globalAbortController . isAborting ( ) ) {
118
-
119
134
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.` ;
121
136
error . name = 'TIMEOUT' ;
122
137
}
123
138
124
139
if ( this . recoverableErrors . has ( error . name ) ) {
125
140
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
+ ) {
127
145
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 ) ;
129
148
return ;
130
149
}
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
+ ) ;
132
154
this . dispatchItem ( disptItem ) ;
133
155
return ;
134
156
}
@@ -138,17 +160,23 @@ export class Dispatcher extends EventEmitter {
138
160
139
161
async dispatch ( item : DispatchableItem ) {
140
162
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
+ ) ;
142
167
let plain_response : string ;
143
168
try {
144
169
this . emit ( ScannerEvents . DISPATCHER_WFP_SENDED ) ;
145
170
const response = await fetch ( this . scannerCfg . API_URL , {
146
171
agent : this . proxyAgent ,
147
172
method : 'post' ,
148
173
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 ,
152
180
} ,
153
181
signal : timeoutController . signal ,
154
182
} ) ;
@@ -158,22 +186,27 @@ export class Dispatcher extends EventEmitter {
158
186
159
187
if ( ! response . ok ) {
160
188
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
+ ) ;
162
192
err . name = 'HTTP_ERROR' ;
163
193
throw err ;
164
194
}
165
195
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 ) ;
168
198
169
- const dispatcherResponse = new DispatcherResponse ( dataAsObj , item . getFingerprintPackage ( ) . getContent ( ) ) ;
199
+ const dispatcherResponse = new DispatcherResponse (
200
+ dataAsObj ,
201
+ item . getFingerprintPackage ( ) . getContent ( )
202
+ ) ;
170
203
this . emit ( ScannerEvents . DISPATCHER_NEW_DATA , dispatcherResponse ) ;
171
204
return Promise . resolve ( ) ;
172
205
} 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 ( ) ;
177
210
}
178
211
}
179
212
}
0 commit comments