-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: @fly/proxy for sending requests to origin servers #81
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
/** | ||
* @module fly | ||
*/ | ||
declare const bridge: any |
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,117 @@ | ||
/** | ||
* @module fly/proxy | ||
* Library for proxying requests to origins. | ||
*/ | ||
/** | ||
* This generates a `fetch` like function for proxying requests to a given origin. | ||
* When this function makes origin requests, it adds standard proxy headers like | ||
* `X-Forwarded-Host` and `X-Forwarded-For`. It also passes headers from the original | ||
* request to the origin. | ||
* @param origin A URL to an origin, can include a path to rebase requests. | ||
* @param options Options and headers to control origin request. | ||
*/ | ||
export default function proxy(origin: string | URL, options?: ProxyOptions) { | ||
return function proxyFetch(req: RequestInfo, init?: RequestInit) { | ||
if (!options) { | ||
options = {} | ||
} | ||
const breq = buildProxyRequest(origin, options, req, init) | ||
return fetch(breq) | ||
} | ||
} | ||
|
||
interface FlyRequest extends Request { | ||
url: string | ||
} | ||
|
||
/** | ||
* @protected | ||
* @hidden | ||
* @param origin | ||
* @param options | ||
* @param req | ||
* @param init | ||
*/ | ||
export function buildProxyRequest(origin: string | URL, options: ProxyOptions, req: RequestInfo, init?: RequestInit) { | ||
|
||
if (typeof req === "string") { | ||
req = new Request(req) | ||
} | ||
const url = new URL(req.url) | ||
let breq: FlyRequest | null = null | ||
|
||
if (req instanceof Request) { | ||
breq = req.clone() | ||
} else { | ||
breq = new Request(req) | ||
} | ||
|
||
if (typeof origin === "string") { | ||
origin = new URL(origin) | ||
} | ||
|
||
url.hostname = origin.hostname | ||
url.protocol = origin.protocol | ||
url.port = origin.port | ||
|
||
if (options.rewritePath && typeof options.rewritePath === 'string') { | ||
// remove basePath so we can serve `onehosthame.com/dir/` from `origin.com/` | ||
url.pathname = url.pathname.substring(options.rewritePath.length) | ||
console.log("rewriting base path:", options.rewritePath, url.pathname) | ||
} | ||
if (origin.pathname && origin.pathname.length > 0) { | ||
url.pathname = [origin.pathname.replace(/\/$/, ''), url.pathname.replace(/^\//, "")].join("/") | ||
} | ||
if (url.pathname.startsWith("//")) { | ||
url.pathname = url.pathname.substring(1) | ||
} | ||
|
||
breq.url = url.toString() | ||
// we extend req with remoteAddr | ||
breq.headers.set("x-forwarded-for", (<any>req).remoteAddr) | ||
breq.headers.set("x-forwarded-host", url.hostname) | ||
|
||
if (options.headers) { | ||
for (const h of Object.getOwnPropertyNames(options.headers)) { | ||
const v = options.headers[h] | ||
if (v === false) { | ||
breq.headers.delete(h) | ||
} else if (v && typeof v === "string") { | ||
breq.headers.set(h, v) | ||
} | ||
} | ||
} | ||
return <Request>breq | ||
} | ||
|
||
/** | ||
* Options for `proxy`. | ||
*/ | ||
export interface ProxyOptions { | ||
/** | ||
* Replace this portion of URL path before making request to origin. | ||
* | ||
* For example, this makes a request to `https://fly.io/path1/to/document.html`: | ||
* ```javascript | ||
* const opts = { rewritePath: "/path2/"} | ||
* const origin = proxy("https://fly.io/path1/", opts) | ||
* origin("https://somehostname.com/path2/to/document.html") | ||
* ``` | ||
*/ | ||
rewritePath?: string, | ||
|
||
/** | ||
* Headers to set on backend request. Each header accepts either a `boolean` or `string`. | ||
* * If set to `false`, strip header entirely before sending. | ||
* * `true` or `undefined` send the header through unmodified from the original request. | ||
* * `string` header values are sent as is | ||
*/ | ||
headers?: { | ||
[key: string]: string | boolean | undefined, | ||
/** | ||
* Host header to set before sending origin request. Some sites only respond to specific | ||
* host headers. | ||
*/ | ||
host?: string | boolean | ||
} | ||
} |
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,20 @@ | ||
import { expect } from 'chai' | ||
|
||
import * as proxy from '@fly/proxy' | ||
|
||
const origin = "https://fly.io/proxy/" | ||
const req = new Request("https://wat.com/path/to/thing", { headers: { "host": "wut.com" } }) | ||
describe("proxy", () => { | ||
it('includes host header and base path properly', () => { | ||
const breq = proxy.buildProxyRequest(origin, {}, req) | ||
const url = new URL(breq.url) | ||
expect(breq.headers.get("host")).to.eq("wut.com") | ||
expect(url.pathname).to.eq("/proxy/path/to/thing") | ||
}) | ||
|
||
it('rewrite paths properly', () => { | ||
const breq = proxy.buildProxyRequest(origin, { rewritePath: "/path/to/" }, req) | ||
const url = new URL(breq.url) | ||
expect(url.pathname).to.eq("/proxy/thing") | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this option confusing.
I usually associate "rewrite" like "replace". Needs 2 arguments in my opinion. Some kind of "from" and "to".
Also, a regex would be nice. Hell, I guess it should act just like
String.prototype.replace
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't really want to make it that complex since that option only exists to support
/mount/
type stuff right now. I did just renamed it tostripPath
though, which I think makes a little more sense.