quisi.do
quisi.do
CDCloudflare Developers
Created by quisi.do on 6/14/2024 in #workers-help
What storage model should I use to validate that an API key is only used X times each month?
I want to vend API keys to users who can call the API e.g. 100 times each month. I'm considering using KV namespaces with $API_KEY=$BUDGET and decrementing the budget by 1 each call, but I'm afraid of the cost of reading and writing to KV for every API call. To improve performance and cost, I'm considering writing API calls to an Analytics Engine dataset, then using cron (e.g. every 5 minutes) to read from the dataset and update the KV namespace accordingly. Additionally to improve performance and cost, I'm considering duplicating the KV data to the Cache API and using that as long as its within e.g. 5 minutes old. This assumes that the inaccuracy within 5 minutes is cheaper than the KV reads and writes would have been. Am I thinking about this right? Or is there a simpler solution? User story: Given the API key abcdefg, when you call /my-api/?key=abcdefg the first 100 times, you get an OK response code. When you call the API the 101st time, you get an error response. I understand this question is very subjective and could be solved in a lot of different ways. I'd really just like help from someone smarter than me or who has solved this problem before.
9 replies
CDCloudflare Developers
Created by quisi.do on 3/23/2024 in #workers-help
What's the best way to cache user data?
I want to cache a JSON blob of user data to save latency on multiple-D1-query lookups. Is there a known best option for this? I was considering the Cache API but I'm unsure if other users would be able to access values in the Cache API outside of my worker explicitly returning it.
8 replies
CDCloudflare Developers
Created by quisi.do on 3/16/2024 in #workers-help
Do page builds support patching in Yarn 4?
I'm using Yarn 4 for a Cloudflare Pages build, with the custom command yarn prepack. I have a .yarn/patches/ directory, and I don't believe it is being applied during installation. The build passes in all other environments (local, GitHub Actions), but fails with the pre-patch error only on Cloudflare Pages. Is this because initialization command is yarn instead of yarn install? Because the it detected yarn@3.6.3 as a "tool from environment", even though .yarn/releases/yarn-4.1.1.cjs is present? I'm not sure if there are steps I can use to debug this or if it's a known issue.
1 replies
CDCloudflare Developers
Created by quisi.do on 2/27/2024 in #workers-help
How do you resolve workspace dependencies?
I have a package @me/xyz in the same monorepo as my worker. I list the package as in the worker's package.json dependencies entry as workspace:^. It runs in wrangler dev fine. When I try to deploy, however, I get this error:
Could not resolve "@me/xyz" You can mark the path "@me/xyz" as external to exclude it from the bundle, which will remove this error.
How do I fix this?
5 replies
CDCloudflare Developers
Created by quisi.do on 1/28/2024 in #workers-help
Can two wrangler dev instances share a KV namespace?
I have kv_namespaces = [{ binding = "XYZ", id = "XYZ" }] (where id is just a copy of binding, not a real namespace on Cloudflare) in two different wrangler.toml files. I'm running wrangler dev in both directories. I want the second worker to be able to read the KV pairs set by the first worker. Is this possible? list() returns an empty array, as if no KV pairs have ever been set. I assume it's trying to read from two different KV.
2 replies
CDCloudflare Developers
Created by quisi.do on 1/14/2024 in #workers-help
What's the latency for KV and D1?
I'm debating on just using D1 with a table of key-value pairs, but I was told the latency for KV is significantly less than D1 for repeat lookups. I'm wanting to do a lookup as a part of every request, so the answer is important here, but I haven't been able to find any data. How long does it take to load a value from KV? And how long does it take to load a value from a D1 table?
15 replies
CDCloudflare Developers
Created by quisi.do on 10/8/2023 in #general-help
How do you call the /user/tokens/verify API from the browser?
I'm trying this:
const response: Response = await window.fetch(
'https://api.cloudflare.com/client/v4/user/tokens/verify',
{
method: 'GET',
mode: 'no-cors',
headers: new Headers({
Authorization: `Bearer ${apiToken}`,
'Content-Type': 'application/json',
}),
},
);
const response: Response = await window.fetch(
'https://api.cloudflare.com/client/v4/user/tokens/verify',
{
method: 'GET',
mode: 'no-cors',
headers: new Headers({
Authorization: `Bearer ${apiToken}`,
'Content-Type': 'application/json',
}),
},
);
This is the same code as the official documentation except with mode: 'no-cors' because CORS rejects requests from localhost. Unlike the documentation, this only responds with "400 Bad Request."
17 replies
CDCloudflare Developers
Created by quisi.do on 6/11/2023 in #workers-help
What's the difference between a request and a subrequest?
I'm checking my analytics, workers invocations adaptive, and I see a requests_sum and subrequests_sum, totaling 265 and 175 respectively. What do these two metrics mean?
4 replies
CDCloudflare Developers
Created by quisi.do on 6/10/2023 in #workers-help
When deploying, what does the error `placement_and_triggers_configured` mean?
I am using Wrangler 2.2 (cannot upgrade to 3 for technical limitations), and when I run wrangler publish, I am met with the following error: workers.api.error.placement_and_triggers_configured [code: 100150]. Using the --dry-run flag, I do not receive an error.
9 replies
CDCloudflare Developers
Created by quisi.do on 5/30/2023 in #general-help
How can I test a cache rule?
9 replies
CDCloudflare Developers
Created by quisi.do on 5/13/2023 in #general-help
What are the units for the web analytics?
7 replies
CDCloudflare Developers
Created by quisi.do on 5/8/2023 in #general-help
Is there an API for the content in the "Web Analytics" dashboard?
I'm wanting to create a custom dashboard for my web analytics, but the API documentation seems to have no message of them. There appear to be other, eerily similar APIs, but none that seem to have the same coverage as the Web Analytics dashboard.
11 replies
CDCloudflare Developers
Created by quisi.do on 2/9/2023 in #workers-help
Why is cron running every minute?
4 replies
CDCloudflare Developers
Created by quisi.do on 2/6/2023 in #workers-help
How do you generate cloudflare/templates with Wrangler?
The documentation in the cloudflare/templates repository says that "cloudflare-create" is sunsetting and to use "wrangler generate" instead. However, the wrangler generate command has no documentation for how to do this either.
9 replies