Last active
March 19, 2025 08:05
-
-
Save paulRbr/00dacad11c1cc510173ed8b193309b11 to your computer and use it in GitHub Desktop.
Bump.sh proxy mode
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
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 | |
// | |
// Create self-signed certificate | |
// $ mkdir -p cert | |
// $ cd cert | |
// $ openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365 | |
// $ openssl rsa -in keytmp.pem -out key.pem | |
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 the customers\' origins.', | |
})); | |
} | |
}); | |
const port = process.env.PORT || '5050' | |
console.log(`listening on port ${port}`) | |
// Start the server | |
server.listen(port); | |
// 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