poops 1.9.1 → 1.9.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/lib/server.js +21 -6
- package/package.json +1 -1
package/lib/server.js
CHANGED
|
@@ -63,16 +63,25 @@ export function parseRange(header, size) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export function createStaticHandler(base) {
|
|
66
|
+
// Drop any trailing separator — `serve.base: "/"` resolves to `<cwd>/`, and the
|
|
67
|
+
// containment check below would then compare against `<cwd>//` and reject everything
|
|
68
|
+
base = path.resolve(base)
|
|
66
69
|
const notFoundPage = path.join(base, '404.html')
|
|
67
70
|
|
|
68
71
|
const notFound = (res) => {
|
|
69
72
|
res.statusCode = 404
|
|
70
|
-
if (pathExists(notFoundPage))
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
if (!pathExists(notFoundPage)) return res.end('Not Found')
|
|
74
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
75
|
+
// 404.html lives at the site root but is served at any depth — its relative
|
|
76
|
+
// asset paths would resolve against /a/ for /a/b, so pin them with <base>
|
|
77
|
+
let html
|
|
78
|
+
try {
|
|
79
|
+
html = fs.readFileSync(notFoundPage, 'utf8')
|
|
80
|
+
} catch {
|
|
81
|
+
return res.end('Not Found')
|
|
75
82
|
}
|
|
83
|
+
if (!/<base[\s>]/i.test(html)) html = html.replace(/<head([^>]*)>/i, '<head$1><base href="/">')
|
|
84
|
+
res.end(html)
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
return (req, res) => {
|
|
@@ -100,7 +109,13 @@ export function createStaticHandler(base) {
|
|
|
100
109
|
try {
|
|
101
110
|
stat = fs.statSync(filePath)
|
|
102
111
|
} catch {
|
|
103
|
-
|
|
112
|
+
// GitHub Pages-style extensionless URLs: /a/b serves a/b.html, URL unchanged
|
|
113
|
+
try {
|
|
114
|
+
stat = fs.statSync(filePath + '.html')
|
|
115
|
+
filePath += '.html'
|
|
116
|
+
} catch {
|
|
117
|
+
return notFound(res)
|
|
118
|
+
}
|
|
104
119
|
}
|
|
105
120
|
|
|
106
121
|
if (stat.isDirectory()) {
|