adventofcode

2023-11-18T21:20:47.995759Z

Using Cloudflare Workers for this is very convenient: It’s deployed in seconds and you can just edit and test the worker code in their browser console. You could follow this example, I just deployed it in under 2 minutes: https://developers.cloudflare.com/workers/examples/cors-header-proxy/ handleOptions and handleRequest are the important functions here. If you don’t want to create a Cloudflare account but are interested, feel free to ping me and you could use my worker.

borkdude 2023-11-18T21:31:24.472979Z

@trost.mario I thought about that but wouldn't that be costly if everyone would go through my worker?

borkdude 2023-11-18T21:31:42.328249Z

I have deployed some squint code here: https://freqs.borkdude.workers.dev/?input=1119999

2023-11-18T22:23:30.065299Z

The free tier is 100'000 requests per day. Paid tier is minimum charge of $5 per month, 1 million requests cost $0.15 So you'd need lots of users to get it to what I call expensive! Every Clojure dev and then some? :)

borkdude 2023-11-18T22:24:26.019699Z

what about network traffic though? downloading those inputs and serving them up

borkdude 2023-11-18T22:25:58.788339Z

"Up to 10ms CPU time per request", what do they mean by that? 10ms seems not a lot

2023-11-18T22:26:49.632969Z

When you wait for requests it doesn’t count

2023-11-18T22:27:15.502349Z

You can fetch third party apis that take multiple seconds and that doesn’t count at all

borkdude 2023-11-18T22:29:17.617469Z

Seem great, why has nobody done this before for AOC then ;)

2023-11-18T22:29:19.955899Z

> Up to 10ms CPU time per request This often gets misunderstood: You can make requests to other third party services and also to Cloudflare services like Cache, Images, KV storage, Durable Objects etc. and all of that does not count to the limit

2023-11-18T22:31:48.863169Z

> Seem great, why has nobody done this before for AOC then ;) Haven’t they? 😄

2023-11-18T22:33:01.941099Z

> what about network traffic though? downloading those inputs and serving them up I think it’s free. If they notice you do stuff in the free account, that isn’t “fair use”, like proxying a Media Server with lots of traffic, then they just stop your worker.

borkdude 2023-11-18T22:33:51.780539Z

I would do this, but only if I can be 100% sure I won't receive a bill because of overnight bots trying to spam my worker

2023-11-18T22:33:54.916349Z

I guess compared with the amount of network traffic their “normal” product (reverse proxy for half the internet) the free workers traffic is just a rounding error

2023-11-18T22:34:37.818659Z

In the free tier it just stops serving requests when you reach the limit of 100k per day

borkdude 2023-11-18T22:34:56.468079Z

👍

2023-11-18T22:35:49.955589Z

I’m not even sure you have to give them your credit card details 🤷

borkdude 2023-11-18T22:36:00.094969Z

last time I didn't

borkdude 2023-11-18T22:36:41.700129Z

cool, so I could make an aoc app that is powered by squint worker and a squint playground to do the solutions

borkdude 2023-11-18T22:37:07.911079Z

and maybe a popup to enter your aoc session which can be stored in a cookie

2023-11-18T22:37:41.675309Z

Yes, I guess that shouldn’t be that difficult!

borkdude 2023-11-18T22:38:18.037359Z

ok, I might have a go at this tomorrow...

2023-11-18T22:42:23.594559Z

Very cool! I wanted to try out the Remix.run Cloudflare Workers template and adjust it to use Squint, but never came around to it. But just using a vanilla Cloudflare Workers setup shouldn’t be too difficult. Or: You could even try using Cloudflare Pages (it is powered by Cloudflare Workers): It’s similar to Netlify and Vercel in that you can connect your repository and do some basic configuration (build command and directory) and they build and deploy it for you plus preview deployments for every pull request.

borkdude 2023-11-18T22:43:35.490049Z

I already figured out how to deploy code for just a vanilla worker: https://github.com/borkdude/squint-bun-cloudflare

🙌 1
borkdude 2023-11-18T22:45:06.483309Z

This is just being fancy, an import with .cljs ;) https://github.com/borkdude/squint-bun-cloudflare/blob/6ce8a81da46e2c24ff0677959dbba8125f8803f2/src/index.js#L11

borkdude 2023-11-18T22:46:13.564649Z

not strictly necessary, just mixing various hyped technologies all at once: bun loader + cloudflare workers

2023-11-18T22:48:18.825009Z

Trailblazing as usual 🚀 And hype driven engineering is a legit thing 😄 Especially when the hype is warranted thanks for sharing! I’m off to bed now, good night to you

2023-11-18T22:48:29.526649Z

And: wish you success tomorrow 🙂

🙏 1
borkdude 2023-11-18T22:49:15.164959Z

I guess I could limit access to the worker only from a specific domain, like the squint github pages right?

borkdude 2023-11-18T22:49:36.639909Z

but that's probably easy to fake with a CLI http client

2023-11-18T22:53:08.578339Z

Yes, you could limit it to domain plus set a secure, http-only cookie on the first request and check for that on subsequent ones

2023-11-18T22:55:36.293249Z

Both can be faked outside a browser, but gets harder and harder, the more you add. Some hidden inputs that get sent along on form submits, CSRF tokens etc.

borkdude 2023-11-19T12:02:42.450739Z

it's very strange, in bun at least, when I use js/fetch to call another API, it calls my own worker

borkdude 2023-11-19T12:03:06.438119Z

(defn ^:async fetch [req _env _ctx]
  (if-let [aoc-token (some-> req :headers (.get :cookie) (cookie/parse) :AOC_TOKEN)]
    (let [params (-> req :url (js/URL.) :searchParams)
          {:keys [day year]} params]
      (if (and day year)
        (let [resp (js-await (js/fetch (js/Request. (str "" year "/day/" day "/input"))))]
          (if-not (:ok resp)
            (do
              (prn :not-ok!!!)
              (js/console.log resp)
              (let [txt (js-await (.text resp))]
                (js/console.log :txt txt)
                (js/Response. txt)))
            (js/Response. "dude")))
        (js/Response. "Set 'day' and 'year' as a URL query parameter")))
    (do
      (prn :ending-up-here)
      (js/Response. "Set 'AOC_TOKEN' as a cookie." {:status 400}))))

borkdude 2023-11-19T12:04:46.487109Z

somehow the "url" of the response is empty:

url: ""

borkdude 2023-11-19T12:06:04.531949Z

oh dang... I called my function fetch which overwrites js/fetch facepalm

borkdude 2023-11-19T12:06:30.948449Z

that's it

borkdude 2023-11-19T12:06:45.984409Z

thanks duckie

🙌 1
borkdude 2023-11-19T12:09:13.133239Z

it works magically now

borkdude 2023-11-19T12:09:40.228599Z

The entire code for this worker:

(ns index
  (:require ["cookie" :as cookie]))

(defn ^:async handler [req _env _ctx]
  (if-let [aoc-token (some-> req :headers (.get :cookie) (cookie/parse) :AOC_TOKEN)]
    (let [params (-> req :url (js/URL.) :searchParams)
          {:keys [day year]} params]
      (if (and day year)
        (let [resp (js-await (js/fetch (str "" year "/day/" day "/input")
                                       {:headers {:cookie (str "session=" aoc-token)}}))]
          resp)
        (js/Response. "Set 'day' and 'year' as a URL query parameter")))
    (js/Response. "Set 'AOC_TOKEN' as a cookie." {:status 400})))

(def default
  {:fetch handler})

borkdude 2023-11-19T12:15:51.973929Z

curl '' --cookie "AOC_TOKEN=<your-session>"

borkdude 2023-11-19T13:36:08.220129Z

I'm still running into CORS issues when I do the request from the website and I'm about to give up on this

borkdude 2023-11-19T13:37:24.499089Z

ERR_CONTENT_DECODING_FAILED - some other error

borkdude 2023-11-19T13:38:56.700909Z

aaaah got it, I just copied too much from the original response

borkdude 2023-11-19T13:39:02.934569Z

duckie

😄 1
Sam Ferrell 2023-11-17T23:07:20.924209Z

the server (adventofcode) would need to respond with the appropriate CORS headers to allow cross-origin requests

Sam Ferrell 2023-11-17T23:08:18.380319Z

otherwise you would need to proxy the request through a non-browser environment

borkdude 2023-11-17T23:09:33.014069Z

yeah, it's a bit inconvenient since this doesn't let you solve AoC puzzles from a browser playground I guess I could write a service that requests the input on your behalf

Ellis 2023-11-18T07:02:16.861479Z

Check how I do it https://github.com/elken/aoc/blob/6452447624adf641f4712693369a5ab383eded7d/src/util.clj#L9 Ah it's early, yeah its a CORS issue