-
Notifications
You must be signed in to change notification settings - Fork 53
/
worker.js
163 lines (149 loc) · 4.5 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
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
/** @type {any} Your Key to secure POST requests with a token. null if allowed */
const POSTKEY = null;
/** @type {any} Your Key to secure DELETE requests with a token. null if allowed */
const DELETEKEY = null;
/** You can edit this variable to change KV Name Binding */
const KV_NAMESPACE = TUHIN;
/**
* Handle the request
* @param {Request} request
*/
async function handleRequest(request) {
try {
/** @type {string} Hostname of the worker */
const host = request.headers.get("host");
/** @type {string} Path of the request */
const path = new URL(request.url).pathname;
/**
* Handle the request
* @param {string} key Key to be searched from URL param
* @return {string} Value of the key
*/
function getUrlParam(key) {
return new URL(request.url).searchParams.get(key);
}
switch (request.method) {
case "OPTIONS": {
/** Handle Preflight */
return jsonResponse({
data: { msg: "Preflight request success 🤝" },
});
}
case "POST": {
/** Handle POST */
if (POSTKEY && getUrlParam("key") !== POSTKEY) {
/** Unauthorized */
return jsonResponse({
data: { status: false, msg: "Invalid key, Unauthorized!" },
status: 403,
});
} else {
/** Authorized to save payload */
/** @type {object} Request payload */
var payload = await request.json();
/** @type {object} Save and return payload */
var save = await savePayload(payload);
/** Send response */
return jsonResponse({
data: {
status: true,
_id: save._id,
query: `https://${host}/${save._id}`,
data: save,
},
});
}
}
case "GET": {
/** Handle GET */
if (path === "/") {
return jsonResponse({
data: {
status: "Running",
},
});
} else {
/** @type {object} Retrive payload from ID */
var getData = await KV_NAMESPACE.get(path.substring(1));
if (getData) {
return jsonResponse({
data: JSON.parse(getData),
});
} else {
return jsonResponse({
data: {
status: false,
msg: "Not Found",
},
status: 404,
});
}
}
}
case "DELETE": {
if (DELETEKEY && getUrlParam("key") !== DELETEKEY) {
/** Unauthorized */
return jsonResponse({
data: { status: false, msg: "Invalid key, Unauthorized!" },
status: 403,
});
} else {
/** Authorized to delete payload */
await KV_NAMESPACE.delete(path.substring(1));
return jsonResponse({
data: {
status: true,
msg: "Deleted Successfully",
},
});
}
}
default: {
/** Handle unknown request */
throw new Error("Invalid request method");
}
}
} catch (error) {
return jsonResponse({
data: { status: false, msg: error.message },
status: 500,
});
}
}
/**
* Send a JSON response to the client
*
* @param {object} obj - Response
* @param {any} obj.data - Body of the response
* @param {number} obj.status - Status code of the response
* @param {object} obj.headers - Headers of the response
* @returns {Response}
*/
function jsonResponse({ data = null, status = 200, headers = {} }) {
return new Response(JSON.stringify(data), {
status,
headers: {
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET,POST,DELETE,OPTIONS",
"Access-Control-Max-Age": "86400",
"Content-Type": "application/json",
"Cache-Control": "no-cache, no-store, must-revalidate",
...headers,
},
});
}
/**
* Send a JSON response to the client
*
* @param {Object} payload - Payload to be saved
*/
async function savePayload(payload) {
if (!payload._id)
payload._id =
new Date().getTime() + Math.random().toString(36).substring(9);
await KV_NAMESPACE.put(payload._id, JSON.stringify(payload));
return payload;
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});