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

@codebundlesbyvik/ntp-sync

v1.1.3

Published

A basic Network Time Protocol synchronization library.

Readme

NTP Sync

npm npm - downloads per week

A basic Network Time Protocol synchronization library.

Table of Contents

  1. Usage
  2. Compatibility
  3. Instance options
  4. Methods
  5. License

Usage

If you're not familiar with NTP yet then you'll probably want to read up on what the values mean first.

# Install package from npm
npm install @codebundlesbyvik/ntp-sync

If you're not using a module bundler then:

For the example below I assume the main JavaScript file is processed by a module bundler.

import Ntp, { convertUnixTimeFormatToMs } from "@codebundlesbyvik/ntp-sync";

const ntp = new Ntp({
    t1EndpointUrl: "./api/ntp/get-server-time.php",
    t1CalcFn: async function (response: Response) {
        const fetchedData = (await response.json()) as unknown;

        const isValidData = (data: unknown): data is { received_time: number } =>
            typeof data === "object" && data !== null && "received_time" in data;

        return isValidData(fetchedData)
            ? convertUnixTimeFormatToMs(fetchedData.received_time)
            : null;
    },
    // Providing a t2CalcFn for greater accuracy is recommended but not required.
    t2CalcFn: function (responseHeaders: Headers) {
        // Header value example: t=1747777363406069 D=110
        const header = responseHeaders.get("Response-Timing");

        if (!header) return null;

        const reqReceivedTime = /\bt=([0-9]+)\b/.exec(header);
        const reqProcessingTime = /\bD=([0-9]+)\b/.exec(header);

        if (!reqReceivedTime || !reqProcessingTime) return null;

        const respTransmitTime =
            Number.parseInt(reqReceivedTime[1]) + Number.parseInt(reqProcessingTime[1]);

        return convertUnixTimeFormatToMs(respTransmitTime);
    },
});

const values = await this.ntp.sync();
// {
//     clientOffset: -296,
//     correctedDate: 1747828823908,
//     roundTripDelay: 591,
// }

Compatibility

Requires an ECMAScript 2022 (ES13) compatible environment. Practically speaking, all browsers released in 2021 and onwards are fully supported.

Instance options

| Property | Type | Default | Description | | :-------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------- | | t1EndpointUrl Required if t1Endpoint not provided. | string | - | URL of the endpoint to retrieve t1 from. | | t1Endpoint Required if t1EndpointUrl not provided. | @codebundlesbyvik/js-helpers fetchWithTimeout parameters | - | Parameters for the fetcher used to retrieve t1. | | t1CalcFn Required | (response: Response) => Promise<number \| null> | - | Function used to process t1. | | t2CalcFn | (responseHeaders: Headers) => number \| null | undefined | Function used for calculating t2. Recommended for greater precision. If not provided then t1 = t2. | | maxSyncAttempts | number | 6 | Maximum amount of t1 fetch requests when .sync() is called (i.e. the amount of times .generateData() is called). | | requiredOkSyncAttempts | number | 4 | Required amount of successful t1 fetch requests per .sync() call (i.e. the amount of .generateData() calls that must return a HTTP 200 status code). |

Methods

.generateData()

Returns an object containing round-trip delay and client offset.

.sync()

Calls .generateData() at most maxSyncAttempts times, calculates an average of each value and returns them alongside a corrected Unix timestamp.

A helper function convertUnixTimeFormatToMs() is also available and can be used to convert any format Unix timestamp dated after September 9th 2001 to one with millisecond precision.

License

MIT © 2025 Viktor Chin-Kon-Sung