npm-package-resolver
Version:
An API for Resolving NPM package dependencies
192 lines (127 loc) • 3.92 kB
JavaScript
/**
* Created by Liam Bilich on 07/03/2016.
* This is our redis transport module, all comm with redis is done through it.
*/
var redis = require('redis');
var util = require('util');
var _ = require('underscore');
// logger instance, provide unique id for current logging from this module, as 'redis-transport'
var _config = require('./config.js');
// later assigned to be the current config used, either for dev or production
var config = process.env['NODE_ENV']=="production"?_config.production:_config.development;
// VARIABLES AND CONSTANTS
var redisRetriesBeforeExit = 500;
var redisClientSubscriber;
var redisClientSetter;
var callback;
// wrap properly later on with real logging, such as winston and logstash / logstashUDP
// are we connected or not?
var connected = false;
function retryRedisConnect(){
if (redisRetriesBeforeExit>0) {
redisRetriesBeforeExit--;
} else {
//sentry.captureMessage('unable to reconnect to Redis, after few retries exiting');
console.error('unable to reconnect to Redis, after few retries exiting');
}
}
function connect(messageCallback){
console.log('connecting to Redis... host :',config.redis.host,' port:',config.redis.port);
// if server is not found no re-connection will be done
if (!connected){
redisClientSetter = redis.createClient(config.redis.port,config.redis.host,{connect_timeout:3600000});
redisClientSetter.on('error',function(e){
console.error('error finding redis! will try to reconnect:'+e);
});
connected = true;
if (messageCallback){
messageCallback();
}
} else {
console.log('redis already connected, skipping')
}
}
/**
* save value in redis key
*
* @param packageName;
* @param data
*/
function saveHash(packageName,data){
var _data;
//if (util.isString(data)){
if (typeof(data)=="string"){
_data = data;
} else if (typeof(data)=="object"){ // making sure we are s string
_data = JSON.stringify(data);
}
console.log('Saving data to redis',_data);
redisClientSetter.set(packageName, _data,function (e, reply) {
if (e){
console.error('redis error setting',e);
} else {
console.log(reply.toString());
}
});
}
/**
* wrapper for redis lpush, using proper convention
* @param key
* @param listArray
* @param callback
*/
function lpush(key,listArray,callback){
var cloned = _.clone(listArray);
cloned.unshift(key);
redisClientSetter.lpush(cloned,function(e,reply){
if (callback) {
callback(e,reply);
}
})
}
/**
* save value in redis key
*
* @param packageName - name of the package
* @param depsArray - array of dependencies
*/
function savePackageDeps(packageName,depsArray){
if (!connected) {
console.error('redis not connnected, cannot save data');
return;
}
var _data;
//if (util.isString(data)){
if (typeof(data)=="string"){
_data = data;
} else if (typeof(data)=="object"){ // making sure we are s string
_data = JSON.stringify(data);
}
console.log('Saving data to redis',_data);
redisClientSetter.lpush(packageName,depsArray)
}
function loadData(packageName,callback){
if (!connected) {
console.error('redis not connected, cannot load data');
return;
}
redisClientSetter.get(packageName,callback);
}
module.exports = {
connect:connect,
loadData:loadData,
savePackageDeps:savePackageDeps,
saveHash:saveHash,
lpush:lpush
}
Object.defineProperty(module.exports, 'connected', {
get: function() {
return connected;
}
});
// return redis client instance
Object.defineProperty(module.exports, 'client', {
get: function() {
return redisClientSetter;
}
});