@t8/serve 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -27,18 +27,34 @@ module.exports = __toCommonJS(index_exports);
27
27
  // src/serve.ts
28
28
  var import_node_fs = require("node:fs");
29
29
  var import_node_http = require("node:http");
30
- var import_node_path2 = require("node:path");
30
+ var import_node_path3 = require("node:path");
31
31
 
32
- // src/getFilePath.ts
32
+ // src/bundle.ts
33
+ var import_node_child_process = require("node:child_process");
34
+ var import_promises = require("node:fs/promises");
33
35
  var import_node_path = require("node:path");
36
+ var import_node_util = require("node:util");
37
+ var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
38
+ async function bundle({ path = "", bundle: options } = {}) {
39
+ if (!options) return;
40
+ let inputFile = (0, import_node_path.join)(path, options.input ?? "index.ts");
41
+ let outputFile = (0, import_node_path.join)(path, "dist", options.output ?? "index.js");
42
+ await (0, import_promises.rm)((0, import_node_path.join)(path, "dist"), { recursive: true, force: true });
43
+ await exec(
44
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
45
+ );
46
+ }
47
+
48
+ // src/getFilePath.ts
49
+ var import_node_path2 = require("node:path");
34
50
 
35
51
  // src/isValidFilePath.ts
36
- var import_promises = require("node:fs/promises");
52
+ var import_promises2 = require("node:fs/promises");
37
53
  async function isValidFilePath(filePath, dirPath) {
38
54
  if (!filePath.startsWith(dirPath)) return false;
39
55
  try {
40
- await (0, import_promises.access)(filePath);
41
- return !(await (0, import_promises.lstat)(filePath)).isDirectory();
56
+ await (0, import_promises2.access)(filePath);
57
+ return !(await (0, import_promises2.lstat)(filePath)).isDirectory();
42
58
  } catch {
43
59
  return false;
44
60
  }
@@ -49,11 +65,11 @@ var cwd = process.cwd();
49
65
  async function getFilePath(url = "", { path = "", dirs = [], spa }) {
50
66
  let urlPath = url.replace(/[?#].*$/, "");
51
67
  for (let dir of dirs.length === 0 ? [""] : dirs) {
52
- let dirPath = (0, import_node_path.join)(cwd, path, dir);
53
- let filePath = (0, import_node_path.join)(dirPath, urlPath);
68
+ let dirPath = (0, import_node_path2.join)(cwd, path, dir);
69
+ let filePath = (0, import_node_path2.join)(dirPath, urlPath);
54
70
  if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
55
71
  return filePath;
56
- filePath = (0, import_node_path.join)(dirPath, spa ? "" : urlPath, "index.html");
72
+ filePath = (0, import_node_path2.join)(dirPath, spa ? "" : urlPath, "index.html");
57
73
  if (await isValidFilePath(filePath, dirPath)) return filePath;
58
74
  }
59
75
  }
@@ -76,32 +92,36 @@ var mimeTypes = {
76
92
  // src/serve.ts
77
93
  var defaultHost = "localhost";
78
94
  var defaultPort = 3e3;
79
- function serve(config = {}) {
95
+ async function serve(config = {}) {
80
96
  let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
81
97
  if (!port && /^\d+$/.test(host)) {
82
98
  port = host;
83
99
  host = defaultHost;
84
100
  }
85
- let server = (0, import_node_http.createServer)(async (req, res) => {
86
- let filePath = await getFilePath(req.url, config);
87
- if (filePath === void 0) {
88
- res.writeHead(404, { "content-type": "text/plain" });
89
- res.end("Not found");
90
- return;
91
- }
92
- let ext = (0, import_node_path2.extname)(filePath).slice(1).toLowerCase();
93
- let mimeType = mimeTypes[ext] ?? "application/octet-stream";
94
- res.writeHead(200, { "content-type": mimeType });
95
- (0, import_node_fs.createReadStream)(filePath).pipe(res);
96
- });
97
- server.on("close", () => {
98
- process.exit(0);
101
+ await bundle(config);
102
+ return new Promise((resolve) => {
103
+ let server = (0, import_node_http.createServer)(async (req, res) => {
104
+ let filePath = await getFilePath(req.url, config);
105
+ if (filePath === void 0) {
106
+ res.writeHead(404, { "content-type": "text/plain" });
107
+ res.end("Not found");
108
+ return;
109
+ }
110
+ let ext = (0, import_node_path3.extname)(filePath).slice(1).toLowerCase();
111
+ let mimeType = mimeTypes[ext] ?? "application/octet-stream";
112
+ res.writeHead(200, { "content-type": mimeType });
113
+ (0, import_node_fs.createReadStream)(filePath).pipe(res);
114
+ });
115
+ server.on("close", () => {
116
+ process.exit(0);
117
+ });
118
+ let serverPort = Number(port) || defaultPort;
119
+ let serverHost = host || defaultHost;
120
+ server.listen(serverPort, serverHost, () => {
121
+ console.log(`Server running at http://${serverHost}:${serverPort}`);
122
+ resolve(server);
123
+ });
99
124
  });
100
- let serverPort = Number(port) || defaultPort;
101
- let serverHost = host || defaultHost;
102
- server.listen(serverPort, serverHost);
103
- console.log(`Server running at http://${serverHost}:${serverPort}`);
104
- return server;
105
125
  }
106
126
  // Annotate the CommonJS export names for ESM import in node:
107
127
  0 && (module.exports = {
package/dist/run.cjs CHANGED
@@ -1,27 +1,37 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
- // src/run.ts
5
- var import_node_child_process = require("node:child_process");
6
- var import_promises2 = require("node:fs/promises");
7
- var import_node_path3 = require("node:path");
8
- var import_node_util = require("node:util");
9
-
10
4
  // src/serve.ts
11
5
  var import_node_fs = require("node:fs");
12
6
  var import_node_http = require("node:http");
13
- var import_node_path2 = require("node:path");
7
+ var import_node_path3 = require("node:path");
14
8
 
15
- // src/getFilePath.ts
9
+ // src/bundle.ts
10
+ var import_node_child_process = require("node:child_process");
11
+ var import_promises = require("node:fs/promises");
16
12
  var import_node_path = require("node:path");
13
+ var import_node_util = require("node:util");
14
+ var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
15
+ async function bundle({ path = "", bundle: options } = {}) {
16
+ if (!options) return;
17
+ let inputFile = (0, import_node_path.join)(path, options.input ?? "index.ts");
18
+ let outputFile = (0, import_node_path.join)(path, "dist", options.output ?? "index.js");
19
+ await (0, import_promises.rm)((0, import_node_path.join)(path, "dist"), { recursive: true, force: true });
20
+ await exec(
21
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
22
+ );
23
+ }
24
+
25
+ // src/getFilePath.ts
26
+ var import_node_path2 = require("node:path");
17
27
 
18
28
  // src/isValidFilePath.ts
19
- var import_promises = require("node:fs/promises");
29
+ var import_promises2 = require("node:fs/promises");
20
30
  async function isValidFilePath(filePath, dirPath) {
21
31
  if (!filePath.startsWith(dirPath)) return false;
22
32
  try {
23
- await (0, import_promises.access)(filePath);
24
- return !(await (0, import_promises.lstat)(filePath)).isDirectory();
33
+ await (0, import_promises2.access)(filePath);
34
+ return !(await (0, import_promises2.lstat)(filePath)).isDirectory();
25
35
  } catch {
26
36
  return false;
27
37
  }
@@ -32,11 +42,11 @@ var cwd = process.cwd();
32
42
  async function getFilePath(url = "", { path = "", dirs = [], spa }) {
33
43
  let urlPath = url.replace(/[?#].*$/, "");
34
44
  for (let dir of dirs.length === 0 ? [""] : dirs) {
35
- let dirPath = (0, import_node_path.join)(cwd, path, dir);
36
- let filePath = (0, import_node_path.join)(dirPath, urlPath);
45
+ let dirPath = (0, import_node_path2.join)(cwd, path, dir);
46
+ let filePath = (0, import_node_path2.join)(dirPath, urlPath);
37
47
  if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
38
48
  return filePath;
39
- filePath = (0, import_node_path.join)(dirPath, spa ? "" : urlPath, "index.html");
49
+ filePath = (0, import_node_path2.join)(dirPath, spa ? "" : urlPath, "index.html");
40
50
  if (await isValidFilePath(filePath, dirPath)) return filePath;
41
51
  }
42
52
  }
@@ -59,36 +69,39 @@ var mimeTypes = {
59
69
  // src/serve.ts
60
70
  var defaultHost = "localhost";
61
71
  var defaultPort = 3e3;
62
- function serve(config = {}) {
72
+ async function serve(config = {}) {
63
73
  let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
64
74
  if (!port && /^\d+$/.test(host)) {
65
75
  port = host;
66
76
  host = defaultHost;
67
77
  }
68
- let server = (0, import_node_http.createServer)(async (req, res) => {
69
- let filePath = await getFilePath(req.url, config);
70
- if (filePath === void 0) {
71
- res.writeHead(404, { "content-type": "text/plain" });
72
- res.end("Not found");
73
- return;
74
- }
75
- let ext = (0, import_node_path2.extname)(filePath).slice(1).toLowerCase();
76
- let mimeType = mimeTypes[ext] ?? "application/octet-stream";
77
- res.writeHead(200, { "content-type": mimeType });
78
- (0, import_node_fs.createReadStream)(filePath).pipe(res);
79
- });
80
- server.on("close", () => {
81
- process.exit(0);
78
+ await bundle(config);
79
+ return new Promise((resolve) => {
80
+ let server = (0, import_node_http.createServer)(async (req, res) => {
81
+ let filePath = await getFilePath(req.url, config);
82
+ if (filePath === void 0) {
83
+ res.writeHead(404, { "content-type": "text/plain" });
84
+ res.end("Not found");
85
+ return;
86
+ }
87
+ let ext = (0, import_node_path3.extname)(filePath).slice(1).toLowerCase();
88
+ let mimeType = mimeTypes[ext] ?? "application/octet-stream";
89
+ res.writeHead(200, { "content-type": mimeType });
90
+ (0, import_node_fs.createReadStream)(filePath).pipe(res);
91
+ });
92
+ server.on("close", () => {
93
+ process.exit(0);
94
+ });
95
+ let serverPort = Number(port) || defaultPort;
96
+ let serverHost = host || defaultHost;
97
+ server.listen(serverPort, serverHost, () => {
98
+ console.log(`Server running at http://${serverHost}:${serverPort}`);
99
+ resolve(server);
100
+ });
82
101
  });
83
- let serverPort = Number(port) || defaultPort;
84
- let serverHost = host || defaultHost;
85
- server.listen(serverPort, serverHost);
86
- console.log(`Server running at http://${serverHost}:${serverPort}`);
87
- return server;
88
102
  }
89
103
 
90
104
  // src/run.ts
91
- var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
92
105
  async function run() {
93
106
  let [url, ...args] = process.argv.slice(2);
94
107
  let spa = false;
@@ -96,25 +109,24 @@ async function run() {
96
109
  spa = true;
97
110
  args.shift();
98
111
  }
99
- let buildFlagIndex = args.indexOf("-b");
112
+ let bundleFlagIndex = args.indexOf("-b");
100
113
  let path = args[0];
101
- let dirs = args.slice(
102
- 1,
103
- buildFlagIndex === -1 ? args.length : buildFlagIndex
104
- );
105
- if (buildFlagIndex !== -1) {
106
- let inputFile = (0, import_node_path3.join)(path, args[buildFlagIndex + 1] ?? "index.ts");
107
- let outputFile = (0, import_node_path3.join)(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
108
- await (0, import_promises2.rm)((0, import_node_path3.join)(path, "dist"), { recursive: true, force: true });
109
- await exec(
110
- `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
111
- );
114
+ let dirs;
115
+ let bundle2;
116
+ if (bundleFlagIndex === -1) dirs = args.slice(1);
117
+ else {
118
+ dirs = args.slice(1, bundleFlagIndex);
119
+ bundle2 = {
120
+ input: args[bundleFlagIndex + 1],
121
+ output: args[bundleFlagIndex + 2]
122
+ };
112
123
  }
113
- serve({
124
+ await serve({
114
125
  url,
115
126
  path,
116
127
  dirs,
117
- spa
128
+ spa,
129
+ bundle: bundle2
118
130
  });
119
131
  }
120
132
  run();
package/dist/run.mjs CHANGED
@@ -1,18 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // src/run.ts
4
- import { exec as originalExec } from "node:child_process";
5
- import { rm } from "node:fs/promises";
6
- import { join as join2 } from "node:path";
7
- import { promisify } from "node:util";
8
-
9
3
  // src/serve.ts
10
4
  import { createReadStream } from "node:fs";
11
5
  import { createServer } from "node:http";
12
6
  import { extname } from "node:path";
13
7
 
14
- // src/getFilePath.ts
8
+ // src/bundle.ts
9
+ import { exec as originalExec } from "node:child_process";
10
+ import { rm } from "node:fs/promises";
15
11
  import { join } from "node:path";
12
+ import { promisify } from "node:util";
13
+ var exec = promisify(originalExec);
14
+ async function bundle({ path = "", bundle: options } = {}) {
15
+ if (!options) return;
16
+ let inputFile = join(path, options.input ?? "index.ts");
17
+ let outputFile = join(path, "dist", options.output ?? "index.js");
18
+ await rm(join(path, "dist"), { recursive: true, force: true });
19
+ await exec(
20
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
21
+ );
22
+ }
23
+
24
+ // src/getFilePath.ts
25
+ import { join as join2 } from "node:path";
16
26
 
17
27
  // src/isValidFilePath.ts
18
28
  import { access, lstat } from "node:fs/promises";
@@ -31,11 +41,11 @@ var cwd = process.cwd();
31
41
  async function getFilePath(url = "", { path = "", dirs = [], spa }) {
32
42
  let urlPath = url.replace(/[?#].*$/, "");
33
43
  for (let dir of dirs.length === 0 ? [""] : dirs) {
34
- let dirPath = join(cwd, path, dir);
35
- let filePath = join(dirPath, urlPath);
44
+ let dirPath = join2(cwd, path, dir);
45
+ let filePath = join2(dirPath, urlPath);
36
46
  if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
37
47
  return filePath;
38
- filePath = join(dirPath, spa ? "" : urlPath, "index.html");
48
+ filePath = join2(dirPath, spa ? "" : urlPath, "index.html");
39
49
  if (await isValidFilePath(filePath, dirPath)) return filePath;
40
50
  }
41
51
  }
@@ -58,36 +68,39 @@ var mimeTypes = {
58
68
  // src/serve.ts
59
69
  var defaultHost = "localhost";
60
70
  var defaultPort = 3e3;
61
- function serve(config = {}) {
71
+ async function serve(config = {}) {
62
72
  let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
63
73
  if (!port && /^\d+$/.test(host)) {
64
74
  port = host;
65
75
  host = defaultHost;
66
76
  }
67
- let server = createServer(async (req, res) => {
68
- let filePath = await getFilePath(req.url, config);
69
- if (filePath === void 0) {
70
- res.writeHead(404, { "content-type": "text/plain" });
71
- res.end("Not found");
72
- return;
73
- }
74
- let ext = extname(filePath).slice(1).toLowerCase();
75
- let mimeType = mimeTypes[ext] ?? "application/octet-stream";
76
- res.writeHead(200, { "content-type": mimeType });
77
- createReadStream(filePath).pipe(res);
78
- });
79
- server.on("close", () => {
80
- process.exit(0);
77
+ await bundle(config);
78
+ return new Promise((resolve) => {
79
+ let server = createServer(async (req, res) => {
80
+ let filePath = await getFilePath(req.url, config);
81
+ if (filePath === void 0) {
82
+ res.writeHead(404, { "content-type": "text/plain" });
83
+ res.end("Not found");
84
+ return;
85
+ }
86
+ let ext = extname(filePath).slice(1).toLowerCase();
87
+ let mimeType = mimeTypes[ext] ?? "application/octet-stream";
88
+ res.writeHead(200, { "content-type": mimeType });
89
+ createReadStream(filePath).pipe(res);
90
+ });
91
+ server.on("close", () => {
92
+ process.exit(0);
93
+ });
94
+ let serverPort = Number(port) || defaultPort;
95
+ let serverHost = host || defaultHost;
96
+ server.listen(serverPort, serverHost, () => {
97
+ console.log(`Server running at http://${serverHost}:${serverPort}`);
98
+ resolve(server);
99
+ });
81
100
  });
82
- let serverPort = Number(port) || defaultPort;
83
- let serverHost = host || defaultHost;
84
- server.listen(serverPort, serverHost);
85
- console.log(`Server running at http://${serverHost}:${serverPort}`);
86
- return server;
87
101
  }
88
102
 
89
103
  // src/run.ts
90
- var exec = promisify(originalExec);
91
104
  async function run() {
92
105
  let [url, ...args] = process.argv.slice(2);
93
106
  let spa = false;
@@ -95,25 +108,24 @@ async function run() {
95
108
  spa = true;
96
109
  args.shift();
97
110
  }
98
- let buildFlagIndex = args.indexOf("-b");
111
+ let bundleFlagIndex = args.indexOf("-b");
99
112
  let path = args[0];
100
- let dirs = args.slice(
101
- 1,
102
- buildFlagIndex === -1 ? args.length : buildFlagIndex
103
- );
104
- if (buildFlagIndex !== -1) {
105
- let inputFile = join2(path, args[buildFlagIndex + 1] ?? "index.ts");
106
- let outputFile = join2(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
107
- await rm(join2(path, "dist"), { recursive: true, force: true });
108
- await exec(
109
- `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
110
- );
113
+ let dirs;
114
+ let bundle2;
115
+ if (bundleFlagIndex === -1) dirs = args.slice(1);
116
+ else {
117
+ dirs = args.slice(1, bundleFlagIndex);
118
+ bundle2 = {
119
+ input: args[bundleFlagIndex + 1],
120
+ output: args[bundleFlagIndex + 2]
121
+ };
111
122
  }
112
- serve({
123
+ await serve({
113
124
  url,
114
125
  path,
115
126
  dirs,
116
- spa
127
+ spa,
128
+ bundle: bundle2
117
129
  });
118
130
  }
119
131
  run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/serve",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "node",
package/src/Config.ts CHANGED
@@ -3,4 +3,8 @@ export type Config = {
3
3
  path?: string;
4
4
  dirs?: string[];
5
5
  spa?: boolean;
6
+ bundle?: {
7
+ input?: string;
8
+ output?: string;
9
+ };
6
10
  };
package/src/bundle.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { exec as originalExec } from "node:child_process";
2
+ import { rm } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { promisify } from "node:util";
5
+ import type { Config } from "./Config";
6
+
7
+ const exec = promisify(originalExec);
8
+
9
+ export async function bundle({ path = "", bundle: options }: Config = {}) {
10
+ if (!options) return;
11
+
12
+ let inputFile = join(path, options.input ?? "index.ts");
13
+ let outputFile = join(path, "dist", options.output ?? "index.js");
14
+
15
+ await rm(join(path, "dist"), { recursive: true, force: true });
16
+ await exec(
17
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`,
18
+ );
19
+ }
package/src/run.ts CHANGED
@@ -1,12 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { exec as originalExec } from "node:child_process";
3
- import { rm } from "node:fs/promises";
4
- import { join } from "node:path";
5
- import { promisify } from "node:util";
2
+ import type { Config } from "./Config";
6
3
  import { serve } from "./serve";
7
4
 
8
- const exec = promisify(originalExec);
9
-
10
5
  async function run() {
11
6
  let [url, ...args] = process.argv.slice(2);
12
7
  let spa = false;
@@ -16,28 +11,27 @@ async function run() {
16
11
  args.shift();
17
12
  }
18
13
 
19
- let buildFlagIndex = args.indexOf("-b");
14
+ let bundleFlagIndex = args.indexOf("-b");
20
15
  let path = args[0];
21
- let dirs = args.slice(
22
- 1,
23
- buildFlagIndex === -1 ? args.length : buildFlagIndex,
24
- );
25
16
 
26
- if (buildFlagIndex !== -1) {
27
- let inputFile = join(path, args[buildFlagIndex + 1] ?? "index.ts");
28
- let outputFile = join(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
17
+ let dirs: string[];
18
+ let bundle: Config["bundle"];
29
19
 
30
- await rm(join(path, "dist"), { recursive: true, force: true });
31
- await exec(
32
- `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`,
33
- );
20
+ if (bundleFlagIndex === -1) dirs = args.slice(1);
21
+ else {
22
+ dirs = args.slice(1, bundleFlagIndex);
23
+ bundle = {
24
+ input: args[bundleFlagIndex + 1],
25
+ output: args[bundleFlagIndex + 2],
26
+ };
34
27
  }
35
28
 
36
- serve({
29
+ await serve({
37
30
  url,
38
31
  path,
39
32
  dirs,
40
33
  spa,
34
+ bundle,
41
35
  });
42
36
  }
43
37
 
package/src/serve.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createReadStream } from "node:fs";
2
2
  import { createServer } from "node:http";
3
3
  import { extname } from "node:path";
4
+ import { bundle } from "./bundle";
4
5
  import type { Config } from "./Config";
5
6
  import { getFilePath } from "./getFilePath";
6
7
  import { mimeTypes } from "./mimeTypes";
@@ -8,7 +9,9 @@ import { mimeTypes } from "./mimeTypes";
8
9
  const defaultHost = "localhost";
9
10
  const defaultPort = 3000;
10
11
 
11
- export function serve(config: Config = {}) {
12
+ export type Server = ReturnType<typeof createServer>;
13
+
14
+ export async function serve(config: Config = {}): Promise<Server> {
12
15
  let [, , host, , port] =
13
16
  config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
14
17
 
@@ -17,32 +20,35 @@ export function serve(config: Config = {}) {
17
20
  host = defaultHost;
18
21
  }
19
22
 
20
- let server = createServer(async (req, res) => {
21
- let filePath = await getFilePath(req.url, config);
23
+ await bundle(config);
22
24
 
23
- if (filePath === undefined) {
24
- res.writeHead(404, { "content-type": "text/plain" });
25
- res.end("Not found");
26
- return;
27
- }
25
+ return new Promise((resolve) => {
26
+ let server = createServer(async (req, res) => {
27
+ let filePath = await getFilePath(req.url, config);
28
28
 
29
- let ext = extname(filePath).slice(1).toLowerCase();
30
- let mimeType = mimeTypes[ext] ?? "application/octet-stream";
29
+ if (filePath === undefined) {
30
+ res.writeHead(404, { "content-type": "text/plain" });
31
+ res.end("Not found");
32
+ return;
33
+ }
31
34
 
32
- res.writeHead(200, { "content-type": mimeType });
33
- createReadStream(filePath).pipe(res);
34
- });
35
+ let ext = extname(filePath).slice(1).toLowerCase();
36
+ let mimeType = mimeTypes[ext] ?? "application/octet-stream";
35
37
 
36
- server.on("close", () => {
37
- process.exit(0);
38
- });
38
+ res.writeHead(200, { "content-type": mimeType });
39
+ createReadStream(filePath).pipe(res);
40
+ });
39
41
 
40
- let serverPort = Number(port) || defaultPort;
41
- let serverHost = host || defaultHost;
42
+ server.on("close", () => {
43
+ process.exit(0);
44
+ });
42
45
 
43
- server.listen(serverPort, serverHost);
46
+ let serverPort = Number(port) || defaultPort;
47
+ let serverHost = host || defaultHost;
44
48
 
45
- console.log(`Server running at http://${serverHost}:${serverPort}`);
46
-
47
- return server;
49
+ server.listen(serverPort, serverHost, () => {
50
+ console.log(`Server running at http://${serverHost}:${serverPort}`);
51
+ resolve(server);
52
+ });
53
+ });
48
54
  }