-
Notifications
You must be signed in to change notification settings - Fork 130
/
index.js
319 lines (284 loc) · 10.3 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { WorkerError, parsePath, parseExpiration, genRandStr, decode, params, encodeRFC5987ValueChars, getDispFilename } from "./common.js";
import { handleOptions, corsWrapResponse } from './cors.js'
import { makeHighlight } from "./highlight.js"
import { parseFormdata, getBoundary } from "./parseFormdata.js"
import { getStaticPage } from './staticPages.js'
import { makeMarkdown } from "./markdown.js";
import { getType } from "mime/lite.js"
import {verifyAuth} from "./auth.js";
export default {
async fetch(request, env, ctx) {
return await handleRequest(request, env, ctx)
}
}
async function handleRequest(request, env, ctx) {
try {
if (request.method === "OPTIONS") {
return handleOptions(request)
} else {
const response = await handleNormalRequest(request, env, ctx)
if (response.status !== 302 && response.headers !== undefined) { // because Cloudflare do not allow modifying redirect headers
response.headers.set("Access-Control-Allow-Origin", "*")
}
return response
}
} catch (e) {
console.log(e.stack)
if (e instanceof WorkerError) {
return corsWrapResponse(new Response(`Error ${e.statusCode}: ${e.message}\n`, { status: e.statusCode }))
} else {
return corsWrapResponse(new Response(`Error 500: ${e.message}\n`, { status: 500 }))
}
}
}
async function handleNormalRequest(request, env, ctx) {
if (request.method === "POST") {
return await handlePostOrPut(request, env, ctx, false)
} else if (request.method === "GET") {
return await handleGet(request, env, ctx)
} else if (request.method === "DELETE") {
return await handleDelete(request, env, ctx)
} else if (request.method === "PUT") {
return await handlePostOrPut(request, env, ctx, true)
} else {
throw new WorkerError(405, "method not allowed")
}
}
async function handlePostOrPut(request, env, ctx, isPut) {
const authResponse = verifyAuth(request, env)
if (authResponse !== null) {
return authResponse
}
const contentType = request.headers.get("content-type") || ""
const url = new URL(request.url)
// parse formdata
let form = {}
if (contentType.includes("multipart/form-data")) {
// because cloudflare runtime treat all formdata part as strings thus corrupting binary data,
// we need to manually parse formdata
const uint8Array = new Uint8Array(await request.arrayBuffer())
try {
form = parseFormdata(uint8Array, getBoundary(contentType))
} catch (e) {
throw new WorkerError(400, "error occurs when parsing formdata")
}
} else {
throw new WorkerError(400, `bad usage, please use 'multipart/form-data' instead of ${contentType}`)
}
const content = form.get("c") && form.get("c").content
const filename = form.get("c") && getDispFilename(form.get("c").fields)
const name = form.get("n") && decode(form.get("n").content)
const isPrivate = form.get("p") !== undefined
const passwd = form.get("s") && decode(form.get("s").content)
const expire =
form.has("e") && form.get("e").content.byteLength > 0
? decode(form.get("e").content)
: undefined
// check if paste content is legal
if (content === undefined) {
throw new WorkerError(400, "cannot find content in formdata")
} else if (content.length > params.MAX_LEN) {
throw new WorkerError(413, "payload too large")
}
// check if expiration is legal
let expirationSeconds = undefined
if (expire !== undefined) {
expirationSeconds = parseExpiration(expire)
if (isNaN(expirationSeconds)) {
throw new WorkerError(400, `cannot parse expire ${expirationSeconds} as an number`)
}
if (expirationSeconds < 60) {
throw new WorkerError(
400,
`due to limitation of Cloudflare, expire should be a integer greater than 60, '${expirationSeconds}' given`,
)
}
}
// check if name is legal
if (name !== undefined && !params.NAME_REGEX.test(name)) {
throw new WorkerError(
400,
`Name ${name} not satisfying regexp ${params.NAME_REGEX}`,
)
}
function makeResponse(created) {
return new Response(JSON.stringify(created, null, 2), {
headers: { "content-type": "application/json;charset=UTF-8" },
})
}
if (isPut) {
const { short, passwd } = parsePath(url.pathname)
const item = await env.PB.getWithMetadata(short)
if (item.value === null) {
throw new WorkerError(404, `paste of name '${short}' is not found`)
} else {
const date = item.metadata.postedAt
if (passwd !== item.metadata.passwd) {
throw new WorkerError(403, `incorrect password for paste '${short}`)
} else {
return makeResponse(
await createPaste(env, content, isPrivate, expirationSeconds, short, date, passwd, filename),
)
}
}
} else {
let short = undefined
if (name !== undefined) {
short = "~" + name
if ((await env.PB.get(short)) !== null)
throw new WorkerError(409, `name '${name}' is already used`)
}
return makeResponse(await createPaste(
env, content, isPrivate, expirationSeconds, short, undefined, passwd, filename
))
}
}
function staticPageCacheHeader(env) {
const age = env.CACHE_STATIC_PAGE_AGE
return age ? { "cache-control": `public, max-age=${age}` } : {}
}
function pasteCacheHeader(env) {
const age = env.CACHE_STATIC_PAGE_AGE
return age ? { "cache-control": `public, max-age=${age}` } : {}
}
function lastModifiedHeader(paste) {
const lastModified = paste.metadata.lastModified
return lastModified ? { 'last-modified': new Date(lastModified).toGMTString() } : {}
}
async function handleGet(request, env, ctx) {
const url = new URL(request.url)
const { role, short, ext, passwd, filename } = parsePath(url.pathname)
if (url.pathname == '/favicon.ico' && env.FAVICON) {
return Response.redirect(env.FAVICON)
}
const staticPageContent = getStaticPage(url.pathname, env)
if (staticPageContent) {
// access to all static pages requires auth
const authResponse = verifyAuth(request, env)
if (authResponse !== null) {
return authResponse
}
return new Response(staticPageContent, {
headers: { "content-type": "text/html;charset=UTF-8", ...staticPageCacheHeader(env) }
})
}
// return the editor for admin URL
if (passwd.length > 0) {
const item = await env.PB.get('index')
return new Response(item, {
headers: { "content-type": "text/html;charset=UTF-8" }
})
}
const mime = url.searchParams.get("mime") || getType(ext) || "text/plain"
const disp = url.searchParams.has("a") ? "attachment" : "inline"
const item = await env.PB.getWithMetadata(short, { type: "arrayBuffer" })
// when paste is not found
if (item.value === null) {
throw new WorkerError(404, `paste of name '${short}' not found`)
}
// check `if-modified-since`
const pasteLastModified = item.metadata.lastModified
const headerModifiedSince = request.headers.get('if-modified-since')
if (pasteLastModified && headerModifiedSince) {
let pasteLastModifiedMs = Date.parse(pasteLastModified)
pasteLastModifiedMs -= pasteLastModifiedMs % 1000 // deduct the milliseconds parts
const headerIfModifiedMs = Date.parse(headerModifiedSince)
if (pasteLastModifiedMs <= headerIfModifiedMs) {
return new Response(null, {
status: 304, // Not Modified
headers: lastModifiedHeader(item)
})
}
}
// determine filename with priority: url path > meta
const returnFilename = filename || item.metadata.filename
// handle URL redirection
if (role === "u") {
return Response.redirect(decode(item.value), 302)
}
// handle article (render as markdown)
if (role === "a") {
const md = makeMarkdown(decode(item.value))
return new Response(md, {
headers: { "content-type": `text/html;charset=UTF-8`, ...pasteCacheHeader(env), ...lastModifiedHeader(item) },
})
}
// handle language highlight
const lang = url.searchParams.get("lang")
if (lang) {
return new Response(makeHighlight(decode(item.value), lang), {
headers: { "content-type": `text/html;charset=UTF-8`, ...pasteCacheHeader(env) , ...lastModifiedHeader(item)},
})
} else {
// handle default
const headers = { "content-type": `${mime};charset=UTF-8`, ...pasteCacheHeader(env) , ...lastModifiedHeader(item)}
if (returnFilename) {
const encodedFilename = encodeRFC5987ValueChars(returnFilename)
headers["content-disposition"] = `${disp}; filename*=UTF-8''${encodedFilename}`
} else {
headers["content-disposition"] = `${disp}`
}
return new Response(item.value, { headers })
}
}
async function handleDelete(request, env, ctx) {
const url = new URL(request.url)
const { short, passwd } = parsePath(url.pathname)
const item = await env.PB.getWithMetadata(short)
if (item.value === null) {
throw new WorkerError(404, `paste of name '${short}' not found`)
} else {
if (passwd !== item.metadata.passwd) {
throw new WorkerError(403, `incorrect password for paste '${short}`)
} else {
await env.PB.delete(short)
return new Response("the paste will be deleted in seconds")
}
}
}
async function createPaste(env, content, isPrivate, expire, short, createDate, passwd, filename) {
createDate = createDate || new Date().toISOString()
passwd = passwd || genRandStr(params.ADMIN_PATH_LEN)
const short_len = isPrivate ? params.PRIVATE_RAND_LEN : params.RAND_LEN
if (short === undefined) {
while (true) {
short = genRandStr(short_len)
if ((await env.PB.get(short)) === null) break
}
}
await env.PB.put(short, content, {
expirationTtl: expire,
metadata: {
postedAt: createDate,
passwd: passwd,
filename: filename,
lastModified: new Date().toISOString(),
},
})
let accessUrl = env.BASE_URL + '/' + short
const adminUrl = env.BASE_URL + '/' + short + params.SEP + passwd
return {
url: accessUrl,
suggestUrl: suggestUrl(content, filename, short, env.BASE_URL),
admin: adminUrl,
isPrivate: isPrivate,
expire: expire || null,
}
}
function suggestUrl(content, filename, short, baseUrl) {
function isUrl(text) {
try {
new URL(text)
return true
} catch (e) {
return false
}
}
if (filename) {
return `${baseUrl}/${short}/${filename}`
} else if (isUrl(decode(content))) {
return `${baseUrl}/u/${short}`
} else {
return null
}
}