Comparing version 1.0.13 to 1.0.14
@@ -20,2 +20,3 @@ //Low level connection to PostgreSQL database | ||
types.setTypeParser(1082, d=>(d)) //Don't convert simple dates to JS date/time | ||
types.setTypeParser(20, v=>(parseInt(v))) //Convert int8 to Number; this breaks on numbers > 2^53! | ||
@@ -25,4 +26,5 @@ module.exports = class dbClient { | ||
this.log = conf.log || require('./log') | ||
delete conf.log | ||
this.config = conf | ||
this.config = Object.assign({}, conf) //Private copy for this instance | ||
delete this.config.log | ||
this.notifyCB = notifyCB | ||
@@ -33,9 +35,8 @@ this.connectCB = connectCB | ||
this.connecting = false | ||
//this.log.debug('AAA', conf.listen, typeof(conf.listen)) | ||
if (conf.listen && !Array.isArray(conf.listen)) conf.listen = [conf.listen] | ||
if (conf.listen || conf.connect) this.connect(() => { //Connect now so we can listen | ||
this.log.debug('dbClient listen:', this.config.listen, typeof(this.config.listen)) | ||
if (this.config.listen && !Array.isArray(this.config.listen)) this.config.listen = [this.config.listen] | ||
if (this.config.listen || this.config.connect) this.connect(() => { //Connect now so we can listen | ||
let q; while (q = this.queryQue.shift()) this.query(...q) //And process the queue | ||
if (this.connectCB) this.connectCB() | ||
}) | ||
this.log.trace("New database client:", conf) | ||
} | ||
@@ -53,3 +54,3 @@ | ||
this.client = new Client(this.config) | ||
this.log.trace("New DB client config:", this.config) | ||
this.log.verbose("New DB client config:", this.config) | ||
if (this.config.listen && this.queryQue.length <= 0) { | ||
@@ -129,3 +130,3 @@ this.config.listen.forEach(listen=>{ | ||
query(...args) { // Attempt a DB query. If not yet connected, queue the request and attempt to connect | ||
this.log.trace("Query:", args[0].substr(0,256), args[1]) | ||
this.log.debug("Query:", this.config.user, args[0].substr(0,256), args[1]) | ||
if (this.client && this.client._connected) { //If connection ready, run the query | ||
@@ -132,0 +133,0 @@ this.client.query(...args) |
@@ -11,3 +11,4 @@ //Basic logging if nothing else provided | ||
debug: (...msg) => logger(msg.join(' ')), | ||
verbose: (...msg) => logger(msg.join(' ')), | ||
error: (...msg) => console.error(...msg) | ||
} |
@@ -14,2 +14,3 @@ //Manage the connection between a User Interface and the backend database | ||
//- Return query promise in case no callback given? | ||
//- Restructure code as explained in: https://github.com/websockets/ws/issues/377#issuecomment-462152231 | ||
//- | ||
@@ -38,7 +39,7 @@ | ||
, log = this.log = dbConf.log || wsConf.log || adminConf.log || require('./log') | ||
, ctx = {db:null, control:null, actions, dispatch, expApp, log} | ||
, context = {db:null, control:null, actions, dispatch, expApp, log} | ||
, server = credentials ? Https.createServer(credentials) : Http.createServer() | ||
, adminDB = new DbClient(adminConf) //Admin access to the DB | ||
, validateToken = (user, token, pub, listen, cb) => { //Validate user with one-time login token | ||
, validateToken = (user, token, pub, listen, payload, cb) => { //Validate user with one-time login token | ||
log.trace("Request to validate:", user) | ||
@@ -48,3 +49,3 @@ adminDB.query('select base.validate_token($1,$2,$3) as valid', [user, token, pub], (err, res)=>{ | ||
let valid = (!err && res && res.rows && res.rows.length >= 1) ? res.rows[0].valid : false | ||
if (valid) Object.assign(dbConf, {user,listen}) //Tell later db connect our username and db listen options | ||
if (valid) Object.assign(payload, {user,listen}) //Tell later db connect our username and db listen options | ||
log.debug(" valid result:", valid) | ||
@@ -54,3 +55,3 @@ cb(valid) | ||
} | ||
, validateSignature = (user, sign, message, listen, cb) => { //Validate a user with an existing key | ||
, validateSignature = (user, sign, message, listen, payload, cb) => { //Validate a user with an existing key | ||
log.trace("Validate:", user, sign, message) | ||
@@ -70,3 +71,3 @@ adminDB.query('select conn_pub from base.ent_v where username = $1', [user], (err, res)=>{ | ||
valid = verify.verify(Object.assign({key}, VerifyTpt), rawSig) | ||
if (valid) Object.assign(dbConf, {user,listen}) //Tell later db connect our username and db listen options | ||
if (valid) Object.assign(payload, {user,listen}) //Tell later db connect our username and db listen options | ||
} | ||
@@ -86,5 +87,6 @@ log.trace(" valid:", valid) | ||
, listen = db ? JSON.parse(Buffer.from(db,'hex').toString()) : null | ||
log.debug("Checking client:", origin, "cb:", !!cb, "q:", query, "s:", secure, "IP:", req.connection.remoteAddress, "listen:", listen, typeof(listen)) | ||
, payload = req.WysemanPayload = {} //Custom Wyseman data to pass back to connection | ||
log.trace("Checking client:", origin, "cb:", !!cb, "q:", query, "s:", secure, "IP:", req.connection.remoteAddress, "listen:", listen, typeof(listen)) | ||
if (user && token && pub) | ||
validateToken(user, token, pub, listen, (valid)=>{ | ||
validateToken(user, token, pub, listen, payload, (valid)=>{ | ||
cb(valid, 403, 'Invalid Login') //Tell websocket whether or not to connect | ||
@@ -99,7 +101,7 @@ }) | ||
cb(false, 400, 'Invalid Date Stamp') | ||
else validateSignature(user, sign, message, listen, (valid)=>{ | ||
else validateSignature(user, sign, message, listen, payload, (valid)=>{ | ||
cb(valid, 403, 'Invalid Login') //Tell websocket whether or not to connect | ||
}) | ||
} else if (user && !secure) { | ||
Object.assign(dbConf, {user,listen}) //Tell later db connect our username and db listen options | ||
Object.assign(payload, {user,listen}) //Tell later db connect our username and db listen options | ||
cb(true) //On an insecure/debug web connection | ||
@@ -110,9 +112,12 @@ } else | ||
}) | ||
log.debug("Wyseman listening:", port) | ||
log.info("Wyseman listening:", port) | ||
if (port) server.listen(port) | ||
wss.on('connection', (ws) => { //When connection from view client is open | ||
log.debug("WS Connected; User:", dbConf.user, dbConf) | ||
if (!dbConf.user) return //Shouldn't be able to get here without a username | ||
ctx.db = new DbClient(dbConf, (channel, message, mine) => { | ||
wss.on('connection', (ws, req) => { //When connection from view client is open | ||
let payload = req.WysemanPayload | ||
, config = Object.assign({}, dbConf, payload) //user,listen passed from verifyClient | ||
, ctx = Object.assign({}, context) //Private copy for this instance | ||
log.verbose("WS Connected; User:", config.user, config) | ||
if (!config.user) return //Shouldn't be able to get here without a username | ||
ctx.db = new DbClient(config, (channel, message, mine) => { | ||
let data = JSON.parse(message) | ||
@@ -119,0 +124,0 @@ this.log.trace("Async notify from DB:", channel, data, mine) |
{ | ||
"name": "wyseman", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"description": "PostgreSQL Schema Manager with Javascript, Ruby, TCL API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
619628
632