Last active
June 6, 2021 12:24
-
-
Save mecab/9890416390e8c5a2c42547823d252e20 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
// Needs `npm install yargs connect request superstatic firebase-tools` as prerequisites. | |
const stream = require('stream'); | |
const yargs = require('yargs'); | |
const connect = require('connect'); | |
const request = require('request'); | |
const superstatic = require('superstatic'); | |
const firebaseConfig = require('firebase-tools/lib/config').load({ cwd: __dirname }); | |
function createProxyProvider(base) { | |
return (_req, path) => { | |
const proxyPath = base + path; | |
const passThrough = new stream.PassThrough(); | |
return new Promise((resolve, reject) => { | |
request(proxyPath) | |
.on('error', (err) => { | |
reject(err); | |
}) | |
.on('response', (res) => { | |
if (res.statusCode != 200) { | |
resolve(null); | |
console.error(`statusCode ${res.statusCode} for ${proxyPath}`); | |
return; | |
} | |
const size = res.headers['content-length']; | |
const etag = res.headers['etag']; | |
const date = res.headers['date']; | |
resolve({ stream: passThrough, size, etag, modified: date }); | |
res.pipe(passThrough); | |
}); | |
}); | |
} | |
} | |
const argv = yargs | |
.usage('Usage: $0 [options]') | |
.default({ port: 8081, base: 'http://localhost:8080' }) | |
.alias('p', 'port') | |
.alias('b', 'base') | |
.help('h') | |
.alias('h', 'help') | |
.argv; | |
const port = argv.port; | |
const proxyBase = argv.base; | |
const app = connect(); | |
app.use(superstatic({ | |
config: firebaseConfig.data.hosting, | |
provider: createProxyProvider(proxyBase) | |
})); | |
app.listen(port, () => { | |
console.log(`Superstatic proxy is running on port ${port}. Proxy base is ${proxyBase}.`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment