Skip to content

Commit

Permalink
patch fetch ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Sep 27, 2024
1 parent c5a8c99 commit 6c23b7f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/patch/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
const { fetch: originalFetch } = globalThis
const base = 'https://synergism.cc/'

const patchedFetch: typeof globalThis.fetch = async (input, init) => {
const request = new Request(input, init)

try {
return await originalFetch.apply(null, [input, init])
} catch {
const inputURL = new URL(typeof input === 'string' ? input : (input as Request).url)
const newInit = typeof input === 'string'
? undefined
: init
return await originalFetch.call(null, request)
} catch (e) {
if (location.origin === 'https://1289263890445631581.discordsays.com') {
const requestURL = new URL(request.url)
const url = new URL(requestURL.pathname, location.origin)
url.pathname = `/.proxy${requestURL.pathname}`
url.search = requestURL.search
url.hash = requestURL.hash

return originalFetch.call(null, url, request)
}

const url = new URL(inputURL, base)
url.pathname = `/.proxy${inputURL.pathname}`
url.search = inputURL.search
url.hash = inputURL.hash
return await originalFetch.apply(null, [url, newInit])
throw e
}
}

Expand Down

0 comments on commit 6c23b7f

Please sign in to comment.