Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
cal.com
/
cal.com
/
node_modules
/
level-hooks
/
Filename :
index.js
back
Copy
var ranges = require('string-range') module.exports = function (db) { if(db.hooks) { return } var posthooks = [] var prehooks = [] function getPrefix (p) { return p && ( 'string' === typeof p ? p : 'string' === typeof p.prefix ? p.prefix : 'function' === typeof p.prefix ? p.prefix() : '' ) } function getKeyEncoding (db) { if(db && db._getKeyEncoding) return db._getKeyEncoding(db) } function getValueEncoding (db) { if(db && db._getValueEncoding) return db._getValueEncoding(db) } function remover (array, item) { return function () { var i = array.indexOf(item) if(!~i) return false array.splice(i, 1) return true } } db.hooks = { post: function (prefix, hook) { if(!hook) hook = prefix, prefix = '' var h = {test: ranges.checker(prefix), hook: hook} posthooks.push(h) return remover(posthooks, h) }, pre: function (prefix, hook) { if(!hook) hook = prefix, prefix = '' var h = { test: ranges.checker(prefix), hook: hook, safe: false !== prefix.safe } prehooks.push(h) return remover(prehooks, h) }, posthooks: posthooks, prehooks: prehooks } //POST HOOKS function each (e) { if(e && e.type) { posthooks.forEach(function (h) { if(h.test(e.key)) h.hook(e) }) } } db.on('put', function (key, val) { each({type: 'put', key: key, value: val}) }) db.on('del', function (key, val) { each({type: 'del', key: key, value: val}) }) db.on('batch', function onBatch (ary) { ary.forEach(each) }) //PRE HOOKS var put = db.put var del = db.del var batch = db.batch function callHooks (isBatch, b, opts, cb) { try { b.forEach(function hook(e, i) { prehooks.forEach(function (h) { if(h.test(String(e.key))) { //optimize this? //maybe faster to not create a new object each time? //have one object and expose scope to it? var context = { add: function (ch, db) { if(typeof ch === 'undefined') { return this } if(ch === false) return delete b[i] var prefix = ( getPrefix(ch.prefix) || getPrefix(db) || h.prefix || '' ) //don't leave a circular json object there incase using multilevel. if(prefix) ch.prefix = prefix ch.key = prefix + ch.key if(h.safe && h.test(String(ch.key))) { //this usually means a stack overflow. throw new Error('prehook cannot insert into own range') } var ke = ch.keyEncoding || getKeyEncoding(ch.prefix) var ve = ch.valueEncoding || getValueEncoding(ch.prefix) if(ke) ch.keyEncoding = ke if(ve) ch.valueEncoding = ve b.push(ch) hook(ch, b.length - 1) return this }, put: function (ch, db) { if('object' === typeof ch) ch.type = 'put' return this.add(ch, db) }, del: function (ch, db) { if('object' === typeof ch) ch.type = 'del' return this.add(ch, db) }, veto: function () { return this.add(false) } } h.hook.call(context, e, context.add, b) } }) }) } catch (err) { return (cb || opts)(err) } b = b.filter(function (e) { return e && e.type //filter out empty items }) if(b.length == 1 && !isBatch) { var change = b[0] return change.type == 'put' ? put.call(db, change.key, change.value, opts, cb) : del.call(db, change.key, opts, cb) } return batch.call(db, b, opts, cb) } db.put = function (key, value, opts, cb ) { var batch = [{key: key, value: value, type: 'put'}] return callHooks(false, batch, opts, cb) } db.del = function (key, opts, cb) { var batch = [{key: key, type: 'del'}] return callHooks(false, batch, opts, cb) } db.batch = function (batch, opts, cb) { return callHooks(true, batch, opts, cb) } }