ndn-js-contrib
Version:
Reusable 'Classes' for Named Data Networking: NameTree, PIT, FIB, ContentStore, Interfaces, and Transports
1 lines • 1.17 kB
JavaScript
function Repository(a,b,c){var d=this;return this.index=a,this.nameTree=a.nameTree,this.ndn=a.ndn,this.db=levelup(b.path||path,{db:leveldown,valueEncoding:"json"},function(){d.populateNameTree(c)}),this}var leveldown=require("leveldown"),levelup=require("levelup"),path=(process.env.HOME||process.env.USERPROFILE||process.env.HOMEPATH||"")+"/.NDN-Repo",ndn,debug=!0;Repository.installNDN=function(a){return ndn=a,this},Repository.prototype.getElement=function(a,b){return this.db.get(a.uri,function(a,c){Buffer.isBuffer(c)||(c=new Buffer(c)),b(a,c)}),this},Repository.prototype.insert=function(a,b,c){var d=this.db,e=this;return c=c||function(){},"function"==typeof b&&(c=b,b=new ndn.Data,b.wireDecode(a)),d.put(b.name.toUri(),a,function(d){e.index.insert(a,b),c(d)}),this},Repository.prototype.populateNameTree=function(a){var b=this,c=b.db;a=a||function(){},c.createKeyStream().on("data",function(a){b.index.insert(null,new ndn.Data(new ndn.Name(a)))}).on("error",function(b){a(b)}).on("close",function(){b.spun=!0,a()})},Repository.prototype.check=function(a,b,c){return c=c||this,c.spun?c.index.check(a,b):setTimeout(c.check,200,a,b,c),this},module.exports=Repository;