-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·197 lines (172 loc) · 5.54 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
import http from "http";
import fs from "fs/promises";
import { join } from "path";
import { fileURLToPath } from "url";
const argv = /** @type {string[]} */ (process.argv.slice(2));
/** @param {string} name */
const findArg = (name) => {
const index = argv.indexOf(name);
if (index === -1) return;
if (!argv[index + 1] || argv[index + 1].startsWith("-")) return "true";
return argv[index + 1];
};
if (findArg("--version") || findArg("-v")) {
const packageJsonpath = join(
fileURLToPath(import.meta.url),
"..",
"package.json"
);
const packageJson = await fs.readFile(packageJsonpath, "utf-8");
const { version } = JSON.parse(packageJson);
console.log(version);
process.exit();
}
// user asked for CLI help
if (findArg("--help") || findArg("-h")) {
console.log(
[
"Usage: logpipe [--port PORT] [--title TITLE]",
"",
"Options:",
" --port <PORT> The port to run the web server on",
" --title <TITLE> The title of the web page",
" --help This menu",
" --version The version of logpipe",
"",
"Examples:",
" your-program | logpipe",
' your-program | logpipe --port 8080 --title "My CLI Input"',
' your-program | logpipe -p 8080 -t "My CLI Input"',
"",
].join("\n")
);
process.exit();
}
const port = Number(findArg("--port") ?? findArg("-p")) || 0;
const DEFAULT_TITLE = "CLI Input";
const title = findArg("--title") ?? findArg("-t") ?? "CLI Input";
/** @typedef {import('./types.d.ts').CliInput} CliInput */
/** @type {CliInput[]} */
const lines = [];
/** @type {Set<(lines: CliInput[], newLines: CliInput[]) => void>} */
const notifiers = new Set();
process.stdin.on("data", (data) => {
/** @type {string} */
const input = data.toString();
const date = Date.now();
const id = String(lines.length);
/** @type {CliInput[]} */
const newLines = [];
let prevWhitespace = 0;
for (const line of input.trim().split("\n")) {
const curWhitespace = line.match(/^\s+/)?.[0].length;
if ((curWhitespace > prevWhitespace || line === "}") && newLines.length) {
const { input } = newLines.pop();
newLines.push({ input: [input, line].join("\n"), date, id });
} else {
newLines.push({ input: line, date, id });
}
}
lines.push(...newLines);
for (const func of notifiers) {
func(lines, newLines);
}
});
process.stdin.on("close", () => {
console.log("\x1b[31mInput has ended.\x1b[0m");
console.log("Use Ctrl+C to close the web server.");
});
/** @param {string} path */
const getMimeTypeForFile = (path) => {
const ext = path.split(".").slice(-1)[0];
/** @type {string} */
const mimeType = {
js: "text/javascript",
css: "text/css",
html: "text/html",
png: "image/png",
jpg: "image/jpg",
jpeg: "image/jpeg",
ico: "image/x-icon",
svg: "image/svg+xml",
json: "application/json",
}[ext];
return mimeType ?? "text/plain";
};
const PUBLIC_DIR = join(fileURLToPath(import.meta.url), "..", "public");
const publicFiles = new Set();
fs.readdir(PUBLIC_DIR).then((files) =>
Promise.all(
files.map(async (file) => {
const stat = await fs.stat(join(PUBLIC_DIR, file));
if (!stat.isDirectory()) {
publicFiles.add(file);
return;
}
const curFolder = join(PUBLIC_DIR, file);
for (const subFile of await fs.readdir(curFolder)) {
publicFiles.add(join(file, subFile));
}
})
)
);
const server = http
.createServer(async (req, res) => {
if (req.method !== "GET") return;
if (req.url === "/_/cli-input") {
res.writeHead(200, {
"Content-Type": "text/event-stream",
Connection: "keep-alive",
"Cache-Control": "no-cache",
});
res.write(`data: ${JSON.stringify(lines)}\n\n`);
notifiers.add((_lines, newLines) => {
res.write(`data: ${JSON.stringify(newLines)}\n\n`);
});
return;
}
// We could directly write to FS, but we shouldn't trust the user to specify download location information correctly.
// This means we'd need a lot of error handling. It's easier to let the browser handle file downloads.
if (req.url === "/_/logs" && req.method === "GET") {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify(lines));
res.end();
return;
}
if (req.url == "/" || req.url == "/index.html") {
res.writeHead(200, {
"Content-Type": "text/html",
"X-Custom-Title": title,
});
/** @type {string} */
const site = (await fs.readFile(join(PUBLIC_DIR, "index.html"), "utf8"))
.replace(/<title>.+?<\/title>/g, `<title>${title}</title>`)
.replace(/<h1>.+?<\/h1>/g, `<h1>${title}</h1>`);
res.write(site);
res.end();
return;
} else if (publicFiles.has(req.url.slice(1))) {
res.writeHead(200, { "Content-Type": getMimeTypeForFile(req.url) });
res.write(await fs.readFile(join(PUBLIC_DIR, req.url.slice(1))));
res.end();
return;
}
res.writeHead(404, { "Content-Type": "text/html" });
res.write("Resource not found");
res.end();
})
.listen(port);
if (!server.address() && port) {
console.error("\nServer could not bind to port", port);
console.log(
"Specify a different port or allow the server to choose a random port."
);
process.exit(1);
}
const address = `http://localhost:${server.address().port}`;
let log = `Logs are displayed on \x1b[32;1;4m${address}\x1b[0m`
if (title !== DEFAULT_TITLE) {
log += ` with title \x1b[34;3m"${title}"\x1b[0m`;
}
console.log(`\n${log}\n`);