Skip to content

Commit

Permalink
Release 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanebachelier committed Feb 19, 2016
1 parent 9809510 commit 628e89b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="0.5.3"></a>
## [0.5.3](https://github.com/stephanebachelier/superapi-cache/compare/0.5.3...v0.5.3) (2016-02-19)


### Features

* **cache:** add support for expiration ([9809510](https://github.com/stephanebachelier/superapi-cache/commit/9809510))



<a name="0.5.2"></a>
## [0.5.2](https://github.com/stephanebachelier/superapi-cache/compare/0.5.2...v0.5.2) (2016-02-19)

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superapi-cache",
"version": "0.5.3",
"version": "0.6.0",
"homepage": "https://github.com/stephanebachelier/superapi-cache",
"authors": [
"Stéphane Bachelier <stephane.bachelier@gmail.com>"
Expand Down
7 changes: 4 additions & 3 deletions dist/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ function parseHeader(str) {
}

function hydrate(value) {
var xhr = value.body || {};
var headers = parseHeader(value.headers || {});
var data = value.data;
var xhr = data.body || {};
var headers = parseHeader(data.headers || {});

xhr.getAllResponseHeaders = function () {
return value.headers;
return data.headers;
};

xhr.getResponseHeader = function (header) {
Expand Down
6 changes: 5 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function cache() {
var store = config.store;
var key = config.key || cache.key;

config.maxAge = config.maxAge || 0;
config.readCache = config.readCache || _readCache2.default;
config.serialize = config.serialize || _serialize2.default;

Expand Down Expand Up @@ -55,7 +56,10 @@ function cache() {

var f = function f() {
return next().then(function (res) {
return store.setItem(uuid, config.serialize(req, res));
return store.setItem(uuid, {
expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,
data: config.serialize(req, res)
});
});
};

Expand Down
19 changes: 16 additions & 3 deletions dist/read-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", {
exports.default = function (req, log) {
return function (value) {
return new Promise(function (resolve, reject) {
if (!value) {
if (!value || !value.data) {
log('cache-miss', req.url);
var error = new Error();

Expand All @@ -16,6 +16,21 @@ exports.default = function (req, log) {
return reject(error);
}

var expires = value.expires;
var data = value.data;

if (expires !== 0 && expires < Date.now()) {
log('cache-stale', req.url);
var error = new Error();

error.reason = 'cache-stale';
error.message = 'Value is stale';
return reject(error);
}

// hydrate pseudo xhr from cached value
req.xhr = (0, _hydrate2.default)(data);

// override request end callback
req.callback = function (err, res) {
log('cache-hit', req.url);
Expand All @@ -27,8 +42,6 @@ exports.default = function (req, log) {
resolve(res);
};

// hydrate pseudo xhr from cached value
req.xhr = (0, _hydrate2.default)(value);
req.emit('end');
});
};
Expand Down
32 changes: 25 additions & 7 deletions dist/superapi-cache.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/superapi-cache.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/superapi-cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/superapi-cache.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superapi-cache",
"version": "0.5.3",
"version": "0.6.0",
"description": "Caching module for superapi",
"homepage": "https://github.com/stephanebachelier/superapi-cache",
"author": {
Expand Down

0 comments on commit 628e89b

Please sign in to comment.