@t8/serve 0.1.10 → 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 +24 -50
- package/dist/run.cjs +3 -2
- package/dist/run.mjs +3 -2
- package/package.json +2 -2
- package/src/Config.ts +6 -4
- package/src/bundle.ts +5 -2
package/dist/index.js
CHANGED
|
@@ -1,60 +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
|
-
let
|
|
41
|
-
let
|
|
42
|
-
|
|
14
|
+
let normalizedOptions = typeof options === "boolean" ? {} : options;
|
|
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 });
|
|
43
18
|
await exec(
|
|
44
19
|
`npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
|
|
45
20
|
);
|
|
46
21
|
}
|
|
47
22
|
|
|
48
23
|
// src/getFilePath.ts
|
|
49
|
-
|
|
24
|
+
import { join as join2 } from "node:path";
|
|
50
25
|
|
|
51
26
|
// src/isValidFilePath.ts
|
|
52
|
-
|
|
27
|
+
import { access, lstat } from "node:fs/promises";
|
|
53
28
|
async function isValidFilePath(filePath, dirPath) {
|
|
54
29
|
if (!filePath.startsWith(dirPath)) return false;
|
|
55
30
|
try {
|
|
56
|
-
await
|
|
57
|
-
return !(await
|
|
31
|
+
await access(filePath);
|
|
32
|
+
return !(await lstat(filePath)).isDirectory();
|
|
58
33
|
} catch {
|
|
59
34
|
return false;
|
|
60
35
|
}
|
|
@@ -65,11 +40,11 @@ var cwd = process.cwd();
|
|
|
65
40
|
async function getFilePath(url = "", { path = "", dirs = [], spa }) {
|
|
66
41
|
let urlPath = url.replace(/[?#].*$/, "");
|
|
67
42
|
for (let dir of dirs.length === 0 ? [""] : dirs) {
|
|
68
|
-
let dirPath = (
|
|
69
|
-
let filePath = (
|
|
43
|
+
let dirPath = join2(cwd, path, dir);
|
|
44
|
+
let filePath = join2(dirPath, urlPath);
|
|
70
45
|
if (!urlPath.endsWith("/") && await isValidFilePath(filePath, dirPath))
|
|
71
46
|
return filePath;
|
|
72
|
-
filePath = (
|
|
47
|
+
filePath = join2(dirPath, spa ? "" : urlPath, "index.html");
|
|
73
48
|
if (await isValidFilePath(filePath, dirPath)) return filePath;
|
|
74
49
|
}
|
|
75
50
|
}
|
|
@@ -100,17 +75,17 @@ async function serve(config = {}) {
|
|
|
100
75
|
}
|
|
101
76
|
await bundle(config);
|
|
102
77
|
return new Promise((resolve) => {
|
|
103
|
-
let server =
|
|
78
|
+
let server = createServer(async (req, res) => {
|
|
104
79
|
let filePath = await getFilePath(req.url, config);
|
|
105
80
|
if (filePath === void 0) {
|
|
106
81
|
res.writeHead(404, { "content-type": "text/plain" });
|
|
107
82
|
res.end("Not found");
|
|
108
83
|
return;
|
|
109
84
|
}
|
|
110
|
-
let ext =
|
|
85
|
+
let ext = extname(filePath).slice(1).toLowerCase();
|
|
111
86
|
let mimeType = mimeTypes[ext] ?? "application/octet-stream";
|
|
112
87
|
res.writeHead(200, { "content-type": mimeType });
|
|
113
|
-
|
|
88
|
+
createReadStream(filePath).pipe(res);
|
|
114
89
|
});
|
|
115
90
|
server.on("close", () => {
|
|
116
91
|
process.exit(0);
|
|
@@ -123,7 +98,6 @@ async function serve(config = {}) {
|
|
|
123
98
|
});
|
|
124
99
|
});
|
|
125
100
|
}
|
|
126
|
-
|
|
127
|
-
0 && (module.exports = {
|
|
101
|
+
export {
|
|
128
102
|
serve
|
|
129
|
-
}
|
|
103
|
+
};
|
package/dist/run.cjs
CHANGED
|
@@ -14,8 +14,9 @@ var import_node_util = require("node:util");
|
|
|
14
14
|
var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
|
|
15
15
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
16
16
|
if (!options) return;
|
|
17
|
-
let
|
|
18
|
-
let
|
|
17
|
+
let normalizedOptions = typeof options === "boolean" ? {} : options;
|
|
18
|
+
let inputFile = (0, import_node_path.join)(path, normalizedOptions.input ?? "index.ts");
|
|
19
|
+
let outputFile = (0, import_node_path.join)(path, "dist", normalizedOptions.output ?? "index.js");
|
|
19
20
|
await (0, import_promises.rm)((0, import_node_path.join)(path, "dist"), { recursive: true, force: true });
|
|
20
21
|
await exec(
|
|
21
22
|
`npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
|
package/dist/run.mjs
CHANGED
|
@@ -13,8 +13,9 @@ import { promisify } from "node:util";
|
|
|
13
13
|
var exec = promisify(originalExec);
|
|
14
14
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
15
15
|
if (!options) return;
|
|
16
|
-
let
|
|
17
|
-
let
|
|
16
|
+
let normalizedOptions = typeof options === "boolean" ? {} : options;
|
|
17
|
+
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
18
|
+
let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
|
|
18
19
|
await rm(join(path, "dist"), { recursive: true, force: true });
|
|
19
20
|
await exec(
|
|
20
21
|
`npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
|
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",
|
package/src/Config.ts
CHANGED
package/src/bundle.ts
CHANGED
|
@@ -9,8 +9,11 @@ const exec = promisify(originalExec);
|
|
|
9
9
|
export async function bundle({ path = "", bundle: options }: Config = {}) {
|
|
10
10
|
if (!options) return;
|
|
11
11
|
|
|
12
|
-
let
|
|
13
|
-
|
|
12
|
+
let normalizedOptions: Exclude<Config["bundle"], boolean | undefined> =
|
|
13
|
+
typeof options === "boolean" ? {} : options;
|
|
14
|
+
|
|
15
|
+
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
16
|
+
let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
|
|
14
17
|
|
|
15
18
|
await rm(join(path, "dist"), { recursive: true, force: true });
|
|
16
19
|
await exec(
|