A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

12 years ago
  1. var urlParse = require('url').parse;
  2. var ClientConstants = require('./protocol/constants/client');
  3. var Charsets = require('./protocol/constants/charsets');
  4. module.exports = ConnectionConfig;
  5. function ConnectionConfig(options) {
  6. if (typeof options === 'string') {
  7. options = ConnectionConfig.parseUrl(options);
  8. }
  9. this.host = options.host || 'localhost';
  10. this.port = options.port || 3306;
  11. this.socketPath = options.socketPath;
  12. this.user = options.user || undefined;
  13. this.password = options.password || undefined;
  14. this.database = options.database;
  15. this.insecureAuth = options.insecureAuth || false;
  16. this.debug = options.debug;
  17. this.timezone = options.timezone || 'local';
  18. this.flags = options.flags || '';
  19. this.queryFormat = options.queryFormat;
  20. this.typeCast = (options.typeCast === undefined)
  21. ? true
  22. : options.typeCast;
  23. if (this.timezone[0] == " ") {
  24. // "+" is a url encoded char for space so it
  25. // gets translated to space when giving a
  26. // connection string..
  27. this.timezone = "+" + this.timezone.substr(1);
  28. }
  29. this.maxPacketSize = 0;
  30. this.charsetNumber = (options.charset)
  31. ? ConnectionConfig.getCharsetNumber(options.charset)
  32. : Charsets.UTF8_GENERAL_CI;
  33. this.clientFlags = ConnectionConfig.mergeFlags(ConnectionConfig.getDefaultFlags(options),
  34. options.flags || '');
  35. }
  36. ConnectionConfig.mergeFlags = function(default_flags, user_flags) {
  37. var flags = 0x0, i;
  38. user_flags = (user_flags || '').toUpperCase().split(/\s*,+\s*/);
  39. // add default flags unless "blacklisted"
  40. for (i in default_flags) {
  41. if (user_flags.indexOf("-" + default_flags[i]) >= 0) continue;
  42. flags |= ClientConstants["CLIENT_" + default_flags[i]] || 0x0;
  43. }
  44. // add user flags unless already already added
  45. for (i in user_flags) {
  46. if (user_flags[i][0] == "-") continue;
  47. if (default_flags.indexOf(user_flags[i]) >= 0) continue;
  48. flags |= ClientConstants["CLIENT_" + user_flags[i]] || 0x0;
  49. }
  50. return flags;
  51. };
  52. ConnectionConfig.getDefaultFlags = function(options) {
  53. var defaultFlags = [ "LONG_PASSWORD", "FOUND_ROWS", "LONG_FLAG",
  54. "CONNECT_WITH_DB", "ODBC", "LOCAL_FILES",
  55. "IGNORE_SPACE", "PROTOCOL_41", "IGNORE_SIGPIPE",
  56. "TRANSACTIONS", "RESERVED", "SECURE_CONNECTION",
  57. "MULTI_RESULTS" ];
  58. if (options && options.multipleStatements) {
  59. defaultFlags.push("MULTI_STATEMENTS");
  60. }
  61. return defaultFlags;
  62. };
  63. ConnectionConfig.getCharsetNumber = function(charset) {
  64. return Charsets[charset];
  65. };
  66. ConnectionConfig.parseUrl = function(url) {
  67. url = urlParse(url, true);
  68. var options = {
  69. host : url.hostname,
  70. port : url.port,
  71. database : url.pathname.substr(1),
  72. };
  73. if (url.auth) {
  74. var auth = url.auth.split(':');
  75. options.user = auth[0];
  76. options.password = auth[1];
  77. }
  78. if (url.query) {
  79. for (var key in url.query) {
  80. var value = url.query[key];
  81. try {
  82. // Try to parse this as a JSON expression first
  83. options[key] = JSON.parse(value);
  84. } catch (err) {
  85. // Otherwise assume it is a plain string
  86. options[key] = value;
  87. }
  88. }
  89. }
  90. return options;
  91. };