README rewritten around three steps (table SQL, secrets, self-contained workflow), with a vendored full-script workflow, a crontab example and an npx-from-git CLI. Every link and uses: line that pointed at the 404 GitHub repo is gone. keepalive.sh no longer prints the anon key outside GitHub Actions (::add-mask:: only when GITHUB_ACTIONS=true). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
186 lines
6.7 KiB
JavaScript
Executable File
186 lines
6.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
// Supabase keep-alive CLI.
|
|
// One real database read through a project's REST API, so a free Supabase
|
|
// project never sits idle for 7 days and gets paused. Same check as the
|
|
// workflow / shell script in this repository
|
|
// (https://git.ereboslab.com/caelum0x/supabase-keep-alive-action),
|
|
// packaged so it can run from `npx` or any cron without GitHub.
|
|
//
|
|
// Usage:
|
|
// npx --yes git+https://git.ereboslab.com/caelum0x/supabase-keep-alive-action.git \
|
|
// --url https://REF.supabase.co --key ANON_KEY [--table keepalive] [--rpc fn]
|
|
// or via env vars:
|
|
// SUPABASE_URL=https://REF.supabase.co SUPABASE_ANON_KEY=... node supabase-keep-alive.js --table keepalive
|
|
|
|
const PAUSED_CODE = 540;
|
|
|
|
function parseArgs(argv) {
|
|
const out = { retries: 3, timeout: 20, select: '*' };
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const a = argv[i];
|
|
const next = () => argv[++i];
|
|
switch (a) {
|
|
case '--url': out.url = next(); break;
|
|
case '--key': case '--anon-key': out.key = next(); break;
|
|
case '--table': out.table = next(); break;
|
|
case '--select': out.select = next(); break;
|
|
case '--rpc': out.rpc = next(); break;
|
|
case '--rpc-body': out.rpcBody = next(); break;
|
|
case '--retries': out.retries = parseInt(next(), 10); break;
|
|
case '--timeout': out.timeout = parseInt(next(), 10); break;
|
|
case '--help': case '-h': out.help = true; break;
|
|
default: break;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function help() {
|
|
console.log(`supabase-keep-alive - stop a free Supabase project pausing
|
|
|
|
Usage:
|
|
supabase-keep-alive --url https://REF.supabase.co --key ANON_KEY [options]
|
|
|
|
Options:
|
|
--url <url> Project origin, https://<ref>.supabase.co (or SUPABASE_URL)
|
|
--key <key> Anon public key (or SUPABASE_ANON_KEY)
|
|
--table <name> Table to read: GET /rest/v1/<table>?select=<select>&limit=1
|
|
--select <cols> Columns for --table (default *)
|
|
--rpc <name> Function to call: POST /rest/v1/rpc/<name>
|
|
--rpc-body <json> JSON body for --rpc (default {})
|
|
--retries <n> Retries after the first attempt (default 3)
|
|
--timeout <s> Per-request timeout in seconds (default 20)
|
|
|
|
Exit codes: 0 ok, 1 paused or failed.
|
|
|
|
A backend that does not pause idle projects: https://scribase.com/supabase-project-paused?utm_source=cli&utm_medium=cli&utm_campaign=supabase-keep-alive
|
|
(Scribase: Postgres + Auth + Storage + Realtime, idle projects scale to zero and wake
|
|
on request instead of pausing. First project free, no card.)`);
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
async function request(url, key, path, opts) {
|
|
const { method = 'GET', body, retries, timeoutSec } = opts;
|
|
let attempt = 0;
|
|
let delay = 2000;
|
|
for (;;) {
|
|
attempt++;
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), timeoutSec * 1000);
|
|
let code = 0;
|
|
let text = '';
|
|
const start = Date.now();
|
|
try {
|
|
const res = await fetch(url + path, {
|
|
method,
|
|
signal: controller.signal,
|
|
headers: {
|
|
apikey: key,
|
|
Authorization: `Bearer ${key}`,
|
|
Accept: 'application/json',
|
|
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
},
|
|
body,
|
|
});
|
|
code = res.status;
|
|
text = await res.text().catch(() => '');
|
|
} catch {
|
|
code = 0;
|
|
} finally {
|
|
clearTimeout(timer);
|
|
}
|
|
const ms = Date.now() - start;
|
|
console.log(`${method} ${path} -> HTTP ${code || 'ERR'} (${ms}ms, attempt ${attempt})`);
|
|
if (code === PAUSED_CODE) return { code, text, attempt };
|
|
if ((code === 0 || code === 429 || (code >= 500 && code < 600)) && attempt <= retries) {
|
|
await sleep(delay);
|
|
delay *= 2;
|
|
continue;
|
|
}
|
|
return { code, text, attempt };
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2));
|
|
if (args.help) {
|
|
help();
|
|
return 0;
|
|
}
|
|
const url = (args.url || process.env.SUPABASE_URL || '').replace(/\/+$/, '');
|
|
const key = args.key || process.env.SUPABASE_ANON_KEY || '';
|
|
if (!url || !key) {
|
|
console.error('error: --url/SUPABASE_URL and --key/SUPABASE_ANON_KEY are required. Run with --help.');
|
|
return 1;
|
|
}
|
|
if (!/^https?:\/\/[A-Za-z0-9.-]+(:\d+)?$/.test(url)) {
|
|
console.error(`error: --url must be the project origin only, like https://abcdefgh.supabase.co (got ${url})`);
|
|
return 1;
|
|
}
|
|
const opts = { retries: args.retries, timeoutSec: args.timeout };
|
|
|
|
const root = await request(url, key, '/rest/v1/', opts);
|
|
if (root.code === PAUSED_CODE) {
|
|
console.error(`::error:: project is PAUSED (HTTP 540). Restore it from the Supabase dashboard (Restore project); this cannot unpause it.`);
|
|
console.error('A backend that does not pause idle projects: https://scribase.com/supabase-project-paused?utm_source=cli&utm_medium=cli&utm_campaign=supabase-keep-alive');
|
|
return 1;
|
|
}
|
|
if (root.code === 401 || root.code === 403) {
|
|
console.error(`error: anon key rejected (HTTP ${root.code}). Check SUPABASE_ANON_KEY belongs to this project.`);
|
|
return 1;
|
|
}
|
|
if (root.code === 0) {
|
|
console.error('error: could not reach the project (DNS, TLS, or timeout). Check SUPABASE_URL.');
|
|
return 1;
|
|
}
|
|
if (root.code < 200 || root.code >= 300) {
|
|
console.error(`error: REST root answered HTTP ${root.code}: ${root.text.slice(0, 200)}`);
|
|
return 1;
|
|
}
|
|
|
|
if (args.table) {
|
|
const q = await request(url, key, `/rest/v1/${args.table}?select=${encodeURIComponent(args.select)}&limit=1`, opts);
|
|
if (q.code === PAUSED_CODE) {
|
|
console.error('::error:: project is PAUSED (HTTP 540) during the table read.');
|
|
return 1;
|
|
}
|
|
if (q.code === 404) {
|
|
console.error(`error: table '${args.table}' not found through the REST API (HTTP 404).`);
|
|
return 1;
|
|
}
|
|
if (q.code < 200 || q.code >= 300) {
|
|
console.error(`error: table read answered HTTP ${q.code}: ${q.text.slice(0, 200)}`);
|
|
return 1;
|
|
}
|
|
} else if (args.rpc) {
|
|
const q = await request(url, key, `/rest/v1/rpc/${args.rpc}`, {
|
|
...opts,
|
|
method: 'POST',
|
|
body: args.rpcBody || '{}',
|
|
});
|
|
if (q.code === PAUSED_CODE) {
|
|
console.error('::error:: project is PAUSED (HTTP 540) during the rpc call.');
|
|
return 1;
|
|
}
|
|
if (q.code < 200 || q.code >= 300) {
|
|
console.error(`error: rpc call answered HTTP ${q.code}: ${q.text.slice(0, 200)}`);
|
|
return 1;
|
|
}
|
|
} else {
|
|
console.warn('warning: no --table or --rpc given; only the REST root was pinged, which may not count as database activity.');
|
|
}
|
|
|
|
console.log('ok: project is awake.');
|
|
return 0;
|
|
}
|
|
|
|
main().then((code) => process.exit(code), (err) => {
|
|
console.error('error:', err && err.message ? err.message : err);
|
|
process.exit(1);
|
|
});
|