Skip to content
Open
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: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Main
on:
pull_request:
push:
branches: [main]
jobs:
Expand All @@ -18,7 +19,11 @@ jobs:
- name: Typecheck
run: pnpm check:types

- name: Test
run: pnpm test

deploy:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: check
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -51,3 +56,6 @@ jobs:
with:
command: deploy
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}

- name: Verify deployed Markdown
run: pnpm check:markdown --url https://tips.sh --manifest .artifacts/markdown-manifest.json
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ todos.json
!.env.example
.env
public/robots.txt
worker-configuration.d.ts
worker-configuration.d.ts
.artifacts
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "tips",
"private": true,
"type": "module",
"packageManager": "pnpm@10.33.0",
"imports": {
"#/*": "./src/*"
},
"scripts": {
"build": "vp build",
"check": "vp check --fix",
"check:types": "tsc --noEmit",
"check:markdown": "node --import=tsx scripts/check-markdown.ts",
"db:init": "wrangler d1 migrations apply tips --local && pnpm sync",
"deploy": "npm run build && wrangler deploy",
"dev": "vite dev --port 3000",
Expand All @@ -18,7 +18,7 @@
"prebuild": "node --import=tsx scripts/gen-robots.ts",
"preview": "npm run build && vp preview",
"sync": "node --env-file-if-exists=.env --import=tsx scripts/sync.ts",
"test": "vp test run"
"test": "vp test run --config vitest.config.ts"
},
"dependencies": {
"@cloudflare/puppeteer": "^1.0.6",
Expand All @@ -41,10 +41,12 @@
"rehype-katex": "^7.0.1",
"rehype-raw": "^7.0.0",
"rehype-stringify": "^10.0.1",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.1",
"remark-math": "^6.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.2",
"remark-stringify": "^11.0.0",
"shiki": "^4.0.2",
"tailwindcss": "^4.1.18",
"takumi-js": "^1.0.0-rc.16",
Expand All @@ -67,5 +69,6 @@
"vite-plus": "0.1.16",
"vite-tsconfig-paths": "^5.1.4",
"wrangler": "^4.81.1"
}
},
"packageManager": "pnpm@10.33.0"
}
55 changes: 55 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions scripts/check-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** Verify a deployment against a locally generated, trusted inventory. */
import { readFileSync } from 'node:fs'
import { parseArgs } from 'node:util'
import { hash, type manifest } from '../src/lib/MarkdownExport'

const { values } = parseArgs({
options: {
url: { type: 'string' },
manifest: { type: 'string' },
},
})
if (!values.url || !values.manifest)
throw new Error('Usage: pnpm check:markdown --url <origin> --manifest <local manifest>')
const origin = new URL(values.url)
const expected = JSON.parse(readFileSync(values.manifest, 'utf8')) as Awaited<
ReturnType<typeof manifest>
>
if (!Array.isArray(expected) || !expected.length)
throw new Error('Expected a nonempty local TIP manifest')

async function read(path: string, headers: HeadersInit = {}) {
const response = await fetch(new URL(path, origin), {
headers,
signal: AbortSignal.timeout(30_000),
})
if (response.status !== 200) throw new Error(`${path}: HTTP ${response.status}`)
if (!response.headers.get('content-type')?.includes('text/markdown'))
throw new Error(`${path}: expected text/markdown`)
const content = await response.text()
if (/^\s*(?:<!doctype html|<html[\s>])/i.test(content)) throw new Error(`${path}: HTML response`)
return content
}

const indexes = await Promise.all(['/llms.txt', '/index.md'].map((path) => read(path)))
// Bound requests rather than starting one connection per TIP.
for (let offset = 0; offset < expected.length; offset += 5) {
await Promise.all(
expected.slice(offset, offset + 5).map(async (entry) => {
for (const index of indexes) {
if (!index.includes(`https://tips.sh${entry.path})`))
throw new Error(`${entry.id}: missing from index`)
}
const explicit = await read(entry.path, { 'User-Agent': 'Mozilla/5.0', Accept: 'text/html' })
const negotiated = await read(`/${encodeURIComponent(entry.id)}`, {
Accept: 'text/markdown',
'User-Agent': 'Mozilla/5.0',
})
if ((await hash(explicit)) !== entry.sha256 || (await hash(negotiated)) !== entry.sha256) {
throw new Error(`${entry.id}: deployed content differs from the expected sync`)
}
}),
)
}
console.log(
`Verified ${expected.length} TIPs: explicit and negotiated Markdown, hashes, and discovery`,
)
11 changes: 11 additions & 0 deletions scripts/sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Standalone sync script — run with: npx tsx scripts/sync.ts */

import * as Sync from '../src/lib/Sync'
import * as MarkdownExport from '../src/lib/MarkdownExport'

async function main() {
const token = process.env.GITHUB_TOKEN
Expand Down Expand Up @@ -50,6 +51,16 @@ async function main() {
stdio: 'inherit',
})

// Keep the expected hashes outside the deployment: a stale remote manifest
// must not be able to validate a stale deployment.
const { mkdirSync } = await import('fs')
mkdirSync('.artifacts', { recursive: true })
const inventory = allTips.map((tip) => ({ ...tip, pr_json: tip.prJson }))
writeFileSync(
'.artifacts/markdown-manifest.json',
JSON.stringify(await MarkdownExport.manifest(inventory), null, 2),
)

console.log('\n✅ Sync complete!')
}

Expand Down
Loading
Loading