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
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
@@ -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;
@@ -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

GitHub Actions / Build and Test (18.17)

Forbidden non-null assertion

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

GitHub Actions / Build and Test (20.3)

Forbidden non-null assertion
}
} else {
if (requestParameters[key] !== undefined) {
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
72 changes: 72 additions & 0 deletions test/lib/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -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')
@@ -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' } };


Unchanged files with check annotations Beta

actions.getAll().catch((err) => {
expect(err).toBeDefined();
done();

Check warning on line 105 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 105 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
data[0].all_changes_deployed
);
done();

Check warning on line 153 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 153 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
it('should perform a GET request', (done) => {
actions.getAll().then(() => {
expect(request.isDone()).toBe(true);
done();

Check warning on line 160 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 160 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
actions.getAll().then(() => {
expect(request.isDone()).toBe(true);
done();

Check warning on line 174 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 174 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
actions.getAll(params).then(() => {
expect(request.isDone()).toBe(true);
done();

Check warning on line 190 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 190 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
});
actions.get({ id: data.id as string }).then(() => {
expect(request.isDone()).toBe(true);
done();

Check warning on line 230 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 230 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
actions.get({ id: data.id as string }).catch((err) => {
expect(err).toBeDefined();
done();

Check warning on line 242 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 242 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
expect(credentials.data.integration?.id).toBe(data.integration?.id);
expect(credentials.data.all_changes_deployed).toBe(data.all_changes_deployed);
done();

Check warning on line 274 in test/management/actions.test.ts

GitHub Actions / Build and Test (18.17)

Avoid calling back inside of a promise

Check warning on line 274 in test/management/actions.test.ts

GitHub Actions / Build and Test (20.3)

Avoid calling back inside of a promise
});
});
/**
*
*/
export interface PostFormsRequestNodesInnerAnyOf2ConfigComponentsInnerAnyOf2AnyOf13Config {}

Check warning on line 12849 in src/management/__generated/models/index.ts

GitHub Actions / Build and Test (18.17)

An empty interface is equivalent to `{}`

Check warning on line 12849 in src/management/__generated/models/index.ts

GitHub Actions / Build and Test (20.3)

An empty interface is equivalent to `{}`
/**
*
*/