Skip to content

Instantly share code, notes, and snippets.

@paulRbr
Created May 23, 2024 14:55
Show Gist options
  • Save paulRbr/00dacad11c1cc510173ed8b193309b11 to your computer and use it in GitHub Desktop.
Save paulRbr/00dacad11c1cc510173ed8b193309b11 to your computer and use it in GitHub Desktop.
Bump.sh proxy mode
const https = require('https'),
httpProxy = require('http-proxy'),
path = require('path'),
fs = require('fs');
// Create a proxy server with custom application logic
const proxy = httpProxy.createProxyServer({});
// To modify the proxy connection before data is sent, you can listen
// for the 'proxyReq' event. When the event is fired, you will receive
// the following arguments:
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Bump-Sh-Proxy', process.env.BUMP_PROXY_SECRET);
});
// Http server options
const options = {
key: fs.readFileSync(path.join(__dirname,'./cert/key.pem')),
cert: fs.readFileSync(path.join(__dirname,'./cert/cert.pem'))
}
// Create the server
const server = https.createServer(options, function(req, res) {
if (req.url.startsWith("/docs/api")) {
const targetUrl = 'https://staging.bump.sh/my-org/hub/testing-proxy-mode' + req.url.replace(/^\/docs\/api(.*)/, '$1')
console.log(`Proxy request from 'https://${req.headers.host}${req.url}' to '${targetUrl}'`)
proxy.web(req, res, {
ignorePath: true,
changeOrigin: true,
target: targetUrl
});
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'These are Elastic origins.',
}));
}
});
console.log("listening on port 5050")
// Start the server
server.listen(5050);
// visit https://myproxy.localhost:5050/docs/api/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment