static-server
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "static-server", | ||
"description": "A simple http server to serve static resource files from a local directory.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Eduardo Bohrer <nbluisrs@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -13,4 +13,5 @@ | ||
const DEFAULT_STATUS_INVALID_METHOD = 405; | ||
const DEFAULT_STATUS_REQUEST_RANGE_NOT_SATISFIABLE = 416; | ||
const VALID_HTTP_METHOD = 'GET'; | ||
const VALID_HTTP_METHODS = ['GET', 'HEAD']; | ||
@@ -42,3 +43,2 @@ const TIME_MS_PRECISION = 3; | ||
.parse(process.argv); | ||
; | ||
@@ -73,3 +73,3 @@ program.rootPath = program.args[0] || process.cwd(); | ||
if (req.method !== VALID_HTTP_METHOD) { | ||
if (VALID_HTTP_METHODS.indexOf(req.method) === -1) { | ||
return sendError(req, res, null, DEFAULT_STATUS_INVALID_METHOD); | ||
@@ -283,3 +283,3 @@ } else if (!validPath(filename)) { | ||
function sendError(req, res, error, status, message) { | ||
status = status || res.status || DEFAULT_STATUS_ERR | ||
status = status || res.status || DEFAULT_STATUS_ERR; | ||
message = message || http.STATUS_CODES[status]; | ||
@@ -333,3 +333,27 @@ | ||
var nrmFile; | ||
var range, start, end; | ||
var streamOptions = {flags: 'r'}; | ||
var size = stat.size; | ||
// support range headers | ||
if (req.headers.range) { | ||
range = req.headers.range.split('-').map(Number); | ||
start = range[0]; | ||
end = range[1]; | ||
// check if requested range is within file range | ||
if ((start < 0) || (end < 0) || (start > stat.size) || (end > stat.size)) { | ||
return sendError(req, res, null, DEFAULT_STATUS_REQUEST_RANGE_NOT_SATISFIABLE); | ||
} | ||
res.headers['Content-Range'] = req.headers.range; | ||
// update filestream options | ||
streamOptions.start = start; | ||
streamOptions.end = end; | ||
// update size | ||
size = end - start; | ||
} | ||
res.headers['Etag'] = JSON.stringify([stat.ino, stat.size, stat.mtime.getTime()].join('-')); | ||
@@ -339,4 +363,13 @@ res.headers['Date'] = new Date().toUTCString(); | ||
res.headers['Content-Type'] = mime.lookup(file); | ||
res.headers['Content-Length'] = stat.size; | ||
res.headers['Content-Length'] = size; | ||
// return only headers if request method is HEAD | ||
if (req.method === 'HEAD') { | ||
res.status = DEFAULT_STATUS_OK; | ||
res.writeHead(DEFAULT_STATUS_OK, res.headers); | ||
res.end(); | ||
console.log(chalk.gray('-->'), chalk.green(res.status, http.STATUS_CODES[res.status]), req.path + (nrmFile !== relFile ? (' ' + chalk.dim('(' + relFile + ')')) : ''), fsize(size).human(), '(' + res.elapsedTime + ')'); | ||
return; | ||
} | ||
if (validateClientCache(req, res, stat, file)) { | ||
@@ -348,7 +381,5 @@ return; // abort | ||
nrmFile = path.normalize(req.path.substring(1)); | ||
fs.createReadStream(file, { | ||
flags: 'r', | ||
}).on('close', function () { | ||
fs.createReadStream(file, streamOptions).on('close', function () { | ||
res.end(); | ||
console.log(chalk.gray('-->'), chalk.green(res.status, http.STATUS_CODES[res.status]), req.path + (nrmFile !== relFile ? (' ' + chalk.dim('(' + relFile + ')')) : ''), fsize(stat.size).human(), '(' + res.elapsedTime + ')'); | ||
console.log(chalk.gray('-->'), chalk.green(res.status, http.STATUS_CODES[res.status]), req.path + (nrmFile !== relFile ? (' ' + chalk.dim('(' + relFile + ')')) : ''), fsize(size).human(), '(' + res.elapsedTime + ')'); | ||
}).on('error', function (err) { | ||
@@ -365,2 +396,2 @@ sendError(req, res, err); | ||
}; | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13417
321
1