private _getPacketNumberForNextSend()

in sdk/webpubsub-socketio-extension/src/EIO/components/web-pubsub-transport.ts [162:232]


  private _getPacketNumberForNextSend(packets: EioPacket[]): number {
    if (!this._sioMode) return packets.length;

    /**
     * Binary Packet = an EIO packet whose data is BINARY_EVENT or BINARY_ACK SIO packet.
     * Binary Attachment Packet = an EIO packet whose data is binary attachment of a binary packet.
     */
    let attachmentCount = 0, // the number of binary attachment packets found for the latest binary packet.
      expectedAttachments = 0, // the expected number of binary attachment packets for the latest binary packet.
      lastBinaryMessagePacketIdx = -1, // the index of the latest binary packet.
      lastSentPacketIdx = -1, // the largest index of EIO packet that can be sent in the next REST API call
      shouldBeAttachment = false; // whether the current packet should be a binary attachment packet

    for (let i = 0; i < packets.length; i++) {
      const eioPacket = packets[i];

      // Condition 0: a pure EIO packet without data related to SIO packet.
      if (eioPacket.type !== "message") {
        lastSentPacketIdx++;
        continue;
      }

      // Condition 1: A binary attachment packet
      if (this._isMessageWithBinary(eioPacket)) {
        if (!shouldBeAttachment)
          throw new Error(
            `Expect a packet whose data is binary attachment but not found, packets[${i}] = ${eioPacket}`
          );

        attachmentCount++;
        if (attachmentCount === expectedAttachments) {
          if (lastBinaryMessagePacketIdx < 0 || lastBinaryMessagePacketIdx >= i)
            throw new Error(
              `Invalid lastBinaryMessagePacketIdx = ${lastBinaryMessagePacketIdx}, packets[${i}] = ${eioPacket}`
            );
          attachmentCount = 0;
          shouldBeAttachment = false;
          lastSentPacketIdx = i;
        } else {
          shouldBeAttachment = true;
        }
        continue;
      }

      const sioPacket: PartialSioPacket = decodeStringPartial(eioPacket.data);

      // Condition 2: A binary packet
      if (this._isTypeWithBinary(sioPacket)) {
        if (shouldBeAttachment)
          throw new Error(
            `Expect a packet with binary content, but got a regular packet, packets[${i}] = ${eioPacket}`
          );
        if (attachmentCount !== 0)
          throw new Error(`Exepect attachmentCount = 0 but got ${attachmentCount}, packets[${i}] = ${eioPacket}`);

        attachmentCount = 0;
        expectedAttachments = sioPacket.attachments;
        lastBinaryMessagePacketIdx = i;
        shouldBeAttachment = true;
        continue;
      }

      // Condition 3: A EIO packet whose data is related to SIO packet. But it is neither a binary packet nor a binary attachment packet
      if (shouldBeAttachment) {
        throw new Error(`Expect a packet whose data is binary attachment but not found, packets[${i}] = ${eioPacket}`);
      }
      shouldBeAttachment = false;
      lastSentPacketIdx++;
    }
    return lastSentPacketIdx + 1;
  }