Benchmark storage and redirecting Cloudflare Worker for JSBenchmark.com
Redirects to the benchmark with the given shortcode
.
Examples:
Creates a new shortcode for the provided benchmark state. Must match the structure of a Benchmark or Repl state (see below).
Benchmark
interface BenchmarkState {
cases: {
id: string;
code: string;
name: string;
}[];
config: {
name: string;
parallel: boolean;
globalTestConfig: {
dependencies: {
url: string;
name: string;
esm: boolean;
}[];
};
dataCode: string;
};
}
Repl
interface ReplState {
config: {
name: string;
test: {
dependencies: {
url: string;
name: string;
esm: boolean;
}[];
code: string;
};
};
}
{ "code": "2bEXvzJfZWuvetvRdkEUXwOkCVd" }
Field | Type | Description |
---|---|---|
code |
string |
The shortcode for the benchmark. |
To get up-and-running in development, do the following:
- Setup Wrangler
I use Volta
but npm i -g
works just as well.
$ volta install wrangler # or
$ npm i -g wrangler
Then, run wrangler login
and follow the instructions.
- Clone
.dev.vars
The example file is prefixed with an underscore.
$ cp _.dev.vars .dev.vars
- Start postgres (and pgbouncer)
Run docker compose up --wait
to start postgres and pgbouncer.
Pgbouncer is used to pool connections to the database, which is required for serverless.
The migrations are ran automatically.
Neon, the database provider this project recommends in production, has a native pooling option, so this is only required for development.
To deploy this project, do the following:
- Create a database
...on https://neon.tech. Make sure you their pooling for serverless option.
Then, go to the SQL Editor tab and add the migrations in order from the migrations
folder.
Then, deconstruct the database URL into the following variables.
You're gonna need this later.
It might be a good idea to do this in .dev.vars
and comment out each line -- just so you have somewhere to copy and paste from.
# PG_USER = ""
# PG_PASSWORD = ""
# PG_HOST = ""
# PG_DATABASE = ""
- Deploy the worker
$ wrangler login # login to Cloudflare
$ wrangler deploy # deploy the worker
# for each key and value of PG_*, run the following command
# you can also do this on the Worker dashboard,
# but remember to click Entrypt!
$ wrangler secret put <key> # will open a prompt for the value
- Profit.
Now, you can create your own shortcodes with the API Documentation above.
You can test the worker by visiting the shortcode default
.