larvituser
Advanced tools
Comparing version 0.6.0 to 0.7.0
'use strict'; | ||
const EventEmitter = require('events').EventEmitter, | ||
eventEmitter = new EventEmitter(), | ||
dbmigration = require('larvitdbmigration')({'tableName': 'users_db_version', 'migrationScriptsPath': __dirname + '/dbmigration'}), | ||
helpers = require(__dirname + '/helpers.js'), | ||
lUtils = require('larvitutils'), | ||
intercom = lUtils.instances.intercom, | ||
helpers = require(__dirname + '/helpers.js'), | ||
amsync = require('larvitamsync'), | ||
async = require('async'), | ||
@@ -11,2 +13,6 @@ log = require('winston'), | ||
let readyInProgress = false, | ||
isReady = false, | ||
intercom; | ||
function addField(params, deliveryTag, msgUuid) { | ||
@@ -60,2 +66,157 @@ const uuid = params.uuid, | ||
function listenToQueue(retries, cb) { | ||
const options = {'exchange': exports.exchangeName}; | ||
let listenMethod; | ||
if (typeof retries === 'function') { | ||
cb = retries; | ||
retries = 0; | ||
} | ||
if (typeof cb !== 'function') { | ||
cb = function(){}; | ||
} | ||
if (retries === undefined) { | ||
retries = 0; | ||
} | ||
if (exports.mode === 'master') { | ||
listenMethod = 'consume'; | ||
options.exclusive = true; // It is important no other client tries to sneak | ||
// out messages from us, and we want "consume" | ||
// since we want the queue to persist even if this | ||
// minion goes offline. | ||
} else if (exports.mode === 'slave') { | ||
listenMethod = 'subscribe'; | ||
} else { | ||
const err = new Error('Invalid exports.mode. Must be either "master" or "slave"'); | ||
log.error('larvituser: dataWriter.js - listenToQueue() - ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
intercom = lUtils.instances.intercom; | ||
if ( ! (intercom instanceof require('larvitamintercom')) && retries < 10) { | ||
retries ++; | ||
setTimeout(function() { | ||
listenToQueue(retries, cb); | ||
}, 50); | ||
return; | ||
} else if ( ! (intercom instanceof require('larvitamintercom'))) { | ||
log.error('larvituser: dataWriter.js - listenToQueue() - Intercom is not set!'); | ||
return; | ||
} | ||
log.info('larvituser: dataWriter.js - listenToQueue() - listenMethod: ' + listenMethod); | ||
intercom.ready(function(err) { | ||
if (err) { | ||
log.error('larvituser: dataWriter.js - listenToQueue() - intercom.ready() err: ' + err.message); | ||
return; | ||
} | ||
intercom[listenMethod](options, function(message, ack, deliveryTag) { | ||
exports.ready(function(err) { | ||
ack(err); // Ack first, if something goes wrong we log it and handle it manually | ||
if (err) { | ||
log.error('larvituser: dataWriter.js - listenToQueue() - intercom.' + listenMethod + '() - exports.ready() returned err: ' + err.message); | ||
return; | ||
} | ||
if (typeof message !== 'object') { | ||
log.error('larvituser: dataWriter.js - listenToQueue() - intercom.' + listenMethod + '() - Invalid message received, is not an object! deliveryTag: "' + deliveryTag + '"'); | ||
return; | ||
} | ||
if (typeof exports[message.action] === 'function') { | ||
exports[message.action](message.params, deliveryTag, message.uuid); | ||
} else { | ||
log.warn('larvituser: dataWriter.js - listenToQueue() - intercom.' + listenMethod + '() - Unknown message.action received: "' + message.action + '"'); | ||
} | ||
}); | ||
}, ready); | ||
}); | ||
} | ||
// Run listenToQueue as soon as all I/O is done, this makes sure the exports.mode can be set | ||
// by the application before listening commences | ||
setImmediate(listenToQueue); | ||
function ready(retries, cb) { | ||
const tasks = []; | ||
if (typeof retries === 'function') { | ||
cb = retries; | ||
retries = 0; | ||
} | ||
if (typeof cb !== 'function') { | ||
cb = function(){}; | ||
} | ||
if (retries === undefined) { | ||
retries = 0; | ||
} | ||
if (isReady === true) { cb(); return; } | ||
if (readyInProgress === true) { | ||
eventEmitter.on('ready', cb); | ||
return; | ||
} | ||
intercom = lUtils.instances.intercom; | ||
if ( ! (intercom instanceof require('larvitamintercom')) && retries < 10) { | ||
retries ++; | ||
setTimeout(function() { | ||
ready(retries, cb); | ||
}, 50); | ||
return; | ||
} else if ( ! (intercom instanceof require('larvitamintercom'))) { | ||
log.error('larvituser: dataWriter.js - ready() - Intercom is not set!'); | ||
return; | ||
} | ||
readyInProgress = true; | ||
if (exports.mode === 'both' || exports.mode === 'slave') { | ||
log.verbose('larvituser: dataWriter.js - ready() - exports.mode: "' + exports.mode + '", so read'); | ||
tasks.push(function(cb) { | ||
amsync.mariadb({'exchange': exports.exchangeName + '_dataDump'}, cb); | ||
}); | ||
} | ||
// Migrate database | ||
tasks.push(function(cb) { | ||
dbmigration(function(err) { | ||
if (err) { | ||
log.error('larvituser: dataWriter.js - ready() - Database error: ' + err.message); | ||
} | ||
cb(err); | ||
}); | ||
}); | ||
async.series(tasks, function(err) { | ||
if (err) { | ||
return; | ||
} | ||
isReady = true; | ||
exports.intercom = intercom; | ||
eventEmitter.emit('ready'); | ||
if (exports.mode === 'both' || exports.mode === 'master') { | ||
runDumpServer(cb); | ||
} else { | ||
cb(); | ||
} | ||
}); | ||
} | ||
function replaceFields(params, deliveryTag, msgUuid) { | ||
@@ -173,2 +334,38 @@ const fieldNamesToUuidBufs = {}, | ||
function runDumpServer(cb) { | ||
const options = {'exchange': exports.exchangeName + '_dataDump'}, | ||
args = []; | ||
if (db.conf.host) { | ||
args.push('-h'); | ||
args.push(db.conf.host); | ||
} | ||
args.push('-u'); | ||
args.push(db.conf.user); | ||
if (db.conf.password) { | ||
args.push('-p' + db.conf.password); | ||
} | ||
args.push('--single-transaction'); | ||
args.push('--hex-blob'); | ||
args.push(db.conf.database); | ||
// Tables | ||
args.push('user_data_fields'); | ||
args.push('user_roles_rights'); | ||
args.push('user_users'); | ||
args.push('user_users_data'); | ||
options.dataDumpCmd = { | ||
'command': 'mysqldump', | ||
'args': args | ||
}; | ||
options['Content-Type'] = 'application/sql'; | ||
new amsync.SyncServer(options, cb); | ||
} | ||
function setPassword(params, deliveryTag, msgUuid) { | ||
@@ -204,2 +401,4 @@ const dbFields = [], | ||
exports.exchangeName = 'larvituser'; | ||
exports.mode = ''; // "slave" or "master" | ||
exports.ready = ready; | ||
exports.replaceFields = replaceFields; | ||
@@ -210,22 +409,1 @@ exports.rmUser = rmUser; | ||
exports.setUsername = setUsername; | ||
intercom.subscribe({'exchange': exports.exchangeName}, function(message, ack, deliveryTag) { | ||
ack(); // Ack first, if something goes wrong we log it and handle it manually | ||
if (typeof message !== 'object') { | ||
log.error('larvituser: dataWriter.js - intercom.subscribe() - Invalid message received, is not an object! deliveryTag: "' + deliveryTag + '"'); | ||
return; | ||
} | ||
if (message.uuid === undefined) { | ||
log.warn('larvituser: dataWriter.js - intercom.subscribe() - No message.uuid supplied. deliveryTag: "' + deliveryTag + '", message: "' + JSON.stringify(message) + '"'); | ||
} | ||
if (typeof exports[message.action] === 'function') { | ||
log.debug('larvituser: dataWriter.js - intercom.subscribe() - Running action "' + message.action + '", msgUuid: "' + message.uuid + '", deliveryTag: "' + deliveryTag + '"'); | ||
exports[message.action](message.params, deliveryTag, message.uuid); | ||
} else { | ||
log.warn('larvituser: dataWriter.js - intercom.subscribe() - Unknown message.action received: "' + message.action + '", msgUuid: "' + message.uuid + '", deliveryTag: "' + deliveryTag + '"'); | ||
} | ||
}); |
82
index.js
'use strict'; | ||
const EventEmitter = require('events').EventEmitter, | ||
eventEmitter = new EventEmitter(), | ||
dbmigration = require('larvitdbmigration')({'tableName': 'users_db_version', 'migrationScriptsPath': __dirname + '/dbmigration'}), | ||
const dataWriter = require(__dirname + '/dataWriter.js'), | ||
lUtils = require('larvitutils'), | ||
@@ -13,7 +11,2 @@ uuidLib = require('uuid'), | ||
let readyInProgress = false, | ||
isReady = false, | ||
dataWriter, | ||
intercom; | ||
/** | ||
@@ -37,3 +30,3 @@ * Add a single user field to database | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -102,3 +95,3 @@ | ||
tasks.push(ready); | ||
tasks.push(dataWriter.ready); | ||
@@ -164,3 +157,3 @@ // Check for username availability | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -201,3 +194,3 @@ | ||
function fromField(fieldName, fieldValue, cb) { | ||
ready(function() { | ||
dataWriter.ready(function(err) { | ||
const dbFields = [fieldName.trim(), fieldValue.trim()], | ||
@@ -210,2 +203,4 @@ sql = 'SELECT uud.userUuid\n' + | ||
if (err) { cb(err); return; } | ||
db.query(sql, dbFields, function(err, rows) { | ||
@@ -232,3 +227,3 @@ if (err) { cb(err); return; } | ||
function fromFields(fields, cb) { | ||
ready(function() { | ||
dataWriter.ready(function(err) { | ||
const dbFields = []; | ||
@@ -238,2 +233,4 @@ | ||
if (err) { cb(err); return; } | ||
for (const fieldName of Object.keys(fields)) { | ||
@@ -276,3 +273,3 @@ sql += ' AND uuid IN (SELECT userUuid FROM user_users_data WHERE data = ? AND fieldUuid = (SELECT uuid FROM user_data_fields WHERE name = ?))\n'; | ||
tasks.push(ready); | ||
tasks.push(dataWriter.ready); | ||
@@ -343,3 +340,5 @@ tasks.push(function(cb) { | ||
ready(function() { | ||
dataWriter.ready(function(err) { | ||
if (err) { cb(err); return; } | ||
db.query(sql, dbFields, function(err, rows) { | ||
@@ -384,3 +383,5 @@ if (err) { cb(err); return; } | ||
ready(function() { | ||
dataWriter.ready(function(err) { | ||
if (err) { cb(err); return; } | ||
db.query(sql, dbFields, function(err, rows) { | ||
@@ -468,35 +469,2 @@ if (err) { cb(err); return; } | ||
function ready(cb) { | ||
if (isReady === true) { cb(); return; } | ||
if (readyInProgress === true) { | ||
eventEmitter.on('ready', cb); | ||
return; | ||
} | ||
readyInProgress = true; | ||
intercom = lUtils.instances.intercom; // We must do this here since it might not be instanciated on module load | ||
// We are strictly in need of the intercom! | ||
if ( ! (intercom instanceof require('larvitamintercom'))) { | ||
const err = new Error('larvitutils.instances.intercom is not an instance of Intercom!'); | ||
log.error('larvituser: index.js - ' + err.message); | ||
throw err; | ||
} | ||
dataWriter = require(__dirname + '/dataWriter.js'); // We must do this here since it might not be instanciated on module load | ||
dbmigration(function(err) { | ||
if (err) { | ||
log.error('larvituser: index.js: Database error: ' + err.message); | ||
return; | ||
} | ||
isReady = true; | ||
eventEmitter.emit('ready'); | ||
cb(); | ||
}); | ||
} | ||
/** | ||
@@ -519,3 +487,3 @@ * Replace all fields | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -541,3 +509,3 @@ | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -565,3 +533,3 @@ | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -606,3 +574,3 @@ | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -653,3 +621,3 @@ | ||
intercom.send(sendObj, options, function(err, msgUuid) { | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
@@ -793,3 +761,3 @@ | ||
tasks.push(ready); | ||
tasks.push(dataWriter.ready); | ||
@@ -820,2 +788,3 @@ tasks.push(function(cb) { | ||
exports.create = create; | ||
exports.dataWriter = dataWriter; | ||
exports.fromField = fromField; | ||
@@ -828,3 +797,4 @@ exports.fromFields = fromFields; | ||
exports.hashPassword = hashPassword; | ||
exports.ready = ready; | ||
exports.ready = dataWriter.ready; | ||
exports.replaceUserFields = replaceUserFields; | ||
exports.rmUser = rmUser; | ||
@@ -831,0 +801,0 @@ exports.setUsername = setUsername; |
{ | ||
"name": "larvituser", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"author": { | ||
@@ -14,2 +14,3 @@ "name": "Mikael 'Lilleman' Göransson", | ||
"larvitamintercom": "^0.1.1", | ||
"larvitamsync": "^0.1.10", | ||
"larvitdb": "^1.0.0", | ||
@@ -16,0 +17,0 @@ "larvitdbmigration": "^1.3.3", |
@@ -12,3 +12,5 @@ [![Build Status](https://travis-ci.org/larvit/larvituser.svg?branch=master)](https://travis-ci.org/larvit/larvituser) [![Dependencies](https://david-dm.org/larvit/larvituser.svg)](https://david-dm.org/larvit/larvituser.svg) | ||
```javascript | ||
var userLib = require('larvituser'); | ||
const userLib = require('larvituser'); | ||
userLib.dataWriter.mode = 'master'; // Used for standalone use. If multiple applications connect via rabbitMQ the other should be "slave" | ||
``` | ||
@@ -19,3 +21,3 @@ | ||
```javascript | ||
var userData = { | ||
const userData = { | ||
'firstname': 'Nisse', | ||
@@ -57,3 +59,3 @@ 'lastname': 'Nilsson', | ||
if (err) throw err; | ||
console.log(userList); // An array of objects | ||
@@ -60,0 +62,0 @@ }); |
@@ -12,2 +12,4 @@ 'use strict'; | ||
userLib.dataWriter.mode = 'master'; | ||
// Set up winston | ||
@@ -14,0 +16,0 @@ log.remove(log.transports.Console); |
11
users.js
'use strict'; | ||
const lUtils = require('larvitutils'), | ||
const dataWriter = require(__dirname + '/dataWriter.js'), | ||
lUtils = require('larvitutils'), | ||
async = require('async'), | ||
@@ -17,7 +18,9 @@ db = require('larvitdb'); | ||
tasks.push(dataWriter.ready); | ||
tasks.push(function(cb) { | ||
const dbFields = []; | ||
let sql = 'SELECT uuid, username FROM user_users WHERE 1 '; | ||
let sql = 'SELECT uuid, username FROM user_users WHERE 1 '; | ||
if (that.matchAllFields !== undefined) { | ||
@@ -47,3 +50,3 @@ for (let field in that.matchAllFields) { | ||
const user = {}; | ||
const user = {}; | ||
@@ -50,0 +53,0 @@ user.uuid = lUtils.formatUuid(rows[i].uuid); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
68150
1746
200
0
9
22
21
+ Addedlarvitamsync@^0.1.10
+ Addedlarvitamsync@0.1.10(transitive)