or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

connections.mdindex.mdprotocols.mdservers.mdtransports.md
tile.json

tessl/npm-thrift

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

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/thrift@0.22.x

To install, run

npx @tessl/cli install tessl/npm-thrift@0.22.0

index.mddocs/

Thrift

Node.js bindings for Apache Thrift RPC system providing comprehensive client and server libraries for cross-language service communication. This package offers complete protocol implementations, flexible transport mechanisms, and robust server frameworks optimized for Node.js environments.

Package Information

  • Package Name: thrift
  • Package Type: npm
  • Language: JavaScript/Node.js
  • Installation: npm install thrift
  • Version: 0.22.0
  • License: Apache-2.0

Core Imports

const thrift = require('thrift');

// Common pattern - destructure needed components
const {
  createConnection,
  createClient,
  createServer,
  TBinaryProtocol,
  TBufferedTransport
} = require('thrift');

For ES modules:

import thrift from 'thrift';
import { createConnection, createClient } from 'thrift';

Basic Usage

Client Example

const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');

// Create connection
const connection = thrift.createConnection('localhost', 9090, {
  transport: thrift.TBufferedTransport,
  protocol: thrift.TBinaryProtocol
});

// Handle connection events
connection.on('error', (err) => {
  console.error('Connection error:', err);
});

// Create client
const client = thrift.createClient(MyService, connection);

// Call remote methods
client.myMethod('hello world', (err, result) => {
  if (err) {
    console.error('RPC error:', err);
  } else {
    console.log('Result:', result);
  }
});

Server Example

const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');

// Implement service
const serviceHandler = {
  myMethod: function(message, callback) {
    console.log('Received:', message);
    callback(null, 'Response: ' + message);
  }
};

// Create server
const server = thrift.createServer(MyService, serviceHandler, {
  transport: thrift.TBufferedTransport,
  protocol: thrift.TBinaryProtocol
});

// Start server
server.listen(9090, () => {
  console.log('Server listening on port 9090');
});

Architecture

The Node.js Thrift library provides a modular architecture with pluggable components:

  • Connection Layer: Multiple connection types (TCP, HTTP, WebSocket, XHR) with configurable options
  • Protocol Layer: Serialization formats (Binary, Compact, JSON, Header) for data encoding
  • Transport Layer: Communication mechanisms (Buffered, Framed, WebSocket) with layering support
  • Server Layer: Flexible server implementations (Simple, Multiplexed, Web) with event handling
  • Client Layer: Auto-generated client stubs with callback-based and promise-based APIs
  • Multiplexing: Support for multiple services over a single connection

Capabilities

Connection Management

Comprehensive connection management supporting multiple transport mechanisms including TCP, HTTP, WebSocket, and XHR connections with SSL/TLS support and automatic reconnection.

// TCP connections
function createConnection(host, port, options): Connection;
function createSSLConnection(host, port, options): Connection;  
function createUDSConnection(path, options): Connection;

// HTTP connections
function createHttpConnection(host, port, options): HttpConnection;
function createHttpClient(ServiceClient, connection): Client;

// WebSocket connections
function createWSConnection(host, port, options): WSConnection;
function createWSClient(ServiceClient, connection): Client;

Connection Management

Protocol Support

Multiple serialization protocols for efficient data transmission with full JavaScript type support and configurable options for performance optimization.

// Protocol classes
class TBinaryProtocol {
  constructor(transport, strictRead?, strictWrite?);
}

class TCompactProtocol {
  constructor(transport);
}

class TJSONProtocol {
  constructor(transport);
}

class THeaderProtocol {
  constructor(transport, clientType?);
}

Protocol Support

Transport Layer

Flexible transport implementations providing buffering, framing, and WebSocket capabilities with seamless protocol integration and error handling.

// Transport classes
class TBufferedTransport {
  constructor(buffer?, callback?);
  isOpen(): boolean;
  open(callback): void;
  close(): void;
  read(len): Buffer;
  write(buf): void;
  flush(): void;
}

class TFramedTransport {
  constructor(buffer?, callback?);
  // Same interface as TBufferedTransport
}

Transport Layer

Server Framework

Production-ready server implementations supporting single and multiplexed services with configurable protocols, transports, and comprehensive error handling.

// Server creation
function createServer(processor, handler, options): Server;
function createMultiplexServer(processor, options): MultiplexServer;
function createWebServer(processor, options): WebServer;

// Server interface
class Server {
  listen(port, host?, callback?): void;
  close(callback?): void;
  on(event, listener): void;
}

Server Framework

Types

// Core Thrift types and constants
const Type = {
  STOP: 0, VOID: 1, BOOL: 2, BYTE: 3, I08: 3,
  DOUBLE: 4, I16: 6, I32: 8, I64: 10,
  STRING: 11, UTF7: 11, STRUCT: 12, MAP: 13,
  SET: 14, LIST: 15, UTF8: 16, UTF16: 17
};

const MessageType = {
  CALL: 1, REPLY: 2, EXCEPTION: 3, ONEWAY: 4
};

// Exception classes
class TException extends Error {
  constructor(message: string);
}

class TApplicationException extends TException {
  constructor(type?: number, message?: string);
}

// Utility types
const Int64: typeof import('node-int64');
const Q: typeof import('q');