Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,33 @@ jobs:
run: yarn build
- name: Run examples
run: node examples/example.js

deno:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20', '22', '24', '25']
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true
- name: Cache Node.js modules
id: yarn-cache
uses: actions/cache@v4
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}-${{ matrix.node-version }}-deno
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install
- name: Build
run: yarn build
- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: Run Deno integration test
run: deno run --allow-read test/integration/deno.ts
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ const defaultOptions = {
}
};
```

## Deno

`ntp-time-sync` is compatible with Deno. Use the `npm:` specifier to import the package:

```ts
// Run: deno run --allow-net your-script.ts
import { NtpTimeSync } from "npm:ntp-time-sync";

const timeSync = NtpTimeSync.getInstance();
const result = await timeSync.getTime();

console.log("system time", new Date());
console.log("ntp time", result.now);
console.log("offset (ms)", result.offset);
```
11 changes: 11 additions & 0 deletions examples/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Example: using ntp-time-sync with Deno
// Run: deno run --allow-net examples/deno.ts
import { NtpTimeSync } from "npm:ntp-time-sync";

const timeSync = NtpTimeSync.getInstance();

console.log("system time", new Date());
const result = await timeSync.getTime();
console.log("ntp time", result.now);
console.log("offset (ms)", result.offset);
console.log("precision (ms)", result.precision);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.5.0",
"description": "Fetches the current time from NTP servers and returns offset information",
"main": "dist/index.js",
"type": "commonjs",
"dependencies": {
"ntp-packet-parser": "^0.5.0"
},
Expand Down
4 changes: 3 additions & 1 deletion src/NtpTimeSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ export class NtpTimeSync {

// @see https://quickref.me/check-if-a-value-is-a-plain-object.html
private static isPlainObject(v: any): boolean {
return !!v && typeof v === "object" && (v.__proto__ === null || v.__proto__ === Object.prototype);
if (!v || typeof v !== "object") return false;
const proto = Object.getPrototypeOf(v);
return proto === null || proto === Object.prototype;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions test/integration/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Deno integration test — verifies ntp-time-sync works without --unstable-unsafe-proto
// Run: deno run test/integration/deno.ts

import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { NtpTimeSync } = require("../../dist/index.js");

// Instantiate with nested options to exercise recursiveResolveOptions → isPlainObject
new NtpTimeSync({
sampleCount: 4,
ntpDefaults: {
minPoll: 4,
maxPoll: 10,
},
});

// Verify singleton
const a = NtpTimeSync.getInstance();
const b = NtpTimeSync.getInstance();
if (a !== b) {
throw new Error("Singleton check failed: expected same instance");
}

console.log("✓ NtpTimeSync imported and instantiated");
console.log("✓ Nested options merged without __proto__ issues");
console.log("✓ Singleton pattern works");
Loading
Loading