Skip to content

gitflarePublicPartly agent-ready50

cf-r2-drop

Drag-and-drop file sharing on Cloudflare R2: multipart uploads, expiring share links, previews, download counts, lifecycle expiry.

main
Serve the share page from the extensionless asset path0e4544f
TypeScript54 lines2.3 KB
import { describe, it, expect } from "vitest";// @ts-ignore plain JS modulesimport { planParts, Uploader, withRetry, fmtBytes, fmtCountdown } from "../public/upload.js";// @ts-ignoreimport { encode, toSvg } from "../public/qr.js"; const MB = 1024 * 1024;const ok = (b: unknown) => new Response(JSON.stringify(b), { status: 200 });const file = (size: number) => ({ name: "a.bin", size, type: "", slice: () => new Blob([]) }); describe("ui lib", () => {  it("plans parts", () => {    const p = planParts(17 * MB);    expect(p.map((x: any) => x.end - x.start)).toEqual([8 * MB, 8 * MB, MB]);  });  it("formats", () => {    expect(fmtBytes(1500)).toBe("1.50 KB");    expect(fmtCountdown(3_700_000)).toBe("1h 1m");  });  it("retries 3 times then fails", async () => {    let n = 0;    await expect(withRetry(async () => { n++; throw new Error("x"); }, { wait: async () => {} })).rejects.toThrow();    expect(n).toBe(4);  });  it("uploads multipart with parts and complete", async () => {    const calls: string[] = []; let fail = true;    const f: any = async (url: string, init: any = {}) => {      calls.push(`${init.method} ${url}`);      if (url === "/api/uploads") return ok({ id: "u1", multipart: true, partSize: 8 * MB });      if (url.endsWith("/parts/2") && fail) { fail = false; return new Response("", { status: 500 }); }      if (url.includes("/parts/")) return ok({ etag: "e" + url.split("/").pop() });      return ok({});    };    const u = new Uploader(file(20 * MB) as any, { fetch: f, wait: async () => {} });    const r = await u.run();    expect(r.id).toBe("u1");    expect(calls.filter((c) => c.includes("/parts/")).length).toBe(4);    expect(calls.at(-1)).toBe("POST /api/uploads/u1/complete");  });  it("small file uses single PUT", async () => {    const calls: string[] = [];    const f: any = async (url: string, init: any = {}) => { calls.push(`${init.method} ${url.split("?")[0]}`); return ok({ id: "s" }); };    await new Uploader(file(1000) as any, { fetch: f }).run();    expect(calls).toEqual(["POST /api/uploads", "PUT /api/uploads/s"]);  });  it("encodes qr", () => {    const m = encode("https://x.dev/d/abc");    expect(m.length).toBe(25);    expect(m[0].slice(0, 7).every(Boolean)).toBe(true);    expect(toSvg("hi")).toContain("<svg");    expect(encode("a".repeat(150)).length).toBeGreaterThan(40);  });});