packet.js ➔ dataToString   F
last analyzed

Complexity

Conditions 13
Paths 292

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 292
dl 0
loc 49
rs 3.8204
c 0
b 0
f 0
nop 1

How to fix   Complexity   

Complexity

Complex classes like packet.js ➔ dataToString often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
4
5
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
6
7
var _createClass2 = require('babel-runtime/helpers/createClass');
8
9
var _createClass3 = _interopRequireDefault(_createClass2);
10
11
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13
var sprintf = require('sprintf').sprintf;
14
15
var HEADER_LENGTH = module.exports.HEADER_LENGTH = 8;
16
17
var TYPE = module.exports.TYPE = {
18
  SQL_BATCH: 0x01,
19
  RPC_REQUEST: 0x03,
20
  TABULAR_RESULT: 0x04,
21
  ATTENTION: 0x06,
22
  BULK_LOAD: 0x07,
23
  TRANSACTION_MANAGER: 0x0E,
24
  LOGIN7: 0x10,
25
  NTLMAUTH_PKT: 0x11,
26
  PRELOGIN: 0x12
27
};
28
29
var typeByValue = {};
30
31
for (var name in TYPE) {
32
  typeByValue[TYPE[name]] = name;
33
}
34
35
var STATUS = {
36
  NORMAL: 0x00,
37
  EOM: 0x01,
38
  IGNORE: 0x02,
39
  RESETCONNECTION: 0x08,
40
  RESETCONNECTIONSKIPTRAN: 0x10
41
};
42
43
var OFFSET = module.exports.OFFSET = {
44
  Type: 0,
45
  Status: 1,
46
  Length: 2,
47
  SPID: 4,
48
  PacketID: 6,
49
  Window: 7
50
};
51
52
var DEFAULT_SPID = 0;
53
54
var DEFAULT_PACKETID = 1;
55
56
var DEFAULT_WINDOW = 0;
57
58
var NL = '\n';
59
60
var Packet = function () {
61
  function Packet(typeOrBuffer) {
62
    (0, _classCallCheck3.default)(this, Packet);
63
64
    if (typeOrBuffer instanceof Buffer) {
65
      this.buffer = typeOrBuffer;
66
    } else {
67
      var type = typeOrBuffer;
68
      this.buffer = new Buffer(HEADER_LENGTH);
69
      this.buffer.writeUInt8(type, OFFSET.Type);
70
      this.buffer.writeUInt8(STATUS.NORMAL, OFFSET.Status);
71
      this.buffer.writeUInt16BE(DEFAULT_SPID, OFFSET.SPID);
72
      this.buffer.writeUInt8(DEFAULT_PACKETID, OFFSET.PacketID);
73
      this.buffer.writeUInt8(DEFAULT_WINDOW, OFFSET.Window);
74
      this.setLength();
75
    }
76
  }
77
78
  (0, _createClass3.default)(Packet, [{
79
    key: 'setLength',
80
    value: function setLength() {
81
      return this.buffer.writeUInt16BE(this.buffer.length, OFFSET.Length);
82
    }
83
  }, {
84
    key: 'length',
85
    value: function length() {
86
      return this.buffer.readUInt16BE(OFFSET.Length);
87
    }
88
  }, {
89
    key: 'resetConnection',
90
    value: function resetConnection(reset) {
91
      var status = this.buffer.readUInt8(OFFSET.Status);
92
      if (reset) {
93
        status |= STATUS.RESETCONNECTION;
94
      } else {
95
        status &= 0xFF - STATUS.RESETCONNECTION;
96
      }
97
      return this.buffer.writeUInt8(status, OFFSET.Status);
98
    }
99
  }, {
100
    key: 'last',
101
    value: function last(_last) {
102
      var status = this.buffer.readUInt8(OFFSET.Status);
103
      if (arguments.length > 0) {
104
        if (_last) {
105
          status |= STATUS.EOM;
106
        } else {
107
          status &= 0xFF - STATUS.EOM;
108
        }
109
        this.buffer.writeUInt8(status, OFFSET.Status);
110
      }
111
      return this.isLast();
112
    }
113
  }, {
114
    key: 'isLast',
115
    value: function isLast() {
116
      return !!(this.buffer.readUInt8(OFFSET.Status) & STATUS.EOM);
117
    }
118
  }, {
119
    key: 'packetId',
120
    value: function packetId(_packetId) {
121
      if (_packetId) {
122
        this.buffer.writeUInt8(_packetId % 256, OFFSET.PacketID);
123
      }
124
      return this.buffer.readUInt8(OFFSET.PacketID);
125
    }
126
  }, {
127
    key: 'addData',
128
    value: function addData(data) {
129
      this.buffer = Buffer.concat([this.buffer, data]);
130
      this.setLength();
131
      return this;
132
    }
133
  }, {
134
    key: 'data',
135
    value: function data() {
136
      return this.buffer.slice(HEADER_LENGTH);
137
    }
138
  }, {
139
    key: 'type',
140
    value: function type() {
141
      return this.buffer.readUInt8(OFFSET.Type);
142
    }
143
  }, {
144
    key: 'statusAsString',
145
    value: function statusAsString() {
146
      var status = this.buffer.readUInt8(OFFSET.Status);
147
      var statuses = [];
148
149
      for (var _name in STATUS) {
150
        var value = STATUS[_name];
151
152
        if (status & value) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
153
          statuses.push(_name);
154
        } else {
155
          statuses.push(undefined);
156
        }
157
      }
158
159
      return statuses.join(' ').trim();
160
    }
161
  }, {
162
    key: 'headerToString',
163
    value: function headerToString(indent) {
164
      indent || (indent = '');
165
      var text = sprintf('type:0x%02X(%s), status:0x%02X(%s), length:0x%04X, spid:0x%04X, packetId:0x%02X, window:0x%02X', this.buffer.readUInt8(OFFSET.Type), typeByValue[this.buffer.readUInt8(OFFSET.Type)], this.buffer.readUInt8(OFFSET.Status), this.statusAsString(), this.buffer.readUInt16BE(OFFSET.Length), this.buffer.readUInt16BE(OFFSET.SPID), this.buffer.readUInt8(OFFSET.PacketID), this.buffer.readUInt8(OFFSET.Window));
166
      return indent + text;
167
    }
168
  }, {
169
    key: 'dataToString',
170
    value: function dataToString(indent) {
171
      indent || (indent = '');
172
173
      var BYTES_PER_GROUP = 0x04;
174
      var CHARS_PER_GROUP = 0x08;
175
      var BYTES_PER_LINE = 0x20;
176
      var data = this.data();
177
178
      var dataDump = '';
179
      var chars = '';
180
181
      for (var offset = 0; offset < data.length; offset++) {
182
        if (offset % BYTES_PER_LINE === 0) {
183
          dataDump += indent;
184
          dataDump += sprintf('%04X  ', offset);
185
        }
186
187
        if (data[offset] < 0x20 || data[offset] > 0x7E) {
188
          chars += '.';
189
          if ((offset + 1) % CHARS_PER_GROUP === 0 && !((offset + 1) % BYTES_PER_LINE === 0)) {
190
            chars += ' ';
191
          }
192
        } else {
193
          chars += String.fromCharCode(data[offset]);
194
        }
195
196
        if (data[offset] != null) {
197
          dataDump += sprintf('%02X', data[offset]);
198
        }
199
200
        if ((offset + 1) % BYTES_PER_GROUP === 0 && !((offset + 1) % BYTES_PER_LINE === 0)) {
201
          dataDump += ' ';
202
        }
203
204
        if ((offset + 1) % BYTES_PER_LINE === 0) {
205
          dataDump += '  ' + chars;
206
          chars = '';
207
          if (offset < data.length - 1) {
208
            dataDump += NL;
209
          }
210
        }
211
      }
212
213
      if (chars.length) {
214
        dataDump += '  ' + chars;
215
      }
216
217
      return dataDump;
218
    }
219
  }, {
220
    key: 'toString',
221
    value: function toString(indent) {
222
      indent || (indent = '');
223
      return this.headerToString(indent) + '\n' + this.dataToString(indent + indent);
224
    }
225
  }, {
226
    key: 'payloadString',
227
    value: function payloadString() {
228
      return '';
229
    }
230
  }]);
231
  return Packet;
232
}();
233
234
module.exports.Packet = Packet;
235
236
module.exports.isPacketComplete = isPacketComplete;
237
function isPacketComplete(potentialPacketBuffer) {
238
  if (potentialPacketBuffer.length < HEADER_LENGTH) {
239
    return false;
240
  } else {
241
    return potentialPacketBuffer.length >= potentialPacketBuffer.readUInt16BE(OFFSET.Length);
242
  }
243
}
244
245
module.exports.packetLength = packetLength;
246
function packetLength(potentialPacketBuffer) {
247
  return potentialPacketBuffer.readUInt16BE(OFFSET.Length);
248
}