@bounded-sh/core 0.0.4 → 0.0.6

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,7 +1,7 @@
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';
package/dist/index.js CHANGED
@@ -4277,6 +4277,91 @@ 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` + numeric `status` AND the absence of a
4343
+ // top-level `_id` (a real stored doc always carries `_id`/`pathId` from the
4344
+ // worker; the envelope wrapper does not). This avoids unwrapping a genuine
4345
+ // document that merely happens to own `data`/`status` fields.
4346
+ if (doc && typeof doc === 'object' && !Array.isArray(doc) &&
4347
+ 'data' in doc && 'status' in doc && typeof doc.status === 'number' &&
4348
+ !('_id' in doc) && !('pathId' in doc)) {
4349
+ doc = doc.data;
4350
+ }
4351
+ if (doc === undefined || doc === null)
4352
+ return null;
4353
+ return withBareId(doc);
4354
+ }
4355
+ // Collection read. The worker body is `{ data: rows[], nextCursor?, status? }`.
4356
+ if (responseData && typeof responseData === 'object' && !Array.isArray(responseData) && Array.isArray(responseData.data)) {
4357
+ return Object.assign(Object.assign({}, responseData), { data: responseData.data.map((row) => withBareId(row)) });
4358
+ }
4359
+ // Some paths return a bare array of rows — map id onto each.
4360
+ if (Array.isArray(responseData)) {
4361
+ return responseData.map((row) => withBareId(row));
4362
+ }
4363
+ return responseData;
4364
+ }
4280
4365
  function hashForKey$1(value) {
4281
4366
  let h = 5381;
4282
4367
  for (let i = 0; i < value.length; i++) {
@@ -4701,7 +4786,7 @@ async function get(path, opts = {}) {
4701
4786
  if (hasActiveConnection()) {
4702
4787
  if (pathIsDocument) {
4703
4788
  const wsResult = await wsGet(normalizedPath);
4704
- const responseData = wsResult;
4789
+ const responseData = normalizeReadResult(wsResult, true);
4705
4790
  if (!opts.bypassCache) {
4706
4791
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4707
4792
  }
@@ -4713,7 +4798,7 @@ async function get(path, opts = {}) {
4713
4798
  sort: undefined,
4714
4799
  includeSubPaths: opts.includeSubPaths,
4715
4800
  });
4716
- const responseData = wsResult;
4801
+ const responseData = normalizeReadResult(wsResult, false);
4717
4802
  if (!opts.bypassCache) {
4718
4803
  getCache[cacheKey] = { data: responseData, expiresAt: now + GET_CACHE_TTL };
4719
4804
  }
@@ -4744,7 +4829,11 @@ async function get(path, opts = {}) {
4744
4829
  const apiPath = `items?path=${path}${promptQueryParam}${filterParam}${sortParam}${includeSubPathsParam}${shapeParam}${limitParam}${cursorParam}`;
4745
4830
  response = await makeApiRequest('GET', apiPath, null, opts._overrides);
4746
4831
  }
4747
- const responseData = response.data;
4832
+ // Normalize the worker's raw body into the SDK's stable shape:
4833
+ // - single-doc path → the resolved document or null (Bug 2), and
4834
+ // - collection path → `{ data, nextCursor }` preserved,
4835
+ // with the bare `id` (leaf doc key) attached to every returned row (Bug 1).
4836
+ const responseData = normalizeReadResult(response.data, pathIsDocument);
4748
4837
  // Cache the response (unless bypassing cache)
4749
4838
  if (!opts.bypassCache) {
4750
4839
  getCache[cacheKey] = {
@@ -4842,11 +4931,16 @@ async function getMany(paths, opts = {}) {
4842
4931
  const normalizedPath = uncachedPaths[i];
4843
4932
  const serverResult = serverResultsMap.get(normalizedPath);
4844
4933
  if (serverResult) {
4845
- results[originalIndex] = serverResult;
4846
- if (!serverResult.error && !opts.bypassCache) {
4934
+ // getMany batches single-doc paths — attach the bare `id` (leaf doc
4935
+ // key) to each returned doc, additive (Bug 1), matching get().
4936
+ const normalizedResult = serverResult.error
4937
+ ? serverResult
4938
+ : Object.assign(Object.assign({}, serverResult), { data: withBareId(serverResult.data) });
4939
+ results[originalIndex] = normalizedResult;
4940
+ if (!normalizedResult.error && !opts.bypassCache) {
4847
4941
  const cacheKey = `${principalKey}|${normalizedPath}:`;
4848
4942
  getCache[cacheKey] = {
4849
- data: serverResult.data,
4943
+ data: normalizedResult.data,
4850
4944
  expiresAt: now + GET_CACHE_TTL
4851
4945
  };
4852
4946
  }
@@ -5391,6 +5485,7 @@ var operations = /*#__PURE__*/Object.freeze({
5391
5485
  aggregate: aggregate,
5392
5486
  clearReadCacheForAuthChange: clearReadCacheForAuthChange,
5393
5487
  count: count,
5488
+ docId: docId,
5394
5489
  get: get,
5395
5490
  getFiles: getFiles,
5396
5491
  getMany: getMany,
@@ -5978,14 +6073,46 @@ function handleServerMessage(connection, message) {
5978
6073
  }
5979
6074
  }
5980
6075
  }
6076
+ /**
6077
+ * Additively attach the bare `id` (leaf doc key) to a subscription row (Bug 1).
6078
+ * Rows carry `_id`/`pathId` = the full document path; this leaves those as-is and
6079
+ * adds `id` = the last path segment so `row.id` is the doc key apps expect. No-ops
6080
+ * for non-objects and rows that already define `id`.
6081
+ */
6082
+ function withSubscriptionId(row) {
6083
+ var _a, _b, _c;
6084
+ if (!row || typeof row !== 'object' || Array.isArray(row))
6085
+ return row;
6086
+ if ('id' in row)
6087
+ return row;
6088
+ 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;
6089
+ if (typeof source !== 'string' || source.length === 0)
6090
+ return row;
6091
+ const leaf = docId(source);
6092
+ if (!leaf)
6093
+ return row;
6094
+ return Object.assign(Object.assign({}, row), { id: leaf });
6095
+ }
6096
+ /**
6097
+ * The `onData` payload follows the path: a collection delivers a bare array of
6098
+ * rows, a single-doc path delivers the document (or null). Attach the bare `id`
6099
+ * to each returned row in either case, additive (Bug 1).
6100
+ */
6101
+ function addIdsToSubscriptionData(data) {
6102
+ if (Array.isArray(data))
6103
+ return data.map(withSubscriptionId);
6104
+ return withSubscriptionId(data);
6105
+ }
5981
6106
  function notifyCallbacks(subscription, data) {
5982
6107
  var _a;
6108
+ // Attach the bare `id` (leaf doc key) to every delivered row (Bug 1).
6109
+ const decorated = addIdsToSubscriptionData(data);
5983
6110
  // Snapshot the callbacks array so that unsubscriptions during
5984
6111
  // notification don't cause callbacks to be skipped.
5985
6112
  const callbacks = subscription.callbacks.slice();
5986
6113
  for (const callback of callbacks) {
5987
6114
  try {
5988
- (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, data);
6115
+ (_a = callback.onData) === null || _a === void 0 ? void 0 : _a.call(callback, decorated);
5989
6116
  }
5990
6117
  catch (error) {
5991
6118
  console.error('[WS v2] Error in subscription callback:', error);
@@ -6076,7 +6203,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6076
6203
  if (cachedEntry && Date.now() - cachedEntry.timestamp < CACHE_TTL && subscriptionOptions.onData) {
6077
6204
  setTimeout(() => {
6078
6205
  var _a;
6079
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, cachedEntry.data);
6206
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(cachedEntry.data));
6080
6207
  }, 0);
6081
6208
  }
6082
6209
  // Get or create connection for this routing target (room-scoped when a
@@ -6106,7 +6233,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
6106
6233
  if (existingSubscription.lastData !== undefined && subscriptionOptions.onData) {
6107
6234
  setTimeout(() => {
6108
6235
  var _a;
6109
- (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, existingSubscription.lastData);
6236
+ (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, addIdsToSubscriptionData(existingSubscription.lastData));
6110
6237
  }, 0);
6111
6238
  }
6112
6239
  return async () => {
@@ -7882,6 +8009,7 @@ exports.count = count;
7882
8009
  exports.createSessionWithSignature = createSessionWithSignature;
7883
8010
  exports.defineLiveModule = defineLiveModule;
7884
8011
  exports.deriveUserIdentityFromIdToken = deriveUserIdentityFromIdToken;
8012
+ exports.docId = docId;
7885
8013
  exports.functions = functions;
7886
8014
  exports.genAuthNonce = genAuthNonce;
7887
8015
  exports.genSolanaMessage = genSolanaMessage;