connection.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. // This file was modified by Oracle on June 1, 2021.
  2. // The changes involve new logic to handle an additional ERR Packet sent by
  3. // the MySQL server when the connection is closed unexpectedly.
  4. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  5. // This file was modified by Oracle on June 17, 2021.
  6. // The changes involve logic to ensure the socket connection is closed when
  7. // there is a fatal error.
  8. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  9. // This file was modified by Oracle on September 21, 2021.
  10. // The changes involve passing additional authentication factor passwords
  11. // to the ChangeUser Command instance.
  12. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  13. 'use strict';
  14. const Net = require('net');
  15. const Tls = require('tls');
  16. const Timers = require('timers');
  17. const EventEmitter = require('events').EventEmitter;
  18. const Readable = require('stream').Readable;
  19. const Queue = require('denque');
  20. const SqlString = require('sqlstring');
  21. const LRU = require('lru-cache').default;
  22. const PacketParser = require('./packet_parser.js');
  23. const Packets = require('./packets/index.js');
  24. const Commands = require('./commands/index.js');
  25. const ConnectionConfig = require('./connection_config.js');
  26. const CharsetToEncoding = require('./constants/charset_encodings.js');
  27. let _connectionId = 0;
  28. let convertNamedPlaceholders = null;
  29. class Connection extends EventEmitter {
  30. constructor(opts) {
  31. super();
  32. this.config = opts.config;
  33. // TODO: fill defaults
  34. // if no params, connect to /var/lib/mysql/mysql.sock ( /tmp/mysql.sock on OSX )
  35. // if host is given, connect to host:3306
  36. // TODO: use `/usr/local/mysql/bin/mysql_config --socket` output? as default socketPath
  37. // if there is no host/port and no socketPath parameters?
  38. if (!opts.config.stream) {
  39. if (opts.config.socketPath) {
  40. this.stream = Net.connect(opts.config.socketPath);
  41. } else {
  42. this.stream = Net.connect(
  43. opts.config.port,
  44. opts.config.host
  45. );
  46. // Optionally enable keep-alive on the socket.
  47. if (this.config.enableKeepAlive) {
  48. this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay);
  49. }
  50. // Enable TCP_NODELAY flag. This is needed so that the network packets
  51. // are sent immediately to the server
  52. this.stream.setNoDelay(true);
  53. }
  54. // if stream is a function, treat it as "stream agent / factory"
  55. } else if (typeof opts.config.stream === 'function') {
  56. this.stream = opts.config.stream(opts);
  57. } else {
  58. this.stream = opts.config.stream;
  59. }
  60. this._internalId = _connectionId++;
  61. this._commands = new Queue();
  62. this._command = null;
  63. this._paused = false;
  64. this._paused_packets = new Queue();
  65. this._statements = new LRU({
  66. max: this.config.maxPreparedStatements,
  67. dispose: function(statement) {
  68. statement.close();
  69. }
  70. });
  71. this.serverCapabilityFlags = 0;
  72. this.authorized = false;
  73. this.sequenceId = 0;
  74. this.compressedSequenceId = 0;
  75. this.threadId = null;
  76. this._handshakePacket = null;
  77. this._fatalError = null;
  78. this._protocolError = null;
  79. this._outOfOrderPackets = [];
  80. this.clientEncoding = CharsetToEncoding[this.config.charsetNumber];
  81. this.stream.on('error', this._handleNetworkError.bind(this));
  82. // see https://gist.github.com/khoomeister/4985691#use-that-instead-of-bind
  83. this.packetParser = new PacketParser(p => {
  84. this.handlePacket(p);
  85. });
  86. this.stream.on('data', data => {
  87. if (this.connectTimeout) {
  88. Timers.clearTimeout(this.connectTimeout);
  89. this.connectTimeout = null;
  90. }
  91. this.packetParser.execute(data);
  92. });
  93. this.stream.on('end', () => {
  94. // emit the end event so that the pooled connection can close the connection
  95. this.emit('end');
  96. });
  97. this.stream.on('close', () => {
  98. // we need to set this flag everywhere where we want connection to close
  99. if (this._closing) {
  100. return;
  101. }
  102. if (!this._protocolError) {
  103. // no particular error message before disconnect
  104. this._protocolError = new Error(
  105. 'Connection lost: The server closed the connection.'
  106. );
  107. this._protocolError.fatal = true;
  108. this._protocolError.code = 'PROTOCOL_CONNECTION_LOST';
  109. }
  110. this._notifyError(this._protocolError);
  111. });
  112. let handshakeCommand;
  113. if (!this.config.isServer) {
  114. handshakeCommand = new Commands.ClientHandshake(this.config.clientFlags);
  115. handshakeCommand.on('end', () => {
  116. // this happens when handshake finishes early either because there was
  117. // some fatal error or the server sent an error packet instead of
  118. // an hello packet (for example, 'Too many connections' error)
  119. if (!handshakeCommand.handshake || this._fatalError || this._protocolError) {
  120. return;
  121. }
  122. this._handshakePacket = handshakeCommand.handshake;
  123. this.threadId = handshakeCommand.handshake.connectionId;
  124. this.emit('connect', handshakeCommand.handshake);
  125. });
  126. handshakeCommand.on('error', err => {
  127. this._closing = true;
  128. this._notifyError(err);
  129. });
  130. this.addCommand(handshakeCommand);
  131. }
  132. // in case there was no initial handshake but we need to read sting, assume it utf-8
  133. // most common example: "Too many connections" error ( packet is sent immediately on connection attempt, we don't know server encoding yet)
  134. // will be overwritten with actual encoding value as soon as server handshake packet is received
  135. this.serverEncoding = 'utf8';
  136. if (this.config.connectTimeout) {
  137. const timeoutHandler = this._handleTimeoutError.bind(this);
  138. this.connectTimeout = Timers.setTimeout(
  139. timeoutHandler,
  140. this.config.connectTimeout
  141. );
  142. }
  143. }
  144. promise(promiseImpl) {
  145. const PromiseConnection = require('../promise').PromiseConnection;
  146. return new PromiseConnection(this, promiseImpl);
  147. }
  148. _addCommandClosedState(cmd) {
  149. const err = new Error(
  150. "Can't add new command when connection is in closed state"
  151. );
  152. err.fatal = true;
  153. if (cmd.onResult) {
  154. cmd.onResult(err);
  155. } else {
  156. this.emit('error', err);
  157. }
  158. }
  159. _handleFatalError(err) {
  160. err.fatal = true;
  161. // stop receiving packets
  162. this.stream.removeAllListeners('data');
  163. this.addCommand = this._addCommandClosedState;
  164. this.write = () => {
  165. this.emit('error', new Error("Can't write in closed state"));
  166. };
  167. this._notifyError(err);
  168. this._fatalError = err;
  169. }
  170. _handleNetworkError(err) {
  171. if (this.connectTimeout) {
  172. Timers.clearTimeout(this.connectTimeout);
  173. this.connectTimeout = null;
  174. }
  175. // Do not throw an error when a connection ends with a RST,ACK packet
  176. if (err.code === 'ECONNRESET' && this._closing) {
  177. return;
  178. }
  179. this._handleFatalError(err);
  180. }
  181. _handleTimeoutError() {
  182. if (this.connectTimeout) {
  183. Timers.clearTimeout(this.connectTimeout);
  184. this.connectTimeout = null;
  185. }
  186. this.stream.destroy && this.stream.destroy();
  187. const err = new Error('connect ETIMEDOUT');
  188. err.errorno = 'ETIMEDOUT';
  189. err.code = 'ETIMEDOUT';
  190. err.syscall = 'connect';
  191. this._handleNetworkError(err);
  192. }
  193. // notify all commands in the queue and bubble error as connection "error"
  194. // called on stream error or unexpected termination
  195. _notifyError(err) {
  196. if (this.connectTimeout) {
  197. Timers.clearTimeout(this.connectTimeout);
  198. this.connectTimeout = null;
  199. }
  200. // prevent from emitting 'PROTOCOL_CONNECTION_LOST' after EPIPE or ECONNRESET
  201. if (this._fatalError) {
  202. return;
  203. }
  204. let command;
  205. // if there is no active command, notify connection
  206. // if there are commands and all of them have callbacks, pass error via callback
  207. let bubbleErrorToConnection = !this._command;
  208. if (this._command && this._command.onResult) {
  209. this._command.onResult(err);
  210. this._command = null;
  211. // connection handshake is special because we allow it to be implicit
  212. // if error happened during handshake, but there are others commands in queue
  213. // then bubble error to other commands and not to connection
  214. } else if (
  215. !(
  216. this._command &&
  217. this._command.constructor === Commands.ClientHandshake &&
  218. this._commands.length > 0
  219. )
  220. ) {
  221. bubbleErrorToConnection = true;
  222. }
  223. while ((command = this._commands.shift())) {
  224. if (command.onResult) {
  225. command.onResult(err);
  226. } else {
  227. bubbleErrorToConnection = true;
  228. }
  229. }
  230. // notify connection if some comands in the queue did not have callbacks
  231. // or if this is pool connection ( so it can be removed from pool )
  232. if (bubbleErrorToConnection || this._pool) {
  233. this.emit('error', err);
  234. }
  235. // close connection after emitting the event in case of a fatal error
  236. if (err.fatal) {
  237. this.close();
  238. }
  239. }
  240. write(buffer) {
  241. const result = this.stream.write(buffer, err => {
  242. if (err) {
  243. this._handleNetworkError(err);
  244. }
  245. });
  246. if (!result) {
  247. this.stream.emit('pause');
  248. }
  249. }
  250. // http://dev.mysql.com/doc/internals/en/sequence-id.html
  251. //
  252. // The sequence-id is incremented with each packet and may wrap around.
  253. // It starts at 0 and is reset to 0 when a new command
  254. // begins in the Command Phase.
  255. // http://dev.mysql.com/doc/internals/en/example-several-mysql-packets.html
  256. _resetSequenceId() {
  257. this.sequenceId = 0;
  258. this.compressedSequenceId = 0;
  259. }
  260. _bumpCompressedSequenceId(numPackets) {
  261. this.compressedSequenceId += numPackets;
  262. this.compressedSequenceId %= 256;
  263. }
  264. _bumpSequenceId(numPackets) {
  265. this.sequenceId += numPackets;
  266. this.sequenceId %= 256;
  267. }
  268. writePacket(packet) {
  269. const MAX_PACKET_LENGTH = 16777215;
  270. const length = packet.length();
  271. let chunk, offset, header;
  272. if (length < MAX_PACKET_LENGTH) {
  273. packet.writeHeader(this.sequenceId);
  274. if (this.config.debug) {
  275. // eslint-disable-next-line no-console
  276. console.log(
  277. `${this._internalId} ${this.connectionId} <== ${this._command._commandName}#${this._command.stateName()}(${[this.sequenceId, packet._name, packet.length()].join(',')})`
  278. );
  279. // eslint-disable-next-line no-console
  280. console.log(
  281. `${this._internalId} ${this.connectionId} <== ${packet.buffer.toString('hex')}`
  282. );
  283. }
  284. this._bumpSequenceId(1);
  285. this.write(packet.buffer);
  286. } else {
  287. if (this.config.debug) {
  288. // eslint-disable-next-line no-console
  289. console.log(
  290. `${this._internalId} ${this.connectionId} <== Writing large packet, raw content not written:`
  291. );
  292. // eslint-disable-next-line no-console
  293. console.log(
  294. `${this._internalId} ${this.connectionId} <== ${this._command._commandName}#${this._command.stateName()}(${[this.sequenceId, packet._name, packet.length()].join(',')})`
  295. );
  296. }
  297. for (offset = 4; offset < 4 + length; offset += MAX_PACKET_LENGTH) {
  298. chunk = packet.buffer.slice(offset, offset + MAX_PACKET_LENGTH);
  299. if (chunk.length === MAX_PACKET_LENGTH) {
  300. header = Buffer.from([0xff, 0xff, 0xff, this.sequenceId]);
  301. } else {
  302. header = Buffer.from([
  303. chunk.length & 0xff,
  304. (chunk.length >> 8) & 0xff,
  305. (chunk.length >> 16) & 0xff,
  306. this.sequenceId
  307. ]);
  308. }
  309. this._bumpSequenceId(1);
  310. this.write(header);
  311. this.write(chunk);
  312. }
  313. }
  314. }
  315. // 0.11+ environment
  316. startTLS(onSecure) {
  317. if (this.config.debug) {
  318. // eslint-disable-next-line no-console
  319. console.log('Upgrading connection to TLS');
  320. }
  321. const secureContext = Tls.createSecureContext({
  322. ca: this.config.ssl.ca,
  323. cert: this.config.ssl.cert,
  324. ciphers: this.config.ssl.ciphers,
  325. key: this.config.ssl.key,
  326. passphrase: this.config.ssl.passphrase,
  327. minVersion: this.config.ssl.minVersion,
  328. maxVersion: this.config.ssl.maxVersion
  329. });
  330. const rejectUnauthorized = this.config.ssl.rejectUnauthorized;
  331. const verifyIdentity = this.config.ssl.verifyIdentity;
  332. const host = this.config.host;
  333. let secureEstablished = false;
  334. const secureSocket = new Tls.TLSSocket(this.stream, {
  335. rejectUnauthorized: rejectUnauthorized,
  336. requestCert: true,
  337. secureContext: secureContext,
  338. isServer: false
  339. });
  340. if (typeof host === 'string') {
  341. secureSocket.setServername(host);
  342. }
  343. // error handler for secure socket
  344. secureSocket.on('_tlsError', err => {
  345. if (secureEstablished) {
  346. this._handleNetworkError(err);
  347. } else {
  348. onSecure(err);
  349. }
  350. });
  351. secureSocket.on('secure', () => {
  352. secureEstablished = true;
  353. let callbackValue = null;
  354. if (rejectUnauthorized) {
  355. callbackValue = secureSocket.ssl.verifyError()
  356. if (!callbackValue && typeof host === 'string' && verifyIdentity) {
  357. const cert = secureSocket.ssl.getPeerCertificate(true);
  358. callbackValue = Tls.checkServerIdentity(host, cert)
  359. }
  360. }
  361. onSecure(callbackValue);
  362. });
  363. secureSocket.on('data', data => {
  364. this.packetParser.execute(data);
  365. });
  366. this.write = buffer => {
  367. secureSocket.write(buffer);
  368. };
  369. // start TLS communications
  370. secureSocket._start();
  371. }
  372. pipe() {
  373. if (this.stream instanceof Net.Stream) {
  374. this.stream.ondata = (data, start, end) => {
  375. this.packetParser.execute(data, start, end);
  376. };
  377. } else {
  378. this.stream.on('data', data => {
  379. this.packetParser.execute(
  380. data.parent,
  381. data.offset,
  382. data.offset + data.length
  383. );
  384. });
  385. }
  386. }
  387. protocolError(message, code) {
  388. // Starting with MySQL 8.0.24, if the client closes the connection
  389. // unexpectedly, the server will send a last ERR Packet, which we can
  390. // safely ignore.
  391. // https://dev.mysql.com/worklog/task/?id=12999
  392. if (this._closing) {
  393. return;
  394. }
  395. const err = new Error(message);
  396. err.fatal = true;
  397. err.code = code || 'PROTOCOL_ERROR';
  398. this.emit('error', err);
  399. }
  400. get fatalError() {
  401. return this._fatalError;
  402. }
  403. handlePacket(packet) {
  404. if (this._paused) {
  405. this._paused_packets.push(packet);
  406. return;
  407. }
  408. if (this.config.debug) {
  409. if (packet) {
  410. // eslint-disable-next-line no-console
  411. console.log(
  412. ` raw: ${packet.buffer
  413. .slice(packet.offset, packet.offset + packet.length())
  414. .toString('hex')}`
  415. );
  416. // eslint-disable-next-line no-console
  417. console.trace();
  418. const commandName = this._command
  419. ? this._command._commandName
  420. : '(no command)';
  421. const stateName = this._command
  422. ? this._command.stateName()
  423. : '(no command)';
  424. // eslint-disable-next-line no-console
  425. console.log(
  426. `${this._internalId} ${this.connectionId} ==> ${commandName}#${stateName}(${[packet.sequenceId, packet.type(), packet.length()].join(',')})`
  427. );
  428. }
  429. }
  430. if (!this._command) {
  431. const marker = packet.peekByte();
  432. // If it's an Err Packet, we should use it.
  433. if (marker === 0xff) {
  434. const error = Packets.Error.fromPacket(packet);
  435. this.protocolError(error.message, error.code);
  436. } else {
  437. // Otherwise, it means it's some other unexpected packet.
  438. this.protocolError(
  439. 'Unexpected packet while no commands in the queue',
  440. 'PROTOCOL_UNEXPECTED_PACKET'
  441. );
  442. }
  443. this.close();
  444. return;
  445. }
  446. if (packet) {
  447. // Note: when server closes connection due to inactivity, Err packet ER_CLIENT_INTERACTION_TIMEOUT from MySQL 8.0.24, sequenceId will be 0
  448. if (this.sequenceId !== packet.sequenceId) {
  449. const err = new Error(
  450. `Warning: got packets out of order. Expected ${this.sequenceId} but received ${packet.sequenceId}`
  451. );
  452. err.expected = this.sequenceId;
  453. err.received = packet.sequenceId;
  454. this.emit('warn', err); // REVIEW
  455. // eslint-disable-next-line no-console
  456. console.error(err.message);
  457. }
  458. this._bumpSequenceId(packet.numPackets);
  459. }
  460. const done = this._command.execute(packet, this);
  461. if (done) {
  462. this._command = this._commands.shift();
  463. if (this._command) {
  464. this.sequenceId = 0;
  465. this.compressedSequenceId = 0;
  466. this.handlePacket();
  467. }
  468. }
  469. }
  470. addCommand(cmd) {
  471. // this.compressedSequenceId = 0;
  472. // this.sequenceId = 0;
  473. if (this.config.debug) {
  474. const commandName = cmd.constructor.name;
  475. // eslint-disable-next-line no-console
  476. console.log(`Add command: ${commandName}`);
  477. cmd._commandName = commandName;
  478. }
  479. if (!this._command) {
  480. this._command = cmd;
  481. this.handlePacket();
  482. } else {
  483. this._commands.push(cmd);
  484. }
  485. return cmd;
  486. }
  487. format(sql, values) {
  488. if (typeof this.config.queryFormat === 'function') {
  489. return this.config.queryFormat.call(
  490. this,
  491. sql,
  492. values,
  493. this.config.timezone
  494. );
  495. }
  496. const opts = {
  497. sql: sql,
  498. values: values
  499. };
  500. this._resolveNamedPlaceholders(opts);
  501. return SqlString.format(
  502. opts.sql,
  503. opts.values,
  504. this.config.stringifyObjects,
  505. this.config.timezone
  506. );
  507. }
  508. escape(value) {
  509. return SqlString.escape(value, false, this.config.timezone);
  510. }
  511. escapeId(value) {
  512. return SqlString.escapeId(value, false);
  513. }
  514. raw(sql) {
  515. return SqlString.raw(sql);
  516. }
  517. _resolveNamedPlaceholders(options) {
  518. let unnamed;
  519. if (this.config.namedPlaceholders || options.namedPlaceholders) {
  520. if (Array.isArray(options.values)) {
  521. // if an array is provided as the values, assume the conversion is not necessary.
  522. // this allows the usage of unnamed placeholders even if the namedPlaceholders flag is enabled.
  523. return
  524. }
  525. if (convertNamedPlaceholders === null) {
  526. convertNamedPlaceholders = require('named-placeholders')();
  527. }
  528. unnamed = convertNamedPlaceholders(options.sql, options.values);
  529. options.sql = unnamed[0];
  530. options.values = unnamed[1];
  531. }
  532. }
  533. query(sql, values, cb) {
  534. let cmdQuery;
  535. if (sql.constructor === Commands.Query) {
  536. cmdQuery = sql;
  537. } else {
  538. cmdQuery = Connection.createQuery(sql, values, cb, this.config);
  539. }
  540. this._resolveNamedPlaceholders(cmdQuery);
  541. const rawSql = this.format(cmdQuery.sql, cmdQuery.values !== undefined ? cmdQuery.values : []);
  542. cmdQuery.sql = rawSql;
  543. return this.addCommand(cmdQuery);
  544. }
  545. pause() {
  546. this._paused = true;
  547. this.stream.pause();
  548. }
  549. resume() {
  550. let packet;
  551. this._paused = false;
  552. while ((packet = this._paused_packets.shift())) {
  553. this.handlePacket(packet);
  554. // don't resume if packet handler paused connection
  555. if (this._paused) {
  556. return;
  557. }
  558. }
  559. this.stream.resume();
  560. }
  561. // TODO: named placeholders support
  562. prepare(options, cb) {
  563. if (typeof options === 'string') {
  564. options = { sql: options };
  565. }
  566. return this.addCommand(new Commands.Prepare(options, cb));
  567. }
  568. unprepare(sql) {
  569. let options = {};
  570. if (typeof sql === 'object') {
  571. options = sql;
  572. } else {
  573. options.sql = sql;
  574. }
  575. const key = Connection.statementKey(options);
  576. const stmt = this._statements.get(key);
  577. if (stmt) {
  578. this._statements.delete(key);
  579. stmt.close();
  580. }
  581. return stmt;
  582. }
  583. execute(sql, values, cb) {
  584. let options = {};
  585. if (typeof sql === 'object') {
  586. // execute(options, cb)
  587. options = sql;
  588. if (typeof values === 'function') {
  589. cb = values;
  590. } else {
  591. options.values = options.values || values;
  592. }
  593. } else if (typeof values === 'function') {
  594. // execute(sql, cb)
  595. cb = values;
  596. options.sql = sql;
  597. options.values = undefined;
  598. } else {
  599. // execute(sql, values, cb)
  600. options.sql = sql;
  601. options.values = values;
  602. }
  603. this._resolveNamedPlaceholders(options);
  604. // check for values containing undefined
  605. if (options.values) {
  606. //If namedPlaceholder is not enabled and object is passed as bind parameters
  607. if (!Array.isArray(options.values)) {
  608. throw new TypeError(
  609. 'Bind parameters must be array if namedPlaceholders parameter is not enabled'
  610. );
  611. }
  612. options.values.forEach(val => {
  613. //If namedPlaceholder is not enabled and object is passed as bind parameters
  614. if (!Array.isArray(options.values)) {
  615. throw new TypeError(
  616. 'Bind parameters must be array if namedPlaceholders parameter is not enabled'
  617. );
  618. }
  619. if (val === undefined) {
  620. throw new TypeError(
  621. 'Bind parameters must not contain undefined. To pass SQL NULL specify JS null'
  622. );
  623. }
  624. if (typeof val === 'function') {
  625. throw new TypeError(
  626. 'Bind parameters must not contain function(s). To pass the body of a function as a string call .toString() first'
  627. );
  628. }
  629. });
  630. }
  631. const executeCommand = new Commands.Execute(options, cb);
  632. const prepareCommand = new Commands.Prepare(options, (err, stmt) => {
  633. if (err) {
  634. // skip execute command if prepare failed, we have main
  635. // combined callback here
  636. executeCommand.start = function() {
  637. return null;
  638. };
  639. if (cb) {
  640. cb(err);
  641. } else {
  642. executeCommand.emit('error', err);
  643. }
  644. executeCommand.emit('end');
  645. return;
  646. }
  647. executeCommand.statement = stmt;
  648. });
  649. this.addCommand(prepareCommand);
  650. this.addCommand(executeCommand);
  651. return executeCommand;
  652. }
  653. changeUser(options, callback) {
  654. if (!callback && typeof options === 'function') {
  655. callback = options;
  656. options = {};
  657. }
  658. const charsetNumber = options.charset
  659. ? ConnectionConfig.getCharsetNumber(options.charset)
  660. : this.config.charsetNumber;
  661. return this.addCommand(
  662. new Commands.ChangeUser(
  663. {
  664. user: options.user || this.config.user,
  665. // for the purpose of multi-factor authentication, or not, the main
  666. // password (used for the 1st authentication factor) can also be
  667. // provided via the "password1" option
  668. password: options.password || options.password1 || this.config.password || this.config.password1,
  669. password2: options.password2 || this.config.password2,
  670. password3: options.password3 || this.config.password3,
  671. passwordSha1: options.passwordSha1 || this.config.passwordSha1,
  672. database: options.database || this.config.database,
  673. timeout: options.timeout,
  674. charsetNumber: charsetNumber,
  675. currentConfig: this.config
  676. },
  677. err => {
  678. if (err) {
  679. err.fatal = true;
  680. }
  681. if (callback) {
  682. callback(err);
  683. }
  684. }
  685. )
  686. );
  687. }
  688. // transaction helpers
  689. beginTransaction(cb) {
  690. return this.query('START TRANSACTION', cb);
  691. }
  692. commit(cb) {
  693. return this.query('COMMIT', cb);
  694. }
  695. rollback(cb) {
  696. return this.query('ROLLBACK', cb);
  697. }
  698. ping(cb) {
  699. return this.addCommand(new Commands.Ping(cb));
  700. }
  701. _registerSlave(opts, cb) {
  702. return this.addCommand(new Commands.RegisterSlave(opts, cb));
  703. }
  704. _binlogDump(opts, cb) {
  705. return this.addCommand(new Commands.BinlogDump(opts, cb));
  706. }
  707. // currently just alias to close
  708. destroy() {
  709. this.close();
  710. }
  711. close() {
  712. if (this.connectTimeout) {
  713. Timers.clearTimeout(this.connectTimeout);
  714. this.connectTimeout = null;
  715. }
  716. this._closing = true;
  717. this.stream.end();
  718. this.addCommand = this._addCommandClosedState;
  719. }
  720. createBinlogStream(opts) {
  721. // TODO: create proper stream class
  722. // TODO: use through2
  723. let test = 1;
  724. const stream = new Readable({ objectMode: true });
  725. stream._read = function() {
  726. return {
  727. data: test++
  728. };
  729. };
  730. this._registerSlave(opts, () => {
  731. const dumpCmd = this._binlogDump(opts);
  732. dumpCmd.on('event', ev => {
  733. stream.push(ev);
  734. });
  735. dumpCmd.on('eof', () => {
  736. stream.push(null);
  737. // if non-blocking, then close stream to prevent errors
  738. if (opts.flags && opts.flags & 0x01) {
  739. this.close();
  740. }
  741. });
  742. // TODO: pipe errors as well
  743. });
  744. return stream;
  745. }
  746. connect(cb) {
  747. if (!cb) {
  748. return;
  749. }
  750. if (this._fatalError || this._protocolError) {
  751. return cb(this._fatalError || this._protocolError);
  752. }
  753. if (this._handshakePacket) {
  754. return cb(null, this);
  755. }
  756. let connectCalled = 0;
  757. function callbackOnce(isErrorHandler) {
  758. return function(param) {
  759. if (!connectCalled) {
  760. if (isErrorHandler) {
  761. cb(param);
  762. } else {
  763. cb(null, param);
  764. }
  765. }
  766. connectCalled = 1;
  767. };
  768. }
  769. this.once('error', callbackOnce(true));
  770. this.once('connect', callbackOnce(false));
  771. }
  772. // ===================================
  773. // outgoing server connection methods
  774. // ===================================
  775. writeColumns(columns) {
  776. this.writePacket(Packets.ResultSetHeader.toPacket(columns.length));
  777. columns.forEach(column => {
  778. this.writePacket(
  779. Packets.ColumnDefinition.toPacket(column, this.serverConfig.encoding)
  780. );
  781. });
  782. this.writeEof();
  783. }
  784. // row is array of columns, not hash
  785. writeTextRow(column) {
  786. this.writePacket(
  787. Packets.TextRow.toPacket(column, this.serverConfig.encoding)
  788. );
  789. }
  790. writeBinaryRow(column) {
  791. this.writePacket(
  792. Packets.BinaryRow.toPacket(column, this.serverConfig.encoding)
  793. );
  794. }
  795. writeTextResult(rows, columns, binary=false) {
  796. this.writeColumns(columns);
  797. rows.forEach(row => {
  798. const arrayRow = new Array(columns.length);
  799. columns.forEach(column => {
  800. arrayRow.push(row[column.name]);
  801. });
  802. if(binary) {
  803. this.writeBinaryRow(arrayRow);
  804. }
  805. else this.writeTextRow(arrayRow);
  806. });
  807. this.writeEof();
  808. }
  809. writeEof(warnings, statusFlags) {
  810. this.writePacket(Packets.EOF.toPacket(warnings, statusFlags));
  811. }
  812. writeOk(args) {
  813. if (!args) {
  814. args = { affectedRows: 0 };
  815. }
  816. this.writePacket(Packets.OK.toPacket(args, this.serverConfig.encoding));
  817. }
  818. writeError(args) {
  819. // if we want to send error before initial hello was sent, use default encoding
  820. const encoding = this.serverConfig ? this.serverConfig.encoding : 'cesu8';
  821. this.writePacket(Packets.Error.toPacket(args, encoding));
  822. }
  823. serverHandshake(args) {
  824. this.serverConfig = args;
  825. this.serverConfig.encoding =
  826. CharsetToEncoding[this.serverConfig.characterSet];
  827. return this.addCommand(new Commands.ServerHandshake(args));
  828. }
  829. // ===============================================================
  830. end(callback) {
  831. if (this.config.isServer) {
  832. this._closing = true;
  833. const quitCmd = new EventEmitter();
  834. setImmediate(() => {
  835. this.stream.end();
  836. quitCmd.emit('end');
  837. });
  838. return quitCmd;
  839. }
  840. // trigger error if more commands enqueued after end command
  841. const quitCmd = this.addCommand(new Commands.Quit(callback));
  842. this.addCommand = this._addCommandClosedState;
  843. return quitCmd;
  844. }
  845. static createQuery(sql, values, cb, config) {
  846. let options = {
  847. rowsAsArray: config.rowsAsArray
  848. };
  849. if (typeof sql === 'object') {
  850. // query(options, cb)
  851. options = sql;
  852. if (typeof values === 'function') {
  853. cb = values;
  854. } else if (values !== undefined) {
  855. options.values = values;
  856. }
  857. } else if (typeof values === 'function') {
  858. // query(sql, cb)
  859. cb = values;
  860. options.sql = sql;
  861. options.values = undefined;
  862. } else {
  863. // query(sql, values, cb)
  864. options.sql = sql;
  865. options.values = values;
  866. }
  867. return new Commands.Query(options, cb);
  868. }
  869. static statementKey(options) {
  870. return (
  871. `${typeof options.nestTables}/${options.nestTables}/${options.rowsAsArray}${options.sql}`
  872. );
  873. }
  874. }
  875. if (Tls.TLSSocket) {
  876. // not supported
  877. } else {
  878. Connection.prototype.startTLS = function _startTLS(onSecure) {
  879. if (this.config.debug) {
  880. // eslint-disable-next-line no-console
  881. console.log('Upgrading connection to TLS');
  882. }
  883. const crypto = require('crypto');
  884. const config = this.config;
  885. const stream = this.stream;
  886. const rejectUnauthorized = this.config.ssl.rejectUnauthorized;
  887. const credentials = crypto.createCredentials({
  888. key: config.ssl.key,
  889. cert: config.ssl.cert,
  890. passphrase: config.ssl.passphrase,
  891. ca: config.ssl.ca,
  892. ciphers: config.ssl.ciphers
  893. });
  894. const securePair = Tls.createSecurePair(
  895. credentials,
  896. false,
  897. true,
  898. rejectUnauthorized
  899. );
  900. if (stream.ondata) {
  901. stream.ondata = null;
  902. }
  903. stream.removeAllListeners('data');
  904. stream.pipe(securePair.encrypted);
  905. securePair.encrypted.pipe(stream);
  906. securePair.cleartext.on('data', data => {
  907. this.packetParser.execute(data);
  908. });
  909. this.write = function(buffer) {
  910. securePair.cleartext.write(buffer);
  911. };
  912. securePair.on('secure', () => {
  913. onSecure(rejectUnauthorized ? securePair.ssl.verifyError() : null);
  914. });
  915. };
  916. }
  917. module.exports = Connection;