@noy4/docserve 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server/files.mjs +30 -2
- package/server/index.mjs +11 -2
package/package.json
CHANGED
package/server/files.mjs
CHANGED
|
@@ -78,6 +78,27 @@ export async function apiFiles(docsDir) {
|
|
|
78
78
|
// files that changed.
|
|
79
79
|
const metaCache = new Map() // "path:mtimeMs" -> { title, date, favicon }
|
|
80
80
|
|
|
81
|
+
// Decode <title> entities so cards read "A & B", not "A & B".
|
|
82
|
+
const ENTITIES = {
|
|
83
|
+
amp: "&",
|
|
84
|
+
lt: "<",
|
|
85
|
+
gt: ">",
|
|
86
|
+
quot: '"',
|
|
87
|
+
apos: "'",
|
|
88
|
+
nbsp: "\u00a0",
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function decodeEntities(s) {
|
|
92
|
+
return s.replace(/&(#[xX]?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g, (m, ref) => {
|
|
93
|
+
if (ref[0] === "#") {
|
|
94
|
+
const hex = /[xX]/.test(ref[1])
|
|
95
|
+
const cp = parseInt(hex ? ref.slice(2) : ref.slice(1), hex ? 16 : 10)
|
|
96
|
+
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m
|
|
97
|
+
}
|
|
98
|
+
return ENTITIES[ref.toLowerCase()] ?? m
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
81
102
|
async function metaOf(full, mtimeMs, docsDir) {
|
|
82
103
|
const key = `${full}:${mtimeMs}`
|
|
83
104
|
const cached = metaCache.get(key)
|
|
@@ -88,7 +109,10 @@ async function metaOf(full, mtimeMs, docsDir) {
|
|
|
88
109
|
}
|
|
89
110
|
|
|
90
111
|
async function extractMeta(full, docsDir) {
|
|
91
|
-
const findTitle = (text) =>
|
|
112
|
+
const findTitle = (text) => {
|
|
113
|
+
const raw = text.match(/<title>([^<]*)<\/title>/)?.[1]?.trim()
|
|
114
|
+
return raw == null ? null : decodeEntities(raw)
|
|
115
|
+
}
|
|
92
116
|
const findDate = (text) => {
|
|
93
117
|
for (const tag of text.match(/<meta\b[^>]*>/gi) ?? []) {
|
|
94
118
|
if (!/\bname\s*=\s*["']created_at["']/i.test(tag)) continue
|
|
@@ -131,7 +155,11 @@ async function extractMeta(full, docsDir) {
|
|
|
131
155
|
}
|
|
132
156
|
// Titles and meta tags live in <head>; read only the first 4 KB.
|
|
133
157
|
const head = await read(4096, 0)
|
|
134
|
-
let meta = {
|
|
158
|
+
let meta = {
|
|
159
|
+
title: findTitle(head),
|
|
160
|
+
date: findDate(head),
|
|
161
|
+
favicon: findFavicon(head),
|
|
162
|
+
}
|
|
135
163
|
if (meta.title === null || meta.date === null || meta.favicon === null) {
|
|
136
164
|
// Fallback for documents with a long preamble (inline favicon etc.)
|
|
137
165
|
const text = await read(1 << 20, 0)
|
package/server/index.mjs
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
// │ │ ├─ /api/files ──▶ apiFiles() │
|
|
7
7
|
// │ │ ├─ /, /index.html ──▶ gallery template │
|
|
8
8
|
// │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
|
|
9
|
+
// │ │ └─ /dir/ ──▶ dir/index.html │
|
|
9
10
|
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
10
11
|
// │ └─ listen ──▶ listener.start() + writeState() │
|
|
11
12
|
// └────────────────────────────────────────────────────────┘
|
|
12
13
|
import { createServer } from "node:http"
|
|
13
|
-
import { readFile } from "node:fs/promises"
|
|
14
|
+
import { readFile, stat } from "node:fs/promises"
|
|
14
15
|
import os from "node:os"
|
|
15
16
|
import { basename, extname, resolve } from "node:path"
|
|
16
17
|
import { exec } from "node:child_process"
|
|
@@ -135,7 +136,9 @@ function createHandler({ docsDir }) {
|
|
|
135
136
|
return new Response("Bad Request", { status: 400 })
|
|
136
137
|
}
|
|
137
138
|
|
|
138
|
-
|
|
139
|
+
// /sub/ → /sub/index.html
|
|
140
|
+
const target = pathname.endsWith("/") ? `${pathname}index.html` : pathname
|
|
141
|
+
const path = resolve(docsDir, target.slice(1))
|
|
139
142
|
if (!isWithin(docsDir, path)) {
|
|
140
143
|
return new Response("Forbidden", { status: 403 })
|
|
141
144
|
}
|
|
@@ -151,6 +154,12 @@ function createHandler({ docsDir }) {
|
|
|
151
154
|
}
|
|
152
155
|
return new Response(body, { headers: { "content-type": type } })
|
|
153
156
|
} catch {
|
|
157
|
+
// Redirect /sub → /sub/
|
|
158
|
+
try {
|
|
159
|
+
if ((await stat(path)).isDirectory()) {
|
|
160
|
+
return Response.redirect(new URL(`${url.pathname}/`, url), 301)
|
|
161
|
+
}
|
|
162
|
+
} catch { }
|
|
154
163
|
return new Response("Not Found", { status: 404 })
|
|
155
164
|
}
|
|
156
165
|
}
|