This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
207 lines (196 loc) · 6.39 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
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
198
199
200
201
202
203
204
205
206
207
var index_page = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron text-center">
<h1>Lil Link</h1>
<p>Create a new shortlink here</p>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 text-center pt-4">
<form action="/" method="post">
<label for="shortlink">Name:</label>
<input name="shortlink" id="shortlink" value="shortlink" class="form-control input-lg">
</div>
</div>
<div class="row">
<div class="col-md-12 text-center pt-4">
<label for="url">Destination URL:</label>
<input name="url" id="url" value="https://example.com" class="form-control input-lg">
</div>
</div>
<div class="row">
<div class="col-md-12 text-center pt-4 pb-5">
<button class="btn btn-secondary btn-lg btn-block">Create Link</button>
</div>
</div>
</form>
</div>
$ADBANNER
</body>
</html>
`;
var ad_banner = `
<a style="display:block" href="https://github.com/eldridgea/lil-link">
<div class="jumbotron text-center border">
<h1>Deploy shortlinks for your organzation!</h1>
<h2>Your very own internal branded shortlinks</h2>
</div>
</a>
`;
var no_ad_banner = `
<div class="jumbotron text-center border">
</div>
</a>
`;
var created = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron text-center">
<h1>Lil Link</h1>
</div>
<div class="container">
<div class="row pt-4">
<div class="col-md-12 text-center">
Your shortlink <a href="$SHORTLINK">$SHORTLINK</a> has been created pointing to <a href="$URL">$URL</a>.
</div>
</div>
</div>
</body>
</html>
`;
var already_exists = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron text-center">
<h1>Lil Link</h1>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 text-center pt-4">
I'm sorry, the shortlink <a href="$SHORTLINK">$SHORTLINK</a> already exists.
</div>
</div>
</div>
</body>
</html>
`;
var doesnt_exist = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron text-center">
<h1>Lil Link</h1>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 text-center pt-4">
I'm sorry, that shortlink doesn't exist. <a href="/">Create it now!</a>
</div>
</div>
</div>
</body>
</html>
`;
/**
* rawHtmlResponse delievers a response with HTML inputted directly
* into the worker script
* @param {string} html
*/
async function rawHtmlResponse(html) {
const init = {
headers: {
"content-type": "text/html;charset=UTF-8"
}
};
return new Response(html, init);
}
//intercepts HTTP requests and cals functions based on if request is POST or GET
addEventListener("fetch", event => {
const { request } = event;
const { url } = request;
if (request.method === "POST") {
return event.respondWith(handlePostRequest(request));
} else if (request.method === "GET") {
//return event.resondWith(handleGetRequest(requet));
return event.respondWith(handleGetRequest(request));
}
});
async function handleGetRequest(request) {
const url = request.url;
var removeHttp = url.slice(url.indexOf("//") + 2); //removes everything before the doubleslash, e.g. http://
var afterSlash = removeHttp.slice(removeHttp.indexOf("/") + 1); //removes everything before the slash
if (afterSlash === "") {
//if there's nothing after the slash, reurn the create shortlink form
if (request.url.includes('//lillink.co')) {
index_page = index_page.replace(/\$ADBANNER/g, ad_banner)
return rawHtmlResponse(index_page);
} else {
index_page = index_page.replace(/\$ADBANNER/g, no_ad_banner)
return rawHtmlResponse(index_page);
}
} else if (afterSlash === "admin") {
return new Response("admin stub area"); //stub out /admin for future usage so early deployments can't reserve that.
} else if (afterSlash.startsWith("admin/")) {
return new Response("admin stub area"); //stub out /admin for future usage so early deployments can't reserve that.
}
else{
return handleRedirect(afterSlash);
}
}
async function handleRedirect(shortlink) {
const getCache = () => SHORTLINKS.get(shortlink);
const url = await getCache();
if (url === null) {
doesnt_exist = doesnt_exist.replace(/\$SHORTLINK/g, shortlink);
return rawHtmlResponse(doesnt_exist);
} else {
const getHits = () => HITS.get(shortlink);
const hits = await getHits();
await HITS.put(shortlink, Number(hits) + 1);
return Response.redirect(url, 302);
}
}
//Function to take JSON POST request and return JSON as a string with HTTP Code 200
async function handlePostRequest(request) {
const body = await request.formData();
var shortlink = body.get("shortlink");
var url = body.get("url");
const getCache = () => SHORTLINKS.get(shortlink);
const url2 = await getCache();
if (url2 === null) {
if (url.startsWith("http://")) {
} else if (url.startsWith("https://")) {
} else {
url = "http://" + url;
}
await SHORTLINKS.put(shortlink, url);
await HITS.put(shortlink, 0);
full_shortlink_url = request.url + shortlink
created = created.replace(/\$SHORTLINK/g, full_shortlink_url)
created = created.replace(/\$URL/g, url)
return rawHtmlResponse(created);
} else {
already_exists = already_exists.replace(/\$SHORTLINK/g, request.url + shortlink);
return rawHtmlResponse(already_exists);
}
}