auth_switch_request.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest
  3. const Packet = require('../packets/packet');
  4. class AuthSwitchRequest {
  5. constructor(opts) {
  6. this.pluginName = opts.pluginName;
  7. this.pluginData = opts.pluginData;
  8. }
  9. toPacket() {
  10. const length = 6 + this.pluginName.length + this.pluginData.length;
  11. const buffer = Buffer.allocUnsafe(length);
  12. const packet = new Packet(0, buffer, 0, length);
  13. packet.offset = 4;
  14. packet.writeInt8(0xfe);
  15. // TODO: use server encoding
  16. packet.writeNullTerminatedString(this.pluginName, 'cesu8');
  17. packet.writeBuffer(this.pluginData);
  18. return packet;
  19. }
  20. static fromPacket(packet) {
  21. packet.readInt8(); // marker
  22. // assert marker == 0xfe?
  23. // TODO: use server encoding
  24. const name = packet.readNullTerminatedString('cesu8');
  25. const data = packet.readBuffer();
  26. return new AuthSwitchRequest({
  27. pluginName: name,
  28. pluginData: data
  29. });
  30. }
  31. }
  32. module.exports = AuthSwitchRequest;