CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-webrtc-adapter

A shim to insulate apps from WebRTC spec changes and browser prefix differences

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-6/

WebRTC Data Channel Message Size Manager

Build a utility that helps developers safely send large messages over WebRTC data channels by determining the maximum safe message size and chunking oversized messages accordingly.

Background

WebRTC data channels have browser-specific limitations on message sizes. Different browsers support different maximum message sizes, and these limits can vary based on browser versions and what the remote peer supports. Attempting to send messages that exceed these limits will cause errors.

Requirements

Create a module that provides the following functionality:

  1. Maximum Size Retrieval: Get the maximum safe message size for a data channel
  2. Message Size Checking: Determine if a message can be sent safely through a data channel
  3. Message Chunking: Split large messages into safe-sized chunks that can be sent sequentially

Your implementation should:

  • Work with RTCPeerConnection and RTCDataChannel instances
  • Handle both string and binary (ArrayBuffer/Uint8Array) messages
  • Account for browser-specific message size limitations that are automatically calculated
  • Use a simple chunking approach that splits messages evenly based on the max size

Test Cases

  • Given a peer connection, getMaxMessageSize(peerConnection) returns a positive number representing the maximum safe message size in bytes @test
  • Given a peer connection and a small message (under 1KB), canSendMessage(peerConnection, message) returns true @test
  • Given a peer connection and a message larger than the maximum size, canSendMessage(peerConnection, message) returns false @test
  • Given a large message, chunkMessage(message, maxSize) returns an array of chunks where each chunk is at or under the specified size @test

Implementation

@generates

API

/**
 * Get the maximum safe message size for a peer connection's data channels
 * @param {RTCPeerConnection} peerConnection - The peer connection to check
 * @returns {number} Maximum message size in bytes
 */
function getMaxMessageSize(peerConnection) {}

/**
 * Check if a message can be sent safely through a data channel
 * @param {RTCPeerConnection} peerConnection - The peer connection to check
 * @param {string|ArrayBuffer|Uint8Array} message - The message to check
 * @returns {boolean} True if message can be sent safely
 */
function canSendMessage(peerConnection, message) {}

/**
 * Split a message into chunks of safe size
 * @param {string|ArrayBuffer|Uint8Array} message - The message to chunk
 * @param {number} maxSize - Maximum size per chunk in bytes
 * @returns {Array<string|Uint8Array>} Array of message chunks
 */
function chunkMessage(message, maxSize) {}

module.exports = {
  getMaxMessageSize,
  canSendMessage,
  chunkMessage
};

Dependencies { .dependencies }

webrtc-adapter { .dependency }

Provides cross-browser WebRTC compatibility and data channel capabilities.

Install with Tessl CLI

npx tessl i tessl/npm-webrtc-adapter

tile.json