Skip to content

Commit

Permalink
💫 added initial redirect proxy support #8
Browse files Browse the repository at this point in the history
  • Loading branch information
8eecf0d2 committed Oct 19, 2018
1 parent 4f743f7 commit 4f17bfb
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import * as serveStatic from "serve-static";
import * as queryString from "querystring";
import * as jwt from "jsonwebtoken";
import * as http from "http";
import { URL } from "url";
import * as UrlPattern from "url-pattern";
//@ts-ignore
import * as expressHttpProxy from "express-http-proxy";

import { Logger } from "./helper";
import { Netlify } from "./netlify";
Expand Down Expand Up @@ -70,11 +74,17 @@ export class Server {

private routeRedirects (redirects: Netlify.Redirect[]): void {
for(const redirect of redirects) {
// XXX: Need to check if this can be made stricter to just match "http" and "https"
if(redirect.to.match(/^(?:[a-z]+:)?\/\//i)) {
this.handleProxy(redirect);
continue;
}
if([301, 302, 303].includes(redirect.status)) {
this.handleRedirect(redirect);
} else {
this.handleRewrite(redirect)
continue;
}

this.handleRewrite(redirect);
}
}

Expand All @@ -92,6 +102,23 @@ export class Server {
});
}

private handleProxy (redirect: Netlify.Redirect) {
const redirectUrl = new URL(redirect.to);
const redirectPattern = new UrlPattern(redirectUrl.pathname);

this.express.all(redirect.from, this.handleRedirectHeaders(redirect), (request, response, next) => {
const params = {
splat: request.params["0"],
...request.params,
}
expressHttpProxy(redirectUrl.origin, {
proxyReqPathResolver: (proxyRequest: express.Request) => {
return redirectPattern.stringify(params);
}
})(request, response, next);
});
}

private handleRedirectHeaders (redirect: Netlify.Redirect): express.Handler {
return (request, response, next) => {
if(redirect.headers) {
Expand Down

0 comments on commit 4f17bfb

Please sign in to comment.