Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
</a>

<p align="center">
<a href="https://devstream.jamesconway.dev"><img src="https://img.shields.io/endpoint?url=https://devstream.jamesconway.dev/badge.json&cacheSeconds=3600" alt="Repos listed on DevStream"></a>
<a href="https://devstream.jamesconway.dev"><img src="https://devstream.jamesconway.dev/badge.svg" alt="Repos listed on DevStream"></a>
<a href="https://github.com/devstreamhq/devstream/actions/workflows/pr-checks.yml"><img src="https://github.com/devstreamhq/devstream/actions/workflows/pr-checks.yml/badge.svg" alt="PR checks"></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/license-CC0-blue" alt="License: CC0"></a>
<a href="https://github.com/devstreamhq/devstream/discussions"><img src="https://img.shields.io/github/discussions/devstreamhq/devstream" alt="GitHub Discussions"></a>
<a href="https://devstream.jamesconway.dev"><img src="https://img.shields.io/website?url=https%3A%2F%2Fdevstream.jamesconway.dev" alt="Website"></a>
<a href="https://scorecard.dev/viewer/?uri=github.com/devstreamhq/devstream"><img src="https://img.shields.io/ossf-scorecard/github.com/devstreamhq/devstream?label=openssf%20scorecard" alt="OpenSSF Scorecard"></a>
<a href="https://github.com/devstreamhq/devstream/discussions"><img src="https://img.shields.io/badge/discussions-join%20in-blue?logo=github" alt="GitHub Discussions"></a>
<a href="https://devstream.jamesconway.dev"><img src="https://img.shields.io/badge/site-devstream.jamesconway.dev-c4155f" alt="Website"></a>
<a href="https://scorecard.dev/viewer/?uri=github.com/devstreamhq/devstream"><img src="https://devstream.jamesconway.dev/scorecard.svg" alt="OpenSSF Scorecard"></a>
</p>

Awesome lists rot. That link someone merged in 2019 is still sitting there, long after the repo's last commit, and the star count beside it only tells you who got excited once. DevStream checks a repo before it lands and again every week. Then it ranks what survives by Likes, one per account.
Expand Down
18 changes: 17 additions & 1 deletion site/src/lib/embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const W = 720;
const ROW = 44;
const TOP = 76;

const esc = (v: unknown) =>
export const esc = (v: unknown) =>
String(v)
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
Expand Down Expand Up @@ -84,3 +84,19 @@ export const badge = (live: SiteEntry[]) => ({
message: `${live.length} ${live.length === 1 ? "repo" : "repos"}`,
color: BRAND_HEX,
});

// shortcut: widths are estimated at 6.6px a character, not measured; fine for short ASCII labels.
export function flatBadge(label: string, message: string, color: string): string {
const lw = Math.round(label.length * 6.6 + 10);
const rw = Math.round(message.length * 6.6 + 10);
const w = lw + rw;
const t = (x: number, s: string) =>
`<text x="${x}" y="14" fill="#fff" text-anchor="middle">${esc(s)}</text>`;
return (
`<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="20" role="img" aria-label="${esc(label)}: ${esc(message)}">` +
`<title>${esc(label)}: ${esc(message)}</title>` +
`<clipPath id="r"><rect width="${w}" height="20" rx="3"/></clipPath>` +
`<g clip-path="url(#r)"><rect width="${lw}" height="20" fill="#555"/><rect x="${lw}" width="${rw}" height="20" fill="${esc(color)}"/></g>` +
`<g font-family="${esc(FONT)}" font-size="11">${t(lw / 2, label)}${t(lw + rw / 2, message)}</g></svg>`
);
}
9 changes: 9 additions & 0 deletions site/src/pages/badge.svg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { loadAll, liveList } from "../lib/data.ts";
import { badge, flatBadge } from "../lib/embeds.ts";

export function GET() {
const b = badge(liveList(loadAll().entries));
return new Response(flatBadge(b.label, b.message, b.color), {
headers: { "Content-Type": "image/svg+xml; charset=utf-8" },
});
}
21 changes: 21 additions & 0 deletions site/src/pages/scorecard.svg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { flatBadge } from "../lib/embeds.ts";

const URL_ = "https://api.scorecard.dev/projects/github.com/devstreamhq/devstream";

export async function GET() {
let msg = "pending";
let color = "#9f9f9f";
try {
const res = await fetch(URL_, { signal: AbortSignal.timeout(10_000) });
const score = (await res.json())?.score;
if (res.ok && typeof score === "number" && Number.isFinite(score)) {
msg = score.toFixed(1);
color = score >= 7 ? "#4c1" : score >= 4 ? "#dfb317" : "#e05d44";
}
} catch {
// Never fail the build over a badge.
}
return new Response(flatBadge("openssf scorecard", msg, color), {
headers: { "Content-Type": "image/svg+xml; charset=utf-8" },
});
}
58 changes: 58 additions & 0 deletions site/tests/badges.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { flatBadge } from "../src/lib/embeds.ts";
import { GET as listed } from "../src/pages/badge.svg.ts";
import { GET as scorecard } from "../src/pages/scorecard.svg.ts";

const realFetch = globalThis.fetch;
const mock = (fn) => (globalThis.fetch = fn);
const json = (body, ok = true) => async () => ({ ok, json: async () => body });
const text = async (r) => r.text();
const checks = (svg) => {
assert.ok(!/<script/i.test(svg), "no script");
assert.ok(!/(href|src)\s*=\s*["']?https?:/i.test(svg), "no external refs");
assert.ok(Buffer.byteLength(svg) < 5 * 1024, "under 5 KB");
};

test("badge.svg shows the count, no script", async () => {
const svg = await text(listed());
assert.match(svg, />\d+ repos?</);
assert.ok(svg.includes(">listed<"));
checks(svg);
});

test("scorecard.svg renders the numeric score", async () => {
mock(json({ score: 7.34 }));
const svg = await text(await scorecard());
assert.ok(svg.includes(">7.3<"));
assert.ok(svg.includes("#4c1"));
checks(svg);
mock(json({ score: 5 }));
assert.ok((await text(await scorecard())).includes("#dfb317"));
mock(json({ score: 2.1 }));
assert.ok((await text(await scorecard())).includes("#e05d44"));
});

test("scorecard.svg says pending on throw or garbage", async () => {
for (const f of [
async () => { throw new Error("down"); },
json({ score: "9" }),
json({ score: NaN }),
json(null),
json({ score: 8 }, false),
]) {
mock(f);
const svg = await text(await scorecard());
assert.ok(svg.includes(">pending<"));
assert.ok(svg.includes("#9f9f9f"));
checks(svg);
}
});

test("both SVGs escape their input", () => {
const svg = flatBadge('<b>"', '<script>&', '"><x');
assert.ok(!svg.includes("<script>") && !svg.includes("<b>") && !svg.includes('"><x'));
assert.ok(svg.includes("&lt;script&gt;&amp;"));
});

test.after(() => { globalThis.fetch = realFetch; });
Loading