Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LeShaunJ committed Nov 29, 2018
1 parent 1413640 commit d64576a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 57 deletions.
34 changes: 28 additions & 6 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@
qry = Assign({}, Defq, req.body, req.query),
opt = THS.Opts(prm), pth = THS.Path(Routes),
cls = ()=>THS.Clause(Assign({}, prm, qry), RQ.Params),

onSuccess = function (rer, ret) {
qEN = THS.TimerEnd(qTM); aTM = THS.TimerStart();
RQ.links = {}; //!!con && con.release(); // Release connection; if needed
Expand Down Expand Up @@ -591,14 +592,25 @@
// Something fails before Query executes
return err => (
LG.Error(err.message, ident, 'GET'),
[ req.originalUrl, err.message, 500 ]
[ req.originalUrl, {
message: err.message,
stack: err.stack.split('\n'),
}, {} ]
);
// return err => (
// LG.Error(err.message, ident, 'GET'),
// [ req.originalUrl, JSON.stringify({
// message: err.message,
// stack: err.stack,
// }, null, ' '), JSN.Optify(
// Imm.Map(qry).filter(v=>v!='').toObject(),
// [], pth, opt, {}
// ) ]
// );
},
isFunc = !!RQ.Query.match(/^function/);

bEN = THS.TimerEnd(bTM);
cTM = THS.TimerStart();

// ---------------------------------------------
bEN = THS.TimerEnd(bTM); cTM = THS.TimerStart();
// Determine if Database is needed or not
if (isFunc) try {
cEN = THS.TimerEnd(cTM); qTM = THS.TimerStart();
Expand Down Expand Up @@ -643,6 +655,7 @@
let THS = this, sIOc = require('socket.io-client');
THS.Points = {};
THS.IO = services.map((u,i) => (
console.log(`SOCKET: ${u}`),
sIOc(u)
.removeAllListeners()
.on('points', points =>
Expand Down Expand Up @@ -740,7 +753,16 @@
rsv = ret => (ret.status==200?resolve(ret):reject(ret)),
def = THS._defaults(RID, point, method, props),
req = Assign({},misc,{method:method,params:params||{}},def);
if (!!!SCK) reject({ message: 'Endpoint does not Exist.' });
if (!!!SCK) {
console.log(
'\n',
THS.Points,
point,
THS.Points[point],
'\n'
)
reject({ message: 'Endpoint does not Exist.' });
}
SCK.on(RID, rsv); SCK.emit(point, req);
} );
}
Expand Down
75 changes: 47 additions & 28 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
function SockRoute (...args) { Socks.push(args); }

function ReqIOID(req) {
return (req.headers.cookie.match(/io=(\S+)(?=;|$)/)||[])[1];
console.log('ReqIOID:', JSON.stringify(req.headers,null,' '), '\n')

let cookie = ((req||{}).headers||{}).cookie||'';
return (cookie.match(/io=(\S+)(?=;|$)/)||[])[1];
}
function ReqLog (name, router) {
LG.IF(`\n\n\n\n${name} |`, router.stack.map((v,i) => {
Expand Down Expand Up @@ -427,7 +430,9 @@
new Array(6).fill(null),
send, [,,,,,next]
));
} catch (rerr) { route.ER(res, ...rerr); };
} catch (rerr) {
route.ER(res, ...rerr);
};
}; return Main;
default:
return async (req, res) => {
Expand Down Expand Up @@ -460,28 +465,39 @@
return FromJS(Assign({},itms));
} catch (e) { return e; } };
return Promise.all(links.toArray().map(prom));
}
};
// -----------------------------------------
let omits = (v,k)=>!['prev','next'].has(k),
{ query, body } = meta.Options,
gtLnk = L=>(typeof(L)=='string'?JSON.parse(L):L),
doLnk = gtLnk((query||body).links||false),
filtr = {
'array': (v,k)=>doLnk.has(k),
'boolean': ()=>true,
}, lnks, othr;
// -----------------------------------------
if (!!items && !!doLnk) {
lnks = Imm .Map(meta.Links)
.filter(filtr[ISS(doLnk)])
.filter(omits);
if (lnks.size) {
othr = await runLinks(lnks);
items = othr.reduce(
(R,V)=>R.mergeDeep(V),
FromJS(Assign({},items))
).toJS();
} }; return items;
try {
let omits = (v,k)=>!['prev','next'].has(k),
{ query, body } = meta.Options,
gtLnk = L=>(typeof(L)=='string'?JSON.parse(L):L),
doLnk = gtLnk((query||body).links||false),
filtr = {
'array': (v,k)=>doLnk.has(k),
'boolean': ()=>true,
}, lnks, othr;
// -----------------------------------------
if (!!items && !!doLnk) {
lnks = Imm .Map(meta.Links)
.filter(filtr[ISS(doLnk)])
.filter(omits);
if (lnks.size) {
othr = await runLinks(lnks);
items = othr.reduce(
(R,V)=>R.mergeDeep(V),
FromJS(Assign({},items))
).toJS();
} }; return items;
} catch(e) {
console.log('LERR:', e, '\n')
console.log(
req.originalUrl,
JSON.stringify({
items: items, meta: meta
}, null, ' '),
'\n'
); return [];
}
}; itms = await doLinks(itms,opts);
// --------------------------------------------
route.SN(res, err||itms, opts);
Expand Down Expand Up @@ -823,7 +839,6 @@
'Cache-Control': 'no-cache', // `public, max-age=${Publics.Age}`
},
LOKR = Session.Stores.Lockers;
cnfg.host = (Setting.API||'');
// -----------
fs.readFile(Publics.Folder.index, 'utf8', (err,mark) => {
if (err) return LG.Error(err);
Expand All @@ -833,7 +848,10 @@
});
// -----------
return function AddingSite(req, res) {
let path = req.originalUrl;
let path = req.originalUrl,
host = req.headers.host,
ptcl = req.protocol;
cnfg.host = `${ptcl}://${host}`;
LG.Timed(()=>{
let ctn, rnd, rep, call,
lid = ReqIOID(req),
Expand All @@ -844,7 +862,7 @@
rnd.Auth(cfg.title)
);

console.log(lid)
console.log('AddingSite.LID:', lid, JSON.stringify(req.headers,null,' '), '\n')

delete req.profile;
rnd = Renders(global,cfg,REST,usr,path);
Expand Down Expand Up @@ -873,7 +891,8 @@
}).catch(err => {
console.log(
`${refl} CALL CACHE ERROR:`,
JSON.stringify(err,null,' '));
JSON.stringify(err,null,' '),
'\n');
!!rnd.Redirect &&
res.redirect(301,rnd.Redirect);
});
Expand All @@ -888,7 +907,7 @@
`${refl} CACHE ERROR:`,
JSON.stringify(err,null,' ')
);
else console.log(`${refl} CACHER:`, stat);
else console.log(`${refl} CACHER:`,stat,'\n');
} );
res.status(200).send(ctn);
}
Expand Down
37 changes: 18 additions & 19 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,24 @@
}
function stripSID (sid) { return sid.replace(/^sess:/, ''); }
function sendLoad (which) {
return function (sid, strict) {
const payloads = {
Logout: {
status: 200, payload: { options: {
query:{ path: '/auth/logout' }
}, result: { code: 2, message: 'User Logged Out.' } }
},
Expired: {
status: 200, payload: { options: {
query:{ path: '/auth/logout' }
}, result: { code: 3, message: 'Session Expired.' } }
},
Regenerate: {
status: 200, payload: { options: {
query:{ path: '/auth/regenerate' }
} }
},
};
return function (sid, strict) {
var payload = payloads[which];
if (!!sid) THS.Accessor
.to(stripSID(sid))
Expand Down Expand Up @@ -192,24 +209,6 @@

THS.Stores = { [SCFG.REDIS.Main]: CStore.client };
SCFG.REDIS.Stores.map(s => (THS.Stores[s]=redis(config.store)))

const payloads = {
Logout: {
status: 200, payload: { options: {
query:{ path: '/auth/logout' }
}, result: { code: 2, message: 'User Logged Out.' } }
},
Expired: {
status: 200, payload: { options: {
query:{ path: '/auth/logout' }
}, result: { code: 3, message: 'Session Expired.' } }
},
Regenerate: {
status: 200, payload: { options: {
query:{ path: '/auth/regenerate' }
} }
},
};

const sessMDW = session({
store: CStore,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dffrnt.route",
"version": "2.0.5",
"version": "2.0.6",
"description": "An Express/SocketIO/Passport-based API Router",
"keywords": [
"api",
Expand Down Expand Up @@ -33,8 +33,8 @@
"cookie-parser": "1.4.3",
"cors": "2.8.4",
"crypto-token": "1.0.1",
"dffrnt.confs": "2.0.5",
"dffrnt.utils": "2.0.5",
"dffrnt.confs": "2.0.6",
"dffrnt.utils": "2.0.6",
"express": "4.16.3",
"express-limiter": "1.6.1",
"express-recaptcha": "4.0.2",
Expand All @@ -53,4 +53,4 @@
"optionalDependencies": {
"bufferutil": "1.3.0"
}
}
}

0 comments on commit d64576a

Please sign in to comment.