@@ -37,6 +37,7 @@ const ANY_VALUE = Symbol('test.ANY_VALUE')
3737 */
3838async function runAndCheckOutput ( filename , cwd , expectedOut , expectedSource ) {
3939 const proc = spawn ( process . execPath , [ filename ] , { cwd, stdio : 'pipe' } )
40+ assert ( proc . pid !== undefined , 'Process PID is not available' )
4041 const pid = proc . pid
4142 let out = await new Promise ( ( resolve , reject ) => {
4243 proc . on ( 'error' , reject )
@@ -123,15 +124,11 @@ function assertTelemetryPoints (pid, msgs, expectedTelemetryPoints) {
123124 */
124125 function getPoints ( ...args ) {
125126 const expectedPoints = [ ]
126- let currentPoint = /** @type {{ name?: string, tags?: string[] } } */ ( { } )
127- for ( const arg of args ) {
128- if ( ! currentPoint . name ) {
129- currentPoint . name = 'library_entrypoint.' + arg
130- } else {
131- currentPoint . tags = arg . split ( ',' ) . filter ( Boolean )
132- expectedPoints . push ( currentPoint )
133- currentPoint = { }
134- }
127+ for ( let i = 0 ; i < args . length ; i += 2 ) {
128+ expectedPoints . push ( {
129+ name : 'library_entrypoint.' + args [ i ] ,
130+ tags : args [ i + 1 ] . split ( ',' ) . filter ( Boolean )
131+ } )
135132 }
136133 return expectedPoints
137134 }
@@ -147,7 +144,7 @@ function assertTelemetryPoints (pid, msgs, expectedTelemetryPoints) {
147144 runtime_name : 'nodejs' ,
148145 runtime_version : process . versions . node ,
149146 tracer_version : require ( '../../package.json' ) . version ,
150- pid : Number ( pid )
147+ pid
151148 }
152149
153150 // Validate basic metadata
@@ -156,6 +153,9 @@ function assertTelemetryPoints (pid, msgs, expectedTelemetryPoints) {
156153 }
157154
158155 // Validate result metadata is present and has valid values
156+ assert ( typeof actualMetadata . result === 'string' , 'result should be a string' )
157+ assert ( typeof actualMetadata . result_class === 'string' , 'result_class should be a string' )
158+ assert ( typeof actualMetadata . result_reason === 'string' , 'result_reason should be a string' )
159159 assert ( actualMetadata . result , 'result field should be present' )
160160 assert ( actualMetadata . result_class , 'result_class field should be present' )
161161 assert ( actualMetadata . result_reason , 'result_reason field should be present' )
@@ -167,7 +167,6 @@ function assertTelemetryPoints (pid, msgs, expectedTelemetryPoints) {
167167 assert ( validResults . includes ( actualMetadata . result ) , `Invalid result: ${ actualMetadata . result } ` )
168168 assert ( validResultClasses . includes ( actualMetadata . result_class ) ,
169169 `Invalid result_class: ${ actualMetadata . result_class } ` )
170- assert ( typeof actualMetadata . result_reason === 'string' , 'result_reason should be a string' )
171170 }
172171}
173172
@@ -217,7 +216,7 @@ function spawnProc (filename, options = {}, stdioHandler, stderrHandler) {
217216
218217 return new Promise ( ( resolve , reject ) => {
219218 proc
220- . on ( 'message' , ( { port } ) => {
219+ . on ( 'message' , ( /** @type { { port?: unknown } } */ { port } ) => {
221220 if ( typeof port !== 'number' && typeof port !== 'string' ) {
222221 return reject ( new Error ( `${ filename } sent invalid port: ${ port } . Expected a number or string.` ) )
223222 }
@@ -342,6 +341,9 @@ async function createSandbox (
342341 if ( builtinModules . includes ( dep ) ) return dep
343342
344343 const match = dep . replaceAll ( / [ ' " ] / g, '' ) . match ( / ^ ( @ ? [ ^ @ ] + ) ( @ ( .+ ) ) ? $ / )
344+
345+ assert ( match !== null , `Invalid dependency format: ${ dep } ` )
346+
345347 const name = match [ 1 ]
346348 const range = match [ 3 ] || ''
347349 const cappedRange = getCappedRange ( name , range )
@@ -503,9 +505,10 @@ varySandbox.VARIANTS = ['default', 'star', 'destructure']
503505 * @param {boolean } shouldExpectTelemetryPoints
504506 */
505507function telemetryForwarder ( shouldExpectTelemetryPoints = true ) {
506- process . env . DD_TELEMETRY_FORWARDER_PATH =
507- path . join ( __dirname , '..' , 'telemetry-forwarder.sh' )
508- process . env . FORWARDER_OUT = path . join ( __dirname , 'output' , `forwarder-${ Date . now ( ) } .out` )
508+ const forwarderOut = path . join ( __dirname , 'output' , `forwarder-${ Date . now ( ) } .out` )
509+
510+ process . env . DD_TELEMETRY_FORWARDER_PATH = path . join ( __dirname , '..' , 'telemetry-forwarder.sh' )
511+ process . env . FORWARDER_OUT = forwarderOut
509512
510513 let retries = 0
511514
@@ -516,17 +519,20 @@ function telemetryForwarder (shouldExpectTelemetryPoints = true) {
516519 }
517520
518521 const cleanup = function ( ) {
519- let msgs
522+ /** @type {string[] } */
523+ let lines
520524 try {
521- msgs = readFileSync ( process . env . FORWARDER_OUT , 'utf8' ) . trim ( ) . split ( '\n' )
525+ lines = readFileSync ( forwarderOut , 'utf8' ) . trim ( ) . split ( '\n' )
522526 } catch ( e ) {
523527 if ( shouldExpectTelemetryPoints && e . code === 'ENOENT' && retries < 10 ) {
524528 return tryAgain ( )
525529 }
526530 return [ ]
527531 }
528- for ( let i = 0 ; i < msgs . length ; i ++ ) {
529- const [ telemetryType , data ] = msgs [ i ] . split ( '\t' )
532+ /** @type {Array<[string, unknown]> } */
533+ const msgs = [ ]
534+ for ( const line of lines ) {
535+ const [ telemetryType , data ] = line . split ( '\t' )
530536 if ( ! data && retries < 10 ) {
531537 return tryAgain ( )
532538 }
@@ -539,9 +545,9 @@ function telemetryForwarder (shouldExpectTelemetryPoints = true) {
539545 }
540546 throw new SyntaxError ( `error parsing data: ${ e . message } \n${ data } ` )
541547 }
542- msgs [ i ] = [ telemetryType , parsed ]
548+ msgs . push ( [ telemetryType , parsed ] )
543549 }
544- unlinkSync ( process . env . FORWARDER_OUT )
550+ unlinkSync ( forwarderOut )
545551 delete process . env . FORWARDER_OUT
546552 delete process . env . DD_TELEMETRY_FORWARDER_PATH
547553 return msgs
@@ -551,23 +557,25 @@ function telemetryForwarder (shouldExpectTelemetryPoints = true) {
551557}
552558
553559/**
554- * @param {string|{ then: (callback: () => Promise<string>) => Promise<string> }|URL } url
560+ * @param {string | URL | Promise<string | URL | { url: string }> | { url: string } } url
561+ * @returns {Promise<import('http').IncomingMessage & { body: string }> }
555562 */
556563async function curl ( url ) {
557564 if ( url !== null && typeof url === 'object' ) {
558- if ( url . then ) {
565+ if ( ' then' in url ) {
559566 return curl ( await url )
560567 }
561- url = url . url
568+ if ( 'url' in url ) {
569+ url = url . url
570+ }
562571 }
563572
564573 return new Promise ( ( resolve , reject ) => {
565574 http . get ( url , res => {
566575 const bufs = [ ]
567576 res . on ( 'data' , d => bufs . push ( d ) )
568577 res . on ( 'end' , ( ) => {
569- res . body = Buffer . concat ( bufs ) . toString ( 'utf8' )
570- resolve ( res )
578+ resolve ( Object . assign ( res , { body : Buffer . concat ( bufs ) . toString ( 'utf8' ) } ) )
571579 } )
572580 res . on ( 'error' , reject )
573581 } ) . on ( 'error' , reject )
@@ -576,7 +584,7 @@ async function curl (url) {
576584
577585/**
578586 * @param {FakeAgent } agent
579- * @param {string|{ then: (callback: () => Promise<string>) => Promise< string> }|URL } procOrUrl
587+ * @param {string | URL | Promise<string | URL | { url: string }> | { url: string } } procOrUrl
580588 * @param {(res: { headers: Record<string, string>, payload: unknown[] }) => void } fn
581589 * @param {number } [timeout]
582590 * @param {number } [expectedMessageCount]
0 commit comments