cache-storage
Advanced tools
Comparing version 1.0.0 to 1.0.2
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var Cache, crypto, fs, moment, path; | ||
var Cache; | ||
crypto = require('crypto'); | ||
path = require('path'); | ||
moment = require('moment'); | ||
fs = require('fs'); | ||
Cache = (function() { | ||
@@ -39,49 +31,14 @@ Cache.FILES = 'files'; | ||
Cache.prototype.generateKey = function(key) { | ||
return crypto.createHash('sha1').update(key).digest('hex'); | ||
}; | ||
Cache.prototype.parseDependencies = function(dependencies) { | ||
var file, files, i, item, result, time, typefn, _i, _j, _len, _len1, _ref, _ref1; | ||
typefn = Object.prototype.toString; | ||
result = {}; | ||
if (typefn.call(dependencies) === '[object Object]') { | ||
if (typeof dependencies[Cache.FILES] !== 'undefined') { | ||
files = {}; | ||
_ref = dependencies[Cache.FILES]; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
file = _ref[_i]; | ||
file = path.resolve(file); | ||
files[file] = (new Date(fs.statSync(file).mtime)).getTime(); | ||
} | ||
result[Cache.FILES] = files; | ||
} | ||
if (typeof dependencies[Cache.EXPIRE] !== 'undefined') { | ||
switch (typefn.call(dependencies[Cache.EXPIRE])) { | ||
case '[object String]': | ||
time = moment(dependencies[Cache.EXPIRE], Cache.TIME_FORMAT); | ||
break; | ||
case '[object Object]': | ||
time = moment().add(dependencies[Cache.EXPIRE]); | ||
break; | ||
default: | ||
throw new Error('Expire format is not valid'); | ||
} | ||
result[Cache.EXPIRE] = time.valueOf(); | ||
} | ||
if (typeof dependencies[Cache.ITEMS] !== 'undefined') { | ||
result[Cache.ITEMS] = []; | ||
_ref1 = dependencies[Cache.ITEMS]; | ||
for (i = _j = 0, _len1 = _ref1.length; _j < _len1; i = ++_j) { | ||
item = _ref1[i]; | ||
result[Cache.ITEMS].push(this.generateKey(item)); | ||
} | ||
} | ||
if (typeof dependencies[Cache.PRIORITY] !== 'undefined') { | ||
result[Cache.PRIORITY] = dependencies[Cache.PRIORITY]; | ||
} | ||
if (typeof dependencies[Cache.TAGS] !== 'undefined') { | ||
result[Cache.TAGS] = dependencies[Cache.TAGS]; | ||
} | ||
var char, hash, i, max, _i; | ||
hash = 0; | ||
if (key.length === 0) { | ||
return hash; | ||
} | ||
return result; | ||
max = key.length - 1; | ||
for (i = _i = 0; 0 <= max ? _i <= max : _i >= max; i = 0 <= max ? ++_i : --_i) { | ||
char = key.charCodeAt(i); | ||
hash = ((hash << 5) - hash) + char; | ||
hash |= 0; | ||
} | ||
return hash; | ||
}; | ||
@@ -112,3 +69,3 @@ | ||
} else { | ||
this.storage.write(key, data, this.parseDependencies(dependencies)); | ||
this.storage.write(key, data, this.storage.parseDependencies(dependencies)); | ||
} | ||
@@ -115,0 +72,0 @@ return data; |
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var Cache, Storage, fs, moment; | ||
var Cache, Storage, fs, isWindow, moment, path; | ||
fs = require('fs'); | ||
isWindow = typeof window === 'undefined' ? false : true; | ||
if (!isWindow) { | ||
fs = require('fs'); | ||
path = require('path'); | ||
} | ||
moment = require('moment'); | ||
@@ -80,2 +85,5 @@ | ||
if (typeof meta[Cache.FILES] !== 'undefined') { | ||
if (isWindow) { | ||
throw new Error('Files meta information is not supported in browser'); | ||
} | ||
_ref = meta[Cache.FILES]; | ||
@@ -108,2 +116,51 @@ for (file in _ref) { | ||
Storage.prototype.parseDependencies = function(dependencies) { | ||
var file, files, i, item, result, time, typefn, _i, _j, _len, _len1, _ref, _ref1; | ||
typefn = Object.prototype.toString; | ||
result = {}; | ||
if (typefn.call(dependencies) === '[object Object]') { | ||
if (typeof dependencies[Cache.FILES] !== 'undefined') { | ||
if (isWindow) { | ||
throw new Error('Files meta information is not supported in browser'); | ||
} | ||
files = {}; | ||
_ref = dependencies[Cache.FILES]; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
file = _ref[_i]; | ||
file = path.resolve(file); | ||
files[file] = (new Date(fs.statSync(file).mtime)).getTime(); | ||
} | ||
result[Cache.FILES] = files; | ||
} | ||
if (typeof dependencies[Cache.EXPIRE] !== 'undefined') { | ||
switch (typefn.call(dependencies[Cache.EXPIRE])) { | ||
case '[object String]': | ||
time = moment(dependencies[Cache.EXPIRE], Cache.TIME_FORMAT); | ||
break; | ||
case '[object Object]': | ||
time = moment().add(dependencies[Cache.EXPIRE]); | ||
break; | ||
default: | ||
throw new Error('Expire format is not valid'); | ||
} | ||
result[Cache.EXPIRE] = time.valueOf(); | ||
} | ||
if (typeof dependencies[Cache.ITEMS] !== 'undefined') { | ||
result[Cache.ITEMS] = []; | ||
_ref1 = dependencies[Cache.ITEMS]; | ||
for (i = _j = 0, _len1 = _ref1.length; _j < _len1; i = ++_j) { | ||
item = _ref1[i]; | ||
result[Cache.ITEMS].push(this.generateKey(item)); | ||
} | ||
} | ||
if (typeof dependencies[Cache.PRIORITY] !== 'undefined') { | ||
result[Cache.PRIORITY] = dependencies[Cache.PRIORITY]; | ||
} | ||
if (typeof dependencies[Cache.TAGS] !== 'undefined') { | ||
result[Cache.TAGS] = dependencies[Cache.TAGS]; | ||
} | ||
} | ||
return result; | ||
}; | ||
return Storage; | ||
@@ -110,0 +167,0 @@ |
{ | ||
"name": "cache-storage", | ||
"description": "Cache storage for node js", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "David Kudera", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27120
13
473
2