@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/time.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Time units in Bounded — read this once and never get a 1000× timestamp bug.
3
+ *
4
+ * **Bounded's policy/proof layer is Unix SECONDS.** `@time.now` in a rule,
5
+ * `rollingSum` `windowSeconds`, `scheduledAt`, and any timestamp *field your
6
+ * policy compares against `@time.now`* are all **seconds**. (This is also what
7
+ * the chain uses — Solana's on-chain clock is `unix_timestamp` in seconds — so a
8
+ * single seconds unit works for both onchain and offchain rules.)
9
+ *
10
+ * **JavaScript is MILLISECONDS.** `Date.now()`, `new Date().getTime()`, and the
11
+ * auto-stamped system fields `_createdAt` / `_updatedAt` are all **ms**.
12
+ *
13
+ * Comparing across the two (e.g. `@time.now - myField` where `myField` was set
14
+ * from `Date.now()`) is 1000× off, so a freshness / TTL check silently treats
15
+ * every row as ancient (or far-future) and drops it — which reads as "realtime
16
+ * isn't delivering" when the data is actually fine.
17
+ *
18
+ * **The rules:**
19
+ * - To **write** a timestamp a policy will read, prefer **`serverTimestamp()`**
20
+ * (from this package) — the *server* stamps it in seconds, so it matches
21
+ * `@time.now` AND can't be forged by the client (use it for TTLs, rate windows,
22
+ * anti-cheat). Use `now()` only when you need the value in client code before
23
+ * the write.
24
+ * - To **compare** timestamps in client/render code, use `now()` (seconds), not
25
+ * `Date.now()` (ms), and `toSeconds()` to convert the ms system fields
26
+ * (`_createdAt`/`_updatedAt`) or any `Date.now()` value first.
27
+ */
28
+ /**
29
+ * Current time as a **Unix timestamp in seconds** — the unit Bounded policy rules
30
+ * use (`@time.now`). Use this (not `Date.now()`) when you need a timestamp in
31
+ * client code (e.g. a freshness check). For a value you *store* and a policy
32
+ * reads, prefer the server-authoritative {@link serverTimestamp} instead.
33
+ *
34
+ * ```ts
35
+ * // stale if >15s old — seconds vs seconds ✓
36
+ * if (now() - doc.lastSeenSeconds > 15) renderStale();
37
+ * ```
38
+ */
39
+ export declare function now(): number;
40
+ /**
41
+ * Convert a JavaScript millisecond timestamp to Bounded's **seconds**. Accepts a
42
+ * `Date`, or an ms number such as `Date.now()` or a doc's `_createdAt` /
43
+ * `_updatedAt` system field.
44
+ *
45
+ * ```ts
46
+ * // doc is >15s old (compare the ms system field in seconds):
47
+ * if (now() - toSeconds(doc._updatedAt) > 15) renderStale();
48
+ * ```
49
+ */
50
+ export declare function toSeconds(msOrDate: number | Date): number;
51
+ /**
52
+ * Convert a Bounded **seconds** timestamp back to JavaScript **milliseconds** —
53
+ * e.g. to build a `Date` or do client-side date math/formatting.
54
+ *
55
+ * ```ts
56
+ * new Date(toMillis(doc.createdAtSeconds)).toLocaleString();
57
+ * ```
58
+ */
59
+ export declare function toMillis(seconds: number): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bounded-sh/core",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Core functionality for Poof SDK",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",