@t8/serve 0.1.11 → 0.1.12
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 -50
- package/package.json +2 -2
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,17 +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
|
-
|
|
88
|
+
createReadStream(filePath).pipe(res);
|
|
115
89
|
});
|
|
116
90
|
server.on("close", () => {
|
|
117
91
|
process.exit(0);
|
|
@@ -124,7 +98,6 @@ async function serve(config = {}) {
|
|
|
124
98
|
});
|
|
125
99
|
});
|
|
126
100
|
}
|
|
127
|
-
|
|
128
|
-
0 && (module.exports = {
|
|
101
|
+
export {
|
|
129
102
|
serve
|
|
130
|
-
}
|
|
103
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/serve",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
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",
|