moqtail 0.8.0 → 0.9.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.
package/dist/util.cjs ADDED
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ // src/util/telemetry.ts
4
+ var NetworkTelemetry = class {
5
+ events = [];
6
+ windowMs;
7
+ constructor(windowMs = 1e3) {
8
+ this.windowMs = windowMs;
9
+ }
10
+ push({ latency, size }) {
11
+ const now = Date.now();
12
+ this.events.push({ timestamp: now, latency, size });
13
+ }
14
+ clean() {
15
+ const cutoff = Date.now() - this.windowMs;
16
+ while (this.events.length && this.events[0] && this.events[0].timestamp < cutoff) {
17
+ this.events.shift();
18
+ }
19
+ }
20
+ get throughput() {
21
+ this.clean();
22
+ const totalBytes = this.events.reduce((sum, e) => sum + e.size, 0);
23
+ return totalBytes / (this.windowMs / 1e3);
24
+ }
25
+ get latency() {
26
+ this.clean();
27
+ if (!this.events.length) return 0;
28
+ return this.events.reduce((sum, e) => sum + e.latency, 0) / this.events.length;
29
+ }
30
+ };
31
+
32
+ // src/util/clock_normalizer.ts
33
+ var DEFAULT_SAMPLE_SIZE = 5;
34
+ var DEFAULT_TIME_SERVER = "https://time.akamai.com/?ms";
35
+ var ClockNormalizer = class _ClockNormalizer {
36
+ offset;
37
+ timeServerUrl;
38
+ numSamples;
39
+ constructor(timeServerUrl, offset, numSamples) {
40
+ this.timeServerUrl = timeServerUrl;
41
+ this.offset = offset;
42
+ this.numSamples = numSamples;
43
+ }
44
+ static async create(timeServerUrl, numberOfSamples) {
45
+ const url = timeServerUrl ? timeServerUrl : DEFAULT_TIME_SERVER;
46
+ const numSamples = numberOfSamples ? numberOfSamples : DEFAULT_SAMPLE_SIZE;
47
+ const offset = await _ClockNormalizer.calculateSkew(url, numSamples);
48
+ return new _ClockNormalizer(url, offset, numSamples);
49
+ }
50
+ getSkew() {
51
+ return this.offset;
52
+ }
53
+ now() {
54
+ return Date.now() - this.offset;
55
+ }
56
+ async recalibrate() {
57
+ this.offset = await _ClockNormalizer.calculateSkew(this.timeServerUrl, this.numSamples);
58
+ return this.offset;
59
+ }
60
+ static async calculateSkew(timeServerUrl, numSamples) {
61
+ const samples = [];
62
+ for (let i = 0; i < numSamples; i++) {
63
+ const sample = await _ClockNormalizer.takeSingleSample(timeServerUrl);
64
+ if (sample !== null) {
65
+ samples.push(sample);
66
+ }
67
+ if (i < numSamples - 1) {
68
+ await new Promise((resolve) => setTimeout(resolve, 20));
69
+ }
70
+ }
71
+ if (samples.length === 0) {
72
+ throw new Error("Failed to get any valid samples");
73
+ }
74
+ const offsets = samples.map((s) => s.offset);
75
+ const average = offsets.reduce((sum, offset) => sum + offset, 0) / offsets.length;
76
+ return Math.round(average);
77
+ }
78
+ static async takeSingleSample(timeServerUrl) {
79
+ const separator = timeServerUrl.includes("?") ? "&" : "?";
80
+ const url = `${timeServerUrl}${separator}_=${Date.now()}`;
81
+ const t0 = performance.now();
82
+ const localTimeBeforeRequest = Date.now();
83
+ const response = await fetch(url, { cache: "no-store" });
84
+ const serverTimeText = await response.text();
85
+ const t1 = performance.now();
86
+ const localTimeAfterRequest = Date.now();
87
+ const serverTime = parseFloat(serverTimeText);
88
+ if (isNaN(serverTime)) {
89
+ return null;
90
+ }
91
+ const serverTimeMs = serverTime * 1e3;
92
+ const rtt = t1 - t0;
93
+ const oneWayDelay = rtt / 2;
94
+ const avgLocalTime = (localTimeBeforeRequest + localTimeAfterRequest) / 2;
95
+ const localTimeAtServer = avgLocalTime - oneWayDelay;
96
+ const offset = localTimeAtServer - serverTimeMs;
97
+ return { offset, rtt };
98
+ }
99
+ };
100
+
101
+ exports.ClockNormalizer = ClockNormalizer;
102
+ exports.NetworkTelemetry = NetworkTelemetry;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Copyright 2025 The MOQtail Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ declare class NetworkTelemetry {
17
+ private events;
18
+ private windowMs;
19
+ constructor(windowMs?: number);
20
+ push({ latency, size }: {
21
+ latency: number;
22
+ size: number;
23
+ }): void;
24
+ private clean;
25
+ get throughput(): number;
26
+ get latency(): number;
27
+ }
28
+
29
+ /**
30
+ * Copyright 2025 The MOQtail Authors
31
+ *
32
+ * Licensed under the Apache License, Version 2.0 (the "License");
33
+ * you may not use this file except in compliance with the License.
34
+ * You may obtain a copy of the License at
35
+ *
36
+ * http://www.apache.org/licenses/LICENSE-2.0
37
+ *
38
+ * Unless required by applicable law or agreed to in writing, software
39
+ * distributed under the License is distributed on an "AS IS" BASIS,
40
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ * See the License for the specific language governing permissions and
42
+ * limitations under the License.
43
+ */
44
+ declare class ClockNormalizer {
45
+ private offset;
46
+ private timeServerUrl;
47
+ private numSamples;
48
+ private constructor();
49
+ static create(timeServerUrl?: string, numberOfSamples?: number): Promise<ClockNormalizer>;
50
+ getSkew(): number;
51
+ now(): number;
52
+ recalibrate(): Promise<number>;
53
+ private static calculateSkew;
54
+ private static takeSingleSample;
55
+ }
56
+
57
+ export { ClockNormalizer, NetworkTelemetry };
package/dist/util.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Copyright 2025 The MOQtail Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ declare class NetworkTelemetry {
17
+ private events;
18
+ private windowMs;
19
+ constructor(windowMs?: number);
20
+ push({ latency, size }: {
21
+ latency: number;
22
+ size: number;
23
+ }): void;
24
+ private clean;
25
+ get throughput(): number;
26
+ get latency(): number;
27
+ }
28
+
29
+ /**
30
+ * Copyright 2025 The MOQtail Authors
31
+ *
32
+ * Licensed under the Apache License, Version 2.0 (the "License");
33
+ * you may not use this file except in compliance with the License.
34
+ * You may obtain a copy of the License at
35
+ *
36
+ * http://www.apache.org/licenses/LICENSE-2.0
37
+ *
38
+ * Unless required by applicable law or agreed to in writing, software
39
+ * distributed under the License is distributed on an "AS IS" BASIS,
40
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ * See the License for the specific language governing permissions and
42
+ * limitations under the License.
43
+ */
44
+ declare class ClockNormalizer {
45
+ private offset;
46
+ private timeServerUrl;
47
+ private numSamples;
48
+ private constructor();
49
+ static create(timeServerUrl?: string, numberOfSamples?: number): Promise<ClockNormalizer>;
50
+ getSkew(): number;
51
+ now(): number;
52
+ recalibrate(): Promise<number>;
53
+ private static calculateSkew;
54
+ private static takeSingleSample;
55
+ }
56
+
57
+ export { ClockNormalizer, NetworkTelemetry };
package/dist/util.js ADDED
@@ -0,0 +1,99 @@
1
+ // src/util/telemetry.ts
2
+ var NetworkTelemetry = class {
3
+ events = [];
4
+ windowMs;
5
+ constructor(windowMs = 1e3) {
6
+ this.windowMs = windowMs;
7
+ }
8
+ push({ latency, size }) {
9
+ const now = Date.now();
10
+ this.events.push({ timestamp: now, latency, size });
11
+ }
12
+ clean() {
13
+ const cutoff = Date.now() - this.windowMs;
14
+ while (this.events.length && this.events[0] && this.events[0].timestamp < cutoff) {
15
+ this.events.shift();
16
+ }
17
+ }
18
+ get throughput() {
19
+ this.clean();
20
+ const totalBytes = this.events.reduce((sum, e) => sum + e.size, 0);
21
+ return totalBytes / (this.windowMs / 1e3);
22
+ }
23
+ get latency() {
24
+ this.clean();
25
+ if (!this.events.length) return 0;
26
+ return this.events.reduce((sum, e) => sum + e.latency, 0) / this.events.length;
27
+ }
28
+ };
29
+
30
+ // src/util/clock_normalizer.ts
31
+ var DEFAULT_SAMPLE_SIZE = 5;
32
+ var DEFAULT_TIME_SERVER = "https://time.akamai.com/?ms";
33
+ var ClockNormalizer = class _ClockNormalizer {
34
+ offset;
35
+ timeServerUrl;
36
+ numSamples;
37
+ constructor(timeServerUrl, offset, numSamples) {
38
+ this.timeServerUrl = timeServerUrl;
39
+ this.offset = offset;
40
+ this.numSamples = numSamples;
41
+ }
42
+ static async create(timeServerUrl, numberOfSamples) {
43
+ const url = timeServerUrl ? timeServerUrl : DEFAULT_TIME_SERVER;
44
+ const numSamples = numberOfSamples ? numberOfSamples : DEFAULT_SAMPLE_SIZE;
45
+ const offset = await _ClockNormalizer.calculateSkew(url, numSamples);
46
+ return new _ClockNormalizer(url, offset, numSamples);
47
+ }
48
+ getSkew() {
49
+ return this.offset;
50
+ }
51
+ now() {
52
+ return Date.now() - this.offset;
53
+ }
54
+ async recalibrate() {
55
+ this.offset = await _ClockNormalizer.calculateSkew(this.timeServerUrl, this.numSamples);
56
+ return this.offset;
57
+ }
58
+ static async calculateSkew(timeServerUrl, numSamples) {
59
+ const samples = [];
60
+ for (let i = 0; i < numSamples; i++) {
61
+ const sample = await _ClockNormalizer.takeSingleSample(timeServerUrl);
62
+ if (sample !== null) {
63
+ samples.push(sample);
64
+ }
65
+ if (i < numSamples - 1) {
66
+ await new Promise((resolve) => setTimeout(resolve, 20));
67
+ }
68
+ }
69
+ if (samples.length === 0) {
70
+ throw new Error("Failed to get any valid samples");
71
+ }
72
+ const offsets = samples.map((s) => s.offset);
73
+ const average = offsets.reduce((sum, offset) => sum + offset, 0) / offsets.length;
74
+ return Math.round(average);
75
+ }
76
+ static async takeSingleSample(timeServerUrl) {
77
+ const separator = timeServerUrl.includes("?") ? "&" : "?";
78
+ const url = `${timeServerUrl}${separator}_=${Date.now()}`;
79
+ const t0 = performance.now();
80
+ const localTimeBeforeRequest = Date.now();
81
+ const response = await fetch(url, { cache: "no-store" });
82
+ const serverTimeText = await response.text();
83
+ const t1 = performance.now();
84
+ const localTimeAfterRequest = Date.now();
85
+ const serverTime = parseFloat(serverTimeText);
86
+ if (isNaN(serverTime)) {
87
+ return null;
88
+ }
89
+ const serverTimeMs = serverTime * 1e3;
90
+ const rtt = t1 - t0;
91
+ const oneWayDelay = rtt / 2;
92
+ const avgLocalTime = (localTimeBeforeRequest + localTimeAfterRequest) / 2;
93
+ const localTimeAtServer = avgLocalTime - oneWayDelay;
94
+ const offset = localTimeAtServer - serverTimeMs;
95
+ return { offset, rtt };
96
+ }
97
+ };
98
+
99
+ export { ClockNormalizer, NetworkTelemetry };