serve-handler
Advanced tools
Comparing version 2.2.1 to 2.3.0
{ | ||
"name": "serve-handler", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "The routing foundation of `serve` and static deployments on Now", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
138
src/index.js
@@ -270,5 +270,6 @@ // Native | ||
const renderDirectory = async (current, relativePath, absolutePath, handlers, config) => { | ||
const renderDirectory = async (current, acceptsJSON, handlers, config, paths) => { | ||
const {directoryListing, trailingSlash, unlisted = []} = config; | ||
const slashSuffix = typeof trailingSlash === 'boolean' ? (trailingSlash ? '/' : '') : '/'; | ||
const {relativePath, absolutePath} = paths; | ||
@@ -300,3 +301,6 @@ const excluded = [ | ||
details.isDirectory = true; | ||
// This is not camelcase, as we might be sending | ||
// the data away as JSON later, which shouldn't contain | ||
// any camelcased keys. | ||
details['is-directory'] = true; | ||
} else { | ||
@@ -326,4 +330,4 @@ details.ext = details.ext.split('.')[1] || 'txt'; | ||
files = files.sort((a, b) => { | ||
const aIsDir = a.isDirectory; | ||
const bIsDir = b.isDirectory; | ||
const aIsDir = a['is-directory']; | ||
const bIsDir = b['is-directory']; | ||
@@ -361,3 +365,3 @@ if (aIsDir && !bIsDir) { | ||
const paths = []; | ||
const subPaths = []; | ||
@@ -377,3 +381,3 @@ for (let index = 0; index < pathParts.length; index++) { | ||
paths.push({ | ||
subPaths.push({ | ||
name: pathParts[index] + (isLast ? slashSuffix : '/'), | ||
@@ -384,7 +388,13 @@ url: index === 0 ? '' : parents.join('/') + slashSuffix | ||
return template({ | ||
const spec = { | ||
files, | ||
directory, | ||
paths | ||
}); | ||
paths: subPaths | ||
}; | ||
if (acceptsJSON) { | ||
return JSON.stringify(spec); | ||
} | ||
return template(spec); | ||
}; | ||
@@ -407,7 +417,8 @@ | ||
response.end(); | ||
return; | ||
} | ||
const relativePath = applyRewrites(decodedPath, config.rewrites); | ||
let relativePath = applyRewrites(decodedPath, config.rewrites); | ||
let absolutePath = path.join(current, relativePath); | ||
let absolutePath = path.join(current, relativePath); | ||
let stats = null; | ||
@@ -426,50 +437,93 @@ | ||
if (!stats || stats.isDirectory()) { | ||
if (cleanUrl) { | ||
try { | ||
const related = await findRelated(current, relativePath, handlers.stat); | ||
if ((!stats || stats.isDirectory()) && cleanUrl) { | ||
try { | ||
const related = await findRelated(current, relativePath, handlers.stat); | ||
if (related) { | ||
({stats, absolutePath} = related); | ||
} | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
response.statusCode = 500; | ||
response.end(err.message); | ||
if (related) { | ||
({stats, absolutePath} = related); | ||
} | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
response.statusCode = 500; | ||
response.end(err.message); | ||
return; | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
if (!stats) { | ||
response.statusCode = 404; | ||
response.end('Not Found'); | ||
const acceptsJSON = request.headers.accept.includes('application/json'); | ||
if (((stats && stats.isDirectory()) || !stats) && acceptsJSON) { | ||
response.setHeader('Content-Type', 'application/json'); | ||
} | ||
if (stats && stats.isDirectory()) { | ||
let directory = null; | ||
try { | ||
directory = await renderDirectory(current, acceptsJSON, handlers, config, { | ||
relativePath, | ||
absolutePath | ||
}); | ||
} catch (err) { | ||
response.statusCode = 500; | ||
response.end(err.message); | ||
return; | ||
} | ||
} | ||
const headers = await getHeaders(config.headers, relativePath, stats); | ||
if (directory) { | ||
response.statusCode = 200; | ||
response.end(directory); | ||
if (stats.isFile()) { | ||
response.writeHead(200, headers); | ||
handlers.createReadStream(absolutePath).pipe(response); | ||
return; | ||
} | ||
return; | ||
// The directory listing is disabled, so we want to | ||
// render a 404 error. | ||
stats = null; | ||
} | ||
let directory = null; | ||
if (!stats) { | ||
response.statusCode = 404; | ||
try { | ||
directory = await renderDirectory(current, relativePath, absolutePath, handlers, config); | ||
} catch (err) { | ||
response.statusCode = 500; | ||
response.end(err.message); | ||
if (acceptsJSON) { | ||
response.end(JSON.stringify({ | ||
error: { | ||
code: 'not_found', | ||
message: 'Not Found' | ||
} | ||
})); | ||
return; | ||
return; | ||
} | ||
const errorPage = '404.html'; | ||
const errorPageFull = path.join(current, errorPage); | ||
try { | ||
stats = await handlers.stat(errorPage); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
response.statusCode = 500; | ||
response.end(err.message); | ||
return; | ||
} | ||
} | ||
if (!stats) { | ||
response.end('Not Found'); | ||
return; | ||
} | ||
absolutePath = errorPageFull; | ||
relativePath = errorPage; | ||
} | ||
response.statusCode = directory ? 200 : 404; | ||
response.end(directory || 'Not Found'); | ||
const headers = await getHeaders(config.headers, relativePath, stats); | ||
response.writeHead(200, headers); | ||
handlers.createReadStream(absolutePath).pipe(response); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21473
404