register_slave.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. // http://dev.mysql.com/doc/internals/en/com-register-slave.html
  3. // note that documentation is incorrect, for example command code is actually 0x15 but documented as 0x14
  4. const Packet = require('../packets/packet');
  5. const CommandCodes = require('../constants/commands');
  6. class RegisterSlave {
  7. constructor(opts) {
  8. this.serverId = opts.serverId || 0;
  9. this.slaveHostname = opts.slaveHostname || '';
  10. this.slaveUser = opts.slaveUser || '';
  11. this.slavePassword = opts.slavePassword || '';
  12. this.slavePort = opts.slavePort || 0;
  13. this.replicationRank = opts.replicationRank || 0;
  14. this.masterId = opts.masterId || 0;
  15. }
  16. toPacket() {
  17. const length =
  18. 15 + // TODO: should be ascii?
  19. Buffer.byteLength(this.slaveHostname, 'utf8') +
  20. Buffer.byteLength(this.slaveUser, 'utf8') +
  21. Buffer.byteLength(this.slavePassword, 'utf8') +
  22. 3 +
  23. 4;
  24. const buffer = Buffer.allocUnsafe(length);
  25. const packet = new Packet(0, buffer, 0, length);
  26. packet.offset = 4;
  27. packet.writeInt8(CommandCodes.REGISTER_SLAVE);
  28. packet.writeInt32(this.serverId);
  29. packet.writeInt8(Buffer.byteLength(this.slaveHostname, 'utf8'));
  30. packet.writeString(this.slaveHostname);
  31. packet.writeInt8(Buffer.byteLength(this.slaveUser, 'utf8'));
  32. packet.writeString(this.slaveUser);
  33. packet.writeInt8(Buffer.byteLength(this.slavePassword, 'utf8'));
  34. packet.writeString(this.slavePassword);
  35. packet.writeInt16(this.slavePort);
  36. packet.writeInt32(this.replicationRank);
  37. packet.writeInt32(this.masterId);
  38. return packet;
  39. }
  40. }
  41. module.exports = RegisterSlave;