安装

shell
npm install -g profile-graphics

PNG 和 WebP 输出使用 sharp,这是一个可选依赖,npm 在受支持的平台上会默认安装。没有它时,请使用 SVG。

命令

命令 何时使用 示例
init 你在配置仓库,希望自动写好 profile-graphics.yml 和 GitHub Actions 或 GitLab CI 工作流。 profile-graphics init --user octocat
generate 你想用提供方的真实数据生成卡片。gen 是简短别名。 profile-graphics generate --user octocat --charts streak,langs
demo 你想在没有令牌的情况下试用卡片和主题;默认渲染所有卡片。 profile-graphics demo --theme tokyo-night
preview 你想得到卡片以及并排展示它们的 HTML 页面。加上 --watch 后,配置文件一变化就重新渲染,打开的页面每两秒刷新一次。 profile-graphics preview --demo --watch
validate 你想在 CI 运行前检查 YAML。 profile-graphics validate --config profile-graphics.yml
doctor 你想检查配置和每个来源的令牌,包括它是否是受限的 Actions 令牌。 profile-graphics doctor --config profile-graphics.yml
themes 你想查看预设列表。 profile-graphics themes --json
schema 你想知道 JSON schema 的路径。 profile-graphics schema
migrate 你正在从 github-readme-stats 或类似服务迁移。 profile-graphics migrate --readme README.md
serve 你想要实时图片 URL,而不是提交到仓库的文件。 profile-graphics serve --port 3000 --allow octocat
studio 你想在本地浏览器应用中通过拖放构建 README 和配置,并基于真实数据预览。参见工作室。 profile-graphics studio --demo

generate 接受所有选项作为参数,详见 CLI 参数和 Action 输入;demo 接受除提供方设置以外的全部选项。两者都会读取当前文件夹中的 profile-graphics.yml(如果存在),或 --config 指定的文件。

退出码

代码 含义
0 成功。
1 配置或校验错误。
2 提供方或 API 错误。
3 输出写入错误。

自托管图片端点

profile-graphics serve 按请求在 /<provider>/<user>/<card>.<svg|png|webp> 渲染卡片,支持 github、gitlab 和 forgejo。/<provider>/<user>/dashboard.svg?charts=… 返回仪表盘,/<provider>/<user>.json 为 <profile-graphics> Web 组件返回统计数据。其他选项以 Action 输入名放在查询字符串中,例如 ?theme=dracula&locale=de&calendar_mode=rolling。在单卡片路由上,custom_title 设置卡片标题,与 github-readme-stats 相同。统计和图片在内存中保留 --cache 秒,并带有相应的 Cache-Control 头。除非 --host 另行指定,服务器监听 127.0.0.1。

shell
GITHUB_TOKEN=github_pat_... profile-graphics serve --host 0.0.0.0 --allow octocat --rate-limit 60/60
curl "http://localhost:3000/github/octocat/streak.svg?theme=dracula&locale=de"
curl "http://localhost:3000/github/octocat/dashboard.svg?charts=streak,summary,langs"

--rate-limit 60/60 允许每个客户端每 60 秒发送 60 次请求;Node.js 服务器按套接字地址识别客户端。超出的请求会收到带 Retry-After 头的 HTTP 429,格式错误的值会让命令以退出码 1 终止。

profile-graphics/server 导出同一个处理函数 createCardHandler,它是从 Request 到 Response 的函数,因此可在 Bun、Deno 以及任何接受 Fetch API 处理函数的主机上运行。在那里,rateLimit 通过 x-forwarded-for 中的第一个地址或你自己的 clientKey 函数识别客户端;没有这些信息的请求不受限制。PNG 和 WebP 需要 sharp;在没有原生模块的环境中请使用 SVG。

bun.ts
import { createCardHandler } from "profile-graphics/server";

const handler = createCardHandler({
  allowedUsers: ["octocat"],
  cacheSeconds: 3600,
  rateLimit: { requests: 60, windowSeconds: 60 }
});

Bun.serve({ port: 3000, fetch: handler });
deno.ts
import { createCardHandler } from "npm:profile-graphics/server";

Deno.serve(createCardHandler({ allowedUsers: ["octocat"] }));

容器环境下,在 Node.js 镜像中安装 CLI,并以环境变量传入提供方令牌。

Dockerfile
FROM node:22-slim
RUN npm install -g profile-graphics
USER node
EXPOSE 3000
CMD ["profile-graphics", "serve", "--host", "0.0.0.0", "--port", "3000"]
shell
docker build -t profile-graphics .
docker run -p 3000:3000 -e GITHUB_TOKEN=github_pat_... profile-graphics
服务器从环境中读取 GITHUB_TOKEN、GITLAB_TOKEN 和 FORGEJO_TOKEN,每个未命中缓存的请求都会消耗它们的限额;GitHub 路由需要 GITHUB_TOKEN。在公共主机上请设置 --allow 或 allowedUsers,以及 --rate-limit 或 rateLimit。

统计主页访问量

views 卡片在服务器上计数:每次 GET /<provider>/<user>/views.svg(或 .png、.webp)都会计入一次访问并返回新的计数。HEAD 请求和 /<provider>/<user>/views.json 只读取计数;JSON 包含原始 total 和最近 14 天的 days。该路由从不调用提供方,因此不需要令牌。把图片粘贴到你的 README:

README.md
![Profile views](https://cards.example.com/github/octocat/views.svg)

GitHub 通过 camo 代理显示 README 图片,并会缓存它们。计数器响应带有 Cache-Control: no-cache, no-store, max-age=0,所以每次页面浏览 camo 都会重新请求服务器。因此计数是页面加载次数,而不是独立访客数:GitHub 不会透露谁在浏览。

计数保存在存储中。默认是内存,每次重启都从零开始。--counter-file data/views.json 把计数保存在 JSON 文件中。在 Vercel 上设置 UPSTASH_REDIS_REST_URL 和 UPSTASH_REDIS_REST_TOKEN(或 Vercel 市场集成创建的 KV_REST_API_URL 和 KV_REST_API_TOKEN),计数就会写入 Upstash Redis;否则每个函数实例各自计数,并且会丢失。

shell
profile-graphics serve --host 0.0.0.0 --allow octocat --counter-file data/views.json
curl "http://localhost:3000/github/octocat/views.svg?views_style=card&theme=dracula"
curl "http://localhost:3000/github/octocat/views.json"

views_style、views_label、views_start 以及所有显示选项都放在查询字符串中,例如 ?views_style=card&theme=dracula。

--allow 和 --rate-limit 在这里同样适用:其他用户会收到 HTTP 403,超出限额的客户端仍会得到显示当前计数的图片,但其请求不会被计入。在公共主机上请使用 --allow,以免有人用其他名字塞满存储。

令牌安全

通过环境变量或 CI 密钥传递令牌。切勿把令牌提交到 profile-graphics.yml。CLI 错误信息会遮蔽经典 GitHub 令牌(ghp_)和 GitLab 令牌(glpat-)。
shell
profile-graphics generate \
  --provider github \
  --user octocat \
  --token "$GITHUB_TOKEN" \
  --output dist

可重现构建

把 SOURCE_DATE_EPOCH 设为以秒为单位的 Unix 时间戳,generate、demo 和 preview 就会把它当作当前时间:日期窗口、连续贡献和“今天”都按那个时刻计算,因此相同的数据总能得到相同的文件。未设置时使用系统时钟。