Created
May 25, 2020 02:20
-
-
Save ArnaudBuchholz/0bbb6ee5cc1d96157e00adbf11cbb620 to your computer and use it in GitHub Desktop.
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 { 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: [{ | |
match: /^\/(.*)/, | |
file: './cache/$1', | |
'ignore-if-not-found': true | |
}, { | |
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