Deterministic shortlink resolution for Deno.
This library resolves shortlinks (compact path aliases) into fully-qualified URLs. It supports:
- Absolute destinations (e.g.,
example -> https://example.com) - Origin-relative internal redirects with loop protection (e.g.,
docs -> /documentation) - Path appending (e.g.,
/github/denoland/denoresolves againstgithub -> https://github.com) - Deterministic query parameter merging and fragment selection
The resolution algorithm is documented as an Internet-Draft: see “A Deterministic Algorithm for Resolving Shortlinks with Internal Redirects” (IETF Datatracker, spec repo).
Many organizations maintain internal "go/" links (e.g., go/docs) for knowledge
sharing. The need is portability: define shortlinks as a simple, static
Record<string, string> (JSON) and get identical behavior across Cloudflare
Workers, Vercel Edge, Deno servers, or static sites. Without a standard,
behaviors diverge, especially around query params, fragments, and internal
redirects.
This project and its Internet-Draft standardize the deterministic resolution algorithm so a single ruleset behaves the same everywhere.
- Simplicity: ruleset is a plain key-value map
- Determinism: longest-prefix match, well-defined query/fragment precedence
- Portability: no runtime-specific features; works on any platform
- Safety: loop protection for internal redirects
- No regex or wildcards
- No user-agent or conditional logic
- No complex rule engines
Rationale: allowing regex/wildcards transforms the data model into
Record<regex, substitution> and forces O(n) scans with engine-specific
semantics, undermining portability and the “simple JSON file” anchor. If you
need advanced routing, use tools like Nginx, Caddy, or Cloudflare Rules; this
library standardizes the "fiddly bits" for simple maps.
type Shortlinks = Record<string, string>;
// Example
const shortlinks = {
"github": "https://github.com",
"docs": "/documentation",
"docs/api": "/documentation/reference",
};- Stateless resolution from a provided ruleset
- Storage is out of scope (KV, JSON, DB all fine)
- Works anywhere a
URLand aRecord<string, string>are available
Teams and OSS orgs running internal go-links who want consistent behavior across platforms. This library is in production use across several open-source projects and sites.
Use via JSR in Deno:
import { go } from "jsr:@fartlabs/go";
// Given an incoming request URL and a shortlink ruleset
const url = new URL("https://example.com/docs/api/v1/users");
const shortlinks = {
docs: "/documentation",
"docs/api": "/documentation/reference",
};
const destination = go(url, shortlinks);
console.log(destination.href); // https://example.com/documentation/reference/v1/usersJSR documentation: https://jsr.io/@fartlabs/go
The library runs in the browser via esm.sh — no build step required:
<script id="shortlinks" type="application/json">
{
"github": "https://github.com"
}
</script>
<script type="importmap">
{
"imports": {
"@fartlabs/go": "https://esm.sh/jsr/@fartlabs/go@0.0.3"
}
}
</script>
<script type="module">
import { go } from "@fartlabs/go";
const shortlinks = JSON.parse(
document.getElementById("shortlinks").textContent,
);
const result = go(new URL(location.href), shortlinks);
// ...
</script>See demo/browser.html for a full working example.
See the Getting Started example above for basic usage. The function signature is:
function go(
url: URL,
shortlinks: Record<string, string>,
options?: { caseSensitive?: boolean },
): URL;By default, shortlink keys are matched case-sensitively. Pass
{ caseSensitive: false } to match keys regardless of case; an exact-case key
still wins, and the remainder of the path keeps its original casing.
Ruleset value types:
"https://..."→ absolute destination"/path"→ internal redirect (origin-relative), supports chained redirects with loop protection (256 max)
Query and fragment handling:
- Query parameters merge in this order: destination base query, then any query embedded in the matched pathname, then the request query (later values overwrite earlier ones)
- Fragment precedence: request
#hashif present, else matched-path#hashif present, else destination#hash
Absolute destination with path appending:
go(new URL("https://example.com/github/ietf/guidelines"), {
github: "https://github.com",
});
// => https://github.com/ietf/guidelinesInternal redirect chain:
go(new URL("https://example.com/docs/api/v1/users"), {
docs: "/documentation",
"docs/api": "/documentation/reference",
});
// => https://example.com/documentation/reference/v1/usersQuery merging and fragment precedence:
go(new URL("https://example.com/example?foo=bar#yang"), {
example: "https://example.com?baz=qux#yin",
});
// => https://example.com/?baz=qux&foo=bar#yangBackwards compatibility (keeping old links alive):
Version migration without breaking old links:
go(new URL("https://example.com/docs/v1/getting-started"), {
// Old version path now redirects to the stable location
"docs/v1": "/docs/latest",
});
// => https://example.com/docs/latest/getting-startedAlias rename while preserving existing references:
go(new URL("https://example.com/handbook/intro"), {
// Keep old alias but forward it internally to the new one
handbook: "/guide",
guide: "https://docs.example.com",
});
// => https://docs.example.com/introDeprecate a whole section but keep deep links working via longest-prefix:
go(new URL("https://example.com/blog/2023/launch"), {
blog: "https://news.example.com",
});
// => https://news.example.com/2023/launchSince shortlink rulesets are JSON objects, you can use jq for efficient
management:
jq '. + {"alias": "https://example.com"}' links.json > tmp.json && mv tmp.json links.jsonjq 'del(."old-alias")' links.json > tmp.json && mv tmp.json links.jsonjq --sort-keys . links.json > tmp.json && mv tmp.json links.jsonFind empty destinations:
jq 'to_entries | .[] | select(.value == "")' links.jsonInstall the skill to teach AI coding agents
to manage shortlinks using the go link protocol:
npx skills add fartlabs/goThe agent will then trigger the go skill and use standard jq patterns to
perform the task safely and accurately.
- Format:
deno fmt - Lint:
deno lint - Test:
deno test
This project is licensed under the Do What The Fuck You Want To Public License
(WTFPL). See LICENSE for details.
- JSR package: https://jsr.io/@fartlabs/go
- Internet-Draft: IETF Datatracker
- Protocol spec repo: https://github.com/EthanThatOneKid/go-protocol/
- Prior art: Deno's official documentation (Deno's implementation) implements a similar go-link pattern, demonstrating institutional demand for a standardized go-link protocol.
This package is only the library. go.fart.tools runs it
as a Cloudflare Worker; see
FartLabs/go.fart.tools. The web UI
and Deno KV API that used to live in this repo (cli/) were removed in 0.0.5.
Run deno fmt to format the code.
Run deno lint to lint the code.
Run deno test to run the tests.
Developed with ❤️ @FartLabs