npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@sailboat-computer/data-storage-client

v1.1.56

Published

Client library for the sailboat computer data storage service

Downloads

46

Readme

Data Storage Client

A client library for the Sailboat Computer Data Storage Service with resilience features and offline support.

Features

  • Simplified API: Easy-to-use interface for storing and retrieving data
  • Caching: LRU cache with TTL for frequently accessed data
  • Resilience: Circuit breakers, retries, and timeouts for API calls
  • Offline Support: Local storage fallback when service is unavailable
  • Real-time Updates: WebSocket support for data change notifications
  • Type Safety: Full TypeScript support with generated types

Installation

npm install @sailboat-computer/data-storage-client

Quick Start

import { createDataStorageClient } from '@sailboat-computer/data-storage-client';

// Create a client
const client = createDataStorageClient({
  serviceUrl: 'http://localhost:3000',
  cache: {
    enabled: true,
    maxItems: 1000,
    defaultTtlMs: 60000, // 1 minute
  },
  offline: {
    enabled: true,
    maxBufferSize: 1000,
    syncIntervalMs: 60000, // 1 minute
  },
});

// Initialize the client
await client.initialize();

// Store data
const id = await client.store({ value: 42 }, 'sensor-readings');

// Retrieve data
const data = await client.retrieve({ id });

// Update data
await client.update({
  data: { value: 43 },
  metadata: {
    id,
    category: 'sensor-readings',
    timestamp: new Date(),
  },
});

// Delete data
await client.delete(id);

// Subscribe to data changes
const subscriptionId = client.subscribe('sensor-readings', (data) => {
  console.log('Data changed:', data);
});

// Unsubscribe from data changes
client.unsubscribe(subscriptionId);

// Close the client
await client.close();

Configuration Options

Client Options

interface DataStorageClientOptions {
  // Service URL
  serviceUrl: string;
  
  // Authentication token
  authToken?: string;
  
  // Local storage directory
  localDir?: string;
  
  // Cache options
  cache?: {
    // Whether to enable caching
    enabled: boolean;
    
    // Maximum cache size in items
    maxItems: number;
    
    // Default TTL in milliseconds
    defaultTtlMs: number;
    
    // Category-specific TTLs
    categoryTtls?: Record<string, number>;
  };
  
  // Resilience options
  resilience?: {
    // Circuit breaker options
    circuitBreaker?: {
      // Failure threshold before opening circuit
      failureThreshold: number;
      
      // Reset timeout in milliseconds
      resetTimeoutMs: number;
    };
    
    // Retry options
    retry?: {
      // Maximum number of retries
      maxRetries: number;
      
      // Base delay in milliseconds
      baseDelayMs: number;
    };
    
    // Timeout options
    timeout?: {
      // Default timeout in milliseconds
      defaultTimeoutMs: number;
    };
  };
  
  // Offline options
  offline?: {
    // Whether to enable offline mode
    enabled: boolean;
    
    // Maximum offline buffer size
    maxBufferSize: number;
    
    // Sync interval in milliseconds
    syncIntervalMs: number;
  };
}

Storage Options

interface StorageOptions {
  // Data timestamp
  timestamp?: Date;
  
  // Data tags
  tags?: Record<string, string>;
  
  // Storage tier
  tier?: StorageTier;
  
  // Data priority
  priority?: 'low' | 'normal' | 'high' | 'critical';
  
  // Time to live in seconds
  ttl?: number;
}

Data Query

interface DataQuery {
  // Data ID
  id?: string;
  
  // Data category
  category?: string;
  
  // Time range
  timeRange?: {
    // Start time
    start?: string;
    
    // End time
    end?: string;
  };
  
  // Sort options
  sort?: {
    // Sort field
    field: string;
    
    // Sort order
    order: 'asc' | 'desc';
  };
}

Advanced Usage

Offline Mode

The client automatically detects when the service is unavailable and switches to offline mode. In offline mode, all operations are stored locally and synchronized when the service becomes available again.

// Check if the client is in offline mode
const status = await client.getStatus();
console.log('Offline mode:', status.offline);

// Force synchronization
await client.forceSynchronization();

Caching

The client uses an LRU cache to store frequently accessed data. You can configure the cache size and TTL for different categories.

const client = createDataStorageClient({
  serviceUrl: 'http://localhost:3000',
  cache: {
    enabled: true,
    maxItems: 1000,
    defaultTtlMs: 60000, // 1 minute
    categoryTtls: {
      'sensor-readings': 30000, // 30 seconds
      'system-status': 300000, // 5 minutes
    },
  },
});

Real-time Updates

The client can subscribe to data changes using WebSockets.

// Subscribe to all changes in a category
const subscriptionId = client.subscribe('sensor-readings', (data) => {
  console.log('Data changed:', data);
});

// Unsubscribe when done
client.unsubscribe(subscriptionId);

Error Handling

The client uses a resilience framework to handle errors and recover from failures.

try {
  await client.store({ value: 42 }, 'sensor-readings');
} catch (error) {
  if (error.code === 'CIRCUIT_OPEN') {
    console.log('Circuit breaker is open, service is unavailable');
  } else if (error.code === 'TIMEOUT') {
    console.log('Operation timed out');
  } else if (error.code === 'NETWORK_ERROR') {
    console.log('Network error');
  } else {
    console.log('Unknown error:', error);
  }
}

Development

Building

npm run build

Testing

npm test

Linting

npm run lint

License

MIT