Comparing version 0.0.10 to 1.0.0
185
index.js
@@ -1,114 +0,123 @@ | ||
var send = require("send"), | ||
path = require("path"), | ||
crypto = require("crypto"), | ||
fs = require("fs"), | ||
url = require("url"); | ||
'use strict'; | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const fs = require('fs'); | ||
const url = require('url'); | ||
const send = require('send'); | ||
function buildVersionHash(directory, root, versions) { | ||
// Walks the directory tree, finding files, generating a version hash | ||
var files = fs.readdirSync(directory); | ||
root = root || directory; | ||
versions = versions || {}; | ||
// Walks the directory tree, finding files, generating a version hash | ||
const files = fs.readdirSync(directory); | ||
files.forEach(function(file) { | ||
var filePath = path.posix.join(directory, file), | ||
stat = fs.statSync(filePath); | ||
root = root || directory; | ||
versions = versions || {}; | ||
if (stat.isDirectory()) { | ||
buildVersionHash(filePath, root, versions); // Whee! | ||
} else if (stat.isFile()) { | ||
var hash = crypto.createHash("md5").update(fs.readFileSync(filePath, "utf8"), "utf8").digest("hex"); | ||
versions["/" + path.posix.relative(root, filePath)] = hash; | ||
} | ||
}); | ||
files.forEach(file => { | ||
const filePath = path.posix.join(directory, file); | ||
const stat = fs.statSync(filePath); | ||
return versions; | ||
if (stat.isDirectory()) { | ||
buildVersionHash(filePath, root, versions); // Whee! | ||
} else if (stat.isFile()) { | ||
const fileStr = fs.readFileSync(filePath, 'utf8'); | ||
const hash = crypto.createHash('md5') | ||
.update(fileStr, 'utf8') | ||
.digest('hex') | ||
.slice(0, 7); | ||
versions[`/${path.posix.relative(root, filePath)}`] = hash; | ||
} | ||
}); | ||
return versions; | ||
} | ||
function stripVersion(p) { | ||
// index.<hash>.js -> index.js | ||
var fileName = path.basename(p); | ||
// index.<hash>.js -> index.js | ||
const fileName = path.basename(p); | ||
const fileNameParts = fileName.split('.'); | ||
var fileNameParts = fileName.split("."); | ||
if ( | ||
fileNameParts.length >= 3 | ||
&& fileNameParts[fileNameParts.length - 2].length === 32 | ||
&& /^[0-9a-f]{32}$/i.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2] | ||
) { | ||
var stripped = fileNameParts.slice(0, fileNameParts.length - 2); | ||
stripped.push(fileNameParts[fileNameParts.length - 1]); | ||
return path.join(path.dirname(p), stripped.join(".")); | ||
} | ||
if (fileNameParts.length >= 3 && | ||
fileNameParts[fileNameParts.length - 2].length === 7 && | ||
/^[0-9a-f]{7}$/i.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2] | ||
) { | ||
const stripped = fileNameParts.slice(0, fileNameParts.length - 2); | ||
return p; | ||
stripped.push(fileNameParts[fileNameParts.length - 1]); | ||
return path.join(path.dirname(p), stripped.join('.')); | ||
} | ||
return p; | ||
} | ||
module.exports = function(root, options) { | ||
var versions = buildVersionHash(root); | ||
options = options || {}; | ||
module.exports = (root, options) => { | ||
let versions = buildVersionHash(root); | ||
options = options || {}; | ||
function getVersionedPath(p) { | ||
// index.js -> index.<hash>.js | ||
if (!versions[p]) { | ||
return p; | ||
} | ||
function getVersionedPath(p) { | ||
// index.js -> index.<hash>.js | ||
if (!versions[p]) { | ||
return p; | ||
} | ||
var fileName = path.basename(p), | ||
fileNameParts = fileName.split("."); | ||
const fileName = path.basename(p); | ||
const fileNameParts = fileName.split('.'); | ||
fileNameParts.push(versions[p], fileNameParts.pop()); | ||
fileNameParts.push(versions[p], fileNameParts.pop()); | ||
return path.posix.join(path.dirname(p), fileNameParts.join(".")); | ||
} | ||
return path.posix.join(path.dirname(p), fileNameParts.join('.')); | ||
} | ||
function serve(req) { | ||
var filePath = stripVersion(url.parse(req.url).pathname); | ||
var MAX_AGE = 1000 * 60 * 60 * 24 * 365; // 1 year | ||
function serve(req) { | ||
const filePath = stripVersion(url.parse(req.url).pathname); | ||
const MAX_AGE = 1000 * 60 * 60 * 24 * 365; // 1 year | ||
return send(req, filePath, { | ||
maxage: filePath === req.url ? 0 : MAX_AGE, | ||
index: options.index || "index.html", | ||
ignore: options.hidden, | ||
root: root | ||
}); | ||
} | ||
return send(req, filePath, { | ||
maxage: filePath === req.url ? 0 : MAX_AGE, | ||
index: options.index || 'index.html', | ||
ignore: options.hidden, | ||
root | ||
}); | ||
} | ||
function middleware(req, res, next) { | ||
if (req.method !== "GET" && req.method !== "HEAD") { | ||
return next(); | ||
} | ||
function middleware(req, res, next) { | ||
if (req.method !== 'GET' && req.method !== 'HEAD') { | ||
return next(); | ||
} | ||
serve(req, res) | ||
.on("error", function(err) { | ||
if (err.status === 404) { | ||
return next(); | ||
} | ||
return next(err); | ||
}) | ||
.pipe(res); | ||
} | ||
serve(req, res) | ||
.on('error', err => { | ||
if (err.status === 404) { | ||
return next(); | ||
} | ||
return next(err); | ||
}) | ||
.pipe(res); | ||
} | ||
function replacePaths(fileContents) { | ||
var urls = Object.keys(versions); | ||
function replacePaths(fileContents) { | ||
const urls = Object.keys(versions); | ||
urls.forEach(function(url) { | ||
fileContents = fileContents.replace(url, getVersionedPath(url)); | ||
}); | ||
urls.forEach(url => { | ||
fileContents = fileContents.replace(url, getVersionedPath(url)); | ||
}); | ||
return fileContents; | ||
} | ||
return fileContents; | ||
} | ||
function refresh() { | ||
versions = buildVersionHash(root); | ||
} | ||
function refresh() { | ||
versions = buildVersionHash(root); | ||
} | ||
return { | ||
_versions: versions, | ||
getVersionedPath: getVersionedPath, | ||
stripVersion: stripVersion, | ||
serve: serve, | ||
refresh: refresh, | ||
middleware: middleware, | ||
replacePaths: replacePaths | ||
}; | ||
return { | ||
_versions: versions, | ||
getVersionedPath, | ||
stripVersion, | ||
serve, | ||
refresh, | ||
middleware, | ||
replacePaths | ||
}; | ||
}; |
{ | ||
"name": "staticify", | ||
"version": "0.0.10", | ||
"version": "1.0.0", | ||
"description": "A better static asset handler for node.js", | ||
"main": "index.js", | ||
"author": "Rakesh Pai <rakeshpai@errorception.com>", | ||
"license": "MIT", | ||
"repository": { | ||
@@ -11,6 +13,6 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha --reporter spec" | ||
"mocha": "mocha --reporter spec", | ||
"xo": "xo", | ||
"test": "npm run xo && npm run mocha" | ||
}, | ||
"author": "Rakesh Pai <rakeshpai@errorception.com>", | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -24,2 +26,5 @@ "static", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
@@ -30,4 +35,20 @@ "send": "~0.16.1" | ||
"mocha": "~4.0.1", | ||
"should": "~13.1.1" | ||
"should": "~13.1.3", | ||
"xo": "^0.18.2" | ||
}, | ||
"xo": { | ||
"space": 4, | ||
"rules": { | ||
"capitalized-comments": "off" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": "test/**", | ||
"envs": [ | ||
"mocha", | ||
"node" | ||
] | ||
} | ||
] | ||
}, | ||
"engines": { | ||
@@ -34,0 +55,0 @@ "node": ">=4" |
@@ -5,3 +5,3 @@ # staticify | ||
[![Linux Build Status](https://img.shields.io/travis/errorception/staticify/master.svg?label=Linux%20build)](https://travis-ci.org/errorception/staticify) | ||
[![Windows Build status](https://img.shields.io/appveyor/ci/errorception/staticify/master.svg?label=Windows%20build)](https://ci.appveyor.com/project/errorception/staticify/branch/master) | ||
[![Windows Build status](https://img.shields.io/appveyor/ci/rakeshpai/staticify/master.svg?label=Windows%20build)](https://ci.appveyor.com/project/rakeshpai/staticify/branch/master) | ||
[![dependencies Status](https://img.shields.io/david/errorception/staticify.svg)](https://david-dm.org/errorception/staticify) | ||
@@ -8,0 +8,0 @@ [![devDependencies Status](https://img.shields.io/david/dev/errorception/staticify.svg)](https://david-dm.org/errorception/staticify?type=dev) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
8775
3
3
97
2