@t8/serve 0.1.35 → 0.1.37
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/README.md +1 -1
- package/dist/index.d.ts +71 -3
- package/package.json +9 -14
- 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/{run.mjs → run.js} +0 -0
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
import type { IncomingMessage, ServerResponse, createServer } from "node:http";
|
|
2
|
+
|
|
3
|
+
export 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
|
+
export type Config = {
|
|
24
|
+
/** Server URL. */
|
|
25
|
+
url?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Server host.
|
|
28
|
+
*
|
|
29
|
+
* @defaultValue "localhost"
|
|
30
|
+
*/
|
|
31
|
+
host?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Server port.
|
|
34
|
+
*
|
|
35
|
+
* @defaultValue 3000
|
|
36
|
+
*/
|
|
37
|
+
port?: number;
|
|
38
|
+
/** Application path. */
|
|
39
|
+
path?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Public assets directories. If not provided, the application
|
|
42
|
+
* `path` is served as a public assets directory.
|
|
43
|
+
*/
|
|
44
|
+
dirs?: string[];
|
|
45
|
+
/**
|
|
46
|
+
* Enables single-page application (SPA) mode. If `true`, all
|
|
47
|
+
* unmatched URLs are served as "/".
|
|
48
|
+
*/
|
|
49
|
+
spa?: boolean;
|
|
50
|
+
/** Whether to rebuild whenever the bundled files change. */
|
|
51
|
+
watch?: boolean;
|
|
52
|
+
/** Whether the bundle should be minified. */
|
|
53
|
+
minify?: boolean;
|
|
54
|
+
/** Whether to log to the console. */
|
|
55
|
+
log?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Bundle config.
|
|
58
|
+
*
|
|
59
|
+
* If `undefined`, bundling is skipped.
|
|
60
|
+
* Otherwise, its type is `BundleConfig`, with the following shorthand options:
|
|
61
|
+
* If `true`, it's equivalent to `{ input: "index.ts", output: "index.js" }`.
|
|
62
|
+
* If `string`, it's equivalent to `input` in `{ input, ouput: "index.js" }`.
|
|
63
|
+
*/
|
|
64
|
+
bundle?: boolean | string | BundleConfig | undefined;
|
|
65
|
+
/** Custom request handler. */
|
|
66
|
+
onRequest?: (req?: IncomingMessage, res?: ServerResponse<IncomingMessage>) => void | Promise<void>;
|
|
67
|
+
};
|
|
68
|
+
export type Server = ReturnType<typeof createServer>;
|
|
69
|
+
export declare function serve(config?: Config): Promise<Server>;
|
|
70
|
+
|
|
71
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/serve",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.37",
|
|
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
|
-
"serve": "dist/run.
|
|
12
|
+
"serve": "dist/run.js"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
@@ -20,22 +21,16 @@
|
|
|
20
21
|
"main": "dist/index.js",
|
|
21
22
|
"types": "dist/index.d.ts",
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build": "npx npm-run-all clean -p compile compile-mjs-bin compile-cjs-bin -s types",
|
|
24
24
|
"clean": "node -e \"require('node:fs').rmSync('dist', { force: true, recursive: true });\"",
|
|
25
25
|
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node --format=esm --external:esbuild",
|
|
26
|
-
"compile-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"preversion": "npx npm-run-all typecheck shape build",
|
|
30
|
-
"shape": "npx codeshape",
|
|
31
|
-
"typecheck": "tsc --noEmit",
|
|
32
|
-
"types": "tsc --emitDeclarationOnly"
|
|
26
|
+
"compile-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.js --platform=node --format=esm --external:esbuild",
|
|
27
|
+
"preversion": "npx npm-run-all clean shape -p compile compile-bin",
|
|
28
|
+
"shape": "npx codeshape --typecheck --emit-types"
|
|
33
29
|
},
|
|
34
30
|
"devDependencies": {
|
|
35
|
-
"@types/node": "^24.5.2"
|
|
36
|
-
"typescript": "^5.9.2"
|
|
31
|
+
"@types/node": "^24.5.2"
|
|
37
32
|
},
|
|
38
33
|
"dependencies": {
|
|
39
|
-
"esbuild": "^0.
|
|
34
|
+
"esbuild": "^0.27.1"
|
|
40
35
|
}
|
|
41
36
|
}
|
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
/package/dist/{run.mjs → run.js}
RENAMED
|
File without changes
|