Last active
October 5, 2021 21:12
-
-
Save samueljseay/9d729d25707f8d975160bf389e637c4b to your computer and use it in GitHub Desktop.
Snowpack custom headers plugin
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
{ | |
"name": "snowpack-custom-headers", | |
"version": "0.0.1", | |
"description": "", | |
"main": "snowpack-custom-headers.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Samuel Seay", | |
"license": "MIT", | |
"dependencies": { | |
"http-proxy": "^1.18.1", | |
"execa": "^5.1.1" | |
} | |
} |
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 http = require("http"), | |
httpProxy = require("http-proxy"); | |
var proxy = httpProxy.createProxyServer({}); | |
const { | |
sch_headers: headers, | |
sch_port: port, | |
sch_secure: secure, | |
sch_hostname: hostname, | |
sch_proxy_port: proxy_port, | |
} = process.env; | |
const headersObj = headers.split(",").reduce((acc, curr) => { | |
return { | |
...acc, | |
[curr.split(":")[0]]: curr.split(":")[1], | |
}; | |
}, {}); | |
proxy.on("proxyRes", function (proxyRes, req, res, options) { | |
Object.entries(headersObj).forEach(([key, val]) => { | |
proxyRes.headers[key] = val; | |
}); | |
}); | |
var server = http.createServer(function (req, res) { | |
proxy.web(req, res, { | |
target: `${secure === "true" ? "https" : "http"}://${hostname}:${port}`, | |
}); | |
}); | |
console.log(`Proxying headers on port ${proxy_port}`); | |
server.listen(proxy_port); |
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 execa = require("execa"); | |
module.exports = function (snowpackConfig, pluginOptions) { | |
return { | |
name: "snowpack-custom-headers", | |
async run({ log }) { | |
const port = pluginOptions.port || "9001"; | |
log(`proxying headers on port ${port}`); | |
const workerPromise = execa.node(`./proxy-server.js`, { | |
env: { | |
sch_proxy_port: port, | |
sch_port: snowpackConfig.devOptions.port, | |
sch_hostname: snowpackConfig.devOptions.hostname, | |
sch_secure: snowpackConfig.devOptions.secure, | |
sch_headers: Object.entries(pluginOptions.headers) | |
.map(([key, val]) => `${key}:${val}`) | |
.join(","), | |
}, | |
extendEnv: true, | |
windowsHide: false, | |
cwd: __dirname, | |
}); | |
try { | |
execa.commandSync( | |
`open "${snowpackConfig.devOptions.secure ? "https://" : "http://"}${ | |
snowpackConfig.devOptions.hostname | |
}:${port}"`, | |
{ shell: true } | |
); | |
} catch (err) { | |
console.log(err); | |
} | |
const { stdout, stderr } = workerPromise; | |
function dataListener(chunk) { | |
let stdOutput = chunk.toString(); | |
log("WORKER_MSG", { msg: stdOutput }); | |
} | |
stdout && stdout.on("data", dataListener); | |
stderr && stderr.on("data", dataListener); | |
return workerPromise.catch((err) => { | |
if (/ENOENT/.test(err.message)) { | |
log("WORKER_MSG", { | |
msg: 'WARN: "snowpack custom headers" run failed.', | |
}); | |
} | |
throw err; | |
}); | |
}, | |
}; | |
}; |
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
export default { | |
devOptions: { | |
open: "none", | |
port: 9000, | |
}, | |
plugins: [ | |
[ | |
"snowpack-custom-headers", | |
{ | |
headers: { | |
"Cross-Origin-Opener-Policy": "same-origin", | |
"Cross-Origin-Embedder-Policy": "require-corp", | |
}, | |
}, | |
], | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment