@t8/serve 0.1.14 → 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 +19 -12
- package/dist/run.cjs +21 -13
- package/dist/run.mjs +21 -13
- package/package.json +1 -1
- package/src/Config.ts +3 -1
- package/src/getTarget.ts +21 -0
- package/src/run.ts +1 -0
- package/src/serve.ts +4 -16
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,11 +96,9 @@ async function serve(config = {}) {
|
|
|
87
96
|
res.writeHead(200, { "content-type": mimeType });
|
|
88
97
|
createReadStream(filePath).pipe(res);
|
|
89
98
|
});
|
|
90
|
-
let
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (!config.silent)
|
|
94
|
-
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}`);
|
|
95
102
|
resolve(server);
|
|
96
103
|
});
|
|
97
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,11 +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
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (!config.silent)
|
|
97
|
-
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}`);
|
|
98
105
|
resolve(server);
|
|
99
106
|
});
|
|
100
107
|
});
|
|
@@ -125,7 +132,8 @@ async function run() {
|
|
|
125
132
|
path,
|
|
126
133
|
dirs,
|
|
127
134
|
spa,
|
|
128
|
-
bundle: bundle2
|
|
135
|
+
bundle: bundle2,
|
|
136
|
+
log: true
|
|
129
137
|
});
|
|
130
138
|
}
|
|
131
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,11 +98,9 @@ async function serve(config = {}) {
|
|
|
89
98
|
res.writeHead(200, { "content-type": mimeType });
|
|
90
99
|
createReadStream(filePath).pipe(res);
|
|
91
100
|
});
|
|
92
|
-
let
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (!config.silent)
|
|
96
|
-
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}`);
|
|
97
104
|
resolve(server);
|
|
98
105
|
});
|
|
99
106
|
});
|
|
@@ -124,7 +131,8 @@ async function run() {
|
|
|
124
131
|
path,
|
|
125
132
|
dirs,
|
|
126
133
|
spa,
|
|
127
|
-
bundle: bundle2
|
|
134
|
+
bundle: bundle2,
|
|
135
|
+
log: true
|
|
128
136
|
});
|
|
129
137
|
}
|
|
130
138
|
run();
|
package/package.json
CHANGED
package/src/Config.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { BundleConfig } from "./BundleConfig";
|
|
|
2
2
|
|
|
3
3
|
export type Config = {
|
|
4
4
|
url?: string;
|
|
5
|
+
host?: string;
|
|
6
|
+
port?: number;
|
|
5
7
|
path?: string;
|
|
6
8
|
dirs?: string[];
|
|
7
9
|
spa?: boolean;
|
|
8
|
-
|
|
10
|
+
log?: boolean;
|
|
9
11
|
bundle?: boolean | BundleConfig;
|
|
10
12
|
};
|
package/src/getTarget.ts
ADDED
|
@@ -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
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,12 +29,10 @@ export async function serve(config: Config = {}): Promise<Server> {
|
|
|
39
29
|
createReadStream(filePath).pipe(res);
|
|
40
30
|
});
|
|
41
31
|
|
|
42
|
-
let
|
|
43
|
-
let serverHost = host || defaultHost;
|
|
32
|
+
let { host, port } = getTarget(config);
|
|
44
33
|
|
|
45
|
-
server.listen(
|
|
46
|
-
if (
|
|
47
|
-
console.log(`Server running at http://${serverHost}:${serverPort}`);
|
|
34
|
+
server.listen(port, host, () => {
|
|
35
|
+
if (config.log) console.log(`Server running at http://${host}:${port}`);
|
|
48
36
|
|
|
49
37
|
resolve(server);
|
|
50
38
|
});
|