-
Notifications
You must be signed in to change notification settings - Fork 26
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
Cloudflare worker with the same behavior as Node.js/Express middleware #248
Conversation
@@ -4,6 +4,7 @@ | |||
"description": "GitHub OAuth toolset for Node.js", | |||
"scripts": { | |||
"build": "pika build", | |||
"build:cloudflare": "esbuild src/middleware/cloudflare/index.ts --bundle --format=esm --outfile=dist/worker.mjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed. Pika automatically includes the files in src/**/*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloudflare worker accepts a single JS file. Please let me know if I need to use Pika to bundle the worker script. How should the pipeline be structured? Sorry, I’m new to Pika.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundling should be left to end-users. We only compile the source down to plain JavaScript, which Pika handles.
This comment has been minimized.
This comment has been minimized.
Sorry I missed your comment. I think you'll need to create a new pull request on your fork at baoshan/oauth-app.js@runtime-agnostic-middleware...baoshan:cloudflare-worker |
Could you add a README with usage instructions to the One thing I'm not sure about is whether we should read out environment variables within the code, I think that's an anti-pattern. I'd rather have normal options and let the user do the mapping to environment variables they defined. |
de53ad4
to
df544e8
Compare
Thanks for the suggestions: Here is the PR: baoshan#3 I expect users of
Do you suggest users need to write JavaScript code to bind In the documentation: https://developers.cloudflare.com/workers/cli-wrangler/configuration#modules
To do that, users have to change I’ll add a README (maybe to Thanks again. |
You wouldn't deploy For comparison, if you wanted to deploy an app to heroku or another deployment target that uses a long-lived node process, you could create a new repository with this code const { OAuthApp, createNodeMiddleware } = require("@octokit/oauth-app");
const app = new OAuthApp({
clientType: process.env.CLIENT_TYPE || "oauth-app",
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
require("http").createServer(createNodeMiddleware(app)).listen(process.env.PORT); And then deploy that. That's where you set the environment variables, not in the source code of the library. Because using environment variables in the library code itself can cause unforeseen side effects. For comparison, see the Probot adapter we created for Azure functions and the example app that uses that adapter Basically I'd suggest you create a repository such as https://github.com/gr2m/cloudflare-worker-github-app-example which uses the new cloudflare middleware in Does that make sense? |
This PR creates a
cloudflare
folder which exports a Cloudflare worker that behaves exactly the same as the Node.js/Express middleware (because the same generalized HTTP handler is doing the job).I think it’s simpler to keep middlewares for different platforms in a same repo to reduce cognition cost. Cloudflare has two types of worker:
service-worker
andmodule
. The former requires themain
field ofpackage.json
points to a worker while themodule
type supports specifying the path of the module inwrangler.toml
. So this PR uses themodule
type.Following environment variables are required/supported by the script:
No automated tests are authored because I don’t know whether it’s worthy to import a simulator for Cloudflare workers such as
miniflare
. But I’d like to add automated tests if the maintainers believe it is necessary.esbuild
is used to bundle the script because I have no idea of howpika
works (and it is no longer maintained). I’d like to change the bundler topika
or something else if necessary.I have no idea of how to submit a PR against the previous one #247, so the diff is long. Only files in the
cloudflare
folders (pluswrangler.toml
andpackage.json
) are pertinent.I’d like to catch up with a README section later. For quick test, I’ve deployed a live version to
https://octokit-oauth-app.icmd.workers.dev
.Thanks.