@dithrkit/sdk 0.1.0
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/bin/dithrkit.js +4 -0
- package/configs/eslint.config.js +96 -0
- package/configs/tsconfig.base.json +13 -0
- package/lib/binary.js +33 -0
- package/lib/build-helper.js +82 -0
- package/lib/cli.js +91 -0
- package/lib/commands/build.js +74 -0
- package/lib/commands/create.js +235 -0
- package/lib/commands/dev.js +145 -0
- package/lib/commands/export.js +164 -0
- package/lib/commands/run.js +83 -0
- package/lib/commands/serve.js +165 -0
- package/lib/commands/watch.js +447 -0
- package/lib/packager.js +159 -0
- package/package.json +52 -0
- package/templates/blank.js +14 -0
- package/templates/blank.ts +14 -0
- package/templates/platformer.js +53 -0
- package/templates/platformer.ts +53 -0
- package/templates/shell.html +77 -0
- package/types/dithr.d.ts +700 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { spawn } = require("node:child_process");
|
|
7
|
+
const { getBinaryPath } = require("../binary.js");
|
|
8
|
+
const { detectBuild, findEsbuild } = require("../build-helper.js");
|
|
9
|
+
|
|
10
|
+
function usage() {
|
|
11
|
+
console.log("Usage: dithrkit dev [cart-dir] [options]");
|
|
12
|
+
console.log("");
|
|
13
|
+
console.log("Build TypeScript in watch mode and launch the native dithr runtime.");
|
|
14
|
+
console.log("The runtime's built-in hot-reload picks up changes automatically.");
|
|
15
|
+
console.log("");
|
|
16
|
+
console.log("Options:");
|
|
17
|
+
console.log(" --help, -h Show help");
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
if (args.includes("--help") || args.includes("-h")) usage();
|
|
23
|
+
|
|
24
|
+
let cartDir = ".";
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < args.length; i++) {
|
|
27
|
+
const arg = args[i];
|
|
28
|
+
if (!arg.startsWith("-")) {
|
|
29
|
+
cartDir = arg;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const resolvedCartDir = path.resolve(cartDir);
|
|
34
|
+
const cartJson = path.join(resolvedCartDir, "cart.json");
|
|
35
|
+
|
|
36
|
+
if (!fs.existsSync(cartJson)) {
|
|
37
|
+
console.error(`No cart.json found in ${resolvedCartDir}`);
|
|
38
|
+
console.error(
|
|
39
|
+
"Make sure you are in a dithr project directory, or pass the path as an argument.",
|
|
40
|
+
);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const binary = getBinaryPath();
|
|
45
|
+
if (!fs.existsSync(binary)) {
|
|
46
|
+
console.error(`dithr binary not found at: ${binary}`);
|
|
47
|
+
console.error(
|
|
48
|
+
"Install the platform package for your OS:\n" +
|
|
49
|
+
" npm install @dithrkit/win32-x64 (Windows)\n" +
|
|
50
|
+
" npm install @dithrkit/linux-x64 (Linux)\n" +
|
|
51
|
+
" npm install @dithrkit/darwin-arm64 (macOS)",
|
|
52
|
+
);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const buildInfo = detectBuild(resolvedCartDir);
|
|
57
|
+
let esbuildProc = null;
|
|
58
|
+
|
|
59
|
+
function cleanup() {
|
|
60
|
+
if (esbuildProc) {
|
|
61
|
+
esbuildProc.kill();
|
|
62
|
+
esbuildProc = null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
process.on("SIGINT", () => {
|
|
67
|
+
cleanup();
|
|
68
|
+
process.exit(0);
|
|
69
|
+
});
|
|
70
|
+
process.on("SIGTERM", () => {
|
|
71
|
+
cleanup();
|
|
72
|
+
process.exit(0);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
function launchRuntime() {
|
|
76
|
+
console.log("[dev] Launching dithr runtime…");
|
|
77
|
+
const child = spawn(binary, [cartJson], {
|
|
78
|
+
stdio: "inherit",
|
|
79
|
+
cwd: process.cwd(),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
child.on("close", (code) => {
|
|
83
|
+
cleanup();
|
|
84
|
+
process.exit(code || 0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
child.on("error", (err) => {
|
|
88
|
+
console.error(`Failed to launch dithr: ${err.message}`);
|
|
89
|
+
cleanup();
|
|
90
|
+
process.exit(1);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (buildInfo) {
|
|
95
|
+
// TypeScript project — start esbuild in watch mode, then launch runtime
|
|
96
|
+
const esbuildBin = findEsbuild();
|
|
97
|
+
if (!esbuildBin) {
|
|
98
|
+
console.error("esbuild not found. Install @dithrkit/sdk or run npm install.");
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
fs.mkdirSync(path.dirname(buildInfo.outFile), { recursive: true });
|
|
103
|
+
|
|
104
|
+
const esbuildArgs = [
|
|
105
|
+
esbuildBin,
|
|
106
|
+
buildInfo.srcEntry,
|
|
107
|
+
"--bundle",
|
|
108
|
+
"--format=esm",
|
|
109
|
+
"--target=es2020",
|
|
110
|
+
`--outfile=${buildInfo.outFile}`,
|
|
111
|
+
"--watch",
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
console.log("[dev] Starting esbuild in watch mode…");
|
|
115
|
+
esbuildProc = spawn(process.execPath, esbuildArgs, {
|
|
116
|
+
stdio: ["ignore", "pipe", "inherit"],
|
|
117
|
+
cwd: resolvedCartDir,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
let launched = false;
|
|
121
|
+
esbuildProc.stdout.on("data", (data) => {
|
|
122
|
+
process.stdout.write(data);
|
|
123
|
+
// Launch runtime after first successful build
|
|
124
|
+
if (!launched) {
|
|
125
|
+
launched = true;
|
|
126
|
+
launchRuntime();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
esbuildProc.on("error", (err) => {
|
|
131
|
+
console.error(`Failed to start esbuild: ${err.message}`);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
esbuildProc.on("close", (code) => {
|
|
136
|
+
esbuildProc = null;
|
|
137
|
+
if (!launched) {
|
|
138
|
+
console.error(`esbuild exited with code ${code} before producing output.`);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
// Plain JS project — just launch runtime directly
|
|
144
|
+
launchRuntime();
|
|
145
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { getBinaryPath } = require("../binary.js");
|
|
7
|
+
const { packDirectory, generatePackageLoader } = require("../packager.js");
|
|
8
|
+
|
|
9
|
+
function usage() {
|
|
10
|
+
console.log("Usage: dithrkit export <--web | --desktop> [options]");
|
|
11
|
+
console.log("");
|
|
12
|
+
console.log("Export the current cart for distribution.");
|
|
13
|
+
console.log("");
|
|
14
|
+
console.log("Targets:");
|
|
15
|
+
console.log(" --web Export as self-contained web app (WASM)");
|
|
16
|
+
console.log(" --desktop Export as standalone desktop app");
|
|
17
|
+
console.log("");
|
|
18
|
+
console.log("Options:");
|
|
19
|
+
console.log(" --out, -o <dir> Output directory (default: build/web or build/desktop)");
|
|
20
|
+
console.log(" --help, -h Show help");
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
if (args.includes("--help") || args.includes("-h") || args.length === 0) usage();
|
|
26
|
+
|
|
27
|
+
let target = null;
|
|
28
|
+
let outDir = null;
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < args.length; i++) {
|
|
31
|
+
if (args[i] === "--web") target = "web";
|
|
32
|
+
else if (args[i] === "--desktop") target = "desktop";
|
|
33
|
+
else if (args[i] === "--out" || args[i] === "-o") outDir = args[++i];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!target) {
|
|
37
|
+
console.error("Must specify --web or --desktop");
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const cartJson = path.resolve("cart.json");
|
|
42
|
+
if (!fs.existsSync(cartJson)) {
|
|
43
|
+
console.error("No cart.json found in current directory.");
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const cart = JSON.parse(fs.readFileSync(cartJson, "utf-8"));
|
|
48
|
+
|
|
49
|
+
if (target === "web") {
|
|
50
|
+
exportWeb(cart, outDir || path.resolve("build", "web"));
|
|
51
|
+
} else {
|
|
52
|
+
exportDesktop(cart, outDir || path.resolve("build", "desktop"));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function exportWeb(cart, out) {
|
|
56
|
+
// Resolve WASM runtime
|
|
57
|
+
let wasmPkg;
|
|
58
|
+
try {
|
|
59
|
+
wasmPkg = require("@dithrkit/wasm");
|
|
60
|
+
} catch {
|
|
61
|
+
console.error("@dithrkit/wasm is not installed.");
|
|
62
|
+
console.error("Install it: npm install @dithrkit/wasm");
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!fs.existsSync(wasmPkg.jsPath) || !fs.existsSync(wasmPkg.wasmPath)) {
|
|
67
|
+
console.error("WASM runtime files not found in @dithrkit/wasm.");
|
|
68
|
+
console.error("The package may not have been built yet.");
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fs.mkdirSync(out, { recursive: true });
|
|
73
|
+
|
|
74
|
+
// Copy WASM runtime files
|
|
75
|
+
fs.copyFileSync(wasmPkg.jsPath, path.join(out, "dithr.js"));
|
|
76
|
+
fs.copyFileSync(wasmPkg.wasmPath, path.join(out, "dithr.wasm"));
|
|
77
|
+
|
|
78
|
+
// Pack cart directory into .data file
|
|
79
|
+
const cartDir = path.dirname(cartJson);
|
|
80
|
+
const { dataBuffer, files } = packDirectory(cartDir, "/cart");
|
|
81
|
+
|
|
82
|
+
fs.writeFileSync(path.join(out, "dithr.data"), dataBuffer);
|
|
83
|
+
|
|
84
|
+
// Generate package loader
|
|
85
|
+
const loaderJs = generatePackageLoader("dithr.data", files);
|
|
86
|
+
fs.writeFileSync(path.join(out, "dithr.data.js"), loaderJs);
|
|
87
|
+
|
|
88
|
+
// Generate index.html from shell template
|
|
89
|
+
const shellPath = path.join(__dirname, "..", "..", "templates", "shell.html");
|
|
90
|
+
let html = fs.readFileSync(shellPath, "utf-8");
|
|
91
|
+
|
|
92
|
+
// Replace title
|
|
93
|
+
const title = cart.title || "dithr";
|
|
94
|
+
html = html.replace("<title>dithr</title>", `<title>${title}</title>`);
|
|
95
|
+
|
|
96
|
+
// Replace {{{ SCRIPT }}} with script tags
|
|
97
|
+
const scripts = [
|
|
98
|
+
'<script src="dithr.data.js"></script>',
|
|
99
|
+
'<script async src="dithr.js"></script>',
|
|
100
|
+
].join("\n");
|
|
101
|
+
html = html.replace("{{{ SCRIPT }}}", scripts);
|
|
102
|
+
|
|
103
|
+
fs.writeFileSync(path.join(out, "index.html"), html);
|
|
104
|
+
|
|
105
|
+
console.log(`Web export created in ${out}/`);
|
|
106
|
+
console.log("Files:");
|
|
107
|
+
console.log(" index.html");
|
|
108
|
+
console.log(" dithr.js");
|
|
109
|
+
console.log(" dithr.wasm");
|
|
110
|
+
console.log(" dithr.data");
|
|
111
|
+
console.log(" dithr.data.js");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function exportDesktop(cart, out) {
|
|
115
|
+
const binary = getBinaryPath();
|
|
116
|
+
|
|
117
|
+
if (!fs.existsSync(binary)) {
|
|
118
|
+
console.error(`dithr binary not found at: ${binary}`);
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fs.mkdirSync(out, { recursive: true });
|
|
123
|
+
|
|
124
|
+
// Copy binary
|
|
125
|
+
const binaryName = path.basename(binary);
|
|
126
|
+
const destBinary = path.join(out, binaryName);
|
|
127
|
+
fs.copyFileSync(binary, destBinary);
|
|
128
|
+
|
|
129
|
+
// Make executable on Unix
|
|
130
|
+
if (process.platform !== "win32") {
|
|
131
|
+
fs.chmodSync(destBinary, 0o755);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Copy cart directory
|
|
135
|
+
const cartDir = path.dirname(cartJson);
|
|
136
|
+
const destCart = path.join(out, "cart");
|
|
137
|
+
copyDirSync(cartDir, destCart);
|
|
138
|
+
|
|
139
|
+
console.log(`Desktop export created in ${out}/`);
|
|
140
|
+
console.log("Files:");
|
|
141
|
+
console.log(` ${binaryName}`);
|
|
142
|
+
console.log(" cart/");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function copyDirSync(src, dest) {
|
|
146
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
147
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
148
|
+
|
|
149
|
+
for (const entry of entries) {
|
|
150
|
+
const srcPath = path.join(src, entry.name);
|
|
151
|
+
const destPath = path.join(dest, entry.name);
|
|
152
|
+
|
|
153
|
+
// Skip node_modules, build, .git
|
|
154
|
+
if (entry.name === "node_modules" || entry.name === "build" || entry.name === ".git") {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (entry.isDirectory()) {
|
|
159
|
+
copyDirSync(srcPath, destPath);
|
|
160
|
+
} else {
|
|
161
|
+
fs.copyFileSync(srcPath, destPath);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { spawn } = require("node:child_process");
|
|
7
|
+
const { getBinaryPath } = require("../binary.js");
|
|
8
|
+
const { detectBuild, buildSync } = require("../build-helper.js");
|
|
9
|
+
|
|
10
|
+
function usage() {
|
|
11
|
+
console.log("Usage: dithrkit run [cart-dir] [options]");
|
|
12
|
+
console.log("");
|
|
13
|
+
console.log("Launch the native dithr runtime with the cart in the current directory");
|
|
14
|
+
console.log("(or the specified directory). TypeScript projects are automatically");
|
|
15
|
+
console.log("built before launching.");
|
|
16
|
+
console.log("");
|
|
17
|
+
console.log("Options:");
|
|
18
|
+
console.log(" --no-build Skip automatic TypeScript build");
|
|
19
|
+
console.log(" --help, -h Show help");
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const args = process.argv.slice(2);
|
|
24
|
+
if (args.includes("--help") || args.includes("-h")) usage();
|
|
25
|
+
|
|
26
|
+
// Parse arguments
|
|
27
|
+
let cartDir = ".";
|
|
28
|
+
let skipBuild = false;
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < args.length; i++) {
|
|
31
|
+
const arg = args[i];
|
|
32
|
+
if (arg === "--no-build") {
|
|
33
|
+
skipBuild = true;
|
|
34
|
+
} else if (!arg.startsWith("-")) {
|
|
35
|
+
cartDir = arg;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const cartJson = path.resolve(cartDir, "cart.json");
|
|
40
|
+
|
|
41
|
+
if (!fs.existsSync(cartJson)) {
|
|
42
|
+
console.error(`No cart.json found in ${path.resolve(cartDir)}`);
|
|
43
|
+
console.error(
|
|
44
|
+
"Make sure you are in a dithr project directory, or pass the path as an argument.",
|
|
45
|
+
);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Auto-build TypeScript projects
|
|
50
|
+
if (!skipBuild && detectBuild(path.resolve(cartDir))) {
|
|
51
|
+
console.log("Building TypeScript...");
|
|
52
|
+
if (!buildSync(path.resolve(cartDir))) {
|
|
53
|
+
console.error("Build failed. Fix errors above and try again.");
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const binary = getBinaryPath();
|
|
59
|
+
|
|
60
|
+
if (!fs.existsSync(binary)) {
|
|
61
|
+
console.error(`dithr binary not found at: ${binary}`);
|
|
62
|
+
console.error(
|
|
63
|
+
"Install the platform package for your OS:\n" +
|
|
64
|
+
" npm install @dithrkit/win32-x64 (Windows)\n" +
|
|
65
|
+
" npm install @dithrkit/linux-x64 (Linux)\n" +
|
|
66
|
+
" npm install @dithrkit/darwin-arm64 (macOS)",
|
|
67
|
+
);
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const child = spawn(binary, [cartJson], {
|
|
72
|
+
stdio: "inherit",
|
|
73
|
+
cwd: process.cwd(),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
child.on("close", (code) => {
|
|
77
|
+
process.exit(code || 0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
child.on("error", (err) => {
|
|
81
|
+
console.error(`Failed to launch dithr: ${err.message}`);
|
|
82
|
+
process.exit(1);
|
|
83
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const http = require("node:http");
|
|
5
|
+
const fs = require("node:fs");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const zlib = require("node:zlib");
|
|
8
|
+
const { execSync } = require("node:child_process");
|
|
9
|
+
|
|
10
|
+
function usage() {
|
|
11
|
+
console.log("Usage: dithrkit serve [port] [options]");
|
|
12
|
+
console.log("");
|
|
13
|
+
console.log("Start a local WASM dev server for the current cart.");
|
|
14
|
+
console.log("Serves from the build/web/ directory (created by dithrkit export --web).");
|
|
15
|
+
console.log("");
|
|
16
|
+
console.log("Options:");
|
|
17
|
+
console.log(" --dir <path> Directory to serve (default: build/web)");
|
|
18
|
+
console.log(" --help, -h Show help");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const args = process.argv.slice(2);
|
|
23
|
+
if (args.includes("--help") || args.includes("-h")) usage();
|
|
24
|
+
|
|
25
|
+
let port = 8080;
|
|
26
|
+
let serveDir = path.resolve("build", "web");
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < args.length; i++) {
|
|
29
|
+
if (args[i] === "--dir") {
|
|
30
|
+
serveDir = path.resolve(args[++i]);
|
|
31
|
+
} else if (!isNaN(parseInt(args[i], 10))) {
|
|
32
|
+
port = parseInt(args[i], 10);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const MIME_TYPES = {
|
|
37
|
+
".html": "text/html; charset=utf-8",
|
|
38
|
+
".js": "application/javascript; charset=utf-8",
|
|
39
|
+
".wasm": "application/wasm",
|
|
40
|
+
".data": "application/octet-stream",
|
|
41
|
+
".json": "application/json; charset=utf-8",
|
|
42
|
+
".css": "text/css; charset=utf-8",
|
|
43
|
+
".png": "image/png",
|
|
44
|
+
".ico": "image/x-icon",
|
|
45
|
+
".svg": "image/svg+xml",
|
|
46
|
+
".txt": "text/plain; charset=utf-8",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function getMime(filePath) {
|
|
50
|
+
return MIME_TYPES[path.extname(filePath).toLowerCase()] || "application/octet-stream";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pickEncoding(acceptEncoding) {
|
|
54
|
+
if (acceptEncoding.includes("br")) return "br";
|
|
55
|
+
if (acceptEncoding.includes("gzip")) return "gzip";
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function compressStream(encoding) {
|
|
60
|
+
if (encoding === "br") return zlib.createBrotliCompress();
|
|
61
|
+
if (encoding === "gzip") return zlib.createGzip();
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const server = http.createServer((req, res) => {
|
|
66
|
+
const url = new URL(req.url, `http://localhost:${port}`);
|
|
67
|
+
let relPath = decodeURIComponent(url.pathname);
|
|
68
|
+
|
|
69
|
+
if (relPath === "/") relPath = "/index.html";
|
|
70
|
+
|
|
71
|
+
// Prevent path traversal
|
|
72
|
+
const filePath = path.resolve(serveDir, relPath.slice(1));
|
|
73
|
+
if (!filePath.startsWith(serveDir + path.sep) && filePath !== serveDir) {
|
|
74
|
+
res.writeHead(403);
|
|
75
|
+
res.end("Forbidden");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fs.stat(filePath, (err, stat) => {
|
|
80
|
+
if (err || !stat.isFile()) {
|
|
81
|
+
res.writeHead(404);
|
|
82
|
+
res.end("Not Found");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const mime = getMime(filePath);
|
|
87
|
+
const accept = req.headers["accept-encoding"] || "";
|
|
88
|
+
const encoding = pickEncoding(accept);
|
|
89
|
+
|
|
90
|
+
res.setHeader("Content-Type", mime);
|
|
91
|
+
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
|
|
92
|
+
res.setHeader("Pragma", "no-cache");
|
|
93
|
+
res.setHeader("Expires", "0");
|
|
94
|
+
const stream = fs.createReadStream(filePath);
|
|
95
|
+
const comp = compressStream(encoding);
|
|
96
|
+
|
|
97
|
+
if (comp) {
|
|
98
|
+
res.setHeader("Content-Encoding", encoding);
|
|
99
|
+
res.writeHead(200);
|
|
100
|
+
stream.pipe(comp).pipe(res);
|
|
101
|
+
} else {
|
|
102
|
+
res.setHeader("Content-Length", stat.size);
|
|
103
|
+
res.writeHead(200);
|
|
104
|
+
stream.pipe(res);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
function startServer() {
|
|
110
|
+
server.listen(port, () => {
|
|
111
|
+
const url = `http://localhost:${port}`;
|
|
112
|
+
console.log(`Serving ${serveDir}`);
|
|
113
|
+
console.log(` → ${url}`);
|
|
114
|
+
console.log("Press Ctrl+C to stop.\n");
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const cmd =
|
|
118
|
+
process.platform === "win32"
|
|
119
|
+
? `start ${url}`
|
|
120
|
+
: process.platform === "darwin"
|
|
121
|
+
? `open ${url}`
|
|
122
|
+
: `xdg-open ${url}`;
|
|
123
|
+
execSync(cmd, { stdio: "ignore" });
|
|
124
|
+
} catch {
|
|
125
|
+
/* ignore */
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
server.on("error", (err) => {
|
|
130
|
+
if (err.code === "EADDRINUSE") {
|
|
131
|
+
console.log(`Port ${port} in use — killing previous process…`);
|
|
132
|
+
try {
|
|
133
|
+
if (process.platform === "win32") {
|
|
134
|
+
const out = execSync(`netstat -ano | findstr :${port} | findstr LISTENING`, {
|
|
135
|
+
encoding: "utf8",
|
|
136
|
+
});
|
|
137
|
+
const pids = new Set(
|
|
138
|
+
out
|
|
139
|
+
.trim()
|
|
140
|
+
.split(/\r?\n/)
|
|
141
|
+
.map((l) => l.trim().split(/\s+/).pop()),
|
|
142
|
+
);
|
|
143
|
+
for (const pid of pids) {
|
|
144
|
+
execSync(`taskkill /F /PID ${pid}`, { stdio: "ignore" });
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
execSync(`fuser -k ${port}/tcp`, { stdio: "ignore" });
|
|
148
|
+
}
|
|
149
|
+
} catch {
|
|
150
|
+
/* ignore */
|
|
151
|
+
}
|
|
152
|
+
setTimeout(startServer, 500);
|
|
153
|
+
} else {
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!fs.existsSync(serveDir)) {
|
|
160
|
+
console.error(`Serve directory not found: ${serveDir}`);
|
|
161
|
+
console.error('Run "dithrkit export --web" first to create a web build.');
|
|
162
|
+
process.exit(1);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
startServer();
|