export function decodeStringPartial()

in sdk/webpubsub-socketio-extension/src/SIO/components/decoder.ts [18:47]


export function decodeStringPartial(str: string): PartialSioPacket {
  let i = 0;
  // look up type
  const p: PartialSioPacket = {
    type: Number(str.charAt(0)),
    attachments: 0,
  };

  if (PacketType[p.type] === undefined) {
    throw new Error("unknown packet type " + p.type);
  }

  // look up attachments if type binary
  if (p.type === PacketType.BINARY_EVENT || p.type === PacketType.BINARY_ACK) {
    const start = i + 1;
    // eslint-disable-next-line no-empty
    while (str.charAt(++i) !== "-" && i != str.length) {}
    const buf = str.substring(start, i);
    // Native implementation is `buf != Number(buf) || ...`. Modify it to pass typescript compilation check
    if (buf !== Number(buf).toString() || str.charAt(i) !== "-") {
      throw new Error("Illegal attachments");
    }
    p.attachments = Number(buf);
  }

  // Skip decoding `p.namespace`, `p.id` and `p.data`

  debug("decoded %s as %j", str, p);
  return p;
}