Skip to content

Commit 60a3ecd

Browse files
author
Rory Fitzpatrick
committed
It exists
1 parent 8febcd2 commit 60a3ecd

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2012-2014, Walmart and other contributors.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright
9+
notice, this list of conditions and the following disclaimer in the
10+
documentation and/or other materials provided with the distribution.
11+
* The names of any contributors may not be used to endorse or promote
12+
products derived from this software without specific prior written
13+
permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
19+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
* * *

index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = function parseCacheControl(field) {
2+
3+
/*
4+
Cache-Control = 1#cache-directive
5+
cache-directive = token [ "=" ( token / quoted-string ) ]
6+
token = [^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+
7+
quoted-string = "(?:[^"\\]|\\.)*"
8+
*/
9+
10+
// 1: directive = 2: token 3: quoted-string
11+
var regex = /(?:^|(?:\s*\,\s*))([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)(?:\=(?:([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)|(?:\"((?:[^"\\]|\\.)*)\")))?/g;
12+
13+
var header = {};
14+
var err = field.replace(regex, function($0, $1, $2, $3) {
15+
var value = $2 || $3;
16+
header[$1] = value ? value.toLowerCase() : true;
17+
return '';
18+
});
19+
20+
if (header['max-age']) {
21+
try {
22+
var maxAge = parseInt(header['max-age'], 10);
23+
if (isNaN(maxAge)) {
24+
return null;
25+
}
26+
27+
header['max-age'] = maxAge;
28+
}
29+
catch (err) { }
30+
}
31+
32+
return (err ? null : header);
33+
};

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "parse-cache-control",
3+
"version": "1.0.0",
4+
"description": "Parse Cache-Control headers.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "tape test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/roryf/parse-cache-control.git"
12+
},
13+
"devDependencies": {
14+
"tape": "^3.5.0"
15+
},
16+
"licenses": [
17+
{
18+
"type": "BSD",
19+
"url": "http://github.com/hapijs/wreck/raw/master/LICENSE"
20+
}
21+
]
22+
}

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var test = require('tape');
2+
var parseCacheControl = require('./index');
3+
4+
test('parseCacheControl', function (t) {
5+
var header = parseCacheControl('must-revalidate, max-age=3600');
6+
t.ok(header);
7+
t.equal(header['must-revalidate'], true);
8+
t.equal(header['max-age'], 3600);
9+
10+
header = parseCacheControl('must-revalidate, max-age="3600"');
11+
t.ok(header);
12+
t.equal(header['must-revalidate'], true);
13+
t.equal(header['max-age'], 3600);
14+
15+
header = parseCacheControl('must-revalidate, b =3600');
16+
t.notOk(header);
17+
18+
header = parseCacheControl('must-revalidate, max-age=a3600');
19+
t.notOk(header);
20+
21+
t.end();
22+
});

0 commit comments

Comments
 (0)