@bounded-sh/core 0.0.4 → 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.
package/dist/index.mjs CHANGED
@@ -4257,6 +4257,87 @@ const pendingRequests = {};
4257
4257
  const GET_CACHE_TTL = 500; // Adjust this value as needed (in milliseconds)
4258
4258
  // Last time we cleaned up the cache
4259
4259
  let lastCacheCleanup = Date.now();
4260
+ /**
4261
+ * Return the leaf document key (last path segment) for a document path.
4262
+ *
4263
+ * Bounded rows carry `_id` (and `pathId`) set to the FULL document path
4264
+ * (e.g. `"rooms/r1/prompts/8rd49se3sg"`). Apps that build a child path from a
4265
+ * row naturally want the bare doc key, not the whole path — using the full path
4266
+ * doubles it (`rooms/r1/prompts/rooms/r1/prompts/8rd.../votes/...`) → 403/404.
4267
+ * `docId` extracts that leaf key. Tolerates leading slashes and a trailing `*`.
4268
+ *
4269
+ * @example docId("rooms/r1/prompts/8rd49se3sg") // "8rd49se3sg"
4270
+ */
4271
+ function docId(path) {
4272
+ if (typeof path !== 'string')
4273
+ return '';
4274
+ let p = path.startsWith('/') ? path.slice(1) : path;
4275
+ if (p.endsWith('*') && p.length > 1)
4276
+ p = p.slice(0, -1);
4277
+ if (p.endsWith('/'))
4278
+ p = p.slice(0, -1);
4279
+ const segments = p.split('/').filter(Boolean);
4280
+ return segments.length ? segments[segments.length - 1] : '';
4281
+ }
4282
+ /**
4283
+ * Additively attach a convenience bare `id` (the leaf doc key) to a returned row.
4284
+ *
4285
+ * Rows already carry `_id`/`pathId` = the full document path; this leaves those
4286
+ * untouched and adds `id` = the last path segment so `row.id` is the doc key apps
4287
+ * expect. No-ops for non-objects, arrays, and rows that already define `id`
4288
+ * (never clobber a user field named `id`). Source for the leaf is `_id` (falling
4289
+ * back to `pathId`/`relativePath`/`absolutePath`).
4290
+ */
4291
+ function withBareId(row) {
4292
+ var _a, _b, _c;
4293
+ if (!row || typeof row !== 'object' || Array.isArray(row))
4294
+ return row;
4295
+ const r = row;
4296
+ if ('id' in r)
4297
+ return row; // don't clobber an explicit user field
4298
+ 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;
4299
+ if (typeof source !== 'string' || source.length === 0)
4300
+ return row;
4301
+ const leaf = docId(source);
4302
+ if (!leaf)
4303
+ return row;
4304
+ return Object.assign(Object.assign({}, r), { id: leaf });
4305
+ }
4306
+ /**
4307
+ * Normalize a raw read response from the worker into the SDK's stable shape and
4308
+ * attach the bare `id` to every returned row (Bug 1 + Bug 2).
4309
+ *
4310
+ * - Single-document path: returns EXACTLY ONE shape — the resolved document, or
4311
+ * `null` if missing (Firebase/Mongo convention). Unwraps the ambiguous
4312
+ * `{ data, status }` envelope the worker sometimes emits for a single doc so a
4313
+ * single-doc `get` never sometimes-returns the doc and sometimes the envelope.
4314
+ * - Collection path: preserves the `{ data, nextCursor, ... }` envelope and maps
4315
+ * the bare `id` onto each row in `data`.
4316
+ */
4317
+ function normalizeReadResult(responseData, pathIsDocument) {
4318
+ if (pathIsDocument) {
4319
+ let doc = responseData;
4320
+ // Unwrap the single-doc envelope `{ data, status }` (Bug 2). A real document
4321
+ // never has the exact shape `{ data, status }` with nothing else meaningful,
4322
+ // so detect the envelope by `data` being present alongside a numeric `status`.
4323
+ if (doc && typeof doc === 'object' && !Array.isArray(doc) &&
4324
+ 'data' in doc && 'status' in doc && typeof doc.status === 'number') {
4325
+ doc = doc.data;
4326
+ }
4327
+ if (doc === undefined || doc === null)
4328
+ return null;
4329
+ return withBareId(doc);
4330
+ }
4331
+ // Collection read. The worker body is `{ data: rows[], nextCursor?, status? }`.
4332
+ if (responseData && typeof responseData === 'object' && !Array.isArray(responseData) && Array.isArray(responseData.data)) {
4333
+ return Object.assign(Object.assign({}, responseData), { data: responseData.data.map((row) => withBareId(row)) });
4334
+ }
4335
+ // Some paths return a bare array of rows — map id onto each.
4336
+ if (Array.isArray(responseData)) {
4337
+ return responseData.map((row) => withBareId(row));
4338
+ }
4339
+ return responseData;
4340
+ }
4260
4341
  function hashForKey$1(value) {
4261
4342
  let h = 5381;
4262
4343
  for (let i = 0; i < value.length; i++) {
@@ -4681,7 +4762,7 @@ async function get(path, opts = {}) {
4681
4762
  if (hasActiveConnection()) {
4682
4763
  if (pathIsDocument) {
4683
4764
  const wsResult = await wsGet(normalizedPath);
4684
- const responseData = wsResult;
4765
+ const responseData = normalizeReadResult(wsResult, true);
4685
4766
  if (!opts.bypassCache) {
4686
4767
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4687
4768
  }
@@ -4693,7 +4774,7 @@ async function get(path, opts = {}) {
4693
4774
  sort: undefined,
4694
4775
  includeSubPaths: opts.includeSubPaths,
4695
4776
  });
4696
- const responseData = wsResult;
4777
+ const responseData = normalizeReadResult(wsResult, false);
4697
4778
  if (!opts.bypassCache) {
4698
4779
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4699
4780
  }
@@ -4724,7 +4805,11 @@ async function get(path, opts = {}) {
4724
4805
  const apiPath = `items?path=${path}${promptQueryParam}${filterParam}${sortParam}${includeSubPathsParam}${shapeParam}${limitParam}${cursorParam}`;
4725
4806
  response = await makeApiRequest('GET', apiPath, null, opts._overrides);
4726
4807
  }
4727
- const responseData = response.data;
4808
+ // Normalize the worker's raw body into the SDK's stable shape:
4809
+ // - single-doc path → the resolved document or null (Bug 2), and
4810
+ // - collection path → `{ data, nextCursor }` preserved,
4811
+ // with the bare `id` (leaf doc key) attached to every returned row (Bug 1).
4812
+ const responseData = normalizeReadResult(response.data, pathIsDocument);
4728
4813
  // Cache the response (unless bypassing cache)
4729
4814
  if (!opts.bypassCache) {
4730
4815
  getCache[cacheKey] = {
@@ -4822,11 +4907,16 @@ async function getMany(paths, opts = {}) {
4822
4907
  const normalizedPath = uncachedPaths[i];
4823
4908
  const serverResult = serverResultsMap.get(normalizedPath);
4824
4909
  if (serverResult) {
4825
- results[originalIndex] = serverResult;
4826
- if (!serverResult.error && !opts.bypassCache) {
4910
+ // getMany batches single-doc paths — attach the bare `id` (leaf doc
4911
+ // key) to each returned doc, additive (Bug 1), matching get().
4912
+ const normalizedResult = serverResult.error
4913
+ ? serverResult
4914
+ : Object.assign(Object.assign({}, serverResult), { data: withBareId(serverResult.data) });
4915
+ results[originalIndex] = normalizedResult;
4916
+ if (!normalizedResult.error && !opts.bypassCache) {
4827
4917
  const cacheKey = `${principalKey}|${normalizedPath}:`;
4828
4918
  getCache[cacheKey] = {
4829
- data: serverResult.data,
4919
+ data: normalizedResult.data,
4830
4920
  expiresAt: now + GET_CACHE_TTL
4831
4921
  };
4832
4922
  }
@@ -5371,6 +5461,7 @@ var operations = /*#__PURE__*/Object.freeze({
5371
5461
  aggregate: aggregate,
5372
5462
  clearReadCacheForAuthChange: clearReadCacheForAuthChange,
5373
5463
  count: count,
5464
+ docId: docId,
5374
5465
  get: get,
5375
5466
  getFiles: getFiles,
5376
5467
  getMany: getMany,
@@ -5958,14 +6049,46 @@ function handleServerMessage(connection, message) {
5958
6049
  }
5959
6050
  }
5960
6051
  }
6052
+ /**
6053
+ * Additively attach the bare `id` (leaf doc key) to a subscription row (Bug 1).
6054
+ * Rows carry `_id`/`pathId` = the full document path; this leaves those as-is and
6055
+ * adds `id` = the last path segment so `row.id` is the doc key apps expect. No-ops
6056
+ * for non-objects and rows that already define `id`.
6057
+ */
6058
+ function withSubscriptionId(row) {
6059
+ var _a, _b, _c;
6060
+ if (!row || typeof row !== 'object' || Array.isArray(row))
6061
+ return row;
6062
+ if ('id' in row)
6063
+ return row;
6064
+ 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;
6065
+ if (typeof source !== 'string' || source.length === 0)
6066
+ return row;
6067
+ const leaf = docId(source);
6068
+ if (!leaf)
6069
+ return row;
6070
+ return Object.assign(Object.assign({}, row), { id: leaf });
6071
+ }
6072
+ /**
6073
+ * The `onData` payload follows the path: a collection delivers a bare array of
6074
+ * rows, a single-doc path delivers the document (or null). Attach the bare `id`
6075
+ * to each returned row in either case, additive (Bug 1).
6076
+ */
6077
+ function addIdsToSubscriptionData(data) {
6078
+ if (Array.isArray(data))
6079
+ return data.map(withSubscriptionId);
6080
+ return withSubscriptionId(data);
6081
+ }
5961
6082
  function notifyCallbacks(subscription, data) {
5962
6083
  var _a;
6084
+ // Attach the bare `id` (leaf doc key) to every delivered row (Bug 1).
6085
+ const decorated = addIdsToSubscriptionData(data);
5963
6086
  // Snapshot the callbacks array so that unsubscriptions during
5964
6087
  // notification don't cause callbacks to be skipped.
5965
6088
  const callbacks = subscription.callbacks.slice();
5966
6089
  for (const callback of callbacks) {
5967
6090
  try {
5968
- (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, data);
6091
+ (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, decorated);
5969
6092
  }
5970
6093
  catch (error) {
5971
6094
  console.error('[WS v2] Error in subscription callback:', error);
@@ -6056,7 +6179,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6056
6179
  if (cachedEntry && Date.now() - cachedEntry.timestamp < CACHE_TTL && subscriptionOptions.onData) {
6057
6180
  setTimeout(() => {
6058
6181
  var _a;
6059
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, cachedEntry.data);
6182
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(cachedEntry.data));
6060
6183
  }, 0);
6061
6184
  }
6062
6185
  // Get or create connection for this routing target (room-scoped when a
@@ -6086,7 +6209,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6086
6209
  if (existingSubscription.lastData !== undefined && subscriptionOptions.onData) {
6087
6210
  setTimeout(() => {
6088
6211
  var _a;
6089
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, existingSubscription.lastData);
6212
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(existingSubscription.lastData));
6090
6213
  }, 0);
6091
6214
  }
6092
6215
  return async () => {
@@ -7845,5 +7968,5 @@ function defineLiveModule(mod) {
7845
7968
  return mod;
7846
7969
  }
7847
7970
 
7848
- export { EFFECT_INTENT_ADDRESS, FunctionInvokeError, InsufficientBalanceError, LiveIntentError, ReactNativeSessionManager, RealtimeStore, ServerSessionManager, WebSessionManager, aggregate, buildSetDocumentsTransaction, clearCache, closeAllSubscriptions, convertRemainingAccounts, count, createSessionWithSignature, defineLiveModule, deriveUserIdentityFromIdToken, functions, genAuthNonce, genSolanaMessage, get, getActiveSessionManager, getCachedData, getConfig, getFiles, getIdToken, getMany, getRealtimeStore, getWebhookKeysUrl, hasActiveConnection, increment, init, invoke as invokeFunction, isEffectResult, live, intent as liveIntent, status as liveStatus, now, queryAggregate, reconnectWithNewAuth, refreshSession, resetRealtimeStore, revokeSession, runExpression, runExpressionMany, runQuery, runQueryMany, search, serverTimestamp, set, setFile, setMany, signAndSubmitTransaction, signMessage, signSessionCreateMessage, signTransaction, subscribe, subscribeView as subscribeLiveView, toMillis, toSeconds, withEffects, wsDelete, wsGet, wsGetMany, wsQuery, wsSet };
7971
+ export { EFFECT_INTENT_ADDRESS, FunctionInvokeError, InsufficientBalanceError, LiveIntentError, ReactNativeSessionManager, RealtimeStore, ServerSessionManager, WebSessionManager, aggregate, buildSetDocumentsTransaction, clearCache, closeAllSubscriptions, convertRemainingAccounts, count, createSessionWithSignature, defineLiveModule, deriveUserIdentityFromIdToken, docId, functions, genAuthNonce, genSolanaMessage, get, getActiveSessionManager, getCachedData, getConfig, getFiles, getIdToken, getMany, getRealtimeStore, getWebhookKeysUrl, hasActiveConnection, increment, init, invoke as invokeFunction, isEffectResult, live, intent as liveIntent, status as liveStatus, now, queryAggregate, reconnectWithNewAuth, refreshSession, resetRealtimeStore, revokeSession, runExpression, runExpressionMany, runQuery, runQueryMany, search, serverTimestamp, set, setFile, setMany, signAndSubmitTransaction, signMessage, signSessionCreateMessage, signTransaction, subscribe, subscribeView as subscribeLiveView, toMillis, toSeconds, withEffects, wsDelete, wsGet, wsGetMany, wsQuery, wsSet };
7849
7972
  //# sourceMappingURL=index.mjs.map