@t8/serve 0.1.36 → 0.1.38
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.cjs +120 -0
- package/dist/index.d.ts +73 -3
- package/dist/{index.js → index.mjs} +24 -47
- package/package.json +11 -17
- package/tsconfig.json +1 -1
- package/dist/run.cjs +0 -190
- package/dist/src/BundleConfig.d.ts +0 -20
- package/dist/src/Config.d.ts +0 -47
- package/dist/src/bundle.d.ts +0 -2
- package/dist/src/getFilePath.d.ts +0 -2
- package/dist/src/getTarget.d.ts +0 -5
- package/dist/src/isValidFilePath.d.ts +0 -1
- package/dist/src/mimeTypes.d.ts +0 -1
- package/dist/src/run.d.ts +0 -2
- package/dist/src/serve.d.ts +0 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
let node_fs = require("node:fs");
|
|
2
|
+
let node_http = require("node:http");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
let node_fs_promises = require("node:fs/promises");
|
|
5
|
+
let esbuild = require("esbuild");
|
|
6
|
+
|
|
7
|
+
async function bundle({ path = "", bundle: options, watch, minify } = {}) {
|
|
8
|
+
if (!options) return;
|
|
9
|
+
let normalizedOptions;
|
|
10
|
+
if (typeof options === "boolean") normalizedOptions = {};
|
|
11
|
+
else if (typeof options === "string") normalizedOptions = { input: options };
|
|
12
|
+
else normalizedOptions = options;
|
|
13
|
+
let dir = normalizedOptions.dir ?? "dist";
|
|
14
|
+
let inputFile = (0, node_path.join)(path, normalizedOptions.input ?? "index.ts");
|
|
15
|
+
await (0, node_fs_promises.rm)((0, node_path.join)(path, dir), {
|
|
16
|
+
recursive: true,
|
|
17
|
+
force: true
|
|
18
|
+
});
|
|
19
|
+
let buildOptions = {
|
|
20
|
+
entryPoints: [inputFile],
|
|
21
|
+
bundle: true,
|
|
22
|
+
format: "esm",
|
|
23
|
+
platform: "browser",
|
|
24
|
+
logLevel: "warning",
|
|
25
|
+
minify
|
|
26
|
+
};
|
|
27
|
+
if (normalizedOptions.output) buildOptions.outfile = (0, node_path.join)(path, dir, normalizedOptions.output);
|
|
28
|
+
else {
|
|
29
|
+
buildOptions.outdir = (0, node_path.join)(path, dir);
|
|
30
|
+
buildOptions.splitting = true;
|
|
31
|
+
}
|
|
32
|
+
if (watch) {
|
|
33
|
+
let ctx = await (0, esbuild.context)(buildOptions);
|
|
34
|
+
await ctx.watch();
|
|
35
|
+
return async () => {
|
|
36
|
+
await ctx.dispose();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
await (0, esbuild.build)(buildOptions);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function isValidFilePath(filePath, dirPath) {
|
|
43
|
+
if (!filePath.startsWith(dirPath)) return false;
|
|
44
|
+
try {
|
|
45
|
+
await (0, node_fs_promises.access)(filePath);
|
|
46
|
+
return !(await (0, node_fs_promises.lstat)(filePath)).isDirectory();
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const cwd = process.cwd();
|
|
53
|
+
async function getFilePath(url = "", { path = "", dirs = [], spa }) {
|
|
54
|
+
let urlPath = url.replace(/[?#].*$/, "");
|
|
55
|
+
for (let dir of dirs.length === 0 ? [""] : dirs) {
|
|
56
|
+
let dirPath = (0, node_path.join)(cwd, path, dir);
|
|
57
|
+
let filePath = (0, node_path.join)(dirPath, urlPath);
|
|
58
|
+
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath)) return filePath;
|
|
59
|
+
filePath = (0, node_path.join)(dirPath, spa ? "" : urlPath, "index.html");
|
|
60
|
+
if (await isValidFilePath(filePath, dirPath)) return filePath;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const defaultHost = "localhost";
|
|
65
|
+
const defaultPort = 3e3;
|
|
66
|
+
function getTarget(config = {}) {
|
|
67
|
+
let { host, port, url } = config;
|
|
68
|
+
let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
|
|
69
|
+
if (!urlPort && /^\d+$/.test(urlHost)) {
|
|
70
|
+
urlPort = urlHost;
|
|
71
|
+
urlHost = "";
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
port: port || Number(urlPort) || defaultPort,
|
|
75
|
+
host: host || urlHost || defaultHost
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const mimeTypes = {
|
|
80
|
+
html: "text/html; charset=utf-8",
|
|
81
|
+
js: "text/javascript",
|
|
82
|
+
json: "application/json",
|
|
83
|
+
css: "text/css",
|
|
84
|
+
svg: "image/svg+xml",
|
|
85
|
+
png: "image/png",
|
|
86
|
+
jpg: "image/jpeg",
|
|
87
|
+
gif: "image/gif",
|
|
88
|
+
ico: "image/x-icon",
|
|
89
|
+
txt: "text/plain",
|
|
90
|
+
md: "text/markdown"
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
async function serve(config = {}) {
|
|
94
|
+
let stop = await bundle(config);
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
let server = (0, node_http.createServer)(async (req, res) => {
|
|
97
|
+
await config.onRequest?.(req, res);
|
|
98
|
+
if (res.headersSent) return;
|
|
99
|
+
let filePath = await getFilePath(req.url, config);
|
|
100
|
+
if (filePath === void 0) {
|
|
101
|
+
res.writeHead(404, { "content-type": "text/plain" });
|
|
102
|
+
res.end("Not found");
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
let mimeType = mimeTypes[(0, node_path.extname)(filePath).slice(1).toLowerCase()] ?? "application/octet-stream";
|
|
106
|
+
res.writeHead(200, { "content-type": mimeType });
|
|
107
|
+
(0, node_fs.createReadStream)(filePath).pipe(res);
|
|
108
|
+
});
|
|
109
|
+
if (stop) server.on("close", async () => {
|
|
110
|
+
await stop();
|
|
111
|
+
});
|
|
112
|
+
let { host, port } = getTarget(config);
|
|
113
|
+
server.listen(port, host, () => {
|
|
114
|
+
if (config.log) console.log(`Server running at http://${host}:${port}`);
|
|
115
|
+
resolve(server);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.serve = serve;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { IncomingMessage, ServerResponse, createServer } from "node:http";
|
|
2
|
+
|
|
3
|
+
type BundleConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* Output directory path relative to the server config `path`.
|
|
6
|
+
*
|
|
7
|
+
* @defaultValue "dist"
|
|
8
|
+
*/
|
|
9
|
+
dir?: string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Input path relative to the server config `path`.
|
|
12
|
+
*
|
|
13
|
+
* @defaultValue "index.ts"
|
|
14
|
+
*/
|
|
15
|
+
input?: string | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Output path relative to the bundle config `dir`.
|
|
18
|
+
*
|
|
19
|
+
* @defaultValue "index.js"
|
|
20
|
+
*/
|
|
21
|
+
output?: string | undefined;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type Config = {
|
|
25
|
+
/** Server URL. */
|
|
26
|
+
url?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Server host.
|
|
29
|
+
*
|
|
30
|
+
* @defaultValue "localhost"
|
|
31
|
+
*/
|
|
32
|
+
host?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Server port.
|
|
35
|
+
*
|
|
36
|
+
* @defaultValue 3000
|
|
37
|
+
*/
|
|
38
|
+
port?: number;
|
|
39
|
+
/** Application path. */
|
|
40
|
+
path?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Public assets directories. If not provided, the application
|
|
43
|
+
* `path` is served as a public assets directory.
|
|
44
|
+
*/
|
|
45
|
+
dirs?: string[];
|
|
46
|
+
/**
|
|
47
|
+
* Enables single-page application (SPA) mode. If `true`, all
|
|
48
|
+
* unmatched URLs are served as "/".
|
|
49
|
+
*/
|
|
50
|
+
spa?: boolean;
|
|
51
|
+
/** Whether to rebuild whenever the bundled files change. */
|
|
52
|
+
watch?: boolean;
|
|
53
|
+
/** Whether the bundle should be minified. */
|
|
54
|
+
minify?: boolean;
|
|
55
|
+
/** Whether to log to the console. */
|
|
56
|
+
log?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Bundle config.
|
|
59
|
+
*
|
|
60
|
+
* If `undefined`, bundling is skipped.
|
|
61
|
+
* Otherwise, its type is `BundleConfig`, with the following shorthand options:
|
|
62
|
+
* If `true`, it's equivalent to `{ input: "index.ts", output: "index.js" }`.
|
|
63
|
+
* If `string`, it's equivalent to `input` in `{ input, ouput: "index.js" }`.
|
|
64
|
+
*/
|
|
65
|
+
bundle?: boolean | string | BundleConfig | undefined;
|
|
66
|
+
/** Custom request handler. */
|
|
67
|
+
onRequest?: (req?: IncomingMessage, res?: ServerResponse<IncomingMessage>) => void | Promise<void>;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
type Server = ReturnType<typeof createServer>;
|
|
71
|
+
declare function serve(config?: Config): Promise<Server>;
|
|
72
|
+
|
|
73
|
+
export { BundleConfig, Config, Server, serve };
|
|
@@ -1,29 +1,21 @@
|
|
|
1
|
-
// src/serve.ts
|
|
2
1
|
import { createReadStream } from "node:fs";
|
|
3
2
|
import { createServer } from "node:http";
|
|
4
|
-
import { extname } from "node:path";
|
|
5
|
-
|
|
6
|
-
// src/bundle.ts
|
|
7
|
-
import { rm } from "node:fs/promises";
|
|
8
|
-
import { join } from "node:path";
|
|
3
|
+
import { extname, join } from "node:path";
|
|
4
|
+
import { access, lstat, rm } from "node:fs/promises";
|
|
9
5
|
import { build, context } from "esbuild";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
bundle: options,
|
|
13
|
-
watch,
|
|
14
|
-
minify
|
|
15
|
-
} = {}) {
|
|
6
|
+
|
|
7
|
+
async function bundle({ path = "", bundle: options, watch, minify } = {}) {
|
|
16
8
|
if (!options) return;
|
|
17
9
|
let normalizedOptions;
|
|
18
10
|
if (typeof options === "boolean") normalizedOptions = {};
|
|
19
|
-
else if (typeof options === "string")
|
|
20
|
-
normalizedOptions = {
|
|
21
|
-
input: options
|
|
22
|
-
};
|
|
11
|
+
else if (typeof options === "string") normalizedOptions = { input: options };
|
|
23
12
|
else normalizedOptions = options;
|
|
24
13
|
let dir = normalizedOptions.dir ?? "dist";
|
|
25
14
|
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
26
|
-
await rm(join(path, dir), {
|
|
15
|
+
await rm(join(path, dir), {
|
|
16
|
+
recursive: true,
|
|
17
|
+
force: true
|
|
18
|
+
});
|
|
27
19
|
let buildOptions = {
|
|
28
20
|
entryPoints: [inputFile],
|
|
29
21
|
bundle: true,
|
|
@@ -32,8 +24,7 @@ async function bundle({
|
|
|
32
24
|
logLevel: "warning",
|
|
33
25
|
minify
|
|
34
26
|
};
|
|
35
|
-
if (normalizedOptions.output)
|
|
36
|
-
buildOptions.outfile = join(path, dir, normalizedOptions.output);
|
|
27
|
+
if (normalizedOptions.output) buildOptions.outfile = join(path, dir, normalizedOptions.output);
|
|
37
28
|
else {
|
|
38
29
|
buildOptions.outdir = join(path, dir);
|
|
39
30
|
buildOptions.splitting = true;
|
|
@@ -48,11 +39,6 @@ async function bundle({
|
|
|
48
39
|
await build(buildOptions);
|
|
49
40
|
}
|
|
50
41
|
|
|
51
|
-
// src/getFilePath.ts
|
|
52
|
-
import { join as join2 } from "node:path";
|
|
53
|
-
|
|
54
|
-
// src/isValidFilePath.ts
|
|
55
|
-
import { access, lstat } from "node:fs/promises";
|
|
56
42
|
async function isValidFilePath(filePath, dirPath) {
|
|
57
43
|
if (!filePath.startsWith(dirPath)) return false;
|
|
58
44
|
try {
|
|
@@ -63,23 +49,20 @@ async function isValidFilePath(filePath, dirPath) {
|
|
|
63
49
|
}
|
|
64
50
|
}
|
|
65
51
|
|
|
66
|
-
|
|
67
|
-
var cwd = process.cwd();
|
|
52
|
+
const cwd = process.cwd();
|
|
68
53
|
async function getFilePath(url = "", { path = "", dirs = [], spa }) {
|
|
69
54
|
let urlPath = url.replace(/[?#].*$/, "");
|
|
70
55
|
for (let dir of dirs.length === 0 ? [""] : dirs) {
|
|
71
|
-
let dirPath =
|
|
72
|
-
let filePath =
|
|
73
|
-
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
|
|
74
|
-
|
|
75
|
-
filePath = join2(dirPath, spa ? "" : urlPath, "index.html");
|
|
56
|
+
let dirPath = join(cwd, path, dir);
|
|
57
|
+
let filePath = join(dirPath, urlPath);
|
|
58
|
+
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath)) return filePath;
|
|
59
|
+
filePath = join(dirPath, spa ? "" : urlPath, "index.html");
|
|
76
60
|
if (await isValidFilePath(filePath, dirPath)) return filePath;
|
|
77
61
|
}
|
|
78
62
|
}
|
|
79
63
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var defaultPort = 3e3;
|
|
64
|
+
const defaultHost = "localhost";
|
|
65
|
+
const defaultPort = 3e3;
|
|
83
66
|
function getTarget(config = {}) {
|
|
84
67
|
let { host, port, url } = config;
|
|
85
68
|
let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
|
|
@@ -93,8 +76,7 @@ function getTarget(config = {}) {
|
|
|
93
76
|
};
|
|
94
77
|
}
|
|
95
78
|
|
|
96
|
-
|
|
97
|
-
var mimeTypes = {
|
|
79
|
+
const mimeTypes = {
|
|
98
80
|
html: "text/html; charset=utf-8",
|
|
99
81
|
js: "text/javascript",
|
|
100
82
|
json: "application/json",
|
|
@@ -108,7 +90,6 @@ var mimeTypes = {
|
|
|
108
90
|
md: "text/markdown"
|
|
109
91
|
};
|
|
110
92
|
|
|
111
|
-
// src/serve.ts
|
|
112
93
|
async function serve(config = {}) {
|
|
113
94
|
let stop = await bundle(config);
|
|
114
95
|
return new Promise((resolve) => {
|
|
@@ -121,16 +102,13 @@ async function serve(config = {}) {
|
|
|
121
102
|
res.end("Not found");
|
|
122
103
|
return;
|
|
123
104
|
}
|
|
124
|
-
let
|
|
125
|
-
let mimeType = mimeTypes[ext] ?? "application/octet-stream";
|
|
105
|
+
let mimeType = mimeTypes[extname(filePath).slice(1).toLowerCase()] ?? "application/octet-stream";
|
|
126
106
|
res.writeHead(200, { "content-type": mimeType });
|
|
127
107
|
createReadStream(filePath).pipe(res);
|
|
128
108
|
});
|
|
129
|
-
if (stop) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
});
|
|
133
|
-
}
|
|
109
|
+
if (stop) server.on("close", async () => {
|
|
110
|
+
await stop();
|
|
111
|
+
});
|
|
134
112
|
let { host, port } = getTarget(config);
|
|
135
113
|
server.listen(port, host, () => {
|
|
136
114
|
if (config.log) console.log(`Server running at http://${host}:${port}`);
|
|
@@ -138,6 +116,5 @@ async function serve(config = {}) {
|
|
|
138
116
|
});
|
|
139
117
|
});
|
|
140
118
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
};
|
|
119
|
+
|
|
120
|
+
export { serve };
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/serve",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.38",
|
|
4
4
|
"description": "Simple static file server + bundler, primarily for demo apps and tests, manual or automated",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
7
7
|
"server",
|
|
8
|
-
"static"
|
|
8
|
+
"static server",
|
|
9
|
+
"bundler"
|
|
9
10
|
],
|
|
10
11
|
"bin": {
|
|
11
12
|
"serve": "dist/run.mjs"
|
|
@@ -17,25 +18,18 @@
|
|
|
17
18
|
"license": "MIT",
|
|
18
19
|
"author": "axtk",
|
|
19
20
|
"type": "module",
|
|
20
|
-
"main": "dist/index.
|
|
21
|
-
"
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
22
24
|
"scripts": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"compile-mjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm --external:esbuild",
|
|
27
|
-
"compile-cjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.cjs --platform=node --format=cjs --external:esbuild",
|
|
28
|
-
"prepublishOnly": "npm run build",
|
|
29
|
-
"preversion": "npx npm-run-all typecheck shape build",
|
|
30
|
-
"shape": "npx codeshape",
|
|
31
|
-
"typecheck": "tsc --noEmit",
|
|
32
|
-
"types": "tsc --emitDeclarationOnly"
|
|
25
|
+
"compile-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm --external:esbuild",
|
|
26
|
+
"preversion": "npx npm-run-all shape compile-bin",
|
|
27
|
+
"shape": "npx codeshape"
|
|
33
28
|
},
|
|
34
29
|
"devDependencies": {
|
|
35
|
-
"@types/node": "^24.5.2"
|
|
36
|
-
"typescript": "^5.9.2"
|
|
30
|
+
"@types/node": "^24.5.2"
|
|
37
31
|
},
|
|
38
32
|
"dependencies": {
|
|
39
|
-
"esbuild": "^0.27.
|
|
33
|
+
"esbuild": "^0.27.1"
|
|
40
34
|
}
|
|
41
35
|
}
|
package/tsconfig.json
CHANGED
package/dist/run.cjs
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
// src/serve.ts
|
|
5
|
-
var import_node_fs = require("node:fs");
|
|
6
|
-
var import_node_http = require("node:http");
|
|
7
|
-
var import_node_path3 = require("node:path");
|
|
8
|
-
|
|
9
|
-
// src/bundle.ts
|
|
10
|
-
var import_promises = require("node:fs/promises");
|
|
11
|
-
var import_node_path = require("node:path");
|
|
12
|
-
var import_esbuild = require("esbuild");
|
|
13
|
-
async function bundle({
|
|
14
|
-
path = "",
|
|
15
|
-
bundle: options,
|
|
16
|
-
watch,
|
|
17
|
-
minify
|
|
18
|
-
} = {}) {
|
|
19
|
-
if (!options) return;
|
|
20
|
-
let normalizedOptions;
|
|
21
|
-
if (typeof options === "boolean") normalizedOptions = {};
|
|
22
|
-
else if (typeof options === "string")
|
|
23
|
-
normalizedOptions = {
|
|
24
|
-
input: options
|
|
25
|
-
};
|
|
26
|
-
else normalizedOptions = options;
|
|
27
|
-
let dir = normalizedOptions.dir ?? "dist";
|
|
28
|
-
let inputFile = (0, import_node_path.join)(path, normalizedOptions.input ?? "index.ts");
|
|
29
|
-
await (0, import_promises.rm)((0, import_node_path.join)(path, dir), { recursive: true, force: true });
|
|
30
|
-
let buildOptions = {
|
|
31
|
-
entryPoints: [inputFile],
|
|
32
|
-
bundle: true,
|
|
33
|
-
format: "esm",
|
|
34
|
-
platform: "browser",
|
|
35
|
-
logLevel: "warning",
|
|
36
|
-
minify
|
|
37
|
-
};
|
|
38
|
-
if (normalizedOptions.output)
|
|
39
|
-
buildOptions.outfile = (0, import_node_path.join)(path, dir, normalizedOptions.output);
|
|
40
|
-
else {
|
|
41
|
-
buildOptions.outdir = (0, import_node_path.join)(path, dir);
|
|
42
|
-
buildOptions.splitting = true;
|
|
43
|
-
}
|
|
44
|
-
if (watch) {
|
|
45
|
-
let ctx = await (0, import_esbuild.context)(buildOptions);
|
|
46
|
-
await ctx.watch();
|
|
47
|
-
return async () => {
|
|
48
|
-
await ctx.dispose();
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
await (0, import_esbuild.build)(buildOptions);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/getFilePath.ts
|
|
55
|
-
var import_node_path2 = require("node:path");
|
|
56
|
-
|
|
57
|
-
// src/isValidFilePath.ts
|
|
58
|
-
var import_promises2 = require("node:fs/promises");
|
|
59
|
-
async function isValidFilePath(filePath, dirPath) {
|
|
60
|
-
if (!filePath.startsWith(dirPath)) return false;
|
|
61
|
-
try {
|
|
62
|
-
await (0, import_promises2.access)(filePath);
|
|
63
|
-
return !(await (0, import_promises2.lstat)(filePath)).isDirectory();
|
|
64
|
-
} catch {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// src/getFilePath.ts
|
|
70
|
-
var cwd = process.cwd();
|
|
71
|
-
async function getFilePath(url = "", { path = "", dirs = [], spa }) {
|
|
72
|
-
let urlPath = url.replace(/[?#].*$/, "");
|
|
73
|
-
for (let dir of dirs.length === 0 ? [""] : dirs) {
|
|
74
|
-
let dirPath = (0, import_node_path2.join)(cwd, path, dir);
|
|
75
|
-
let filePath = (0, import_node_path2.join)(dirPath, urlPath);
|
|
76
|
-
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
|
|
77
|
-
return filePath;
|
|
78
|
-
filePath = (0, import_node_path2.join)(dirPath, spa ? "" : urlPath, "index.html");
|
|
79
|
-
if (await isValidFilePath(filePath, dirPath)) return filePath;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// src/getTarget.ts
|
|
84
|
-
var defaultHost = "localhost";
|
|
85
|
-
var defaultPort = 3e3;
|
|
86
|
-
function getTarget(config = {}) {
|
|
87
|
-
let { host, port, url } = config;
|
|
88
|
-
let [, , urlHost, , urlPort] = url?.match(/^(https?:\/\/)?([^:/]+)(:(\d+))?\/?/) ?? [];
|
|
89
|
-
if (!urlPort && /^\d+$/.test(urlHost)) {
|
|
90
|
-
urlPort = urlHost;
|
|
91
|
-
urlHost = "";
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
port: port || Number(urlPort) || defaultPort,
|
|
95
|
-
host: host || urlHost || defaultHost
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// src/mimeTypes.ts
|
|
100
|
-
var mimeTypes = {
|
|
101
|
-
html: "text/html; charset=utf-8",
|
|
102
|
-
js: "text/javascript",
|
|
103
|
-
json: "application/json",
|
|
104
|
-
css: "text/css",
|
|
105
|
-
svg: "image/svg+xml",
|
|
106
|
-
png: "image/png",
|
|
107
|
-
jpg: "image/jpeg",
|
|
108
|
-
gif: "image/gif",
|
|
109
|
-
ico: "image/x-icon",
|
|
110
|
-
txt: "text/plain",
|
|
111
|
-
md: "text/markdown"
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// src/serve.ts
|
|
115
|
-
async function serve(config = {}) {
|
|
116
|
-
let stop = await bundle(config);
|
|
117
|
-
return new Promise((resolve) => {
|
|
118
|
-
let server = (0, import_node_http.createServer)(async (req, res) => {
|
|
119
|
-
await config.onRequest?.(req, res);
|
|
120
|
-
if (res.headersSent) return;
|
|
121
|
-
let filePath = await getFilePath(req.url, config);
|
|
122
|
-
if (filePath === void 0) {
|
|
123
|
-
res.writeHead(404, { "content-type": "text/plain" });
|
|
124
|
-
res.end("Not found");
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
let ext = (0, import_node_path3.extname)(filePath).slice(1).toLowerCase();
|
|
128
|
-
let mimeType = mimeTypes[ext] ?? "application/octet-stream";
|
|
129
|
-
res.writeHead(200, { "content-type": mimeType });
|
|
130
|
-
(0, import_node_fs.createReadStream)(filePath).pipe(res);
|
|
131
|
-
});
|
|
132
|
-
if (stop) {
|
|
133
|
-
server.on("close", async () => {
|
|
134
|
-
await stop();
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
let { host, port } = getTarget(config);
|
|
138
|
-
server.listen(port, host, () => {
|
|
139
|
-
if (config.log) console.log(`Server running at http://${host}:${port}`);
|
|
140
|
-
resolve(server);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// src/run.ts
|
|
146
|
-
async function run() {
|
|
147
|
-
let [url, ...args] = process.argv.slice(2);
|
|
148
|
-
let spa = false;
|
|
149
|
-
let watch = false;
|
|
150
|
-
let minify = false;
|
|
151
|
-
if (args[0] === "*") {
|
|
152
|
-
spa = true;
|
|
153
|
-
args.shift();
|
|
154
|
-
}
|
|
155
|
-
while (args.at(-1)?.startsWith("--")) {
|
|
156
|
-
let arg = args.pop();
|
|
157
|
-
switch (arg) {
|
|
158
|
-
case "--watch":
|
|
159
|
-
watch = true;
|
|
160
|
-
break;
|
|
161
|
-
case "--minify":
|
|
162
|
-
minify = true;
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
let bundleFlagIndex = args.indexOf("-b");
|
|
167
|
-
let path = args[0];
|
|
168
|
-
let dirs;
|
|
169
|
-
let bundle2;
|
|
170
|
-
if (bundleFlagIndex === -1) dirs = args.slice(1);
|
|
171
|
-
else {
|
|
172
|
-
dirs = args.slice(1, bundleFlagIndex);
|
|
173
|
-
bundle2 = {
|
|
174
|
-
input: args[bundleFlagIndex + 1] || void 0,
|
|
175
|
-
output: args[bundleFlagIndex + 2] || void 0,
|
|
176
|
-
dir: args[bundleFlagIndex + 3] || void 0
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
await serve({
|
|
180
|
-
url,
|
|
181
|
-
path,
|
|
182
|
-
dirs,
|
|
183
|
-
spa,
|
|
184
|
-
bundle: bundle2,
|
|
185
|
-
log: true,
|
|
186
|
-
watch,
|
|
187
|
-
minify
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
run();
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export type BundleConfig = {
|
|
2
|
-
/**
|
|
3
|
-
* Output directory path relative to the server config `path`.
|
|
4
|
-
*
|
|
5
|
-
* @defaultValue "dist"
|
|
6
|
-
*/
|
|
7
|
-
dir?: string | undefined;
|
|
8
|
-
/**
|
|
9
|
-
* Input path relative to the server config `path`.
|
|
10
|
-
*
|
|
11
|
-
* @defaultValue "index.ts"
|
|
12
|
-
*/
|
|
13
|
-
input?: string | undefined;
|
|
14
|
-
/**
|
|
15
|
-
* Output path relative to the bundle config `dir`.
|
|
16
|
-
*
|
|
17
|
-
* @defaultValue "index.js"
|
|
18
|
-
*/
|
|
19
|
-
output?: string | undefined;
|
|
20
|
-
};
|
package/dist/src/Config.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import type { BundleConfig } from "./BundleConfig.ts";
|
|
3
|
-
export type Config = {
|
|
4
|
-
/** Server URL. */
|
|
5
|
-
url?: string;
|
|
6
|
-
/**
|
|
7
|
-
* Server host.
|
|
8
|
-
*
|
|
9
|
-
* @defaultValue "localhost"
|
|
10
|
-
*/
|
|
11
|
-
host?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Server port.
|
|
14
|
-
*
|
|
15
|
-
* @defaultValue 3000
|
|
16
|
-
*/
|
|
17
|
-
port?: number;
|
|
18
|
-
/** Application path. */
|
|
19
|
-
path?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Public assets directories. If not provided, the application
|
|
22
|
-
* `path` is served as a public assets directory.
|
|
23
|
-
*/
|
|
24
|
-
dirs?: string[];
|
|
25
|
-
/**
|
|
26
|
-
* Enables single-page application (SPA) mode. If `true`, all
|
|
27
|
-
* unmatched URLs are served as "/".
|
|
28
|
-
*/
|
|
29
|
-
spa?: boolean;
|
|
30
|
-
/** Whether to rebuild whenever the bundled files change. */
|
|
31
|
-
watch?: boolean;
|
|
32
|
-
/** Whether the bundle should be minified. */
|
|
33
|
-
minify?: boolean;
|
|
34
|
-
/** Whether to log to the console. */
|
|
35
|
-
log?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Bundle config.
|
|
38
|
-
*
|
|
39
|
-
* If `undefined`, bundling is skipped.
|
|
40
|
-
* Otherwise, its type is `BundleConfig`, with the following shorthand options:
|
|
41
|
-
* If `true`, it's equivalent to `{ input: "index.ts", output: "index.js" }`.
|
|
42
|
-
* If `string`, it's equivalent to `input` in `{ input, ouput: "index.js" }`.
|
|
43
|
-
*/
|
|
44
|
-
bundle?: boolean | string | BundleConfig | undefined;
|
|
45
|
-
/** Custom request handler. */
|
|
46
|
-
onRequest?: (req?: IncomingMessage, res?: ServerResponse<IncomingMessage>) => void | Promise<void>;
|
|
47
|
-
};
|
package/dist/src/bundle.d.ts
DELETED
package/dist/src/getTarget.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isValidFilePath(filePath: string, dirPath: string): Promise<boolean>;
|
package/dist/src/mimeTypes.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const mimeTypes: Record<string, string>;
|
package/dist/src/run.d.ts
DELETED
package/dist/src/serve.d.ts
DELETED