forked from Setariaeas/Unlock-netease-cloud-music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.worker.js
More file actions
20 lines (18 loc) · 735 Bytes
/
Copy pathendpoint.worker.js
File metadata and controls
20 lines (18 loc) · 735 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const pattern = /^\/package\/([0-9a-zA-Z_\-=]+)\/(\w+\.\w+)$/
const handleRequest = async request => {
const notFound = new Response(null, { status: 404 })
const path = new URL(request.url).pathname
const [matched, base64Url, fileName] = pattern.exec(path || '') || []
if (!matched) return notFound
let url = base64Url.replace(/-/g, '+').replace(/_/g, '/')
try { url = new URL(atob(url)) } catch(_) { url = null }
if (!url) return notFound
const headers = new Headers(request.headers)
headers.set('host', url.host)
headers.delete('cookie')
const { method, body } = request
return fetch(url, { method, headers, body })
}