PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/mysql/lib/ConnectionConfig.js

https://gitlab.com/ralexs04/shop-List-w
JavaScript | 201 lines | 155 code | 34 blank | 12 comment | 37 complexity | 9cfe0784f15eed7c5d6dca3dbe615b16 MD5 | raw file
  1. var urlParse = require('url').parse;
  2. var ClientConstants = require('./protocol/constants/client');
  3. var Charsets = require('./protocol/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.host = options.host || 'localhost';
  11. this.port = options.port || 3306;
  12. this.localAddress = options.localAddress;
  13. this.socketPath = options.socketPath;
  14. this.user = options.user || undefined;
  15. this.password = options.password || undefined;
  16. this.database = options.database;
  17. this.connectTimeout = (options.connectTimeout === undefined)
  18. ? (10 * 1000)
  19. : options.connectTimeout;
  20. this.insecureAuth = options.insecureAuth || false;
  21. this.supportBigNumbers = options.supportBigNumbers || false;
  22. this.bigNumberStrings = options.bigNumberStrings || false;
  23. this.dateStrings = options.dateStrings || false;
  24. this.debug = options.debug;
  25. this.trace = options.trace !== false;
  26. this.stringifyObjects = options.stringifyObjects || false;
  27. this.timezone = options.timezone || 'local';
  28. this.flags = options.flags || '';
  29. this.queryFormat = options.queryFormat;
  30. this.pool = options.pool || undefined;
  31. this.ssl = (typeof options.ssl === 'string')
  32. ? ConnectionConfig.getSSLProfile(options.ssl)
  33. : (options.ssl || false);
  34. this.multipleStatements = options.multipleStatements || false;
  35. this.typeCast = (options.typeCast === undefined)
  36. ? true
  37. : options.typeCast;
  38. if (this.timezone[0] == " ") {
  39. // "+" is a url encoded char for space so it
  40. // gets translated to space when giving a
  41. // connection string..
  42. this.timezone = "+" + this.timezone.substr(1);
  43. }
  44. if (this.ssl) {
  45. // Default rejectUnauthorized to true
  46. this.ssl.rejectUnauthorized = this.ssl.rejectUnauthorized !== false;
  47. }
  48. this.maxPacketSize = 0;
  49. this.charsetNumber = (options.charset)
  50. ? ConnectionConfig.getCharsetNumber(options.charset)
  51. : options.charsetNumber||Charsets.UTF8_GENERAL_CI;
  52. // Set the client flags
  53. var defaultFlags = ConnectionConfig.getDefaultFlags(options);
  54. this.clientFlags = ConnectionConfig.mergeFlags(defaultFlags, options.flags)
  55. }
  56. ConnectionConfig.mergeFlags = function mergeFlags(defaultFlags, userFlags) {
  57. var allFlags = ConnectionConfig.parseFlagList(defaultFlags);
  58. var newFlags = ConnectionConfig.parseFlagList(userFlags);
  59. // Merge the new flags
  60. for (var flag in newFlags) {
  61. if (allFlags[flag] !== false) {
  62. allFlags[flag] = newFlags[flag];
  63. }
  64. }
  65. // Build flags
  66. var flags = 0x0;
  67. for (var flag in allFlags) {
  68. if (allFlags[flag]) {
  69. // TODO: Throw here on some future release
  70. flags |= ClientConstants['CLIENT_' + flag] || 0x0;
  71. }
  72. }
  73. return flags;
  74. };
  75. ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset) {
  76. var num = Charsets[charset.toUpperCase()];
  77. if (num === undefined) {
  78. throw new TypeError('Unknown charset \'' + charset + '\'');
  79. }
  80. return num;
  81. };
  82. ConnectionConfig.getDefaultFlags = function getDefaultFlags(options) {
  83. var defaultFlags = [
  84. '-COMPRESS', // Compression protocol *NOT* supported
  85. '-CONNECT_ATTRS', // Does *NOT* send connection attributes in Protocol::HandshakeResponse41
  86. '+CONNECT_WITH_DB', // One can specify db on connect in Handshake Response Packet
  87. '+FOUND_ROWS', // Send found rows instead of affected rows
  88. '+IGNORE_SIGPIPE', // Don't issue SIGPIPE if network failures
  89. '+IGNORE_SPACE', // Let the parser ignore spaces before '('
  90. '+LOCAL_FILES', // Can use LOAD DATA LOCAL
  91. '+LONG_FLAG', // Longer flags in Protocol::ColumnDefinition320
  92. '+LONG_PASSWORD', // Use the improved version of Old Password Authentication
  93. '+MULTI_RESULTS', // Can handle multiple resultsets for COM_QUERY
  94. '+ODBC', // Special handling of ODBC behaviour
  95. '-PLUGIN_AUTH', // Does *NOT* support auth plugins
  96. '+PROTOCOL_41', // Uses the 4.1 protocol
  97. '+PS_MULTI_RESULTS', // Can handle multiple resultsets for COM_STMT_EXECUTE
  98. '+RESERVED', // Unused
  99. '+SECURE_CONNECTION', // Supports Authentication::Native41
  100. '+TRANSACTIONS' // Expects status flags
  101. ];
  102. if (options && options.multipleStatements) {
  103. // May send multiple statements per COM_QUERY and COM_STMT_PREPARE
  104. defaultFlags.push('+MULTI_STATEMENTS');
  105. }
  106. return defaultFlags;
  107. };
  108. ConnectionConfig.getSSLProfile = function getSSLProfile(name) {
  109. if (!SSLProfiles) {
  110. SSLProfiles = require('./protocol/constants/ssl_profiles');
  111. }
  112. var ssl = SSLProfiles[name];
  113. if (ssl === undefined) {
  114. throw new TypeError('Unknown SSL profile \'' + name + '\'');
  115. }
  116. return ssl;
  117. };
  118. ConnectionConfig.parseFlagList = function parseFlagList(flagList) {
  119. var allFlags = Object.create(null);
  120. if (!flagList) {
  121. return allFlags;
  122. }
  123. var flags = !Array.isArray(flagList)
  124. ? String(flagList || '').toUpperCase().split(/\s*,+\s*/)
  125. : flagList;
  126. for (var i = 0; i < flags.length; i++) {
  127. var flag = flags[i];
  128. var offset = 1;
  129. var state = flag[0];
  130. if (state === undefined) {
  131. // TODO: throw here on some future release
  132. continue;
  133. }
  134. if (state !== '-' && state !== '+') {
  135. offset = 0;
  136. state = '+';
  137. }
  138. allFlags[flag.substr(offset)] = state === '+';
  139. }
  140. return allFlags;
  141. };
  142. ConnectionConfig.parseUrl = function(url) {
  143. url = urlParse(url, true);
  144. var options = {
  145. host : url.hostname,
  146. port : url.port,
  147. database : url.pathname.substr(1),
  148. };
  149. if (url.auth) {
  150. var auth = url.auth.split(':');
  151. options.user = auth[0];
  152. options.password = auth[1];
  153. }
  154. if (url.query) {
  155. for (var key in url.query) {
  156. var value = url.query[key];
  157. try {
  158. // Try to parse this as a JSON expression first
  159. options[key] = JSON.parse(value);
  160. } catch (err) {
  161. // Otherwise assume it is a plain string
  162. options[key] = value;
  163. }
  164. }
  165. }
  166. return options;
  167. };