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

ntp.js

v1.2.0

Published

A robust Network Time Protocol (NTP) client implementation for Node.js applications.

Readme

NTP.JS

A robust Network Time Protocol (NTP) client implementation for Node.js applications with high-precision time synchronization and clock drift compensation.

Features

  • High-precision time sync using performance.now() for sub-millisecond accuracy
  • Clock drift compensation via linear regression analysis
  • Burst synchronization on startup for faster initial time lock
  • IPv4/IPv6 support with automatic protocol detection
  • Outlier filtering to reject high-latency responses (>250ms RTT)
  • Automatic retry mechanism with configurable max retries
  • Event-driven architecture for real-time status updates
  • TypeScript support with full type definitions

Installation

npm install ntp.js
yarn add ntp.js
pnpm add ntp.js

Quick Start

import NTPClient, { NTP_EVENTS } from 'ntp.js';

const ntpClient = new NTPClient();

ntpClient.on(NTP_EVENTS.SYNC, (time) => {
  console.log('Synchronized time:', new Date(time));
});

ntpClient.on(NTP_EVENTS.ERROR, (error) => {
  console.error('NTP error:', error);
});

ntpClient.begin();

Usage Examples

Basic Usage

import NTPClient, { NTP_EVENTS } from 'ntp.js';

const ntpClient = new NTPClient({
  poolServerName: 'pool.ntp.org',
  port: 123,
  updateInterval: 60000, // Sync every 1 minute
});

ntpClient.on(NTP_EVENTS.SYNC, (time) => {
  console.log('Time synced:', new Date(time));
});

ntpClient.begin();

Getting Current Time

// After sync, get the current NTP time anytime
const currentTime = ntpClient.getTime();
console.log('Current NTP time:', new Date(currentTime));

// Check sync status before using time
if (ntpClient.getSyncStatus() === 'synced') {
  const accurateTime = ntpClient.getTime();
}

Monitoring Sync Status

ntpClient.on(NTP_EVENTS.SYNC_STATUS, (status) => {
  // status: 'syncing' | 'synced' | 'error'
  console.log('Sync status changed:', status);
});

Force Immediate Sync

// Trigger an immediate time sync
await ntpClient.forceUpdate();

Cleanup

// Stop the client and release resources
ntpClient.stop();

API Reference

Constructor

new NTPClient(options?: NTPClientOptions)

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | poolServerName | string | 'pool.ntp.org' | NTP server hostname | | port | number | 123 | NTP server port (1-65535) | | timeOffset | number | 0 | Additional time offset in milliseconds | | updateInterval | number | 60000 | Time between sync attempts in ms (must be > 0) | | maxRetries | number | 3 | Maximum retry attempts per sync cycle (must be ≥ 0) | | protocol | 'udp4' \| 'udp6' | 'udp4' | UDP protocol preference (auto-switches based on DNS) | | rttThreshold | number | 250 | RTT threshold in ms for outlier filtering (must be > 0) |

Methods

| Method | Returns | Description | |--------|---------|-------------| | begin() | void | Start the NTP client with burst sync on startup | | forceUpdate() | Promise<void> | Force an immediate time synchronization | | getTime() | number | Get current synchronized time (returns 0 if not synced) | | getSyncStatus() | SyncStatus | Get current sync status: 'syncing' | 'synced' | 'error' | | setTimeOffset(offset) | void | Set additional time offset in milliseconds | | setUpdateInterval(interval) | void | Update the sync interval in milliseconds | | stop() | void | Stop the client and close UDP connection |

Events

Use the NTP_EVENTS enum for type-safe event handling:

| Event | Callback Signature | Description | |-------|-------------------|-------------| | NTP_EVENTS.SYNC | (time: number) => void | Emitted when time is successfully synchronized. time is the synced timestamp in milliseconds. | | NTP_EVENTS.ERROR | (error: Error) => void | Emitted when an error occurs (DNS failure, max retries exceeded, etc.) | | NTP_EVENTS.SYNC_STATUS | (status: SyncStatus) => void | Emitted when sync status changes between 'syncing', 'synced', or 'error' |

// Using NTP_EVENTS enum (recommended)
ntpClient.on(NTP_EVENTS.SYNC, (time) => { /* ... */ });
ntpClient.on(NTP_EVENTS.ERROR, (error) => { /* ... */ });
ntpClient.on(NTP_EVENTS.SYNC_STATUS, (status) => { /* ... */ });

// Using string literals
ntpClient.on('sync', (time) => { /* ... */ });
ntpClient.on('error', (error) => { /* ... */ });
ntpClient.on('syncStatus', (status) => { /* ... */ });

Requirements

  • Node.js >= 16.0.0

License

MIT

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

Author

wraith4081 - GitHub

Contributors