@t8/serve 0.1.11 → 0.1.13
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 +23 -53
- package/dist/run.cjs +0 -3
- package/dist/run.mjs +0 -3
- package/package.json +2 -2
- package/src/serve.ts +0 -4
package/dist/index.js
CHANGED
|
@@ -1,61 +1,35 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
serve: () => serve
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
|
|
27
1
|
// src/serve.ts
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { extname } from "node:path";
|
|
31
5
|
|
|
32
6
|
// src/bundle.ts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var exec =
|
|
7
|
+
import { exec as originalExec } from "node:child_process";
|
|
8
|
+
import { rm } from "node:fs/promises";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { promisify } from "node:util";
|
|
11
|
+
var exec = promisify(originalExec);
|
|
38
12
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
39
13
|
if (!options) return;
|
|
40
14
|
let normalizedOptions = typeof options === "boolean" ? {} : options;
|
|
41
|
-
let inputFile =
|
|
42
|
-
let outputFile =
|
|
43
|
-
await
|
|
15
|
+
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
16
|
+
let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
|
|
17
|
+
await rm(join(path, "dist"), { recursive: true, force: true });
|
|
44
18
|
await exec(
|
|
45
19
|
`npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
|
|
46
20
|
);
|
|
47
21
|
}
|
|
48
22
|
|
|
49
23
|
// src/getFilePath.ts
|
|
50
|
-
|
|
24
|
+
import { join as join2 } from "node:path";
|
|
51
25
|
|
|
52
26
|
// src/isValidFilePath.ts
|
|
53
|
-
|
|
27
|
+
import { access, lstat } from "node:fs/promises";
|
|
54
28
|
async function isValidFilePath(filePath, dirPath) {
|
|
55
29
|
if (!filePath.startsWith(dirPath)) return false;
|
|
56
30
|
try {
|
|
57
|
-
await
|
|
58
|
-
return !(await
|
|
31
|
+
await access(filePath);
|
|
32
|
+
return !(await lstat(filePath)).isDirectory();
|
|
59
33
|
} catch {
|
|
60
34
|
return false;
|
|
61
35
|
}
|
|
@@ -66,11 +40,11 @@ var cwd = process.cwd();
|
|
|
66
40
|
async function getFilePath(url = "", { path = "", dirs = [], spa }) {
|
|
67
41
|
let urlPath = url.replace(/[?#].*$/, "");
|
|
68
42
|
for (let dir of dirs.length === 0 ? [""] : dirs) {
|
|
69
|
-
let dirPath = (
|
|
70
|
-
let filePath = (
|
|
43
|
+
let dirPath = join2(cwd, path, dir);
|
|
44
|
+
let filePath = join2(dirPath, urlPath);
|
|
71
45
|
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
|
|
72
46
|
return filePath;
|
|
73
|
-
filePath = (
|
|
47
|
+
filePath = join2(dirPath, spa ? "" : urlPath, "index.html");
|
|
74
48
|
if (await isValidFilePath(filePath, dirPath)) return filePath;
|
|
75
49
|
}
|
|
76
50
|
}
|
|
@@ -101,20 +75,17 @@ async function serve(config = {}) {
|
|
|
101
75
|
}
|
|
102
76
|
await bundle(config);
|
|
103
77
|
return new Promise((resolve) => {
|
|
104
|
-
let server =
|
|
78
|
+
let server = createServer(async (req, res) => {
|
|
105
79
|
let filePath = await getFilePath(req.url, config);
|
|
106
80
|
if (filePath === void 0) {
|
|
107
81
|
res.writeHead(404, { "content-type": "text/plain" });
|
|
108
82
|
res.end("Not found");
|
|
109
83
|
return;
|
|
110
84
|
}
|
|
111
|
-
let ext =
|
|
85
|
+
let ext = extname(filePath).slice(1).toLowerCase();
|
|
112
86
|
let mimeType = mimeTypes[ext] ?? "application/octet-stream";
|
|
113
87
|
res.writeHead(200, { "content-type": mimeType });
|
|
114
|
-
|
|
115
|
-
});
|
|
116
|
-
server.on("close", () => {
|
|
117
|
-
process.exit(0);
|
|
88
|
+
createReadStream(filePath).pipe(res);
|
|
118
89
|
});
|
|
119
90
|
let serverPort = Number(port) || defaultPort;
|
|
120
91
|
let serverHost = host || defaultHost;
|
|
@@ -124,7 +95,6 @@ async function serve(config = {}) {
|
|
|
124
95
|
});
|
|
125
96
|
});
|
|
126
97
|
}
|
|
127
|
-
|
|
128
|
-
0 && (module.exports = {
|
|
98
|
+
export {
|
|
129
99
|
serve
|
|
130
|
-
}
|
|
100
|
+
};
|
package/dist/run.cjs
CHANGED
|
@@ -90,9 +90,6 @@ async function serve(config = {}) {
|
|
|
90
90
|
res.writeHead(200, { "content-type": mimeType });
|
|
91
91
|
(0, import_node_fs.createReadStream)(filePath).pipe(res);
|
|
92
92
|
});
|
|
93
|
-
server.on("close", () => {
|
|
94
|
-
process.exit(0);
|
|
95
|
-
});
|
|
96
93
|
let serverPort = Number(port) || defaultPort;
|
|
97
94
|
let serverHost = host || defaultHost;
|
|
98
95
|
server.listen(serverPort, serverHost, () => {
|
package/dist/run.mjs
CHANGED
|
@@ -89,9 +89,6 @@ async function serve(config = {}) {
|
|
|
89
89
|
res.writeHead(200, { "content-type": mimeType });
|
|
90
90
|
createReadStream(filePath).pipe(res);
|
|
91
91
|
});
|
|
92
|
-
server.on("close", () => {
|
|
93
|
-
process.exit(0);
|
|
94
|
-
});
|
|
95
92
|
let serverPort = Number(port) || defaultPort;
|
|
96
93
|
let serverHost = host || defaultHost;
|
|
97
94
|
server.listen(serverPort, serverHost, () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/serve",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "npx npm-run-all clean -p compile compile-mjs-bin compile-cjs-bin",
|
|
23
23
|
"clean": "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true});\"",
|
|
24
|
-
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node",
|
|
24
|
+
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node --format=esm",
|
|
25
25
|
"compile-mjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm",
|
|
26
26
|
"compile-cjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.cjs --platform=node --format=cjs",
|
|
27
27
|
"prepublishOnly": "npm run build",
|
package/src/serve.ts
CHANGED
|
@@ -39,10 +39,6 @@ export async function serve(config: Config = {}): Promise<Server> {
|
|
|
39
39
|
createReadStream(filePath).pipe(res);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
server.on("close", () => {
|
|
43
|
-
process.exit(0);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
42
|
let serverPort = Number(port) || defaultPort;
|
|
47
43
|
let serverHost = host || defaultHost;
|
|
48
44
|
|