make Radisk available in browser for @fuchidahiro

This commit is contained in:
Mark Nadal 2018-07-09 18:21:15 -07:00
parent ae3839c531
commit 9474753a8e
11 changed files with 473 additions and 429 deletions

View File

@ -44,6 +44,11 @@ ul, li {
}
.model, .none { display: none }
.hide {
opacity: 0;
visibility: hidden;
transition: all 2s;
}
.page {
width: 100%;
@ -81,8 +86,7 @@ ul, li {
padding: 3%;
}
.gully {
margin-top: 2em;
margin-bottom: 2em;
margin-bottom: 1%;
}
.sit { margin-bottom: 0; }

View File

@ -1,9 +1,9 @@
if(typeof window !== "undefined"){
var Gun = window.Gun;
} else {
var Gun = require('gun/gun');
var Gun = require('../gun');
}
Gun.chain.open || require('gun/lib/open');
Gun.chain.open || require('./open');
Gun.chain.load = function(cb, opt, at){
(opt = opt || {}).off = !0;

View File

@ -1,7 +1,7 @@
if(typeof window !== "undefined"){
var Gun = window.Gun;
} else {
var Gun = require('gun/gun');
var Gun = require('../gun');
}
Gun.chain.open = function(cb, opt, at){

View File

@ -1,8 +1,6 @@
var fs = require('fs');
var Gun = require('../gun');
var Radix = require('./radix');
;(function(){
function Radisk(opt){
function Radisk(opt){
opt = opt || {};
opt.file = String(opt.file || 'radata');
@ -264,10 +262,10 @@ function Radisk(opt){
var q = {}, noop = function(){}, RAD, u;
return r;
}
}
;(function(){
;(function(){
s = String.fromCharCode(31);
Radisk.encode = function(d, o){
var t = s, tmp;
@ -335,8 +333,18 @@ function Radisk(opt){
return false;
}
}
}());
if(typeof window !== "undefined"){
var Gun = window.Gun;
var Radix = window.Radix;
window.Radisk = Radisk;
} else {
var Gun = require('../gun');
var Radix = require('./radix');
try{ module.exports = Radisk }catch(e){}
}
Radisk.Radix = Radix;
}());
Radisk.Radix = Radix;
module.exports = Radisk;

View File

@ -1,8 +1,4 @@
var Gun = require('../gun');
;(function(){
var map = Gun.obj.map, no = {}, u;
var $ = String.fromCharCode(30), _ = String.fromCharCode(29);
function Radix(){
var radix = function(key, val, t){
@ -46,6 +42,7 @@ var Gun = require('../gun');
}
return radix;
};
Radix.map = function map(radix, cb, opt, pre){ pre = pre || [];
var t = radix[_] || radix, keys = Object.keys(t).sort(), i = 0, l = keys.length;
for(;i < l; i++){ var key = keys[i], tree = t[key], tmp;
@ -64,7 +61,18 @@ var Gun = require('../gun');
}
}
};
Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
module.exports = Radix;
if(typeof window !== "undefined"){
var Gun = window.Gun;
window.Radix = Radix;
} else {
var Gun = require('../gun');
try{ module.exports = Radix }catch(e){}
}
var map = Gun.obj.map, no = {}, u;
var $ = String.fromCharCode(30), _ = String.fromCharCode(29);
}());

61
lib/rfs.js Normal file
View File

@ -0,0 +1,61 @@
function Store(opt){
opt = opt || {};
opt.file = String(opt.file || 'radata');
var Gun = require('../gun'), fs = require('fs'), u;
var store = function Store(){};
store.put = function(file, data, cb){
var random = Math.random().toString(36).slice(-3)
fs.writeFile(opt.file+'-'+random+'.tmp', data, function(err, ok){
if(err){ return cb(err) }
move(opt.file+'-'+random+'.tmp', opt.file+'/'+file, cb);
});
};
store.get = function(file, cb){
fs.readFile(opt.file+'/'+file, function(err, data){
if(err){
if('ENOENT' === (err.code||'').toUpperCase()){
return cb(null);
}
Gun.log("ERROR:", err)
}
if(data){ data = data.toString() }
cb(err, data);
});
};
store.list = function(cb, match){
fs.readdir(opt.file, function(err, dir){
Gun.obj.map(dir, cb) || cb(); // Stream interface requires a final call to know when to be done.
});
};
if(!fs.existsSync(opt.file)){ fs.mkdirSync(opt.file) }
//store.list(function(){ return true });
function move(oldPath, newPath, cb) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', cb);
writeStream.on('error', cb);
readStream.on('close', function () {
fs.unlink(oldPath, cb);
});
readStream.pipe(writeStream);
} else {
cb(err);
}
} else {
cb();
}
});
};
return store;
}
module.exports = Store;

View File

@ -1,17 +1,21 @@
var Gun = require('../gun');
var Radisk = require('./radisk');
var fs = require('fs');
var Radix = Radisk.Radix;
var u;
if(typeof window === "undefined"){
var Gun = require('../gun');
}
Gun.on('opt', function(ctx){
this.to.next(ctx);
var opt = ctx.opt;
var opt = ctx.opt, u;
if(typeof window !== "undefined"){
opt.window = window;
}
if(ctx.once){ return }
if(false !== opt.localStorage && !process.env.AWS_S3_BUCKET){ return } // TODO: Remove this after migration.
if(false !== opt.localStorage && !(!opt.window && process.env.AWS_S3_BUCKET)){ return } // TODO: Remove this after migration.
if(false === opt.radisk){ return }
console.log("BUG WARNING: Radix Storage Engine (RAD) has a known rare edge case, if data gets split between file chunks, a GET may only return the first chunk!!!");
opt.store = opt.store || Store(opt);
var Radisk = (opt.window && opt.window.Radisk) || require('./radisk');
var Radix = Radisk.Radix;
opt.store = opt.store || (!opt.window && require('./rfs')(opt));
var rad = Radisk(opt);
ctx.on('put', function(at){
@ -53,80 +57,3 @@ Gun.on('opt', function(ctx){
});
});
function Store(opt){
opt = opt || {};
opt.file = String(opt.file || 'radata');
var store = function Store(){};
store.put = function(file, data, cb){
var random = Math.random().toString(36).slice(-3)
fs.writeFile(opt.file+'-'+random+'.tmp', data, function(err, ok){
if(err){ return cb(err) }
move(opt.file+'-'+random+'.tmp', opt.file+'/'+file, cb);
});
};
store.get = function(file, cb){
fs.readFile(opt.file+'/'+file, function(err, data){
if(err){
if('ENOENT' === (err.code||'').toUpperCase()){
return cb(null);
}
Gun.log("ERROR:", err)
}
if(data){ data = data.toString() }
cb(err, data);
});
};
store.list = function(cb, match){
fs.readdir(opt.file, function(err, dir){
Gun.obj.map(dir, cb) || cb(); // Stream interface requires a final call to know when to be done.
});
};
if(!fs.existsSync(opt.file)){ fs.mkdirSync(opt.file) }
//store.list(function(){ return true });
return store;
}
function move(oldPath, newPath, cb) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', cb);
writeStream.on('error', cb);
readStream.on('close', function () {
fs.unlink(oldPath, cb);
});
readStream.pipe(writeStream);
} else {
cb(err);
}
} else {
cb();
}
});
};
module.exports = Store;
;(function(){
return;
process.env.AWS_S3_BUCKET = 'test-s3';
process.env.AWS_ACCESS_KEY_ID = 'asdf';
process.env.AWS_SECRET_ACCESS_KEY = 'fdsa';
process.env.fakes3 = 'http://localhost:4567';
process.env.AWS_S3_THROTTLE = 0;
return;
global.Gun = require('../gun');
//require('./rs3');
require('../test/abc');
}());

View File

@ -1,5 +1,5 @@
if(typeof window === "undefined"){ //Not in the browser, Include from node
var Gun = require('gun/gun');
var Gun = require('../gun');
}
;(function(){

View File

@ -27,7 +27,11 @@
var file = (((e.event || e).target || e).result || e), img = new Image();
img.src = file;
img.onload = function(){
if(!h && img.width > w){ h = img.height * (w / img.width) }
if(img.width < w && img.height < (h||Infinity)){
e.base64 = file;
return cb(e || file);
}
if(!h){ h = img.height * (w / img.width) }
var canvas = document.createElement('canvas'), ctx = canvas.getContext('2d');
canvas.width = w;
canvas.height = h;

View File

@ -3,10 +3,12 @@ var config = {
port: 8080,
servers: 2,
browsers: 2,
each: 2000,
each: 100,
burst: 50,
wait: 1,
dir: __dirname,
chunk: 1024 * 10,
notrad: false,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
@ -50,25 +52,19 @@ describe("Make sure the Radix Storage Engine (RSE) works.", function(){
it("GUN started!", function(){
return server.run(function(test){
var env = test.props;
console.log("????", process.argv);
test.async();
if(require('fs').existsSync('radata')){
console.log("Please delete previous data first!");
explode;
return;
}
/*setInterval(function(){
var mem = process.memoryUsage();
var u = Math.round(mem.heapUsed / 1024 / 1024 * 100) / 100;
console.log(u, 'MB of', Math.round(mem.heapTotal / 1024 / 1024 * 100) / 100);
}, 1000);*/
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var Gun = require('gun');
//require('gun/lib/store');
var gun = Gun({web: server, localStorage: false, until: 1, memory: 50, chunk: 1024 * 100});
var gun = Gun({web: server, localStorage: env.config.notrad, until: 1, memory: 50, chunk: env.config.chunk, file: 'radata'});
server.listen(port, function(){
test.done();
});
@ -86,7 +82,7 @@ describe("Make sure the Radix Storage Engine (RSE) works.", function(){
console.log("I AM ALICE");
localStorage.clear();
var env = test.props;
var gun = Gun({peers: 'http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun', localStorage: false});
var gun = Gun({peers: 'http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun', localStorage: env.config.notrad});
window.gun = gun;
var n = Gun.time.is(), i = 0, c = 0, b = env.config.burst, l = env.config.each;
@ -154,7 +150,7 @@ describe("Make sure the Radix Storage Engine (RSE) works.", function(){
});
var Gun = require('gun');
//require('gun/lib/store');
var gun = Gun({web: server, localStorage: false, until: 1, memory: 50, chunk: 1024 * 100});
var gun = Gun({web: server, localStorage: env.config.notrad, until: 1, memory: 50, chunk: env.config.notrad, file: 'radata'});
server.listen(port, function(){
test.done();
});
@ -167,11 +163,12 @@ describe("Make sure the Radix Storage Engine (RSE) works.", function(){
console.log("I AM BOB");
localStorage.clear();
var env = test.props;
var gun = Gun({peers: 'http://'+ env.config.IP + ':' + (env.config.port + 2) + '/gun', localStorage: false});
var gun = Gun({peers: 'http://'+ env.config.IP + ':' + (env.config.port + 2) + '/gun', localStorage: env.config.notrad});
window.gun = gun;
var n = Gun.time.is(), i = 0, c = 0, b = env.config.burst, l = env.config.each;
var raw = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
window.FOO = [];
function check(i){
if(i > l){
@ -183,7 +180,8 @@ describe("Make sure the Radix Storage Engine (RSE) works.", function(){
if((raw+i) !== data.hello){ return test.fail('wrong ' + i) }
if(d){ return } d = true;
//!(c % b) &&
console.log(c+'/'+l);//, '@'+Math.floor(b/((-n + (n = Gun.time.is()))/1000))+'/sec'));
window.FOO.push(i);
console.log(c+'/'+l, 'yeah?', i, Gun.node.soul(data));//, '@'+Math.floor(b/((-n + (n = Gun.time.is()))/1000))+'/sec'));
window.GOT = c++;
//localStorage.clear();
ref.off();

34
test/radisk.html Normal file
View File

@ -0,0 +1,34 @@
<h1>Radisk</h1>
<script src="../examples/jquery.js"></script>
<script src="../gun.js"></script>
<script src="../lib/radix.js"></script>
<script src="../lib/radisk.js"></script>
<script src="../lib/store.js"></script>
<script>
localStorage.clear();
function Store(opt){
console.log(opt);
opt = opt || {};
opt.file = String(opt.file || 'radata');
var storage = {};
var store = function Store(){};
store.put = function(file, data, cb){
storage[file] = data
cb(undefined, 1)
};
store.get = function(file, cb){
var temp = storage[file] || undefined
cb(temp)
};
store.list = function(cb, match){
var arr = Object.entries(storage)[0];
Gun.obj.map(arr, cb) || cb();
};
return store;
}
var gun = Gun({localStorage: false, store: Store()});
</script>