connect-cachify
Advanced tools
Comparing version 0.0.12 to 0.0.13alpha
@@ -14,3 +14,4 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
var opts, | ||
var existsSync = fs.existsSync || path.existsSync, | ||
opts, | ||
_assets, | ||
@@ -55,2 +56,3 @@ _cache = {}; | ||
if (typeof resp.local === 'function') { | ||
resp.local('cachify_prefetch', cachify_prefetch); | ||
resp.local('cachify_js', cachify_js); | ||
@@ -62,2 +64,3 @@ resp.local('cachify_css', cachify_css); | ||
resp.locals({ | ||
cachify_prefetch: cachify_prefetch, | ||
cachify_js: cachify_js, | ||
@@ -70,3 +73,3 @@ cachify_css: cachify_css, | ||
// 10 first characters of md5 + 1 for slash | ||
true_path = req.url.slice(prefix.length + 11); | ||
true_path = req.url.slice(prefix.length + 11); | ||
exists = false; | ||
@@ -88,3 +91,3 @@ | ||
// Would we ever want to use cachify for non-file resources? | ||
if (path.existsSync(path.join(opts.root, true_path))) { | ||
if (existsSync(path.join(opts.root, true_path))) { | ||
exists = true; | ||
@@ -128,8 +131,9 @@ _cache[true_path] = {exists: true}; | ||
*/ | ||
var hashify = function (filename, hash) { | ||
if (typeof filename !== 'string') | ||
throw "cachify ERROR, expected string for filename, got " + filename; | ||
if (filename.indexOf('://') !== -1) { | ||
return filename; | ||
var hashify = function (resource, hash) { | ||
if (typeof resource !== 'string') | ||
throw "cachify ERROR, expected string for resource, got " + resource; | ||
if (resource.indexOf('://') !== -1) { | ||
return resource; | ||
} | ||
if (! hash && opts.global_hash) { | ||
@@ -141,4 +145,10 @@ hash = opts.global_hash; | ||
opts.debug === false) { | ||
return filename; | ||
return resource; | ||
} | ||
// fragment identifiers on URLs are never sent to the server and they | ||
// should not exist on the filename. When looking up the resource on | ||
// disk, do so without the fragment identifier. | ||
var filename = resource.replace(/#.*$/, ''); | ||
if (hash) { | ||
@@ -154,2 +164,3 @@ // No-op, specifing a hash skips all this craziness | ||
var data = fs.readFileSync(path.join(opts.root, filename)); | ||
md5.update(data); | ||
@@ -161,12 +172,13 @@ // Expensive, maintain in-memory cache | ||
// Not intersting to cache, programmer error? | ||
exports.uncached_resources.push(resource); | ||
console.error('Cachify bailing on hash... no such file ' + filename ); | ||
console.error(e); | ||
return filename; | ||
return resource; | ||
} | ||
} | ||
if (opts.prefix.indexOf('://') === -1 && | ||
filename[0] === '/') { | ||
return format('/%s%s%s', opts.prefix, hash, filename); | ||
resource[0] === '/') { | ||
return format('/%s%s%s', opts.prefix, hash, resource); | ||
} else { | ||
return format('%s%s%s%s', opts.prefix, hash, filename[0] === '/' ? '' : '/', filename); | ||
return format('%s%s%s%s', opts.prefix, hash, resource[0] === '/' ? '' : '/', resource); | ||
} | ||
@@ -199,2 +211,9 @@ }; | ||
var cachify_prefetch = exports.cachify_prefetch = function (filename, options) { | ||
if (! options) options = {}; | ||
return prod_or_dev_tags(filename, | ||
'<link rel="prefetch" href="%s">', | ||
options.hash); | ||
}; | ||
var cachify = exports.cachify = function (filename, options) { | ||
@@ -220,1 +239,3 @@ var tag_format; | ||
}; | ||
exports.uncached_resources = []; |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["cache", "HTTP", "expires", "asset", "max-age", "caching"], | ||
"version": "0.0.12", | ||
"version": "0.0.13alpha", | ||
"homepage": "http://github.com/mozilla/connect-cachify", | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -72,2 +72,14 @@ [![build status](https://secure.travis-ci.org/mozilla/connect-cachify.png)](http://travis-ci.org/mozilla/connect-cachify) | ||
## In a Jade template | ||
... | ||
title= Dashboard: Hamsters of North America | ||
meta(charset='utf-8') | ||
| !{cachify_css('/css/dashboard.min.css')} | ||
... | ||
body | ||
... | ||
| !{cachify_js('/js/main.min.js')} | ||
block scripts | ||
In production mode, a call to ``cachify_js`` will produce a single script tag | ||
@@ -152,2 +164,2 @@ like: | ||
## Wordpress Cachify ## | ||
Does this all sound like gobbledygook? Maybe your looking for [Wordpress cachify plugin](http://wordpress.org/extend/plugins/cachify/) instead of ``connect-cachify``. | ||
Does this all sound like gobbledygook? Maybe you're looking for [Wordpress cachify plugin](http://wordpress.org/extend/plugins/cachify/) instead of ``connect-cachify``. |
@@ -18,4 +18,6 @@ var cachify = require('../lib/connect-cachify'), | ||
"/js/main.min.js": [ | ||
"/js/lib/jquery.js", "/js/lib/jquery-foomatic.js", | ||
"/js/main.js" | ||
"/js/lib/jquery.js", | ||
"/js/lib/jquery-foomatic.js", | ||
"/js/main.js", | ||
"/js/font-loader.js#with_fragment_id" | ||
]}; }; | ||
@@ -44,2 +46,5 @@ | ||
function (err) { | ||
fs.writeFile('/tmp/js/font-loader.js', "", "utf8", this); | ||
}, | ||
function (err) { | ||
cb(); | ||
@@ -53,3 +58,3 @@ } | ||
function (err) { | ||
fs.unlink('/tmp/js/lib/jquery-foomatic.js', this); | ||
fs.unlink('/tmp/js/main.min.js', this); | ||
}, | ||
@@ -60,5 +65,8 @@ function (err) { | ||
function (err) { | ||
fs.unlink('/tmp/js/main.min.js', this); | ||
fs.unlink('/tmp/js/lib/jquery-foomatic.js', this); | ||
}, | ||
function (err) { | ||
fs.unlink('/tmp/js/font-loader.js', this); | ||
}, | ||
function (err) { | ||
fs.rmdir('/tmp/js/lib', this); | ||
@@ -83,2 +91,3 @@ }, | ||
mddlwr; | ||
mddlwr = cachify.setup(assets, { | ||
@@ -88,2 +97,3 @@ root: '/tmp', | ||
debug: true}); | ||
links = cachify.cachify_js("/js/main.min.js").split('\n'); | ||
@@ -94,2 +104,4 @@ test.equal(links[0], '<script src="/d41d8cd98f/js/lib/jquery.js"></script>', | ||
test.equal(files[0], '<script src="/d41d8cd98f/js/lib/jquery.js" defer></script>'); | ||
hints = cachify.cachify_prefetch("/js/main.min.js").split('\n'); | ||
test.equal(hints[0], '<link rel="prefetch" href="/d41d8cd98f/js/lib/jquery.js">'); | ||
test.done(); | ||
@@ -122,10 +134,20 @@ }, | ||
var links = cachify.cachify_js("/js/main.min.js").split('\n'); | ||
test.ok(links.length, "Multiple script tags"); | ||
test.equal(links.length, assets["/js/main.min.js"].length, | ||
"All script tags created"); | ||
test.equal(links[0], '<script src="/js/lib/jquery.js"></script>', | ||
"No hashes in all urls during development"); | ||
test.equal(links[3], '<script src="/js/font-loader.js#with_fragment_id"></script>', | ||
"Fragment identifier in URL is preserved"); | ||
test.equal(cachify.uncached_resources.indexOf("/js/font-loader.js#with_fragment_id"), -1, | ||
"Fragment identifiers are never sent to server, search on disk for resource by removing fragment id"); | ||
var files = cachify.cachify("/js/main.min.js").split('\n'); | ||
test.ok(files.length, "Multiple script tags"); | ||
test.equal(files.length, assets["/js/main.min.js"].length, | ||
"All urls created"); | ||
test.equal(files[0], '/js/lib/jquery.js', | ||
"No hashes in all urls during development"); | ||
var hints = cachify.cachify_prefetch("/js/main.min.js").split('\n'); | ||
test.ok(hints.length, "Multiple link tags"); | ||
test.equal(hints[0], '<link rel="prefetch" href="/js/lib/jquery.js">'); | ||
mddlwr(req, resp, function () { | ||
test.ok(resp.state.cachify_prefetch); | ||
test.ok(resp.state.cachify_js); | ||
@@ -166,3 +188,6 @@ test.ok(resp.state.cachify_css); | ||
test.equal(file, "/d41d8cd98f/js/main.min.js"); | ||
var hint = cachify.cachify_prefetch("/js/main.min.js"); | ||
test.equal(hint, '<link rel="prefetch" href="/d41d8cd98f/js/main.min.js">'); | ||
mddlwr(req, resp, function () { | ||
test.ok(resp.state.cachify_prefetch); | ||
test.ok(resp.state.cachify_js); | ||
@@ -206,3 +231,6 @@ test.ok(resp.state.cachify_css); | ||
test.equal(file, "http://example.com/v/d41d8cd98f/js/main.min.js"); | ||
var hint = cachify.cachify_prefetch("/js/main.min.js"); | ||
test.equal(hint, '<link rel="prefetch" href="http://example.com/v/d41d8cd98f/js/main.min.js">'); | ||
mddlwr(req, resp, function () { | ||
test.ok(resp.state.cachify_prefetch); | ||
test.ok(resp.state.cachify_js); | ||
@@ -245,2 +273,3 @@ test.ok(resp.state.cachify_css); | ||
mddlwr(req, resp, function () { | ||
test.ok(resp.state.cachify_prefetch); | ||
test.ok(resp.state.cachify_js); | ||
@@ -254,2 +283,2 @@ test.ok(resp.state.cachify_css); | ||
} | ||
}); | ||
}); |
@@ -58,3 +58,3 @@ /* Test Express 2.x to maintain compatability */ | ||
var app = i === 0 ? express.createServer() : express(); | ||
app.use(cachify.setup()); | ||
app.use(cachify.setup([])); | ||
app.use(function (req, res, next) { | ||
@@ -65,2 +65,3 @@ var locals = res.locals(); | ||
test.ok(!! locals.cachify_js); | ||
test.ok(!! locals.cachify_prefetch); | ||
res.send('ok'); | ||
@@ -72,3 +73,3 @@ }); | ||
var resText = ""; | ||
var req = http.request({ port: port, path: '/'}, function (res) { | ||
var req = http.request({ port: port, path: '/baddecafe1/foo.js'}, function (res) { | ||
test.equal(res.statusCode, 200); | ||
@@ -75,0 +76,0 @@ res.on('data', function (chunk) { |
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
49089
628
164
0