diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/stylus/lib/cache | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/stylus/lib/cache')
| -rw-r--r-- | node_modules/stylus/lib/cache/fs.js | 80 | ||||
| -rw-r--r-- | node_modules/stylus/lib/cache/index.js | 25 | ||||
| -rw-r--r-- | node_modules/stylus/lib/cache/memory.js | 116 | ||||
| -rw-r--r-- | node_modules/stylus/lib/cache/null.js | 50 |
4 files changed, 271 insertions, 0 deletions
diff --git a/node_modules/stylus/lib/cache/fs.js b/node_modules/stylus/lib/cache/fs.js new file mode 100644 index 00000000..910bf9fb --- /dev/null +++ b/node_modules/stylus/lib/cache/fs.js @@ -0,0 +1,80 @@ +/** + * Module dependencies. + */ + +var crypto = require('crypto') + , fs = require('fs') + , join = require('path').join + , version = require('../../package').version + , nodes = require('../nodes'); + +var FSCache = module.exports = function(options) { + options = options || {}; + this._location = options['cache location'] || '.styl-cache'; + if (!fs.existsSync(this._location)) fs.mkdirSync(this._location); +}; + +/** + * Set cache item with given `key` to `value`. + * + * @param {String} key + * @param {Object} value + * @api private + */ + +FSCache.prototype.set = function(key, value) { + fs.writeFileSync(join(this._location, key), JSON.stringify(value)); +}; + +/** + * Get cache item with given `key`. + * + * @param {String} key + * @return {Object} + * @api private + */ + +FSCache.prototype.get = function(key) { + var data = fs.readFileSync(join(this._location, key), 'utf-8'); + return JSON.parse(data, FSCache.fromJSON); +}; + +/** + * Check if cache has given `key`. + * + * @param {String} key + * @return {Boolean} + * @api private + */ + +FSCache.prototype.has = function(key) { + return fs.existsSync(join(this._location, key)); +}; + +/** + * Generate key for the source `str` with `options`. + * + * @param {String} str + * @param {Object} options + * @return {String} + * @api private + */ + +FSCache.prototype.key = function(str, options) { + var hash = crypto.createHash('sha1'); + hash.update(str + version + options.prefix); + return hash.digest('hex'); +}; + +/** + * JSON to Stylus nodes converter. + * + * @api private + */ + +FSCache.fromJSON = function(key, val) { + if (val && val.__type) { + val.__proto__ = nodes[val.__type].prototype; + } + return val; +}; diff --git a/node_modules/stylus/lib/cache/index.js b/node_modules/stylus/lib/cache/index.js new file mode 100644 index 00000000..58a0f6c8 --- /dev/null +++ b/node_modules/stylus/lib/cache/index.js @@ -0,0 +1,25 @@ +/** + * Get cache object by `name`. + * + * @param {String|Function} name + * @param {Object} options + * @return {Object} + * @api private + */ + +var getCache = module.exports = function(name, options){ + if ('function' == typeof name) return new name(options); + + var cache; + switch (name){ + // case 'fs': + // cache = require('./fs') + // break; + case 'memory': + cache = require('./memory'); + break; + default: + cache = require('./null'); + } + return new cache(options); +}; diff --git a/node_modules/stylus/lib/cache/memory.js b/node_modules/stylus/lib/cache/memory.js new file mode 100644 index 00000000..bc80c429 --- /dev/null +++ b/node_modules/stylus/lib/cache/memory.js @@ -0,0 +1,116 @@ +/** + * Module dependencies. + */ + +var crypto = require('crypto') + , nodes = require('../nodes'); + +var MemoryCache = module.exports = function(options) { + options = options || {}; + this.limit = options['cache limit'] || 256; + this._cache = {}; + this.length = 0; + this.head = this.tail = null; +}; + +/** + * Set cache item with given `key` to `value`. + * + * @param {String} key + * @param {Object} value + * @api private + */ + +MemoryCache.prototype.set = function(key, value) { + var clone = value.clone() + , item; + + clone.filename = nodes.filename; + clone.lineno = nodes.lineno; + clone.column = nodes.column; + item = { key: key, value: clone }; + this._cache[key] = item; + + if (this.tail) { + this.tail.next = item; + item.prev = this.tail; + } else { + this.head = item; + } + + this.tail = item; + if (this.length++ == this.limit) this.purge(); +}; + +/** + * Get cache item with given `key`. + * + * @param {String} key + * @return {Object} + * @api private + */ + +MemoryCache.prototype.get = function(key) { + var item = this._cache[key] + , val = item.value.clone(); + + if (item == this.tail) return val; + if (item.next) { + if (item == this.head) this.head = item.next; + item.next.prev = item.prev; + } + if (item.prev) item.prev.next = item.next; + + item.next = null; + item.prev = this.tail; + + if (this.tail) this.tail.next = item; + this.tail = item; + + return val; +}; + +/** + * Check if cache has given `key`. + * + * @param {String} key + * @return {Boolean} + * @api private + */ + +MemoryCache.prototype.has = function(key) { + return !!this._cache[key]; +}; + +/** + * Generate key for the source `str` with `options`. + * + * @param {String} str + * @param {Object} options + * @return {String} + * @api private + */ + +MemoryCache.prototype.key = function(str, options) { + var hash = crypto.createHash('sha1'); + hash.update(str + options.prefix); + return hash.digest('hex'); +}; + +/** + * Remove the oldest item from the cache. + * + * @api private + */ + +MemoryCache.prototype.purge = function() { + var item = this.head; + + if (this.head.next) { + this.head = this.head.next; + this.head.prev = null; + } + + this._cache[item.key] = item.prev = item.next = null; + this.length--; +}; diff --git a/node_modules/stylus/lib/cache/null.js b/node_modules/stylus/lib/cache/null.js new file mode 100644 index 00000000..07f1b011 --- /dev/null +++ b/node_modules/stylus/lib/cache/null.js @@ -0,0 +1,50 @@ +/** + * Module dependencies. + */ + +var NullCache = module.exports = function() {}; + +/** + * Set cache item with given `key` to `value`. + * + * @param {String} key + * @param {Object} value + * @api private + */ + +NullCache.prototype.set = function(key, value) {}; + +/** + * Get cache item with given `key`. + * + * @param {String} key + * @return {Object} + * @api private + */ + +NullCache.prototype.get = function(key) {}; + +/** + * Check if cache has given `key`. + * + * @param {String} key + * @return {Boolean} + * @api private + */ + +NullCache.prototype.has = function(key) { + return false; +}; + +/** + * Generate key for the source `str` with `options`. + * + * @param {String} str + * @param {Object} options + * @return {String} + * @api private + */ + +NullCache.prototype.key = function(str, options) { + return ''; +}; |
