Skip to content

Instantly share code, notes, and snippets.

@zephraph
Created October 25, 2019 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zephraph/e3f1db3da01edadb91a9abd4d0d0d837 to your computer and use it in GitHub Desktop.
Save zephraph/e3f1db3da01edadb91a9abd4d0d0d837 to your computer and use it in GitHub Desktop.
A ziet now router for github webhooks
import { NowRequest, NowResponse } from "@now/node";
import { http, https } from "follow-redirects";
import { URL } from "url";
export = (request: NowRequest, response: NowResponse) => {
console.log(request.headers);
const event = request.headers["x-github-event"];
if (!event) return response.status(404).end();
const url = new URL(`https://${request.headers.host}/${request.url}`);
const [host, port] = request.headers.host?.split(":") ?? [];
let connector = (process.env.NODE_ENV === "production" ? https : http).request(
{
host,
port: port ? parseInt(port) : undefined,
path: `${url.pathname}/${event}.ts`,
headers: request.headers
},
forwardedResponse => forwardedResponse.pipe(response)
);
request.pipe(connector);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment