mysql2 3.21.1 → 3.21.2-canary.49a64cca

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -928,6 +928,10 @@ class BaseConnection extends EventEmitter {
928
928
  return this.addCommand(new Commands.Ping(cb));
929
929
  }
930
930
 
931
+ reset(cb) {
932
+ return this.addCommand(new Commands.ResetConnection(cb));
933
+ }
934
+
931
935
  _registerSlave(opts, cb) {
932
936
  return this.addCommand(new Commands.RegisterSlave(opts, cb));
933
937
  }
package/lib/base/pool.js CHANGED
@@ -127,7 +127,37 @@ class BasePool extends EventEmitter {
127
127
  cb = this._connectionQueue.shift();
128
128
  process.nextTick(this.getConnection.bind(this, cb));
129
129
  }
130
- } else if (this._connectionQueue.length) {
130
+ return;
131
+ }
132
+
133
+ // Reset connection state if configured
134
+ if (this.config.resetOnRelease && connection.reset) {
135
+ connection.reset((err) => {
136
+ if (err) {
137
+ // If reset fails, remove connection from pool
138
+ connection._pool = null;
139
+ spliceConnection(this._allConnections, connection);
140
+ connection.destroy();
141
+
142
+ // Try to create a new connection for waiting callbacks
143
+ if (this._connectionQueue.length) {
144
+ cb = this._connectionQueue.shift();
145
+ process.nextTick(this.getConnection.bind(this, cb));
146
+ }
147
+ return;
148
+ }
149
+
150
+ // Reset successful, continue with normal release flow
151
+ this._handleSuccessfulRelease(connection);
152
+ });
153
+ } else {
154
+ this._handleSuccessfulRelease(connection);
155
+ }
156
+ }
157
+
158
+ _handleSuccessfulRelease(connection) {
159
+ let cb;
160
+ if (this._connectionQueue.length) {
131
161
  cb = this._connectionQueue.shift();
132
162
  process.nextTick(() => {
133
163
  connection._released = false;
@@ -72,6 +72,28 @@ function authSwitchRequest(packet, connection, command) {
72
72
  return;
73
73
  }
74
74
 
75
+ if (pluginName === 'mysql_clear_password') {
76
+ const hasCustomPlugin =
77
+ connection.config.authPlugins &&
78
+ Object.prototype.hasOwnProperty.call(
79
+ connection.config.authPlugins,
80
+ 'mysql_clear_password'
81
+ );
82
+ if (!hasCustomPlugin && !connection.config.enableCleartextPlugin) {
83
+ const err = new Error(
84
+ 'Server requested authentication using mysql_clear_password, ' +
85
+ 'which sends the password in plaintext over the network and is ' +
86
+ 'disabled by default. To enable it, set the `enableCleartextPlugin` ' +
87
+ 'option to `true` in your connection configuration, or provide a ' +
88
+ 'custom `mysql_clear_password` auth plugin via the `authPlugins` ' +
89
+ 'option. Only use this over a secure connection (TLS/SSL).'
90
+ );
91
+ err.code = 'MYSQL_CLEAR_PASSWORD_NOT_ENABLED';
92
+ err.fatal = true;
93
+ throw err;
94
+ }
95
+ }
96
+
75
97
  const authPlugin = getAuthPlugin(pluginName, connection);
76
98
  if (!authPlugin) {
77
99
  throw new Error(
@@ -105,7 +105,9 @@ class ClientHandshake extends Command {
105
105
  const canUseDirectAuth =
106
106
  !hasCustomAuthPlugin &&
107
107
  !hasLegacyAuthSwitchHandler &&
108
- this.canUseAuthMethodDirectly(serverAuthMethod, isSecureConnection);
108
+ this.canUseAuthMethodDirectly(serverAuthMethod, isSecureConnection) &&
109
+ (serverAuthMethod !== 'mysql_clear_password' ||
110
+ connection.config.enableCleartextPlugin);
109
111
 
110
112
  const clientAuthMethod = canUseDirectAuth
111
113
  ? serverAuthMethod
@@ -339,8 +341,9 @@ class ClientHandshake extends Command {
339
341
  }
340
342
  return ClientHandshake.prototype.handshakeResult;
341
343
  } catch (err) {
342
- // Authentication errors are fatal
343
- err.code = 'AUTH_SWITCH_PLUGIN_ERROR';
344
+ if (!err.code) {
345
+ err.code = 'AUTH_SWITCH_PLUGIN_ERROR';
346
+ }
344
347
  err.fatal = true;
345
348
 
346
349
  if (this.onResult) {
@@ -10,6 +10,7 @@ const Ping = require('./ping.js');
10
10
  const RegisterSlave = require('./register_slave.js');
11
11
  const BinlogDump = require('./binlog_dump.js');
12
12
  const ChangeUser = require('./change_user.js');
13
+ const ResetConnection = require('./reset_connection.js');
13
14
  const Quit = require('./quit.js');
14
15
 
15
16
  module.exports = {
@@ -23,5 +24,6 @@ module.exports = {
23
24
  RegisterSlave,
24
25
  BinlogDump,
25
26
  ChangeUser,
27
+ ResetConnection,
26
28
  Quit,
27
29
  };
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const Command = require('./command');
4
+ const Packets = require('../packets/index.js');
5
+
6
+ class ResetConnection extends Command {
7
+ constructor(callback) {
8
+ super();
9
+ this.onResult = callback;
10
+ }
11
+
12
+ start(packet, connection) {
13
+ const req = new Packets.ResetConnection();
14
+ connection.writePacket(req.toPacket());
15
+ return ResetConnection.prototype.resetConnectionResponse;
16
+ }
17
+
18
+ resetConnectionResponse(packet, connection) {
19
+ if (connection._statements) {
20
+ connection._statements.clear();
21
+ }
22
+ if (this.onResult) {
23
+ process.nextTick(this.onResult.bind(this, null));
24
+ }
25
+ return null;
26
+ }
27
+ }
28
+
29
+ module.exports = ResetConnection;
@@ -60,6 +60,7 @@ const validOptions = {
60
60
  uri: 1,
61
61
  user: 1,
62
62
  disableEval: 1,
63
+ enableCleartextPlugin: 1,
63
64
  // These options are used for Pool
64
65
  connectionLimit: 1,
65
66
  maxIdle: 1,
@@ -152,6 +153,7 @@ class ConnectionConfig {
152
153
  options.nestTables === undefined ? undefined : options.nestTables;
153
154
  this.typeCast = options.typeCast === undefined ? true : options.typeCast;
154
155
  this.disableEval = Boolean(options.disableEval);
156
+ this.enableCleartextPlugin = Boolean(options.enableCleartextPlugin);
155
157
  if (this.timezone[0] === ' ') {
156
158
  // "+" is a url encoded char for space so it
157
159
  // gets translated to space when giving a
@@ -32,5 +32,6 @@ module.exports = {
32
32
  STMT_FETCH: 0x1c,
33
33
  DAEMON: 0x1d, // deprecated
34
34
  BINLOG_DUMP_GTID: 0x1e,
35
+ RESET_CONNECTION: 0x1f, // introduced in 5.7.3
35
36
  UNKNOWN: 0xff, // bad!
36
37
  };
@@ -27,6 +27,7 @@ const PrepareStatement = require('./prepare_statement');
27
27
  const PreparedStatementHeader = require('./prepared_statement_header');
28
28
  const Query = require('./query');
29
29
  const RegisterSlave = require('./register_slave');
30
+ const ResetConnection = require('./reset_connection');
30
31
  const ResultSetHeader = require('./resultset_header');
31
32
  const SSLRequest = require('./ssl_request');
32
33
  const TextRow = require('./text_row');
@@ -48,6 +49,7 @@ const ctorMap = {
48
49
  PreparedStatementHeader,
49
50
  Query,
50
51
  RegisterSlave,
52
+ ResetConnection,
51
53
  ResultSetHeader,
52
54
  SSLRequest,
53
55
  TextRow,
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const Packet = require('../packets/packet');
4
+ const CommandCodes = require('../constants/commands');
5
+
6
+ class ResetConnection {
7
+ constructor() {}
8
+
9
+ toPacket() {
10
+ const packet = new Packet(0, Buffer.alloc(5), 0, 5);
11
+ packet.offset = 4;
12
+ packet.writeInt8(CommandCodes.RESET_CONNECTION);
13
+ return packet;
14
+ }
15
+ }
16
+
17
+ module.exports = ResetConnection;
@@ -24,6 +24,10 @@ class PoolConfig {
24
24
  this.queueLimit = isNaN(options.queueLimit)
25
25
  ? 0
26
26
  : Number(options.queueLimit);
27
+ this.resetOnRelease =
28
+ options.resetOnRelease === undefined
29
+ ? false
30
+ : Boolean(options.resetOnRelease);
27
31
  }
28
32
  }
29
33
 
@@ -118,6 +118,25 @@ class PromiseConnection extends EventEmitter {
118
118
  });
119
119
  }
120
120
 
121
+ reset() {
122
+ const c = this.connection;
123
+ const localErr = new Error();
124
+ return new this.Promise((resolve, reject) => {
125
+ c.reset((err) => {
126
+ if (err) {
127
+ localErr.message = err.message;
128
+ localErr.code = err.code;
129
+ localErr.errno = err.errno;
130
+ localErr.sqlState = err.sqlState;
131
+ localErr.sqlMessage = err.sqlMessage;
132
+ reject(localErr);
133
+ } else {
134
+ resolve();
135
+ }
136
+ });
137
+ });
138
+ }
139
+
121
140
  connect() {
122
141
  const c = this.connection;
123
142
  const localErr = new Error();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysql2",
3
- "version": "3.21.1",
3
+ "version": "3.21.2-canary.49a64cca",
4
4
  "description": "fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS",
5
5
  "main": "index.js",
6
6
  "typings": "typings/mysql/index",
package/promise.d.ts CHANGED
@@ -51,6 +51,8 @@ export declare class Connection extends QueryableAndExecutableBase {
51
51
 
52
52
  ping(): Promise<void>;
53
53
 
54
+ reset(): Promise<void>;
55
+
54
56
  beginTransaction(): Promise<void>;
55
57
 
56
58
  commit(): Promise<void>;
@@ -326,6 +326,18 @@ export interface ConnectionOptions {
326
326
 
327
327
  disableEval?: boolean;
328
328
 
329
+ /**
330
+ * Enable the `mysql_clear_password` authentication plugin, which sends the
331
+ * password in plaintext. Disabled by default for security — only enable
332
+ * over secure connections (TLS/SSL).
333
+ *
334
+ * Providing a custom `mysql_clear_password` function via `authPlugins`
335
+ * implicitly enables cleartext authentication without this flag.
336
+ *
337
+ * (Default: false)
338
+ */
339
+ enableCleartextPlugin?: boolean;
340
+
329
341
  authPlugins?: {
330
342
  [key: string]: AuthPlugin;
331
343
  };
@@ -426,6 +438,8 @@ declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
426
438
 
427
439
  ping(callback?: (err: QueryError | null) => any): void;
428
440
 
441
+ reset(callback?: (err: QueryError | null) => any): void;
442
+
429
443
  writeOk(args?: OkPacketParams): void;
430
444
 
431
445
  writeError(args?: ErrorPacketParams): void;
@@ -37,6 +37,13 @@ export interface PoolOptions extends ConnectionOptions {
37
37
  * is no limit to the number of queued connection requests. (Default: 0)
38
38
  */
39
39
  queueLimit?: number;
40
+
41
+ /**
42
+ * Whether to reset the connection state (user variables, temporary tables, transactions, etc.) when
43
+ * releasing the connection back to the pool. This ensures each connection starts clean for the next user.
44
+ * Requires MySQL 5.7.3+. (Default: false)
45
+ */
46
+ resetOnRelease?: boolean;
40
47
  }
41
48
 
42
49
  declare class Pool extends QueryableBase(ExecutableBase(EventEmitter)) {