Skip to content

Instantly share code, notes, and snippets.

@muan

muan/worker.js Secret

Last active July 2, 2025 13:10
Show Gist options
  • Select an option

  • Save muan/388430d0ed03c55662e72bb98ff28f03 to your computer and use it in GitHub Desktop.

Select an option

Save muan/388430d0ed03c55662e72bb98ff28f03 to your computer and use it in GitHub Desktop.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
if (request.method !== 'POST') return
const url = new URL(request.url)
const id = url.searchParams.get('id')
const emoji = ensureEmoji(await request.text())
if (!id || !emoji) return new Response('not ok', {status: 500})
const key = `${id}:${emoji}`
// https://developers.cloudflare.com/workers/runtime-apis/kv/
const currentCount = Number(await(NAMESPACE.get(key)) || 0)
await NAMESPACE.put(key, currentCount + 1)
return new Response('ok')
}
function ensureEmoji(emoji) {
const segments = Array.from(new Intl.Segmenter({ granularity: 'grapheme' }).segment(emoji.trim()))
const parsedEmoji = segments.length > 0 ? segments[0].segment : null
if (/\p{Emoji}/u.test(parsedEmoji)) return parsedEmoji
}
@brockbnntt
Copy link
Copy Markdown

Hi! I updated this here to enable the same Cloudflare worker to also respond to the GET requests that openHeart.getCount() sends out. Sorry if it's terrible, it's literally the first thing I've ever written in JS. Also I updated it to ES modules format, because apparently what's what Cloudflare prefers now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment