@bounded-sh/core 0.0.3 → 0.0.4

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/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export { hasActiveConnection, wsGet, wsSet, wsQuery, wsDelete, wsGetMany } from
7
7
  export * from './types';
8
8
  export { increment, serverTimestamp } from './client/field-values';
9
9
  export type { FieldOperation } from './client/field-values';
10
+ export { now, toSeconds, toMillis } from './time';
10
11
  export { getIdToken, deriveUserIdentityFromIdToken } from './utils/utils';
11
12
  export type { UserIdentity } from './utils/utils';
12
13
  export { WebSessionManager } from './utils/web-session-manager';
package/dist/index.js CHANGED
@@ -6614,6 +6614,73 @@ function serverTimestamp() {
6614
6614
  return { operation: 'time', value: 'now' };
6615
6615
  }
6616
6616
 
6617
+ /**
6618
+ * Time units in Bounded — read this once and never get a 1000× timestamp bug.
6619
+ *
6620
+ * **Bounded's policy/proof layer is Unix SECONDS.** `@time.now` in a rule,
6621
+ * `rollingSum` `windowSeconds`, `scheduledAt`, and any timestamp *field your
6622
+ * policy compares against `@time.now`* are all **seconds**. (This is also what
6623
+ * the chain uses — Solana's on-chain clock is `unix_timestamp` in seconds — so a
6624
+ * single seconds unit works for both onchain and offchain rules.)
6625
+ *
6626
+ * **JavaScript is MILLISECONDS.** `Date.now()`, `new Date().getTime()`, and the
6627
+ * auto-stamped system fields `_createdAt` / `_updatedAt` are all **ms**.
6628
+ *
6629
+ * Comparing across the two (e.g. `@time.now - myField` where `myField` was set
6630
+ * from `Date.now()`) is 1000× off, so a freshness / TTL check silently treats
6631
+ * every row as ancient (or far-future) and drops it — which reads as "realtime
6632
+ * isn't delivering" when the data is actually fine.
6633
+ *
6634
+ * **The rules:**
6635
+ * - To **write** a timestamp a policy will read, prefer **`serverTimestamp()`**
6636
+ * (from this package) — the *server* stamps it in seconds, so it matches
6637
+ * `@time.now` AND can't be forged by the client (use it for TTLs, rate windows,
6638
+ * anti-cheat). Use `now()` only when you need the value in client code before
6639
+ * the write.
6640
+ * - To **compare** timestamps in client/render code, use `now()` (seconds), not
6641
+ * `Date.now()` (ms), and `toSeconds()` to convert the ms system fields
6642
+ * (`_createdAt`/`_updatedAt`) or any `Date.now()` value first.
6643
+ */
6644
+ /**
6645
+ * Current time as a **Unix timestamp in seconds** — the unit Bounded policy rules
6646
+ * use (`@time.now`). Use this (not `Date.now()`) when you need a timestamp in
6647
+ * client code (e.g. a freshness check). For a value you *store* and a policy
6648
+ * reads, prefer the server-authoritative {@link serverTimestamp} instead.
6649
+ *
6650
+ * ```ts
6651
+ * // stale if >15s old — seconds vs seconds ✓
6652
+ * if (now() - doc.lastSeenSeconds > 15) renderStale();
6653
+ * ```
6654
+ */
6655
+ function now() {
6656
+ return Math.floor(Date.now() / 1000);
6657
+ }
6658
+ /**
6659
+ * Convert a JavaScript millisecond timestamp to Bounded's **seconds**. Accepts a
6660
+ * `Date`, or an ms number such as `Date.now()` or a doc's `_createdAt` /
6661
+ * `_updatedAt` system field.
6662
+ *
6663
+ * ```ts
6664
+ * // doc is >15s old (compare the ms system field in seconds):
6665
+ * if (now() - toSeconds(doc._updatedAt) > 15) renderStale();
6666
+ * ```
6667
+ */
6668
+ function toSeconds(msOrDate) {
6669
+ const ms = msOrDate instanceof Date ? msOrDate.getTime() : msOrDate;
6670
+ return Math.floor(ms / 1000);
6671
+ }
6672
+ /**
6673
+ * Convert a Bounded **seconds** timestamp back to JavaScript **milliseconds** —
6674
+ * e.g. to build a `Date` or do client-side date math/formatting.
6675
+ *
6676
+ * ```ts
6677
+ * new Date(toMillis(doc.createdAtSeconds)).toLocaleString();
6678
+ * ```
6679
+ */
6680
+ function toMillis(seconds) {
6681
+ return seconds * 1000;
6682
+ }
6683
+
6617
6684
  // ---------------------------------------------------------------------------
6618
6685
  // realtime-store.ts — Client-side state manager for realtime apps.
6619
6686
  //
@@ -7835,6 +7902,7 @@ exports.isEffectResult = isEffectResult;
7835
7902
  exports.live = live;
7836
7903
  exports.liveIntent = intent;
7837
7904
  exports.liveStatus = status;
7905
+ exports.now = now;
7838
7906
  exports.queryAggregate = queryAggregate;
7839
7907
  exports.reconnectWithNewAuth = reconnectWithNewAuth;
7840
7908
  exports.refreshSession = refreshSession;
@@ -7855,6 +7923,8 @@ exports.signSessionCreateMessage = signSessionCreateMessage;
7855
7923
  exports.signTransaction = signTransaction;
7856
7924
  exports.subscribe = subscribe;
7857
7925
  exports.subscribeLiveView = subscribeView;
7926
+ exports.toMillis = toMillis;
7927
+ exports.toSeconds = toSeconds;
7858
7928
  exports.withEffects = withEffects;
7859
7929
  exports.wsDelete = wsDelete;
7860
7930
  exports.wsGet = wsGet;