-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from movie-web/dev
Version 2.1.1: Fix support for JWT on non-Cloudflare platforms
- Loading branch information
Showing
7 changed files
with
176 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 movie-web | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
H3Event, | ||
Duplex, | ||
ProxyOptions, | ||
getProxyRequestHeaders, | ||
RequestHeaders, | ||
} from 'h3'; | ||
|
||
const PayloadMethods = new Set(['PATCH', 'POST', 'PUT', 'DELETE']); | ||
|
||
export interface ExtraProxyOptions { | ||
blacklistedHeaders?: string[]; | ||
} | ||
|
||
function mergeHeaders( | ||
defaults: HeadersInit, | ||
...inputs: (HeadersInit | RequestHeaders | undefined)[] | ||
) { | ||
const _inputs = inputs.filter(Boolean) as HeadersInit[]; | ||
if (_inputs.length === 0) { | ||
return defaults; | ||
} | ||
const merged = new Headers(defaults); | ||
for (const input of _inputs) { | ||
if (input.entries) { | ||
for (const [key, value] of (input.entries as any)()) { | ||
if (value !== undefined) { | ||
merged.set(key, value); | ||
} | ||
} | ||
} else { | ||
for (const [key, value] of Object.entries(input)) { | ||
if (value !== undefined) { | ||
merged.set(key, value); | ||
} | ||
} | ||
} | ||
} | ||
return merged; | ||
} | ||
|
||
export async function specificProxyRequest( | ||
event: H3Event, | ||
target: string, | ||
opts: ProxyOptions & ExtraProxyOptions = {}, | ||
) { | ||
let body; | ||
let duplex: Duplex | undefined; | ||
if (PayloadMethods.has(event.method)) { | ||
if (opts.streamRequest) { | ||
body = getRequestWebStream(event); | ||
duplex = 'half'; | ||
} else { | ||
body = await readRawBody(event, false).catch(() => undefined); | ||
} | ||
} | ||
|
||
const method = opts.fetchOptions?.method || event.method; | ||
const oldHeaders = getProxyRequestHeaders(event); | ||
opts.blacklistedHeaders?.forEach((header) => { | ||
const keys = Object.keys(oldHeaders).filter( | ||
(v) => v.toLowerCase() === header.toLowerCase(), | ||
); | ||
keys.forEach((k) => delete oldHeaders[k]); | ||
}); | ||
|
||
const fetchHeaders = mergeHeaders( | ||
oldHeaders, | ||
opts.fetchOptions?.headers, | ||
opts.headers, | ||
); | ||
(fetchHeaders.forEach as any)(console.log); | ||
|
||
return sendProxy(event, target, { | ||
...opts, | ||
fetchOptions: { | ||
method, | ||
body, | ||
duplex, | ||
...opts.fetchOptions, | ||
headers: fetchHeaders, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters