-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,12 @@ | |
|
||
var has = (sT.RAD.has || (sT.RAD.has = {}))[opt.file]; | ||
if(has){ return has } | ||
var r = function rad(word, is, reply){ | ||
var r = function rad(word, is, reply){ r.word = word; | ||
if(!b){ start(word, is, reply); return r } | ||
if(is === undefined || 'function' == typeof is){ // THIS IS A READ: | ||
var page = b.page(word); | ||
if(page.from){ | ||
return is(null, page); | ||
} | ||
read(word, is, page); // get from disk | ||
return | ||
if(page.from){ return is && is(page, null), r } | ||
return read(word, is, page), r; // get from disk | ||
} | ||
//console.log("OFF");return; | ||
// ON WRITE: | ||
|
@@ -25,15 +22,16 @@ | |
write(word, reply); | ||
return r; | ||
}, /** @param b the book */ b; | ||
r.then = function(cb, p){ return p = (new Promise(function(yes, no){ r(r.word, yes) })), cb? p.then(cb) : p } | ||
r.read = r.results = function(cb){ return (new Promise(async function(yes, no){ yes((await r(r.word)).read(cb)) })) } | ||
|
||
async function read(word, reply, page){ | ||
async function read(word, reply, page){ // TODO: this function doesn't do much, inline it??? | ||
if(!reply){ return } | ||
var p = page || b.page(word); | ||
reply = reply.call ? reply : () => { }; | ||
log(`read() ${word.slice(0, 40)}`); | ||
get(p, function(err, disk){ | ||
if(err){ log("ERR! in read() get() cb", err); reply(err); return } | ||
if(err){ log("ERR! in read() get() cb", err); reply(p.no, err); return } | ||
p.from = disk || p.from; | ||
reply(null, p, b); | ||
reply(p, null, b); | ||
}) | ||
} | ||
|
||
|
@@ -68,7 +66,7 @@ | |
RAD.put(file, data, function(err, ok){ | ||
delete put[file]; | ||
cb && cb(err, ok); | ||
}); | ||
}, opt); | ||
}; | ||
function get(file, cb){ | ||
var tmp; | ||
|
@@ -78,9 +76,9 @@ | |
if(tmp = put[file = fname(file)]){ cb(u, tmp.data); return } | ||
if(tmp = get[file]){ tmp.push(cb); return } get[file] = [cb]; | ||
RAD.get(file, function(err, data){ | ||
tmp = get[file]; delete get[file]; | ||
tmp = get[file]||''; delete get[file]; | ||
var i = -1, f; while (f = tmp[++i]){ f(err, data) } // TODO: BUG! CPU SCHEDULE? | ||
}); | ||
}, opt); | ||
}; | ||
|
||
function start(word, is, reply){ | ||
|
@@ -90,7 +88,7 @@ | |
if(b){ r(word, is, reply); return } | ||
b = r.book = Book(); | ||
if((d = Book.slot(d)).length){ b.list = d } // TODO: BUG! Add some other sort of corrupted/error check here? | ||
watch(b).parse = function(t){ return ('string' == typeof t)? Book.decode(Book.slot(t)[0]) : t } // TODO: This was ugly temporary, but is necessary, and is logically correct, but is there a cleaner, nicer, less assumptiony way to do it? | ||
watch(b).parse = function(t){ return ('string' == typeof t)? Book.decode(Book.slot(t)[0]) : t } // TODO: This was ugly temporary, but is necessary, and is logically correct, but is there a cleaner, nicer, less assumptiony way to do it? // TODO: SOLUTION?! I think this needs to be in Book, not RAD. | ||
r(word, is, reply); | ||
}) | ||
} | ||
|
@@ -116,7 +114,8 @@ | |
} | ||
|
||
function ename(t){ return encodeURIComponent(t).replace(/\*/g, '%2A').slice(0, 250) } | ||
function fname(p){ return opt.file + '/' + ename(p.substring()) } | ||
//function fname(p){ return opt.file + '/' + ename(p.substring()) } | ||
function fname(p){ return ename(p.substring()) } | ||
|
||
|
||
function valid(word, is, reply){ | ||
|
@@ -131,6 +130,7 @@ | |
|
||
return r; | ||
}), MAX = 1000/* 300000000 */; | ||
sT.each = sT.each || function(l,f){l.forEach(f)}; | ||
|
||
try { module.exports = RAD } catch (e){ } | ||
|
||
|
@@ -169,13 +169,13 @@ | |
if(!fs){ return } | ||
|
||
var sT = setTimeout, RAD = sT.RAD; | ||
RAD.put = function(file, data, cb){ | ||
fs.writeFile(file, data, cb); | ||
RAD.put = function(file, data, cb, opt){ | ||
fs.writeFile(opt.file+'/'+file, data, cb); | ||
} | ||
RAD.get = function(file, cb){ | ||
fs.readFile(file, function(err, data){ | ||
if(err && 'ENOENT' === (err.code || '').toUpperCase()){ return cb() } | ||
cb(err, data.toString()); | ||
RAD.get = function(file, cb, opt){ | ||
fs.readFile(opt.file+'/'+file, function(err, data){ | ||
if(err && 'ENOENT' === (err.code||'').toUpperCase()){ return cb() } | ||
cb(err, (data||'').toString()||data); | ||
}); | ||
} | ||
}()); | ||
|
@@ -187,28 +187,30 @@ | |
if(!lS){ return } | ||
|
||
var sT = setTimeout, RAD = sT.RAD; | ||
RAD.put = function(file, data, cb){ | ||
RAD.put = function(file, data, cb, opt){ | ||
setTimeout(function(){ | ||
lS[file] = data; | ||
lS[opt.file+'/'+file] = data; | ||
cb(null, 1); | ||
},9); | ||
},1); | ||
} | ||
RAD.get = function(file, cb){ | ||
RAD.get = function(file, cb, opt){ | ||
setTimeout(function(){ | ||
cb(null, lS[file]); | ||
},9); | ||
cb(null, lS[opt.file+'/'+file]); | ||
},1); | ||
} | ||
}()); | ||
|
||
;(function(){ return; | ||
var get; | ||
try { get = fetch } catch (e){ }; | ||
try { get = fetch } catch (e){ console.log("WARNING! need `npm install [email protected]`"); get = fetch = require('node-fetch') }; | ||
if(!get){ return } | ||
|
||
var sT = setTimeout, RAD = sT.RAD; | ||
RAD.put = function(file, data, cb){ cb(401) } | ||
RAD.get = async function(file, cb){ | ||
var t = (await (await fetch('http://localhost:8765/gun/'+file)).text()); | ||
var sT = setTimeout, RAD = sT.RAD, put = RAD.put, get = RAD.get; | ||
RAD.put = function(file, data, cb, opt){ put && put(file, data, cb, opt); | ||
cb(401) | ||
} | ||
RAD.get = async function(file, cb, opt){ get && get(file, cb, opt); | ||
var t = (await (await fetch('http://localhost:8766/gun/1data/'+file)).text()); | ||
if('404' == t){ cb(); return } | ||
cb(null, t); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.