@@ -10,13 +10,37 @@ const upload = require('multer')()
1010
1111const noop = ( ) => { }
1212
13+ /**
14+ * @typedef {object } RemoteConfigFile
15+ * @property {number } orgId
16+ * @property {string } product
17+ * @property {string } id
18+ * @property {string } name
19+ * @property {string } config
20+ * @property {string } path
21+ * @property {string } fileHash
22+ * @property {object } meta
23+ * @property {object } meta.custom
24+ * @property {number } meta.custom.v
25+ * @property {object } meta.hashes
26+ * @property {string } meta.hashes.sha256
27+ * @property {number } meta.length
28+ */
29+
1330module . exports = class FakeAgent extends EventEmitter {
31+ port = 0
32+ /** @type {Set<import('net').Socket> } */
33+ #sockets = new Set ( )
34+ /** @type {Record<string, RemoteConfigFile> } */
35+ _rcFiles = { }
36+ _rcTargetsVersion = 0
37+ /** @type {Set<string> } */
38+ _rcSeenStates = new Set ( )
39+
1440 constructor ( port = 0 ) {
1541 // Redirect rejections to the error event
1642 super ( { captureRejections : true } )
1743 this . port = port
18- this . resetRemoteConfig ( )
19- this . _sockets = new Set ( )
2044 }
2145
2246 start ( ) {
@@ -29,14 +53,16 @@ module.exports = class FakeAgent extends EventEmitter {
2953
3054 // Track connections to force close them later
3155 this . server . on ( 'connection' , ( socket ) => {
32- this . _sockets . add ( socket )
56+ this . #sockets . add ( socket )
3357 socket . on ( 'close' , ( ) => {
34- this . _sockets . delete ( socket )
58+ this . #sockets . delete ( socket )
3559 } )
3660 } )
3761
3862 this . server . listen ( this . port , ( ) => {
39- this . port = this . server . address ( ) . port
63+ this . port = ( /** @type {import('net').AddressInfo } */ (
64+ ( /** @type {import('http').Server } */ ( this . server ) ) . address ( )
65+ ) ) . port
4066 clearTimeout ( timeoutObj )
4167 resolve ( this )
4268 } )
@@ -46,10 +72,10 @@ module.exports = class FakeAgent extends EventEmitter {
4672 stop ( ) {
4773 if ( ! this . server ?. listening ) return
4874
49- for ( const socket of this . _sockets ) {
75+ for ( const socket of this . #sockets ) {
5076 socket . destroy ( )
5177 }
52- this . _sockets . clear ( )
78+ this . #sockets . clear ( )
5379 this . server . close ( )
5480
5581 return once ( this . server , 'close' )
@@ -65,19 +91,27 @@ module.exports = class FakeAgent extends EventEmitter {
6591 * @param {object } config.config - The Remote Config "file" object
6692 */
6793 addRemoteConfig ( config ) {
68- config = { ...config }
69- config . orgId = config . orgId || 2
70- config . name = config . name || createHash ( 'sha256' ) . update ( config . id ) . digest ( 'hex' )
71- config . config = JSON . stringify ( config . config )
72- config . path = `datadog/${ config . orgId } /${ config . product } /${ config . id } /${ config . name } `
73- config . fileHash = createHash ( 'sha256' ) . update ( config . config ) . digest ( 'hex' )
74- config . meta = {
94+ const orgId = config . orgId || 2
95+ const name = config . name || createHash ( 'sha256' ) . update ( config . id ) . digest ( 'hex' )
96+ const configStr = JSON . stringify ( config . config )
97+ const path = `datadog/${ orgId } /${ config . product } /${ config . id } /${ name } `
98+ const fileHash = createHash ( 'sha256' ) . update ( configStr ) . digest ( 'hex' )
99+ const meta = {
75100 custom : { v : 1 } ,
76- hashes : { sha256 : config . fileHash } ,
77- length : config . config . length
101+ hashes : { sha256 : fileHash } ,
102+ length : configStr . length
78103 }
79104
80- this . _rcFiles [ config . id ] = config
105+ this . _rcFiles [ config . id ] = {
106+ orgId,
107+ product : config . product ,
108+ id : config . id ,
109+ name,
110+ config : configStr ,
111+ path,
112+ fileHash,
113+ meta
114+ }
81115 this . _rcTargetsVersion ++
82116 }
83117
@@ -386,7 +420,7 @@ function buildExpressServer (agent) {
386420 res . status ( 200 ) . send ( )
387421 agent . emit ( 'debugger-diagnostics' , {
388422 headers : req . headers ,
389- payload : JSON . parse ( req . files [ 0 ] . buffer . toString ( ) )
423+ payload : JSON . parse ( ( /** @type { Express.Multer.File[] } */ ( req . files ) ) [ 0 ] . buffer . toString ( ) )
390424 } )
391425 } )
392426
0 commit comments