Skip to content

Workerd fixes #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}

this.middleware = configuration.middleware || [];
this.fetchApi = configuration.fetch || fetch;
this.fetchApi = configuration.fetch || globalThis.fetch.bind(globalThis);
this.parseError = configuration.parseError;
this.timeoutDuration =
typeof configuration.timeoutDuration === 'number' ? configuration.timeoutDuration : 10000;
Expand Down Expand Up @@ -276,7 +276,7 @@
if (config.isCollectionFormatMulti) {
value = requestParameters[key];
} else {
value = requestParameters[key].join(COLLECTION_FORMATS[config.collectionFormat!]);

Check warning on line 279 in src/lib/runtime.ts

View workflow job for this annotation

GitHub Actions / Build and Test (18.17)

Forbidden non-null assertion

Check warning on line 279 in src/lib/runtime.ts

View workflow job for this annotation

GitHub Actions / Build and Test (20.3)

Forbidden non-null assertion
}
} else {
if (requestParameters[key] !== undefined) {
Expand Down
38 changes: 31 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import { version } from './version.js';

/* eslint-disable @typescript-eslint/ban-ts-comment */
function detectRuntime() {
// Node.js
if (typeof process !== 'undefined' && process.versions?.node) {
return 'node';
}

// Cloudflare Workers
if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') {
return 'cloudflare-workers';
}

// Deno
// @ts-ignore
if (typeof Deno !== 'undefined') {
return 'deno';
}

return 'unknown';
}

/**
* @private
*/
export const generateClientInfo = () => ({
name: 'node-auth0',
version: version,
env: {
node: process.version.replace('v', ''),
},
});
export const generateClientInfo = () => {
const runtime = detectRuntime();
return {
name: 'node-auth0',
version: version,
env: {
[runtime]: process.version?.replace('v', '') ?? 'unknown',
},
};
};

/**
* @private
Expand Down
72 changes: 72 additions & 0 deletions test/lib/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ describe('Runtime', () => {
clearInterval(interval);
});

it('should use globalThis.fetch bound to globalThis when fetch is not provided in configuration', () => {
// Mock globalThis.fetch to verify it's used
const originalFetch = globalThis.fetch;
let calledWithGlobalThis = false;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Ignoring type errors for test purposes
globalThis.fetch = async function () {
//This is important for "workerd" the process used by cloudflare workers.
calledWithGlobalThis = this === globalThis;
return new Response();
};

try {
const client = new TestClient({
baseUrl: URL,
parseError,
});

// Call the fetchApi
(client as any).fetchApi('https://example.com');

expect(calledWithGlobalThis).toBe(true);
} finally {
// Restore the original fetch
globalThis.fetch = originalFetch;
}
});

it('should retry 429 until getting a succesful response', async () => {
const request = nock(URL, { encodedQueryParams: true })
.get('/clients')
Expand Down Expand Up @@ -526,6 +554,50 @@ describe('Runtime for ManagementClient', () => {
expect(request.isDone()).toBe(true);
});

/* eslint-disable @typescript-eslint/ban-ts-comment */
it('should add the telemetry in workerd contexts', async () => {
const originalVersion = process.version;
const originalNodeVersion = process.versions.node;
const originalNavigator = globalThis.navigator;
try {
// Simulate a workerd context where process.version is not available
// @ts-ignore
delete process.version;

// Simulate a workerd context where process.versions.node is not available
// @ts-ignore
delete process.versions.node;

// @ts-ignore
Object.defineProperty(globalThis, 'navigator', {
value: { userAgent: 'Cloudflare-Workers' },
configurable: true,
});

const clientInfo = utils.generateClientInfo();

expect(clientInfo).toEqual({
name: 'node-auth0',
version: expect.any(String),
env: {
'cloudflare-workers': 'unknown',
},
});

expect(clientInfo.version).toMatch(/^\d+\.\d+\.\d+(?:-[\w.]+)?$/);
} finally {
// @ts-ignore
process.version = originalVersion;
// @ts-ignore
process.versions.node = originalNodeVersion;
//@ts-ignore
Object.defineProperty(globalThis, 'navigator', {
value: originalNavigator,
configurable: true,
});
}
});

it('should add custom telemetry when provided', async () => {
const mockClientInfo = { name: 'test', version: '12', env: { node: '16' } };

Expand Down
Loading