Skip to content
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
merged 3 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/bridge/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @module fly
*/

/**
* @private
*/

import './proxy_stream'
import './fetch'
import './heap'
Expand All @@ -16,15 +24,24 @@ import { MemoryCacheStore } from '../memory_cache_store';

const errNoSuchBridgeFn = "Attempted to call a unregistered bridge function."

/**
* @private
*/
interface IterableIterator<T> extends Iterator<T> {
[Symbol.iterator](): IterableIterator<T>;
}

/**
* @private
*/
export interface BridgeOptions {
cacheStore?: CacheStore
fileStore?: FileStore
}

/**
* @private
*/
export class Bridge {
cacheStore: CacheStore
fileStore?: FileStore
Expand Down
3 changes: 2 additions & 1 deletion src/utils/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export function getWebpackConfig(cwd: string, opts?: AppBuilderOptions): webpack

conf.resolve = Object.assign({
alias: Object.assign({}, conf.resolve.alias, {
"@fly/image": v8EnvPath + "/fly/image"
"@fly/image": v8EnvPath + "/fly/image",
"@fly/proxy": v8EnvPath + "/fly/proxy"
})
}, conf.resolve)

Expand Down
3 changes: 3 additions & 0 deletions v8env/ts/bridge.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/**
* @module fly
*/
declare const bridge: any
117 changes: 117 additions & 0 deletions v8env/ts/fly/proxy.ts
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,
Copy link
Member

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!

Copy link
Member Author

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 to stripPath though, which I think makes a little more sense.


/**
* 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
}
}
20 changes: 20 additions & 0 deletions v8env_test/proxy.spec.js
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")
})
})