Socket
Socket
Sign inDemoInstall

serve-handler

Package Overview
Dependencies
18
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

83

lib/index.js

@@ -83,3 +83,3 @@ // Native

const shouldRedirect = (rewrittenURL, redirects = []) => {
const shouldRedirect = (rewrittenPath, {redirects = [], trailingSlash = null}) => {
if (redirects.length === 0) {

@@ -89,7 +89,33 @@ return null;

const defaultType = 301;
if (typeof trailingSlash === 'boolean') {
const {ext} = path.parse(rewrittenPath);
const isTrailed = rewrittenPath.endsWith('/');
let target = null;
if (!trailingSlash && isTrailed) {
target = rewrittenPath.slice(0, -1);
} else if (trailingSlash && !isTrailed && !ext) {
target = `${rewrittenPath}/`;
}
if (rewrittenPath.indexOf('//') > -1) {
target = rewrittenPath.replace(/\/+/g, '/');
}
if (target) {
return {
target,
statusCode: defaultType
};
}
}
// This is currently the fastest way to
// iterate over an array
for (let index = 0; index < redirects.length; index++) {
const {source, destination, statusCode} = redirects[index];
const target = toTarget(source, destination, rewrittenURL);
const {source, destination, type} = redirects[index];
const target = toTarget(source, destination, rewrittenPath);

@@ -99,3 +125,3 @@ if (target) {

target,
statusCode: statusCode || 301
statusCode: type || defaultType
};

@@ -115,3 +141,3 @@ }

const getHeaders = async (handlers, customHeaders = [], {relative, absolute}) => {
const getHeaders = async (handlers, customHeaders = [], relativePath, stats) => {
const related = {};

@@ -126,3 +152,3 @@

if (sourceMatches(source, relative)) {
if (sourceMatches(source, relativePath)) {
appendHeaders(related, headers);

@@ -133,6 +159,4 @@ }

const stats = await handlers.stat(absolute);
const defaultHeaders = {
'Content-Type': mime.getType(relative),
'Content-Type': mime.getType(relativePath),
'Last-Modified': stats.mtime.toUTCString(),

@@ -150,5 +174,5 @@ 'Content-Length': stats.size

const {pathname} = url.parse(request.url);
const rewrittenURL = applyRewrites(pathname, config.rewrites);
const redirect = shouldRedirect(rewrittenURL, config.redirects);
const decodedPath = decodeURIComponent(url.parse(request.url).pathname);
const relativePath = applyRewrites(decodedPath, config.rewrites);
const redirect = shouldRedirect(decodedPath, config);

@@ -163,13 +187,28 @@ if (redirect) {

const related = decodeURIComponent(path.join(current, rewrittenURL));
const relatedExists = await fs.exists(related);
const absolutePath = path.join(current, relativePath);
let stats = null;
if (relatedExists) {
const headers = await getHeaders(handlers, config.headers, {
relative: rewrittenURL,
absolute: related
});
try {
stats = await handlers.stat(absolutePath);
} catch (err) {
if (err.code !== 'ENOENT') {
response.statusCode = 500;
response.end(err.message);
return;
}
}
if (!stats) {
response.statusCode = 404;
response.end('Not Found');
return;
}
const headers = await getHeaders(handlers, config.headers, relativePath, stats);
if (stats.isFile()) {
response.writeHead(200, headers);
handlers.createReadStream(related).pipe(response);
handlers.createReadStream(absolutePath).pipe(response);

@@ -179,4 +218,4 @@ return;

response.statusCode = 404;
response.end('Not Found');
response.statusCode = 200;
response.end('Directory');
};
{
"name": "serve-handler",
"version": "1.1.0",
"version": "1.2.0",
"description": "The routing foundation of `serve` and static deployments on Now",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc