Skip to content

Commit 6aabaf8

Browse files
committed
feat: optimize file reader
1 parent ed0786d commit 6aabaf8

31 files changed

+87
-86
lines changed

dist/server/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/server/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async function cook(inputConfig, { createServer, requestHandler }) {
2424
const server = await createServer(config);
2525
// extend with resulting config
2626
server.config = config;
27+
server.config.folder ||= ".";
2728
// spread
2829
const { folder, maxCacheSize, type, port, plugins, ssl } = server.config;
2930
// create the static files reader based on folder

dist/server/static-files.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/server/static-files.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,37 @@ const fs_1 = require("fs");
1010
const config_1 = __importDefault(require("../config"));
1111
const path_1 = require("path");
1212
const mime_types_1 = require("mime-types");
13-
const readFile = (folder, filename = "index.html") => {
13+
const readFile = (path) => {
1414
try {
15-
const path = (0, path_1.join)(folder, filename);
1615
return (0, fs_1.readFileSync)(path, { encoding: "utf8" }) || "";
1716
} catch (_err) {
18-
return "";
17+
return "<html></html>";
1918
}
2019
};
2120
const getMimeFromURL = (url) =>
2221
(0, mime_types_1.lookup)(url) || "application/octet-stream";
2322
function createFileReader(folder) {
2423
// get main index
25-
const indexHTML = readFile(folder);
24+
const indexURL = "index.html";
25+
const indexHTML = readFile((0, path_1.join)(folder, indexURL));
2626
// this is used as file reader cache
27-
return function fileReader(url) {
27+
return function fileReader(inputURL) {
28+
const url = inputURL || indexURL;
2829
const mime = getMimeFromURL(url);
2930
const filename = (0, path_1.join)(folder, url);
31+
if (config_1.default.debug) {
32+
console.log(filename);
33+
}
3034
if (!(0, fs_1.existsSync)(filename)) {
31-
return { mime: "text/html", body: indexHTML, status: 200 };
35+
if (config_1.default.spa) {
36+
return { mime: "text/html", body: indexHTML, status: 200 };
37+
} else {
38+
return { mime: "text/plain", body: "", status: 404 };
39+
}
3240
}
3341
if ((0, fs_1.lstatSync)(filename).isDirectory()) {
3442
return fileReader(`${url}/index.html`);
3543
}
36-
if (config_1.default.spa) {
37-
const body = (0, fs_1.readFileSync)(filename);
38-
return { mime, body, status: 200 };
39-
} else {
40-
return null;
41-
}
44+
return { mime, body: readFile(filename), status: 200 };
4245
};
4346
}

docs/functions/cook.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ <h4 class="tsd-returns-title">
167167
<li>
168168
Defined in
169169
<a
170-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/server/index.ts#L20"
171-
>server/index.ts:20</a
170+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/server/index.ts#L14"
171+
>server/index.ts:14</a
172172
>
173173
</li>
174174
</ul>

docs/functions/createFileReader.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ <h4 class="tsd-returns-title">
131131
<li>
132132
Defined in
133133
<a
134-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/server/static-files.ts#L21"
135-
>server/static-files.ts:21</a
134+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/server/static-files.ts#L19"
135+
>server/static-files.ts:19</a
136136
>
137137
</li>
138138
</ul>

docs/functions/getParam.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ <h4 class="tsd-returns-title">
133133
<li>
134134
Defined in
135135
<a
136-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/get-param.ts#L1"
136+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/get-param.ts#L1"
137137
>get-param.ts:1</a
138138
>
139139
</li>

docs/functions/getPlugin.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ <h4 class="tsd-returns-title">
148148
<li>
149149
Defined in
150150
<a
151-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/plugins.ts#L31"
151+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/plugins.ts#L31"
152152
>plugins.ts:31</a
153153
>
154154
</li>

docs/functions/getUrl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ <h4 class="tsd-returns-title">
123123
<li>
124124
Defined in
125125
<a
126-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/server/get-url.ts#L1"
126+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/server/get-url.ts#L1"
127127
>server/get-url.ts:1</a
128128
>
129129
</li>

docs/functions/populatePlugins.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ <h4 class="tsd-returns-title">
136136
<li>
137137
Defined in
138138
<a
139-
href="https://github.com/chef-js/core/blob/e14b0fa2395235059fd95409b6156f764931ea4b/src/plugins.ts#L5"
139+
href="https://github.com/chef-js/core/blob/472c23126bdbbb1fd9a6aa7a5b5d04a051c31b26/src/plugins.ts#L5"
140140
>plugins.ts:5</a
141141
>
142142
</li>

0 commit comments

Comments
 (0)