Skip to content

Instantly share code, notes, and snippets.

@ArnaudBuchholz
Created May 25, 2020 02:20
Show Gist options
  • Save ArnaudBuchholz/e6d7caba31ec63465ebaa2586ad35405 to your computer and use it in GitHub Desktop.
Save ArnaudBuchholz/e6d7caba31ec63465ebaa2586ad35405 to your computer and use it in GitHub Desktop.
const { createWriteStream, mkdir } = require('fs')
const { dirname, join } = require('path')
const { capture, log, serve } = require('..')
const mkdirAsync = require('util').promisify(mkdir)
const cacheBasePath = join(__dirname, 'cache')
// Should wait for completion
mkdirAsync(cacheBasePath, { recursive: true })
log(serve({
port: 8005,
mappings: [{
method: 'GET',
custom: async (request, response) => {
if (/\.(ico|js|css|svg|jpe?g)$/.exec(request.url)) {
const cachePath = join(cacheBasePath, '.' + request.url)
const cacheFolder = dirname(cachePath)
await mkdirAsync(cacheFolder, { recursive: true })
const file = createWriteStream(cachePath) // auto closed
capture(response, file)
.catch(reason => {
console.error(`Unable to cache ${cachePath}`, reason)
})
}
}
}, {
match: /^\/(.*)/,
url: 'http://facetheforce.today/$1'
}]
}), process.argv.includes('--verbose'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment