@datafn/server 0.1.4-maint.0 → 0.1.5-maint.0

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.js CHANGED
@@ -1331,7 +1331,11 @@ function validateQuery(query, index, basePath = "$", limits) {
1331
1331
  }
1332
1332
  }
1333
1333
  if (q.temporal !== void 0) {
1334
- const temporalResult = validateTemporal(q, fieldNames);
1334
+ const temporalResult = validateTemporal(
1335
+ q,
1336
+ fieldNames,
1337
+ (field) => resolveFieldType(index, resourceName, field)
1338
+ );
1335
1339
  if (!temporalResult.ok) {
1336
1340
  return temporalResult;
1337
1341
  }
@@ -1575,7 +1579,19 @@ function validateGroupBy(q, fieldNames, relationNames) {
1575
1579
  }
1576
1580
  return vOk(void 0);
1577
1581
  }
1578
- function validateTemporal(q, fieldNames) {
1582
+ function resolveFieldType(index, resourceName, field) {
1583
+ const resource = index.schema.resources.find((r) => r.name === resourceName);
1584
+ if (!resource) return void 0;
1585
+ const capabilityFields = getCapabilityFields(
1586
+ resolveCapabilities(index.schema.capabilities, resource.capabilities)
1587
+ );
1588
+ const injected = capabilityFields.find((f) => f.name === field);
1589
+ if (injected) return { type: injected.type, capability: true };
1590
+ const declared = resource.fields.find((f) => f.name === field);
1591
+ return declared ? { type: declared.type, capability: false } : void 0;
1592
+ }
1593
+ var NON_NUMERIC_TEMPORAL_STORAGE = /* @__PURE__ */ new Set(["date", "iso"]);
1594
+ function validateTemporal(q, fieldNames, fieldType = () => void 0) {
1579
1595
  const clauses = Array.isArray(q.temporal) ? q.temporal : [q.temporal];
1580
1596
  const validScales = /* @__PURE__ */ new Set(["hour", "day", "week", "month", "quarter", "year"]);
1581
1597
  const validStorage = /* @__PURE__ */ new Set(["unix-ms", "unix-s", "date", "iso"]);
@@ -1595,6 +1611,16 @@ function validateTemporal(q, fieldNames) {
1595
1611
  if (temporal.storage !== void 0 && !validStorage.has(String(temporal.storage))) {
1596
1612
  return vErr("DFQL_INVALID", "Invalid temporal storage", `${path}.storage`);
1597
1613
  }
1614
+ const target = fieldType(temporal.field);
1615
+ const storage = temporal.storage === void 0 ? void 0 : String(temporal.storage);
1616
+ const mismatched = storage !== void 0 && (target?.type === "number" && NON_NUMERIC_TEMPORAL_STORAGE.has(storage) || target?.type === "date" && target.capability && storage !== "unix-ms");
1617
+ if (mismatched) {
1618
+ return vErr(
1619
+ "DFQL_INVALID",
1620
+ target.type === "date" ? `Invalid DFQL: temporal storage "${storage}" does not match ${temporal.field}, which DataFn stores as epoch milliseconds; use "unix-ms" (the default)` : `Invalid DFQL: temporal storage "${storage}" does not match number field ${temporal.field}`,
1621
+ `${path}.storage`
1622
+ );
1623
+ }
1598
1624
  if (temporal.period !== void 0) {
1599
1625
  if (typeof temporal.period !== "object" || temporal.period === null || Array.isArray(temporal.period)) {
1600
1626
  return vErr("DFQL_INVALID", "Invalid DFQL: temporal period must be object", `${path}.period`);
@@ -2746,6 +2772,11 @@ function getShareableCapability(resource, schema) {
2746
2772
  const resolved = resolveCapabilities(schema.capabilities, resource.capabilities);
2747
2773
  return resolved.find(isShareableCapability2);
2748
2774
  }
2775
+ function isNamespaceOwnerActor(actorId, namespace) {
2776
+ if (!actorId) return false;
2777
+ const namespacePrincipal = normalizeNonEmptyString3(namespace);
2778
+ return !!namespacePrincipal && canonicalizeActorPrincipal(actorId) === namespacePrincipal;
2779
+ }
2749
2780
  function isPrivateShareableResource(schema, resourceName) {
2750
2781
  const resource = schema.resources.find((r) => r.name === resourceName);
2751
2782
  if (!resource) return false;
@@ -2924,8 +2955,7 @@ async function resolveAccessLevel(db, schema, resource, recordId, actorId, names
2924
2955
  }
2925
2956
  const recordObject = record;
2926
2957
  const actorPrincipal = canonicalizeActorPrincipal(actorId);
2927
- const namespacePrincipal = normalizeNonEmptyString3(namespace);
2928
- if (namespacePrincipal && actorPrincipal === namespacePrincipal) {
2958
+ if (isNamespaceOwnerActor(actorId, namespace)) {
2929
2959
  return "owner";
2930
2960
  }
2931
2961
  const createdByValue = normalizeNonEmptyString3(recordObject.createdBy);
@@ -3936,6 +3966,25 @@ function resolveValue(record, path, schema, store, resourceName, precomputedReso
3936
3966
  const lastField = parts[parts.length - 1];
3937
3967
  return currentRecord[lastField];
3938
3968
  }
3969
+ var MAX_PUSHDOWN_IN_VALUES = 3e4;
3970
+ function isOversizedInList(adapterOp, value) {
3971
+ return adapterOp === "in" && Array.isArray(value) && value.length > MAX_PUSHDOWN_IN_VALUES;
3972
+ }
3973
+ function countInValues(filters) {
3974
+ if (Array.isArray(filters)) {
3975
+ return filters.reduce((sum, item) => sum + countInValues(item), 0);
3976
+ }
3977
+ if (typeof filters !== "object" || filters === null) return 0;
3978
+ let total = 0;
3979
+ for (const [key, value] of Object.entries(filters)) {
3980
+ if ((key === "$in" || key === "in") && Array.isArray(value)) {
3981
+ total += value.length;
3982
+ } else if (typeof value === "object" && value !== null) {
3983
+ total += countInValues(value);
3984
+ }
3985
+ }
3986
+ return total;
3987
+ }
3939
3988
  var DFQL_TO_ADAPTER_OP = {
3940
3989
  // $-prefixed DFQL operators
3941
3990
  $eq: "eq",
@@ -4005,6 +4054,9 @@ function convertDfqlFilters(filters, resource, schema) {
4005
4054
  if (adapterOp === null) {
4006
4055
  return null;
4007
4056
  }
4057
+ if (isOversizedInList(adapterOp, opVal)) {
4058
+ return null;
4059
+ }
4008
4060
  if (op === "$is_null" || op === "is_null") {
4009
4061
  result.push({
4010
4062
  field: key,
@@ -4090,8 +4142,26 @@ function extractPushableFilters(filters, resource, schema) {
4090
4142
  hasRemaining = true;
4091
4143
  }
4092
4144
  }
4145
+ let inBudget = MAX_PUSHDOWN_IN_VALUES;
4146
+ const withinBudget = [];
4147
+ const overBudget = [];
4148
+ for (const clause of pushable) {
4149
+ if (clause.operator === "in" && Array.isArray(clause.value)) {
4150
+ if (clause.value.length > inBudget) {
4151
+ overBudget.push({ [clause.field]: { $in: clause.value } });
4152
+ continue;
4153
+ }
4154
+ inBudget -= clause.value.length;
4155
+ }
4156
+ withinBudget.push(clause);
4157
+ }
4158
+ if (overBudget.length > 0) {
4159
+ const existing = Array.isArray(remaining.$and) ? remaining.$and : [];
4160
+ remaining.$and = [...existing, ...overBudget];
4161
+ hasRemaining = true;
4162
+ }
4093
4163
  return {
4094
- pushable,
4164
+ pushable: withinBudget,
4095
4165
  remaining: hasRemaining ? remaining : null
4096
4166
  };
4097
4167
  }
@@ -4106,6 +4176,16 @@ function classifyQuery(query, schema) {
4106
4176
  if (hasInMemoryFilterPatterns(query.filters)) {
4107
4177
  return "IN_MEMORY";
4108
4178
  }
4179
+ if (countInValues(query.filters) > MAX_PUSHDOWN_IN_VALUES) {
4180
+ return "IN_MEMORY";
4181
+ }
4182
+ if (convertDfqlFilters(
4183
+ query.filters,
4184
+ typeof query.resource === "string" ? query.resource : void 0,
4185
+ schema
4186
+ ) === null) {
4187
+ return "IN_MEMORY";
4188
+ }
4109
4189
  }
4110
4190
  if (Array.isArray(query.select)) {
4111
4191
  const relationNames = buildRelationNames(
@@ -4135,6 +4215,9 @@ function hasInMemoryFilterPatterns(filters) {
4135
4215
  if (!(op in DFQL_TO_ADAPTER_OP) || DFQL_TO_ADAPTER_OP[op] === null) {
4136
4216
  return true;
4137
4217
  }
4218
+ if (isOversizedInList(DFQL_TO_ADAPTER_OP[op], ops[op])) {
4219
+ return true;
4220
+ }
4138
4221
  }
4139
4222
  }
4140
4223
  if ((key === "$and" || key === "$or") && Array.isArray(value)) {
@@ -4168,6 +4251,7 @@ function convertSingleFilter(key, value) {
4168
4251
  if (!(op in DFQL_TO_ADAPTER_OP)) return null;
4169
4252
  const adapterOp = DFQL_TO_ADAPTER_OP[op];
4170
4253
  if (adapterOp === null) return null;
4254
+ if (isOversizedInList(adapterOp, opVal)) return null;
4171
4255
  if (op === "$is_null" || op === "is_null") {
4172
4256
  result.push({
4173
4257
  field: key,
@@ -4195,6 +4279,7 @@ function isRelationKey(key, resource, schema) {
4195
4279
  (r) => endpointIncludes(r.from, resource) && r.relation === key || endpointIncludes(r.to, resource) && r.inverse === key
4196
4280
  );
4197
4281
  }
4282
+ var FIND_BY_IDS_CHUNK = 3e4;
4198
4283
  var DbDataStore = class _DbDataStore {
4199
4284
  constructor() {
4200
4285
  this.recordsCache = /* @__PURE__ */ new Map();
@@ -4219,7 +4304,26 @@ var DbDataStore = class _DbDataStore {
4219
4304
  )
4220
4305
  );
4221
4306
  }
4307
+ /**
4308
+ * `field IN ids` in chunks, so a lookup over every record of a large table
4309
+ * stays under a driver's bound-parameter limit (Postgres: 65,535).
4310
+ */
4311
+ static async findManyByIds(db, model, field, ids, namespace) {
4312
+ const rows = [];
4313
+ for (let i = 0; i < ids.length; i += FIND_BY_IDS_CHUNK) {
4314
+ const chunk = await db.findMany({
4315
+ model,
4316
+ where: [{ field, operator: "in", value: ids.slice(i, i + FIND_BY_IDS_CHUNK) }],
4317
+ namespace
4318
+ });
4319
+ for (const row of chunk) rows.push(row);
4320
+ }
4321
+ return rows;
4322
+ }
4222
4323
  static async applyActorVisibilityFilter(store, db, schema, namespace, actorId, logger) {
4324
+ if (isNamespaceOwnerActor(actorId, namespace)) {
4325
+ return;
4326
+ }
4223
4327
  for (const [resourceName, rows] of store.recordsCache.entries()) {
4224
4328
  if (!isPrivateShareableResource(schema, resourceName)) {
4225
4329
  continue;
@@ -4318,11 +4422,7 @@ var DbDataStore = class _DbDataStore {
4318
4422
  idsByResource.set(resource, current);
4319
4423
  }
4320
4424
  for (const [resource, resourceIds] of idsByResource.entries()) {
4321
- const rows = resourceIds.length > 0 ? await db.findMany({
4322
- model: resource,
4323
- where: [{ field: "id", operator: "in", value: resourceIds }],
4324
- namespace
4325
- }) : [];
4425
+ const rows = resourceIds.length > 0 ? await _DbDataStore.findManyByIds(db, resource, "id", resourceIds, namespace) : [];
4326
4426
  store.mergeRecords(resource, rows);
4327
4427
  }
4328
4428
  for (const resource of endpointList(endpoint)) {
@@ -4361,11 +4461,7 @@ var DbDataStore = class _DbDataStore {
4361
4461
  const fkField = rel.fkField || rel.foreignKey || `${rel.inverse}Id`;
4362
4462
  try {
4363
4463
  for (const fromResource of endpointList(rel.from)) {
4364
- const rows = primaryIds.length > 0 ? await db.findMany({
4365
- model: fromResource,
4366
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4367
- namespace
4368
- }) : [];
4464
+ const rows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, fromResource, fkField, primaryIds, namespace) : [];
4369
4465
  store.recordsCache.set(fromResource, rows);
4370
4466
  }
4371
4467
  } catch (error) {
@@ -4376,11 +4472,7 @@ var DbDataStore = class _DbDataStore {
4376
4472
  const fkField = rel.fkField || rel.foreignKey || `${rel.inverse}Id`;
4377
4473
  try {
4378
4474
  for (const toResource of endpointList(rel.to)) {
4379
- const rows = primaryIds.length > 0 ? await db.findMany({
4380
- model: toResource,
4381
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4382
- namespace
4383
- }) : [];
4475
+ const rows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, toResource, fkField, primaryIds, namespace) : [];
4384
4476
  store.recordsCache.set(toResource, rows);
4385
4477
  }
4386
4478
  } catch (error) {
@@ -4408,11 +4500,7 @@ var DbDataStore = class _DbDataStore {
4408
4500
  let joinRows = [];
4409
4501
  try {
4410
4502
  const filterField = isForward ? fromCol : toCol;
4411
- joinRows = primaryIds.length > 0 ? await db.findMany({
4412
- model: tableName,
4413
- where: [{ field: filterField, operator: "in", value: primaryIds }],
4414
- namespace
4415
- }) : [];
4503
+ joinRows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, tableName, filterField, primaryIds, namespace) : [];
4416
4504
  } catch (error) {
4417
4505
  logger?.warn("forPushdownResult: many-many join load failed", { error: String(error), operation: "db-store-pushdown" });
4418
4506
  joinRows = [];
@@ -4475,11 +4563,7 @@ var DbDataStore = class _DbDataStore {
4475
4563
  const relationKey2 = relationKeyFor(fromResource, rel2);
4476
4564
  let joinRows2 = [];
4477
4565
  try {
4478
- joinRows2 = intermediateIds.length > 0 ? await db.findMany({
4479
- model: tableName2,
4480
- where: [{ field: isF2 ? fromCol2 : toCol2, operator: "in", value: intermediateIds }],
4481
- namespace
4482
- }) : [];
4566
+ joinRows2 = intermediateIds.length > 0 ? await _DbDataStore.findManyByIds(db, tableName2, isF2 ? fromCol2 : toCol2, intermediateIds, namespace) : [];
4483
4567
  } catch (error) {
4484
4568
  logger?.warn("forPushdownResult: nested many-many join load failed", { error: String(error), operation: "db-store-pushdown" });
4485
4569
  joinRows2 = [];
@@ -4512,11 +4596,7 @@ var DbDataStore = class _DbDataStore {
4512
4596
  const fkField2 = rel2.fkField || rel2.foreignKey || `${rel2.inverse}Id`;
4513
4597
  try {
4514
4598
  for (const targetResource of endpointList(rel2.to)) {
4515
- const rows2 = intermediateIds.length > 0 ? await db.findMany({
4516
- model: targetResource,
4517
- where: [{ field: fkField2, operator: "in", value: intermediateIds }],
4518
- namespace
4519
- }) : [];
4599
+ const rows2 = intermediateIds.length > 0 ? await _DbDataStore.findManyByIds(db, targetResource, fkField2, intermediateIds, namespace) : [];
4520
4600
  store.mergeRecords(targetResource, rows2);
4521
4601
  }
4522
4602
  } catch (error) {
@@ -4770,14 +4850,13 @@ var DbDataStore = class _DbDataStore {
4770
4850
  const filterField = primaryIsFrom ? fromCol : toCol;
4771
4851
  const secondaryEndpoint = primaryIsFrom ? rel.to : rel.from;
4772
4852
  try {
4773
- let joinRows = [];
4774
- if (primaryIds.length > 0) {
4775
- joinRows = await db.findMany({
4776
- model: tableName,
4777
- where: [{ field: filterField, operator: "in", value: primaryIds }],
4778
- namespace
4779
- });
4780
- }
4853
+ const joinRows = await _DbDataStore.findManyByIds(
4854
+ db,
4855
+ tableName,
4856
+ filterField,
4857
+ primaryIds,
4858
+ namespace
4859
+ );
4781
4860
  const normalizedRows = _DbDataStore.normalizeJoinRows(joinRows, fromCol, toCol);
4782
4861
  const rowsForRelation = _DbDataStore.filterJoinRowsForResource(
4783
4862
  schema,
@@ -4801,13 +4880,13 @@ var DbDataStore = class _DbDataStore {
4801
4880
  idsByResource.set(secondaryResource, ids);
4802
4881
  }
4803
4882
  for (const [secondaryModel, ids] of idsByResource.entries()) {
4804
- const records = ids.length > 0 ? await db.findMany({
4805
- model: secondaryModel,
4806
- where: [
4807
- { field: "id", operator: "in", value: ids }
4808
- ],
4883
+ const records = await _DbDataStore.findManyByIds(
4884
+ db,
4885
+ secondaryModel,
4886
+ "id",
4887
+ ids,
4809
4888
  namespace
4810
- }) : [];
4889
+ );
4811
4890
  store.mergeRecords(secondaryModel, records);
4812
4891
  manyManySecondaries.add(secondaryModel);
4813
4892
  }
@@ -4852,29 +4931,17 @@ var DbDataStore = class _DbDataStore {
4852
4931
  )
4853
4932
  ];
4854
4933
  if (fkValues.length > 0) {
4855
- records = await db.findMany({
4856
- model: secondaryResource,
4857
- where: [{ field: "id", operator: "in", value: fkValues }],
4858
- namespace
4859
- });
4934
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, "id", fkValues, namespace);
4860
4935
  }
4861
4936
  } else if (rel.type === "many-one" && !isForward) {
4862
4937
  const fkField = rel.fkField || rel.foreignKey || `${rel.relation}Id`;
4863
4938
  if (primaryIds.length > 0) {
4864
- records = await db.findMany({
4865
- model: secondaryResource,
4866
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4867
- namespace
4868
- });
4939
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, fkField, primaryIds, namespace);
4869
4940
  }
4870
4941
  } else if (rel.type === "one-many" && isForward) {
4871
4942
  const fkField = rel.fkField || rel.foreignKey || rel.inverse || `${query.resource}Id`;
4872
4943
  if (primaryIds.length > 0) {
4873
- records = await db.findMany({
4874
- model: secondaryResource,
4875
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4876
- namespace
4877
- });
4944
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, fkField, primaryIds, namespace);
4878
4945
  }
4879
4946
  } else if (rel.type === "one-many" && !isForward) {
4880
4947
  const fkField = rel.fkField || rel.foreignKey || rel.relation || `${secondaryResource}Id`;
@@ -4884,11 +4951,7 @@ var DbDataStore = class _DbDataStore {
4884
4951
  )
4885
4952
  ];
4886
4953
  if (fkValues.length > 0) {
4887
- records = await db.findMany({
4888
- model: secondaryResource,
4889
- where: [{ field: "id", operator: "in", value: fkValues }],
4890
- namespace
4891
- });
4954
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, "id", fkValues, namespace);
4892
4955
  }
4893
4956
  } else {
4894
4957
  logger?.warn("Unhandled relation type for secondary resource; returning empty set instead of full scan", {
@@ -5084,11 +5147,22 @@ async function pushdownCore(query, schema, db, namespace) {
5084
5147
  if (metadata?.accessMode === "sharedWithMe" && (metadata.enforceAclIndexCatalog === true || Array.isArray(metadata.aclIndexCatalog))) {
5085
5148
  assertRequiredAclIndexes(metadata.aclIndexCatalog ?? []);
5086
5149
  }
5087
- const whereClauses = queryWithAutoFilters.filters ? convertDfqlFilters(
5088
- queryWithAutoFilters.filters,
5089
- queryWithAutoFilters.resource,
5090
- schema
5091
- ) ?? [] : [];
5150
+ let whereClauses = [];
5151
+ if (queryWithAutoFilters.filters) {
5152
+ const converted = convertDfqlFilters(
5153
+ queryWithAutoFilters.filters,
5154
+ queryWithAutoFilters.resource,
5155
+ schema
5156
+ );
5157
+ if (converted === null) {
5158
+ throw new DatafnExecutionError(
5159
+ "INTERNAL",
5160
+ "Query filters cannot be pushed down to the database",
5161
+ "filters"
5162
+ );
5163
+ }
5164
+ whereClauses = converted;
5165
+ }
5092
5166
  const orderBy = convertDfqlSort(queryWithAutoFilters.sort);
5093
5167
  const sortTerms = parseSortTerms(queryWithAutoFilters.sort);
5094
5168
  const hasCursor = queryWithAutoFilters.cursor?.after != null || queryWithAutoFilters.cursor?.before != null;
@@ -7486,6 +7560,9 @@ function createQueryHandler(validatedSchema, maxLimit, extractNamespace, extract
7486
7560
  if (!actorId) {
7487
7561
  return { shareableAccessIds: [] };
7488
7562
  }
7563
+ if (isNamespaceOwnerActor(actorId, namespace)) {
7564
+ return { shareableAccessIds: [], hasShareableScopeAccess: true };
7565
+ }
7489
7566
  const ids = /* @__PURE__ */ new Set();
7490
7567
  const readMode = getSpv2MigrationRuntimeConfig().readMode;
7491
7568
  const ownedRows = await db.findMany({
@@ -8029,10 +8106,15 @@ function createQueryHandler(validatedSchema, maxLimit, extractNamespace, extract
8029
8106
  namespace,
8030
8107
  actorId
8031
8108
  ).then((shareableAccess) => {
8109
+ const {
8110
+ shareableAccessIds: _clientAccessIds,
8111
+ hasShareableScopeAccess: _clientScopeAccess,
8112
+ ...clientMetadata
8113
+ } = metadata ?? {};
8032
8114
  const queryWithMetadata = {
8033
8115
  ...query,
8034
8116
  metadata: {
8035
- ...metadata ?? {},
8117
+ ...clientMetadata,
8036
8118
  ...shareableAccess ?? {}
8037
8119
  }
8038
8120
  };
@@ -13773,6 +13855,14 @@ function resourceIdMatches(schema, candidates, resourceName, id) {
13773
13855
  }
13774
13856
 
13775
13857
  // src/execution/sync/clone.ts
13858
+ var JoinVisibilityError = class extends Error {
13859
+ constructor(reason) {
13860
+ super("Clone: join endpoint access check failed");
13861
+ this.reason = reason;
13862
+ }
13863
+ };
13864
+ var JOIN_LOOKUP_CHUNK = 90;
13865
+ var JOIN_LOOKUP_BUDGET = 16;
13776
13866
  async function executeClone(request, schema, db, namespace, sequenceStore, maxCloneRecords = 1e4, logger) {
13777
13867
  if (!request.clientId || typeof request.clientId !== "string") {
13778
13868
  return {
@@ -13790,6 +13880,11 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13790
13880
  const cursors = {};
13791
13881
  const next = {};
13792
13882
  let joins;
13883
+ let cappedClone = false;
13884
+ const fullyLoadedTables = /* @__PURE__ */ new Set();
13885
+ const accessDecisions = /* @__PURE__ */ new Map();
13886
+ const accessKey = (resource, id) => `${resource}\0${id}`;
13887
+ const countFailedTables = /* @__PURE__ */ new Set();
13793
13888
  const tablesToClone = request.tables || schema.resources.map((r) => r.name);
13794
13889
  const actorId = request.actorId;
13795
13890
  const filterActorVisibleRows = async (resource, rows) => {
@@ -13799,6 +13894,9 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13799
13894
  if (!actorId) {
13800
13895
  return [];
13801
13896
  }
13897
+ if (isNamespaceOwnerActor(actorId, namespace)) {
13898
+ return rows;
13899
+ }
13802
13900
  const visible = [];
13803
13901
  for (const row of rows) {
13804
13902
  const recordId = typeof row.id === "string" ? row.id : null;
@@ -13816,6 +13914,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13816
13914
  if (level) {
13817
13915
  visible.push(row);
13818
13916
  }
13917
+ accessDecisions.set(accessKey(resource, recordId), !!level);
13819
13918
  }
13820
13919
  return visible;
13821
13920
  };
@@ -13912,6 +14011,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13912
14011
  } catch (error) {
13913
14012
  logger?.warn("Clone: table count failed", { error: String(error), resource: tableName, operation: "clone-count" });
13914
14013
  tableCounts[tableName] = 0;
14014
+ countFailedTables.add(tableName);
13915
14015
  }
13916
14016
  }
13917
14017
  let paginated = false;
@@ -13922,22 +14022,24 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13922
14022
  try {
13923
14023
  if (remaining <= 0) {
13924
14024
  data[tableName] = [];
13925
- next[tableName] = tableCounts[tableName] > 0 ? "" : null;
14025
+ next[tableName] = tableCounts[tableName] > 0 || countFailedTables.has(tableName) ? "" : null;
13926
14026
  } else {
14027
+ const readLimit = remaining;
13927
14028
  const records = await db.findMany({
13928
14029
  model: tableName,
13929
14030
  where: [],
13930
14031
  orderBy: [{ field: "id", direction: "asc" }],
13931
- limit: remaining,
14032
+ limit: readLimit,
13932
14033
  namespace
13933
14034
  });
13934
14035
  data[tableName] = await filterActorVisibleRows(tableName, records);
13935
14036
  remaining -= records.length;
13936
- if (records.length < tableCounts[tableName]) {
14037
+ if (records.length < tableCounts[tableName] || countFailedTables.has(tableName) && records.length >= readLimit) {
13937
14038
  const lastRecord = records[records.length - 1];
13938
14039
  next[tableName] = lastRecord?.id ?? null;
13939
14040
  } else {
13940
14041
  next[tableName] = null;
14042
+ fullyLoadedTables.add(tableName);
13941
14043
  }
13942
14044
  }
13943
14045
  const latestSeq = await changeTracking.getLatestServerSeq({ resource: tableName });
@@ -13959,6 +14061,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13959
14061
  namespace
13960
14062
  });
13961
14063
  data[tableName] = await filterActorVisibleRows(tableName, records);
14064
+ fullyLoadedTables.add(tableName);
13962
14065
  const latestSeq = await changeTracking.getLatestServerSeq({ resource: tableName });
13963
14066
  cursors[tableName] = String(latestSeq);
13964
14067
  } catch (error) {
@@ -13968,19 +14071,63 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13968
14071
  }
13969
14072
  }
13970
14073
  }
13971
- if (paginated) {
13972
- return {
13973
- ok: true,
13974
- data,
13975
- cursors,
13976
- joins,
13977
- next: Object.keys(next).length > 0 ? next : void 0,
13978
- paginated: true
13979
- };
13980
- }
14074
+ cappedClone = paginated;
13981
14075
  }
13982
14076
  if (request.includeJoins && schema.relations) {
13983
14077
  joins = {};
14078
+ const actorIsOwner = isNamespaceOwnerActor(actorId, namespace);
14079
+ const returnedIds = /* @__PURE__ */ new Map();
14080
+ for (const [table, rows] of Object.entries(data)) {
14081
+ returnedIds.set(
14082
+ table,
14083
+ new Set(rows.map((row) => row.id).filter((id) => typeof id === "string"))
14084
+ );
14085
+ }
14086
+ const endpointAccess = /* @__PURE__ */ new Map();
14087
+ const isEndpointVisible = async (resource, id) => {
14088
+ if (actorIsOwner || !isPrivateShareableResource(schema, resource)) return true;
14089
+ if (typeof id !== "string" || !actorId) return false;
14090
+ if (returnedIds.get(resource)?.has(id)) return true;
14091
+ const decided = accessDecisions.get(accessKey(resource, id));
14092
+ if (decided !== void 0) return decided;
14093
+ if (fullyLoadedTables.has(resource)) return false;
14094
+ const key = `${resource}\0${id}`;
14095
+ let visible = endpointAccess.get(key);
14096
+ if (visible === void 0) {
14097
+ try {
14098
+ visible = !!await resolveAccessLevel(db, schema, resource, id, actorId, namespace);
14099
+ } catch (error) {
14100
+ throw new JoinVisibilityError(error);
14101
+ }
14102
+ endpointAccess.set(key, visible);
14103
+ }
14104
+ return visible;
14105
+ };
14106
+ let scopedIds;
14107
+ if (request.page || cappedClone) {
14108
+ scopedIds = /* @__PURE__ */ new Map();
14109
+ for (const [table, rows] of Object.entries(data)) {
14110
+ const ids = new Set(
14111
+ rows.map((row) => row.id).filter((id) => typeof id === "string")
14112
+ );
14113
+ if (ids.size > 0) scopedIds.set(table, ids);
14114
+ }
14115
+ }
14116
+ let useIdLookups = false;
14117
+ if (scopedIds && request.page && tablesToClone.length === 1) {
14118
+ let lookups = 0;
14119
+ for (const relation of schema.relations) {
14120
+ if (relation.type !== "many-many") continue;
14121
+ const froms = Array.isArray(relation.from) ? relation.from : [relation.from];
14122
+ const tos = Array.isArray(relation.to) ? relation.to : [relation.to];
14123
+ for (const side of [froms, tos]) {
14124
+ let ids = 0;
14125
+ for (const resource of side) ids += scopedIds.get(resource)?.size ?? 0;
14126
+ lookups += Math.ceil(ids / JOIN_LOOKUP_CHUNK);
14127
+ }
14128
+ }
14129
+ useIdLookups = lookups <= JOIN_LOOKUP_BUDGET;
14130
+ }
13984
14131
  for (const relation of schema.relations) {
13985
14132
  if (relation.type !== "many-many") continue;
13986
14133
  const froms = Array.isArray(relation.from) ? relation.from : [relation.from];
@@ -13988,28 +14135,75 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
13988
14135
  const joinTableName = getRelationJoinTableName(relation, froms[0]);
13989
14136
  const fromCol = relation.joinColumns?.from || "from";
13990
14137
  const toCol = relation.joinColumns?.to || "to";
14138
+ const fromScopes = scopedIds ? froms.filter((resource) => scopedIds.has(resource)) : [];
14139
+ const toScopes = scopedIds ? tos.filter((resource) => scopedIds.has(resource)) : [];
14140
+ if (scopedIds && fromScopes.length === 0 && toScopes.length === 0) {
14141
+ continue;
14142
+ }
13991
14143
  try {
13992
- const joinRecords = await db.findMany({
13993
- model: joinTableName,
13994
- where: [],
13995
- orderBy: [{ field: "id", direction: "asc" }],
13996
- namespace
13997
- });
14144
+ let joinRecords;
14145
+ if (scopedIds && useIdLookups) {
14146
+ const byId = /* @__PURE__ */ new Map();
14147
+ const lookups = /* @__PURE__ */ new Map();
14148
+ const addLookup = (column, resources) => {
14149
+ const ids = lookups.get(column) ?? /* @__PURE__ */ new Set();
14150
+ for (const resource of resources) {
14151
+ for (const id of scopedIds.get(resource)) ids.add(id);
14152
+ }
14153
+ if (ids.size > 0) lookups.set(column, ids);
14154
+ };
14155
+ addLookup(fromCol, fromScopes);
14156
+ addLookup(toCol, toScopes);
14157
+ for (const [column, ids] of lookups) {
14158
+ const values = [...ids];
14159
+ for (let i = 0; i < values.length; i += JOIN_LOOKUP_CHUNK) {
14160
+ const rows = await db.findMany({
14161
+ model: joinTableName,
14162
+ where: [
14163
+ { field: column, operator: "in", value: values.slice(i, i + JOIN_LOOKUP_CHUNK) }
14164
+ ],
14165
+ namespace
14166
+ });
14167
+ for (const row of rows) byId.set(row.id, row);
14168
+ }
14169
+ }
14170
+ joinRecords = [...byId.values()].sort((a, b) => {
14171
+ const left = String(a.id ?? "");
14172
+ const right = String(b.id ?? "");
14173
+ return left < right ? -1 : left > right ? 1 : 0;
14174
+ });
14175
+ } else {
14176
+ joinRecords = await db.findMany({
14177
+ model: joinTableName,
14178
+ where: [],
14179
+ orderBy: [{ field: "id", direction: "asc" }],
14180
+ namespace
14181
+ });
14182
+ }
13998
14183
  const normalizedRows = joinRecords.map(
13999
14184
  (row) => normalizeJoinCloneRow(row, fromCol, toCol)
14000
14185
  );
14001
14186
  for (const from of froms) {
14002
14187
  for (const to of tos) {
14003
14188
  const joinStoreKey = getJoinStoreKey(from, relation.relation, to);
14004
- const rowsForStore = normalizedRows.filter(
14005
- (row) => shouldIncludeJoinRowForStore(row, relation, schema, from, to)
14189
+ const candidateRows = normalizedRows.filter(
14190
+ (row) => shouldIncludeJoinRowForStore(row, relation, schema, from, to) && (!scopedIds || typeof row.from === "string" && !!scopedIds.get(from)?.has(row.from) || typeof row.to === "string" && !!scopedIds.get(to)?.has(row.to))
14006
14191
  );
14192
+ const rowsForStore = [];
14193
+ for (const row of candidateRows) {
14194
+ if (await isEndpointVisible(from, row.from) && await isEndpointVisible(to, row.to)) {
14195
+ rowsForStore.push(row);
14196
+ }
14197
+ }
14007
14198
  if (rowsForStore.length > 0) {
14008
14199
  joins[joinStoreKey] = rowsForStore;
14009
14200
  }
14010
14201
  }
14011
14202
  }
14012
14203
  } catch (error) {
14204
+ if (error instanceof JoinVisibilityError) {
14205
+ throw error.reason;
14206
+ }
14013
14207
  logger?.warn("Clone: join table fetch failed", { error: String(error), operation: "clone-joins" });
14014
14208
  continue;
14015
14209
  }
@@ -14020,7 +14214,8 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14020
14214
  data,
14021
14215
  cursors,
14022
14216
  joins,
14023
- next: Object.keys(next).length > 0 ? next : void 0
14217
+ next: Object.keys(next).length > 0 ? next : void 0,
14218
+ ...cappedClone ? { paginated: true } : {}
14024
14219
  };
14025
14220
  }
14026
14221
  function normalizeJoinCloneRow(row, fromCol, toCol) {
@@ -16010,6 +16205,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16010
16205
  const counts = {};
16011
16206
  let joinCounts;
16012
16207
  const actorId = request.actorId;
16208
+ const checkedVisibleIds = /* @__PURE__ */ new Map();
16013
16209
  for (const resourceName of request.resources) {
16014
16210
  const resource = schema.resources.find((r) => r.name === resourceName);
16015
16211
  if (!resource) {
@@ -16033,7 +16229,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16033
16229
  };
16034
16230
  }
16035
16231
  try {
16036
- if (isPrivateShareableResource(schema, resourceName)) {
16232
+ if (isPrivateShareableResource(schema, resourceName) && !isNamespaceOwnerActor(actorId, namespace)) {
16037
16233
  if (!actorId) {
16038
16234
  counts[resourceName] = 0;
16039
16235
  continue;
@@ -16044,6 +16240,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16044
16240
  namespace
16045
16241
  });
16046
16242
  let visibleCount = 0;
16243
+ const visibleIds = /* @__PURE__ */ new Set();
16047
16244
  for (const row of rows) {
16048
16245
  const recordId = typeof row.id === "string" ? row.id : null;
16049
16246
  if (!recordId) {
@@ -16059,9 +16256,11 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16059
16256
  );
16060
16257
  if (level) {
16061
16258
  visibleCount++;
16259
+ visibleIds.add(recordId);
16062
16260
  }
16063
16261
  }
16064
16262
  counts[resourceName] = visibleCount;
16263
+ checkedVisibleIds.set(resourceName, visibleIds);
16065
16264
  continue;
16066
16265
  }
16067
16266
  if (typeof db.count === "function") {
@@ -16089,6 +16288,21 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16089
16288
  }
16090
16289
  if (request.includeJoins && schema.relations) {
16091
16290
  joinCounts = {};
16291
+ const actorIsOwner = isNamespaceOwnerActor(actorId, namespace);
16292
+ const endpointAccess = /* @__PURE__ */ new Map();
16293
+ const isJoinEndpointVisible = async (resource, id) => {
16294
+ if (!isPrivateShareableResource(schema, resource)) return true;
16295
+ if (typeof id !== "string" || !actorId) return false;
16296
+ const checked = checkedVisibleIds.get(resource);
16297
+ if (checked) return checked.has(id);
16298
+ const key = `${resource}\0${id}`;
16299
+ let visible = endpointAccess.get(key);
16300
+ if (visible === void 0) {
16301
+ visible = !!await resolveAccessLevel(db, schema, resource, id, actorId, namespace);
16302
+ endpointAccess.set(key, visible);
16303
+ }
16304
+ return visible;
16305
+ };
16092
16306
  for (const relation of schema.relations) {
16093
16307
  if (relation.type !== "many-many") continue;
16094
16308
  const fromResources = Array.isArray(relation.from) ? relation.from : [relation.from];
@@ -16146,9 +16360,29 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16146
16360
  joinCounts[joinStoreKey] = 0;
16147
16361
  continue;
16148
16362
  }
16149
- joinCounts[joinStoreKey] = normalizedRows.filter(
16363
+ const storeRows = normalizedRows.filter(
16150
16364
  (row) => shouldIncludeJoinRowForStore2(row, relation, schema, fromResource, toResource)
16151
- ).length;
16365
+ );
16366
+ if (!relationTouchesPrivateResource || actorIsOwner) {
16367
+ joinCounts[joinStoreKey] = storeRows.length;
16368
+ continue;
16369
+ }
16370
+ let visibleRows = 0;
16371
+ try {
16372
+ for (const row of storeRows) {
16373
+ if (await isJoinEndpointVisible(fromResource, row.from) && await isJoinEndpointVisible(toResource, row.to)) {
16374
+ visibleRows += 1;
16375
+ }
16376
+ }
16377
+ } catch (error) {
16378
+ logger?.warn("Reconcile: join visibility check failed", {
16379
+ error: String(error),
16380
+ joinTable: joinTableName,
16381
+ operation: "reconcile-join-count"
16382
+ });
16383
+ visibleRows = 0;
16384
+ }
16385
+ joinCounts[joinStoreKey] = visibleRows;
16152
16386
  }
16153
16387
  }
16154
16388
  }
@@ -18127,7 +18361,9 @@ function createRateLimitMiddleware(config) {
18127
18361
  error: {
18128
18362
  code: "RATE_LIMITED",
18129
18363
  message: "Too many requests",
18130
- details: { path: "$" }
18364
+ // Also in the body: browsers hide Retry-After from cross-origin
18365
+ // callers unless the server exposes it.
18366
+ details: { path: "$", retryAfterMs: windowSec * 1e3 }
18131
18367
  }
18132
18368
  }),
18133
18369
  {