PURL-Workers
This project has been deprecated. Write your own serverless application instead.
A serverless URL redirection service running on Cloudflare Workers, with a few bells and whistles.
You may:
- Use Wrangler:
- Ensure that you have Node.js installed.
- Complete
wrangler.toml
by adding appropriate options; see https://developers.cloudflare.com/workers/wrangler/configuration/ for how to do this. - Run
npm install
to install dependencies. - Run
npm run deploy
to compile, upload, and configure the worker on Cloudflare.
- Copy & Paste:
- Ensure that you have Node.js installed.
- Go to Cloudflare Dashboard > Workers > Overview, press the "Create a Service" button (the starter template doesn't matter), then configure any necessary options.
- Run
npm install
to install dependencies. - Run
npm run build
to compile PURL-workers. - Press the "Quick edit" button, remove any residual content in the editor, copy the content of
dist/index.js
, paste it to the editor, then press the "Save and Deploy" button.
PURL-workers uses the path name of the incoming URL to look for an entry, which describes how to respond to the request:
http://www.example.com/foo/bar?q=baz&z#ch1
~~~~~~~~ path name
Use Wrangler to add, edit, or remove entries in Workers KV. For example, to put the content of file 403.html
under the current working directory to key /example/forbidden
that will be returned in a HTTP 403 Forbidden response:
npx wrangler kv:key put \
--binding kv --preview false \
--metadata '{"status":"403"}' \
--path ./403.html \
'/example/forbidden'
To remove it:
npx wrangler kv:key delete \
--preview false \
'/example/forbidden'
Run npx wrangler kv:key --help
for more information on how to use Wrangler to interact with a Workers KV.
An entry is indexed by a string as key and can be:
- A string shortcut.
- A
ResponseInit
object. - A responder function.
- A raw value.
A string shortcut may be further interpreted as a:
- Temporary redirection
- Permanent redirection
- Alias
- Raw string
There are two ways to define entries:
-
Build-time configuration: fill the
entries
object inpurl-workers.config.js
. Entries specified in this way:- are much faster than those in Workers KV;
- take precedence (that is, if
/key
is defined in both places, then the one in the configuration file will be chosen);
These entries will also be bundled together with PURL-worker during compile time; see The Configuration File for more information.
-
Run-time configuration: use Workers KV. Each key corresponds to a value and a metadata, both of which can be set with Wrangler:
- For a KV entry with metadata, the value will be returned directly (without any processing) based on what the metadata specified, regardless of the shape of value. The metadata must be a
ResponseInit
object1. - For a KV entry without metadata, PURL-workers will first try parsing it as an entry (can be a string shortcut or a
ResponseInit
object in JSON). If parsing fails, then the value will be returned directly as a binary blob.
Entries specified in this way:
- can be raw (binary) and larger
- can have metadata attached
- can be added and removed without re-deploying the worker
- For a KV entry with metadata, the value will be returned directly (without any processing) based on what the metadata specified, regardless of the shape of value. The metadata must be a
A temporary redirection tells the HTTP client to visit the URL in the Location
header, but without remembering (caching) this redirection. Most often, this is what you want.
https://www.example.com/redir/temp
Prepend an exclamation mark (!
) before the URL to create a permanent redirection instead, hinting HTTP clients to remember (cache) this redirection.
!https://www.example.com/redir/perm
ResponseInit
Object
The object will be used to create a Response
(as the option
parameter of its constructor). This is convenient for creating header-only responses without a body.
{
"status": 300,
"statusText": "Multiple Choice",
"headers": {
"Link": "</foo>; rel=alternate, </bar>; rel=alternate, </baz>; rel=alternate"
}
}
Specify any string that can possibly be a key name. If a string cannot be parsed into a URL redirection or an object, then it is used as a key to find another entry to respond.
/alias/maybe
A responder function is just a Cloudflare Workers fetch
handler function. This enables running any logic without losing the support of PURL-workers shortcuts. It's obvious that a responder function can only be specified in the configuration file.
See https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker for the definition of a fetch
handler function in the module worker syntax (which PURL-workers uses).
{
"/uwu": async (req, env) => {
const key = req.headers.get("API-Key");
if (key !== "Suki") {
return new Response("Kirai", { status: 406 });
}
const himitsu = await env.kv?.get("_senpai", "stream");
if (!himitsu) {
return new Response("Zannen", { status: 404 });
}
return new Response(himitsu);
}
}
purl-workers.config.js
is the configuration file (script, actually) for PURL-workers. It's a ECMAScript (JavaScript) module that is imported into the source code of PURL-workers, so code in the file will be bundled together with PURL-worker during compile time.
Two variables are expected from the file:
entries
: an object of entries in key-value pairs.config
: an object of options.
See The entries
Object for configuring entries
. The config
object supports the following options:
temporaryRedirect
: specify what status code should be used to indicate a temporary redirection. Optional. The default is302
.permanentRedirect
: specify what status code should be used to indicate a permanenet redirection. Optional. The default is301
.
This software is licensed under the GNU Affero General Public License; see COPYING for details.