@t8/serve 0.1.13 → 0.1.15

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
@@ -49,6 +49,22 @@ async function getFilePath(url = "", { path = "", dirs = [], spa }) {
49
49
  }
50
50
  }
51
51
 
52
+ // src/getTarget.ts
53
+ var defaultHost = "localhost";
54
+ var defaultPort = 3e3;
55
+ function getTarget(config = {}) {
56
+ let { host, port, url } = config;
57
+ let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
58
+ if (!urlPort && /^\d+$/.test(urlHost)) {
59
+ urlPort = urlHost;
60
+ urlHost = "";
61
+ }
62
+ return {
63
+ port: port || Number(urlPort) || defaultPort,
64
+ host: host || urlHost || defaultHost
65
+ };
66
+ }
67
+
52
68
  // src/mimeTypes.ts
53
69
  var mimeTypes = {
54
70
  html: "text/html; charset=utf-8",
@@ -65,14 +81,7 @@ var mimeTypes = {
65
81
  };
66
82
 
67
83
  // src/serve.ts
68
- var defaultHost = "localhost";
69
- var defaultPort = 3e3;
70
84
  async function serve(config = {}) {
71
- let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
72
- if (!port && /^\d+$/.test(host)) {
73
- port = host;
74
- host = defaultHost;
75
- }
76
85
  await bundle(config);
77
86
  return new Promise((resolve) => {
78
87
  let server = createServer(async (req, res) => {
@@ -87,10 +96,9 @@ async function serve(config = {}) {
87
96
  res.writeHead(200, { "content-type": mimeType });
88
97
  createReadStream(filePath).pipe(res);
89
98
  });
90
- let serverPort = Number(port) || defaultPort;
91
- let serverHost = host || defaultHost;
92
- server.listen(serverPort, serverHost, () => {
93
- console.log(`Server running at http://${serverHost}:${serverPort}`);
99
+ let { host, port } = getTarget(config);
100
+ server.listen(port, host, () => {
101
+ if (config.log) console.log(`Server running at http://${host}:${port}`);
94
102
  resolve(server);
95
103
  });
96
104
  });
package/dist/run.cjs CHANGED
@@ -52,6 +52,22 @@ async function getFilePath(url = "", { path = "", dirs = [], spa }) {
52
52
  }
53
53
  }
54
54
 
55
+ // src/getTarget.ts
56
+ var defaultHost = "localhost";
57
+ var defaultPort = 3e3;
58
+ function getTarget(config = {}) {
59
+ let { host, port, url } = config;
60
+ let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
61
+ if (!urlPort && /^\d+$/.test(urlHost)) {
62
+ urlPort = urlHost;
63
+ urlHost = "";
64
+ }
65
+ return {
66
+ port: port || Number(urlPort) || defaultPort,
67
+ host: host || urlHost || defaultHost
68
+ };
69
+ }
70
+
55
71
  // src/mimeTypes.ts
56
72
  var mimeTypes = {
57
73
  html: "text/html; charset=utf-8",
@@ -68,14 +84,7 @@ var mimeTypes = {
68
84
  };
69
85
 
70
86
  // src/serve.ts
71
- var defaultHost = "localhost";
72
- var defaultPort = 3e3;
73
87
  async function serve(config = {}) {
74
- let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
75
- if (!port && /^\d+$/.test(host)) {
76
- port = host;
77
- host = defaultHost;
78
- }
79
88
  await bundle(config);
80
89
  return new Promise((resolve) => {
81
90
  let server = (0, import_node_http.createServer)(async (req, res) => {
@@ -90,10 +99,9 @@ async function serve(config = {}) {
90
99
  res.writeHead(200, { "content-type": mimeType });
91
100
  (0, import_node_fs.createReadStream)(filePath).pipe(res);
92
101
  });
93
- let serverPort = Number(port) || defaultPort;
94
- let serverHost = host || defaultHost;
95
- server.listen(serverPort, serverHost, () => {
96
- console.log(`Server running at http://${serverHost}:${serverPort}`);
102
+ let { host, port } = getTarget(config);
103
+ server.listen(port, host, () => {
104
+ if (config.log) console.log(`Server running at http://${host}:${port}`);
97
105
  resolve(server);
98
106
  });
99
107
  });
@@ -124,7 +132,8 @@ async function run() {
124
132
  path,
125
133
  dirs,
126
134
  spa,
127
- bundle: bundle2
135
+ bundle: bundle2,
136
+ log: true
128
137
  });
129
138
  }
130
139
  run();
package/dist/run.mjs CHANGED
@@ -51,6 +51,22 @@ async function getFilePath(url = "", { path = "", dirs = [], spa }) {
51
51
  }
52
52
  }
53
53
 
54
+ // src/getTarget.ts
55
+ var defaultHost = "localhost";
56
+ var defaultPort = 3e3;
57
+ function getTarget(config = {}) {
58
+ let { host, port, url } = config;
59
+ let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
60
+ if (!urlPort && /^\d+$/.test(urlHost)) {
61
+ urlPort = urlHost;
62
+ urlHost = "";
63
+ }
64
+ return {
65
+ port: port || Number(urlPort) || defaultPort,
66
+ host: host || urlHost || defaultHost
67
+ };
68
+ }
69
+
54
70
  // src/mimeTypes.ts
55
71
  var mimeTypes = {
56
72
  html: "text/html; charset=utf-8",
@@ -67,14 +83,7 @@ var mimeTypes = {
67
83
  };
68
84
 
69
85
  // src/serve.ts
70
- var defaultHost = "localhost";
71
- var defaultPort = 3e3;
72
86
  async function serve(config = {}) {
73
- let [, , host, , port] = config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
74
- if (!port && /^\d+$/.test(host)) {
75
- port = host;
76
- host = defaultHost;
77
- }
78
87
  await bundle(config);
79
88
  return new Promise((resolve) => {
80
89
  let server = createServer(async (req, res) => {
@@ -89,10 +98,9 @@ async function serve(config = {}) {
89
98
  res.writeHead(200, { "content-type": mimeType });
90
99
  createReadStream(filePath).pipe(res);
91
100
  });
92
- let serverPort = Number(port) || defaultPort;
93
- let serverHost = host || defaultHost;
94
- server.listen(serverPort, serverHost, () => {
95
- console.log(`Server running at http://${serverHost}:${serverPort}`);
101
+ let { host, port } = getTarget(config);
102
+ server.listen(port, host, () => {
103
+ if (config.log) console.log(`Server running at http://${host}:${port}`);
96
104
  resolve(server);
97
105
  });
98
106
  });
@@ -123,7 +131,8 @@ async function run() {
123
131
  path,
124
132
  dirs,
125
133
  spa,
126
- bundle: bundle2
134
+ bundle: bundle2,
135
+ log: true
127
136
  });
128
137
  }
129
138
  run();
package/index.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from "./src/BundleConfig";
1
2
  export * from "./src/Config";
2
3
  export * from "./src/serve";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/serve",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "node",
@@ -0,0 +1,4 @@
1
+ export type BundleConfig = {
2
+ input?: string;
3
+ output?: string;
4
+ };
package/src/Config.ts CHANGED
@@ -1,12 +1,12 @@
1
+ import type { BundleConfig } from "./BundleConfig";
2
+
1
3
  export type Config = {
2
4
  url?: string;
5
+ host?: string;
6
+ port?: number;
3
7
  path?: string;
4
8
  dirs?: string[];
5
9
  spa?: boolean;
6
- bundle?:
7
- | boolean
8
- | {
9
- input?: string;
10
- output?: string;
11
- };
10
+ log?: boolean;
11
+ bundle?: boolean | BundleConfig;
12
12
  };
package/src/bundle.ts CHANGED
@@ -2,6 +2,7 @@ import { exec as originalExec } from "node:child_process";
2
2
  import { rm } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { promisify } from "node:util";
5
+ import type { BundleConfig } from "./BundleConfig";
5
6
  import type { Config } from "./Config";
6
7
 
7
8
  const exec = promisify(originalExec);
@@ -9,7 +10,7 @@ const exec = promisify(originalExec);
9
10
  export async function bundle({ path = "", bundle: options }: Config = {}) {
10
11
  if (!options) return;
11
12
 
12
- let normalizedOptions: Exclude<Config["bundle"], boolean | undefined> =
13
+ let normalizedOptions: BundleConfig =
13
14
  typeof options === "boolean" ? {} : options;
14
15
 
15
16
  let inputFile = join(path, normalizedOptions.input ?? "index.ts");
@@ -0,0 +1,21 @@
1
+ import type { Config } from "./Config";
2
+
3
+ const defaultHost = "localhost";
4
+ const defaultPort = 3000;
5
+
6
+ export function getTarget(config: Config = {}) {
7
+ let { host, port, url } = config;
8
+
9
+ let [, , urlHost, , urlPort] =
10
+ url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
11
+
12
+ if (!urlPort && /^\d+$/.test(urlHost)) {
13
+ urlPort = urlHost;
14
+ urlHost = "";
15
+ }
16
+
17
+ return {
18
+ port: port || Number(urlPort) || defaultPort,
19
+ host: host || urlHost || defaultHost,
20
+ };
21
+ }
package/src/run.ts CHANGED
@@ -32,6 +32,7 @@ async function run() {
32
32
  dirs,
33
33
  spa,
34
34
  bundle,
35
+ log: true,
35
36
  });
36
37
  }
37
38
 
package/src/serve.ts CHANGED
@@ -4,22 +4,12 @@ import { extname } from "node:path";
4
4
  import { bundle } from "./bundle";
5
5
  import type { Config } from "./Config";
6
6
  import { getFilePath } from "./getFilePath";
7
+ import { getTarget } from "./getTarget";
7
8
  import { mimeTypes } from "./mimeTypes";
8
9
 
9
- const defaultHost = "localhost";
10
- const defaultPort = 3000;
11
-
12
10
  export type Server = ReturnType<typeof createServer>;
13
11
 
14
12
  export async function serve(config: Config = {}): Promise<Server> {
15
- let [, , host, , port] =
16
- config.url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
17
-
18
- if (!port && /^\d+$/.test(host)) {
19
- port = host;
20
- host = defaultHost;
21
- }
22
-
23
13
  await bundle(config);
24
14
 
25
15
  return new Promise((resolve) => {
@@ -39,11 +29,11 @@ export async function serve(config: Config = {}): Promise<Server> {
39
29
  createReadStream(filePath).pipe(res);
40
30
  });
41
31
 
42
- let serverPort = Number(port) || defaultPort;
43
- let serverHost = host || defaultHost;
32
+ let { host, port } = getTarget(config);
33
+
34
+ server.listen(port, host, () => {
35
+ if (config.log) console.log(`Server running at http://${host}:${port}`);
44
36
 
45
- server.listen(serverPort, serverHost, () => {
46
- console.log(`Server running at http://${serverHost}:${serverPort}`);
47
37
  resolve(server);
48
38
  });
49
39
  });