@@ -52,6 +52,34 @@ describe('Runtime', () => {
5252 clearInterval ( interval ) ;
5353 } ) ;
5454
55+ it ( 'should use globalThis.fetch bound to globalThis when fetch is not provided in configuration' , ( ) => {
56+ // Mock globalThis.fetch to verify it's used
57+ const originalFetch = globalThis . fetch ;
58+ let calledWithGlobalThis = false ;
59+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
60+ // @ts -ignore - Ignoring type errors for test purposes
61+ globalThis . fetch = async function ( ) {
62+ //This is important for "workerd" the process used by cloudflare workers.
63+ calledWithGlobalThis = this === globalThis ;
64+ return new Response ( ) ;
65+ } ;
66+
67+ try {
68+ const client = new TestClient ( {
69+ baseUrl : URL ,
70+ parseError,
71+ } ) ;
72+
73+ // Call the fetchApi
74+ ( client as any ) . fetchApi ( 'https://example.com' ) ;
75+
76+ expect ( calledWithGlobalThis ) . toBe ( true ) ;
77+ } finally {
78+ // Restore the original fetch
79+ globalThis . fetch = originalFetch ;
80+ }
81+ } ) ;
82+
5583 it ( 'should retry 429 until getting a succesful response' , async ( ) => {
5684 const request = nock ( URL , { encodedQueryParams : true } )
5785 . get ( '/clients' )
@@ -526,6 +554,50 @@ describe('Runtime for ManagementClient', () => {
526554 expect ( request . isDone ( ) ) . toBe ( true ) ;
527555 } ) ;
528556
557+ /* eslint-disable @typescript-eslint/ban-ts-comment */
558+ it ( 'should add the telemetry in workerd contexts' , async ( ) => {
559+ const originalVersion = process . version ;
560+ const originalNodeVersion = process . versions . node ;
561+ const originalNavigator = globalThis . navigator ;
562+ try {
563+ // Simulate a workerd context where process.version is not available
564+ // @ts -ignore
565+ delete process . version ;
566+
567+ // Simulate a workerd context where process.versions.node is not available
568+ // @ts -ignore
569+ delete process . versions . node ;
570+
571+ // @ts -ignore
572+ Object . defineProperty ( globalThis , 'navigator' , {
573+ value : { userAgent : 'Cloudflare-Workers' } ,
574+ configurable : true ,
575+ } ) ;
576+
577+ const clientInfo = utils . generateClientInfo ( ) ;
578+
579+ expect ( clientInfo ) . toEqual ( {
580+ name : 'node-auth0' ,
581+ version : expect . any ( String ) ,
582+ env : {
583+ 'cloudflare-workers' : 'unknown' ,
584+ } ,
585+ } ) ;
586+
587+ expect ( clientInfo . version ) . toMatch ( / ^ \d + \. \d + \. \d + (?: - [ \w . ] + ) ? $ / ) ;
588+ } finally {
589+ // @ts -ignore
590+ process . version = originalVersion ;
591+ // @ts -ignore
592+ process . versions . node = originalNodeVersion ;
593+ //@ts -ignore
594+ Object . defineProperty ( globalThis , 'navigator' , {
595+ value : originalNavigator ,
596+ configurable : true ,
597+ } ) ;
598+ }
599+ } ) ;
600+
529601 it ( 'should add custom telemetry when provided' , async ( ) => {
530602 const mockClientInfo = { name : 'test' , version : '12' , env : { node : '16' } } ;
531603
0 commit comments