|
| 1 | +# Plan: Deno Microservice for Documentation Generation |
| 2 | + |
| 3 | +> **Status**: Recommended approach. Uses official `@deno/doc` with full feature support. |
| 4 | +
|
| 5 | +Deploy a separate Vercel project using `vercel-deno` runtime that exposes a docs generation API. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +``` |
| 10 | +npmx.dev (Nuxt/Node.js) docs-api.npmx.dev (Deno) |
| 11 | +┌─────────────────────┐ ┌─────────────────────┐ |
| 12 | +│ /api/registry/docs │ HTTP │ /api/generate │ |
| 13 | +│ │ │ ──────> │ │ │ |
| 14 | +│ ▼ │ │ ▼ │ |
| 15 | +│ generateDocsWithDeno│ │ @deno/doc │ |
| 16 | +└─────────────────────┘ └─────────────────────┘ |
| 17 | +``` |
| 18 | + |
| 19 | +## Implementation |
| 20 | + |
| 21 | +### Part 1: Deno Microservice |
| 22 | + |
| 23 | +#### Project Structure |
| 24 | + |
| 25 | +``` |
| 26 | +docs-api/ |
| 27 | +├── api/ |
| 28 | +│ └── generate.ts |
| 29 | +├── vercel.json |
| 30 | +└── README.md |
| 31 | +``` |
| 32 | + |
| 33 | +#### `vercel.json` |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "$schema": "https://openapi.vercel.sh/vercel.json", |
| 38 | + "functions": { |
| 39 | + "api/**/*.[jt]s": { |
| 40 | + "runtime": "vercel-deno@3.1.0" |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +#### `api/generate.ts` |
| 47 | + |
| 48 | +```typescript |
| 49 | +#!/usr/bin/env deno run --allow-net --allow-env |
| 50 | + |
| 51 | +import { doc } from "jsr:@deno/doc"; |
| 52 | + |
| 53 | +interface GenerateRequest { |
| 54 | + package: string; |
| 55 | + version: string; |
| 56 | +} |
| 57 | + |
| 58 | +function validateAuth(req: Request): boolean { |
| 59 | + const authHeader = req.headers.get("Authorization"); |
| 60 | + const expectedToken = Deno.env.get("API_SECRET"); |
| 61 | + if (!expectedToken) return true; |
| 62 | + return authHeader === `Bearer ${expectedToken}`; |
| 63 | +} |
| 64 | + |
| 65 | +export default async function handler(req: Request): Promise<Response> { |
| 66 | + const headers = { |
| 67 | + "Access-Control-Allow-Origin": "https://npmx.dev", |
| 68 | + "Access-Control-Allow-Methods": "POST, OPTIONS", |
| 69 | + "Access-Control-Allow-Headers": "Content-Type, Authorization", |
| 70 | + "Content-Type": "application/json", |
| 71 | + }; |
| 72 | + |
| 73 | + if (req.method === "OPTIONS") { |
| 74 | + return new Response(null, { status: 204, headers }); |
| 75 | + } |
| 76 | + |
| 77 | + if (req.method !== "POST") { |
| 78 | + return new Response( |
| 79 | + JSON.stringify({ error: "method_not_allowed" }), |
| 80 | + { status: 405, headers } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (!validateAuth(req)) { |
| 85 | + return new Response( |
| 86 | + JSON.stringify({ error: "unauthorized" }), |
| 87 | + { status: 401, headers } |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + const body: GenerateRequest = await req.json(); |
| 93 | + |
| 94 | + if (!body.package || !body.version) { |
| 95 | + return new Response( |
| 96 | + JSON.stringify({ error: "bad_request" }), |
| 97 | + { status: 400, headers } |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const specifier = `https://esm.sh/${body.package}@${body.version}?target=deno`; |
| 102 | + const nodes = await doc(specifier); |
| 103 | + |
| 104 | + return new Response(JSON.stringify({ nodes }), { status: 200, headers }); |
| 105 | + } catch (error) { |
| 106 | + const message = error instanceof Error ? error.message : "Unknown error"; |
| 107 | + |
| 108 | + if (message.includes("Could not find")) { |
| 109 | + return new Response( |
| 110 | + JSON.stringify({ error: "not_found" }), |
| 111 | + { status: 404, headers } |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return new Response( |
| 116 | + JSON.stringify({ error: "generation_failed", message }), |
| 117 | + { status: 500, headers } |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Part 2: Update Main App |
| 124 | + |
| 125 | +#### Environment Variables |
| 126 | + |
| 127 | +```bash |
| 128 | +DOCS_API_URL=https://docs-api.npmx.dev/api/generate |
| 129 | +DOCS_API_SECRET=your-secret-token |
| 130 | +``` |
| 131 | + |
| 132 | +#### Update `server/utils/docs.ts` |
| 133 | + |
| 134 | +```typescript |
| 135 | +const DOCS_API_URL = process.env.DOCS_API_URL || 'https://docs-api.npmx.dev/api/generate' |
| 136 | +const DOCS_API_SECRET = process.env.DOCS_API_SECRET |
| 137 | + |
| 138 | +async function runDenoDoc(packageName: string, version: string): Promise<DenoDocResult> { |
| 139 | + const headers: Record<string, string> = { |
| 140 | + 'Content-Type': 'application/json', |
| 141 | + } |
| 142 | + |
| 143 | + if (DOCS_API_SECRET) { |
| 144 | + headers['Authorization'] = `Bearer ${DOCS_API_SECRET}` |
| 145 | + } |
| 146 | + |
| 147 | + const response = await fetch(DOCS_API_URL, { |
| 148 | + method: 'POST', |
| 149 | + headers, |
| 150 | + body: JSON.stringify({ package: packageName, version }), |
| 151 | + }) |
| 152 | + |
| 153 | + if (!response.ok) { |
| 154 | + const error = await response.json().catch(() => ({ message: 'Unknown error' })) |
| 155 | + if (response.status === 404) { |
| 156 | + return { nodes: [] } |
| 157 | + } |
| 158 | + throw new Error(`Docs API error: ${error.message}`) |
| 159 | + } |
| 160 | + |
| 161 | + return await response.json() as DenoDocResult |
| 162 | +} |
| 163 | + |
| 164 | +export async function generateDocsWithDeno( |
| 165 | + packageName: string, |
| 166 | + version: string, |
| 167 | +): Promise<DocsGenerationResult | null> { |
| 168 | + const result = await runDenoDoc(packageName, version) |
| 169 | + |
| 170 | + if (!result.nodes || result.nodes.length === 0) { |
| 171 | + return null |
| 172 | + } |
| 173 | + |
| 174 | + // Rest remains the same |
| 175 | + const flattenedNodes = flattenNamespaces(result.nodes) |
| 176 | + const mergedSymbols = mergeOverloads(flattenedNodes) |
| 177 | + const symbolLookup = buildSymbolLookup(flattenedNodes) |
| 178 | + |
| 179 | + const html = await renderDocNodes(mergedSymbols, symbolLookup) |
| 180 | + const toc = renderToc(mergedSymbols) |
| 181 | + |
| 182 | + return { html, toc, nodes: flattenedNodes } |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +#### Remove Unused Code |
| 187 | + |
| 188 | +Delete from `server/utils/docs.ts`: |
| 189 | +- `execFileAsync` import |
| 190 | +- `DENO_DOC_TIMEOUT_MS`, `DENO_DOC_MAX_BUFFER` constants |
| 191 | +- `denoCheckPromise`, `isDenoInstalled()`, `verifyDenoInstalled()` |
| 192 | +- `buildEsmShUrl()` (moved to microservice) |
| 193 | +- Old `runDenoDoc()` implementation |
| 194 | + |
| 195 | +### Local Development |
| 196 | + |
| 197 | +Keep subprocess as fallback for local dev: |
| 198 | + |
| 199 | +```typescript |
| 200 | +async function runDenoDoc(packageName: string, version: string): Promise<DenoDocResult> { |
| 201 | + if (process.dev && await isDenoInstalled()) { |
| 202 | + return runLocalDenoDoc(packageName, version) |
| 203 | + } |
| 204 | + return runRemoteDenoDoc(packageName, version) |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +## Pros/Cons |
| 209 | + |
| 210 | +**Pros**: Uses official `@deno/doc`, exact parity with `deno doc` CLI, actively maintained, clean separation |
| 211 | + |
| 212 | +**Cons**: Two deployments, +100-200ms latency, CORS/auth setup, more complex local dev |
0 commit comments