-
Notifications
You must be signed in to change notification settings - Fork 62
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
Remove Final Blockers to CloudFlare Workers Support #793
Conversation
dc442cb
to
e69fa5f
Compare
6f40b85
to
e3764ee
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## devel #793 +/- ##
==========================================
+ Coverage 96.89% 97.02% +0.13%
==========================================
Files 30 31 +1
Lines 965 976 +11
Branches 212 217 +5
==========================================
+ Hits 935 947 +12
+ Misses 30 29 -1 ☔ View full report in Codecov by Sentry. |
src/lib/getCrumb.ts
Outdated
import type { ExtendedCookieJar } from "./cookieJar"; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: we have to ignore this for csm output. | ||
import pkg from "../../package.json" assert { type: "json" }; |
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.
I know you only added these recently @gadicc but looking at the commit log it seems like you added them to allow backwards support for Node 17 which is dropped by this PR, so, yay?
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.
Very off topic here, but this annoyingness was mostly related to Jest in this case. Curious on your thoughts on moving to vitest for nicer ESM support, and also quicker tests
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.
I know you only added these recently @gadicc but looking at the commit log it seems like you added them to allow backwards support for Node 17 which is dropped by this PR, so, yay?
Kinda. This one was a total pain, because, newer nodes fail without the assertion, older nodes fail with it, and they didn't have a sufficient gap between enforcing - so became very tricky to support a range (also because it's at the static level and you can't do runtime checks).
The Node 17 issue was that it requires the older assert
{ type: "json" }
syntax vs the newer (and better supported?) with
{ type: "json" }
syntax that superseded it. So it definitely helps that we're dropping Node < 18, but I think this is still required for newer versions. Did you have any luck with a CLI run? e.g. yarn build
and then yahoo-finance quote AAPL
from the command line? This is where I ran into this.
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.
Very off topic here, but this annoyingness was mostly related to Jest in this case. Curious on your thoughts on moving to vitest for nicer ESM support, and also quicker tests
Open to discussing this, feel free to open another issue ;) Also FYI is #767 from my other favourite contributor at the moment to get rid of all the fetchDevel
stuff and replace it with my https://www.npmjs.com/package/jest-fetch-mock-cache package (which was designed to be a more general fetchDevel
that works via mocks).
src/lib/options.ts
Outdated
@@ -53,7 +52,7 @@ export type YahooFinanceOptions = Static<typeof YahooFinanceOptionsSchema> & { | |||
}; | |||
|
|||
const options: YahooFinanceOptions = { | |||
YF_QUERY_HOST: process.env.YF_QUERY_HOST || "query2.finance.yahoo.com", |
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 one was a bit painful as CloudFlare would require a import * as process from "node:process"
here. But that won't work in a browser, seeing as this was one of very few instances of this I figured I could just delete it. Perhaps not, though?
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.
Yeah, I wouldn't just delete this 😅 Also because that would be a breaking change. I'll write some initial thoughts on process.env
further below.
src/lib/options.ts
Outdated
@@ -21,7 +21,6 @@ const ValidationOptionsSchema = Type.Object({ | |||
logOptionsErrors: Type.Optional(Type.Boolean()), | |||
_internalThrowOnAdditionalProperties: Type.Optional( | |||
Type.Boolean({ | |||
default: process.env.NODE_ENV === "test", |
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.
Also had to remove this to get process.env
off the main code path. Was pretty trivial to override the property in testYf.ts
, though
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.
As per above, I'll have some more comments on process.env
further below.
Looks good, @eddie-atkinson 🙏 Replying to your individual comments inline. |
Ok so re
As a small side note, I think giving into the pressure to support running in the browser was probably a mistake, initially because of all the CORS hassles which I've never managed to emphasize clearly enough, and more recently, I need to actually see if any of the new cookie code even works in the browser at all because without Lastly, definite advantage to having a single default import, but if there's no avoiding it, we could also have a particular import path for e.g. CloudFlare workers. But let's save that as a last resort if we don't manage to solve it any other way. Re all the above, I'm happy to take it on and experiment a bit. You could just revert / add back in the relevant lines and I'll implement the above, commit to dev, and you can see if its workable for CloudFlare? |
Otherwise yeah, all looks good. Very exciting to finally move this forward. Never used Cloudflare workers but I've always wanted to support them (and Vercel Edge functions). So thanks - once again! - for your passion and efforts here 🙏 😁 |
Live and learn I guess, on the
I can't believe this didn't occur to me 🤦. Looking at the various other runtimes it seems like we could almost just get away with
Based on the above I think an option which has legs is to create a
Yep that works for me, happy to be pragmatic. Also happy to have a hack, but it seems like you've got an idea for how this should work, so also happy to defer to that 👍 |
No, I think it's a no-go. That page looks a lot clearer than the last time I read it, and from my understanding,
Ooh, thanks for checking up on all the different environments! Super helpful and very encouraging. So if I understood correctly then, the user should implement such env copying logic themselves at the start of the worker handler? I guess once we have all the variables consolidated in one place we could also quite easily provide a function to do this for them, that they could call with 1 line at the beginning of the handler, which I'm sure would be quite welcome. (Forgive any mislabled terms, never touched Cloudflare workers before).
If you're happy to do it, I'll be very happy for the help :) Was a little worried that you once again took on something small that become big; glad that's not the case. I currently have family visiting abroad so this will definitely all happen much more quickly with your help. So thanks again 🙏
Ok great, let's go the This also opens the the door to a more generalized way to deal with the Cloudflare env problem, which export default {
fetch(req, env) {
yahooFinance.setGlobalConfig({ mergeEnv: env });
}
}; although I haven't thought about it too deeply yet. How does that look to you as someone more experienced with Cloudflare workers? We could also check on first |
@eddie-atkinson, quick heads up, had another struggle with the import assertions under various environments, eventually I replaced I'm still using the |
Hi, thanks all of you for your wonderful contributions. Really appreciated. 😊🙏🏻 When can we merge this PR and finalize this for CF use? |
Hey, @ManasMadrecha. This is on our radar but unfortunately the PR isn't complete yet. We're almost there but need a bit more time to finish this up. Watch this space :) |
Right, well that was annoying. I synced the fork and everything got removed 🫠 The fork was pretty far behind, so I'll recreate the PR. I might get the less tricky bits in first then I can noodle more on the env issue |
Hey @ManasMadrecha, I have opened a follow up here, with some discussion about the remaining blockers to CF support. Thankfully the list is shorter than last time I opened this PR 😄 |
Closes #719 .
Changes
node-fetch
in favour of native Node 18fetch
process.env
usage in main code pathYF_QUERY_HOST
env varType
Comments/notes
Final tweaks to the library needed for support for CloudFlare Workers (and possibly alternate runtimes 🤷 ), more discussion here
Working in miniflare locally: