Skip to content

Commit 6484d94

Browse files
authored
Merge pull request #574 from albedosehen/fix/web-server-mime-types
Expand MIME type map in web server to broaden asset type support with fallback (fonts, source maps, etc)
2 parents 7bdd1ac + cccffd5 commit 6484d94

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

packages/nuekit/src/nueserver.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ import http from 'node:http'
1010
import { join, extname } from 'node:path'
1111

1212
export const TYPES = {
13-
html: 'text/html; charset=UTF-8',
14-
js: 'application/javascript',
15-
wasm: 'application/wasm',
16-
json: 'application/json',
17-
svg: 'image/svg+xml',
18-
ico: 'image/x-icon',
19-
webp: 'image/webp',
20-
png: 'image/png',
21-
jpeg: 'image/jpg',
22-
jpg: 'image/jpg',
23-
css: 'text/css',
24-
csv: 'text/csv',
13+
html: 'text/html; charset=UTF-8',
14+
js: 'application/javascript',
15+
wasm: 'application/wasm',
16+
json: 'application/json',
17+
svg: 'image/svg+xml',
18+
ico: 'image/x-icon',
19+
webp: 'image/webp',
20+
png: 'image/png',
21+
jpeg: 'image/jpeg',
22+
jpg: 'image/jpeg',
23+
txt: 'text/plain',
24+
css: 'text/css',
25+
csv: 'text/csv',
26+
woff: 'font/woff',
27+
woff2: 'font/woff2',
28+
ttf: 'font/ttf',
29+
otf: 'font/otf',
30+
eot: 'application/vnd.ms-fontobject',
31+
mp4: 'video/mp4',
32+
webm: 'video/webm',
33+
mp3: 'audio/mpeg',
34+
ogg: 'audio/ogg',
35+
default: 'application/octet-stream'
2536
}
2637

2738
let sessions = []
@@ -53,7 +64,9 @@ export function createServer(root, callback) {
5364
const { code, path } = !ext || ext == 'html' ? await callback(url, _) : { path: url }
5465
if (!path) throw { errno: -2 }
5566
const buffer = await fs.readFile(join(root, path))
56-
res.writeHead(code || 200, { 'content-type': TYPES[ext] })
67+
res.writeHead(code || 200, {
68+
'content-type': TYPES[ext] || TYPES.default
69+
})
5770
res.end(buffer)
5871

5972
} catch (e) {

packages/nuekit/test/misc.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ beforeEach(async () => {
1919
await fs.mkdir(root, { recursive: true })
2020
})
2121

22-
afterEach(async () => await fs.rm(root, { recursive: true, force: true }))
22+
afterEach(async () => {
23+
await fs.rm(root, { recursive: true, force: true })
24+
})
2325

2426
async function write(filename, code) {
2527
const file = join(root, filename)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { promises as fs } from 'node:fs'
2+
import { join } from 'node:path'
3+
4+
import { createServer, TYPES } from '../src/nueserver.js'
5+
6+
// temporary directory
7+
const root = '_test'
8+
const PORT = 3403
9+
const HOST = `http://localhost:${PORT}`
10+
const testFiles = []
11+
12+
13+
// setup and teardown
14+
beforeAll(async () => {
15+
await fs.rm(root, { recursive: true, force: true })
16+
await fs.mkdir(root, { recursive: true })
17+
18+
// Setup: Create a temporary directory & a test file for each MIME type
19+
for (const ext in TYPES) {
20+
const filename = `test.${ext == 'default' ? 'unknown' : ext}`
21+
const filePath = join(root, filename)
22+
23+
await fs.writeFile(filePath, '')
24+
testFiles.push({ ext, filename, filePath })
25+
}
26+
})
27+
28+
afterAll(async () => {
29+
await fs.rm(root, { recursive: true, force: true })
30+
})
31+
32+
33+
test('serve all file extensions with correct MIME type', async () => {
34+
const server = createServer(root, (path) => ({ path }))
35+
server.listen(PORT)
36+
37+
try {
38+
for (const { ext, filename } of testFiles) {
39+
const res = await fetch(`${HOST}/${filename}`)
40+
const contentType = res.headers.get('content-type')
41+
42+
expect(res.status).toBe(200)
43+
expect(contentType).toBe(TYPES[ext])
44+
}
45+
} finally { server.close() }
46+
})

0 commit comments

Comments
 (0)