-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
108 lines (94 loc) · 3.24 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
const Router = require('./router');
const home = require('./html/index');
const renderArticle = require('./html/post');
const shortid = require('shortid');
const sanitizeHtml = require('sanitize-html');
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
/**
* readRequestBody reads in the incoming request body
* Use await readRequestBody(..) in an async function to get the string
* @param {Request} request the incoming request to read from
*/
async function readRequestBody(request) {
const { headers } = request
const contentType = headers.get('content-type')
if (contentType.includes('application/json')) {
const body = await request.json()
return body;
} else if (contentType.includes('application/text')) {
const body = await request.text()
return body
} else if (contentType.includes('text/html')) {
const body = await request.text()
return body
} else if (contentType.includes('form')) {
const formData = await request.formData()
let body = {}
for (let entry of formData.entries()) {
body[entry[0]] = entry[1]
}
return JSON.stringify(body)
} else {
let myBlob = await request.blob()
var objectURL = URL.createObjectURL(myBlob)
return objectURL
}
}
/**
* Sanitizes the keys of data object
* @param {*} data
*/
function sanitize(data) {
const sanitizedData = {};
const keys = Object.keys(data);
keys.forEach(key => {
sanitizedData[key] = data[key] && sanitizeHtml(data[key]);
});
return sanitizedData;
}
async function handleRequest(request) {
const r = new Router();
r.get('/', () => {
return new Response(home, { headers: { 'content-type': 'text/html' } })
});
r.post('/save', async (req) => {
const body = await readRequestBody(req);
const title = body.title, content = body.content, name = body.name;
if (!title || !content) {
return new Response("Please provide a title and content", {
headers: { "Content-Type": "text/html" },
status: 400,
statusText: "Forbidden"
});
}
const key = shortid.generate().toLowerCase();
await articles.put(key, JSON.stringify({ key: key, title: title, name: name, content: content, dateAdded: new Date() }));
return new Response(JSON.stringify({ key: key }), {
headers: { "Content-Type": "application/json" },
status: 200,
statusText: "OK"
});
});
r.get('/p/.+', async (req) => {
const url = new URL(req.url).pathname;
const key = url.substring(url.lastIndexOf('/') + 1).toLowerCase();
let article = await articles.get(key);
if (!article) {
return new Response("Not Found", {
headers: { "Content-Type": "text/html" },
status: 404,
statusText: "Not Found"
});
}
article = sanitize(JSON.parse(article));
return new Response(renderArticle(article), {
headers: { "Content-Type": "text/html" },
status: 200,
statusText: "OK"
});
});
const resp = await r.route(request);
return resp;
}