@bounded-sh/core 0.0.3 → 0.0.5

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.
@@ -25,6 +25,18 @@ export declare class InsufficientBalanceError extends Error {
25
25
  deficitSol: number;
26
26
  constructor(address: string, balanceLamports: number, estimatedCostLamports: number, deficitLamports: number, deficitSol: number);
27
27
  }
28
+ /**
29
+ * Return the leaf document key (last path segment) for a document path.
30
+ *
31
+ * Bounded rows carry `_id` (and `pathId`) set to the FULL document path
32
+ * (e.g. `"rooms/r1/prompts/8rd49se3sg"`). Apps that build a child path from a
33
+ * row naturally want the bare doc key, not the whole path — using the full path
34
+ * doubles it (`rooms/r1/prompts/rooms/r1/prompts/8rd.../votes/...`) → 403/404.
35
+ * `docId` extracts that leaf key. Tolerates leading slashes and a trailing `*`.
36
+ *
37
+ * @example docId("rooms/r1/prompts/8rd49se3sg") // "8rd49se3sg"
38
+ */
39
+ export declare function docId(path: string): string;
28
40
  /**
29
41
  * Options for the get function.
30
42
  */
package/dist/index.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  export { init } from './client/config';
2
2
  export { getConfig, ClientConfig } from './client/config';
3
3
  export { getWebhookKeysUrl } from './client/config';
4
- export { get, getMany, set, setMany, setFile, getFiles, search, queryAggregate, runQuery, runQueryMany, runExpression, runExpressionMany, signMessage, signTransaction, signAndSubmitTransaction, count, aggregate, RequestOverrides, SetOptions, GetOptions, SearchOptions, RunQueryOptions, CountOptions, AggregateOptions, AggregateOperation, AggregateResult, AggregateSpec, AggregateRow, QueryAggregateOptions, RunExpressionOptions, RunExpressionResult, InsufficientBalanceError, GetManyResult } from './client/operations';
4
+ export { get, getMany, docId, set, setMany, setFile, getFiles, search, queryAggregate, runQuery, runQueryMany, runExpression, runExpressionMany, signMessage, signTransaction, signAndSubmitTransaction, count, aggregate, RequestOverrides, SetOptions, GetOptions, SearchOptions, RunQueryOptions, CountOptions, AggregateOptions, AggregateOperation, AggregateResult, AggregateSpec, AggregateRow, QueryAggregateOptions, RunExpressionOptions, RunExpressionResult, InsufficientBalanceError, GetManyResult } from './client/operations';
5
5
  export { subscribe, closeAllSubscriptions, clearCache, getCachedData, reconnectWithNewAuth } from './client/subscription';
6
6
  export { hasActiveConnection, wsGet, wsSet, wsQuery, wsDelete, wsGetMany } from './client/subscription-v2';
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
@@ -4277,6 +4277,87 @@ const pendingRequests = {};
4277
4277
  const GET_CACHE_TTL = 500; // Adjust this value as needed (in milliseconds)
4278
4278
  // Last time we cleaned up the cache
4279
4279
  let lastCacheCleanup = Date.now();
4280
+ /**
4281
+ * Return the leaf document key (last path segment) for a document path.
4282
+ *
4283
+ * Bounded rows carry `_id` (and `pathId`) set to the FULL document path
4284
+ * (e.g. `"rooms/r1/prompts/8rd49se3sg"`). Apps that build a child path from a
4285
+ * row naturally want the bare doc key, not the whole path — using the full path
4286
+ * doubles it (`rooms/r1/prompts/rooms/r1/prompts/8rd.../votes/...`) → 403/404.
4287
+ * `docId` extracts that leaf key. Tolerates leading slashes and a trailing `*`.
4288
+ *
4289
+ * @example docId("rooms/r1/prompts/8rd49se3sg") // "8rd49se3sg"
4290
+ */
4291
+ function docId(path) {
4292
+ if (typeof path !== 'string')
4293
+ return '';
4294
+ let p = path.startsWith('/') ? path.slice(1) : path;
4295
+ if (p.endsWith('*') && p.length > 1)
4296
+ p = p.slice(0, -1);
4297
+ if (p.endsWith('/'))
4298
+ p = p.slice(0, -1);
4299
+ const segments = p.split('/').filter(Boolean);
4300
+ return segments.length ? segments[segments.length - 1] : '';
4301
+ }
4302
+ /**
4303
+ * Additively attach a convenience bare `id` (the leaf doc key) to a returned row.
4304
+ *
4305
+ * Rows already carry `_id`/`pathId` = the full document path; this leaves those
4306
+ * untouched and adds `id` = the last path segment so `row.id` is the doc key apps
4307
+ * expect. No-ops for non-objects, arrays, and rows that already define `id`
4308
+ * (never clobber a user field named `id`). Source for the leaf is `_id` (falling
4309
+ * back to `pathId`/`relativePath`/`absolutePath`).
4310
+ */
4311
+ function withBareId(row) {
4312
+ var _a, _b, _c;
4313
+ if (!row || typeof row !== 'object' || Array.isArray(row))
4314
+ return row;
4315
+ const r = row;
4316
+ if ('id' in r)
4317
+ return row; // don't clobber an explicit user field
4318
+ const source = (_c = (_b = (_a = r._id) !== null && _a !== void 0 ? _a : r.pathId) !== null && _b !== void 0 ? _b : r.relativePath) !== null && _c !== void 0 ? _c : r.absolutePath;
4319
+ if (typeof source !== 'string' || source.length === 0)
4320
+ return row;
4321
+ const leaf = docId(source);
4322
+ if (!leaf)
4323
+ return row;
4324
+ return Object.assign(Object.assign({}, r), { id: leaf });
4325
+ }
4326
+ /**
4327
+ * Normalize a raw read response from the worker into the SDK's stable shape and
4328
+ * attach the bare `id` to every returned row (Bug 1 + Bug 2).
4329
+ *
4330
+ * - Single-document path: returns EXACTLY ONE shape — the resolved document, or
4331
+ * `null` if missing (Firebase/Mongo convention). Unwraps the ambiguous
4332
+ * `{ data, status }` envelope the worker sometimes emits for a single doc so a
4333
+ * single-doc `get` never sometimes-returns the doc and sometimes the envelope.
4334
+ * - Collection path: preserves the `{ data, nextCursor, ... }` envelope and maps
4335
+ * the bare `id` onto each row in `data`.
4336
+ */
4337
+ function normalizeReadResult(responseData, pathIsDocument) {
4338
+ if (pathIsDocument) {
4339
+ let doc = responseData;
4340
+ // Unwrap the single-doc envelope `{ data, status }` (Bug 2). A real document
4341
+ // never has the exact shape `{ data, status }` with nothing else meaningful,
4342
+ // so detect the envelope by `data` being present alongside a numeric `status`.
4343
+ if (doc && typeof doc === 'object' && !Array.isArray(doc) &&
4344
+ 'data' in doc && 'status' in doc && typeof doc.status === 'number') {
4345
+ doc = doc.data;
4346
+ }
4347
+ if (doc === undefined || doc === null)
4348
+ return null;
4349
+ return withBareId(doc);
4350
+ }
4351
+ // Collection read. The worker body is `{ data: rows[], nextCursor?, status? }`.
4352
+ if (responseData && typeof responseData === 'object' && !Array.isArray(responseData) && Array.isArray(responseData.data)) {
4353
+ return Object.assign(Object.assign({}, responseData), { data: responseData.data.map((row) => withBareId(row)) });
4354
+ }
4355
+ // Some paths return a bare array of rows — map id onto each.
4356
+ if (Array.isArray(responseData)) {
4357
+ return responseData.map((row) => withBareId(row));
4358
+ }
4359
+ return responseData;
4360
+ }
4280
4361
  function hashForKey$1(value) {
4281
4362
  let h = 5381;
4282
4363
  for (let i = 0; i < value.length; i++) {
@@ -4701,7 +4782,7 @@ async function get(path, opts = {}) {
4701
4782
  if (hasActiveConnection()) {
4702
4783
  if (pathIsDocument) {
4703
4784
  const wsResult = await wsGet(normalizedPath);
4704
- const responseData = wsResult;
4785
+ const responseData = normalizeReadResult(wsResult, true);
4705
4786
  if (!opts.bypassCache) {
4706
4787
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4707
4788
  }
@@ -4713,7 +4794,7 @@ async function get(path, opts = {}) {
4713
4794
  sort: undefined,
4714
4795
  includeSubPaths: opts.includeSubPaths,
4715
4796
  });
4716
- const responseData = wsResult;
4797
+ const responseData = normalizeReadResult(wsResult, false);
4717
4798
  if (!opts.bypassCache) {
4718
4799
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4719
4800
  }
@@ -4744,7 +4825,11 @@ async function get(path, opts = {}) {
4744
4825
  const apiPath = `items?path=${path}${promptQueryParam}${filterParam}${sortParam}${includeSubPathsParam}${shapeParam}${limitParam}${cursorParam}`;
4745
4826
  response = await makeApiRequest('GET', apiPath, null, opts._overrides);
4746
4827
  }
4747
- const responseData = response.data;
4828
+ // Normalize the worker's raw body into the SDK's stable shape:
4829
+ // - single-doc path → the resolved document or null (Bug 2), and
4830
+ // - collection path → `{ data, nextCursor }` preserved,
4831
+ // with the bare `id` (leaf doc key) attached to every returned row (Bug 1).
4832
+ const responseData = normalizeReadResult(response.data, pathIsDocument);
4748
4833
  // Cache the response (unless bypassing cache)
4749
4834
  if (!opts.bypassCache) {
4750
4835
  getCache[cacheKey] = {
@@ -4842,11 +4927,16 @@ async function getMany(paths, opts = {}) {
4842
4927
  const normalizedPath = uncachedPaths[i];
4843
4928
  const serverResult = serverResultsMap.get(normalizedPath);
4844
4929
  if (serverResult) {
4845
- results[originalIndex] = serverResult;
4846
- if (!serverResult.error && !opts.bypassCache) {
4930
+ // getMany batches single-doc paths — attach the bare `id` (leaf doc
4931
+ // key) to each returned doc, additive (Bug 1), matching get().
4932
+ const normalizedResult = serverResult.error
4933
+ ? serverResult
4934
+ : Object.assign(Object.assign({}, serverResult), { data: withBareId(serverResult.data) });
4935
+ results[originalIndex] = normalizedResult;
4936
+ if (!normalizedResult.error && !opts.bypassCache) {
4847
4937
  const cacheKey = `${principalKey}|${normalizedPath}:`;
4848
4938
  getCache[cacheKey] = {
4849
- data: serverResult.data,
4939
+ data: normalizedResult.data,
4850
4940
  expiresAt: now + GET_CACHE_TTL
4851
4941
  };
4852
4942
  }
@@ -5391,6 +5481,7 @@ var operations = /*#__PURE__*/Object.freeze({
5391
5481
  aggregate: aggregate,
5392
5482
  clearReadCacheForAuthChange: clearReadCacheForAuthChange,
5393
5483
  count: count,
5484
+ docId: docId,
5394
5485
  get: get,
5395
5486
  getFiles: getFiles,
5396
5487
  getMany: getMany,
@@ -5978,14 +6069,46 @@ function handleServerMessage(connection, message) {
5978
6069
  }
5979
6070
  }
5980
6071
  }
6072
+ /**
6073
+ * Additively attach the bare `id` (leaf doc key) to a subscription row (Bug 1).
6074
+ * Rows carry `_id`/`pathId` = the full document path; this leaves those as-is and
6075
+ * adds `id` = the last path segment so `row.id` is the doc key apps expect. No-ops
6076
+ * for non-objects and rows that already define `id`.
6077
+ */
6078
+ function withSubscriptionId(row) {
6079
+ var _a, _b, _c;
6080
+ if (!row || typeof row !== 'object' || Array.isArray(row))
6081
+ return row;
6082
+ if ('id' in row)
6083
+ return row;
6084
+ const source = (_c = (_b = (_a = row._id) !== null && _a !== void 0 ? _a : row.pathId) !== null && _b !== void 0 ? _b : row.relativePath) !== null && _c !== void 0 ? _c : row.absolutePath;
6085
+ if (typeof source !== 'string' || source.length === 0)
6086
+ return row;
6087
+ const leaf = docId(source);
6088
+ if (!leaf)
6089
+ return row;
6090
+ return Object.assign(Object.assign({}, row), { id: leaf });
6091
+ }
6092
+ /**
6093
+ * The `onData` payload follows the path: a collection delivers a bare array of
6094
+ * rows, a single-doc path delivers the document (or null). Attach the bare `id`
6095
+ * to each returned row in either case, additive (Bug 1).
6096
+ */
6097
+ function addIdsToSubscriptionData(data) {
6098
+ if (Array.isArray(data))
6099
+ return data.map(withSubscriptionId);
6100
+ return withSubscriptionId(data);
6101
+ }
5981
6102
  function notifyCallbacks(subscription, data) {
5982
6103
  var _a;
6104
+ // Attach the bare `id` (leaf doc key) to every delivered row (Bug 1).
6105
+ const decorated = addIdsToSubscriptionData(data);
5983
6106
  // Snapshot the callbacks array so that unsubscriptions during
5984
6107
  // notification don't cause callbacks to be skipped.
5985
6108
  const callbacks = subscription.callbacks.slice();
5986
6109
  for (const callback of callbacks) {
5987
6110
  try {
5988
- (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, data);
6111
+ (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, decorated);
5989
6112
  }
5990
6113
  catch (error) {
5991
6114
  console.error('[WS v2] Error in subscription callback:', error);
@@ -6076,7 +6199,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6076
6199
  if (cachedEntry && Date.now() - cachedEntry.timestamp < CACHE_TTL && subscriptionOptions.onData) {
6077
6200
  setTimeout(() => {
6078
6201
  var _a;
6079
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, cachedEntry.data);
6202
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(cachedEntry.data));
6080
6203
  }, 0);
6081
6204
  }
6082
6205
  // Get or create connection for this routing target (room-scoped when a
@@ -6106,7 +6229,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6106
6229
  if (existingSubscription.lastData !== undefined && subscriptionOptions.onData) {
6107
6230
  setTimeout(() => {
6108
6231
  var _a;
6109
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, existingSubscription.lastData);
6232
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(existingSubscription.lastData));
6110
6233
  }, 0);
6111
6234
  }
6112
6235
  return async () => {
@@ -6614,6 +6737,73 @@ function serverTimestamp() {
6614
6737
  return { operation: 'time', value: 'now' };
6615
6738
  }
6616
6739
 
6740
+ /**
6741
+ * Time units in Bounded — read this once and never get a 1000× timestamp bug.
6742
+ *
6743
+ * **Bounded's policy/proof layer is Unix SECONDS.** `@time.now` in a rule,
6744
+ * `rollingSum` `windowSeconds`, `scheduledAt`, and any timestamp *field your
6745
+ * policy compares against `@time.now`* are all **seconds**. (This is also what
6746
+ * the chain uses — Solana's on-chain clock is `unix_timestamp` in seconds — so a
6747
+ * single seconds unit works for both onchain and offchain rules.)
6748
+ *
6749
+ * **JavaScript is MILLISECONDS.** `Date.now()`, `new Date().getTime()`, and the
6750
+ * auto-stamped system fields `_createdAt` / `_updatedAt` are all **ms**.
6751
+ *
6752
+ * Comparing across the two (e.g. `@time.now - myField` where `myField` was set
6753
+ * from `Date.now()`) is 1000× off, so a freshness / TTL check silently treats
6754
+ * every row as ancient (or far-future) and drops it — which reads as "realtime
6755
+ * isn't delivering" when the data is actually fine.
6756
+ *
6757
+ * **The rules:**
6758
+ * - To **write** a timestamp a policy will read, prefer **`serverTimestamp()`**
6759
+ * (from this package) — the *server* stamps it in seconds, so it matches
6760
+ * `@time.now` AND can't be forged by the client (use it for TTLs, rate windows,
6761
+ * anti-cheat). Use `now()` only when you need the value in client code before
6762
+ * the write.
6763
+ * - To **compare** timestamps in client/render code, use `now()` (seconds), not
6764
+ * `Date.now()` (ms), and `toSeconds()` to convert the ms system fields
6765
+ * (`_createdAt`/`_updatedAt`) or any `Date.now()` value first.
6766
+ */
6767
+ /**
6768
+ * Current time as a **Unix timestamp in seconds** — the unit Bounded policy rules
6769
+ * use (`@time.now`). Use this (not `Date.now()`) when you need a timestamp in
6770
+ * client code (e.g. a freshness check). For a value you *store* and a policy
6771
+ * reads, prefer the server-authoritative {@link serverTimestamp} instead.
6772
+ *
6773
+ * ```ts
6774
+ * // stale if >15s old — seconds vs seconds ✓
6775
+ * if (now() - doc.lastSeenSeconds > 15) renderStale();
6776
+ * ```
6777
+ */
6778
+ function now() {
6779
+ return Math.floor(Date.now() / 1000);
6780
+ }
6781
+ /**
6782
+ * Convert a JavaScript millisecond timestamp to Bounded's **seconds**. Accepts a
6783
+ * `Date`, or an ms number such as `Date.now()` or a doc's `_createdAt` /
6784
+ * `_updatedAt` system field.
6785
+ *
6786
+ * ```ts
6787
+ * // doc is >15s old (compare the ms system field in seconds):
6788
+ * if (now() - toSeconds(doc._updatedAt) > 15) renderStale();
6789
+ * ```
6790
+ */
6791
+ function toSeconds(msOrDate) {
6792
+ const ms = msOrDate instanceof Date ? msOrDate.getTime() : msOrDate;
6793
+ return Math.floor(ms / 1000);
6794
+ }
6795
+ /**
6796
+ * Convert a Bounded **seconds** timestamp back to JavaScript **milliseconds** —
6797
+ * e.g. to build a `Date` or do client-side date math/formatting.
6798
+ *
6799
+ * ```ts
6800
+ * new Date(toMillis(doc.createdAtSeconds)).toLocaleString();
6801
+ * ```
6802
+ */
6803
+ function toMillis(seconds) {
6804
+ return seconds * 1000;
6805
+ }
6806
+
6617
6807
  // ---------------------------------------------------------------------------
6618
6808
  // realtime-store.ts — Client-side state manager for realtime apps.
6619
6809
  //
@@ -7815,6 +8005,7 @@ exports.count = count;
7815
8005
  exports.createSessionWithSignature = createSessionWithSignature;
7816
8006
  exports.defineLiveModule = defineLiveModule;
7817
8007
  exports.deriveUserIdentityFromIdToken = deriveUserIdentityFromIdToken;
8008
+ exports.docId = docId;
7818
8009
  exports.functions = functions;
7819
8010
  exports.genAuthNonce = genAuthNonce;
7820
8011
  exports.genSolanaMessage = genSolanaMessage;
@@ -7835,6 +8026,7 @@ exports.isEffectResult = isEffectResult;
7835
8026
  exports.live = live;
7836
8027
  exports.liveIntent = intent;
7837
8028
  exports.liveStatus = status;
8029
+ exports.now = now;
7838
8030
  exports.queryAggregate = queryAggregate;
7839
8031
  exports.reconnectWithNewAuth = reconnectWithNewAuth;
7840
8032
  exports.refreshSession = refreshSession;
@@ -7855,6 +8047,8 @@ exports.signSessionCreateMessage = signSessionCreateMessage;
7855
8047
  exports.signTransaction = signTransaction;
7856
8048
  exports.subscribe = subscribe;
7857
8049
  exports.subscribeLiveView = subscribeView;
8050
+ exports.toMillis = toMillis;
8051
+ exports.toSeconds = toSeconds;
7858
8052
  exports.withEffects = withEffects;
7859
8053
  exports.wsDelete = wsDelete;
7860
8054
  exports.wsGet = wsGet;