-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathworker.js
128 lines (111 loc) · 3.46 KB
/
worker.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
addEventListener('fetch', event => {
const { request } = event;
switch (request.method) {
case 'POST':
return event.respondWith(handlePOST(request));
case 'DELETE':
return event.respondWith(handleDELETE(request));
default:
return event.respondWith(handleRequest(request, event));
}
});
const html = `<!DOCTYPE html>
<body>
<pre>
use an actual path if you're trying to fetch something.
send a POST request with form data "url" and "path" if you're trying to put something.
set x-preshared-key header for authentication.
source: <a href="https://github.com/VandyHacks/vhl.ink">VandyHacks/vhl.ink</a>
</pre>
</body>`;
/**
* Respond to POST requests with shortened URL creation
* @param {Request} request
*/
async function handlePOST(request) {
const psk = request.headers.get('x-preshared-key');
if (psk !== SECRET_KEY)
return new Response('Sorry, bad key.', { status: 403 });
const shortener = new URL(request.url);
const data = await request.formData();
const redirectURL = data.get('url');
const path = data.get('path');
if (!redirectURL || !path)
return new Response('`url` and `path` need to be set.', { status: 400 });
// validate redirectURL is a URL
try {
new URL(redirectURL);
} catch (e) {
if (e instanceof TypeError)
return new Response('`url` needs to be a valid http url.', { status: 400 });
else throw e;
};
// will overwrite current path if it exists
await LINKS.put(path, redirectURL);
return new Response(`${redirectURL} available at ${shortener}${path}`, {
status: 201,
});
}
/**
* Respond to DELETE requests by deleting the shortlink
* @param {Request} request
*/
async function handleDELETE(request) {
const psk = request.headers.get('x-preshared-key');
if (psk !== SECRET_KEY)
return new Response('Sorry, bad key.', { status: 403 });
const url = new URL(request.url);
const path = url.pathname.split('/')[1];
if (!path) return new Response('Not found', { status: 404 });
await LINKS.delete(path);
return new Response(`${request.url} deleted!`, { status: 200 });
}
/**
* Respond to GET requests with redirects.
*
* Authenticated GET requests without a path will return a list of all
* shortlinks registered with the service.
* @param {Request} request
*/
async function handleRequest(request, event) {
const url = new URL(request.url);
const path = url.pathname.split('/')[1];
if (!path) {
// Return list of available shortlinks if user supplies admin credentials.
const psk = request.headers.get('x-preshared-key');
if (psk === SECRET_KEY) {
const { keys } = await LINKS.list();
let paths = "";
keys.forEach(element => paths += `${element.name}\n`);
return new Response(paths, { status: 200 });
}
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
}
if (path === 'quack') {
const resObject = {
text: 'You just got ducked 🦆',
response_type: 'in_channel',
};
// Just hope it works lol
await fetch(SLACK_WEBHOOK_QUACK, {
method: 'POST',
body: JSON.stringify(resObject),
headers: { 'Content-Type': 'application/json' },
});
}
const redirectURL = await LINKS.get(path);
if (redirectURL) {
const analyticsReq = {
method: 'POST',
body: JSON.stringify({ 'path': path }),
headers: { 'Content-Type': 'application/json' },
};
event.waitUntil(fetch(ANALYTICS_URL, analyticsReq));
return Response.redirect(redirectURL, 302);
}
return new Response('URL not found. Sad!', { status: 404 });
}