Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix notepack's encoding problems #570

Closed
9 tasks done
Tracked by #577
Domiii opened this issue Aug 26, 2021 · 1 comment
Closed
9 tasks done
Tracked by #577

fix notepack's encoding problems #570

Domiii opened this issue Aug 26, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Domiii
Copy link
Owner

Domiii commented Aug 26, 2021

Big Notepack Problems

  • encoding of symbol type not supported; workaround:
    • open file code node_modules/notepack.io/lib/encode.js, line 255
    • insert: case 'symbol': return _encode(bytes, defers, value.toString());
    • NOTE: once patched, you have to let webpack know that things changed:
      • either restart webpack
      • or force a version change to trigger a webpack rebuild
        • code node_modules/notepack.io/package.json -> change version
  • When working with large applications (millions of traces), we run out of memory when when Client sends runtime data back to the server.

Solution #1

Finalize it

  • for now, we (temporarily) put it in dbux-common\src\msgpackParser.js, but it should go into a new, more dedicated library (maybe dbux-net etc.)
  • get rid of notepack.io completely

NOTEs

  • socket.io does not handle decode errors gracefully. It does not report the error. It does not notify the client of a connect error apparently.
@Domiii Domiii changed the title Runtime runs out of memory due to performance bug in notepack's encode When debugging big applications, @dbux/runtime runs out of memory due to performance bug in notepack's encode Aug 26, 2021
@Domiii Domiii changed the title When debugging big applications, @dbux/runtime runs out of memory due to performance bug in notepack's encode fix notepack's encoding problems Sep 12, 2021
@Domiii
Copy link
Owner Author

Domiii commented Nov 13, 2021

var msgpack = require('@msgpack/msgpack');
var Emitter = require('component-emitter');

exports.protocol = 5;

/**
 * Packet types (see https://github.com/socketio/socket.io-protocol)
 */

var PacketType = (exports.PacketType = {
  CONNECT: 0,
  DISCONNECT: 1,
  EVENT: 2,
  ACK: 3,
  CONNECT_ERROR: 4,
});

var isInteger =
  Number.isInteger ||
  function (value) {
    return (
      typeof value === "number" &&
      isFinite(value) &&
      Math.floor(value) === value
    );
  };

var isString = function (value) {
  return typeof value === "string";
};

var isObject = function (value) {
  return Object.prototype.toString.call(value) === "[object Object]";
};

function Encoder() { }

Encoder.prototype.encode = function (packet) {
  console.warn(`msgpack ENCODE`, packet);
  return [msgpack.encode(packet)];
};

function Decoder() { }

Emitter(Decoder.prototype);

Decoder.prototype.add = function (obj) {
  console.warn(`msgpack DECODE`);
  try {
    var decoded = msgpack.decode(obj);
    this.checkPacket(decoded);
    this.emit("decoded", decoded);
  }
  catch (err) {
    console.error(`DECODE error -`, err);
  }
};

function exists(x) {
  return x !== null && x !== undefined;
}

function isDataValid(decoded) {
  switch (decoded.type) {
    case PacketType.CONNECT:
      return !exists(decoded.data) || isObject(decoded.data);
    case PacketType.DISCONNECT:
      return !exists(decoded.data);
    case PacketType.CONNECT_ERROR:
      return isString(decoded.data) || isObject(decoded.data);
    default:
      return Array.isArray(decoded.data);
  }
}

Decoder.prototype.checkPacket = function (decoded) {
  var isTypeValid =
    isInteger(decoded.type) &&
    decoded.type >= PacketType.CONNECT &&
    decoded.type <= PacketType.CONNECT_ERROR;
  if (!isTypeValid) {
    throw new Error("invalid packet type");
  }

  if (!isString(decoded.nsp)) {
    throw new Error("invalid namespace");
  }

  if (!isDataValid(decoded)) {
    throw new Error("invalid payload");
  }

  var isAckValid = !exists(decoded.id) || isInteger(decoded.id);
  if (!isAckValid) {
    throw new Error("invalid packet id");
  }
};

Decoder.prototype.destroy = function () { };

exports.Encoder = Encoder;
exports.Decoder = Decoder;

@Domiii Domiii self-assigned this Nov 13, 2021
@Domiii Domiii added the bug Something isn't working label Nov 14, 2021
@Domiii Domiii closed this as completed Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant