-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
feat: support Cloudflare Workers #2289
Conversation
6954a22
to
c4d5458
Compare
This PR can run on Cloudflare Workers with the following database:
@sidorares could you please approve to run the GitHub action? |
4760d37
to
0b9245b
Compare
@shiyuhang0 @Mini256 thanks for working on this! Could you document simple steps I can follow to try it myself? |
This commit adds a step-by-step tutorial. Since it's user-facing, it needs a new release of MySQL2 including this PR. |
8949aca
to
cc3458f
Compare
CI is green. Maybe we can ignore the failure of CodeQL action, because the error is reported in the code that originally existed (just the location has changed) As for the static parser. We'd like to merge your PR #2099 rather than cherry-pick it into this PR. What's your opinion? @sidorares |
I can probably merge it first, together with added binary protocol non-jit parser. re testing - thanks a lot for dev documentation. Are you aware of any way to test workers locally or in actions runtime? something similar to localstack. Ideally with the same checks, e.i errors when code tries to eval. Any container we can use for that? |
Cloudflare Workers provide an https://developers.cloudflare.com/workers/wrangler/api/#unstable_dev Test code in node-postgres: |
I prefer |
I like it more than |
I have tested this pr on multiple MySQL server ranging from old 5.7 to more recent MySQL and MariaDB version locally and on deployed workers so far it works flawlessly. The only issue is having to set |
FYI:
Otherwise, we'd love to see this merged + we're bringing MySQL support to Hyperdrive: https://developers.cloudflare.com/hyperdrive/ |
I have resolved the conflict with master branch. |
Hi, @shiyuhang0 🙋🏻♂️ If it's okay, I'd like to make a few simple suggestions in the documentation:
|
already doing this, will push a commit soon |
@shiyuhang0 what do you think about |
Both it is fine for me. But we shouldn't worry about modifying parameter names in an unmerged PR. For me, I think it can even be modified before release. I will adjust this PR after the name is determined |
I really need this feature. I need to use the node-mysql2 driver with Drizzle in Cloudflare Workers. If everything is confirmed to be correct, could you please merge this PR as soon as possible? Thanks |
@m430 last time I checked Drizzle/Mysql was not compatible with cloudflare even using this updated driver. That is because of some node compatibility issue. @shiyuhang0 disableEval makes more sense to me |
@shiyuhang0 missed your message, I also prefer |
@sidorares I have pushed a new commit to use |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2289 +/- ##
=======================================
Coverage 88.97% 88.97%
=======================================
Files 86 86
Lines 13527 13527
Branches 1564 1564
=======================================
Hits 12035 12035
Misses 1492 1492
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Is there a path to instead supporting our modern https://developers.cloudflare.com/workers/runtime-apis/nodejs/ |
I did a simple test with However, when I connect in SSL, I will get an error:
https://github.com/Mini256/mysql2-cloudflare-test/blob/main/src/index.ts I will try to add this integration test to the node-mysql2 test and try to solve it. |
node compatibility table: https://workers-nodejs-compat-matrix.pages.dev/ |
still can't use in cloudflare worker
|
Hey folks! Are you using nodejs_compat flag? With the nodejs_compat flag in your wrangler.jsonc/wrangler.toml , and using the most recent version of the wrangler CLI which uses the most recent version of workerd with the missing pieces (cloudflare/workerd#3315), you should be able to use the static parser in a way that works on Cloudflare Workers |
I went ahead and created a repo that you can use as a reference for a sample project that works @i18nsite @Mini256 @wellwelwel https://github.com/thomasgauvin/mysql2-on-cf-workers You'll have to excuse my quick code that does not make use of env variables for connection details. It does run on Cloudflare Workers now with Please let me know if there's anything else I can do to help! |
@thomasgauvin, thanks for the update! I suppose all that's missing is just documenting Once this is confirmed, we can officially say that MySQL2 supports Cloudflare Workers by release in fact the canary version 🚀 |
@wellwelwel got it! Yes, I can confirm that |
@wellwelwel update on this thread: creating tls context is not something that can be done from within the Cloudflare Workers runtime because it is the Cloudflare runners (which are responsible for running Workers isolates) that establish the outbound connections What we're thinking here is to make it possible to set ssl certs in a Hyperdrive configuration rather than in the code itself. Bindings, such as the Hyperdrive binding or the mTLS binding, is usually how we resolve these limitations. Then we'd recommend folks use mysql2 to connect using Hyperdrive, leaving the ssl option undefined in the driver itself. (Hyperdrive is a connection pooler that also does query caching, which we're going make available on the free plan for this) |
@Mini256, I believe we can remove network wrappers and update the {
"compatibility_flags": ["nodejs_compat"],
} As Hyperdrive will be responsible for the secure TLS context with Cloudflare Workers, we can release the canary version with the documentation you have created. What do you think? |
@wellwelwel LGTM, let me rebase this PR and update the docs. |
5dd33a2
to
a514556
Compare
@thomasgauvin According to the Hyperdrive documentation, it currently only supports the Postgres protocol, right? Are there any plan to support the MySQL and the MySQL protocol-compatible database (like TiDB Serverless, PlanetScale) |
Thanks to everyone for your patience and great teamwork. Version |
@Mini256, yes, getting the runtime to work with MySQL drivers goes hand in hand with our work to support MySQL on Hyperdrive. We're still working on it but I should be able to get any of you early access if you email me (first initial last name at cloudflare.com) or drop a chat in our Discord channel |
try to close #2179
Summary
window is not found
, replace nodeTimers
with web timersCode generation from strings disallowed for this context
, using the static parser in PR Add "interpreting" parser #2099crypto.hash() is not a function
, polyfill nodecrypto
withwebcrypto
, and modify the functions which directly/indirectly called thecrypto
library as async function.TODOs:
pg-cloudflare
withmysql2-cloudflare
?Usage
For users, they need to:
node_compat = true
flag to polyfill the missing node modulesuseStaticParser
flag to force mysql2 to use static parser.For example: https://github.com/Mini256/mysql2-cloudflare-test/blob/1f339a85f4457b696f122503051991326dbc23dc/src/index.ts#L30