diff --git a/AGENTS.md b/AGENTS.md index e59cd3e..7c27cae 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -69,16 +69,6 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ **ข้อจำกัดที่ต้องรู้:** การทำ hash กันไม่ให้รหัสโผล่ใน source แต่ไม่ได้กันคนที่เปิด devtools (แก้ JS ให้ `isAuthed()` คืน true ได้เลย) และ verifier ที่ได้ก็ถูกเดารหัส offline ได้ถ้ารหัสอ่อน — การป้องกันจริงคือ `ADMIN_TOKEN` ของ `server/publish.mjs` ซึ่งต้องตั้งทุกครั้งที่เปิดเซิร์ฟเวอร์ให้เครื่องอื่นเข้าถึง -## โปรเจคในเครื่อง (`/projects/*`) - -`server/publish.mjs` สแกนโฟลเดอร์ข้างเคียง (ค่าเริ่มต้น `D:\Git\workspace`) แล้วเปิดหน้า `index.html` ของแต่ละโปรเจคให้คลิกดูได้จากหน้าแรก - -- `GET /api/local-projects` — รายการโฟลเดอร์ที่พบ (เรียงตามเวลาแก้ล่าสุด), `GET /projects/<โฟลเดอร์>/…` — เสิร์ฟไฟล์ -- **รับเฉพาะ loopback** ทั้งสอง endpoint (`isLoopback`) ห้าม loosen เป็น 0.0.0.0 เด็ดขาด เพราะจะเปิดไฟล์ทั้งเครื่องให้ LAN -- เพิ่มโฟลเดอร์เข้า `SKIP_NAMES` เมื่อมีของที่ไม่ควรถูกสแกน; ชื่อ `` ที่เป็นคำลอย ๆ ถูกแทนด้วยชื่อโฟลเดอร์ผ่าน `GENERIC_TITLES` -- `ui/views/home.js` ซ่อนส่วนนี้เมื่อ API ไม่ตอบ (static host) — อย่าทำให้มันแสดงรายการปลอม -- `sw.js` ข้าม `/projects/*` (ไม่แคชไฟล์ของโปรเจคอื่น) - ## ขั้นตอนตรวจก่อนส่งงาน ```bash diff --git a/DESIGN.md b/DESIGN.md index ffd17a2..f0d8974 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -54,8 +54,6 @@ - **ฮีโร่**: พื้นไล่เฉดอุ่นอ่อน + การ์ดลอย 3 ใบ **สุ่มจากโปรเจคทั้งหมดใหม่ทุกครั้งที่โหลดหน้า** (Fisher–Yates, ไม่ซ้ำ) แสดงเฉพาะ ≥1080px, `aria-hidden` - **หลังบ้าน**: พาเนล + การ์ดรายการ, ปุ่มเลื่อนขึ้น/ลง/ลบ, แถบบันทึกแบบ sticky ด้านล่าง, `<dialog>` ยืนยันการลบ/ทิ้งการแก้ไข -- **โปรเจคในเครื่อง (localhost)**: รายการ 2 คอลัมน์ (≥700px) / 1 คอลัมน์ (มือถือ) — ไอคอนโฟลเดอร์ 34px, ชื่อโปรเจค 15px ตัวกลาง, บรรทัดล่างเป็น `<folder> · index.html · แก้ล่าสุด <วันที่ไทย>` (12.5px, โมโนสเปซเฉพาะชื่อโฟลเดอร์), ปุ่มขวา “เปิดหน้าแรก →” (ซ่อนข้อความเมื่อ <560px เหลือลูกศร) · แสดงเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์ในเครื่อง - ## 6. สิ่งที่ห้าม - ห้ามใช้อิโมจิเป็นไอคอน (ใช้ SVG เส้น 1.7px ใน `ui/ui.js`) diff --git a/README.md b/README.md index d3486ce..dcbaff1 100644 --- a/README.md +++ b/README.md @@ -45,25 +45,6 @@ server/publish.mjs ถ้าไม่ได้ตั้ง `ADMIN_TOKEN` เซิร์ฟเวอร์จะอนุญาตเฉพาะเครื่องที่รันมันอยู่ — เปิดให้เครื่องอื่นแก้ต้องตั้งรหัสเสมอ -## โปรเจคในเครื่อง (เห็นเฉพาะ localhost) - -หน้าแรกมีส่วน “โปรเจคในเครื่อง” ที่สแกนโฟลเดอร์ข้างเคียง (`D:\Git\workspace`) แล้วลิงก์ไป **หน้า index.html ของแต่ละโปรเจค** โดยไม่ต้องหาโฟลเดอร์เอง - -``` -GET /api/local-projects → รายการ {folder, entry, url, title, updated_at} -GET /projects/<โฟลเดอร์>/ → เสิร์ฟหน้าแรกของโฟลเดอร์นั้น -``` - -| ตัวแปร | ค่าเริ่มต้น | ความหมาย | -|---|---|---| -| `WORKSPACE_DIR` | โฟลเดอร์แม่ของ repo นี้ (`D:\Git\workspace`) | ที่สแกนหาโปรเจค | -| `WORKSPACE_DIR=none` | — | ปิดฟีเจอร์นี้ | - -- รับเฉพาะ request จาก **เครื่องที่รันเซิร์ฟเวอร์ (loopback)** — เปิดให้เครื่องอื่นดูเว็บได้ แต่ดูไฟล์ในเครื่องไม่ได้ -- บล็อก `node_modules`, `.git`, `.hermes` และกัน path traversal -- โฟลเดอร์ที่ไม่มี `index.html` (เช่น โปรเจคที่ต้องรัน `npm run dev`) จะไม่ถูกใส่ในรายการ -- บนเว็บจริง (static host) API นี้ไม่มี → ส่วนนี้ซ่อนตัวเองอัตโนมัติ - ## เนื้อหาเริ่มต้น (มาจากตารางรายชื่อโปรเจค) | ชื่อโปรเจค | ซับโดเมน | อีเมล | diff --git a/assets/css/home.css b/assets/css/home.css index 7ed4fe2..7727d45 100644 --- a/assets/css/home.css +++ b/assets/css/home.css @@ -400,123 +400,6 @@ cursor: grab; } -/* ------------------------------------------------ โปรเจคในเครื่อง (localhost) */ -.local-section { - margin-top: var(--space-4); -} -.local-section[hidden] { - display: none; -} -.local-list { - list-style: none; - margin: 0; - padding: 0; - display: flex; - flex-direction: column; - gap: 8px; -} -.local-row__link { - display: flex; - align-items: center; - gap: var(--space-3); - min-height: 64px; - padding: 12px var(--space-4); - border: 1px solid var(--border); - border-radius: var(--radius-md); - background: var(--surface); - color: inherit; - text-decoration: none; - transition: - border-color 160ms var(--ease), - background 160ms var(--ease), - translate 160ms var(--ease); -} -.local-row__link:hover { - border-color: var(--brand-300); - background: var(--brand-50); - translate: 0 -1px; - text-decoration: none; -} -.local-row__icon { - flex: none; - display: grid; - place-items: center; - width: 34px; - height: 34px; - border-radius: 10px; - background: var(--brand-50); - border: 1px solid var(--brand-200); - color: var(--brand-700); -} -.local-row__icon svg { - width: 18px; - height: 18px; -} -.local-row__body { - flex: 1 1 auto; - min-width: 0; - display: flex; - flex-direction: column; - gap: 2px; -} -.local-row__title { - font-weight: 500; - color: var(--brand-900); - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} -.local-row__meta { - font-size: 12.5px; - color: var(--muted-foreground); - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} -.local-row__meta code { - font-family: ui-monospace, SFMono-Regular, Menlo, monospace; - font-size: 11.5px; - background: var(--brand-50); - border: 1px solid var(--border); - border-radius: 5px; - padding: 0 5px; -} -.local-row__go { - flex: none; - display: inline-flex; - align-items: center; - gap: 6px; - font-size: 13.5px; - font-weight: 500; - color: var(--brand-700); -} -.local-row__go svg { - width: 16px; - height: 16px; - transition: transform 160ms var(--ease); -} -.local-row__link:hover .local-row__go svg { - transform: translateX(3px); -} -@media (max-width: 560px) { - .local-row__go-text { - display: none; - } -} - -@media (min-width: 700px) { - .local-list { - display: grid; - grid-template-columns: repeat(2, minmax(0, 1fr)); - } -} - -@media (prefers-reduced-motion: reduce) { - .local-row__link:hover { - translate: none; - } -} - /* ------------------------------------------------------------ empty state */ .empty { margin-top: var(--space-4); diff --git a/index.html b/index.html index d0fe671..70ca6d2 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@ content="รายชื่อโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดูได้เลย" /> <meta name="theme-color" content="#8A4722" /> - <meta name="app-version" content="2026.11.19.5" /> + <meta name="app-version" content="2026.11.19.4" /> <link rel="manifest" href="manifest.json" /> <link rel="icon" type="image/png" sizes="192x192" href="icons/icon-192.png" /> <link rel="icon" type="image/png" sizes="64x64" href="icons/favicon-64.png" /> diff --git a/server/publish.mjs b/server/publish.mjs index 24acf30..ce3bdf1 100644 --- a/server/publish.mjs +++ b/server/publish.mjs @@ -4,26 +4,20 @@ * * GET /api/projects อ่านข้อมูลปัจจุบัน * PUT /api/projects เขียน data/projects.json แล้ว commit + push ขึ้น Gitea - * GET /api/local-projects รายชื่อโปรเจคในโฟลเดอร์ข้างเคียง (เครื่องตัวเองเท่านั้น) - * GET /projects/<โฟลเดอร์>/… เสิร์ฟหน้า index.html ของโปรเจคใน D:\Git\workspace (เครื่องตัวเองเท่านั้น) * static เสิร์ฟไฟล์หน้าเว็บจาก root ของโปรเจค * * ใช้งาน: * node server/publish.mjs # http://localhost:8090 * PORT=8090 ADMIN_TOKEN=xxxx node server/publish.mjs * PUBLISH_PUSH=0 node server/publish.mjs # เขียนไฟล์ + commit แต่ไม่ push - * WORKSPACE_DIR=D:/Git/workspace node server/publish.mjs # เปลี่ยนที่สแกนโปรเจคข้างเคียง - * WORKSPACE_DIR=none node server/publish.mjs # ปิดการเสิร์ฟโฟลเดอร์ข้างเคียง * * ความปลอดภัย: * - ถ้าตั้ง ADMIN_TOKEN ไว้ ต้องส่ง header x-admin-token ให้ตรงทุกครั้งที่เขียน * - ถ้าไม่ได้ตั้ง อนุญาตเฉพาะ request ที่มาจากเครื่องเดียวกัน (loopback) - * - /api/local-projects และ /projects/* รับเฉพาะ request จากเครื่องเดียวกันเสมอ - * (เปิดเซิร์ฟเวอร์ให้เครื่องอื่นดูเว็บได้ แต่เปิดดูไฟล์ในเครื่องไม่ได้) */ import { createServer } from "node:http"; -import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises"; +import { readFile, stat, writeFile, mkdir } from "node:fs/promises"; import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { timingSafeEqual } from "node:crypto"; @@ -39,26 +33,6 @@ const HOST = process.env.HOST || "0.0.0.0"; const ADMIN_TOKEN = (process.env.ADMIN_TOKEN || "").trim(); const SHOULD_PUSH = process.env.PUBLISH_PUSH !== "0"; -/* โฟลเดอร์โปรเจคข้างเคียง (ค่าเริ่มต้น: โฟลเดอร์แม่ของ repo นี้ = D:\Git\workspace) */ -const WORKSPACE_DIR = - process.env.WORKSPACE_DIR === "none" - ? null - : path.resolve(process.env.WORKSPACE_DIR || path.join(ROOT, "..")); - -const ENTRY_CANDIDATES = [ - "index.html", - "public/index.html", - "ui/index.html", - "dist/index.html", - "frontend/dist/index.html", - "frontend/index.html", -]; - -const SKIP_NAMES = new Set(["node_modules", ".git", ".hermes", "archive", "Library", "Local Settings"]); - -/** ชื่อที่ไม่มีข้อมูล (index.html ของบางโปรเจคตั้งไว้ลอย ๆ) → ใช้ชื่อโฟลเดอร์แทน */ -const GENERIC_TITLES = new Set(["หน้าแรก", "home", "index", "untitled", "document", "new page", ""]); - const MIME = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", @@ -201,158 +175,7 @@ async function readData() { return JSON.parse(raw); } -/* ------------------------------------------------ โปรเจคในเครื่อง (workspace) */ - -async function readEntryTitle(file) { - try { - const html = await readFile(file, "utf8"); - const head = html.slice(0, 8192); - const title = head.match(/<title[^>]*>([^<]+)<\/title>/i); - if (title) return title[1].trim().replace(/\s+/g, " "); - const h1 = head.match(/<h1[^>]*>([\s\S]{1,160}?)<\/h1>/i); - if (h1) return h1[1].replace(/<[^>]+>/g, "").trim().replace(/\s+/g, " "); - } catch { - /* อ่านไม่ได้ก็ใช้ชื่อโฟลเดอร์แทน */ - } - return ""; -} - -async function findEntry(dir) { - for (const rel of ENTRY_CANDIDATES) { - try { - const info = await stat(path.join(dir, rel)); - if (info.isFile()) return { rel, mtime: info.mtimeMs }; - } catch { - /* ลองตัวถัดไป */ - } - } - return null; -} - -async function scanLocalProjects() { - if (!WORKSPACE_DIR) return []; - let dirents = []; - try { - dirents = await readdir(WORKSPACE_DIR, { withFileTypes: true }); - } catch { - return []; - } - const items = []; - for (const dirent of dirents) { - if (!dirent.isDirectory()) continue; - const folder = dirent.name; - if (folder.startsWith(".") || folder.startsWith("~") || SKIP_NAMES.has(folder)) continue; - const dir = path.join(WORKSPACE_DIR, folder); - if (dir === ROOT) continue; - const entry = await findEntry(dir); - if (!entry) continue; - const rawTitle = (await readEntryTitle(path.join(dir, entry.rel))).trim(); - items.push({ - folder, - entry: entry.rel, - url: `/projects/${encodeURIComponent(folder)}/${entry.rel.replace(/index\.html$/, "")}`, - title: GENERIC_TITLES.has(rawTitle.toLowerCase()) ? folder : rawTitle || folder, - updated_at: new Date(entry.mtime).toISOString(), - }); - } - items.sort((a, b) => b.updated_at.localeCompare(a.updated_at)); - return items; -} - -async function sendFile(res, filePath) { - const ext = path.extname(filePath).toLowerCase(); - const headers = { "content-type": MIME[ext] || "application/octet-stream" }; - if ([".html", ".css", ".js", ".mjs", ".json", ""].includes(ext)) { - Object.assign(headers, NO_CACHE); - } else { - headers["cache-control"] = "public, max-age=300"; - } - res.writeHead(200, headers); - res.end(await readFile(filePath)); -} - -function denyWorkspace(res, reason) { - const payload = `<!doctype html><meta charset="utf-8"><title>เปิดไม่ได้ -

เปิดไม่ได้

${reason}

`; - res.writeHead(403, { "content-type": "text/html; charset=utf-8" }); - res.end(payload); -} - -async function serveWorkspace(req, res, pathname) { - if (!WORKSPACE_DIR) { - res.writeHead(404, { "content-type": "text/html; charset=utf-8" }); - res.end("

ปิดการเสิร์ฟโฟลเดอร์โปรเจค (WORKSPACE_DIR=none)

"); - return; - } - if (!isLoopback(req)) { - denyWorkspace(res, "ลิงก์นี้เปิดได้เฉพาะเครื่องที่รันเซิร์ฟเวอร์ (localhost)"); - return; - } - - const rel = decodeURIComponent(pathname.replace(/^\/projects\/?/, "")); - const target = path.resolve(WORKSPACE_DIR, `./${rel}`); - const withinWorkspace = target === WORKSPACE_DIR || target.startsWith(WORKSPACE_DIR + path.sep); - if (!withinWorkspace || /(^|[\\/])(node_modules|\.git|\.hermes)([\\/]|$)/i.test(target)) { - denyWorkspace(res, "เส้นทางนี้ไม่ได้รับอนุญาต"); - return; - } - - try { - const info = await stat(target); - if (info.isDirectory()) { - if (!pathname.endsWith("/")) { - res.writeHead(301, { location: `${pathname}/` }); - res.end(); - return; - } - const indexFile = path.join(target, "index.html"); - try { - const indexInfo = await stat(indexFile); - if (!indexInfo.isFile()) throw new Error("not a file"); - } catch { - res.writeHead(404, { "content-type": "text/html; charset=utf-8" }); - res.end( - `ไม่มี index.html -
-

โฟลเดอร์นี้ไม่มี index.html

-

โปรเจคนี้ต้องรัน dev server แยก (เช่น npm run dev)

-
`, - ); - return; - } - await sendFile(res, indexFile); - return; - } - await sendFile(res, target); - } catch { - res.writeHead(404, { "content-type": "text/html; charset=utf-8" }); - res.end( - `ไม่พบไฟล์ -

ไม่พบไฟล์นี้

-

กลับไปหน้ารายชื่อโปรเจค

`, - ); - } -} - async function handleApi(req, res, pathname) { - if (pathname === "/api/local-projects" && req.method === "GET") { - if (!canWrite(req)) { - sendJson(res, 403, { ok: false, error: "รายชื่อโปรเจคในเครื่องเปิดได้เฉพาะเครื่องที่รันเซิร์ฟเวอร์" }); - return; - } - try { - const projects = await scanLocalProjects(); - sendJson(res, 200, { - ok: true, - workspace: WORKSPACE_DIR ? path.basename(WORKSPACE_DIR) : null, - projects, - }); - } catch (error) { - sendJson(res, 500, { ok: false, error: `สแกนโฟลเดอร์ไม่สำเร็จ: ${error.message}` }); - } - return; - } - if (pathname === "/api/projects" && req.method === "GET") { try { const data = await readData(); @@ -464,16 +287,6 @@ const server = createServer((req, res) => { res.end(); return; } - - // ลิงก์ไปหน้าแรกของแต่ละโปรเจคในโฟลเดอร์ข้างเคียง (เฉพาะเครื่องนี้) - if (url.pathname === "/projects" || url.pathname.startsWith("/projects/")) { - serveWorkspace(req, res, url.pathname).catch((error) => { - res.writeHead(500, { "content-type": "text/plain; charset=utf-8" }); - res.end(`อ่านโฟลเดอร์โปรเจคไม่สำเร็จ: ${error.message}`); - }); - return; - } - handleApi(req, res, url.pathname).catch((error) => { sendJson(res, 500, { ok: false, error: error.message }); }); @@ -484,7 +297,4 @@ server.listen(PORT, HOST, () => { console.log(`[hobbypj] เซิร์ฟเวอร์ทำงานที่ http://localhost:${PORT}`); console.log(`[hobbypj] เขียนข้อมูลได้: ${writable}`); console.log(`[hobbypj] ไฟล์ข้อมูล: ${path.relative(ROOT, DATA_FILE)} · push: ${SHOULD_PUSH ? "เปิด" : "ปิด"}`); - console.log( - `[hobbypj] โปรเจคในเครื่อง: ${WORKSPACE_DIR ? `${WORKSPACE_DIR} → /projects/<โฟลเดอร์>/` : "ปิด"} (เฉพาะ localhost)`, - ); }); diff --git a/sw.js b/sw.js index d10493d..ee76b37 100644 --- a/sw.js +++ b/sw.js @@ -2,7 +2,7 @@ กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */ -const VERSION = "2026.11.19.5"; +const VERSION = "2026.11.19.4"; const CACHE = `hobbypj-${VERSION}`; const PRECACHE = [ @@ -66,8 +66,8 @@ self.addEventListener("fetch", (event) => { const url = new URL(request.url); if (url.origin !== self.location.origin) return; - // อย่าแตะ API และหน้าโปรเจคข้างเคียง: ต้องถึงเซิร์ฟเวอร์เสมอ (ไม่ควรเอาไฟล์โปรเจคอื่นมาแคช) - if (url.pathname.includes("/api/") || url.pathname.startsWith("/projects/")) return; + // อย่าแตะ API: คำขอเผยแพร่/อ่านข้อมูลสดต้องถึงเซิร์ฟเวอร์เสมอ + if (url.pathname.includes("/api/")) return; const isContent = request.destination === "document" || diff --git a/ui/data.js b/ui/data.js index 34deb54..296fd39 100644 --- a/ui/data.js +++ b/ui/data.js @@ -189,22 +189,6 @@ export async function probePublishApi() { } } -/** รายชื่อโปรเจคในโฟลเดอร์ข้างเคียง (มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้านในเครื่องนี้) */ -export async function fetchLocalProjects() { - try { - const res = await fetch("api/local-projects", { cache: "no-store" }); - if (!res.ok) return { available: false, workspace: null, projects: [] }; - const body = await res.json(); - return { - available: true, - workspace: body.workspace || null, - projects: Array.isArray(body.projects) ? body.projects : [], - }; - } catch { - return { available: false, workspace: null, projects: [] }; - } -} - export function downloadJson(data) { const payload = toPublishPayload(data); const blob = new Blob([`${JSON.stringify(payload, null, 2)}\n`], { diff --git a/ui/views/home.js b/ui/views/home.js index d9d6032..a0afbe9 100644 --- a/ui/views/home.js +++ b/ui/views/home.js @@ -2,16 +2,6 @@ import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js"; import { initReveal, splitWordsHtml } from "../motion.js"; -import { fetchLocalProjects } from "../data.js"; - -const TH_DATE = new Intl.DateTimeFormat("th-TH", { day: "numeric", month: "short", year: "numeric" }); -function formatDate(iso) { - try { - return TH_DATE.format(new Date(iso)); - } catch { - return ""; - } -} /** สุ่มลำดับด้วย Fisher–Yates แล้วหยิบมา count ใบ (ไม่ซ้ำ) */ function sampleRandom(list, count) { @@ -96,40 +86,6 @@ export function homeView(ctx) { initReveal(gridEl); } - function localRowHtml(item) { - const folder = escapeHtml(item.folder); - const entry = escapeHtml(item.entry || "index.html"); - const title = escapeHtml(item.title || item.folder); - const date = formatDate(item.updated_at); - return ` -
  • - - - - ${title} - ${folder} · ${entry}${ - date ? ` · แก้ล่าสุด ${date}` : "" - } - - เปิดหน้าแรก${icon("arrowRight")} - -
  • `; - } - - /** โปรเจคในเครื่อง — มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้าน (ไม่ขึ้นบนเว็บจริง) */ - async function loadLocalProjects() { - const box = root?.querySelector("[data-local]"); - const list = root?.querySelector("[data-local-list]"); - const countEl = root?.querySelector("[data-local-count]"); - if (!box || !list) return; - const result = await fetchLocalProjects(); - if (!root || !result.projects.length) return; - if (countEl) countEl.textContent = `${result.projects.length} โฟลเดอร์`; - list.innerHTML = result.projects.map(localRowHtml).join(""); - box.hidden = false; - initReveal(box); - } - return { mount(container) { root = container; @@ -194,19 +150,6 @@ export function homeView(ctx) {
    - - `; gridEl = root.querySelector("[data-grid]"); @@ -233,7 +176,6 @@ export function homeView(ctx) { }); renderGrid(); - loadLocalProjects(); }, destroy() { root = null;