Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rs3.js now has an endpoint option, bumped aws-sdk #1112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 53 additions & 45 deletions lib/rs3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ var fs = require('fs');
var Radix = Radisk.Radix;
var u, AWS;

Gun.on('create', function(root){
Gun.on('create', function (root) {
// console.log("s3 create");
// console.log(root)
this.to.next(root);
var opt = root.opt;
if(!opt.s3 && !process.env.AWS_S3_BUCKET){ return }
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){
try {
AWS = require('aws-sdk');
} catch (e) {
console.log("aws-sdk is no longer included by default, you must add it to your package.json! `npm install aws-sdk`.");
AWS_SDK_NOT_INSTALLED;
}
Expand All @@ -24,83 +27,88 @@ Gun.on('create', function(root){
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;

if(opt.fakes3 = opt.fakes3 || process.env.fakes3){
if (process.env.AWS_ENDPOINT) opts.endpoint = opts.endpoint || process.env.AWS_ENDPOINT

if (opt.fakes3 = opt.fakes3 || process.env.fakes3) {
opts.endpoint = opt.fakes3;
opts.sslEnabled = false;
opts.bucket = opts.bucket.replace('.','p');
opts.bucket = opts.bucket.replace('.', 'p');
}

opts.config = new AWS.Config(opts);
opts.s3 = opts.s3 || new AWS.S3(opts.config);
// console.log(opts);
// opts.config = new AWS.Config(opts);
// with new sdk version AWS.Config isn't needed
opts.s3 = opts.s3 || new AWS.S3(opts);

opt.store = Object.keys(opts.s3).length === 0 ? opt.store : Store(opt);
});

function 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]){
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};
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');
});
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 }
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||''};
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||'').toString().slice(0,20));
s3.getObject(params, function got (err, ack) {
if (err && 'NoSuchKey' === err.code) { err = u }
// console.log("RS3 GOT <----", err, file, cbs.length, ((ack || {}).Body || '').toString().slice(0, 20));
delete c.g[file];//Gun.obj.del(c.g, file);
var data, data = (ack||'').Body;
var i = 0, cba; while(cba = cbs[i++]){ cba && cba(err, data) }//Gun.obj.map(cbs, cbe);
var data, data = (ack || '').Body;
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) }
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};
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){
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 }
})) { 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);
if(!IT){ Gun.obj.del(c.l, 1); return }
params.ContinuationToken = data.NextContinuationToken;
store.list(cb, match, params, cbs);
});
if (!IT) { Gun.obj.del(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.
if (false !== opt.rfs) { require('./rfsmix')(opt, store) } // ugly, but gotta move fast for now.
return store;
}

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@types/node": "^13.7.0",
"@types/uglify-js": "^3.0.4",
"@types/ws": "^7.2.1",
"aws-sdk": "^2.528.0",
"aws-sdk": "^2.965.0",
"ip": "^1.1.5",
"iris-messenger": "https://github.com/irislib/iris-messenger",
"mocha": "^6.2.0",
Expand Down