mirror of
https://github.com/amark/gun.git
synced 2025-09-13 13:00:11 +00:00

* Thank you Murage Martin @murageyun for donating!!! * Fix opt.s3.fakes3 parsing issue (#1318) * Fix opt.s3.fakes3 parsing issue * Fix second typo within if block * Support variable number of auth retry attempts through opt.retries (#1325) Maintain default to 9 to ensure backwards compatibility * Thanks Jason Stallings @octalmage !!! * Remove unused imports (#1337) * Update README.md * yay format change * encode objects * WS ws.path fix (#1343) * Update wire.js * Update wire.js * Update wire.js * add one click deploy to readme (#1342) * update src/index (#1254) * update src/index * update * src/index fix * added src/core * is ??? this a MVP of book & rad ???? thanks to @rogowski * book & rad APIs stabilizing * RAD & Book promoted! + buggy example: test/rad/book.html * bump path * cleaned up Book results & sorting & caching * sea blobs! (#1353) * sea blobs! * and null origins * null fix * null check is last * add a way to select stats file from url (#1351) * react-native detection, and load needed shims (#1349) * react-native detection * added lib mobile * changed back to gun. for another solution * have unbuild function wrap to prevent scope leaks & allow RETURN hehehe so I can reject @bmatusiak 's lS change O:) O:) I love you you're a hero! later with @bmatusiak check sea.then for '../gun.js' vs '../' vs ... note: src/index -> core.js TODO: something about WebRTC candidates hitting ack decrement limits? * quick-fix (#1355) * Fix SEA certificate verification, allow multiple pubs (#1358) * Create SECURITY.md (#1364) * ... works (#1357) * Loading fix (#1356) * does this load better * check window.Gun too in rfs * update SECURITY.md file and change the versions to 0.2020.x (#1365) * webrtc accept getUserMedia streams as peer * Check atom exists in graph when deciding to read from disk (#1371) * fix: ERROR: Radisk needs `store.put` interface (#1374) * Update STUN servers (#1381) Commented out sipgate.net STUN server. Added Cloudflare STUN server. * universal notification system --------- Co-authored-by: ritchia1 <andrew.ritchie@estimateone.com> Co-authored-by: Anton <dev@atjn.dk> Co-authored-by: Bradley Matusiak <bmatusiak@gmail.com> Co-authored-by: Jay Byoun <jay8061@pm.me> Co-authored-by: mimiza <dev@mimiza.com> Co-authored-by: Simardeep Singh <1003simar@gmail.com> Co-authored-by: Malcolm Blaney <mblaney@gmail.com> Co-authored-by: Andreas Heissenberger <andreas@heissenberger.at> Co-authored-by: carlin978 <120719190+carlin978@users.noreply.github.com>
89 lines
2.2 KiB
JavaScript
89 lines
2.2 KiB
JavaScript
function Store(opt){
|
|
opt = opt || {};
|
|
opt.log = opt.log || console.log;
|
|
opt.file = String(opt.file || 'radata');
|
|
var fs = require('fs'), u;
|
|
|
|
var store = function Store(){};
|
|
if(Store[opt.file]){
|
|
console.log("Warning: reusing same fs store and options as 1st.");
|
|
return Store[opt.file];
|
|
}
|
|
Store[opt.file] = store;
|
|
var puts = {};
|
|
|
|
// TODO!!! ADD ZLIB INFLATE / DEFLATE COMPRESSION!
|
|
store.put = function(file, data, cb){
|
|
var random = Math.random().toString(36).slice(-3);
|
|
puts[file] = {id: random, data: data};
|
|
var tmp = opt.file+'-'+file+'-'+random+'.tmp';
|
|
fs.writeFile(tmp, data, function(err, ok){
|
|
if(err){
|
|
if(random === (puts[file]||'').id){ delete puts[file] }
|
|
return cb(err);
|
|
}
|
|
move(tmp, opt.file+'/'+file, function(err, ok){
|
|
if(random === (puts[file]||'').id){ delete puts[file] }
|
|
cb(err, ok || !err);
|
|
});
|
|
});
|
|
};
|
|
store.get = function(file, cb){ var tmp; // this took 3s+?
|
|
if(tmp = puts[file]){ cb(u, tmp.data); return }
|
|
fs.readFile(opt.file+'/'+file, function(err, data){
|
|
if(err){
|
|
if('ENOENT' === (err.code||'').toUpperCase()){
|
|
return cb();
|
|
}
|
|
opt.log("ERROR:", err);
|
|
}
|
|
cb(err, data);
|
|
});
|
|
};
|
|
|
|
if(!fs.existsSync(opt.file)){ fs.mkdirSync(opt.file) }
|
|
|
|
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();
|
|
}
|
|
});
|
|
};
|
|
|
|
store.list = function(cb, match, params, cbs){
|
|
var dir = fs.readdirSync(opt.file);
|
|
dir.forEach(function(file){
|
|
cb(file);
|
|
})
|
|
cb();
|
|
};
|
|
|
|
return store;
|
|
}
|
|
|
|
var Gun = (typeof window !== "undefined" && window.Gun) ? window.Gun : require('../gun');
|
|
Gun.on('create', function(root){
|
|
this.to.next(root);
|
|
var opt = root.opt;
|
|
if(opt.rfs === false){ return }
|
|
opt.store = opt.store || ((!Gun.window || opt.rfs === true) && Store(opt));
|
|
});
|
|
|
|
module.exports = Store; |