Skip to content

Commit 5064c9d

Browse files
committed
Version 0.1.0 release
1 parent aea31c6 commit 5064c9d

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,27 @@ strong caching headers to the response.
5656
**"Cache Strategy" Object** - a "cache strategy" object should implement one or
5757
more of the following methods:
5858

59-
- `lastModified(filename, cb)` - a function that accepts a filename and returns
60-
its last modified date to the callback. If a last modified date could not
61-
be determined, null is passed to the callback; otherwise, static-asset
59+
- `lastModified(filename)` - a function that accepts a filename and returns
60+
its last modified date. If a last modified date could not
61+
be determined, the function should return `null`; otherwise, static-asset
6262
*may* use this Date to set the `Last-Modified` HTTP response header when
6363
the resource is requested.
64-
65-
- `label_or_filename` - a label or filename in `path`
66-
- `cb` - a callback of the form `cb(err, lastModifiedDate)`
6764
- `etag(filename, cb)` - Same as lastModified (above), except that it must
68-
return an ETag (or hash value) to the callback. If the
69-
returned ETag is not null, static-asset *may* use this value to set the
65+
return an ETag (or hash value). If the
66+
returned ETag is not `null`, static-asset *may* use this value to set the
7067
`ETag` HTTP header when the named resource is requested.
71-
- `expires(filename, cb)` - Same as lastModified (above), except
68+
- `expires(filename)` - Same as lastModified (above), except
7269
that it must return a Date Object indicating when the resource shall
7370
expire. The Date may be no more than one year in the future. If
7471
`expires` is implemented, static-asset *may* use the date to set an
7572
`Expires` and/or `Cache-Control: max-age` HTTP headers; otherwise,
7673
static-asset will use a Date approximately one year into the future.
7774

78-
**req.assetFingerprint(label_or_filename)** - Using static-asset's "cache
79-
strategy", return a URL fingerprint for the labelled resource, or if no such
80-
label is registered, attempt to locate the specified filename within the `path`
81-
to determine its fingerprint.
75+
**req.assetFingerprint(label_or_filename)** - Return a URL fingerprint for the
76+
labelled resource, or if no such label is registered, use the "cache
77+
strategy" to determine the file's ETag or last modified date. If an ETag is
78+
provided by the cache strategy, it will be used to generate the fingerprint;
79+
otherwise, the last modified date will be used.
8280

8381
**req.assetFingerprint(label, urlFingerprint, cacheInfo)** - Registers a URL
8482
fingerprint for the specified label.

lib/static-asset.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ function staticAsset(path, strategy) {
99
//If req.assetFingerprint is already there, do nothing.
1010
if(req.assetFingerprint)
1111
return next();
12-
//Create req.assetFingerprint
12+
//Helper function that adds fingerprints and returns them
1313
function addFingerprint(fingerprint, headers) {
1414
fingerprints[fingerprint] = headers;
1515
return fingerprint;
1616
}
17+
//Create req.assetFingerprint
1718
req.assetFingerprint = function(label, fingerprint, cacheInfo) {
18-
if(arguments.length == 1)
19+
if(arguments.length > 1)
1920
{
2021
//Add a label
2122
var labelInfo = labels[label] = {"fingerprint": fingerprint};
@@ -27,18 +28,32 @@ function staticAsset(path, strategy) {
2728
//Get a fingerprint
2829
var info = labels[label];
2930
if(info)
30-
return addFingerprint(info.fingerprint, {"ETag": info.etag,
31-
"Last-Modified": info.lastModified});
31+
{
32+
var headers = {};
33+
if(info.etag)
34+
headers["ETag"] = info.etag;
35+
if(info.lastModified)
36+
headers["Last-Modified"] = info.lastModified.getUTCString();
37+
if(info.expires !== null)
38+
{
39+
if(!info.expires)
40+
{
41+
var d = info.expires = new Date();
42+
d.setFullYear(d.getFullYear() + 1);
43+
}
44+
headers["Expires"] = info.expires.getUTCString();
45+
}
46+
return addFingerprint(info.fingerprint, headers);
47+
}
3248
else
3349
{
3450
//Use the "cache strategy" to get a fingerprint
3551
//Prefer the use of etag over lastModified when generating
3652
//fingerprints
3753
var expires = cacheStrategy.expires ||
3854
staticAsset.strategies["default"].expires;
39-
expires = expires(label);
4055
var headers = {
41-
"Expires": expires //TODO: add Cache-Control: max-age=
56+
"Expires": expires(label).getUTCString()
4257
};
4358
if(cacheStrategy.etag)
4459
{
@@ -49,9 +64,11 @@ function staticAsset(path, strategy) {
4964
else if(cacheStrategy.lastModified)
5065
{
5166
var mdate = cacheStrategy.lastModified(label);
52-
headers["Last-Modified"] = mdate;
53-
return addFingerprint(label + "?v=" + mdate.getTime(),
54-
headers);
67+
mdate.setMilliseconds(0);
68+
headers["Last-Modified"] = mdate.toUTCString();
69+
//Encode the Date as a radix 36 UTC timestamp
70+
mdate = new Number(mdate.getTime() / 1000).toString(36);
71+
return addFingerprint(label + "?v=" + mdate, headers);
5572
}
5673
else
5774
return label; //Do not generate a fingerprint

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
"author": "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com)",
33
"name": "static-asset",
44
"description": "Static asset manager for Node.JS and Express",
5-
"version": "0.0.0",
5+
"keywords": [
6+
"asset",
7+
"static",
8+
"cache",
9+
"caching",
10+
"fingerprint",
11+
"express",
12+
"middleware"
13+
]
14+
"version": "0.1.0",
615
"repository": {
716
"type": "git",
817
"url": "git://github.com/bminer/node-static-asset.git"
918
},
1019
"main": "index.js",
1120
"engines": {
12-
"node": ">=0.6.0"
21+
"node": ">=0.6"
1322
},
1423
"dependencies": {
1524
"crc": ">=0.2"

0 commit comments

Comments
 (0)