Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[misc] code cleaning
  • Loading branch information
rusher committed Jan 29, 2019
1 parent 59fafd1 commit b85206e
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 154 deletions.
2 changes: 0 additions & 2 deletions lib/cmd/batch-bulk.js
Expand Up @@ -4,7 +4,6 @@ const CommonBinary = require("./common-binary-cmd");
const Errors = require("../misc/errors");
const Parse = require("../misc/parse");
const BulkPacket = require("../io/bulk-packet");
const QUOTE = 0x27;

/**
* Protocol COM_STMT_BULK_EXECUTE
Expand Down Expand Up @@ -228,7 +227,6 @@ class BatchBulk extends CommonBinary {
this.reject = null;
this.onPacketReceive = null;
this.emit("end", this.firstError);
return;
} else {
this._responseIndex++;
this.onPacketReceive = this.readResponsePacket;
Expand Down
2 changes: 0 additions & 2 deletions lib/cmd/batch-rewrite.js
Expand Up @@ -177,7 +177,6 @@ class BatchRewrite extends CommonText {
process.nextTick(this.reject, this.firstError);
this.reject = null;
this.emit("end", this.firstError);
return;
}
} else {
if (!this.sending && this.packet.waitingResponseNo === 0) {
Expand Down Expand Up @@ -237,7 +236,6 @@ class BatchRewrite extends CommonText {
process.nextTick(this.reject, this.firstError);
this.reject = null;
this.emit("end", this.firstError);
return;
} else {
this._responseIndex++;
this.onPacketReceive = this.readResponsePacket;
Expand Down
20 changes: 10 additions & 10 deletions lib/cmd/common-binary-cmd.js
Expand Up @@ -83,7 +83,7 @@ class CommonBinary extends ResultSet {
switch (value.type) {
case "Point":
geoBuff = Buffer.allocUnsafe(21);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(1, 1); //wkbPoint
if (
value.coordinates &&
Expand All @@ -103,7 +103,7 @@ class CommonBinary extends ResultSet {
if (value.coordinates && Array.isArray(value.coordinates)) {
const pointNumber = value.coordinates.length;
geoBuff = Buffer.allocUnsafe(9 + 16 * pointNumber);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(2, 1); //wkbLineString
geoBuff.writeInt32LE(pointNumber, 5);
for (let i = 0; i < pointNumber; i++) {
Expand Down Expand Up @@ -133,7 +133,7 @@ class CommonBinary extends ResultSet {
size += 4 + 16 * value.coordinates[i].length;
}
geoBuff = Buffer.allocUnsafe(9 + size);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(3, 1); //wkbPolygon
geoBuff.writeInt32LE(numRings, 5);
pos = 9;
Expand Down Expand Up @@ -167,27 +167,27 @@ class CommonBinary extends ResultSet {
case "MultiPoint":
type = "MultiPoint";
geoBuff = Buffer.allocUnsafe(9);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(4, 1); //wkbMultiPoint
break;

case "MultiLineString":
type = "MultiLineString";
geoBuff = Buffer.allocUnsafe(9);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(5, 1); //wkbMultiLineString
break;

case "MultiPolygon":
type = "MultiPolygon";
geoBuff = Buffer.allocUnsafe(9);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(6, 1); //wkbMultiPolygon
break;

case "GeometryCollection":
geoBuff = Buffer.allocUnsafe(9);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(7, 1); //wkbGeometryCollection

if (value.geometries && Array.isArray(value.geometries)) {
Expand Down Expand Up @@ -232,7 +232,7 @@ class CommonBinary extends ResultSet {
!isNaN(value[1])
) {
geoBuff = Buffer.allocUnsafe(21);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(1, 1); //wkbPoint
geoBuff.writeDoubleLE(value[0], 5); //X
geoBuff.writeDoubleLE(value[1], 13); //Y
Expand All @@ -244,7 +244,7 @@ class CommonBinary extends ResultSet {
if (value && Array.isArray(value)) {
const pointNumber = value.length;
geoBuff = Buffer.allocUnsafe(9 + 16 * pointNumber);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(2, 1); //wkbLineString
geoBuff.writeInt32LE(pointNumber, 5);
for (let i = 0; i < pointNumber; i++) {
Expand Down Expand Up @@ -273,7 +273,7 @@ class CommonBinary extends ResultSet {
size += 4 + 16 * value[i].length;
}
geoBuff = Buffer.allocUnsafe(9 + size);
geoBuff.writeInt8(0x01); //LITTLE ENDIAN
geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
geoBuff.writeInt32LE(3, 1); //wkbPolygon
geoBuff.writeInt32LE(numRings, 5);
pos = 9;
Expand Down
12 changes: 5 additions & 7 deletions lib/cmd/common-text-cmd.js
Expand Up @@ -154,7 +154,7 @@ class CommonText extends ResultSet {
let st = "";
for (let i = 0; i < geo.length; i++) {
//GeoJSON format.
st += i != 0 ? "," : "";
st += i !== 0 ? "," : "";
switch (geo[i].type) {
case "Point":
st += "POINT(" + this.geoPointToString(geo[i].coordinates) + ")";
Expand Down Expand Up @@ -188,7 +188,7 @@ class CommonText extends ResultSet {
if (!coords) return "";
let st = "";
for (let i = 0; i < coords.length; i++) {
st += (i != 0 ? ",(" : "(") + this.geoMultiArrayPointToString(coords[i]) + ")";
st += (i !== 0 ? ",(" : "(") + this.geoMultiArrayPointToString(coords[i]) + ")";
}
return st;
}
Expand All @@ -197,7 +197,7 @@ class CommonText extends ResultSet {
if (!coords) return "";
let st = "";
for (let i = 0; i < coords.length; i++) {
st += (i != 0 ? ",(" : "(") + this.geoArrayPointToString(coords[i]) + ")";
st += (i !== 0 ? ",(" : "(") + this.geoArrayPointToString(coords[i]) + ")";
}
return st;
}
Expand All @@ -206,7 +206,7 @@ class CommonText extends ResultSet {
if (!coords) return "";
let st = "";
for (let i = 0; i < coords.length; i++) {
st += (i != 0 ? "," : "") + this.geoPointToString(coords[i]);
st += (i !== 0 ? "," : "") + this.geoPointToString(coords[i]);
}
return st;
}
Expand Down Expand Up @@ -281,9 +281,7 @@ class CommonText extends ResultSet {
this.castTextWrapper(column, opts, connOpts, packet);
return opts.typeCast(
column,
function() {
return this.readRowData(index, column, opts, connOpts, packet);
}.bind(this)
this.readRowData.bind(this, index, column, opts, connOpts, packet)
);
}

Expand Down
1 change: 0 additions & 1 deletion lib/cmd/handshake/auth/clear_password_auth.js
Expand Up @@ -7,7 +7,6 @@ const Command = require("../../command");
class ClearPasswordAuth extends Command {
constructor(packSeq, pluginData, resolve, reject) {
super(resolve, reject);
this.pluginData = pluginData;
this.sequenceNo = packSeq;
}

Expand Down
1 change: 0 additions & 1 deletion lib/cmd/handshake/auth/pam_password_auth.js
@@ -1,5 +1,4 @@
const Command = require("../../command");
const Utils = require("../../../misc/utils");

/**
* Use PAM authentication
Expand Down
80 changes: 40 additions & 40 deletions lib/cmd/handshake/handshake.js
Expand Up @@ -22,7 +22,7 @@ class Handshake extends Command {
}

parseHandshakeInit(packet, out, opts, info) {
if (packet.peek() == 0xff) {
if (packet.peek() === 0xff) {
//in case that some host is not permit to connect server
const authErr = packet.readError(info);
authErr.fatal = true;
Expand Down Expand Up @@ -143,9 +143,7 @@ class Handshake extends Command {
pluginData = packet.readBufferRemaining();
}

const authSwitchHandler = opts.authSwitchHandler || this.defaultAuthSwitchHandler();
authSwitchHandler.call(
this,
const plugin = Handshake.pluginHandler(
pluginName,
this.sequenceNo,
pluginData,
Expand All @@ -155,44 +153,46 @@ class Handshake extends Command {
this.resolve,
this.reject
);
}

defaultAuthSwitchHandler() {
return (pluginName, packSeq, pluginData, info, opts, out, authResolv, authReject) => {
let pluginAuth;
switch (pluginName) {
case "mysql_native_password":
pluginAuth = require("./auth/native_password_auth.js");
break;

case "mysql_clear_password":
pluginAuth = require("./auth/clear_password_auth.js");
break;

case "dialog":
pluginAuth = require("./auth/pam_password_auth.js");
break;

//TODO "auth_gssapi_client"
//TODO "client_ed25519"

default:
authReject(
Errors.createError(
"Client does not support authentication protocol '" +
pluginName +
"' requested by server. ",
true,
info,
"08004",
Errors.ER_AUTHENTICATION_PLUGIN_NOT_SUPPORTED
)
);
return;
}
const plugin = new pluginAuth(packSeq, pluginData, authResolv, authReject);
if (!plugin) {
this.reject(
Errors.createError(
"Client does not support authentication protocol '" +
pluginName +
"' requested by server. ",
true,
info,
"08004",
Errors.ER_AUTHENTICATION_PLUGIN_NOT_SUPPORTED
)
);
} else {
this._addCommand(plugin, false);
};
}
}

static pluginHandler(pluginName, packSeq, pluginData, info, opts, out, authResolve, authReject) {
let pluginAuth;
switch (pluginName) {
case "mysql_native_password":
pluginAuth = require("./auth/native_password_auth.js");
break;

case "mysql_clear_password":
pluginAuth = require("./auth/clear_password_auth.js");
break;

case "dialog":
pluginAuth = require("./auth/pam_password_auth.js");
break;

//TODO "auth_gssapi_client"
//TODO "client_ed25519"

default:
return null;
}
return new pluginAuth(packSeq, pluginData, authResolve, authReject);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cmd/ping.js
Expand Up @@ -32,7 +32,7 @@ class Ping extends Command {
* @param info connection info
*/
readPingResponsePacket(packet, out, opts, info) {
if (packet.peek() != 0x00) {
if (packet.peek() !== 0x00) {
return this.throwNewError(
"unexpected packet",
false,
Expand Down
2 changes: 1 addition & 1 deletion lib/cmd/reset.js
Expand Up @@ -32,7 +32,7 @@ class Reset extends Command {
* @param info connection info
*/
readResetResponsePacket(packet, out, opts, info) {
if (packet.peek() != 0x00) {
if (packet.peek() !== 0x00) {
return this.throwNewError(
"unexpected packet",
false,
Expand Down
16 changes: 12 additions & 4 deletions lib/cmd/resultset.js
Expand Up @@ -221,6 +221,14 @@ class ResultSet extends Command {
info.status |= ServerStatus.STATUS_IN_TRANS;
this.onPacketReceive = this.readResponsePacket;
return this.throwError(err, info);

//*********************************************************************************************************
//* Unexpected response
//*********************************************************************************************************
default:
info.status |= ServerStatus.STATUS_IN_TRANS;
this.onPacketReceive = this.readResponsePacket;
return this.throwError(Errors.ER_UNEXPECTED_PACKET, info);
}
}

Expand Down Expand Up @@ -312,7 +320,7 @@ class ResultSet extends Command {
* @returns {*}
*/
readIntermediateEOF(packet, out, opts, info) {
if (packet.peek() != 0xfe) {
if (packet.peek() !== 0xfe) {
return this.throwNewError(
"Error in protocol, expected EOF packet",
true,
Expand Down Expand Up @@ -430,7 +438,7 @@ class ResultSet extends Command {
}
sqlMsg += "'" + key + "':";
let param = values[key];
sqlMsg = this.logParam(sqlMsg, param);
sqlMsg = ResultSet.logParam(sqlMsg, param);
if (sqlMsg.length > this.opts.debugLen) {
sqlMsg = sqlMsg.substr(0, this.opts.debugLen) + "...";
break;
Expand All @@ -442,7 +450,7 @@ class ResultSet extends Command {
for (let i = 0; i < values.length; i++) {
if (i !== 0) sqlMsg += ",";
let param = values[i];
sqlMsg = this.logParam(sqlMsg, param);
sqlMsg = ResultSet.logParam(sqlMsg, param);
if (sqlMsg.length > this.opts.debugLen) {
sqlMsg = sqlMsg.substr(0, this.opts.debugLen) + "...";
break;
Expand Down Expand Up @@ -491,7 +499,7 @@ class ResultSet extends Command {
this.onPacketReceive = this.readResponsePacket;
}

logParam(sqlMsg, param) {
static logParam(sqlMsg, param) {
if (!param) {
sqlMsg += param === undefined ? "undefined" : "null";
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/config/connection-options.js
Expand Up @@ -80,7 +80,7 @@ class ConnectionOptions {
if (this.timezone === "Z") {
this.timezoneMillisOffset = 0;
} else {
const matched = this.timezone.match(/([\+\-\s])(\d\d):?(\d\d)?/);
const matched = this.timezone.match(/([+\-\s])(\d\d):?(\d\d)?/);
if (!matched) {
throw new RangeError(
"timezone format error. must be 'local'/'Z' or ±HH:MM. was '" + this.timezone + "'"
Expand All @@ -93,7 +93,7 @@ class ConnectionOptions {
}
this.trace = opts.trace || false;
this.typeCast = opts.typeCast;
if (this.typeCast != undefined && typeof this.typeCast !== "function") {
if (this.typeCast !== undefined && typeof this.typeCast !== "function") {
this.typeCast = undefined;
}
this.user = opts.user || process.env.USERNAME;
Expand Down

0 comments on commit b85206e

Please sign in to comment.