Backend for TrackSense.fm: Cloud Functions (v2, TypeScript), plus the Realtime Database and Storage security rules. The iOS app, web app and website all use the same Firebase project, resonate-ra.
functions/
src/index.ts # Every Cloud Function lives in this one file
lib/ # Compiled output (generated, don't edit)
database.rules.json # Realtime Database rules
storage.rules # Cloud Storage rules
firebase.json # Deploy config (functions predeploy runs lint + build)
test-command.sh # Sends a transport command to a user's devices
| Function | Type | Auth | Purpose |
|---|---|---|---|
getMusicToken |
HTTP | ID token | Signs an ES256 Apple Music developer token (7-day expiry) |
getCredits |
HTTP | ID token | Song credits (writers, producers, featured artists) from the Genius API |
getArtwork |
HTTP | none | Album artwork URL from the iTunes Search API, plus a dominant color computed with sharp |
validateShareCode |
HTTP | ID token | Resolves a 6-character share code to its owner and writes a 24-hour guest grant. Rate-limited to 10 requests/min per IP |
createShareCode |
Callable | signed in | Issues a new share code, replaces the old one and clears all guest grants |
generateAuthCode |
Callable | signed in | Creates a one-hour, 8-character code (stored hashed) for signing in to the web app |
validateAuthCode |
HTTP | none | Exchanges an auth code for a Firebase custom token. Rate-limited per IP |
claimUsername |
Callable | signed in | Claims or clears a unique username atomically |
pruneStaleDevices |
Scheduled | n/a | Every 24 hours, removes devices not seen for 30 days (keeps the active one) |
"ID token" means the request must send Authorization: Bearer <Firebase ID token>. Anonymous users count as signed in. HTTP functions allow CORS from any origin.
Realtime Database, under users/{uid}/:
| Path | Contents | Guest access |
|---|---|---|
session |
The current queue | read |
playback |
Playback state and position | read |
command |
Transport commands for the active device | write |
devices/{deviceId} |
Device presence | read; manage web devices only |
guests/{guestUID} |
Time-limited grants written by validateShareCode |
none |
authCodes/ holds hashed web sign-in codes and is only touched by functions.
Firestore holds the shareCodes collection (keyed by code, with ownerUID and permissions) and a shareCode field on each user document.
Storage allows a user to upload users/{uid}/profile/avatar.jpg (images under 5 MB), readable by any signed-in user. Everything else is denied.
Requires Node.js 24 and the Firebase CLI, signed in to an account with access to resonate-ra.
cd functions
npm installThe functions read these secrets from Firebase Secret Manager:
| Secret | Used by |
|---|---|
APPLE_PRIVATE_KEY, APPLE_KEY_ID, APPLE_TEAM_ID |
getMusicToken |
GENIUS_ACCESS_TOKEN |
getCredits |
Set one with firebase functions:secrets:set <NAME>.
Run from functions/:
npm run build # Compile src/ → lib/
npm run build:watch # Compile on change
npm run lint # ESLint
npm run serve # Build + start the Functions emulator
npm run shell # Build + interactive functions shell
npm run logs # Stream function logs
npm run deploy # Deploy all functions (lint + build run first)Prefer deploying only what changed:
firebase deploy --only functions:getArtwork
firebase deploy --only database
firebase deploy --only storagenpm run deploy deploys every function at once, so only use it when all of them are ready to ship.
test-command.sh writes a command to users/{uid}/command, which the active device picks up:
./test-command.sh <uid> pause
./test-command.sh <uid> skipTo 3Actions: play, pause, skipNext, skipPrevious, toggleShuffle, cycleRepeat, and skipTo / requeue with an index.
See LICENSE.