-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·159 lines (153 loc) · 3.84 KB
/
index.js
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
#!/usr/bin/env -S bun run
const curl = fetch;
const backend = "http://127.0.0.1:3000";
const date = (json) => {
if (!json)
return "";
else if (json.publishedText)
return json.publishedText;
else if (json.published) {
const dt = new Date(json.published * 1000);
return `since ${dt.toISOString()}`;
} else
return "";
};
// Fetch functions (nl = NewLeaf)
const nl = {
search: async (query) => {
if (!query)
return new Response({});
return await curl(backend + "/api/v1/search?q=" + query)
.then(res => res.json());
},
video: async (id) => {
if (!id)
return new Response({});
return await curl(backend + "/api/v1/videos/" + id)
.then(res => res.json());
},
channel: async (ucid) => {
if (!ucid)
return new Response({});
return await curl(backend + "/api/v1/channels/" + ucid)
.then(res => res.json());
}
};
// Render functions
const render = {
search: (json) => {
if (!json)
return "";
let html = "<ul>\n";
for (let i = 0; i < json.length; i++) {
const o = json[i];
html += `<li>${o.second__lengthText} <a href="/watch?v=${o.videoId}">${o.title}</a> by <a href="/channel/${o.authorId}">${o.author}</a> ${date(o)}</li>\n`;
}
html += "</ul>\n";
return html;
},
video: (json) => {
if (!json)
return "";
let html = `<h1>${json.title}</h1>\n`;
html += "<video controls>";
for (let i = 0; i < json.formatStreams.length; i++) {
const o = json.formatStreams[i];
html += `\n\t<source src="${o.url}" type="${o.second__mime}" />`;
}
html += "\n</video>";
html += `\n<div>by <a href="/channel/${json.authorId}">${json.author}</a>`;
html += ` ${date(json)}</div>\n`;
html += `\n<div>${json.descriptionHtml}</div>\n`;
html += "\n<div>\n" + render.search(json.recommendedVideos) + "\n</div>\n";
return html;
},
channel: (json) => {
if (!json)
return "";
let html = `<h1>${json.author}</h1>\n`;
html += `\n<div>${json.descriptionHtml}</div>\n`;
html += "<div>\n" + render.search(json.latestVideos) + "\n</div>\n";
return html;
}
};
let argparse_state = "default";
let port = 11102;
for (let i = 2; i < Bun.argv.length; i++) {
switch (argparse_state) {
case "default":
if (Bun.argv[i] == "-p")
argparse_state = "port";
break;
case "port":
port = Number(Bun.argv[i]);
argparse_state = "default";
}
}
Bun.serve({
port: port,
fetch: async (req) => {
// console.log(await curl(backend));
const url = new URL(req.url);
let mread = nl.search;
let mwrite = render.search;
let mparams = undefined;
switch (url.pathname) {
case "/":
mread = () => null;
mwrite = () => "";
mparams = "Startpage";
break;
case "/mimasu.svg":
return new Response(Bun.file("./mimasu.svg"));
case "/mimasu.css":
return new Response(Bun.file("./mimasu.css"));
case "/search":
mread = nl.search;
mwrite = render.search;
mparams = url.searchParams.get("q");
break;
case "/watch":
mread = nl.video;
mwrite = render.video;
mparams = url.searchParams.get("v");
break;
default:
mread = nl.channel;
mwrite = render.channel;
mparams = url.pathname.split("/")[2];
}
const json = await mread(mparams);
const query = url.searchParams.get("q");
return new Response(
`<!doctype html>
<html>
<head>
<title>Mimasu – ${mparams}</title>
<meta charset=utf-8 />
<link rel="shortcut icon" type="image/svg" href="/mimasu.svg" />
<link rel="stylesheet" type="text/css" href="/mimasu.css" />
</head>
<body>
<a id=icon class="fixbar" href="/">
<img src="/mimasu.svg" alt="MIMASU" />
</a>
<form id=searchbar class="fixbar" action="/search" method="get">
<input name=q type="text" ${query ? "value=\"" + query + "\"" : ""} placeholder="Search" />
<button>Search</button>
</form>
<div id=content>
${mwrite(json)}
</div>
</body>
</html>
`
, {
status: 200,
statusText: "OK",
headers: {
"Content-Type": "text/html"
}
});
}
});