packet-node-encoded.js 847 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const Packet = require('./packet');
  3. class PacketNodeEncoded extends Packet {
  4. constructor(buf, pos, end, encoding) {
  5. super(buf, pos, end);
  6. this.encoding = encoding;
  7. }
  8. readStringLength() {
  9. const len = this.readUnsignedLength();
  10. if (len === null) return null;
  11. this.pos += len;
  12. return this.buf.toString(this.encoding, this.pos - len, this.pos);
  13. }
  14. readString(beg, len) {
  15. return this.buf.toString(this.encoding, beg, beg + len);
  16. }
  17. subPacketLengthEncoded() {
  18. const len = this.readUnsignedLength();
  19. this.skip(len);
  20. return new PacketNodeEncoded(this.buf, this.pos - len, this.pos, this.encoding);
  21. }
  22. readStringRemaining() {
  23. const str = this.buf.toString(this.encoding, this.pos, this.end);
  24. this.pos = this.end;
  25. return str;
  26. }
  27. }
  28. module.exports = PacketNodeEncoded;