Skip to content

Conversation

@DeanMauro
Copy link

@DeanMauro DeanMauro commented Dec 12, 2025

This PR makes OAuthHelpers available outside of the fetch method (i.e. in a worker's RPC methods).

Problem

image

this.env.OAUTH_PROVIDER gets populated in OAuthProviderImpl.fetch(), so if we try to use it in a worker's RPC methods, for example, it will always be undefined.

const oauthProvider = new OAuthProvider({});

export default class extends WorkerEntrypoint<AuthWorkerEnv> {
  async fetch(request: Request) {...}

  async checkClient(id: string) {
    // ERROR because this.env.OAUTH_PROVIDER isn't set yet
    const existingClient = await this.env.OAUTH_PROVIDER.lookupClient(id);

    // SUCCESS
    const existingClient = await oauthProvider.getOAuthHelpers(this.env).lookupClient(id);
  }
}

Solution

We need a method OAuthProvider.getOAuthHelpers(env: any) that initializes and retrieves OAuthHelpers directly from the provider.

❌ We can't put the method directly onto the provider because it will get exposed, intentionally or otherwise, as a public RPC method on the worker.

export class OAuthProvider {
  ...
 getOAuthHelpers(env: any) {
      return this.#impl.getOAuthHelpers(env);
  };
}

✅ But if we use a Symbol to name the method, it appears on the provider like we want, but excluded from the worker's RPC methods.

export class OAuthProvider {
  ...
  [getOAuthHelpersSymbol](env: any): OAuthHelpers {
    return this.#impl.getOAuthHelpers(env);
  }
}

export function getOAuthHelpers(provider: OAuthProvider, env: any): OAuthHelpers {
  return provider[getOAuthHelpersSymbol](env);
}

@changeset-bot
Copy link

changeset-bot bot commented Dec 12, 2025

⚠️ No Changeset found

Latest commit: d289be5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@DeanMauro
Copy link
Author

@mattzcarey - Can you give this a review?

@kentonv - Found a neat way to add methods to the provider without exposing them over RPC

/**
* Gets OAuthHelpers for the given environment, setting env.OAUTH_PROVIDER if not already set
* This is a getter property that returns a function, allowing it to be accessed like a normal class member
* without being exposed as an RPC method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hate to say but getters can, in fact, be invoked over RPC. It's an explicit feature of the RPC system.

You could perhaps create a separate freestanding function getOAuthHelpers(provider :OAuthProvider), and this function could work by reading a property whose name is a private Symbol.

Copy link
Author

@DeanMauro DeanMauro Dec 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh thought I was being clever here. Must have messed something up when testing.

Switched it over to a Symbol and getOAuthHelpers(provider: OAuthProvider, env: any) method. Confirmed this is working as expected. Thanks!

@DeanMauro DeanMauro requested a review from kentonv December 13, 2025 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants