-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.ts
59 lines (45 loc) · 1.76 KB
/
mod.ts
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
import { Application, Ngrok, path, send } from "./deps.ts"
type hcloadParams = {
files?: string[]
urls?: string[]
}
export default function(options: hcloadParams): Promise<string[]> {
return new Promise(resolve => {
if (!(options.files || options.urls)) throw "Missing params"
const baseMap: Record<string, string> = {}
if (options.files) {
options.files.forEach((file: string) => {
baseMap[path.parse(file).base] = file
})
}
const controller = new AbortController()
const app = new Application()
app.use(async (context) => {
await send(context, baseMap[decodeURI(context.request.url.pathname.substring(1))], { root: "/" })
})
app.addEventListener("listen", async ({ port }) => {
const ngrok = await Ngrok.create({ protocol: "http", port })
ngrok.addEventListener("ready", async (event) => {
const ngrokUrl = "https://" + event.detail
const data = []
if (options.files) {
Object.keys(baseMap).forEach(base => {
data.push(ngrokUrl + "/" + base)
})
}
if (options.urls) {
data.push(...options.urls)
}
const response = await fetch("https://cdn.hackclub.com/api/new", {
method: "POST",
body: JSON.stringify(data),
})
const uploadedURLs = await response.json()
await ngrok.destroy()
resolve(uploadedURLs)
controller.abort()
})
})
app.listen({ port: 20845, signal: controller.signal })
})
}