DEV Community

MT
MT

Posted on • Originally published at miantiao.me on

hink - A short link system with just 10 lines of code.

I’d like to share a small tool I recently created—hink, a short link system built with fewer than 10 lines of code.

This tool leverages Git and Serverless platforms to enable short link generation and access analytics.

Core Concept: The Clever Use of Git Commit Hashes

The core idea behind hink is simple: it uses the hash value of an empty Git commit as a unique identifier for the short link, while storing the original long URL in the commit message. When a short link is accessed, the system retrieves the commit message via GitHub’s .patch file interface, extracts the long URL, and redirects to it. Paired with a cloud platform’s WAF (Web Application Firewall) analytics dashboard, it also provides access statistics.

This solution has been successfully tested on the following platforms:

  • Cloudflare Workers/Snippets + Cloudflare WAF (Pro)
  • Tencent Cloud EdgeOne (Free)
  • Alibaba Cloud ESA (Free)

Code: Minimalist Implementation

The code for hink is extremely concise, with the core logic boiled down to just a few lines. Below are the implementations for different platforms.

Cloudflare Workers / Alibaba Cloud ESA Version

const GIT_REPO = "https://github.com/ccbikai/hink"
export default {
  async fetch(request) {
    const { pathname } = new URL(request.url)
    const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
    const patch = await fetch(gitPatch, { cf: { cacheEverything: true, cacheTtlByStatus: { '200-299': 86400 } }}).then(res => res.text())
    const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
    return Response.redirect(url || GIT_REPO)
  }
}
Enter fullscreen mode Exit fullscreen mode

Tencent Cloud EdgeOne Version

const GIT_REPO = "https://github.com/ccbikai/hink"
addEventListener("fetch", async (event) => {
  const { pathname } = new URL(event.request.url)
  const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
  const patch = await fetch(gitPatch).then(res => res.text())
  const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
  event.respondWith(new Response(null, { status: 302, headers: { Location: url || GIT_REPO } }))
});
Enter fullscreen mode Exit fullscreen mode

Once deployed to a Serverless platform and bound to a domain, you’ll have your own short link service.

Why Build hink?

Short link services aren’t new—there are plenty of tools out there. However, hink’s goal was to explore a minimalist and creative approach. By using Git commit hashes, it eliminates the need for database management, relies on GitHub for storage, and utilizes cloud platform WAF features for easy access analytics. For me, this was a technical experiment to solve a real problem with the least amount of code.

Demo: Performance Across Three Platforms

I’ve deployed hink on Cloudflare Workers, Alibaba Cloud ESA, and Tencent Cloud EdgeOne, and monitored access stats via their WAF dashboards. Below are screenshots of the performance on each platform:

Cloudflare Workers

Cloudflare

Alibaba Cloud ESA

Alibaba

Tencent Cloud EdgeOne

Tencent

Project Repository

ccbikai/hink - GitHub

Top comments (0)