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