CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-thrift

Node.js bindings for Apache Thrift RPC system providing client/server libraries, protocols, and transports for cross-language service communication.

Pending
Overview
Eval results
Files

transports.mddocs/

Transport Layer

Flexible transport implementations providing buffering, framing, and WebSocket capabilities with seamless protocol integration, error handling, and optimized I/O operations for Node.js environments.

Capabilities

Buffered Transport

Standard buffered transport providing efficient I/O operations with configurable buffer sizes and automatic flushing for optimal performance in most scenarios.

/**
 * Buffered transport implementation for efficient I/O operations
 * @param buffer - Optional initial buffer or underlying transport
 * @param callback - Optional callback function for transport events
 */
class TBufferedTransport {
  constructor(buffer?, callback?);
  
  // Connection management
  isOpen(): boolean;
  open(callback?): void;
  close(): void;
  
  // I/O operations
  read(len): Buffer;
  readAll(len): Buffer;
  write(buf): void;
  flush(callback?): void;
  
  // Buffer management
  getCurrent(): number;
  commitPosition(): void;
  rollbackPosition(): void;
  
  // Event handling
  on(event, listener): void;
  emit(event, ...args): void;
  
  // Configuration properties
  readBufferSize: number;
  writeBufferSize: number;
}

Usage Examples:

const thrift = require('thrift');

// Basic buffered transport
const transport = new thrift.TBufferedTransport();

// With custom buffer sizes
const customTransport = new thrift.TBufferedTransport(null, {
  readBufferSize: 8192,
  writeBufferSize: 8192
});

// With callback for events
const callbackTransport = new thrift.TBufferedTransport(null, (err, result) => {
  if (err) {
    console.error('Transport error:', err);
  } else {
    console.log('Transport operation completed');
  }
});

// Writing data
transport.write(Buffer.from('Hello, Thrift!'));
transport.flush((err) => {
  if (err) {
    console.error('Flush error:', err);
  } else {
    console.log('Data flushed successfully');
  }
});

// Reading data
const data = transport.read(1024);
console.log('Read data:', data.toString());

Framed Transport

Framed transport implementation that adds length prefixes to messages for reliable message boundaries and improved parsing efficiency over stream-oriented connections.

/**
 * Framed transport implementation with message framing
 * @param buffer - Optional initial buffer or underlying transport
 * @param callback - Optional callback function for transport events
 */
class TFramedTransport {
  constructor(buffer?, callback?);
  
  // Same interface as TBufferedTransport with framing support
  isOpen(): boolean;
  open(callback?): void;
  close(): void;
  
  read(len): Buffer;
  readAll(len): Buffer;
  write(buf): void;
  flush(callback?): void;
  
  getCurrent(): number;
  commitPosition(): void;
  rollbackPosition(): void;
  
  on(event, listener): void;
  emit(event, ...args): void;
  
  // Framing-specific properties
  frameSize: number;
  maxFrameSize: number;
}

Usage Examples:

// Basic framed transport for reliable message boundaries
const framedTransport = new thrift.TFramedTransport();

// With custom frame size limits
const customFramedTransport = new thrift.TFramedTransport(null, {
  maxFrameSize: 1024 * 1024 // 1MB max frame size
});

// Automatic message framing
framedTransport.write(Buffer.from('Message 1'));
framedTransport.write(Buffer.from('Message 2'));
framedTransport.flush(); // Sends both messages with proper framing

// Frame boundaries are handled automatically
const message1 = framedTransport.read(); // Reads exactly one complete message
const message2 = framedTransport.read(); // Reads the next complete message

WebSocket Transport

WebSocket transport implementation for real-time, bidirectional communication with support for browser and Node.js environments.

/**
 * WebSocket transport implementation for real-time communication
 * @param websocket - WebSocket instance or connection options
 */
class TWSTransport {
  constructor(websocket);
  
  // WebSocket-specific interface
  isOpen(): boolean;
  open(callback?): void;
  close(code?, reason?): void;
  
  read(len): Buffer;
  readAll(len): Buffer;
  write(buf): void;
  flush(callback?): void;
  
  // WebSocket event handling
  on(event, listener): void;
  emit(event, ...args): void;
  
  // WebSocket properties
  readyState: number;
  protocol: string;
  extensions: object;
}

Usage Examples:

const WebSocket = require('ws');

// WebSocket transport for real-time communication
const ws = new WebSocket('ws://localhost:8080/thrift');
const wsTransport = new thrift.TWSTransport(ws);

// Handle WebSocket events
wsTransport.on('open', () => {
  console.log('WebSocket transport connected');
});

wsTransport.on('message', (data) => {
  console.log('Received WebSocket message');
});

wsTransport.on('close', (code, reason) => {
  console.log(`WebSocket closed: ${code} - ${reason}`);
});

wsTransport.on('error', (error) => {
  console.error('WebSocket error:', error);
});

// Real-time bidirectional communication
wsTransport.write(Buffer.from('Real-time message'));
wsTransport.flush();

Memory Transport

In-memory transport implementation for testing, debugging, and local inter-process communication without network overhead.

/**
 * Memory transport implementation for in-memory communication
 * @param buffer - Optional initial buffer content
 */
class TMemoryBuffer {
  constructor(buffer?);
  
  // Memory-specific interface
  isOpen(): boolean;
  open(): void;
  close(): void;
  
  read(len): Buffer;
  readAll(len): Buffer;
  write(buf): void;
  flush(): void;
  
  // Memory buffer operations
  clear(): void;
  getBuffer(): Buffer;
  available(): number;
  
  // Position management
  getCurrent(): number;
  setCurrent(position): void;
  reset(): void;
}

Usage Examples:

// Memory transport for testing and debugging
const memoryTransport = new thrift.TMemoryBuffer();

// Pre-populate with test data
const testData = Buffer.from('test message');
const preloadedTransport = new thrift.TMemoryBuffer(testData);

// Write and read operations
memoryTransport.write(Buffer.from('Hello'));
memoryTransport.write(Buffer.from(', World!'));

// Read back the data
const result = memoryTransport.read(13);
console.log('Memory transport result:', result.toString()); // "Hello, World!"

// Useful for unit testing
const protocol = new thrift.TBinaryProtocol(memoryTransport);
protocol.writeString('test');
protocol.flush();

memoryTransport.reset(); // Reset position to beginning
const readResult = protocol.readString();
console.log('Round-trip test:', readResult); // "test"

Transport Layering

// Transport layering for combining features
interface TransportLayering {
  // Base transport provides the actual I/O
  baseTransport: TSocket | THttpTransport | TWSTransport;
  
  // Layered transports add functionality
  bufferedTransport: TBufferedTransport;
  framedTransport: TFramedTransport;
  
  // Example: Framed + Buffered over TCP
  layeredStack: TFramedTransport(TBufferedTransport(TSocket));
}

Usage Examples:

// Layering transports for enhanced functionality
const socket = new thrift.TSocket('localhost', 9090);
const buffered = new thrift.TBufferedTransport(socket);
const framed = new thrift.TFramedTransport(buffered);

// Use the layered transport with protocol
const protocol = new thrift.TBinaryProtocol(framed);

// Benefits:
// - Socket provides network I/O
// - Buffered improves I/O efficiency  
// - Framed ensures message boundaries
// - Protocol handles serialization

Transport Selection Guidelines

// Transport selection criteria and use cases
interface TransportSelection {
  TBufferedTransport: {
    performance: 'High',
    reliability: 'Good',
    useCase: 'General purpose, most common choice'
  };
  
  TFramedTransport: {
    performance: 'High',
    reliability: 'Excellent',
    useCase: 'Stream protocols, message boundaries required'
  };
  
  TWSTransport: {
    performance: 'Good',
    reliability: 'Good',
    useCase: 'Real-time communication, browser compatibility'
  };
  
  TMemoryBuffer: {
    performance: 'Highest',
    reliability: 'Excellent',
    useCase: 'Testing, debugging, in-memory processing'
  };
}

Install with Tessl CLI

npx tessl i tessl/npm-thrift

docs

connections.md

index.md

protocols.md

servers.md

transports.md

tile.json