ping.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const Command = require('./command');
  3. const Errors = require('../misc/errors');
  4. /**
  5. * send a COM_PING: permits sending a packet containing one byte to check that the connection is active.
  6. * see https://mariadb.com/kb/en/library/com_ping/
  7. */
  8. class Ping extends Command {
  9. constructor(resolve, reject) {
  10. super(resolve, reject);
  11. }
  12. start(out, opts, info) {
  13. out.startPacket(this);
  14. out.writeInt8(0x0e);
  15. out.flushBuffer(true);
  16. this.emit('send_end');
  17. this.onPacketReceive = this.readPingResponsePacket;
  18. }
  19. /**
  20. * Read ping response packet.
  21. * packet can be :
  22. * - an ERR_Packet
  23. * - a OK_Packet
  24. *
  25. * @param packet query response
  26. * @param out output writer
  27. * @param opts connection options
  28. * @param info connection info
  29. */
  30. readPingResponsePacket(packet, out, opts, info) {
  31. if (packet.peek() !== 0x00) {
  32. return this.throwNewError(
  33. 'unexpected packet',
  34. false,
  35. info,
  36. '42000',
  37. Errors.ER_PING_BAD_PACKET
  38. );
  39. }
  40. packet.skip(1); //skip header
  41. packet.skipLengthCodedNumber(); //affected rows
  42. packet.skipLengthCodedNumber(); //insert ids
  43. info.status = packet.readUInt16();
  44. this.successEnd(null);
  45. }
  46. }
  47. module.exports = Ping;