Skip to content

Commit 59dd024

Browse files
committed
refactor: enhance directory handling in HttpCommand by using stat for symlink resolution and improve content type setting for Koa responses
1 parent ce4d3f3 commit 59dd024

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎commands/http.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ class HttpCommand extends Command {
8787
const entries = await fs.promises.readdir(dir, { withFileTypes: true });
8888
const files = await Promise.all(entries.map(async (entry) => {
8989
const filepath = path.join(dir, entry.name);
90-
const is_dir = entry.isDirectory();
90+
// Use stat (not entry.isDirectory()) so symlinks to directories are
91+
// resolved as directories. `Dirent.isDirectory()` is false for
92+
// symlinks regardless of their target.
9193
const stats = await stat(filepath);
94+
const is_dir = stats.isDirectory();
9295
return {
9396
filename: entry.name,
9497
is_dir,
@@ -157,8 +160,11 @@ class HttpCommand extends Command {
157160
const extname = _ext(d);
158161
const mime = mimeTypes[extname];
159162
if (mime) {
160-
context.koa.type = extname;
161-
context.koa.set('Content-Type', mime);
163+
// Route through Koa's type setter so charset is auto-appended
164+
// for text/* types (and others registered with a charset in
165+
// mime-db). Calling `set('Content-Type', mime)` directly would
166+
// strip that charset and break non-ASCII content.
167+
context.koa.type = mime;
162168
} else {
163169
const filename = path.basename(d);
164170
context.koa.set('Content-Type', 'application/octet-stream');

0 commit comments

Comments
 (0)