svamp-cli 0.1.76 → 0.1.78
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/{agentCommands-NVZzP_Vo.mjs → agentCommands-uNFhhdN1.mjs} +16 -51
- package/dist/cli.mjs +82 -30
- package/dist/{commands-lJ8V7MJE.mjs → commands-B6FEeZeP.mjs} +32 -36
- package/dist/{commands-CADr1mQg.mjs → commands-BYbuedOK.mjs} +4 -4
- package/dist/{commands-7Iw1nFwf.mjs → commands-Cf3mXxPZ.mjs} +2 -2
- package/dist/index.mjs +1 -1
- package/dist/{package-Dpz1MLO4.mjs → package-DTOqWYBv.mjs} +2 -2
- package/dist/{run-B29grSMh.mjs → run-DqvxMsWh.mjs} +1 -1
- package/dist/{run-BnFGIK0c.mjs → run-DsXDjwLW.mjs} +199 -50
- package/dist/staticServer-CWcmMF5V.mjs +477 -0
- package/dist/{tunnel-C2kqST5d.mjs → tunnel-BDKdemh0.mjs} +51 -9
- package/package.json +2 -2
- package/dist/staticServer-B-S9sl6E.mjs +0 -198
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import * as http from 'http';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import * as net from 'net';
|
|
5
|
-
|
|
6
|
-
const MIME_TYPES = {
|
|
7
|
-
".html": "text/html; charset=utf-8",
|
|
8
|
-
".htm": "text/html; charset=utf-8",
|
|
9
|
-
".css": "text/css; charset=utf-8",
|
|
10
|
-
".js": "application/javascript; charset=utf-8",
|
|
11
|
-
".mjs": "application/javascript; charset=utf-8",
|
|
12
|
-
".json": "application/json; charset=utf-8",
|
|
13
|
-
".xml": "application/xml; charset=utf-8",
|
|
14
|
-
".csv": "text/csv; charset=utf-8",
|
|
15
|
-
".txt": "text/plain; charset=utf-8",
|
|
16
|
-
".md": "text/markdown; charset=utf-8",
|
|
17
|
-
".png": "image/png",
|
|
18
|
-
".jpg": "image/jpeg",
|
|
19
|
-
".jpeg": "image/jpeg",
|
|
20
|
-
".gif": "image/gif",
|
|
21
|
-
".svg": "image/svg+xml",
|
|
22
|
-
".ico": "image/x-icon",
|
|
23
|
-
".webp": "image/webp",
|
|
24
|
-
".avif": "image/avif",
|
|
25
|
-
".woff": "font/woff",
|
|
26
|
-
".woff2": "font/woff2",
|
|
27
|
-
".ttf": "font/ttf",
|
|
28
|
-
".otf": "font/otf",
|
|
29
|
-
".eot": "application/vnd.ms-fontobject",
|
|
30
|
-
".pdf": "application/pdf",
|
|
31
|
-
".zip": "application/zip",
|
|
32
|
-
".gz": "application/gzip",
|
|
33
|
-
".tar": "application/x-tar",
|
|
34
|
-
".wasm": "application/wasm",
|
|
35
|
-
".mp4": "video/mp4",
|
|
36
|
-
".webm": "video/webm",
|
|
37
|
-
".mp3": "audio/mpeg",
|
|
38
|
-
".ogg": "audio/ogg",
|
|
39
|
-
".wav": "audio/wav",
|
|
40
|
-
".yaml": "text/yaml; charset=utf-8",
|
|
41
|
-
".yml": "text/yaml; charset=utf-8",
|
|
42
|
-
".toml": "text/plain; charset=utf-8"
|
|
43
|
-
};
|
|
44
|
-
function getMimeType(filePath) {
|
|
45
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
46
|
-
return MIME_TYPES[ext] || "application/octet-stream";
|
|
47
|
-
}
|
|
48
|
-
function setCorsHeaders(res) {
|
|
49
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
50
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
|
|
51
|
-
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
52
|
-
res.setHeader("Access-Control-Max-Age", "86400");
|
|
53
|
-
}
|
|
54
|
-
async function findAvailablePort(startPort) {
|
|
55
|
-
return new Promise((resolve, reject) => {
|
|
56
|
-
const server = net.createServer();
|
|
57
|
-
server.listen(startPort, "127.0.0.1", () => {
|
|
58
|
-
const addr = server.address();
|
|
59
|
-
server.close(() => resolve(addr.port));
|
|
60
|
-
});
|
|
61
|
-
server.on("error", () => {
|
|
62
|
-
if (startPort < 65535) {
|
|
63
|
-
findAvailablePort(startPort + 1).then(resolve, reject);
|
|
64
|
-
} else {
|
|
65
|
-
reject(new Error("No available ports"));
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
function generateDirectoryListing(dirPath, urlPath) {
|
|
71
|
-
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
72
|
-
const rows = entries.sort((a, b) => {
|
|
73
|
-
if (a.isDirectory() && !b.isDirectory()) return -1;
|
|
74
|
-
if (!a.isDirectory() && b.isDirectory()) return 1;
|
|
75
|
-
return a.name.localeCompare(b.name);
|
|
76
|
-
}).map((entry) => {
|
|
77
|
-
const name = entry.isDirectory() ? `${entry.name}/` : entry.name;
|
|
78
|
-
const href = path.posix.join(urlPath, entry.name) + (entry.isDirectory() ? "/" : "");
|
|
79
|
-
let size = "";
|
|
80
|
-
if (!entry.isDirectory()) {
|
|
81
|
-
try {
|
|
82
|
-
const stat = fs.statSync(path.join(dirPath, entry.name));
|
|
83
|
-
size = formatSize(stat.size);
|
|
84
|
-
} catch {
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return `<tr><td><a href="${escapeHtml(href)}">${escapeHtml(name)}</a></td><td>${size}</td></tr>`;
|
|
88
|
-
}).join("\n");
|
|
89
|
-
const parent = urlPath !== "/" ? `<tr><td><a href="${escapeHtml(path.posix.dirname(urlPath))}/">..</a></td><td></td></tr>
|
|
90
|
-
` : "";
|
|
91
|
-
return `<!DOCTYPE html>
|
|
92
|
-
<html><head><meta charset="utf-8"><title>Index of ${escapeHtml(urlPath)}</title>
|
|
93
|
-
<style>body{font-family:monospace;margin:2em}table{border-collapse:collapse}td{padding:4px 16px}a{text-decoration:none;color:#0366d6}a:hover{text-decoration:underline}tr:hover{background:#f6f8fa}</style>
|
|
94
|
-
</head><body><h1>Index of ${escapeHtml(urlPath)}</h1><table>${parent}${rows}</table></body></html>`;
|
|
95
|
-
}
|
|
96
|
-
function escapeHtml(s) {
|
|
97
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
98
|
-
}
|
|
99
|
-
function formatSize(bytes) {
|
|
100
|
-
if (bytes < 1024) return `${bytes} B`;
|
|
101
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
102
|
-
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
103
|
-
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
|
|
104
|
-
}
|
|
105
|
-
async function startStaticServer(options) {
|
|
106
|
-
const { directory, listing = true } = options;
|
|
107
|
-
const rootDir = path.resolve(directory);
|
|
108
|
-
if (!fs.existsSync(rootDir) || !fs.statSync(rootDir).isDirectory()) {
|
|
109
|
-
throw new Error(`Not a directory: ${rootDir}`);
|
|
110
|
-
}
|
|
111
|
-
const server = http.createServer((req, res) => {
|
|
112
|
-
setCorsHeaders(res);
|
|
113
|
-
if (req.method === "OPTIONS") {
|
|
114
|
-
res.writeHead(204);
|
|
115
|
-
res.end();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
119
|
-
res.writeHead(405, { "Content-Type": "text/plain" });
|
|
120
|
-
res.end("Method Not Allowed");
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const url = new URL(req.url || "/", `http://localhost`);
|
|
124
|
-
const decodedPath = decodeURIComponent(url.pathname);
|
|
125
|
-
const normalizedPath = path.normalize(decodedPath);
|
|
126
|
-
if (normalizedPath.includes("..")) {
|
|
127
|
-
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
128
|
-
res.end("Forbidden");
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const filePath = path.join(rootDir, normalizedPath);
|
|
132
|
-
if (!filePath.startsWith(rootDir)) {
|
|
133
|
-
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
134
|
-
res.end("Forbidden");
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
try {
|
|
138
|
-
const stat = fs.statSync(filePath);
|
|
139
|
-
if (stat.isDirectory()) {
|
|
140
|
-
const indexPath = path.join(filePath, "index.html");
|
|
141
|
-
if (fs.existsSync(indexPath) && fs.statSync(indexPath).isFile()) {
|
|
142
|
-
serveFile(indexPath, req, res);
|
|
143
|
-
} else if (listing) {
|
|
144
|
-
if (!decodedPath.endsWith("/")) {
|
|
145
|
-
res.writeHead(301, { Location: decodedPath + "/" });
|
|
146
|
-
res.end();
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const html = generateDirectoryListing(filePath, decodedPath);
|
|
150
|
-
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
151
|
-
res.end(html);
|
|
152
|
-
} else {
|
|
153
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
154
|
-
res.end("Not Found");
|
|
155
|
-
}
|
|
156
|
-
} else if (stat.isFile()) {
|
|
157
|
-
serveFile(filePath, req, res);
|
|
158
|
-
} else {
|
|
159
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
160
|
-
res.end("Not Found");
|
|
161
|
-
}
|
|
162
|
-
} catch {
|
|
163
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
164
|
-
res.end("Not Found");
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
const port = await findAvailablePort(options.port || 18080);
|
|
168
|
-
return new Promise((resolve, reject) => {
|
|
169
|
-
server.listen(port, "127.0.0.1", () => {
|
|
170
|
-
resolve({
|
|
171
|
-
server,
|
|
172
|
-
port,
|
|
173
|
-
close: () => server.close()
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
server.on("error", reject);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
function serveFile(filePath, req, res) {
|
|
180
|
-
const stat = fs.statSync(filePath);
|
|
181
|
-
const contentType = getMimeType(filePath);
|
|
182
|
-
res.writeHead(200, {
|
|
183
|
-
"Content-Type": contentType,
|
|
184
|
-
"Content-Length": stat.size,
|
|
185
|
-
"Cache-Control": "no-cache"
|
|
186
|
-
});
|
|
187
|
-
if (req.method === "HEAD") {
|
|
188
|
-
res.end();
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const stream = fs.createReadStream(filePath);
|
|
192
|
-
stream.pipe(res);
|
|
193
|
-
stream.on("error", () => {
|
|
194
|
-
res.end();
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export { startStaticServer };
|