This file is indexed.

/usr/lib/nodejs/pg/utils.js is in node-pg 0.7.1-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
var url = require('url');
var defaults = require(__dirname + "/defaults");
var events = require('events');

//compatibility for old nodes
if(typeof events.EventEmitter.prototype.once !== 'function') {
  events.EventEmitter.prototype.once = function (type, listener) {
    var self = this;
    self.on(type, function g () {
      self.removeListener(type, g);
      listener.apply(this, arguments);
    });
  };
}

var parseConnectionString = function(str) {
  //unix socket
  if(str.charAt(0) === '/') {
    return { host: str };
  }
  var result = url.parse(str);
  var config = {};
  config.host = result.hostname;
  config.database = result.pathname ? result.pathname.slice(1) : null
  var auth = (result.auth || ':').split(':');
  config.user = auth[0];
  config.password = auth[1];
  config.port = result.port;
  return config;
};

//allows passing false as property to remove it from config
var norm = function(config, propName) {
  config[propName] = (config[propName] || (config[propName] === false ? undefined : defaults[propName]))
};

//normalizes connection info
//which can be in the form of an object
//or a connection string
var normalizeConnectionInfo = function(config) {
  switch(typeof config) {
  case 'object':
    norm(config, 'user');
    norm(config, 'password');
    norm(config, 'host');
    norm(config, 'port');
    norm(config, 'database');
    return config;
  case 'string':
    return normalizeConnectionInfo(parseConnectionString(config));
  default:
    throw new Error("Unrecognized connection config parameter: " + config);
  }
};


var add = function(params, config, paramName) {
  var value = config[paramName];
  if(value) {
    params.push(paramName+"='"+value+"'");
  }
}

//builds libpq specific connection string
//from a supplied config object 
//the config object conforms to the interface of the config object
//accepted by the pure javascript client
var getLibpgConString = function(config, callback) {
  if(typeof config == 'object') {
    var params = []
    add(params, config, 'user');
    add(params, config, 'password');
    add(params, config, 'port');
    if(config.database) {
      params.push("dbname='" + config.database + "'");
    }
    if(config.host) {
      if(config.host != 'localhost' && config.host != '127.0.0.1') {
        //do dns lookup
        return require('dns').lookup(config.host, 4, function(err, address) {
          if(err) return callback(err, null);
          params.push("hostaddr="+address)
          callback(null, params.join(" "))
        })
      }
      params.push("hostaddr=127.0.0.1 ");
    }
    callback(null, params.join(" "));
  } else {
    throw new Error("Unrecognized config type for connection");
  }
}

module.exports = {
  normalizeConnectionInfo: normalizeConnectionInfo,
  //only exported here to make testing of this method possible
  //since it contains quite a bit of logic and testing for
  //each connection scenario in an integration test is impractical
  buildLibpqConnectionString: getLibpgConString,
  parseConnectionString: parseConnectionString
}