Node.js bindings for Apache Thrift RPC system providing client/server libraries, protocols, and transports for cross-language service communication.
—
Comprehensive connection management supporting multiple transport mechanisms with flexible configuration options, automatic error handling, and event-driven architecture for Node.js applications.
Standard TCP socket connections with support for SSL/TLS, Unix Domain Sockets, and STDIO communication.
/**
* Create a standard TCP connection to a Thrift server
* @param host - Server hostname or IP address
* @param port - Server port number
* @param options - Connection configuration options
* @returns Connection instance
*/
function createConnection(host, port, options): Connection;
/**
* Create an SSL/TLS encrypted TCP connection
* @param host - Server hostname or IP address
* @param port - Server port number
* @param options - Connection configuration with SSL options
* @returns Connection instance
*/
function createSSLConnection(host, port, options): Connection;
/**
* Create Unix Domain Socket connection for local communication
* @param path - Unix socket file path
* @param options - Connection configuration options
* @returns Connection instance
*/
function createUDSConnection(path, options): Connection;
/**
* Create STDIO connection for subprocess communication
* @param command - Command to execute for STDIO transport
* @param options - Connection configuration options
* @returns Connection instance
*/
function createStdIOConnection(command, options): Connection;
/**
* Base Connection class for TCP connections
*/
class Connection extends EventEmitter {
constructor(stream, options);
/**
* Check if connection is currently open
* @returns true if connection is open
*/
isOpen(): boolean;
/**
* Open the connection
* @param callback - Optional callback when connection opens
*/
open(callback?): void;
/**
* Close the connection
*/
close(): void;
/**
* Write data to the connection
* @param data - Data to write
*/
write(data): void;
/**
* Connection timeout in milliseconds
*/
timeout: number;
/**
* Transport class for this connection
*/
transport: TransportConstructor;
/**
* Protocol class for this connection
*/
protocol: ProtocolConstructor;
}Usage Examples:
const thrift = require('thrift');
// Basic TCP connection
const connection = thrift.createConnection('localhost', 9090, {
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol,
timeout: 5000
});
// SSL connection with certificates
const sslConnection = thrift.createSSLConnection('secure.example.com', 9091, {
transport: thrift.TFramedTransport,
protocol: thrift.TCompactProtocol,
https: true,
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
ca: fs.readFileSync('ca-cert.pem')
});
// Unix Domain Socket
const udsConnection = thrift.createUDSConnection('/tmp/thrift.sock', {
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol
});
// Connection event handling
connection.on('connect', () => {
console.log('Connected to Thrift server');
});
connection.on('error', (err) => {
console.error('Connection error:', err);
});
connection.on('close', () => {
console.log('Connection closed');
});HTTP-based connections supporting both standard HTTP and HTTP over Unix Domain Sockets with configurable headers and request options.
/**
* Create HTTP connection to a Thrift server
* @param host - Server hostname or IP address
* @param port - Server port number
* @param options - HTTP connection configuration options
* @returns HttpConnection instance
*/
function createHttpConnection(host, port, options): HttpConnection;
/**
* Create HTTP connection over Unix Domain Socket
* @param path - Unix socket file path
* @param options - HTTP connection configuration options
* @returns HttpConnection instance
*/
function createHttpUDSConnection(path, options): HttpConnection;
/**
* Create HTTP client with service binding
* @param ServiceClient - Generated service client class
* @param connection - HTTP connection instance
* @returns Bound service client
*/
function createHttpClient(ServiceClient, connection): Client;
/**
* HTTP Connection class for HTTP transport
*/
class HttpConnection {
constructor(host, port, options);
/**
* Check if connection is currently open
* @returns true if connection is open
*/
isOpen(): boolean;
/**
* Open the HTTP connection
*/
open(): void;
/**
* Close the HTTP connection
*/
close(): void;
/**
* Write data via HTTP request
* @param data - Data to write
*/
write(data): void;
/**
* HTTP request options
*/
nodeOptions: object;
/**
* HTTP headers to send with requests
*/
headers: object;
/**
* URL path for HTTP requests
*/
path: string;
/**
* Use HTTPS instead of HTTP
*/
https: boolean;
}Usage Examples:
// Basic HTTP connection
const httpConnection = thrift.createHttpConnection('api.example.com', 80, {
path: '/thrift',
headers: {
'Content-Type': 'application/x-thrift',
'Authorization': 'Bearer token123'
},
transport: thrift.TBufferedTransport,
protocol: thrift.TJSONProtocol
});
// HTTPS connection with custom options
const httpsConnection = thrift.createHttpConnection('secure-api.example.com', 443, {
path: '/api/v1/thrift',
https: true,
headers: {
'User-Agent': 'MyApp/1.0',
'Accept': 'application/x-thrift'
},
nodeOptions: {
timeout: 30000,
keepAlive: true
}
});
// HTTP over Unix Domain Socket
const httpUdsConnection = thrift.createHttpUDSConnection('/tmp/http-thrift.sock', {
path: '/service',
headers: { 'X-Service-Version': '2.0' }
});WebSocket connections for real-time, bidirectional communication with support for secure WebSockets (WSS) and custom headers.
/**
* Create WebSocket connection to a Thrift server
* @param host - Server hostname or IP address
* @param port - Server port number
* @param options - WebSocket connection configuration options
* @returns WSConnection instance
*/
function createWSConnection(host, port, options): WSConnection;
/**
* Create WebSocket client with service binding
* @param ServiceClient - Generated service client class
* @param connection - WebSocket connection instance
* @returns Bound service client
*/
function createWSClient(ServiceClient, connection): Client;
/**
* WebSocket Connection class
*/
class WSConnection extends EventEmitter {
constructor(host, port, options);
/**
* Check if WebSocket connection is currently open
* @returns true if connection is open
*/
isOpen(): boolean;
/**
* Open the WebSocket connection
*/
open(): void;
/**
* Close the WebSocket connection
* @param code - WebSocket close code
* @param reason - Close reason string
*/
close(code?, reason?): void;
/**
* Write data via WebSocket
* @param data - Data to write
*/
write(data): void;
/**
* Use secure WebSocket (WSS)
*/
secure: boolean;
/**
* WebSocket headers
*/
headers: object;
/**
* WebSocket-specific options
*/
wsOptions: object;
}Usage Examples:
// Basic WebSocket connection
const wsConnection = thrift.createWSConnection('localhost', 8080, {
transport: thrift.TFramedTransport,
protocol: thrift.TBinaryProtocol,
wsOptions: {
perMessageDeflate: false
}
});
// Secure WebSocket with authentication
const wssConnection = thrift.createWSConnection('secure.example.com', 8443, {
secure: true,
headers: {
'Authorization': 'Bearer jwt-token',
'X-Client-Version': '1.2.3'
},
wsOptions: {
rejectUnauthorized: true,
ca: fs.readFileSync('ca-cert.pem')
}
});
// WebSocket event handling
wsConnection.on('open', () => {
console.log('WebSocket connected');
});
wsConnection.on('message', (data) => {
console.log('Received WebSocket message');
});
wsConnection.on('close', (code, reason) => {
console.log(`WebSocket closed: ${code} - ${reason}`);
});XMLHttpRequest connections optimized for browser environments with CORS support and configurable request options.
/**
* Create XMLHttpRequest connection for browser environments
* @param host - Server hostname or IP address
* @param port - Server port number
* @param options - XHR connection configuration options
* @returns XHRConnection instance
*/
function createXHRConnection(host, port, options): XHRConnection;
/**
* Create XHR client with service binding
* @param ServiceClient - Generated service client class
* @param connection - XHR connection instance
* @returns Bound service client
*/
function createXHRClient(ServiceClient, connection): Client;
/**
* XMLHttpRequest Connection class for browser environments
*/
class XHRConnection {
constructor(host, port, options);
/**
* Check if XHR connection is ready
* @returns true if connection is ready
*/
isOpen(): boolean;
/**
* Open the XHR connection
*/
open(): void;
/**
* Close the XHR connection
*/
close(): void;
/**
* Write data via XMLHttpRequest
* @param data - Data to write
*/
write(data): void;
/**
* HTTP headers for XHR requests
*/
headers: object;
/**
* Use HTTPS for requests
*/
https: boolean;
/**
* URL path for XHR requests
*/
path: string;
}Generic client factory functions that work with any connection type for maximum flexibility.
/**
* Create generic Thrift client for any connection type
* @param ServiceClient - Generated service client class
* @param connection - Any connection instance
* @returns Bound service client
*/
function createClient(ServiceClient, connection): Client;
/**
* Create STDIO client with service binding
* @param ServiceClient - Generated service client class
* @param connection - STDIO connection instance
* @returns Bound service client
*/
function createStdIOClient(ServiceClient, connection): Client;// Common connection options interface
interface ConnectionOptions {
/** Transport class to use */
transport?: TransportConstructor;
/** Protocol class to use */
protocol?: ProtocolConstructor;
/** Connection timeout in milliseconds */
timeout?: number;
/** Enable debug logging */
debug?: boolean;
/** Maximum connection attempts */
max_attempts?: number;
/** Maximum retry delay in milliseconds */
retry_max_delay?: number;
/** Connection timeout in milliseconds */
connect_timeout?: number;
}
// HTTP-specific options
interface HttpConnectionOptions extends ConnectionOptions {
/** URL path for HTTP requests */
path?: string;
/** HTTP headers to send */
headers?: object;
/** Use HTTPS instead of HTTP */
https?: boolean;
/** Node.js HTTP request options */
nodeOptions?: object;
}
// WebSocket-specific options
interface WSConnectionOptions extends ConnectionOptions {
/** Use secure WebSocket (WSS) */
secure?: boolean;
/** WebSocket headers */
headers?: object;
/** WebSocket-specific options */
wsOptions?: object;
}
// SSL/TLS options
interface SSLConnectionOptions extends ConnectionOptions {
/** Private key for client certificate */
key?: Buffer | string;
/** Client certificate */
cert?: Buffer | string;
/** Certificate Authority certificates */
ca?: Buffer | string | Array<Buffer | string>;
/** Reject unauthorized certificates */
rejectUnauthorized?: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-thrift