Skip to content

Commit f12c2cc

Browse files
committed
feat: use stream toreplace read file directly
1 parent 063d3c6 commit f12c2cc

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"dependencies": {
3939
"chalk": "^5.3.0",
4040
"commander": "^12.1.0",
41-
"ejs": "^3.1.10"
41+
"ejs": "^3.1.10",
42+
"mime": "^4.0.4"
4243
},
4344
"devDependencies": {
4445
"@dhzh/eslint-config": "^0.15.1",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as http from 'node:http';
22
import * as os from 'node:os';
33
import * as fs from 'node:fs/promises';
4-
import { readFileSync } from 'node:fs';
4+
import { createReadStream, readFileSync } from 'node:fs';
55
import * as path from 'node:path';
66
import chalk from 'chalk';
77
import * as ejs from 'ejs';
8+
import mine from 'mime';
89
import { DEFAULT_PORT, DEFAULT_BASE_DIR } from './constants';
910
import type { ServerOptions, Res } from './types';
1011

@@ -35,6 +36,7 @@ export default class Server {
3536
this.processFile(wholePath, res);
3637
}
3738
} catch {
39+
res.statusCode = 404;
3840
res.end('Not found');
3941
}
4042
});
@@ -62,7 +64,7 @@ export default class Server {
6264
}
6365

6466
private async processFile(file: string, res: Res) {
65-
const content = await fs.readFile(file);
66-
res.end(content);
67+
res.setHeader('Content-Type', `${mine.getType(file) ?? 'text/plain'};charset=utf-8`);
68+
createReadStream(file).pipe(res);
6769
}
6870
}

0 commit comments

Comments
 (0)