Last active
April 24, 2025 18:25
-
-
Save paulRbr/3af8e3af0a2450772fb1698bdd7e8910 to your computer and use it in GitHub Desktop.
Bump.sh proxy embed mode
This file contains hidden or 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'), | |
connect = require('connect'), | |
harmon = require('harmon'); | |
// 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); | |
proxyReq.setHeader('X-Bump-Sh-Embed', true); | |
}); | |
// 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 proxyMiddleware = function(req, res) { | |
if (req.url.startsWith("/docs/api")) { | |
const targetUrl = 'http://localhost:3000/paul-gh/hub/development-hub' + 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 travelTopBody = fs.readFileSync('travel.header.html', 'utf8') | |
const travelHead = fs.readFileSync('travel.head.html', 'utf8') | |
const overides = [ | |
{ | |
query: "div#embed-top-body", | |
func: function (node) { | |
// If you want to replace the placeholder | |
// node.createWriteStream({ outer: true }).end(travelTopBody); | |
// If you want to inject inside the placeholder | |
node.createWriteStream().end(travelTopBody); | |
} | |
} | |
,{ | |
query: "meta[name='custom-head']", | |
func: function (node) { | |
node.createWriteStream({ outer: true }).end(travelHead); | |
} | |
} | |
] | |
const app = connect(); | |
app.use(harmon([], overides)) | |
app.use(proxyMiddleware) | |
const server = https.createServer(options, app); | |
const port = process.env.PORT || '5050' | |
console.log(`listening on port ${port}`) | |
// Start the server | |
server.listen(parseInt(port)); | |
// visit https://travel.localhost:5050/docs/api/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment