commit adb363e8bc162d7e705054ea7108fa96af51858f Author: Luke Childs Date: Thu Dec 22 18:53:40 2016 +0700 Extract expired from onionoo-node-client diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..635b656 --- /dev/null +++ b/.gitignore @@ -0,0 +1,72 @@ +## Node + +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +## OS X + +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d8b251d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: node +script: npm run lint && npm test +after_success: npm run coverage +notifications: + email: + on_success: never diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e2db8f2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Luke Childs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..9913880 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "expired", + "version": "0.0.1", + "description": "Calculate when HTTP cache headers expire", + "main": "src/index.js", + "scripts": { + "test": "nyc ava", + "lint": "snazzy", + "coverage": "nyc report --reporter=text-lcov | coveralls" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/lukechilds/expired.git" + }, + "keywords": [ + "HTTP", + "cache", + "headers", + "expire", + "expires", + "expirey" + ], + "author": "Luke Childs (http://lukechilds.co.uk)", + "license": "MIT", + "bugs": { + "url": "https://github.com/lukechilds/expired/issues" + }, + "homepage": "https://github.com/lukechilds/expired", + "dependencies": { + "date-fns": "^1.21.1" + }, + "devDependencies": { + "ava": "^0.17.0", + "coveralls": "^2.11.15", + "nyc": "^10.0.0", + "snazzy": "^5.0.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..6b51323 --- /dev/null +++ b/src/index.js @@ -0,0 +1,19 @@ +const addSeconds = require('date-fns/add_seconds') + +function expired (headers) { + const originDate = new Date(headers.date) + + // Get max age ms + let maxAge = headers['cache-control'] && headers['cache-control'].match(/max-age=(\d+)/) + maxAge = parseInt(maxAge ? maxAge[1] : 0) + + // Take current age into account + if (headers.age) { + maxAge -= headers.age + } + + // Calculate expirey date + return addSeconds(new Date(originDate), maxAge) +} + +module.exports = expired diff --git a/test/unit.js b/test/unit.js new file mode 100644 index 0000000..f6a952f --- /dev/null +++ b/test/unit.js @@ -0,0 +1,6 @@ +import test from 'ava' +import expired from '../' + +test('expired is a function', t => { + t.is(typeof expired, 'function') +})