From 2ab58172cbd720ae41b7da1064a96d721eb492b9 Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Sat, 8 Nov 2025 18:40:59 +0800 Subject: [PATCH 1/2] fix: serve static files with proper content-type --- bin/darko-serve | 13 +++++++------ lib/writers/templated.js | 1 - package.json | 1 + test/fixture/assets/darko.svg | 1 + test/fixture/index.md | 2 ++ 5 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 test/fixture/assets/darko.svg diff --git a/bin/darko-serve b/bin/darko-serve index e6d930c..0ddcc48 100755 --- a/bin/darko-serve +++ b/bin/darko-serve @@ -3,6 +3,7 @@ 'use strict' const program = require('commander') +const mime = require('mime-types') program .option('-s --source [source]', 'Source directory (default to ./)', './') @@ -77,12 +78,7 @@ function handle(req, res) { function sendFile(fpath) { debug('Sending ' + fpath) - const mime = { - '.css': 'text/css', - '.html': 'text/html', - '.js': 'application/javascript' - } - const contentType = mime[path.extname(fpath).toLowerCase()] + const contentType = mime.contentType(path.extname(fpath).toLowerCase()) if (contentType) { res.setHeader('Content-Type', contentType) @@ -115,6 +111,11 @@ function handle(req, res) { return } + if (!(fpath.startsWith(site.dest) || fpath.startsWith(droot))) { + send404() + return + } + const stats = fs.statSync(fpath) if (stats.isFile()) { diff --git a/lib/writers/templated.js b/lib/writers/templated.js index b03d3e3..82ebc72 100644 --- a/lib/writers/templated.js +++ b/lib/writers/templated.js @@ -4,7 +4,6 @@ const path = require('path') const yaml = require('yaml-js') const mkdirp = require('mkdirp') const debug = require('debug')('darko') -const util = require('util') const fs = require('fs') const md = require('../markdown') diff --git a/package.json b/package.json index 4c335cd..92d0de1 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "highlight.js": "^11.11.1", "liquid-node": "^3.0.0", "markit": "~0.1.0", + "mime-types": "^3.0.1", "mkdirp": "~0.3.5", "rimraf": "^2.6.2", "strftime": "~0.9.0", diff --git a/test/fixture/assets/darko.svg b/test/fixture/assets/darko.svg new file mode 100644 index 0000000..0f010a6 --- /dev/null +++ b/test/fixture/assets/darko.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/fixture/index.md b/test/fixture/index.md index 803965c..fe7db78 100644 --- a/test/fixture/index.md +++ b/test/fixture/index.md @@ -3,6 +3,8 @@ layout: post title: Welcome! --- + + Good luck, have fun!