connection_config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var urlParse = require('url').parse;
  2. var ClientConstants = require('./constants/client');
  3. var Charsets = require('./constants/charsets');
  4. var SSLProfiles = null;
  5. module.exports = ConnectionConfig;
  6. function ConnectionConfig(options) {
  7. if (typeof options === 'string') {
  8. options = ConnectionConfig.parseUrl(options);
  9. }
  10. this.isServer = options.isServer;
  11. this.stream = options.stream;
  12. this.host = options.host || 'localhost';
  13. this.port = options.port || 3306;
  14. this.localAddress = options.localAddress;
  15. this.socketPath = options.socketPath;
  16. this.user = options.user || undefined;
  17. this.password = options.password || undefined;
  18. this.passwordSha1 = options.passwordSha1 || undefined;
  19. this.database = options.database;
  20. this.connectTimeout = (options.connectTimeout === undefined)
  21. ? (10 * 1000)
  22. : options.connectTimeout;
  23. this.insecureAuth = options.insecureAuth || false;
  24. this.supportBigNumbers = options.supportBigNumbers || false;
  25. this.bigNumberStrings = options.bigNumberStrings || false;
  26. this.dateStrings = options.dateStrings || false;
  27. this.debug = options.debug;
  28. this.trace = options.trace !== false;
  29. this.stringifyObjects = options.stringifyObjects || false;
  30. this.timezone = options.timezone || 'local';
  31. this.flags = options.flags || '';
  32. this.queryFormat = options.queryFormat;
  33. this.pool = options.pool || undefined;
  34. this.ssl = (typeof options.ssl === 'string')
  35. ? ConnectionConfig.getSSLProfile(options.ssl)
  36. : (options.ssl || false);
  37. this.multipleStatements = options.multipleStatements || false;
  38. this.namedPlaceholders = options.namedPlaceholders || false;
  39. this.typeCast = (options.typeCast === undefined)
  40. ? true
  41. : options.typeCast;
  42. if (this.timezone[0] == " ") {
  43. // "+" is a url encoded char for space so it
  44. // gets translated to space when giving a
  45. // connection string..
  46. this.timezone = "+" + this.timezone.substr(1);
  47. }
  48. if (this.ssl) {
  49. // Default rejectUnauthorized to true
  50. this.ssl.rejectUnauthorized = this.ssl.rejectUnauthorized !== false;
  51. }
  52. this.maxPacketSize = 0;
  53. this.charsetNumber = (options.charset)
  54. ? ConnectionConfig.getCharsetNumber(options.charset)
  55. : options.charsetNumber||Charsets.UTF8_GENERAL_CI;
  56. this.clientFlags = ConnectionConfig.mergeFlags(ConnectionConfig.getDefaultFlags(options),
  57. options.flags || '');
  58. }
  59. ConnectionConfig.mergeFlags = function(default_flags, user_flags) {
  60. var flags = 0x0, i;
  61. user_flags = (user_flags || '').toUpperCase().split(/\s*,+\s*/);
  62. // add default flags unless "blacklisted"
  63. for (i in default_flags) {
  64. if (user_flags.indexOf("-" + default_flags[i]) >= 0) continue;
  65. flags |= ClientConstants[default_flags[i]] || 0x0;
  66. }
  67. // add user flags unless already already added
  68. for (i in user_flags) {
  69. if (user_flags[i][0] == "-") continue;
  70. if (default_flags.indexOf(user_flags[i]) >= 0) continue;
  71. flags |= ClientConstants[user_flags[i]] || 0x0;
  72. }
  73. return flags;
  74. };
  75. ConnectionConfig.getDefaultFlags = function(options) {
  76. var defaultFlags = [ "LONG_PASSWORD", "FOUND_ROWS", "LONG_FLAG",
  77. "CONNECT_WITH_DB", "ODBC", "LOCAL_FILES",
  78. "IGNORE_SPACE", "PROTOCOL_41", "IGNORE_SIGPIPE",
  79. "TRANSACTIONS", "RESERVED", "SECURE_CONNECTION",
  80. "MULTI_RESULTS" ];
  81. if (options && options.multipleStatements) {
  82. defaultFlags.push("MULTI_STATEMENTS");
  83. }
  84. return defaultFlags;
  85. };
  86. ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset) {
  87. var num = Charsets[charset.toUpperCase()];
  88. if (num === undefined) {
  89. throw new TypeError('Unknown charset \'' + charset + '\'');
  90. }
  91. return num;
  92. };
  93. ConnectionConfig.getSSLProfile = function getSSLProfile(name) {
  94. if (!SSLProfiles) {
  95. SSLProfiles = require('./constants/ssl_profiles.js');
  96. }
  97. var ssl = SSLProfiles[name];
  98. if (ssl === undefined) {
  99. throw new TypeError('Unknown SSL profile \'' + name + '\'');
  100. }
  101. return ssl;
  102. };
  103. ConnectionConfig.parseUrl = function(url) {
  104. url = urlParse(url, true);
  105. var options = {
  106. host : url.hostname,
  107. port : url.port,
  108. database : url.pathname.substr(1),
  109. };
  110. if (url.auth) {
  111. var auth = url.auth.split(':');
  112. options.user = auth[0];
  113. options.password = auth[1];
  114. }
  115. if (url.query) {
  116. for (var key in url.query) {
  117. var value = url.query[key];
  118. try {
  119. // Try to parse this as a JSON expression first
  120. options[key] = JSON.parse(value);
  121. } catch (err) {
  122. // Otherwise assume it is a plain string
  123. options[key] = value;
  124. }
  125. }
  126. }
  127. return options;
  128. };