Skip to content

Commit ca62c23

Browse files
authored
Workerd fixes (#1115)
2 parents f56b813 + 730c676 commit ca62c23

3 files changed

Lines changed: 104 additions & 8 deletions

File tree

src/lib/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class BaseAPI {
3131
}
3232

3333
this.middleware = configuration.middleware || [];
34-
this.fetchApi = configuration.fetch || fetch;
34+
this.fetchApi = configuration.fetch || globalThis.fetch.bind(globalThis);
3535
this.parseError = configuration.parseError;
3636
this.timeoutDuration =
3737
typeof configuration.timeoutDuration === 'number' ? configuration.timeoutDuration : 10000;

src/utils.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
import { version } from './version.js';
22

3+
/* eslint-disable @typescript-eslint/ban-ts-comment */
4+
function detectRuntime() {
5+
// Node.js
6+
if (typeof process !== 'undefined' && process.versions?.node) {
7+
return 'node';
8+
}
9+
10+
// Cloudflare Workers
11+
if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') {
12+
return 'cloudflare-workers';
13+
}
14+
15+
// Deno
16+
// @ts-ignore
17+
if (typeof Deno !== 'undefined') {
18+
return 'deno';
19+
}
20+
21+
return 'unknown';
22+
}
23+
324
/**
425
* @private
526
*/
6-
export const generateClientInfo = () => ({
7-
name: 'node-auth0',
8-
version: version,
9-
env: {
10-
node: process.version.replace('v', ''),
11-
},
12-
});
27+
export const generateClientInfo = () => {
28+
const runtime = detectRuntime();
29+
return {
30+
name: 'node-auth0',
31+
version: version,
32+
env: {
33+
[runtime]: process.version?.replace('v', '') ?? 'unknown',
34+
},
35+
};
36+
};
1337

1438
/**
1539
* @private

test/lib/runtime.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)