@not3/sdk
    Preparing search index...

    Class P2PProtocol

    Framing, chunk math, and encryption helpers for the P2P wire protocol. String frames carry P2PControlMessages, binary frames carry chunks whose plaintext is [4-byte big-endian chunk index][payload].

    Index

    Properties

    BUFFER_HIGH_WATER: number = ...

    Pause sending above this many buffered bytes.

    4 * 1024 * 1024
    
    BUFFER_LOW_WATER: number = ...

    Resume sending below this many buffered bytes.

    1 * 1024 * 1024
    
    CHUNK_INDEX_BYTES: 4

    Bytes used for the chunk index prefix inside the plaintext.

    4
    
    CONNECT_TIMEOUT_MS: 30000

    Timeout for establishing the peer connection after pairing.

    30000
    
    CONTROL_TIMEOUT_MS: 30000

    Timeout for awaited control messages (meta/accept/complete).

    30000
    
    MAX_MESSAGE_SIZE: 262144

    Hard cap for a single data-channel message (cross-implementation safe).

    262144
    
    NACK_RETRY_LIMIT: 3

    How often a single chunk may be re-requested before aborting.

    3
    

    Methods

    • Total chunks for a file size.

      Parameters

      • size: number

        The file size in bytes.

      • chunkPayloadSize: number

        The payload size of a single chunk.

      Returns number

      The number of chunks.

    • Usable payload bytes per chunk for a given negotiated SCTP max message size.

      Parameters

      • OptionalsctpMaxMessageSize: number | null

        The negotiated maximum, or null when unknown.

      Returns number

      The plaintext payload size of a single chunk.

    • Decrypt a binary frame into index and payload.

      Parameters

      • data: ArrayBuffer

        The frame to decrypt.

      • key: CryptoKey

        The transfer key.

      Returns Promise<{ index: number; payload: ArrayBuffer }>

      The chunk index and its payload.

      Error If the frame was tampered with or the key is wrong.

    • Decrypt a string frame into a control message.

      Parameters

      • data: string

        The frame to decrypt.

      • key: CryptoKey

        The transfer key.

      Returns Promise<P2PControlMessage>

      The control message.

      Error If the key is wrong or the frame is garbage.

    • Encrypt a chunk into a binary frame.

      Parameters

      • index: number

        The absolute chunk index.

      • payload: ArrayBuffer

        The plaintext chunk payload.

      • key: CryptoKey

        The transfer key.

      Returns Promise<ArrayBuffer>

      The encrypted binary frame.