Node.js/TypeScript SDK
Installation
npm install tollmeshcache
# or
yarn add tollmeshcache
Quick Start
import { Client, ClientConfig } from 'tollmeshcache';
const config = new ClientConfig({
host: 'localhost',
port: 8080,
});
const client = new Client(config);
// Rate limiting
const result = await client.consume('user-123', 100, 60000);
if (result.ok) {
console.log('Request allowed');
}
// Async/await
const value = await client.cacheGet('namespace', 'key');
Features
- ✅ Full TypeScript support
- ✅ Async/await patterns
- ✅ Streaming for large values
- ✅ Type-safe error handling
- ✅ Connection pooling
API Reference
This section covers rate limiting, replay protection, and caching. This SDK also has full, identical-API support for Job Queues, Sorted Sets, Streams, Pub/Sub, Transactions, Persistence, Pipelines, WASM Scripting, Search, Ranking, and Metrics — see the full API Reference for all of them, with live-verified examples.
Rate Limiting
const result = await client.consume(key, limit, windowMs);
// Returns: { ok: boolean, remaining: number, reset_at: number }
Replay Protection
const result = await client.seen(key, ttlMs);
// Returns: { seen: boolean }
Caching
await client.cacheSet(namespace, key, value, ttlMs);
const [value, exists] = await client.cacheGet(namespace, key);
Health
const health = await client.health();
const peers = await client.getPeers();
Examples
See examples/ for:
rate-limiting.ts- Rate limiting scenarioscaching.ts- Cache-aside patternsreplay-protection.ts- Replay detection
Error Handling
import { TollMeshError, RateLimitError, isRetryable } from 'tollmeshcache';
try {
const result = await client.consume('key', 100, 60000);
} catch (error) {
if (error instanceof RateLimitError) {
console.log('Rate limited');
} else if (error instanceof TollMeshError) {
console.log(`Error ${error.code}: ${error.message}`);
}
}
Configuration
const config = new ClientConfig({
host: 'localhost',
port: 8080,
timeout: 5000,
verifySsl: true,
apiKey: 'optional-key',
httpScheme: 'http',
maxRetries: 3,
retryBackoff: 1.0,
connectionPoolSize: 10,
});
Streaming
For large cache values:
import { streamToBuffer, bufferToStream } from 'tollmeshcache';
// Read streaming response
const buffer = await streamToBuffer(response);
// Write streaming request
const stream = bufferToStream(largeBuffer);
Testing
npm install --save-dev jest ts-jest @types/jest
npm test