diff --git a/static-pages-bundler/routerForStaticPages.js b/static-pages-bundler/routerForStaticPages.js index 9039c79..f4d0a2e 100644 --- a/static-pages-bundler/routerForStaticPages.js +++ b/static-pages-bundler/routerForStaticPages.js @@ -24,35 +24,31 @@ function mimeFor(filename) { } -export function routerForStaticPages(rootPath, routes, template, templateArg) { - const altRoot = '/root-meta' // @Convention - +export function routerForStaticPages(rootPath, routes, templatePath, templateConstArg) { + const altRoot = '/root-meta' return async function ({ url, headers }, response) { try { - let htmlTemplate = html => html // A template is optional - if (template) { - const ht = await import(template + '?' + Date.now()) // The date is a cache workaround - htmlTemplate = templateArg - ? ht.default.bind(null, templateArg) - : ht.default - } - - if (url === '/') { /* Redirect to "/index" or fallback to the first route */ - response.statusCode = 302 // Found (Temporary Redirect) - response.setHeader('Location', routes[Math.max(0, routes.indexOf('/index'))]) - response.end() - } + /* Rewrite to "/index" or fallback to the first route */ + if (url === '/') + url = routes[Math.max(0, routes.indexOf('/index'))] - else if (routes.includes(url)) { /* Serve Documents */ + /* Serve Documents */ + if (routes.includes(url)) { + let htmlTemplate = html => html + if (templatePath) { + const t = (await import(templatePath + '?' + Date.now())).default // The date is a cache workaround + htmlTemplate = templateConstArg ? t.bind(null, templateConstArg) : t + } const html = await fs.promises.readFile(rootPath + url + '.html') response.setHeader('Content-Type', mimeFor('html')) response.end(htmlTemplate(html, routes.indexOf(url))) + return } - else if (headers.range) { /* Serve Videos (by range) */ + /* Serve Videos (Partial Content) */ + if (headers.range) { const { size } = await fs.promises.lstat(rootPath + url) let [start, end] = headers.range.replace(/bytes=/, '').split('-').map(n => parseInt(n, 10)) - if (isNaN(end)) end = size - 1 if (isNaN(start)) start = size - end @@ -70,16 +66,16 @@ export function routerForStaticPages(rootPath, routes, template, templateArg) { reader.on('open', function () { this.pipe(response) }) reader.on('error', function (error) { onError(error, response) }) } + return } - else { /* Serve Static Assets */ - response.setHeader('Content-Type', mimeFor(url)) - const reader = fs.existsSync(rootPath + url) - ? fs.createReadStream(rootPath + url) - : fs.createReadStream(rootPath + altRoot + url) - reader.on('open', function () { this.pipe(response) }) - reader.on('error', function (error) { onError(error, response) }) - } + /* Serve Static Assets */ + response.setHeader('Content-Type', mimeFor(url)) + const reader = fs.existsSync(rootPath + url) + ? fs.createReadStream(rootPath + url) + : fs.createReadStream(rootPath + altRoot + url) + reader.on('open', function () { this.pipe(response) }) + reader.on('error', function (error) { onError(error, response) }) } catch (error) { @@ -92,4 +88,4 @@ function onError(error, response) { console.error(error) response.statusCode = 404 response.end() -} +} \ No newline at end of file