Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Dec 2, 2023
1 parent 9af673c commit 5f3df1e
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions static-pages-bundler/routerForStaticPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -92,4 +88,4 @@ function onError(error, response) {
console.error(error)
response.statusCode = 404
response.end()
}
}

0 comments on commit 5f3df1e

Please sign in to comment.