ntpclient 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/{cli.js → dist/cjs/cli.js} +8 -15
- package/dist/cjs/cli.js.map +1 -0
- package/{index.js → dist/cjs/index.js} +2 -6
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/mjs/cli.d.ts +2 -0
- package/dist/mjs/cli.js +27 -0
- package/dist/mjs/cli.js.map +1 -0
- package/dist/mjs/index.d.ts +15 -0
- package/dist/mjs/index.js +93 -0
- package/dist/mjs/index.js.map +1 -0
- package/dist/mjs/package.json +3 -0
- package/package.json +21 -19
- package/src/cli.ts +9 -9
- package/src/index.ts +1 -1
- package/cli.js.map +0 -1
- package/index.js.map +0 -1
- /package/{cli.d.ts → dist/cjs/cli.d.ts} +0 -0
- /package/{index.d.ts → dist/cjs/index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -2,9 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
TypeScript implementation of the NTP Client Protocol. Based on [node-ntp-client](https://github.com/moonpyk/node-ntp-client).
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Prerequisites
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- [Node.js](https://nodejs.org) >= 14
|
|
8
|
+
- npm (preinstalled) or [yarn](https://classic.yarnpkg.com) < 2
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
ℹ️ This is a hybrid [CommonJS](https://nodejs.org/docs/latest/api/modules.html#modules-commonjs-modules) / [ESM](https://nodejs.org/api/esm.html#introduction) module.
|
|
13
|
+
|
|
14
|
+
Run `yarn add ntpclient` or `npm i ntpclient`.
|
|
8
15
|
|
|
9
16
|
```ts
|
|
10
17
|
import {NTPClient} from 'ntpclient';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -9,18 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
9
|
});
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const packageJsonPath = fs.existsSync(defaultPackageJsonPath)
|
|
19
|
-
? defaultPackageJsonPath
|
|
20
|
-
: path.join(__dirname, '../package.json');
|
|
21
|
-
const packageJson = fs.readFileSync(packageJsonPath, 'utf-8');
|
|
22
|
-
const { bin, description, version } = JSON.parse(packageJson);
|
|
23
|
-
commander_1.program
|
|
11
|
+
import { program as commander } from 'commander';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
import { NTPClient } from './index.js';
|
|
15
|
+
const { bin, description, version } = require('../package.json');
|
|
16
|
+
commander
|
|
24
17
|
.name(Object.keys(bin)[0])
|
|
25
18
|
.version(version)
|
|
26
19
|
.description(description)
|
|
@@ -28,10 +21,10 @@ commander_1.program
|
|
|
28
21
|
.option('-p, --port <number>', 'Specify a custom NTP port')
|
|
29
22
|
.option('-t, --timeout <number>', 'Specify the timeout in milliseconds')
|
|
30
23
|
.parse(process.argv);
|
|
31
|
-
const commanderOptions =
|
|
24
|
+
const commanderOptions = commander.opts();
|
|
32
25
|
void (() => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
26
|
try {
|
|
34
|
-
const date = yield new
|
|
27
|
+
const date = yield new NTPClient(Object.assign(Object.assign(Object.assign({}, (commanderOptions.server && { server: commanderOptions.server })), (commanderOptions.port && { port: commanderOptions.port })), (commanderOptions.timeout && { replyTimeout: commanderOptions.timeout }))).getNetworkTime();
|
|
35
28
|
console.info(date.toString());
|
|
36
29
|
process.exit();
|
|
37
30
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";;;;;;;;;;AAEA,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAC1C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AAQrC,MAAM,EAAC,GAAG,EAAE,WAAW,EAAE,OAAO,EAAC,GAAgB,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE5E,SAAS;KACN,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;KACzB,OAAO,CAAC,OAAO,CAAC;KAChB,WAAW,CAAC,WAAW,CAAC;KACxB,MAAM,CAAC,qBAAqB,EAAE,6BAA6B,CAAC;KAC5D,MAAM,CAAC,qBAAqB,EAAE,2BAA2B,CAAC;KAC1D,MAAM,CAAC,wBAAwB,EAAE,qCAAqC,CAAC;KACvE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAEvB,MAAM,gBAAgB,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;AAE1C,KAAK,CAAC,GAAS,EAAE;IACf,IAAI;QACF,MAAM,IAAI,GAAG,MAAM,IAAI,SAAS,+CAC3B,CAAC,gBAAgB,CAAC,MAAM,IAAI,EAAC,MAAM,EAAE,gBAAgB,CAAC,MAAM,EAAC,CAAC,GAC9D,CAAC,gBAAgB,CAAC,IAAI,IAAI,EAAC,IAAI,EAAE,gBAAgB,CAAC,IAAI,EAAC,CAAC,GACxD,CAAC,gBAAgB,CAAC,OAAO,IAAI,EAAC,YAAY,EAAE,gBAAgB,CAAC,OAAO,EAAC,CAAC,EACzE,CAAC,cAAc,EAAE,CAAC;QAEpB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC9B,OAAO,CAAC,IAAI,EAAE,CAAC;KAChB;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;AACH,CAAC,CAAA,CAAC,EAAE,CAAC"}
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NTPClient = void 0;
|
|
4
1
|
/* eslint-disable no-magic-numbers */
|
|
5
|
-
|
|
2
|
+
import dgram from 'node:dgram';
|
|
6
3
|
const TEN_SECONDS_IN_MILLIS = 10000;
|
|
7
4
|
const defaultConfig = {
|
|
8
5
|
port: 123,
|
|
9
6
|
replyTimeout: TEN_SECONDS_IN_MILLIS,
|
|
10
7
|
server: 'pool.ntp.org',
|
|
11
8
|
};
|
|
12
|
-
class NTPClient {
|
|
9
|
+
export class NTPClient {
|
|
13
10
|
constructor(configOrServer, port, replyTimeout) {
|
|
14
11
|
this.config = defaultConfig;
|
|
15
12
|
if (typeof configOrServer === 'string') {
|
|
@@ -93,5 +90,4 @@ class NTPClient {
|
|
|
93
90
|
});
|
|
94
91
|
}
|
|
95
92
|
}
|
|
96
|
-
exports.NTPClient = NTPClient;
|
|
97
93
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,MAAM,qBAAqB,GAAG,KAAM,CAAC;AAWrC,MAAM,aAAa,GAAwB;IACzC,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K,EAAE,EAAE;wBACvC,SAAS,GAAG,GAAG,GAAG,SAAS,GAAG,GAAG,CAAC,kBAAkB,GAAG,KAAK,CAAC,CAAC;qBAC/D;oBAED,MAAM,YAAY,GAAG,OAAO,GAAG,IAAI,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,UAAa,CAAC;oBAEzE,eAAe;oBACf,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;oBACzC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,YAAY,CAAC,CAAC;oBAElE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/dist/mjs/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { program as commander } from 'commander';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
import { NTPClient } from './index.js';
|
|
6
|
+
const { bin, description, version } = require('../package.json');
|
|
7
|
+
commander
|
|
8
|
+
.name(Object.keys(bin)[0])
|
|
9
|
+
.version(version)
|
|
10
|
+
.description(description)
|
|
11
|
+
.option('-s, --server <host>', 'Specify a custom NTP server')
|
|
12
|
+
.option('-p, --port <number>', 'Specify a custom NTP port')
|
|
13
|
+
.option('-t, --timeout <number>', 'Specify the timeout in milliseconds')
|
|
14
|
+
.parse(process.argv);
|
|
15
|
+
const commanderOptions = commander.opts();
|
|
16
|
+
void (async () => {
|
|
17
|
+
try {
|
|
18
|
+
const date = await new NTPClient(Object.assign(Object.assign(Object.assign({}, (commanderOptions.server && { server: commanderOptions.server })), (commanderOptions.port && { port: commanderOptions.port })), (commanderOptions.timeout && { replyTimeout: commanderOptions.timeout }))).getNetworkTime();
|
|
19
|
+
console.info(date.toString());
|
|
20
|
+
process.exit();
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error(error);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
})();
|
|
27
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAC1C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AAQrC,MAAM,EAAC,GAAG,EAAE,WAAW,EAAE,OAAO,EAAC,GAAgB,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE5E,SAAS;KACN,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;KACzB,OAAO,CAAC,OAAO,CAAC;KAChB,WAAW,CAAC,WAAW,CAAC;KACxB,MAAM,CAAC,qBAAqB,EAAE,6BAA6B,CAAC;KAC5D,MAAM,CAAC,qBAAqB,EAAE,2BAA2B,CAAC;KAC1D,MAAM,CAAC,wBAAwB,EAAE,qCAAqC,CAAC;KACvE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAEvB,MAAM,gBAAgB,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;AAE1C,KAAK,CAAC,KAAK,IAAI,EAAE;IACf,IAAI;QACF,MAAM,IAAI,GAAG,MAAM,IAAI,SAAS,+CAC3B,CAAC,gBAAgB,CAAC,MAAM,IAAI,EAAC,MAAM,EAAE,gBAAgB,CAAC,MAAM,EAAC,CAAC,GAC9D,CAAC,gBAAgB,CAAC,IAAI,IAAI,EAAC,IAAI,EAAE,gBAAgB,CAAC,IAAI,EAAC,CAAC,GACxD,CAAC,gBAAgB,CAAC,OAAO,IAAI,EAAC,YAAY,EAAE,gBAAgB,CAAC,OAAO,EAAC,CAAC,EACzE,CAAC,cAAc,EAAE,CAAC;QAEpB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC9B,OAAO,CAAC,IAAI,EAAE,CAAC;KAChB;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;AACH,CAAC,CAAC,EAAE,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface NTPConfig {
|
|
2
|
+
/** Remote NTP Server port number */
|
|
3
|
+
port?: number;
|
|
4
|
+
/** Amount of acceptable time to await for a response from the remote server. */
|
|
5
|
+
replyTimeout?: number;
|
|
6
|
+
/** IP/Hostname of the remote NTP Server */
|
|
7
|
+
server?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class NTPClient {
|
|
10
|
+
private readonly config;
|
|
11
|
+
constructor(server?: string, port?: number, timeout?: number);
|
|
12
|
+
constructor(config?: NTPConfig);
|
|
13
|
+
/** Fetches the current NTP Time from the given server and port. */
|
|
14
|
+
getNetworkTime(ntpReplyTimeout?: number): Promise<Date>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
import dgram from 'node:dgram';
|
|
3
|
+
const TEN_SECONDS_IN_MILLIS = 10000;
|
|
4
|
+
const defaultConfig = {
|
|
5
|
+
port: 123,
|
|
6
|
+
replyTimeout: TEN_SECONDS_IN_MILLIS,
|
|
7
|
+
server: 'pool.ntp.org',
|
|
8
|
+
};
|
|
9
|
+
export class NTPClient {
|
|
10
|
+
constructor(configOrServer, port, replyTimeout) {
|
|
11
|
+
this.config = defaultConfig;
|
|
12
|
+
if (typeof configOrServer === 'string') {
|
|
13
|
+
this.config.server = configOrServer;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this.config = Object.assign(Object.assign({}, this.config), configOrServer);
|
|
17
|
+
}
|
|
18
|
+
if (port) {
|
|
19
|
+
this.config.port = port;
|
|
20
|
+
}
|
|
21
|
+
if (replyTimeout) {
|
|
22
|
+
this.config.replyTimeout = replyTimeout;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Fetches the current NTP Time from the given server and port. */
|
|
26
|
+
getNetworkTime(ntpReplyTimeout) {
|
|
27
|
+
if (ntpReplyTimeout) {
|
|
28
|
+
this.config.replyTimeout = ntpReplyTimeout;
|
|
29
|
+
}
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const client = dgram.createSocket('udp4');
|
|
32
|
+
const ntpData = Buffer.alloc(48);
|
|
33
|
+
// RFC 2030 -> LI = 0 (no warning, 2 bits), VN = 3 (IPv4 only, 3 bits), Mode = 3 (Client Mode, 3 bits) -> 1 byte
|
|
34
|
+
// -> rtol(LI, 6) ^ rotl(VN, 3) ^ rotl(Mode, 0)
|
|
35
|
+
// -> = 0x00 ^ 0x18 ^ 0x03
|
|
36
|
+
ntpData[0] = 0x1b;
|
|
37
|
+
const timeout = setTimeout(() => {
|
|
38
|
+
client.close();
|
|
39
|
+
reject(new Error('Timeout waiting for NTP response.'));
|
|
40
|
+
errorFired = true;
|
|
41
|
+
}, this.config.replyTimeout);
|
|
42
|
+
/*
|
|
43
|
+
* Some errors can happen before/after send() or cause send() to break.
|
|
44
|
+
* Some errors will also be given to send()
|
|
45
|
+
* NOTE: the error rejection is not generalised, as the client has to
|
|
46
|
+
* lose the connection also, apparently.
|
|
47
|
+
*/
|
|
48
|
+
let errorFired = false;
|
|
49
|
+
client.on('error', err => {
|
|
50
|
+
if (errorFired) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
errorFired = true;
|
|
54
|
+
clearTimeout(timeout);
|
|
55
|
+
return reject(err);
|
|
56
|
+
});
|
|
57
|
+
client.send(ntpData, 0, ntpData.length, this.config.port, this.config.server, err => {
|
|
58
|
+
if (err) {
|
|
59
|
+
if (errorFired) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
clearTimeout(timeout);
|
|
63
|
+
errorFired = true;
|
|
64
|
+
client.close();
|
|
65
|
+
return reject(err);
|
|
66
|
+
}
|
|
67
|
+
client.once('message', msg => {
|
|
68
|
+
clearTimeout(timeout);
|
|
69
|
+
client.close();
|
|
70
|
+
// Offset to get to the "Transmit Timestamp" field (time at which the reply
|
|
71
|
+
// departed the server for the client, in 64-bit timestamp format.
|
|
72
|
+
const offsetTransmitTime = 40;
|
|
73
|
+
let intpart = 0;
|
|
74
|
+
let fractpart = 0;
|
|
75
|
+
// Get the seconds part
|
|
76
|
+
for (let index = 0; index <= 3; index++) {
|
|
77
|
+
intpart = 256 * intpart + msg[offsetTransmitTime + index];
|
|
78
|
+
}
|
|
79
|
+
// Get the seconds fraction
|
|
80
|
+
for (let index = 4; index <= 7; index++) {
|
|
81
|
+
fractpart = 256 * fractpart + msg[offsetTransmitTime + index];
|
|
82
|
+
}
|
|
83
|
+
const milliseconds = intpart * 1000 + (fractpart * 1000) / 4294967296;
|
|
84
|
+
// **UTC** time
|
|
85
|
+
const date = new Date('Jan 01 1900 GMT');
|
|
86
|
+
date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds);
|
|
87
|
+
return resolve(date);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,MAAM,qBAAqB,GAAG,KAAM,CAAC;AAWrC,MAAM,aAAa,GAAwB;IACzC,IAAI,E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,EAAE;wBACvC,SAAS,GAAG,GAAG,GAAG,SAAS,GAAG,GAAG,CAAC,kBAAkB,GAAG,KAAK,CAAC,CAAC;qBAC/D;oBAED,MAAM,YAAY,GAAG,OAAO,GAAG,IAAI,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,UAAa,CAAC;oBAEzE,eAAe;oBACf,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;oBACzC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,YAAY,CAAC,CAAC;oBAElE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
3
|
"bin": {
|
|
4
|
-
"ntpclient": "cli.js"
|
|
4
|
+
"ntpclient": "dist/cjs/cli.js"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"commander": "10.0.
|
|
7
|
+
"commander": "10.0.1"
|
|
8
8
|
},
|
|
9
9
|
"description": "A TypeScript implementation of the NTP Client Protocol",
|
|
10
10
|
"devDependencies": {
|
|
@@ -12,32 +12,34 @@
|
|
|
12
12
|
"typescript": "5.0.4"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
15
|
+
"node": ">= 14.16"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/mjs/index.js",
|
|
20
|
+
"require": "./dist/cjs/index.js"
|
|
21
|
+
}
|
|
16
22
|
},
|
|
17
23
|
"files": [
|
|
18
|
-
"
|
|
19
|
-
"cli.js",
|
|
20
|
-
"cli.js.map",
|
|
21
|
-
"index.d.ts",
|
|
22
|
-
"index.js",
|
|
23
|
-
"index.js.map",
|
|
24
|
+
"dist",
|
|
24
25
|
"src"
|
|
25
26
|
],
|
|
26
27
|
"license": "GPL-3.0",
|
|
27
|
-
"main": "index.js",
|
|
28
|
+
"main": "dist/cjs/index.js",
|
|
29
|
+
"module": "dist/mjs/index.js",
|
|
28
30
|
"name": "ntpclient",
|
|
29
|
-
"publishConfig": {
|
|
30
|
-
"directory": "flattened"
|
|
31
|
-
},
|
|
32
31
|
"repository": "https://github.com/ffflorian/node-packages/tree/main/packages/ntpclient",
|
|
33
32
|
"scripts": {
|
|
34
|
-
"build": "
|
|
33
|
+
"build": "yarn build:cjs && yarn build:mjs && yarn generate:packagejson",
|
|
34
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
35
|
+
"build:mjs": "tsc -p tsconfig.json",
|
|
35
36
|
"clean": "rimraf dist",
|
|
36
37
|
"dist": "yarn clean && yarn build",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"test": "ts-node -P tsconfig.jasmine.json ../../node_modules/.bin/jasmine"
|
|
38
|
+
"generate:packagejson": "../../bin/generate-hybrid-package-json.sh",
|
|
39
|
+
"start": "ts-node-esm src/cli.ts",
|
|
40
|
+
"test": "ts-node-esm -P tsconfig.jasmine.json ../../node_modules/.bin/jasmine"
|
|
41
41
|
},
|
|
42
|
-
"
|
|
42
|
+
"type": "module",
|
|
43
|
+
"version": "1.4.1",
|
|
44
|
+
"gitHead": "fee6dde4396255259028c1c723a95b9f6ec5b281"
|
|
43
45
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import {program as commander} from 'commander';
|
|
4
|
-
import
|
|
5
|
-
|
|
4
|
+
import {createRequire} from 'node:module';
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
6
|
|
|
7
|
-
import {NTPClient} from './';
|
|
7
|
+
import {NTPClient} from './index.js';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
:
|
|
9
|
+
interface PackageJson {
|
|
10
|
+
bin: Record<string, string>;
|
|
11
|
+
description: string;
|
|
12
|
+
version: string;
|
|
13
|
+
}
|
|
13
14
|
|
|
14
|
-
const
|
|
15
|
-
const {bin, description, version} = JSON.parse(packageJson);
|
|
15
|
+
const {bin, description, version}: PackageJson = require('../package.json');
|
|
16
16
|
|
|
17
17
|
commander
|
|
18
18
|
.name(Object.keys(bin)[0])
|
package/src/index.ts
CHANGED
package/cli.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,yCAA+C;AAC/C,yBAAyB;AACzB,6BAA6B;AAE7B,yBAA6B;AAE7B,MAAM,sBAAsB,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;AACpE,MAAM,eAAe,GAAG,EAAE,CAAC,UAAU,CAAC,sBAAsB,CAAC;IAC3D,CAAC,CAAC,sBAAsB;IACxB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC;AAE5C,MAAM,WAAW,GAAG,EAAE,CAAC,YAAY,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;AAC9D,MAAM,EAAC,GAAG,EAAE,WAAW,EAAE,OAAO,EAAC,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AAE5D,mBAAS;KACN,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;KACzB,OAAO,CAAC,OAAO,CAAC;KAChB,WAAW,CAAC,WAAW,CAAC;KACxB,MAAM,CAAC,qBAAqB,EAAE,6BAA6B,CAAC;KAC5D,MAAM,CAAC,qBAAqB,EAAE,2BAA2B,CAAC;KAC1D,MAAM,CAAC,wBA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