Files
magpie/site/r2stub.mjs
T
yetone 3fc6ee4111 site: release files from Cloudflare R2, GitHub behind them
Updates and the installer downloaded every file from github.com, which
answered 504 for every release file for hours on 2026-11-08. The site now
keeps a copy on R2 (bucket magpie-releases, binding RELEASES):

- A five-minute cron copies the newest release and the Stable one from
  github.com (or, when that fails, GitHub's API) into R2. R2's put takes a
  file only with the SHA-256 the release's SHA256SUMS gives. A run starts
  no copy after 4 minutes; the feeds' tags and the 4 newest besides are
  kept, older ones deleted.
- /files/<tag>/<file> serves R2's copy, Range and conditional asks
  included; a file R2 doesn't hold, or a failing R2, is a 302 to
  github.com.
- /api/latest (both channels) and /download name /files only for a file
  R2 holds under the release's hash, and give every file's github.com
  address as `github`, which the previous commit's clients fall back to.
  Older clients only mirror a github.com address, so a mirror never goes
  in front of the site's.
- start (en, zh, ja), privacy (en, zh, ja) and reference.md say where
  files come from.

Run (mote): node --test site/*.test.mjs 104/104, with files.test.mjs
(R2 stood in for by r2stub.mjs) and new worker tests for the feed,
/download, /files and the cron. wrangler deploy --dry-run binds RELEASES.

Broken by hand: onR2 taking any held file regardless of hash, two tests
red (the feed test and "bytes R2 holds under another hash").

Not tried yet: the real cron run and R2 serving (after deploy).
2026-11-09 05:48:23 +08:00

92 lines
3.7 KiB
JavaScript

// An R2 bucket binding for the tests: what files.js asks of R2 (get with
// range and onlyIf, head, put with sha256, list with prefix, delimiter
// and include, delete), kept in memory. put refuses a body whose SHA-256
// isn't the one given, as R2 does. fail makes every call throw.
import { createHash } from "node:crypto";
export function bucket() {
const objs = new Map();
const b = {
objs,
fail: false,
puts: 0,
check() {
if (b.fail) throw new Error("R2 is down");
},
view(key, o, body, range) {
return {
key,
size: o.bytes.length,
httpEtag: `"${o.sha256.slice(0, 8)}"`,
customMetadata: o.customMetadata,
writeHttpMetadata(h) {
if (o.httpMetadata.contentType) h.set("Content-Type", o.httpMetadata.contentType);
if (o.httpMetadata.contentDisposition) h.set("Content-Disposition", o.httpMetadata.contentDisposition);
},
...(body === undefined ? {} : { body, range }),
};
},
async head(key) {
b.check();
const o = objs.get(key);
return o ? b.view(key, o) : null;
},
async get(key, opts = {}) {
b.check();
const o = objs.get(key);
if (!o) return null;
const h = opts.onlyIf instanceof Headers ? opts.onlyIf : new Headers();
if (h.get("If-None-Match") === `"${o.sha256.slice(0, 8)}"`) return b.view(key, o);
const rh = opts.range instanceof Headers ? opts.range.get("Range") : null;
if (!rh) return b.view(key, o, new Blob([o.bytes]).stream(), undefined);
const m = rh.match(/^bytes=(\d*)-(\d*)$/);
if (!m || (m[1] === "" && m[2] === "")) throw new Error("bad range");
let range, start, end;
if (m[1] === "") {
range = { suffix: +m[2] };
start = Math.max(0, o.bytes.length - +m[2]);
end = o.bytes.length;
} else {
start = +m[1];
if (start >= o.bytes.length) throw new Error("range not satisfiable");
end = m[2] === "" ? o.bytes.length : Math.min(o.bytes.length, +m[2] + 1);
range = { offset: start, length: end - start };
}
return b.view(key, o, new Blob([o.bytes.slice(start, end)]).stream(), range);
},
async put(key, body, opts = {}) {
b.check();
b.puts++;
const bytes = new Uint8Array(await new Response(body).arrayBuffer());
const sha256 = createHash("sha256").update(bytes).digest("hex");
if (opts.sha256 && opts.sha256 !== sha256) throw new Error(`put: The SHA-256 checksum you specified did not match what we received`);
objs.set(key, { bytes, sha256, httpMetadata: opts.httpMetadata || {}, customMetadata: opts.customMetadata || {} });
return b.view(key, objs.get(key));
},
async list(opts = {}) {
b.check();
const prefix = opts.prefix || "";
const keys = [...objs.keys()].filter((k) => k.startsWith(prefix)).sort();
if (opts.delimiter) {
const pre = new Set(keys.map((k) => k.slice(prefix.length)).filter((k) => k.includes(opts.delimiter)).map((k) => prefix + k.split(opts.delimiter)[0] + opts.delimiter));
return { objects: [], delimitedPrefixes: [...pre], truncated: false };
}
return { objects: keys.map((k) => b.view(k, objs.get(k))), delimitedPrefixes: [], truncated: false };
},
async delete(keys) {
b.check();
for (const k of [].concat(keys)) objs.delete(k);
},
};
return b;
}
export function sha(s) {
return createHash("sha256").update(s).digest("hex");
}
// hold puts body at tag/name, as the cron would have
export async function hold(b, tag, name, body) {
await b.put(`${tag}/${name}`, body, { sha256: sha(body), httpMetadata: { contentType: "application/octet-stream" }, customMetadata: { sha256: sha(body) } });
}