@hyperfrontend/time-utils 0.0.5 → 1.0.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/CHANGELOG.md +6 -0
- package/README.md +3 -2
- package/SECURITY.md +50 -15
- package/bundle/index.iife.js +0 -287
- package/bundle/index.umd.js +0 -287
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.0.0](https://github.com/AndrewRedican/hyperfrontend/compare/5f116abb8ba6355dfb283fa03b7481e5eb029480...4b34883aead4c721f021465b19fa5bdd49514d37) - 2026-08-31
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- Marked stable. No API changes since 0.0.5.
|
|
10
|
+
|
|
5
11
|
## [0.0.5](https://github.com/AndrewRedican/hyperfrontend/compare/c8db08be8b183addd26caf81fdd17fb3693f296f...466c0388c4cd516b9c704214140b4df1004098e6) - 2026-06-23
|
|
6
12
|
|
|
7
13
|
### Other
|
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
Functional time utilities for async operations, intervals, and time normalization.
|
|
37
37
|
|
|
38
38
|
• 👉 See [**documentation**](https://www.hyperfrontend.dev/docs/libraries/utils/time/)
|
|
39
|
+
• 👉 See [**guides & tutorials**](https://www.hyperfrontend.dev/docs/guides/?package=%40hyperfrontend%2Ftime-utils)
|
|
39
40
|
|
|
40
41
|
## What is @hyperfrontend/time-utils?
|
|
41
42
|
|
|
@@ -62,7 +63,7 @@ All timing abstractions maintain internal state privately while exposing frozen
|
|
|
62
63
|
|
|
63
64
|
### 1. Pause/Resume Capabilities Native APIs Lack
|
|
64
65
|
|
|
65
|
-
JavaScript's `setTimeout` and `setInterval` cannot be paused
|
|
66
|
+
JavaScript's `setTimeout` and `setInterval` cannot be paused: once started, they either complete or get cancelled. This creates problems for features like user-initiated pauses in games, animations during background tabs, or request throttling. `createTimer()` tracks elapsed time internally, enabling pause/resume without restarting from the beginning or losing progress.
|
|
66
67
|
|
|
67
68
|
**Example:** A countdown timer in a game needs to pause when the user switches tabs. With `setTimeout`, you'd need to calculate remaining time manually and create a new timeout. With `createTimer`, just call `timer.pause()`.
|
|
68
69
|
|
|
@@ -247,7 +248,7 @@ cleanup()
|
|
|
247
248
|
|
|
248
249
|
### Dependencies
|
|
249
250
|
|
|
250
|
-
None
|
|
251
|
+
None: zero external dependencies.
|
|
251
252
|
|
|
252
253
|
## Part of hyperfrontend
|
|
253
254
|
|
package/SECURITY.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Security Policy
|
|
2
2
|
|
|
3
|
+
## The Security Model
|
|
4
|
+
|
|
5
|
+
Before reporting or reviewing, read the model this project is built on:
|
|
6
|
+
**[Security Model](https://www.hyperfrontend.dev/docs/core-concepts/security)**. It is the canonical
|
|
7
|
+
statement of what hyperfrontend defends against and what it does not, and it is the reference every
|
|
8
|
+
other security claim in these docs points back to. In short:
|
|
9
|
+
|
|
10
|
+
- **The named adversary is co-resident script**: an analytics snippet, tag manager, compromised
|
|
11
|
+
dependency, or unknown page that embeds a feature URL. A host that deliberately installs a feature
|
|
12
|
+
is trusting it, the way it trusts any dependency; the controls exist to bound a trusted
|
|
13
|
+
feature's bad day, not to treat its authors as hostile.
|
|
14
|
+
- **Origin checks authenticate rooms, not speakers.** Once arbitrary script runs inside a page, no
|
|
15
|
+
message check distinguishes it from the application. Threats inside a page need Content Security
|
|
16
|
+
Policy, Trusted Types, dependency provenance, and server-side authorisation. That is a different
|
|
17
|
+
treatment, deliberately outside this model.
|
|
18
|
+
- **Three parties carry the security of an integration.** The browser enforces document isolation
|
|
19
|
+
and the frame's capability attributes; the protocol enforces the relationship (pinned
|
|
20
|
+
counterparts, gated handshake, validated payloads, versioned contracts, an optional encrypted
|
|
21
|
+
envelope); and **you** decide authorisation: `frame-ancestors`, backend checks, HTTPS, the
|
|
22
|
+
envelope you choose, and the containment posture you set.
|
|
23
|
+
|
|
24
|
+
A vulnerability report is most useful when it names which of those three the issue defeats.
|
|
25
|
+
|
|
3
26
|
## Reporting a Vulnerability
|
|
4
27
|
|
|
5
28
|
We take the security of hyperfrontend seriously. If you discover a security vulnerability, please help us protect our users by following responsible disclosure practices.
|
|
@@ -58,14 +81,30 @@ Thank you for helping keep hyperfrontend and its users safe!
|
|
|
58
81
|
|
|
59
82
|
## Security Best Practices
|
|
60
83
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
84
|
+
These are the decisions the SDK cannot make for you. Everything the protocol already enforces —
|
|
85
|
+
origin pinning, window binding, instance identity, the gated handshake, payload validation on both
|
|
86
|
+
ends — is on by default and is not something you should be re-implementing by hand.
|
|
87
|
+
|
|
88
|
+
1. **Restrict who may embed the feature.** Send
|
|
89
|
+
`Content-Security-Policy: frame-ancestors <hosts>` on the response that serves the feature
|
|
90
|
+
document. Origin pinning keeps a conversation consistent; only `frame-ancestors` decides whether
|
|
91
|
+
a page was ever allowed to frame you.
|
|
92
|
+
2. **Authorise on the server.** A message that crossed the boundary is not an authorised operation.
|
|
93
|
+
Protected work needs credentials the feature's own backend validates.
|
|
94
|
+
3. **Choose the envelope deliberately.** `v2` with a pre-shared key is the confidentiality control;
|
|
95
|
+
`v1` is time-window obfuscation and buys deterrence only. Provision and rotate the `v2` key
|
|
96
|
+
yourself; a key is never baked into a built artifact. A handshake that cannot agree on an
|
|
97
|
+
encrypted transport falls back to plaintext; where that would be unacceptable, drive
|
|
98
|
+
`@hyperfrontend/nexus` directly and set `security.mode: 'fail-closed'` on the channel so the
|
|
99
|
+
connection is denied instead.
|
|
100
|
+
4. **Declare schemas and a contract version.** Actions without a schema pass unvalidated, and a side
|
|
101
|
+
without a version always passes the compatibility gate. Both are how drift is caught early.
|
|
102
|
+
5. **Grant capability narrowly.** Delegate only the Permissions-Policy features the integration
|
|
103
|
+
needs, and price a `sandbox` posture against what the product actually requires.
|
|
104
|
+
6. **Serve everything over HTTPS**, host and feature alike.
|
|
105
|
+
7. **Keep dependencies updated** on both sides of the boundary, and pair that with the page-integrity
|
|
106
|
+
controls this model deliberately leaves to you: Content Security Policy, Trusted Types,
|
|
107
|
+
Subresource Integrity, and dependency provenance.
|
|
69
108
|
|
|
70
109
|
## Security Updates
|
|
71
110
|
|
|
@@ -73,10 +112,6 @@ Security updates will be released as patch versions and documented in the [CHANG
|
|
|
73
112
|
|
|
74
113
|
## Supported Versions
|
|
75
114
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
| ------- | ------------------ |
|
|
80
|
-
| 0.0.x | :white_check_mark: |
|
|
81
|
-
|
|
82
|
-
As the project matures, we will update this table to reflect our long-term support policy.
|
|
115
|
+
Security updates are provided for the latest published version of each `@hyperfrontend/*` package.
|
|
116
|
+
Older releases receive no backports. A long-term support policy will replace this section once the
|
|
117
|
+
packages settle on a stable release cadence.
|
package/bundle/index.iife.js
CHANGED
|
@@ -1,158 +1,29 @@
|
|
|
1
1
|
var HyperfrontendTimeUtils = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Safe copies of Date built-in via factory function and static methods.
|
|
6
|
-
*
|
|
7
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
8
|
-
*/
|
|
9
|
-
/* eslint-disable jsdoc/require-param */
|
|
10
4
|
const _Date = globalThis.Date;
|
|
11
5
|
const _Reflect$2 = globalThis.Reflect;
|
|
12
|
-
/**
|
|
13
|
-
* (Safe copy) Creates a new Date using the captured Date constructor.
|
|
14
|
-
* Use this instead of `new Date()`. Accepts all standard Date constructor signatures.
|
|
15
|
-
*
|
|
16
|
-
* @returns A new Date instance.
|
|
17
|
-
*
|
|
18
|
-
* @example Creating Date instances
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const now = createDate()
|
|
21
|
-
* const fromTimestamp = createDate(1704067200000)
|
|
22
|
-
* const fromString = createDate('2024-01-01T00:00:00Z')
|
|
23
|
-
* const fromParts = createDate(2024, 0, 1, 12, 30, 0) // Jan 1, 2024 12:30:00
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
6
|
function createDate(...args) {
|
|
27
7
|
return _Reflect$2.construct(_Date, args);
|
|
28
8
|
}
|
|
29
|
-
/**
|
|
30
|
-
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```typescript
|
|
34
|
-
* const timestamp = dateNow()
|
|
35
|
-
* // => 1704067200000 (example timestamp)
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
9
|
const dateNow = _Date.now;
|
|
39
10
|
|
|
40
|
-
/**
|
|
41
|
-
* Safe copies of Object built-in methods.
|
|
42
|
-
*
|
|
43
|
-
* These references are captured at module initialization time to protect against
|
|
44
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
45
|
-
*
|
|
46
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
47
|
-
*/
|
|
48
11
|
const _Object = globalThis.Object;
|
|
49
|
-
/**
|
|
50
|
-
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
51
|
-
* and prevents the addition of new properties.
|
|
52
|
-
*/
|
|
53
12
|
const freeze = _Object.freeze;
|
|
54
13
|
|
|
55
|
-
/**
|
|
56
|
-
* Safe copies of Timer/Scheduling built-in functions.
|
|
57
|
-
*
|
|
58
|
-
* These references are captured at module initialization time to protect against
|
|
59
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
60
|
-
*
|
|
61
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
62
|
-
*/
|
|
63
14
|
const _setTimeout = globalThis.setTimeout;
|
|
64
15
|
const _setInterval = globalThis.setInterval;
|
|
65
16
|
const _clearTimeout = globalThis.clearTimeout;
|
|
66
17
|
const _clearInterval = globalThis.clearInterval;
|
|
67
|
-
/**
|
|
68
|
-
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
69
|
-
*
|
|
70
|
-
* @param callback - Function to call when the timer elapses.
|
|
71
|
-
* @param delay - Time in milliseconds before executing.
|
|
72
|
-
* @param args - Additional arguments to pass to the callback.
|
|
73
|
-
* @returns A numeric ID for the timer.
|
|
74
|
-
*
|
|
75
|
-
* @example Setting a timeout
|
|
76
|
-
* ```typescript
|
|
77
|
-
* const timerId = setTimeout(() => console.log('Executed'), 1000)
|
|
78
|
-
* // Pass arguments to callback
|
|
79
|
-
* setTimeout((name, count) => console.log(name, count), 500, 'items', 5)
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
18
|
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
83
|
-
/**
|
|
84
|
-
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
85
|
-
*
|
|
86
|
-
* @param callback - Function to call at each interval.
|
|
87
|
-
* @param delay - Time in milliseconds between calls.
|
|
88
|
-
* @param args - Additional arguments to pass to the callback.
|
|
89
|
-
* @returns A numeric ID for the interval.
|
|
90
|
-
*
|
|
91
|
-
* @example Setting an interval
|
|
92
|
-
* ```typescript
|
|
93
|
-
* let count = 0
|
|
94
|
-
* const intervalId = setInterval(() => {
|
|
95
|
-
* count++
|
|
96
|
-
* if (count >= 5) clearInterval(intervalId)
|
|
97
|
-
* }, 1000)
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
19
|
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
101
|
-
/**
|
|
102
|
-
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
103
|
-
*
|
|
104
|
-
* @param id - The identifier of the timeout to cancel.
|
|
105
|
-
*
|
|
106
|
-
* @example Canceling a timeout
|
|
107
|
-
* ```typescript
|
|
108
|
-
* const timerId = setTimeout(() => console.log('Never runs'), 5000)
|
|
109
|
-
* clearTimeout(timerId)
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
20
|
const clearTimeout = (id) => {
|
|
113
21
|
_clearTimeout(id);
|
|
114
22
|
};
|
|
115
|
-
/**
|
|
116
|
-
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
117
|
-
*
|
|
118
|
-
* @param id - The identifier of the interval to cancel.
|
|
119
|
-
*
|
|
120
|
-
* @example Canceling an interval
|
|
121
|
-
* ```typescript
|
|
122
|
-
* const intervalId = setInterval(() => console.log('tick'), 1000)
|
|
123
|
-
* // Stop after some condition
|
|
124
|
-
* clearInterval(intervalId)
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
23
|
const clearInterval = (id) => {
|
|
128
24
|
_clearInterval(id);
|
|
129
25
|
};
|
|
130
26
|
|
|
131
|
-
/**
|
|
132
|
-
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
133
|
-
* at the specified internal (in milliseconds).
|
|
134
|
-
*
|
|
135
|
-
* Allows you to start or stop the interval loop, much like a stop watch.
|
|
136
|
-
* Allows you to unsubscribe callback functions.
|
|
137
|
-
*
|
|
138
|
-
* @param interval - Time in milliseconds between each callback invocation (default: 1000ms)
|
|
139
|
-
* @returns A Clock instance with start, stop, subscribe, and unsubscribe methods
|
|
140
|
-
*
|
|
141
|
-
* @example Creating interval clock
|
|
142
|
-
* ```typescript
|
|
143
|
-
* const clock = createClock(1000)
|
|
144
|
-
*
|
|
145
|
-
* const updateDisplay = (currentTime: Date) => {
|
|
146
|
-
* console.log(currentTime.toISOString())
|
|
147
|
-
* }
|
|
148
|
-
*
|
|
149
|
-
* clock.subscribe(updateDisplay)
|
|
150
|
-
* clock.start()
|
|
151
|
-
*
|
|
152
|
-
* // Later: stop receiving updates
|
|
153
|
-
* clock.stop()
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
27
|
function createClock(interval = 1000) {
|
|
157
28
|
let clockId = null;
|
|
158
29
|
let subscribers = [];
|
|
@@ -179,32 +50,6 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
179
50
|
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
180
51
|
}
|
|
181
52
|
|
|
182
|
-
/**
|
|
183
|
-
* Invokes callback function after the designated time has passed, much like a timer.
|
|
184
|
-
* Allows you to pause, resume, or reset the progress of time tracked.
|
|
185
|
-
*
|
|
186
|
-
* @param callback - The function to invoke after the delay
|
|
187
|
-
* @param delay - Time in milliseconds to wait until callback is invoked
|
|
188
|
-
* @returns A Timer instance with pause, resume, and reset methods
|
|
189
|
-
*
|
|
190
|
-
* @example Creating pausable timer
|
|
191
|
-
* ```typescript
|
|
192
|
-
* const timer = createTimer(() => {
|
|
193
|
-
* console.log('Session expired')
|
|
194
|
-
* }, 30_000)
|
|
195
|
-
*
|
|
196
|
-
* timer.resume() // Start the 30-second countdown
|
|
197
|
-
*
|
|
198
|
-
* // User activity detected - pause the timer
|
|
199
|
-
* timer.pause()
|
|
200
|
-
*
|
|
201
|
-
* // User idle again - resume from where we left off
|
|
202
|
-
* timer.resume()
|
|
203
|
-
*
|
|
204
|
-
* // Reset to full 30 seconds on explicit action
|
|
205
|
-
* timer.reset()
|
|
206
|
-
* ```
|
|
207
|
-
*/
|
|
208
53
|
function createTimer(callback, delay) {
|
|
209
54
|
let timerId = null;
|
|
210
55
|
let start = null;
|
|
@@ -213,7 +58,6 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
213
58
|
if (timerId !== null) {
|
|
214
59
|
clearTimeout(timerId);
|
|
215
60
|
const now = dateNow();
|
|
216
|
-
/* istanbul ignore else - start is always set when timerId is not null */
|
|
217
61
|
if (start !== null) {
|
|
218
62
|
remaining -= now - start;
|
|
219
63
|
}
|
|
@@ -237,79 +81,16 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
237
81
|
return freeze({ pause, resume, reset });
|
|
238
82
|
}
|
|
239
83
|
|
|
240
|
-
/**
|
|
241
|
-
* Safe copies of Error built-ins via factory functions.
|
|
242
|
-
*
|
|
243
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
244
|
-
* provides factory functions that use Reflect.construct internally.
|
|
245
|
-
*
|
|
246
|
-
* These references are captured at module initialization time to protect against
|
|
247
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
248
|
-
*
|
|
249
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
250
|
-
*/
|
|
251
84
|
const _Error = globalThis.Error;
|
|
252
85
|
const _Reflect$1 = globalThis.Reflect;
|
|
253
|
-
/**
|
|
254
|
-
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
255
|
-
* Use this instead of `new Error()`.
|
|
256
|
-
*
|
|
257
|
-
* @param message - Optional error message.
|
|
258
|
-
* @param options - Optional error options.
|
|
259
|
-
* @returns A new Error instance.
|
|
260
|
-
*
|
|
261
|
-
* @example Creating Error instances
|
|
262
|
-
* ```typescript
|
|
263
|
-
* const error = createError('Operation failed')
|
|
264
|
-
* // With cause for error chaining
|
|
265
|
-
* const wrapped = createError('Request failed', { cause: originalError })
|
|
266
|
-
* ```
|
|
267
|
-
*/
|
|
268
86
|
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
269
87
|
|
|
270
|
-
/**
|
|
271
|
-
* Safe copies of Math built-in methods.
|
|
272
|
-
*
|
|
273
|
-
* These references are captured at module initialization time to protect against
|
|
274
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
275
|
-
*
|
|
276
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
277
|
-
*/
|
|
278
88
|
const _Math = globalThis.Math;
|
|
279
|
-
/**
|
|
280
|
-
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
281
|
-
*/
|
|
282
89
|
const floor = _Math.floor;
|
|
283
90
|
|
|
284
|
-
/**
|
|
285
|
-
* Safe copies of Number built-in methods and constants.
|
|
286
|
-
*
|
|
287
|
-
* These references are captured at module initialization time to protect against
|
|
288
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
289
|
-
*
|
|
290
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
291
|
-
*/
|
|
292
91
|
const _isNaN = globalThis.isNaN;
|
|
293
|
-
/**
|
|
294
|
-
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
295
|
-
*/
|
|
296
92
|
const globalIsNaN = _isNaN;
|
|
297
93
|
|
|
298
|
-
/**
|
|
299
|
-
* Normalizes a given time to the nearest base time window.
|
|
300
|
-
*
|
|
301
|
-
* @param time - The Date object to normalize to the nearest time window
|
|
302
|
-
* @param baseTimeWindow - The size of the time window in minutes for normalization
|
|
303
|
-
* @returns A new Date object normalized to the start of the time window
|
|
304
|
-
*
|
|
305
|
-
* @example Normalizing to 15-minute buckets
|
|
306
|
-
* ```typescript
|
|
307
|
-
* // Round timestamps to 15-minute intervals for analytics bucketing
|
|
308
|
-
* const eventTime = new Date('2024-03-15T14:23:45Z')
|
|
309
|
-
* const bucketTime = normalizeToBaseTimeWindow(eventTime, 15)
|
|
310
|
-
* // => 2024-03-15T14:15:00.000Z
|
|
311
|
-
* ```
|
|
312
|
-
*/
|
|
313
94
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
314
95
|
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
315
96
|
throw createError('Invalid time input');
|
|
@@ -323,90 +104,22 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
323
104
|
return createDate(normalizedTimeInMs);
|
|
324
105
|
}
|
|
325
106
|
|
|
326
|
-
/**
|
|
327
|
-
* Creates a repeating interval that invokes a callback function at regular intervals.
|
|
328
|
-
*
|
|
329
|
-
* @param callback - The function to invoke at each interval
|
|
330
|
-
* @param interval - Time in milliseconds between each callback invocation
|
|
331
|
-
* @returns A cleanup function that stops the interval when called
|
|
332
|
-
*
|
|
333
|
-
* @example Setting interval with cleanup
|
|
334
|
-
* ```typescript
|
|
335
|
-
* const stopPolling = setIntervalCallback(() => {
|
|
336
|
-
* fetchLatestData()
|
|
337
|
-
* }, 5000)
|
|
338
|
-
*
|
|
339
|
-
* // Later: clean up when component unmounts
|
|
340
|
-
* stopPolling()
|
|
341
|
-
* ```
|
|
342
|
-
*/
|
|
343
107
|
function setIntervalCallback(callback, interval) {
|
|
344
108
|
const timerId = setInterval(callback, interval);
|
|
345
109
|
return () => clearInterval(timerId);
|
|
346
110
|
}
|
|
347
111
|
|
|
348
|
-
/**
|
|
349
|
-
* Safe Promise factory and bound static methods.
|
|
350
|
-
*
|
|
351
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
352
|
-
*/
|
|
353
|
-
/* eslint-disable workspace/lib-require-jsdoc-example */
|
|
354
112
|
const _Promise = globalThis.Promise;
|
|
355
113
|
const _Reflect = globalThis.Reflect;
|
|
356
|
-
/**
|
|
357
|
-
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
358
|
-
* Use this instead of `new Promise()`.
|
|
359
|
-
*
|
|
360
|
-
* @param executor - The executor function.
|
|
361
|
-
* @returns A new Promise instance.
|
|
362
|
-
*/
|
|
363
114
|
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
364
|
-
/**
|
|
365
|
-
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
366
|
-
*/
|
|
367
115
|
_Promise.resolve.bind(_Promise);
|
|
368
|
-
/**
|
|
369
|
-
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
370
|
-
*/
|
|
371
116
|
_Promise.reject.bind(_Promise);
|
|
372
|
-
/**
|
|
373
|
-
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
374
|
-
*/
|
|
375
117
|
_Promise.all.bind(_Promise);
|
|
376
|
-
/**
|
|
377
|
-
* (Safe copy) Returns a Promise that resolves/rejects with the first settled promise.
|
|
378
|
-
*/
|
|
379
118
|
_Promise.race.bind(_Promise);
|
|
380
|
-
/**
|
|
381
|
-
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
382
|
-
*/
|
|
383
119
|
_Promise.allSettled.bind(_Promise);
|
|
384
|
-
/**
|
|
385
|
-
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
386
|
-
*/
|
|
387
120
|
_Promise.any.bind(_Promise);
|
|
388
|
-
/**
|
|
389
|
-
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
390
|
-
* Note: Available only in ES2024+ environments.
|
|
391
|
-
*/
|
|
392
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
393
121
|
_Promise.withResolvers?.bind(_Promise);
|
|
394
122
|
|
|
395
|
-
/**
|
|
396
|
-
* Pauses execution for a specified duration.
|
|
397
|
-
*
|
|
398
|
-
* @param milliseconds - The duration to sleep in milliseconds
|
|
399
|
-
* @returns A promise that resolves after the specified duration
|
|
400
|
-
*
|
|
401
|
-
* @example Implementing retry backoff
|
|
402
|
-
* ```typescript
|
|
403
|
-
* async function retryWithBackoff(attempt: number) {
|
|
404
|
-
* const backoffMs = Math.min(1000 * 2 ** attempt, 30_000)
|
|
405
|
-
* await sleep(backoffMs)
|
|
406
|
-
* return fetchResource()
|
|
407
|
-
* }
|
|
408
|
-
* ```
|
|
409
|
-
*/
|
|
410
123
|
function sleep(milliseconds) {
|
|
411
124
|
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
412
125
|
}
|
package/bundle/index.umd.js
CHANGED
|
@@ -4,158 +4,29 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.HyperfrontendTimeUtils = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Safe copies of Date built-in via factory function and static methods.
|
|
9
|
-
*
|
|
10
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
11
|
-
*/
|
|
12
|
-
/* eslint-disable jsdoc/require-param */
|
|
13
7
|
const _Date = globalThis.Date;
|
|
14
8
|
const _Reflect$2 = globalThis.Reflect;
|
|
15
|
-
/**
|
|
16
|
-
* (Safe copy) Creates a new Date using the captured Date constructor.
|
|
17
|
-
* Use this instead of `new Date()`. Accepts all standard Date constructor signatures.
|
|
18
|
-
*
|
|
19
|
-
* @returns A new Date instance.
|
|
20
|
-
*
|
|
21
|
-
* @example Creating Date instances
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const now = createDate()
|
|
24
|
-
* const fromTimestamp = createDate(1704067200000)
|
|
25
|
-
* const fromString = createDate('2024-01-01T00:00:00Z')
|
|
26
|
-
* const fromParts = createDate(2024, 0, 1, 12, 30, 0) // Jan 1, 2024 12:30:00
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
9
|
function createDate(...args) {
|
|
30
10
|
return _Reflect$2.construct(_Date, args);
|
|
31
11
|
}
|
|
32
|
-
/**
|
|
33
|
-
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```typescript
|
|
37
|
-
* const timestamp = dateNow()
|
|
38
|
-
* // => 1704067200000 (example timestamp)
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
12
|
const dateNow = _Date.now;
|
|
42
13
|
|
|
43
|
-
/**
|
|
44
|
-
* Safe copies of Object built-in methods.
|
|
45
|
-
*
|
|
46
|
-
* These references are captured at module initialization time to protect against
|
|
47
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
48
|
-
*
|
|
49
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
50
|
-
*/
|
|
51
14
|
const _Object = globalThis.Object;
|
|
52
|
-
/**
|
|
53
|
-
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
54
|
-
* and prevents the addition of new properties.
|
|
55
|
-
*/
|
|
56
15
|
const freeze = _Object.freeze;
|
|
57
16
|
|
|
58
|
-
/**
|
|
59
|
-
* Safe copies of Timer/Scheduling built-in functions.
|
|
60
|
-
*
|
|
61
|
-
* These references are captured at module initialization time to protect against
|
|
62
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
63
|
-
*
|
|
64
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
65
|
-
*/
|
|
66
17
|
const _setTimeout = globalThis.setTimeout;
|
|
67
18
|
const _setInterval = globalThis.setInterval;
|
|
68
19
|
const _clearTimeout = globalThis.clearTimeout;
|
|
69
20
|
const _clearInterval = globalThis.clearInterval;
|
|
70
|
-
/**
|
|
71
|
-
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
72
|
-
*
|
|
73
|
-
* @param callback - Function to call when the timer elapses.
|
|
74
|
-
* @param delay - Time in milliseconds before executing.
|
|
75
|
-
* @param args - Additional arguments to pass to the callback.
|
|
76
|
-
* @returns A numeric ID for the timer.
|
|
77
|
-
*
|
|
78
|
-
* @example Setting a timeout
|
|
79
|
-
* ```typescript
|
|
80
|
-
* const timerId = setTimeout(() => console.log('Executed'), 1000)
|
|
81
|
-
* // Pass arguments to callback
|
|
82
|
-
* setTimeout((name, count) => console.log(name, count), 500, 'items', 5)
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
21
|
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
86
|
-
/**
|
|
87
|
-
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
88
|
-
*
|
|
89
|
-
* @param callback - Function to call at each interval.
|
|
90
|
-
* @param delay - Time in milliseconds between calls.
|
|
91
|
-
* @param args - Additional arguments to pass to the callback.
|
|
92
|
-
* @returns A numeric ID for the interval.
|
|
93
|
-
*
|
|
94
|
-
* @example Setting an interval
|
|
95
|
-
* ```typescript
|
|
96
|
-
* let count = 0
|
|
97
|
-
* const intervalId = setInterval(() => {
|
|
98
|
-
* count++
|
|
99
|
-
* if (count >= 5) clearInterval(intervalId)
|
|
100
|
-
* }, 1000)
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
22
|
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
104
|
-
/**
|
|
105
|
-
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
106
|
-
*
|
|
107
|
-
* @param id - The identifier of the timeout to cancel.
|
|
108
|
-
*
|
|
109
|
-
* @example Canceling a timeout
|
|
110
|
-
* ```typescript
|
|
111
|
-
* const timerId = setTimeout(() => console.log('Never runs'), 5000)
|
|
112
|
-
* clearTimeout(timerId)
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
23
|
const clearTimeout = (id) => {
|
|
116
24
|
_clearTimeout(id);
|
|
117
25
|
};
|
|
118
|
-
/**
|
|
119
|
-
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
120
|
-
*
|
|
121
|
-
* @param id - The identifier of the interval to cancel.
|
|
122
|
-
*
|
|
123
|
-
* @example Canceling an interval
|
|
124
|
-
* ```typescript
|
|
125
|
-
* const intervalId = setInterval(() => console.log('tick'), 1000)
|
|
126
|
-
* // Stop after some condition
|
|
127
|
-
* clearInterval(intervalId)
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
26
|
const clearInterval = (id) => {
|
|
131
27
|
_clearInterval(id);
|
|
132
28
|
};
|
|
133
29
|
|
|
134
|
-
/**
|
|
135
|
-
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
136
|
-
* at the specified internal (in milliseconds).
|
|
137
|
-
*
|
|
138
|
-
* Allows you to start or stop the interval loop, much like a stop watch.
|
|
139
|
-
* Allows you to unsubscribe callback functions.
|
|
140
|
-
*
|
|
141
|
-
* @param interval - Time in milliseconds between each callback invocation (default: 1000ms)
|
|
142
|
-
* @returns A Clock instance with start, stop, subscribe, and unsubscribe methods
|
|
143
|
-
*
|
|
144
|
-
* @example Creating interval clock
|
|
145
|
-
* ```typescript
|
|
146
|
-
* const clock = createClock(1000)
|
|
147
|
-
*
|
|
148
|
-
* const updateDisplay = (currentTime: Date) => {
|
|
149
|
-
* console.log(currentTime.toISOString())
|
|
150
|
-
* }
|
|
151
|
-
*
|
|
152
|
-
* clock.subscribe(updateDisplay)
|
|
153
|
-
* clock.start()
|
|
154
|
-
*
|
|
155
|
-
* // Later: stop receiving updates
|
|
156
|
-
* clock.stop()
|
|
157
|
-
* ```
|
|
158
|
-
*/
|
|
159
30
|
function createClock(interval = 1000) {
|
|
160
31
|
let clockId = null;
|
|
161
32
|
let subscribers = [];
|
|
@@ -182,32 +53,6 @@
|
|
|
182
53
|
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
183
54
|
}
|
|
184
55
|
|
|
185
|
-
/**
|
|
186
|
-
* Invokes callback function after the designated time has passed, much like a timer.
|
|
187
|
-
* Allows you to pause, resume, or reset the progress of time tracked.
|
|
188
|
-
*
|
|
189
|
-
* @param callback - The function to invoke after the delay
|
|
190
|
-
* @param delay - Time in milliseconds to wait until callback is invoked
|
|
191
|
-
* @returns A Timer instance with pause, resume, and reset methods
|
|
192
|
-
*
|
|
193
|
-
* @example Creating pausable timer
|
|
194
|
-
* ```typescript
|
|
195
|
-
* const timer = createTimer(() => {
|
|
196
|
-
* console.log('Session expired')
|
|
197
|
-
* }, 30_000)
|
|
198
|
-
*
|
|
199
|
-
* timer.resume() // Start the 30-second countdown
|
|
200
|
-
*
|
|
201
|
-
* // User activity detected - pause the timer
|
|
202
|
-
* timer.pause()
|
|
203
|
-
*
|
|
204
|
-
* // User idle again - resume from where we left off
|
|
205
|
-
* timer.resume()
|
|
206
|
-
*
|
|
207
|
-
* // Reset to full 30 seconds on explicit action
|
|
208
|
-
* timer.reset()
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
56
|
function createTimer(callback, delay) {
|
|
212
57
|
let timerId = null;
|
|
213
58
|
let start = null;
|
|
@@ -216,7 +61,6 @@
|
|
|
216
61
|
if (timerId !== null) {
|
|
217
62
|
clearTimeout(timerId);
|
|
218
63
|
const now = dateNow();
|
|
219
|
-
/* istanbul ignore else - start is always set when timerId is not null */
|
|
220
64
|
if (start !== null) {
|
|
221
65
|
remaining -= now - start;
|
|
222
66
|
}
|
|
@@ -240,79 +84,16 @@
|
|
|
240
84
|
return freeze({ pause, resume, reset });
|
|
241
85
|
}
|
|
242
86
|
|
|
243
|
-
/**
|
|
244
|
-
* Safe copies of Error built-ins via factory functions.
|
|
245
|
-
*
|
|
246
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
247
|
-
* provides factory functions that use Reflect.construct internally.
|
|
248
|
-
*
|
|
249
|
-
* These references are captured at module initialization time to protect against
|
|
250
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
251
|
-
*
|
|
252
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
253
|
-
*/
|
|
254
87
|
const _Error = globalThis.Error;
|
|
255
88
|
const _Reflect$1 = globalThis.Reflect;
|
|
256
|
-
/**
|
|
257
|
-
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
258
|
-
* Use this instead of `new Error()`.
|
|
259
|
-
*
|
|
260
|
-
* @param message - Optional error message.
|
|
261
|
-
* @param options - Optional error options.
|
|
262
|
-
* @returns A new Error instance.
|
|
263
|
-
*
|
|
264
|
-
* @example Creating Error instances
|
|
265
|
-
* ```typescript
|
|
266
|
-
* const error = createError('Operation failed')
|
|
267
|
-
* // With cause for error chaining
|
|
268
|
-
* const wrapped = createError('Request failed', { cause: originalError })
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
89
|
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
272
90
|
|
|
273
|
-
/**
|
|
274
|
-
* Safe copies of Math built-in methods.
|
|
275
|
-
*
|
|
276
|
-
* These references are captured at module initialization time to protect against
|
|
277
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
278
|
-
*
|
|
279
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
280
|
-
*/
|
|
281
91
|
const _Math = globalThis.Math;
|
|
282
|
-
/**
|
|
283
|
-
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
284
|
-
*/
|
|
285
92
|
const floor = _Math.floor;
|
|
286
93
|
|
|
287
|
-
/**
|
|
288
|
-
* Safe copies of Number built-in methods and constants.
|
|
289
|
-
*
|
|
290
|
-
* These references are captured at module initialization time to protect against
|
|
291
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
292
|
-
*
|
|
293
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
294
|
-
*/
|
|
295
94
|
const _isNaN = globalThis.isNaN;
|
|
296
|
-
/**
|
|
297
|
-
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
298
|
-
*/
|
|
299
95
|
const globalIsNaN = _isNaN;
|
|
300
96
|
|
|
301
|
-
/**
|
|
302
|
-
* Normalizes a given time to the nearest base time window.
|
|
303
|
-
*
|
|
304
|
-
* @param time - The Date object to normalize to the nearest time window
|
|
305
|
-
* @param baseTimeWindow - The size of the time window in minutes for normalization
|
|
306
|
-
* @returns A new Date object normalized to the start of the time window
|
|
307
|
-
*
|
|
308
|
-
* @example Normalizing to 15-minute buckets
|
|
309
|
-
* ```typescript
|
|
310
|
-
* // Round timestamps to 15-minute intervals for analytics bucketing
|
|
311
|
-
* const eventTime = new Date('2024-03-15T14:23:45Z')
|
|
312
|
-
* const bucketTime = normalizeToBaseTimeWindow(eventTime, 15)
|
|
313
|
-
* // => 2024-03-15T14:15:00.000Z
|
|
314
|
-
* ```
|
|
315
|
-
*/
|
|
316
97
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
317
98
|
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
318
99
|
throw createError('Invalid time input');
|
|
@@ -326,90 +107,22 @@
|
|
|
326
107
|
return createDate(normalizedTimeInMs);
|
|
327
108
|
}
|
|
328
109
|
|
|
329
|
-
/**
|
|
330
|
-
* Creates a repeating interval that invokes a callback function at regular intervals.
|
|
331
|
-
*
|
|
332
|
-
* @param callback - The function to invoke at each interval
|
|
333
|
-
* @param interval - Time in milliseconds between each callback invocation
|
|
334
|
-
* @returns A cleanup function that stops the interval when called
|
|
335
|
-
*
|
|
336
|
-
* @example Setting interval with cleanup
|
|
337
|
-
* ```typescript
|
|
338
|
-
* const stopPolling = setIntervalCallback(() => {
|
|
339
|
-
* fetchLatestData()
|
|
340
|
-
* }, 5000)
|
|
341
|
-
*
|
|
342
|
-
* // Later: clean up when component unmounts
|
|
343
|
-
* stopPolling()
|
|
344
|
-
* ```
|
|
345
|
-
*/
|
|
346
110
|
function setIntervalCallback(callback, interval) {
|
|
347
111
|
const timerId = setInterval(callback, interval);
|
|
348
112
|
return () => clearInterval(timerId);
|
|
349
113
|
}
|
|
350
114
|
|
|
351
|
-
/**
|
|
352
|
-
* Safe Promise factory and bound static methods.
|
|
353
|
-
*
|
|
354
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
355
|
-
*/
|
|
356
|
-
/* eslint-disable workspace/lib-require-jsdoc-example */
|
|
357
115
|
const _Promise = globalThis.Promise;
|
|
358
116
|
const _Reflect = globalThis.Reflect;
|
|
359
|
-
/**
|
|
360
|
-
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
361
|
-
* Use this instead of `new Promise()`.
|
|
362
|
-
*
|
|
363
|
-
* @param executor - The executor function.
|
|
364
|
-
* @returns A new Promise instance.
|
|
365
|
-
*/
|
|
366
117
|
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
367
|
-
/**
|
|
368
|
-
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
369
|
-
*/
|
|
370
118
|
_Promise.resolve.bind(_Promise);
|
|
371
|
-
/**
|
|
372
|
-
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
373
|
-
*/
|
|
374
119
|
_Promise.reject.bind(_Promise);
|
|
375
|
-
/**
|
|
376
|
-
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
377
|
-
*/
|
|
378
120
|
_Promise.all.bind(_Promise);
|
|
379
|
-
/**
|
|
380
|
-
* (Safe copy) Returns a Promise that resolves/rejects with the first settled promise.
|
|
381
|
-
*/
|
|
382
121
|
_Promise.race.bind(_Promise);
|
|
383
|
-
/**
|
|
384
|
-
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
385
|
-
*/
|
|
386
122
|
_Promise.allSettled.bind(_Promise);
|
|
387
|
-
/**
|
|
388
|
-
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
389
|
-
*/
|
|
390
123
|
_Promise.any.bind(_Promise);
|
|
391
|
-
/**
|
|
392
|
-
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
393
|
-
* Note: Available only in ES2024+ environments.
|
|
394
|
-
*/
|
|
395
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
396
124
|
_Promise.withResolvers?.bind(_Promise);
|
|
397
125
|
|
|
398
|
-
/**
|
|
399
|
-
* Pauses execution for a specified duration.
|
|
400
|
-
*
|
|
401
|
-
* @param milliseconds - The duration to sleep in milliseconds
|
|
402
|
-
* @returns A promise that resolves after the specified duration
|
|
403
|
-
*
|
|
404
|
-
* @example Implementing retry backoff
|
|
405
|
-
* ```typescript
|
|
406
|
-
* async function retryWithBackoff(attempt: number) {
|
|
407
|
-
* const backoffMs = Math.min(1000 * 2 ** attempt, 30_000)
|
|
408
|
-
* await sleep(backoffMs)
|
|
409
|
-
* return fetchResource()
|
|
410
|
-
* }
|
|
411
|
-
* ```
|
|
412
|
-
*/
|
|
413
126
|
function sleep(milliseconds) {
|
|
414
127
|
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
415
128
|
}
|