packet.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isPacketComplete = isPacketComplete;
  6. exports.packetLength = packetLength;
  7. exports.Packet = exports.OFFSET = exports.TYPE = exports.HEADER_LENGTH = void 0;
  8. var _sprintfJs = require("sprintf-js");
  9. const HEADER_LENGTH = 8;
  10. exports.HEADER_LENGTH = HEADER_LENGTH;
  11. const TYPE = {
  12. SQL_BATCH: 0x01,
  13. RPC_REQUEST: 0x03,
  14. TABULAR_RESULT: 0x04,
  15. ATTENTION: 0x06,
  16. BULK_LOAD: 0x07,
  17. TRANSACTION_MANAGER: 0x0E,
  18. LOGIN7: 0x10,
  19. NTLMAUTH_PKT: 0x11,
  20. PRELOGIN: 0x12,
  21. FEDAUTH_TOKEN: 0x08
  22. };
  23. exports.TYPE = TYPE;
  24. const typeByValue = {};
  25. for (const name in TYPE) {
  26. typeByValue[TYPE[name]] = name;
  27. }
  28. const STATUS = {
  29. NORMAL: 0x00,
  30. EOM: 0x01,
  31. IGNORE: 0x02,
  32. RESETCONNECTION: 0x08,
  33. RESETCONNECTIONSKIPTRAN: 0x10
  34. };
  35. const OFFSET = {
  36. Type: 0,
  37. Status: 1,
  38. Length: 2,
  39. SPID: 4,
  40. PacketID: 6,
  41. Window: 7
  42. };
  43. exports.OFFSET = OFFSET;
  44. const DEFAULT_SPID = 0;
  45. const DEFAULT_PACKETID = 1;
  46. const DEFAULT_WINDOW = 0;
  47. const NL = '\n';
  48. class Packet {
  49. constructor(typeOrBuffer) {
  50. if (typeOrBuffer instanceof Buffer) {
  51. this.buffer = typeOrBuffer;
  52. } else {
  53. const type = typeOrBuffer;
  54. this.buffer = Buffer.alloc(HEADER_LENGTH, 0);
  55. this.buffer.writeUInt8(type, OFFSET.Type);
  56. this.buffer.writeUInt8(STATUS.NORMAL, OFFSET.Status);
  57. this.buffer.writeUInt16BE(DEFAULT_SPID, OFFSET.SPID);
  58. this.buffer.writeUInt8(DEFAULT_PACKETID, OFFSET.PacketID);
  59. this.buffer.writeUInt8(DEFAULT_WINDOW, OFFSET.Window);
  60. this.setLength();
  61. }
  62. }
  63. setLength() {
  64. this.buffer.writeUInt16BE(this.buffer.length, OFFSET.Length);
  65. }
  66. length() {
  67. return this.buffer.readUInt16BE(OFFSET.Length);
  68. }
  69. resetConnection(reset) {
  70. let status = this.buffer.readUInt8(OFFSET.Status);
  71. if (reset) {
  72. status |= STATUS.RESETCONNECTION;
  73. } else {
  74. status &= 0xFF - STATUS.RESETCONNECTION;
  75. }
  76. this.buffer.writeUInt8(status, OFFSET.Status);
  77. }
  78. last(last) {
  79. let status = this.buffer.readUInt8(OFFSET.Status);
  80. if (arguments.length > 0) {
  81. if (last) {
  82. status |= STATUS.EOM;
  83. } else {
  84. status &= 0xFF - STATUS.EOM;
  85. }
  86. this.buffer.writeUInt8(status, OFFSET.Status);
  87. }
  88. return this.isLast();
  89. }
  90. ignore(last) {
  91. let status = this.buffer.readUInt8(OFFSET.Status);
  92. if (last) {
  93. status |= STATUS.IGNORE;
  94. } else {
  95. status &= 0xFF - STATUS.IGNORE;
  96. }
  97. this.buffer.writeUInt8(status, OFFSET.Status);
  98. }
  99. isLast() {
  100. return !!(this.buffer.readUInt8(OFFSET.Status) & STATUS.EOM);
  101. }
  102. packetId(packetId) {
  103. if (packetId) {
  104. this.buffer.writeUInt8(packetId % 256, OFFSET.PacketID);
  105. }
  106. return this.buffer.readUInt8(OFFSET.PacketID);
  107. }
  108. addData(data) {
  109. this.buffer = Buffer.concat([this.buffer, data]);
  110. this.setLength();
  111. return this;
  112. }
  113. data() {
  114. return this.buffer.slice(HEADER_LENGTH);
  115. }
  116. type() {
  117. return this.buffer.readUInt8(OFFSET.Type);
  118. }
  119. statusAsString() {
  120. const status = this.buffer.readUInt8(OFFSET.Status);
  121. const statuses = [];
  122. for (const name in STATUS) {
  123. const value = STATUS[name];
  124. if (status & value) {
  125. statuses.push(name);
  126. } else {
  127. statuses.push(undefined);
  128. }
  129. }
  130. return statuses.join(' ').trim();
  131. }
  132. headerToString(indent = '') {
  133. const text = (0, _sprintfJs.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));
  134. return indent + text;
  135. }
  136. dataToString(indent = '') {
  137. const BYTES_PER_GROUP = 0x04;
  138. const CHARS_PER_GROUP = 0x08;
  139. const BYTES_PER_LINE = 0x20;
  140. const data = this.data();
  141. let dataDump = '';
  142. let chars = '';
  143. for (let offset = 0; offset < data.length; offset++) {
  144. if (offset % BYTES_PER_LINE === 0) {
  145. dataDump += indent;
  146. dataDump += (0, _sprintfJs.sprintf)('%04X ', offset);
  147. }
  148. if (data[offset] < 0x20 || data[offset] > 0x7E) {
  149. chars += '.';
  150. if ((offset + 1) % CHARS_PER_GROUP === 0 && !((offset + 1) % BYTES_PER_LINE === 0)) {
  151. chars += ' ';
  152. }
  153. } else {
  154. chars += String.fromCharCode(data[offset]);
  155. }
  156. if (data[offset] != null) {
  157. dataDump += (0, _sprintfJs.sprintf)('%02X', data[offset]);
  158. }
  159. if ((offset + 1) % BYTES_PER_GROUP === 0 && !((offset + 1) % BYTES_PER_LINE === 0)) {
  160. dataDump += ' ';
  161. }
  162. if ((offset + 1) % BYTES_PER_LINE === 0) {
  163. dataDump += ' ' + chars;
  164. chars = '';
  165. if (offset < data.length - 1) {
  166. dataDump += NL;
  167. }
  168. }
  169. }
  170. if (chars.length) {
  171. dataDump += ' ' + chars;
  172. }
  173. return dataDump;
  174. }
  175. toString(indent = '') {
  176. return this.headerToString(indent) + '\n' + this.dataToString(indent + indent);
  177. }
  178. payloadString() {
  179. return '';
  180. }
  181. }
  182. exports.Packet = Packet;
  183. function isPacketComplete(potentialPacketBuffer) {
  184. if (potentialPacketBuffer.length < HEADER_LENGTH) {
  185. return false;
  186. } else {
  187. return potentialPacketBuffer.length >= potentialPacketBuffer.readUInt16BE(OFFSET.Length);
  188. }
  189. }
  190. function packetLength(potentialPacketBuffer) {
  191. return potentialPacketBuffer.readUInt16BE(OFFSET.Length);
  192. }