@clockworkdog/timesync 2.11.0

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.
@@ -0,0 +1,2 @@
1
+ export * from './timesync';
2
+ export * from './setDate';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './timesync';
2
+ export * from './setDate';
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Patch `Date.now()` and `new Date()` given the current time is @param now
3
+ */
4
+ export declare function setDate(now: number): void;
@@ -0,0 +1,23 @@
1
+ // Adapted from https://stackoverflow.com/a/58325977/244640
2
+ const OriginalDateConstructor = globalThis.Date;
3
+ /**
4
+ * Patch `Date.now()` and `new Date()` given the current time is @param now
5
+ */
6
+ export function setDate(now) {
7
+ const nowDelta = now - OriginalDateConstructor.now();
8
+ function Date(...args) {
9
+ if (args.length === 0) {
10
+ return new OriginalDateConstructor(Date.now()); // Date.now() is implemented below
11
+ }
12
+ // Specific date constructor
13
+ return new OriginalDateConstructor(...args);
14
+ }
15
+ // copy all properties from the original date, this includes the prototype
16
+ const propertyDescriptors = Object.getOwnPropertyDescriptors(OriginalDateConstructor);
17
+ Object.defineProperties(Date, propertyDescriptors);
18
+ // override Date.now to return the adjusted time
19
+ Date.now = function () {
20
+ return OriginalDateConstructor.now() + nowDelta;
21
+ };
22
+ globalThis.Date = Date;
23
+ }
@@ -0,0 +1,28 @@
1
+ export interface TimeSyncRequestData {
2
+ timesync: {
3
+ id: number;
4
+ };
5
+ }
6
+ export interface TimeSyncResponseData {
7
+ timesync: {
8
+ id: number;
9
+ now: number;
10
+ };
11
+ }
12
+ export interface TimeSyncClient {
13
+ receive(data: TimeSyncResponseData): void;
14
+ destroy(): void;
15
+ }
16
+ export interface TimeSyncServer {
17
+ receive(data: TimeSyncRequestData): void;
18
+ }
19
+ export declare function createTimeSyncClient({ interval, send, onChange, syncSampleSize, syncRequestTimeout, }: {
20
+ interval: number;
21
+ send: (data: TimeSyncRequestData) => void | Promise<void>;
22
+ onChange?: (now: number) => void;
23
+ syncSampleSize?: number;
24
+ syncRequestTimeout?: number;
25
+ }): TimeSyncClient;
26
+ export declare function createTimeSyncServer({ send }: {
27
+ send: (data: TimeSyncResponseData) => void | Promise<void>;
28
+ }): TimeSyncServer;
@@ -0,0 +1,63 @@
1
+ import { setDate } from './setDate';
2
+ let id = 0;
3
+ function getId() {
4
+ return ++id;
5
+ }
6
+ const SYNC_SAMPLE_SIZE = 5;
7
+ const SYNC_REQUEST_TIMEOUT = 10000;
8
+ export function createTimeSyncClient({ interval, send, onChange = setDate, syncSampleSize = SYNC_SAMPLE_SIZE, syncRequestTimeout = SYNC_REQUEST_TIMEOUT, }) {
9
+ const requests = {};
10
+ async function synchronize() {
11
+ const promises = [];
12
+ // Send a series of requests
13
+ for (let i = 0; i < syncSampleSize; i++) {
14
+ const promise = new Promise((resolve) => {
15
+ const id = getId();
16
+ const sentAt = performance.now();
17
+ send({ timesync: { id } });
18
+ const complete = (receivedAt, serverNow) => resolve({ sentAt, receivedAt, serverNow, clientNow: Date.now() });
19
+ requests[id] = { complete };
20
+ setTimeout(() => resolve(null), syncRequestTimeout);
21
+ });
22
+ promises.push(promise);
23
+ await promise;
24
+ }
25
+ // Perform calculation with results
26
+ const results = await Promise.all(promises);
27
+ const deltas = results
28
+ .filter((result) => result !== null)
29
+ .map((result) => {
30
+ const { sentAt, receivedAt, serverNow, clientNow } = result;
31
+ const halfLatency = (receivedAt - sentAt) / 2;
32
+ return clientNow - serverNow + halfLatency;
33
+ });
34
+ const averageDelta = deltas.reduce((d1, d2) => d1 + d2, 0) / deltas.length;
35
+ if (!isNaN(averageDelta)) {
36
+ onChange(Date.now() + averageDelta);
37
+ }
38
+ }
39
+ const receive = (data) => {
40
+ const receivedAt = performance.now();
41
+ const req = requests[data.timesync.id];
42
+ if (!req)
43
+ return;
44
+ req.complete(receivedAt, data.timesync.now);
45
+ };
46
+ synchronize();
47
+ const loop = setInterval(synchronize, interval);
48
+ const destroy = () => {
49
+ clearInterval(loop);
50
+ };
51
+ return {
52
+ receive,
53
+ destroy,
54
+ };
55
+ }
56
+ export function createTimeSyncServer({ send }) {
57
+ return {
58
+ receive(data) {
59
+ const { id } = data.timesync;
60
+ send({ timesync: { id, now: Date.now() } });
61
+ },
62
+ };
63
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@clockworkdog/timesync",
3
+ "description": "A library to synchronize a client's clock to a server's",
4
+ "author": "Clockwork Dog <info@clockwork.dog>",
5
+ "homepage": "https://github.com/clockwork-dog/cogs-sdk/tree/main/packages/timesync",
6
+ "version": "2.11.0",
7
+ "keywords": [],
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/clockwork-dog/cogs-sdk.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/clockwork-dog/cogs-sdk/issues"
15
+ },
16
+ "files": [
17
+ "dist/**/*"
18
+ ],
19
+ "main": "dist/index.js",
20
+ "scripts": {
21
+ "test": "yarn lint && vitest",
22
+ "lint": "eslint .",
23
+ "build": "tsc",
24
+ "release": "yarn npm publish --access public"
25
+ },
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.17.0",
28
+ "eslint": "^9.17.0",
29
+ "eslint-config-prettier": "^9.1.0",
30
+ "eslint-plugin-prettier": "^5.2.1",
31
+ "prettier": "^3.4.2",
32
+ "typescript": "~5.7.2",
33
+ "typescript-eslint": "^8.18.1",
34
+ "vitest": "^4.0.6"
35
+ }
36
+ }