- Multi-platform Support: Works in browsers, Node.js, Service Workers, and Edge Workers (Cloudflare Workers, etc.)
- Zero Dependencies: Lightweight with no external dependencies
- Functional Style: Pure functions without classes
- Time-sortable: IDs are chronologically sortable
- High Performance: Fast ID generation with built-in sequence handling
- TypeScript: Full type support with comprehensive type definitions
- Service Worker Support: Generate IDs in background contexts without blocking the main thread
npm install @tknf/snowflakeimport { createSnowflake, generateSnowflakeId } from '@tknf/snowflake';
// Create a generator function
const generateId = createSnowflake();
const id1 = generateId(); // "1234567890123456789"
const id2 = generateId(); // "1234567890123456790"
// Or generate a single ID
const id = generateSnowflakeId(); // "1234567890123456791"import { createSnowflake } from '@tknf/snowflake';
const generateId = createSnowflake({
epoch: 1640995200000, // Custom epoch (2022-01-01)
datacenterId: 1, // Datacenter ID (0-31)
workerId: 5 // Worker ID (0-31)
});
const id = generateId();// src/node.ts - Node.js specific utilities
import { createSnowflakeFromEnv, generateFromEnv } from '@tknf/snowflake/node';
// Set environment variables
process.env.SNOWFLAKE_EPOCH = '1640995200000';
process.env.SNOWFLAKE_DATACENTER_ID = '1';
process.env.SNOWFLAKE_WORKER_ID = '5';
// Create generator from environment
const generateId = createSnowflakeFromEnv();
const id1 = generateId();
// Or generate single ID from environment
const id2 = generateFromEnv();
// Override environment with custom config
const id3 = generateFromEnv({ workerId: 10 });// src/browser.ts - Browser specific utilities
import {
createSnowflakeFromStorage,
generateFromStorage,
initializeBrowserConfig,
saveConfigToStorage
} from '@tknf/snowflake/browser';
// Initialize browser-specific config (auto-generated from browser characteristics)
const config = initializeBrowserConfig({
save: true, // Save to localStorage
config: { epoch: 1640995200000 } // Optional custom config
});
// Create generator using localStorage + browser fingerprint
const generateId = createSnowflakeFromStorage();
const id1 = generateId();
// Or generate single ID from storage
const id2 = generateFromStorage();
// Manual config management
saveConfigToStorage({ datacenterId: 5, workerId: 10 });
const id3 = generateFromStorage();Creates a Snowflake ID generator function.
Parameters:
config(optional): Configuration objectepoch(number): Custom epoch timestamp in milliseconds (default: 2020-01-01)datacenterId(number): Datacenter ID (0-31, default: 0)workerId(number): Worker ID (0-31, default: 0)
Returns: Function that generates Snowflake IDs as strings
const generator = createSnowflake({
epoch: 1577836800000,
datacenterId: 1,
workerId: 2
});
const id = generator(); // "1234567890123456789"Generates a single Snowflake ID.
Parameters:
config(optional): Same ascreateSnowflake
Returns: Snowflake ID as string
const id = generateSnowflakeId({
datacenterId: 1,
workerId: 2
});Parses a Snowflake ID into its components.
Parameters:
id(string): Snowflake ID to parseepoch(number, optional): Epoch used for generation (default: 2020-01-01)
Returns: Object containing:
timestamp(number): Generation timestampdatacenterId(number): Datacenter IDworkerId(number): Worker IDsequence(number): Sequence numberdate(Date): Generation date
const parsed = parseSnowflakeId("1234567890123456789");
console.log(parsed);
// {
// timestamp: 1640995200123,
// datacenterId: 1,
// workerId: 2,
// sequence: 0,
// date: 2022-01-01T00:00:00.123Z
// }Extracts timestamp from a Snowflake ID.
Parameters:
id(string): Snowflake IDepoch(number, optional): Epoch used for generation
Returns: Timestamp in milliseconds
const timestamp = getSnowflakeTimestamp("1234567890123456789");
console.log(timestamp); // 1640995200123Converts a Snowflake ID to a Date object.
Parameters:
id(string): Snowflake IDepoch(number, optional): Epoch used for generation
Returns: Date object
const date = snowflakeToDate("1234567890123456789");
console.log(date); // 2022-01-01T00:00:00.123ZImport from @tknf/snowflake/node for Node.js-specific features:
SNOWFLAKE_EPOCH: Custom epoch timestampSNOWFLAKE_DATACENTER_ID: Datacenter ID (0-31)SNOWFLAKE_WORKER_ID: Worker ID (0-31)
Loads configuration from environment variables.
const config = loadConfigFromEnv();
// Returns SnowflakeConfig object based on environment variablesCreates a generator with environment configuration.
const generator = createSnowflakeFromEnv({ workerId: 5 });Generates a single ID with environment configuration.
const id = generateFromEnv({ datacenterId: 2 });Import from @tknf/snowflake/browser for browser-specific features:
snowflake.epoch: Custom epoch timestampsnowflake.datacenterId: Datacenter ID (0-31)snowflake.workerId: Worker ID (0-31)
Generates browser-specific configuration based on browser characteristics (user agent, timezone, screen properties, etc.).
const config = generateBrowserConfig();
// Returns consistent config for the same browser environmentLoads configuration from localStorage.
const config = loadConfigFromStorage();
// Returns SnowflakeConfig object from localStorageSaves configuration to localStorage.
saveConfigToStorage({ datacenterId: 5, workerId: 10 });Creates a generator with localStorage configuration and browser fingerprinting fallback.
const generator = createSnowflakeFromStorage({ epoch: 1640995200000 });Generates a single ID with localStorage configuration.
const id = generateFromStorage({ workerId: 15 });Initializes browser-specific configuration with optional localStorage persistence.
Parameters:
options.save(boolean): Whether to save to localStorage (default: false)options.config(SnowflakeConfig): Custom configuration to merge
const config = initializeBrowserConfig({
save: true,
config: { epoch: 1640995200000 }
});Clears all stored configuration from localStorage.
clearStoredConfig();Gets detailed browser fingerprint for debugging purposes.
const fingerprint = getBrowserFingerprint();
// Returns object with datacenterId, workerId, timezone, userAgent, screen info, etc.Snowflake IDs are 64-bit integers composed of:
1 bit | 41 bits | 5 bits | 5 bits | 12 bits
unused | timestamp | datacenter| worker | sequence
- Timestamp (41 bits): Milliseconds since custom epoch
- Datacenter ID (5 bits): Identifies the datacenter (0-31)
- Worker ID (5 bits): Identifies the worker process (0-31)
- Sequence (12 bits): Counter for same-millisecond generation (0-4095)
const generator = createSnowflake({ workerId: 1 });
// Generate 1000 IDs quickly
const ids = [];
for (let i = 0; i < 1000; i++) {
ids.push(generator());
}
// All IDs are unique and sortable
console.log(ids.length === new Set(ids).size); // trueconst worker1 = createSnowflake({ workerId: 1 });
const worker2 = createSnowflake({ workerId: 2 });
const id1 = worker1(); // From worker 1
const id2 = worker2(); // From worker 2
// IDs from different workers are still unique
console.log(id1 !== id2); // true// React/Vue/etc. application
import { initializeBrowserConfig, createSnowflakeFromStorage } from '@tknf/snowflake/browser';
// Initialize on app startup
const config = initializeBrowserConfig({
save: true, // Persist across browser sessions
config: { epoch: 1640995200000 }
});
const generator = createSnowflakeFromStorage();
// Use in your app
function createNewRecord() {
const id = generator();
console.log('New record ID:', id);
return id;
}// Service Worker example
// sw.js
importScripts('https://unpkg.com/@tknf/snowflake/dist/snowflake.min.js');
self.addEventListener('message', (event) => {
if (event.data.type === 'GENERATE_ID') {
const generator = Snowflake.createSnowflake({
datacenterId: 1,
workerId: 5
});
const id = generator();
// Send back to main thread
self.clients.matchAll().then(clients => {
for (const client of clients) {
client.postMessage({
type: 'ID_GENERATED',
data: { id }
});
}
});
}
});
// Main thread
navigator.serviceWorker.controller.postMessage({
type: 'GENERATE_ID'
});// Cloudflare Worker example
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const generator = createSnowflake({
datacenterId: 1,
workerId: parseInt(env.WORKER_ID) || 0
});
const id = generator();
return new Response(JSON.stringify({ id }), {
headers: { 'Content-Type': 'application/json' }
});
}
};import { getBrowserFingerprint, generateBrowserConfig } from '@tknf/snowflake/browser';
// Get detailed browser information
const fingerprint = getBrowserFingerprint();
console.log('Browser fingerprint:', fingerprint);
// Consistent config for same browser
const config1 = generateBrowserConfig();
const config2 = generateBrowserConfig();
console.log(config1.workerId === config2.workerId); // true - same browser = same workerIdThe library includes comprehensive examples for different environments:
examples/iife/: IIFE browser example with localStorage integrationexamples/node/: Node.js example with environment variablesexamples/worker/: Service Worker example with background generation
Each example includes:
- Complete working code
- Performance testing
- Error handling
- Detailed README with setup instructions
# IIFE Browser Example
cd examples/iife
pnpm install
pnpm run start
# Node.js Example
cd examples/node
pnpm install
pnpm run start
# Service Worker Example
cd examples/worker
pnpm install
pnpm run startgit clone git@github.com:tknf/snowflake.git
cd snowflake
pnpm install# Build the library
pnpm run build
# Run tests
pnpm run test
# Run tests with coverage
pnpm run test:coverage
# Lint code
pnpm run lint
# Format code
pnpm run format
# Type check
pnpm run typechecksrc/
βββ index.ts # Core Snowflake logic (platform-agnostic)
βββ node.ts # Node.js-specific utilities
βββ browser.ts # Browser-specific utilities
βββ iife.ts # IIFE build for browser usage
βββ index.test.ts # Core functionality tests
βββ node.test.ts # Node.js utilities tests
βββ browser.test.ts # Browser utilities tests
examples/
βββ iife/ # IIFE browser example
βββ node/ # Node.js example
βββ worker/ # Service Worker example
- Runtime: No runtime dependencies
- Development: Node.js 18+ for building and testing
- TypeScript: Full type support included
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Ensure tests pass:
pnpm run test - Ensure linting passes:
pnpm run lint - Submit a pull request
This implementation follows the distributed ID generation pattern, designed for high-performance unique ID generation in distributed systems.
