gun/lib/rs3.js
Mark Nadal b1b408971d
Master into Deploys (#1391)
* 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>
2024-11-23 17:52:28 -08:00

109 lines
3.7 KiB
JavaScript

var Gun = require('../gun');
var Radisk = require('./radisk');
var Radix = Radisk.Radix;
var u, AWS;
Gun.on('create', function(root){
this.to.next(root);
var opt = root.opt;
if(!opt.s3 && !process.env.AWS_S3_BUCKET){ return }
//opt.batch = opt.batch || (1000 * 10);
//opt.until = opt.until || (1000 * 3); // ignoring these now, cause perf > cost
//opt.chunk = opt.chunk || (1024 * 1024 * 10); // 10MB // when cost only cents
try{AWS = require('aws-sdk');
}catch(e){
console.log("Please `npm install aws-sdk` or add it to your package.json !");
AWS_SDK_NOT_INSTALLED;
}
var opts = opt.s3 || (opt.s3 = {});
opts.bucket = opts.bucket || process.env.AWS_S3_BUCKET;
opts.region = opts.region || process.env.AWS_REGION || "us-east-1";
opts.accessKeyId = opts.key = opts.key || opts.accessKeyId || process.env.AWS_ACCESS_KEY_ID;
opts.secretAccessKey = opts.secret = opts.secret || opts.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY;
// opts.fakes3 should be the domain name of the S3-compatible service
if(opts.fakes3 = opts.fakes3 || process.env.fakes3){
opts.endpoint = opts.fakes3;
opts.sslEnabled = false;
opts.bucket = opts.bucket.replace('.','p');
}
opts.config = new AWS.Config(opts);
opts.s3 = opts.s3 || new AWS.S3(opts.config);
opt.store = Object.keys(opts.s3).length === 0 ? opt.store : Store(opt);
});
function Store(opt){
opt = opt || {};
opt.file = String(opt.file || 'radata');
var opts = opt.s3, s3 = opts.s3;
var c = {p: {}, g: {}, l: {}};
var store = function Store(){};
if(Store[opt.file]){
console.log("Warning: reusing same S3 store and options as 1st.");
return Store[opt.file];
}
Store[opt.file] = store;
store.put = function(file, data, cb){
var params = {Bucket: opts.bucket, Key: file, Body: data};
//console.log("RS3 PUT ---->", (data||"").slice(0,20));
c.p[file] = data;
delete c.g[file];//Gun.obj.del(c.g, file);
delete c.l[1];//Gun.obj.del(c.l, 1);
s3.putObject(params, function(err, ok){
delete c.p[file];
cb(err, 's3');
});
};
store.get = function(file, cb){ var tmp;
if(tmp = c.p[file]){ cb(u, tmp); return }
if(tmp = c.g[file]){ tmp.push(cb); return }
var cbs = c.g[file] = [cb];
var params = {Bucket: opts.bucket, Key: file||''};
//console.log("RS3 GET ---->", file);
s3.getObject(params, function got(err, ack){
if(err && 'NoSuchKey' === err.code){ err = u }
//console.log("RS3 GOT <----", err, file, cbs.length, ((ack||{}).Body||'').length);//.toString().slice(0,20));
delete c.g[file];//Gun.obj.del(c.g, file);
var data, data = (ack||'').Body;
//console.log(1, process.memoryUsage().heapUsed);
var i = 0, cba; while(cba = cbs[i++]){ cba && cba(err, data) }//Gun.obj.map(cbs, cbe);
});
};
store.list = function(cb, match, params, cbs){
if(!cbs){
if(c.l[1]){ return c.l[1].push(cb) }
cbs = c.l[1] = [cb];
}
params = params || {Bucket: opts.bucket};
//console.log("RS3 LIST --->");
s3.listObjectsV2(params, function(err, data){
//console.log("RS3 LIST <---", err, data, cbs.length);
if(err){ return Gun.log(err, err.stack) }
var IT = data.IsTruncated, cbe = function(cb){
if(cb.end){ return }
if(Gun.obj.map(data.Contents, function(content){
return cb(content.Key);
})){ cb.end = true; return }
if(IT){ return }
// Stream interface requires a final call to know when to be done.
cb.end = true; cb();
}
// Gun.obj.map(cbs, cbe); // lets see if fixes heroku
if(!IT){ delete c.l[1]; return }
params.ContinuationToken = data.NextContinuationToken;
store.list(cb, match, params, cbs);
});
};
//store.list(function(){ return true });
if(false !== opt.rfs){ require('./rfsmix')(opt, store) } // ugly, but gotta move fast for now.
return store;
}
module.exports = Store;