@datafn/server 0.1.3 → 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
@@ -1,11 +1,10 @@
1
- import { DATAFN_ROUTE_MAX_TTL_MS, validateDatafnRegionalEndpoint, DATAFN_ROUTE_TICKET_HEADER, DATAFN_ROUTE_WS_TICKET_PREFIX, DATAFN_ROUTE_WS_PROTOCOL, validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, assertSupportedNamespaceStorageVersions, namespaceStorageManifestEntry, DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION, DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION, getAncestorInactiveResources, ANCESTOR_INACTIVE_FIELD, parseDatafnRequest, collectStructuralResourceSelectors, hasTemporalGrouping, resolveCapabilities, getCapabilityFields, getRelationJoinTableName, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1, runBeforeHook, getTemporalGroupAliases, getTemporalClauses, parseSortTerm, endpointIncludes, runAfterHook, checkPrototypePollution, validateFieldValue, toBoundsEpochMs, formatBoundEpochMs, TIMEZONE_CHANGE_RESOURCE_NAME, createTimezoneResolver, normalizeTemporalQuery, relationFkFieldForManyOne, relationFkFieldForOneMany, resolveEndpointResource, endpointList, evaluateFilter as evaluateFilter$1, findRelationMatch, relationTargetEndpoint, sortRecords, relationKeyFor, resourceNameFromId, findSystemFieldWrite, normalizeRelationFkRecord, parseSelectToken, getJoinStoreKey, OP_REMAP, parseSortTerms as parseSortTerms$1, stripNullsForNonNullableFields, getTemporalGroups, resolveTemporalBucketValue, calculateAggregation, normalizeRelationPayload, firstEndpoint, RELATION_CAPABILITY_FIELD_DEFS, coerceDateFieldsToEpoch, resourceRequiresAncestorInactive } from '@datafn/core';
2
- export { DATAFN_REQUEST_ACTIONS, DATAFN_REQUEST_PROTOCOL_VERSION, DATAFN_REQUEST_PROTOCOL_VERSIONS, DATAFN_STRUCTURAL_SELECTOR_POSITIONS, collectStructuralResourceSelectors, extractStructuralResourceSelectors, isDatafnRequestAction, parseDatafnRequest } from '@datafn/core';
1
+ import { validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, hasTemporalGrouping, resolveCapabilities, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1, getCapabilityFields, runBeforeHook, getTemporalGroupAliases, getTemporalClauses, parseSortTerm, endpointIncludes, runAfterHook, checkPrototypePollution, validateFieldValue, TIMEZONE_CHANGE_RESOURCE_NAME, createTimezoneResolver, normalizeTemporalQuery, evaluateFilter as evaluateFilter$1, findRelationMatch, endpointList, relationTargetEndpoint, sortRecords, relationKeyFor, getRelationJoinTableName, resolveEndpointResource, resourceNameFromId, normalizeRelationFkRecord, parseSelectToken, getJoinStoreKey, OP_REMAP, parseSortTerms as parseSortTerms$1, getTemporalGroups, resolveTemporalBucketValue, calculateAggregation, normalizeRelationPayload, relationFkFieldForManyOne, firstEndpoint, RELATION_CAPABILITY_FIELD_DEFS, coerceDateFieldsToEpoch } from '@datafn/core';
3
2
  import { createRouter, createObservabilityMiddleware, executeMiddlewareChain } from '@superfunctions/http';
4
3
  import { wrapWithRowLevelNamespace, instrumentAdapter, instrumentKVStore } from '@superfunctions/db';
5
4
  import { normalizeObservability } from '@superfunctions/observability';
6
- import { timingSafeEqual, randomUUID, createHash, createHmac } from 'crypto';
5
+ import { createHash, createHmac, timingSafeEqual, randomUUID } from 'crypto';
7
6
 
8
- // src/index.ts
7
+ // src/server.ts
9
8
 
10
9
  // src/execution/internal-tables.ts
11
10
  var META_COLUMNS = [
@@ -55,9 +54,6 @@ var INTERNAL_TABLE_SCHEMAS = {
55
54
  __datafn_seed: SEED_COLUMNS,
56
55
  __datafn_permission_directory_outbox: PERMISSION_DIRECTORY_OUTBOX_COLUMNS
57
56
  };
58
- function listNamespacedInternalTables() {
59
- return Object.entries(INTERNAL_TABLE_SCHEMAS).filter(([, columns]) => columns.some((column) => column.name === "namespace")).map(([name]) => name).sort();
60
- }
61
57
  async function ensureInternalTable(adapter, tableName) {
62
58
  const columns = INTERNAL_TABLE_SCHEMAS[tableName];
63
59
  if (!columns) {
@@ -474,8 +470,8 @@ function createSequenceStore(config) {
474
470
  // src/http/json.ts
475
471
  async function parseJsonBody(req) {
476
472
  try {
477
- const text2 = await req.text();
478
- if (!text2 || text2.trim() === "") {
473
+ const text = await req.text();
474
+ if (!text || text.trim() === "") {
479
475
  return {
480
476
  ok: false,
481
477
  error: {
@@ -485,7 +481,7 @@ async function parseJsonBody(req) {
485
481
  }
486
482
  };
487
483
  }
488
- const data = JSON.parse(text2);
484
+ const data = JSON.parse(text);
489
485
  return { ok: true, data };
490
486
  } catch (error) {
491
487
  return {
@@ -735,13 +731,6 @@ function validateRecordKeys(index, resourceName, record, basePath, mode = "write
735
731
  for (const key of Object.keys(record)) {
736
732
  if (key === "id") continue;
737
733
  if (!fields.has(key)) {
738
- if (mode === "write" && index.fieldsByResource.get(resourceName)?.has(key)) {
739
- return vErr(
740
- "DFQL_INVALID",
741
- `Field is read-only: ${key} on ${resourceName}`,
742
- `${basePath}.${key}`
743
- );
744
- }
745
734
  return vErr(
746
735
  "DFQL_UNKNOWN_FIELD",
747
736
  `Unknown field: ${key} on ${resourceName}`,
@@ -1030,32 +1019,13 @@ function validateMutation(mutation, index, basePath = "$", opts) {
1030
1019
  `${basePath}.record.${fieldName}`
1031
1020
  );
1032
1021
  }
1033
- if (fieldDef.type === "date" && (fieldDef.min !== void 0 || fieldDef.max !== void 0)) {
1034
- const epoch = toBoundsEpochMs(value);
1035
- if (Number.isFinite(epoch)) {
1036
- if (fieldDef.min !== void 0 && epoch < fieldDef.min) {
1037
- return vErr(
1038
- "DFQL_INVALID",
1039
- `Field '${fieldName}' must be on or after ${formatBoundEpochMs(fieldDef.min)}`,
1040
- `${basePath}.record.${fieldName}`
1041
- );
1042
- }
1043
- if (fieldDef.max !== void 0 && epoch > fieldDef.max) {
1044
- return vErr(
1045
- "DFQL_INVALID",
1046
- `Field '${fieldName}' must be on or before ${formatBoundEpochMs(fieldDef.max)}`,
1047
- `${basePath}.record.${fieldName}`
1048
- );
1049
- }
1050
- }
1051
- }
1052
1022
  }
1053
1023
  if (m.operation === "insert" || m.operation === "replace") {
1054
1024
  const resource2 = resourceResult.result;
1055
1025
  const record2 = m.record;
1056
1026
  for (const field of resource2.fields) {
1057
1027
  if (field.required && field.default === void 0 && field.name !== "id" && !field.readonly) {
1058
- if (!(field.name in record2) || record2[field.name] === void 0 || record2[field.name] === null && !field.nullable) {
1028
+ if (!(field.name in record2) || record2[field.name] === null || record2[field.name] === void 0) {
1059
1029
  return vErr(
1060
1030
  "DFQL_INVALID",
1061
1031
  `Required field missing: ${field.name}`,
@@ -1361,7 +1331,11 @@ function validateQuery(query, index, basePath = "$", limits) {
1361
1331
  }
1362
1332
  }
1363
1333
  if (q.temporal !== void 0) {
1364
- const temporalResult = validateTemporal(q, fieldNames);
1334
+ const temporalResult = validateTemporal(
1335
+ q,
1336
+ fieldNames,
1337
+ (field) => resolveFieldType(index, resourceName, field)
1338
+ );
1365
1339
  if (!temporalResult.ok) {
1366
1340
  return temporalResult;
1367
1341
  }
@@ -1605,7 +1579,19 @@ function validateGroupBy(q, fieldNames, relationNames) {
1605
1579
  }
1606
1580
  return vOk(void 0);
1607
1581
  }
1608
- 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) {
1609
1595
  const clauses = Array.isArray(q.temporal) ? q.temporal : [q.temporal];
1610
1596
  const validScales = /* @__PURE__ */ new Set(["hour", "day", "week", "month", "quarter", "year"]);
1611
1597
  const validStorage = /* @__PURE__ */ new Set(["unix-ms", "unix-s", "date", "iso"]);
@@ -1625,6 +1611,16 @@ function validateTemporal(q, fieldNames) {
1625
1611
  if (temporal.storage !== void 0 && !validStorage.has(String(temporal.storage))) {
1626
1612
  return vErr("DFQL_INVALID", "Invalid temporal storage", `${path}.storage`);
1627
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
+ }
1628
1624
  if (temporal.period !== void 0) {
1629
1625
  if (typeof temporal.period !== "object" || temporal.period === null || Array.isArray(temporal.period)) {
1630
1626
  return vErr("DFQL_INVALID", "Invalid DFQL: temporal period must be object", `${path}.period`);
@@ -2776,6 +2772,11 @@ function getShareableCapability(resource, schema) {
2776
2772
  const resolved = resolveCapabilities(schema.capabilities, resource.capabilities);
2777
2773
  return resolved.find(isShareableCapability2);
2778
2774
  }
2775
+ function isNamespaceOwnerActor(actorId, namespace) {
2776
+ if (!actorId) return false;
2777
+ const namespacePrincipal = normalizeNonEmptyString3(namespace);
2778
+ return !!namespacePrincipal && canonicalizeActorPrincipal(actorId) === namespacePrincipal;
2779
+ }
2779
2780
  function isPrivateShareableResource(schema, resourceName) {
2780
2781
  const resource = schema.resources.find((r) => r.name === resourceName);
2781
2782
  if (!resource) return false;
@@ -2954,8 +2955,7 @@ async function resolveAccessLevel(db, schema, resource, recordId, actorId, names
2954
2955
  }
2955
2956
  const recordObject = record;
2956
2957
  const actorPrincipal = canonicalizeActorPrincipal(actorId);
2957
- const namespacePrincipal = normalizeNonEmptyString3(namespace);
2958
- if (namespacePrincipal && actorPrincipal === namespacePrincipal) {
2958
+ if (isNamespaceOwnerActor(actorId, namespace)) {
2959
2959
  return "owner";
2960
2960
  }
2961
2961
  const createdByValue = normalizeNonEmptyString3(recordObject.createdBy);
@@ -3303,22 +3303,6 @@ function filterAncestorInactiveRecords(records, match, metadata) {
3303
3303
  }
3304
3304
  return records.filter((record) => record.isAncestorInactive !== true);
3305
3305
  }
3306
- function isPlainRecord(value) {
3307
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
3308
- return false;
3309
- }
3310
- const prototype = Object.getPrototypeOf(value);
3311
- return prototype === Object.prototype || prototype === null;
3312
- }
3313
- function applyOmitToArrayValue(value, omit) {
3314
- if (Array.isArray(value)) {
3315
- return value.map((item) => applyOmitToArrayValue(item, omit));
3316
- }
3317
- if (isPlainRecord(value)) {
3318
- return applyOmit(value, omit);
3319
- }
3320
- return value;
3321
- }
3322
3306
  function applyOmit(record, omit) {
3323
3307
  if (!omit || omit.length === 0) {
3324
3308
  return record;
@@ -3327,8 +3311,13 @@ function applyOmit(record, omit) {
3327
3311
  for (const [key, value] of Object.entries(record)) {
3328
3312
  if (key === "id" || !omit.includes(key)) {
3329
3313
  if (Array.isArray(value)) {
3330
- result[key] = value.map((item) => applyOmitToArrayValue(item, omit));
3331
- } else if (isPlainRecord(value) && key !== "$relation_metadata") {
3314
+ result[key] = value.map((item) => {
3315
+ if (typeof item === "object" && item !== null) {
3316
+ return applyOmit(item, omit);
3317
+ }
3318
+ return item;
3319
+ });
3320
+ } else if (typeof value === "object" && value !== null && key !== "$relation_metadata") {
3332
3321
  result[key] = applyOmit(value, omit);
3333
3322
  } else {
3334
3323
  result[key] = value;
@@ -3429,7 +3418,6 @@ function getFkFieldsToOmit(schema, targetResource) {
3429
3418
  function materializeSelect(record, resource, select, schema, store, omit, metadata) {
3430
3419
  const resourceSchema = schema.resources.find((r) => r.name === resource);
3431
3420
  if (!resourceSchema) return { id: record.id };
3432
- record = stripNullsForNonNullableFields(record, resourceSchema);
3433
3421
  const fkFieldsToOmit = getFkFieldsToOmit(schema, resource);
3434
3422
  const effectiveOmit = omit ? [...omit, ...Array.from(fkFieldsToOmit)] : Array.from(fkFieldsToOmit);
3435
3423
  if (!select || select.length === 0) {
@@ -3538,9 +3526,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
3538
3526
  ),
3539
3527
  htreeRelation,
3540
3528
  metadata
3541
- ).map(
3542
- (c) => applyOmit(stripNullsForNonNullableFields(c, resourceSchema), omit)
3543
- );
3529
+ ).map((c) => applyOmit(c, omit));
3544
3530
  } else if (parsed.directive === "**") {
3545
3531
  result[parsed.baseName] = filterAncestorInactiveRecords(
3546
3532
  htreeChildren(
@@ -3552,9 +3538,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
3552
3538
  ),
3553
3539
  htreeRelation,
3554
3540
  metadata
3555
- ).map(
3556
- (d) => applyOmit(stripNullsForNonNullableFields(d, resourceSchema), omit)
3557
- );
3541
+ ).map((d) => applyOmit(d, omit));
3558
3542
  }
3559
3543
  includedFields.add(parsed.baseName);
3560
3544
  } else if (parsed.baseName === "parent") {
@@ -3563,9 +3547,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
3563
3547
  resource,
3564
3548
  htreeRelation.relation,
3565
3549
  store
3566
- ).map(
3567
- (a) => applyOmit(stripNullsForNonNullableFields(a, resourceSchema), omit)
3568
- );
3550
+ ).map((a) => applyOmit(a, omit));
3569
3551
  includedFields.add(parsed.baseName);
3570
3552
  }
3571
3553
  continue;
@@ -3861,10 +3843,8 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
3861
3843
  }
3862
3844
  const aggregations = query.aggregations || {};
3863
3845
  let results = [];
3864
- const samples = /* @__PURE__ */ new WeakMap();
3865
3846
  for (const [, groupRecords] of groups.entries()) {
3866
3847
  const row = {};
3867
- samples.set(row, groupRecords[0]);
3868
3848
  groupBy.forEach((field) => {
3869
3849
  const sample = groupRecords[0];
3870
3850
  const val = resolveValue(
@@ -3875,9 +3855,7 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
3875
3855
  query.resource,
3876
3856
  resourceSchema
3877
3857
  );
3878
- if (val !== void 0) {
3879
- row[field] = val;
3880
- }
3858
+ row[field] = val;
3881
3859
  });
3882
3860
  for (const group of temporalGroups) {
3883
3861
  row[group.alias] = resolveTemporalBucketValue(groupRecords[0], group);
@@ -3917,24 +3895,7 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
3917
3895
  paginated.length = limit;
3918
3896
  }
3919
3897
  return {
3920
- groups: paginated.map((row) => {
3921
- const output = { ...row };
3922
- for (const field of groupBy) {
3923
- if (Object.prototype.hasOwnProperty.call(aggregations, field)) continue;
3924
- if (output[field] === null && resolveValue(
3925
- samples.get(row),
3926
- field,
3927
- schema,
3928
- store,
3929
- query.resource,
3930
- resourceSchema,
3931
- true
3932
- ) === void 0) {
3933
- delete output[field];
3934
- }
3935
- }
3936
- return output;
3937
- }),
3898
+ groups: paginated,
3938
3899
  nextCursor
3939
3900
  };
3940
3901
  }
@@ -3963,10 +3924,9 @@ function orderGroupedResults(groups, sortTerms) {
3963
3924
  return 0;
3964
3925
  });
3965
3926
  }
3966
- function resolveValue(record, path, schema, store, resourceName, precomputedResource, normalize = false) {
3927
+ function resolveValue(record, path, schema, store, resourceName, precomputedResource) {
3967
3928
  if (!path.includes(".")) {
3968
- const definition2 = precomputedResource?.fields.find((field) => field.name === path);
3969
- return normalize && record[path] === null && definition2 && definition2.nullable !== true ? void 0 : record[path];
3929
+ return record[path];
3970
3930
  }
3971
3931
  const parts = path.split(".");
3972
3932
  const resource = precomputedResource ?? schema.resources.find((r) => r.name === resourceName);
@@ -4004,8 +3964,26 @@ function resolveValue(record, path, schema, store, resourceName, precomputedReso
4004
3964
  }
4005
3965
  }
4006
3966
  const lastField = parts[parts.length - 1];
4007
- const definition = schema.resources.find((entry) => entry.name === currentResource)?.fields.find((field) => field.name === lastField);
4008
- return normalize && currentRecord[lastField] === null && definition && definition.nullable !== true ? void 0 : currentRecord[lastField];
3967
+ return currentRecord[lastField];
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;
4009
3987
  }
4010
3988
  var DFQL_TO_ADAPTER_OP = {
4011
3989
  // $-prefixed DFQL operators
@@ -4076,6 +4054,9 @@ function convertDfqlFilters(filters, resource, schema) {
4076
4054
  if (adapterOp === null) {
4077
4055
  return null;
4078
4056
  }
4057
+ if (isOversizedInList(adapterOp, opVal)) {
4058
+ return null;
4059
+ }
4079
4060
  if (op === "$is_null" || op === "is_null") {
4080
4061
  result.push({
4081
4062
  field: key,
@@ -4161,8 +4142,26 @@ function extractPushableFilters(filters, resource, schema) {
4161
4142
  hasRemaining = true;
4162
4143
  }
4163
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
+ }
4164
4163
  return {
4165
- pushable,
4164
+ pushable: withinBudget,
4166
4165
  remaining: hasRemaining ? remaining : null
4167
4166
  };
4168
4167
  }
@@ -4177,6 +4176,16 @@ function classifyQuery(query, schema) {
4177
4176
  if (hasInMemoryFilterPatterns(query.filters)) {
4178
4177
  return "IN_MEMORY";
4179
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
+ }
4180
4189
  }
4181
4190
  if (Array.isArray(query.select)) {
4182
4191
  const relationNames = buildRelationNames(
@@ -4206,6 +4215,9 @@ function hasInMemoryFilterPatterns(filters) {
4206
4215
  if (!(op in DFQL_TO_ADAPTER_OP) || DFQL_TO_ADAPTER_OP[op] === null) {
4207
4216
  return true;
4208
4217
  }
4218
+ if (isOversizedInList(DFQL_TO_ADAPTER_OP[op], ops[op])) {
4219
+ return true;
4220
+ }
4209
4221
  }
4210
4222
  }
4211
4223
  if ((key === "$and" || key === "$or") && Array.isArray(value)) {
@@ -4239,6 +4251,7 @@ function convertSingleFilter(key, value) {
4239
4251
  if (!(op in DFQL_TO_ADAPTER_OP)) return null;
4240
4252
  const adapterOp = DFQL_TO_ADAPTER_OP[op];
4241
4253
  if (adapterOp === null) return null;
4254
+ if (isOversizedInList(adapterOp, opVal)) return null;
4242
4255
  if (op === "$is_null" || op === "is_null") {
4243
4256
  result.push({
4244
4257
  field: key,
@@ -4266,6 +4279,7 @@ function isRelationKey(key, resource, schema) {
4266
4279
  (r) => endpointIncludes(r.from, resource) && r.relation === key || endpointIncludes(r.to, resource) && r.inverse === key
4267
4280
  );
4268
4281
  }
4282
+ var FIND_BY_IDS_CHUNK = 3e4;
4269
4283
  var DbDataStore = class _DbDataStore {
4270
4284
  constructor() {
4271
4285
  this.recordsCache = /* @__PURE__ */ new Map();
@@ -4290,7 +4304,26 @@ var DbDataStore = class _DbDataStore {
4290
4304
  )
4291
4305
  );
4292
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
+ }
4293
4323
  static async applyActorVisibilityFilter(store, db, schema, namespace, actorId, logger) {
4324
+ if (isNamespaceOwnerActor(actorId, namespace)) {
4325
+ return;
4326
+ }
4294
4327
  for (const [resourceName, rows] of store.recordsCache.entries()) {
4295
4328
  if (!isPrivateShareableResource(schema, resourceName)) {
4296
4329
  continue;
@@ -4389,11 +4422,7 @@ var DbDataStore = class _DbDataStore {
4389
4422
  idsByResource.set(resource, current);
4390
4423
  }
4391
4424
  for (const [resource, resourceIds] of idsByResource.entries()) {
4392
- const rows = resourceIds.length > 0 ? await db.findMany({
4393
- model: resource,
4394
- where: [{ field: "id", operator: "in", value: resourceIds }],
4395
- namespace
4396
- }) : [];
4425
+ const rows = resourceIds.length > 0 ? await _DbDataStore.findManyByIds(db, resource, "id", resourceIds, namespace) : [];
4397
4426
  store.mergeRecords(resource, rows);
4398
4427
  }
4399
4428
  for (const resource of endpointList(endpoint)) {
@@ -4432,11 +4461,7 @@ var DbDataStore = class _DbDataStore {
4432
4461
  const fkField = rel.fkField || rel.foreignKey || `${rel.inverse}Id`;
4433
4462
  try {
4434
4463
  for (const fromResource of endpointList(rel.from)) {
4435
- const rows = primaryIds.length > 0 ? await db.findMany({
4436
- model: fromResource,
4437
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4438
- namespace
4439
- }) : [];
4464
+ const rows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, fromResource, fkField, primaryIds, namespace) : [];
4440
4465
  store.recordsCache.set(fromResource, rows);
4441
4466
  }
4442
4467
  } catch (error) {
@@ -4447,11 +4472,7 @@ var DbDataStore = class _DbDataStore {
4447
4472
  const fkField = rel.fkField || rel.foreignKey || `${rel.inverse}Id`;
4448
4473
  try {
4449
4474
  for (const toResource of endpointList(rel.to)) {
4450
- const rows = primaryIds.length > 0 ? await db.findMany({
4451
- model: toResource,
4452
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4453
- namespace
4454
- }) : [];
4475
+ const rows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, toResource, fkField, primaryIds, namespace) : [];
4455
4476
  store.recordsCache.set(toResource, rows);
4456
4477
  }
4457
4478
  } catch (error) {
@@ -4479,11 +4500,7 @@ var DbDataStore = class _DbDataStore {
4479
4500
  let joinRows = [];
4480
4501
  try {
4481
4502
  const filterField = isForward ? fromCol : toCol;
4482
- joinRows = primaryIds.length > 0 ? await db.findMany({
4483
- model: tableName,
4484
- where: [{ field: filterField, operator: "in", value: primaryIds }],
4485
- namespace
4486
- }) : [];
4503
+ joinRows = primaryIds.length > 0 ? await _DbDataStore.findManyByIds(db, tableName, filterField, primaryIds, namespace) : [];
4487
4504
  } catch (error) {
4488
4505
  logger?.warn("forPushdownResult: many-many join load failed", { error: String(error), operation: "db-store-pushdown" });
4489
4506
  joinRows = [];
@@ -4546,11 +4563,7 @@ var DbDataStore = class _DbDataStore {
4546
4563
  const relationKey2 = relationKeyFor(fromResource, rel2);
4547
4564
  let joinRows2 = [];
4548
4565
  try {
4549
- joinRows2 = intermediateIds.length > 0 ? await db.findMany({
4550
- model: tableName2,
4551
- where: [{ field: isF2 ? fromCol2 : toCol2, operator: "in", value: intermediateIds }],
4552
- namespace
4553
- }) : [];
4566
+ joinRows2 = intermediateIds.length > 0 ? await _DbDataStore.findManyByIds(db, tableName2, isF2 ? fromCol2 : toCol2, intermediateIds, namespace) : [];
4554
4567
  } catch (error) {
4555
4568
  logger?.warn("forPushdownResult: nested many-many join load failed", { error: String(error), operation: "db-store-pushdown" });
4556
4569
  joinRows2 = [];
@@ -4583,11 +4596,7 @@ var DbDataStore = class _DbDataStore {
4583
4596
  const fkField2 = rel2.fkField || rel2.foreignKey || `${rel2.inverse}Id`;
4584
4597
  try {
4585
4598
  for (const targetResource of endpointList(rel2.to)) {
4586
- const rows2 = intermediateIds.length > 0 ? await db.findMany({
4587
- model: targetResource,
4588
- where: [{ field: fkField2, operator: "in", value: intermediateIds }],
4589
- namespace
4590
- }) : [];
4599
+ const rows2 = intermediateIds.length > 0 ? await _DbDataStore.findManyByIds(db, targetResource, fkField2, intermediateIds, namespace) : [];
4591
4600
  store.mergeRecords(targetResource, rows2);
4592
4601
  }
4593
4602
  } catch (error) {
@@ -4841,14 +4850,13 @@ var DbDataStore = class _DbDataStore {
4841
4850
  const filterField = primaryIsFrom ? fromCol : toCol;
4842
4851
  const secondaryEndpoint = primaryIsFrom ? rel.to : rel.from;
4843
4852
  try {
4844
- let joinRows = [];
4845
- if (primaryIds.length > 0) {
4846
- joinRows = await db.findMany({
4847
- model: tableName,
4848
- where: [{ field: filterField, operator: "in", value: primaryIds }],
4849
- namespace
4850
- });
4851
- }
4853
+ const joinRows = await _DbDataStore.findManyByIds(
4854
+ db,
4855
+ tableName,
4856
+ filterField,
4857
+ primaryIds,
4858
+ namespace
4859
+ );
4852
4860
  const normalizedRows = _DbDataStore.normalizeJoinRows(joinRows, fromCol, toCol);
4853
4861
  const rowsForRelation = _DbDataStore.filterJoinRowsForResource(
4854
4862
  schema,
@@ -4872,13 +4880,13 @@ var DbDataStore = class _DbDataStore {
4872
4880
  idsByResource.set(secondaryResource, ids);
4873
4881
  }
4874
4882
  for (const [secondaryModel, ids] of idsByResource.entries()) {
4875
- const records = ids.length > 0 ? await db.findMany({
4876
- model: secondaryModel,
4877
- where: [
4878
- { field: "id", operator: "in", value: ids }
4879
- ],
4883
+ const records = await _DbDataStore.findManyByIds(
4884
+ db,
4885
+ secondaryModel,
4886
+ "id",
4887
+ ids,
4880
4888
  namespace
4881
- }) : [];
4889
+ );
4882
4890
  store.mergeRecords(secondaryModel, records);
4883
4891
  manyManySecondaries.add(secondaryModel);
4884
4892
  }
@@ -4923,29 +4931,17 @@ var DbDataStore = class _DbDataStore {
4923
4931
  )
4924
4932
  ];
4925
4933
  if (fkValues.length > 0) {
4926
- records = await db.findMany({
4927
- model: secondaryResource,
4928
- where: [{ field: "id", operator: "in", value: fkValues }],
4929
- namespace
4930
- });
4934
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, "id", fkValues, namespace);
4931
4935
  }
4932
4936
  } else if (rel.type === "many-one" && !isForward) {
4933
4937
  const fkField = rel.fkField || rel.foreignKey || `${rel.relation}Id`;
4934
4938
  if (primaryIds.length > 0) {
4935
- records = await db.findMany({
4936
- model: secondaryResource,
4937
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4938
- namespace
4939
- });
4939
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, fkField, primaryIds, namespace);
4940
4940
  }
4941
4941
  } else if (rel.type === "one-many" && isForward) {
4942
4942
  const fkField = rel.fkField || rel.foreignKey || rel.inverse || `${query.resource}Id`;
4943
4943
  if (primaryIds.length > 0) {
4944
- records = await db.findMany({
4945
- model: secondaryResource,
4946
- where: [{ field: fkField, operator: "in", value: primaryIds }],
4947
- namespace
4948
- });
4944
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, fkField, primaryIds, namespace);
4949
4945
  }
4950
4946
  } else if (rel.type === "one-many" && !isForward) {
4951
4947
  const fkField = rel.fkField || rel.foreignKey || rel.relation || `${secondaryResource}Id`;
@@ -4955,11 +4951,7 @@ var DbDataStore = class _DbDataStore {
4955
4951
  )
4956
4952
  ];
4957
4953
  if (fkValues.length > 0) {
4958
- records = await db.findMany({
4959
- model: secondaryResource,
4960
- where: [{ field: "id", operator: "in", value: fkValues }],
4961
- namespace
4962
- });
4954
+ records = await _DbDataStore.findManyByIds(db, secondaryResource, "id", fkValues, namespace);
4963
4955
  }
4964
4956
  } else {
4965
4957
  logger?.warn("Unhandled relation type for secondary resource; returning empty set instead of full scan", {
@@ -5155,11 +5147,22 @@ async function pushdownCore(query, schema, db, namespace) {
5155
5147
  if (metadata?.accessMode === "sharedWithMe" && (metadata.enforceAclIndexCatalog === true || Array.isArray(metadata.aclIndexCatalog))) {
5156
5148
  assertRequiredAclIndexes(metadata.aclIndexCatalog ?? []);
5157
5149
  }
5158
- const whereClauses = queryWithAutoFilters.filters ? convertDfqlFilters(
5159
- queryWithAutoFilters.filters,
5160
- queryWithAutoFilters.resource,
5161
- schema
5162
- ) ?? [] : [];
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
+ }
5163
5166
  const orderBy = convertDfqlSort(queryWithAutoFilters.sort);
5164
5167
  const sortTerms = parseSortTerms(queryWithAutoFilters.sort);
5165
5168
  const hasCursor = queryWithAutoFilters.cursor?.after != null || queryWithAutoFilters.cursor?.before != null;
@@ -5235,7 +5238,6 @@ async function pushdownCore(query, schema, db, namespace) {
5235
5238
  function projectFieldsPushdown(record, resource, schema, select, omit) {
5236
5239
  const resourceSchema = schema.resources.find((r) => r.name === resource);
5237
5240
  if (!resourceSchema) return { id: record.id };
5238
- record = stripNullsForNonNullableFields(record, resourceSchema);
5239
5241
  const resolvedCapabilities = resolveCapabilities(
5240
5242
  schema.capabilities,
5241
5243
  resourceSchema.capabilities
@@ -6004,213 +6006,6 @@ var AuthResolverCache = class {
6004
6006
  function createAuthResolverCache() {
6005
6007
  return new AuthResolverCache();
6006
6008
  }
6007
- var scopes = /* @__PURE__ */ new Set([
6008
- "query",
6009
- "search",
6010
- "mutation",
6011
- "transact",
6012
- "seed",
6013
- "clone",
6014
- "pull",
6015
- "push",
6016
- "reconcile",
6017
- "custom",
6018
- "websocket"
6019
- ]);
6020
- var text = (value) => typeof value === "string" && value.trim().length > 0 && value.length <= 1024;
6021
- function routeTicketError(code) {
6022
- return new DatafnRoutingError({
6023
- code,
6024
- message: code,
6025
- status: code === "DATAFN_ROUTE_RATE_LIMITED" ? 429 : code === "DATAFN_ROUTE_FORBIDDEN" ? 403 : 401,
6026
- retryable: code === "DATAFN_ROUTE_TICKET_EXPIRED" || code === "DATAFN_ROUTE_RATE_LIMITED",
6027
- executionStarted: false
6028
- });
6029
- }
6030
- function assertClaims(value) {
6031
- if (!value || value.version !== 1 || ![value.issuer, value.audience, value.subject, value.namespace, value.regionId, value.ticketId].every(text) || !Number.isSafeInteger(value.epoch) || value.epoch < 1 || ![value.issuedAt, value.notBefore, value.expiresAt].every(Number.isSafeInteger) || value.notBefore < value.issuedAt || value.notBefore >= value.expiresAt || value.expiresAt - value.issuedAt > DATAFN_ROUTE_MAX_TTL_MS || !Array.isArray(value.scopes) || value.scopes.length === 0 || value.scopes.some((scope) => !scopes.has(scope)) || new Set(value.scopes).size !== value.scopes.length || value.sessionBinding !== void 0 && !text(value.sessionBinding)) {
6032
- throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6033
- }
6034
- }
6035
- function createDatafnHmacRouteTickets(input) {
6036
- const keys = new Map(Object.entries(input.keys).map(([id, key]) => {
6037
- if (!/^[A-Za-z0-9_-]{1,128}$/.test(id) || Buffer.byteLength(key) < 32) {
6038
- throw new Error("DATAFN_ROUTE_KEY_INVALID");
6039
- }
6040
- return [id, Buffer.from(key)];
6041
- }));
6042
- if (!keys.has(input.activeKeyId)) throw new Error("DATAFN_ROUTE_ACTIVE_KEY_MISSING");
6043
- const signature = (content, key) => createHmac("sha256", key).update(`datafn-regional-route-v1:${content}`).digest();
6044
- return {
6045
- sign(claims) {
6046
- assertClaims(claims);
6047
- const header = Buffer.from(JSON.stringify({ typ: "datafn-route-v1", alg: "HS256", kid: input.activeKeyId })).toString("base64url");
6048
- const payload = Buffer.from(JSON.stringify(claims)).toString("base64url");
6049
- const content = `${header}.${payload}`;
6050
- return `${content}.${signature(content, keys.get(input.activeKeyId)).toString("base64url")}`;
6051
- },
6052
- verify(ticket) {
6053
- try {
6054
- if (typeof ticket !== "string" || ticket.length > 16384 || !/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(ticket)) throw new Error();
6055
- const [header, payload, mac] = ticket.split(".");
6056
- const protectedHeader = JSON.parse(Buffer.from(header, "base64url").toString());
6057
- const key = keys.get(protectedHeader.kid);
6058
- if (!key || protectedHeader.typ !== "datafn-route-v1" || protectedHeader.alg !== "HS256") throw new Error();
6059
- const expected = signature(`${header}.${payload}`, key);
6060
- const actual = Buffer.from(mac, "base64url");
6061
- if (actual.length !== expected.length || !timingSafeEqual(actual, expected)) throw new Error();
6062
- const claims = JSON.parse(Buffer.from(payload, "base64url").toString());
6063
- assertClaims(claims);
6064
- return claims;
6065
- } catch {
6066
- throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6067
- }
6068
- }
6069
- };
6070
- }
6071
- function createDatafnRouteBootstrap(config) {
6072
- const ttl = config.ttlMs ?? 6e4;
6073
- if (!text(config.issuer) || !Number.isSafeInteger(ttl) || ttl < 1e3 || ttl > DATAFN_ROUTE_MAX_TTL_MS) {
6074
- throw new Error("DATAFN_ROUTE_BOOTSTRAP_CONFIG_INVALID");
6075
- }
6076
- const now = config.now ?? Date.now;
6077
- return async (request) => {
6078
- try {
6079
- if (request.method !== "POST") return new Response(null, { status: 405, headers: { allow: "POST", "cache-control": "no-store" } });
6080
- const identity = await config.authenticate(request);
6081
- if (!identity || !text(identity.subject) || !text(identity.namespace)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6082
- let placement;
6083
- try {
6084
- placement = await config.directory.get(identity.namespace);
6085
- } catch {
6086
- throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_UNAVAILABLE", message: "Placement unavailable", status: 503, retryable: true });
6087
- }
6088
- if (!placement || placement.namespace !== identity.namespace || placement.state === "tombstoned") {
6089
- throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_NOT_FOUND", message: "Placement not found", status: 404, retryable: false });
6090
- }
6091
- if (placement.state !== "active" || placement.migration) {
6092
- throw new DatafnRoutingError({ code: "DATAFN_NAMESPACE_MOVING", message: "Placement is fenced", status: 409, retryable: true });
6093
- }
6094
- const permitted = [...await config.authorize(identity)];
6095
- const endpoint = await config.resolveEndpoint(placement);
6096
- const issuedAt = now();
6097
- const expiresAt = Math.min(issuedAt + ttl, identity.expiresAt ?? Infinity);
6098
- if (!Number.isSafeInteger(expiresAt) || expiresAt <= issuedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6099
- const httpUrl = validateDatafnRegionalEndpoint(endpoint.httpUrl);
6100
- const wsUrl = endpoint.wsUrl ? validateDatafnRegionalEndpoint(endpoint.wsUrl, true) : void 0;
6101
- if (permitted.includes("websocket") && !wsUrl) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6102
- const ticket = await config.signer.sign({
6103
- version: 1,
6104
- issuer: config.issuer,
6105
- audience: endpoint.audience,
6106
- subject: identity.subject,
6107
- namespace: identity.namespace,
6108
- regionId: placement.regionId,
6109
- epoch: placement.epoch,
6110
- scopes: permitted,
6111
- issuedAt,
6112
- notBefore: issuedAt,
6113
- expiresAt,
6114
- ticketId: randomUUID(),
6115
- ...identity.sessionBinding ? { sessionBinding: identity.sessionBinding } : {}
6116
- });
6117
- const descriptor = {
6118
- version: 1,
6119
- httpUrl,
6120
- ...wsUrl ? { wsUrl } : {},
6121
- ticket,
6122
- expiresAt,
6123
- renewAfter: issuedAt + Math.floor((expiresAt - issuedAt) * 0.8)
6124
- };
6125
- emit(config, { type: "issued" });
6126
- return Response.json(descriptor, { headers: { "cache-control": "no-store", pragma: "no-cache" } });
6127
- } catch (error) {
6128
- emit(config, { type: "rejected", code: error instanceof DatafnRoutingError ? error.code : "DATAFN_ROUTE_FORBIDDEN" });
6129
- return (error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_FORBIDDEN")).toResponse();
6130
- }
6131
- };
6132
- }
6133
- function readDatafnRouteTicket(request, websocket = false) {
6134
- const header = request.headers.get(DATAFN_ROUTE_TICKET_HEADER);
6135
- const protocols = (request.headers.get("sec-websocket-protocol") ?? "").split(",").map((s) => s.trim()).filter(Boolean);
6136
- const tickets = protocols.filter((s) => s.startsWith(DATAFN_ROUTE_WS_TICKET_PREFIX));
6137
- if (tickets.length > 1 || header && tickets.length || !websocket && tickets.length || tickets.length && !protocols.includes(DATAFN_ROUTE_WS_PROTOCOL)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6138
- return header ?? tickets[0]?.slice(DATAFN_ROUTE_WS_TICKET_PREFIX.length);
6139
- }
6140
- async function validateDatafnRouteTicket(input) {
6141
- const { runtime } = input;
6142
- try {
6143
- const origin = input.request.headers.get("origin");
6144
- if (origin && !runtime.allowedOrigins?.includes(origin)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6145
- const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
6146
- if (!token) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6147
- const claims = await runtime.verifier.verify(token);
6148
- assertClaims(claims);
6149
- const now = (runtime.now ?? Date.now)();
6150
- const skew = runtime.clockSkewMs ?? 5e3;
6151
- if (!Number.isFinite(skew) || skew < 0 || skew > 3e4) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6152
- if (claims.expiresAt <= now) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
6153
- if (claims.issuedAt > now + skew || claims.notBefore > now + skew || claims.issuer !== runtime.issuer || claims.audience !== runtime.audience || claims.regionId !== input.regionId || claims.namespace !== input.namespace || !claims.scopes.includes(input.scope)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6154
- const identity = await runtime.authenticate(input.request);
6155
- if (!identity || identity.subject !== claims.subject || identity.namespace !== claims.namespace || identity.sessionBinding !== claims.sessionBinding || identity.expiresAt !== void 0 && (!Number.isFinite(identity.expiresAt) || identity.expiresAt <= (runtime.now ?? Date.now)())) {
6156
- throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6157
- }
6158
- if (identity.expiresAt !== void 0 && claims.expiresAt > identity.expiresAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
6159
- if (runtime.isActive && !await runtime.isActive(claims)) throw routeTicketError("DATAFN_ROUTE_TICKET_REVOKED");
6160
- if (runtime.allowRequest && !await runtime.allowRequest(claims)) throw routeTicketError("DATAFN_ROUTE_RATE_LIMITED");
6161
- const admittedAt = (runtime.now ?? Date.now)();
6162
- if (identity.expiresAt !== void 0 && identity.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
6163
- if (claims.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
6164
- emit(runtime, { type: "accepted" });
6165
- return claims;
6166
- } catch (error) {
6167
- const safe = error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6168
- emit(runtime, { type: "rejected", code: safe.code });
6169
- throw safe;
6170
- }
6171
- }
6172
- function withDatafnRegionalCors(handler, options) {
6173
- const origins = new Set(options.origins.map((origin) => {
6174
- if (new URL(origin).origin !== origin || origin === "null") throw new Error("DATAFN_ROUTE_ORIGIN_INVALID");
6175
- return origin;
6176
- }));
6177
- const allowedHeaders = /* @__PURE__ */ new Set([
6178
- "content-type",
6179
- "authorization",
6180
- DATAFN_ROUTE_TICKET_HEADER,
6181
- ...(options.headers ?? []).map((header) => header.toLowerCase())
6182
- ]);
6183
- const allowedMethods = ["GET", "POST", "PATCH", "DELETE"];
6184
- return async (request) => {
6185
- const origin = request.headers.get("origin");
6186
- if (origin && !origins.has(origin)) return routeTicketError("DATAFN_ROUTE_FORBIDDEN").toResponse();
6187
- if (request.method === "OPTIONS") {
6188
- const method = request.headers.get("access-control-request-method");
6189
- const headers2 = (request.headers.get("access-control-request-headers") ?? "").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
6190
- if (!origin || !allowedMethods.includes(method ?? "") || headers2.some((h) => !allowedHeaders.has(h))) return new Response(null, { status: 403 });
6191
- return new Response(null, { status: 204, headers: {
6192
- "access-control-allow-origin": origin,
6193
- "access-control-allow-methods": allowedMethods.join(", "),
6194
- "access-control-allow-headers": [...allowedHeaders].join(", "),
6195
- vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
6196
- "cache-control": "no-store"
6197
- } });
6198
- }
6199
- const response = await handler(request);
6200
- const headers = new Headers(response.headers);
6201
- headers.append("vary", "Origin");
6202
- headers.delete("access-control-allow-credentials");
6203
- headers.set("cache-control", "no-store");
6204
- if (origin) headers.set("access-control-allow-origin", origin);
6205
- return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
6206
- };
6207
- }
6208
- function emit(config, event) {
6209
- try {
6210
- config.onEvent?.(event);
6211
- } catch {
6212
- }
6213
- }
6214
6009
  var DATAFN_ROUTING_ASSERTION_HEADER = "x-datafn-routing-assertion";
6215
6010
  var DATAFN_ROUTING_INTERNAL_HEADERS = [
6216
6011
  DATAFN_ROUTING_ASSERTION_HEADER,
@@ -6362,28 +6157,6 @@ async function claimDatafnNamespacePlacement(input) {
6362
6157
  async function validateDatafnPlacement(input) {
6363
6158
  const startedAt = Date.now();
6364
6159
  const requestId2 = input.request?.headers.get("x-request-id") ?? void 0;
6365
- let ticket;
6366
- const hasAssertion = Boolean(input.request?.headers.get(input.runtime.assertionHeader ?? DATAFN_ROUTING_ASSERTION_HEADER)?.trim());
6367
- if (input.request) {
6368
- const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
6369
- if (token !== void 0 && (hasAssertion || !input.runtime.routeTickets)) {
6370
- throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6371
- }
6372
- if (input.runtime.routeTickets && (!hasAssertion || token !== void 0)) {
6373
- if (DATAFN_ROUTING_INTERNAL_HEADERS.some((name) => input.request.headers.has(name))) {
6374
- throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6375
- }
6376
- ticket = await validateDatafnRouteTicket({
6377
- request: input.request,
6378
- namespace: input.namespace,
6379
- regionId: input.regionId,
6380
- scope: input.scope ?? "custom",
6381
- runtime: input.runtime.routeTickets
6382
- });
6383
- }
6384
- } else if (input.runtime.routeTickets && !input.trustedInternal) {
6385
- throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
6386
- }
6387
6160
  let placement;
6388
6161
  try {
6389
6162
  placement = await input.runtime.directory.get(input.namespace);
@@ -6421,7 +6194,7 @@ async function validateDatafnPlacement(input) {
6421
6194
  retryable: false
6422
6195
  });
6423
6196
  }
6424
- const assertion = ticket ? void 0 : await validateRoutingAssertion({
6197
+ const assertion = await validateRoutingAssertion({
6425
6198
  namespace: input.namespace,
6426
6199
  regionId: input.regionId,
6427
6200
  placement,
@@ -6477,7 +6250,7 @@ async function validateDatafnPlacement(input) {
6477
6250
  internal: Boolean(assertion)
6478
6251
  });
6479
6252
  }
6480
- if (assertion && assertion.epoch !== placement.epoch || ticket && ticket.epoch !== placement.epoch) {
6253
+ if (assertion && assertion.epoch !== placement.epoch) {
6481
6254
  await emitRoutingEvent(input.runtime, {
6482
6255
  type: "mismatch",
6483
6256
  requestId: requestId2,
@@ -6490,17 +6263,14 @@ async function validateDatafnPlacement(input) {
6490
6263
  });
6491
6264
  throw new DatafnRoutingError({
6492
6265
  code: "DATAFN_REGION_MISMATCH",
6493
- message: "Routing grant uses a stale placement epoch",
6266
+ message: "Routing assertion uses a stale placement epoch",
6494
6267
  status: 409,
6495
6268
  retryable: true,
6496
6269
  placement,
6497
- internal: Boolean(assertion)
6270
+ internal: true
6498
6271
  });
6499
6272
  }
6500
- if (ticket && ticket.expiresAt <= (input.runtime.routeTickets?.now ?? Date.now)()) {
6501
- throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
6502
- }
6503
- return { placement, ...assertion ? { assertion } : {}, ...ticket ? { ticket } : {} };
6273
+ return { placement, ...assertion ? { assertion } : {} };
6504
6274
  }
6505
6275
  function createDatafnGatewayRouter(config) {
6506
6276
  const maxBodyBytes = validateDatafnRoutingBodyLimit(config.maxBodyBytes);
@@ -7790,6 +7560,9 @@ function createQueryHandler(validatedSchema, maxLimit, extractNamespace, extract
7790
7560
  if (!actorId) {
7791
7561
  return { shareableAccessIds: [] };
7792
7562
  }
7563
+ if (isNamespaceOwnerActor(actorId, namespace)) {
7564
+ return { shareableAccessIds: [], hasShareableScopeAccess: true };
7565
+ }
7793
7566
  const ids = /* @__PURE__ */ new Set();
7794
7567
  const readMode = getSpv2MigrationRuntimeConfig().readMode;
7795
7568
  const ownedRows = await db.findMany({
@@ -8333,10 +8106,15 @@ function createQueryHandler(validatedSchema, maxLimit, extractNamespace, extract
8333
8106
  namespace,
8334
8107
  actorId
8335
8108
  ).then((shareableAccess) => {
8109
+ const {
8110
+ shareableAccessIds: _clientAccessIds,
8111
+ hasShareableScopeAccess: _clientScopeAccess,
8112
+ ...clientMetadata
8113
+ } = metadata ?? {};
8336
8114
  const queryWithMetadata = {
8337
8115
  ...query,
8338
8116
  metadata: {
8339
- ...metadata ?? {},
8117
+ ...clientMetadata,
8340
8118
  ...shareableAccess ?? {}
8341
8119
  }
8342
8120
  };
@@ -8456,8 +8234,9 @@ async function recordChangesWithRetry(changeTracking, entries, maxRetries = 2, l
8456
8234
  }
8457
8235
  }
8458
8236
  }
8237
+ var preparedNamespaces = /* @__PURE__ */ new WeakMap();
8459
8238
  var ChangeTrackingService = class _ChangeTrackingService {
8460
- constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false) {
8239
+ constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false, namespaceMetaEnsured = false) {
8461
8240
  this.db = db;
8462
8241
  this.namespace = namespace;
8463
8242
  this.sequenceStore = sequenceStore;
@@ -8465,8 +8244,10 @@ var ChangeTrackingService = class _ChangeTrackingService {
8465
8244
  this.logger = logger;
8466
8245
  this.metaEnsured = false;
8467
8246
  this.changesEnsured = false;
8247
+ this.namespaceMetaEnsured = false;
8468
8248
  this.metaEnsured = metaEnsured;
8469
8249
  this.changesEnsured = changesEnsured;
8250
+ this.namespaceMetaEnsured = namespaceMetaEnsured;
8470
8251
  }
8471
8252
  /** Initialize every durable table before an enclosing transaction begins. */
8472
8253
  async ensureReady() {
@@ -8481,6 +8262,48 @@ var ChangeTrackingService = class _ChangeTrackingService {
8481
8262
  await ensureInternalTable(this.db, "__datafn_meta");
8482
8263
  this.metaEnsured = true;
8483
8264
  }
8265
+ await this.ensureNamespaceMeta();
8266
+ }
8267
+ /**
8268
+ * Creates this namespace's `__datafn_meta` row before an enclosing
8269
+ * transaction begins. Sequence allocation otherwise creates the row inside
8270
+ * the transaction on a namespace's first write, and when two first writes
8271
+ * race, the losing INSERT fails with a unique-key conflict. On PostgreSQL that
8272
+ * aborts the whole transaction, so the recovery read that follows fails with
8273
+ * "current transaction is aborted". Outside a transaction the conflict is
8274
+ * harmless: the row exists either way, and later allocations only read and
8275
+ * CAS-update it. Transaction-bound instances (from `withDb`) never prepare
8276
+ * the row; the base instance does so before the transaction opens.
8277
+ */
8278
+ async ensureNamespaceMeta() {
8279
+ if (this.namespaceMetaEnsured) {
8280
+ return;
8281
+ }
8282
+ let prepared = preparedNamespaces.get(this.db);
8283
+ if (!prepared) {
8284
+ prepared = /* @__PURE__ */ new Set();
8285
+ preparedNamespaces.set(this.db, prepared);
8286
+ }
8287
+ if (prepared.has(this.namespace)) {
8288
+ this.namespaceMetaEnsured = true;
8289
+ return;
8290
+ }
8291
+ const where = [{ field: "namespace", op: "eq", value: this.namespace }];
8292
+ if (!await this.db.internal.findOne("__datafn_meta", where)) {
8293
+ try {
8294
+ await this.db.internal.create("__datafn_meta", {
8295
+ id: `meta:${this.namespace}`,
8296
+ namespace: this.namespace,
8297
+ next_server_seq: 1
8298
+ });
8299
+ } catch (error) {
8300
+ if (!await this.db.internal.findOne("__datafn_meta", where)) {
8301
+ throw error;
8302
+ }
8303
+ }
8304
+ }
8305
+ prepared.add(this.namespace);
8306
+ this.namespaceMetaEnsured = true;
8484
8307
  }
8485
8308
  /**
8486
8309
  * REL-011: Create a new ChangeTrackingService with a different db adapter.
@@ -8496,7 +8319,8 @@ var ChangeTrackingService = class _ChangeTrackingService {
8496
8319
  emitChanges ? this.onChange : void 0,
8497
8320
  this.logger,
8498
8321
  this.metaEnsured,
8499
- this.changesEnsured
8322
+ this.changesEnsured,
8323
+ true
8500
8324
  );
8501
8325
  }
8502
8326
  /**
@@ -8931,16 +8755,12 @@ function buildReplaceRecord(schema, resourceName, existingRecord, newRecord, res
8931
8755
  result[key] = existingRecord[key];
8932
8756
  continue;
8933
8757
  }
8934
- if (field.readonly) {
8935
- result[key] = Object.prototype.hasOwnProperty.call(newRecord, key) ? newRecord[key] : existingRecord[key];
8936
- continue;
8937
- }
8938
8758
  if (Object.prototype.hasOwnProperty.call(newRecord, key)) {
8939
8759
  result[key] = newRecord[key];
8940
8760
  } else {
8941
8761
  result[key] = field.default !== void 0 ? field.default : null;
8942
8762
  }
8943
- if (field.required && (result[key] === void 0 || result[key] === null && !field.nullable)) {
8763
+ if (field.required && (result[key] === null || result[key] === void 0)) {
8944
8764
  return {
8945
8765
  ok: false,
8946
8766
  code: "DFQL_INVALID",
@@ -9027,7 +8847,8 @@ function isRecordEffectivelyInactive(record) {
9027
8847
  return isRecordInactive(record) || record?.isAncestorInactive === true;
9028
8848
  }
9029
8849
  function resourceSupportsAncestorInactive(schema, resource) {
9030
- return resourceRequiresAncestorInactive(schema.relations, resource);
8850
+ const resourceSchema = schema.resources.find((candidate) => candidate.name === resource);
8851
+ return resourceSchema?.fields.some((field) => field.name === "isAncestorInactive") === true;
9031
8852
  }
9032
8853
  function dependentEndpoint(relation) {
9033
8854
  return relation.type === "many-one" ? relation.from : relation.to;
@@ -10446,7 +10267,7 @@ function decideMergeMode(schema, resource, id, delta) {
10446
10267
  for (const field of resourceDef.fields) {
10447
10268
  if (!field.required) continue;
10448
10269
  if (field.name === "id" || field.name === "__ns") continue;
10449
- const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && (delta[field.name] !== null || field.nullable === true);
10270
+ const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && delta[field.name] !== null;
10450
10271
  const hasDefault = field.default !== void 0;
10451
10272
  if (!hasProvided && !hasDefault) return { mode: "not_found" };
10452
10273
  }
@@ -12185,64 +12006,6 @@ async function drainPermissionDirectoryOutbox(db, runtime, logger, limit = 100)
12185
12006
  }
12186
12007
  return { processed, pending: tasks.length - processed };
12187
12008
  }
12188
- async function drainNamespacePermissionDirectoryOutbox(db, namespace, runtime, logger, options = {}) {
12189
- await ensurePermissionDirectoryOutbox(db);
12190
- const limit = options.limit ?? 100;
12191
- const maxRounds = options.maxRounds ?? 32;
12192
- let processed = 0;
12193
- for (let round = 0; round < maxRounds; round += 1) {
12194
- const tasks = await db.internal.findMany(OUTBOX_TABLE, [
12195
- { field: "namespace", op: "eq", value: namespace },
12196
- { field: "region_id", op: "eq", value: runtime.regionId },
12197
- { field: "next_attempt_at", op: "lte", value: (/* @__PURE__ */ new Date()).toISOString() }
12198
- ], {
12199
- orderBy: "next_attempt_at",
12200
- limit
12201
- });
12202
- if (tasks.length === 0) {
12203
- break;
12204
- }
12205
- let roundProcessed = 0;
12206
- for (const task of tasks) {
12207
- const taskId = String(task.id);
12208
- const selectedNextAttemptAt = String(task.next_attempt_at);
12209
- const claimedNextAttemptAt = new Date(
12210
- Date.now() + DRAIN_CLAIM_LEASE_MS
12211
- ).toISOString();
12212
- const claimed = await db.internal.update(OUTBOX_TABLE, [
12213
- { field: "id", op: "eq", value: taskId },
12214
- { field: "namespace", op: "eq", value: namespace },
12215
- { field: "region_id", op: "eq", value: runtime.regionId },
12216
- { field: "next_attempt_at", op: "eq", value: selectedNextAttemptAt }
12217
- ], {
12218
- next_attempt_at: claimedNextAttemptAt
12219
- });
12220
- if (claimed === 0) continue;
12221
- if (await drainPermissionDirectorySync(
12222
- db,
12223
- taskId,
12224
- runtime,
12225
- logger,
12226
- { expectedNextAttemptAt: claimedNextAttemptAt }
12227
- )) {
12228
- processed += 1;
12229
- roundProcessed += 1;
12230
- }
12231
- }
12232
- if (roundProcessed === 0) {
12233
- const remaining2 = await db.internal.findMany(OUTBOX_TABLE, [
12234
- { field: "namespace", op: "eq", value: namespace },
12235
- { field: "region_id", op: "eq", value: runtime.regionId }
12236
- ], { limit: 1 });
12237
- return { processed, pending: remaining2.length };
12238
- }
12239
- }
12240
- const remaining = await db.internal.findMany(OUTBOX_TABLE, [
12241
- { field: "namespace", op: "eq", value: namespace },
12242
- { field: "region_id", op: "eq", value: runtime.regionId }
12243
- ], { limit: 1 });
12244
- return { processed, pending: remaining.length };
12245
- }
12246
12009
 
12247
12010
  // src/execution/mutation/execute.ts
12248
12011
  var FAILED_SHARE_COMPENSATION_RESULT = /* @__PURE__ */ Symbol("failedShareCompensation");
@@ -12344,23 +12107,6 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
12344
12107
  let additionalChanges = [];
12345
12108
  let skipChangeTracking = false;
12346
12109
  let failedShareCompensation = null;
12347
- if ((mutation.operation === "insert" || mutation.operation === "merge" || mutation.operation === "replace") && mutation.record) {
12348
- const systemField = findSystemFieldWrite(schema.relations, mutation.resource, mutation.record);
12349
- if (systemField) {
12350
- return {
12351
- ok: false,
12352
- mutationId: effectiveMutationId,
12353
- affectedIds: [],
12354
- errors: [{
12355
- code: "DFQL_INVALID",
12356
- message: `Field is read-only: ${systemField} on ${mutation.resource}`,
12357
- path: `record.${systemField}`,
12358
- retryable: false
12359
- }],
12360
- deduped: false
12361
- };
12362
- }
12363
- }
12364
12110
  try {
12365
12111
  switch (mutation.operation) {
12366
12112
  case "insert":
@@ -14109,6 +13855,14 @@ function resourceIdMatches(schema, candidates, resourceName, id) {
14109
13855
  }
14110
13856
 
14111
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;
14112
13866
  async function executeClone(request, schema, db, namespace, sequenceStore, maxCloneRecords = 1e4, logger) {
14113
13867
  if (!request.clientId || typeof request.clientId !== "string") {
14114
13868
  return {
@@ -14126,6 +13880,11 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14126
13880
  const cursors = {};
14127
13881
  const next = {};
14128
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();
14129
13888
  const tablesToClone = request.tables || schema.resources.map((r) => r.name);
14130
13889
  const actorId = request.actorId;
14131
13890
  const filterActorVisibleRows = async (resource, rows) => {
@@ -14135,6 +13894,9 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14135
13894
  if (!actorId) {
14136
13895
  return [];
14137
13896
  }
13897
+ if (isNamespaceOwnerActor(actorId, namespace)) {
13898
+ return rows;
13899
+ }
14138
13900
  const visible = [];
14139
13901
  for (const row of rows) {
14140
13902
  const recordId = typeof row.id === "string" ? row.id : null;
@@ -14152,6 +13914,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14152
13914
  if (level) {
14153
13915
  visible.push(row);
14154
13916
  }
13917
+ accessDecisions.set(accessKey(resource, recordId), !!level);
14155
13918
  }
14156
13919
  return visible;
14157
13920
  };
@@ -14248,6 +14011,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14248
14011
  } catch (error) {
14249
14012
  logger?.warn("Clone: table count failed", { error: String(error), resource: tableName, operation: "clone-count" });
14250
14013
  tableCounts[tableName] = 0;
14014
+ countFailedTables.add(tableName);
14251
14015
  }
14252
14016
  }
14253
14017
  let paginated = false;
@@ -14258,22 +14022,24 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14258
14022
  try {
14259
14023
  if (remaining <= 0) {
14260
14024
  data[tableName] = [];
14261
- next[tableName] = tableCounts[tableName] > 0 ? "" : null;
14025
+ next[tableName] = tableCounts[tableName] > 0 || countFailedTables.has(tableName) ? "" : null;
14262
14026
  } else {
14027
+ const readLimit = remaining;
14263
14028
  const records = await db.findMany({
14264
14029
  model: tableName,
14265
14030
  where: [],
14266
14031
  orderBy: [{ field: "id", direction: "asc" }],
14267
- limit: remaining,
14032
+ limit: readLimit,
14268
14033
  namespace
14269
14034
  });
14270
14035
  data[tableName] = await filterActorVisibleRows(tableName, records);
14271
14036
  remaining -= records.length;
14272
- if (records.length < tableCounts[tableName]) {
14037
+ if (records.length < tableCounts[tableName] || countFailedTables.has(tableName) && records.length >= readLimit) {
14273
14038
  const lastRecord = records[records.length - 1];
14274
14039
  next[tableName] = lastRecord?.id ?? null;
14275
14040
  } else {
14276
14041
  next[tableName] = null;
14042
+ fullyLoadedTables.add(tableName);
14277
14043
  }
14278
14044
  }
14279
14045
  const latestSeq = await changeTracking.getLatestServerSeq({ resource: tableName });
@@ -14295,6 +14061,7 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14295
14061
  namespace
14296
14062
  });
14297
14063
  data[tableName] = await filterActorVisibleRows(tableName, records);
14064
+ fullyLoadedTables.add(tableName);
14298
14065
  const latestSeq = await changeTracking.getLatestServerSeq({ resource: tableName });
14299
14066
  cursors[tableName] = String(latestSeq);
14300
14067
  } catch (error) {
@@ -14304,19 +14071,63 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14304
14071
  }
14305
14072
  }
14306
14073
  }
14307
- if (paginated) {
14308
- return {
14309
- ok: true,
14310
- data,
14311
- cursors,
14312
- joins,
14313
- next: Object.keys(next).length > 0 ? next : void 0,
14314
- paginated: true
14315
- };
14316
- }
14074
+ cappedClone = paginated;
14317
14075
  }
14318
14076
  if (request.includeJoins && schema.relations) {
14319
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
+ }
14320
14131
  for (const relation of schema.relations) {
14321
14132
  if (relation.type !== "many-many") continue;
14322
14133
  const froms = Array.isArray(relation.from) ? relation.from : [relation.from];
@@ -14324,28 +14135,75 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14324
14135
  const joinTableName = getRelationJoinTableName(relation, froms[0]);
14325
14136
  const fromCol = relation.joinColumns?.from || "from";
14326
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
+ }
14327
14143
  try {
14328
- const joinRecords = await db.findMany({
14329
- model: joinTableName,
14330
- where: [],
14331
- orderBy: [{ field: "id", direction: "asc" }],
14332
- namespace
14333
- });
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
+ }
14334
14183
  const normalizedRows = joinRecords.map(
14335
14184
  (row) => normalizeJoinCloneRow(row, fromCol, toCol)
14336
14185
  );
14337
14186
  for (const from of froms) {
14338
14187
  for (const to of tos) {
14339
14188
  const joinStoreKey = getJoinStoreKey(from, relation.relation, to);
14340
- const rowsForStore = normalizedRows.filter(
14341
- (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))
14342
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
+ }
14343
14198
  if (rowsForStore.length > 0) {
14344
14199
  joins[joinStoreKey] = rowsForStore;
14345
14200
  }
14346
14201
  }
14347
14202
  }
14348
14203
  } catch (error) {
14204
+ if (error instanceof JoinVisibilityError) {
14205
+ throw error.reason;
14206
+ }
14349
14207
  logger?.warn("Clone: join table fetch failed", { error: String(error), operation: "clone-joins" });
14350
14208
  continue;
14351
14209
  }
@@ -14356,7 +14214,8 @@ async function executeClone(request, schema, db, namespace, sequenceStore, maxCl
14356
14214
  data,
14357
14215
  cursors,
14358
14216
  joins,
14359
- next: Object.keys(next).length > 0 ? next : void 0
14217
+ next: Object.keys(next).length > 0 ? next : void 0,
14218
+ ...cappedClone ? { paginated: true } : {}
14360
14219
  };
14361
14220
  }
14362
14221
  function normalizeJoinCloneRow(row, fromCol, toCol) {
@@ -14820,7 +14679,7 @@ function compareCanonicalChangeEntries(a, b) {
14820
14679
  function mergeCanonicalChangeStreamsBounded(streams, limit) {
14821
14680
  const heads = streams.filter((stream) => stream.changes.length > 0).map((stream) => ({ resource: stream.resource, changes: stream.changes, index: 0 }));
14822
14681
  const merged = [];
14823
- const latestCursorPerResource = /* @__PURE__ */ Object.create(null);
14682
+ const latestCursorPerResource = {};
14824
14683
  let peakWindow = heads.length;
14825
14684
  while (heads.length > 0 && merged.length < limit) {
14826
14685
  let bestIdx = 0;
@@ -15052,11 +14911,11 @@ async function executePullCanonical(request, schema, db, namespace, sequenceStor
15052
14911
  const requestedResourceKeys = Object.keys(request.cursors).filter(
15053
14912
  (resource) => resource !== ACTOR_FEED_CURSOR_KEY
15054
14913
  );
15055
- const records = /* @__PURE__ */ Object.create(null);
15056
- const merged = /* @__PURE__ */ Object.create(null);
15057
- const deleted = /* @__PURE__ */ Object.create(null);
15058
- const joins = /* @__PURE__ */ Object.create(null);
15059
- const cursors = /* @__PURE__ */ Object.create(null);
14914
+ const records = {};
14915
+ const merged = {};
14916
+ const deleted = {};
14917
+ const joins = {};
14918
+ const cursors = {};
15060
14919
  for (const resource of requestedResourceKeys) {
15061
14920
  if (!resource.startsWith("join_") && !resource.startsWith("__datafn_")) {
15062
14921
  records[resource] = [];
@@ -15459,17 +15318,6 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
15459
15318
  const batchLocalSeen = /* @__PURE__ */ new Map();
15460
15319
  async function executeMutationOp(mut, targetDb) {
15461
15320
  const resolvedCapabilities = resolveCapabilitiesForResource2(schema, mut.resource);
15462
- if ((mut.operation === "insert" || mut.operation === "merge" || mut.operation === "replace") && typeof mut.record === "object" && mut.record !== null) {
15463
- const systemField = findSystemFieldWrite(schema.relations, mut.resource, mut.record);
15464
- if (systemField) {
15465
- return {
15466
- ok: false,
15467
- code: "DFQL_INVALID",
15468
- message: `Field is read-only: ${systemField} on ${mut.resource}`,
15469
- path: `record.${systemField}`
15470
- };
15471
- }
15472
- }
15473
15321
  switch (mut.operation) {
15474
15322
  case "insert": {
15475
15323
  const coercedData = applyDateCoercion(schema, mut.resource, mut.record || {}, logger);
@@ -16357,6 +16205,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16357
16205
  const counts = {};
16358
16206
  let joinCounts;
16359
16207
  const actorId = request.actorId;
16208
+ const checkedVisibleIds = /* @__PURE__ */ new Map();
16360
16209
  for (const resourceName of request.resources) {
16361
16210
  const resource = schema.resources.find((r) => r.name === resourceName);
16362
16211
  if (!resource) {
@@ -16380,7 +16229,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16380
16229
  };
16381
16230
  }
16382
16231
  try {
16383
- if (isPrivateShareableResource(schema, resourceName)) {
16232
+ if (isPrivateShareableResource(schema, resourceName) && !isNamespaceOwnerActor(actorId, namespace)) {
16384
16233
  if (!actorId) {
16385
16234
  counts[resourceName] = 0;
16386
16235
  continue;
@@ -16391,6 +16240,7 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16391
16240
  namespace
16392
16241
  });
16393
16242
  let visibleCount = 0;
16243
+ const visibleIds = /* @__PURE__ */ new Set();
16394
16244
  for (const row of rows) {
16395
16245
  const recordId = typeof row.id === "string" ? row.id : null;
16396
16246
  if (!recordId) {
@@ -16406,9 +16256,11 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16406
16256
  );
16407
16257
  if (level) {
16408
16258
  visibleCount++;
16259
+ visibleIds.add(recordId);
16409
16260
  }
16410
16261
  }
16411
16262
  counts[resourceName] = visibleCount;
16263
+ checkedVisibleIds.set(resourceName, visibleIds);
16412
16264
  continue;
16413
16265
  }
16414
16266
  if (typeof db.count === "function") {
@@ -16436,6 +16288,21 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16436
16288
  }
16437
16289
  if (request.includeJoins && schema.relations) {
16438
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
+ };
16439
16306
  for (const relation of schema.relations) {
16440
16307
  if (relation.type !== "many-many") continue;
16441
16308
  const fromResources = Array.isArray(relation.from) ? relation.from : [relation.from];
@@ -16493,9 +16360,29 @@ async function executeReconcile(request, schema, db, namespace, sequenceStore, l
16493
16360
  joinCounts[joinStoreKey] = 0;
16494
16361
  continue;
16495
16362
  }
16496
- joinCounts[joinStoreKey] = normalizedRows.filter(
16363
+ const storeRows = normalizedRows.filter(
16497
16364
  (row) => shouldIncludeJoinRowForStore2(row, relation, schema, fromResource, toResource)
16498
- ).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;
16499
16386
  }
16500
16387
  }
16501
16388
  }
@@ -18000,7 +17887,6 @@ var WebSocketManager = class {
18000
17887
  * Reverse mapping: client → known principalIds for O(1) targeted index cleanup.
18001
17888
  */
18002
17889
  this.clientPrincipals = /* @__PURE__ */ new Map();
18003
- this.routeTickets = /* @__PURE__ */ new Map();
18004
17890
  this.clientPlacement = /* @__PURE__ */ new Map();
18005
17891
  /** Monotonic namespace fence generation closes the validation/registration race. */
18006
17892
  this.namespaceFenceGeneration = /* @__PURE__ */ new Map();
@@ -18152,11 +18038,6 @@ var WebSocketManager = class {
18152
18038
  */
18153
18039
  addClient(client, authContext) {
18154
18040
  const { namespace } = authContext;
18155
- if (this.clientNamespace.has(client)) return false;
18156
- if (authContext.routeTicket && authContext.routeTicket.expiresAt <= (authContext.routeTicket.now ?? Date.now)()) {
18157
- this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
18158
- return false;
18159
- }
18160
18041
  if (this.closed) {
18161
18042
  this.closeClientSafely(client, 1001, "Going Away", "ws-closed-admission");
18162
18043
  return false;
@@ -18190,12 +18071,6 @@ var WebSocketManager = class {
18190
18071
  ...authContext.regionId ? { regionId: authContext.regionId } : {},
18191
18072
  ...Number.isSafeInteger(authContext.routingEpoch) ? { epoch: authContext.routingEpoch } : {}
18192
18073
  });
18193
- if (authContext.routeTicket) {
18194
- const { id, expiresAt, now = Date.now } = authContext.routeTicket;
18195
- const timer = setTimeout(() => this.expireRouteClient(client), Math.max(0, expiresAt - now()));
18196
- timer.unref?.();
18197
- this.routeTickets.set(client, { id, expiresAt, now, timer });
18198
- }
18199
18074
  if (this.heartbeatTimer === null) {
18200
18075
  this.startHeartbeat();
18201
18076
  }
@@ -18204,24 +18079,7 @@ var WebSocketManager = class {
18204
18079
  /**
18205
18080
  * Handle disconnection.
18206
18081
  */
18207
- expireRouteClient(client) {
18208
- this.removeClient(client);
18209
- this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
18210
- }
18211
- revokeRouteTicket(ticketId) {
18212
- let count = 0;
18213
- for (const [client, ticket] of this.routeTickets) {
18214
- if (ticket.id !== ticketId) continue;
18215
- this.removeClient(client);
18216
- this.closeClientSafely(client, 4403, "DATAFN_ROUTE_TICKET_REVOKED", "ws-ticket-revoked");
18217
- count++;
18218
- }
18219
- return count;
18220
- }
18221
18082
  removeClient(client) {
18222
- const ticket = this.routeTickets.get(client);
18223
- if (ticket) clearTimeout(ticket.timer);
18224
- this.routeTickets.delete(client);
18225
18083
  const namespace = this.clientNamespace.get(client);
18226
18084
  if (namespace !== void 0) {
18227
18085
  this.removeTargetingIndexes(client, namespace);
@@ -18338,16 +18196,9 @@ var WebSocketManager = class {
18338
18196
  principals: mode === "targeted" ? Array.from(requestedPrincipals).sort() : void 0
18339
18197
  }
18340
18198
  });
18341
- let sent = 0;
18342
18199
  for (const client of recipients) {
18343
- const ticket = this.routeTickets.get(client);
18344
- if (ticket && ticket.expiresAt <= ticket.now()) {
18345
- this.expireRouteClient(client);
18346
- continue;
18347
- }
18348
18200
  try {
18349
18201
  client.send(msg);
18350
- sent++;
18351
18202
  } catch (e) {
18352
18203
  this.logger?.error("Error sending to websocket client", { error: String(e), operation: "ws-broadcast" });
18353
18204
  this.removeClient(client);
@@ -18357,7 +18208,7 @@ var WebSocketManager = class {
18357
18208
  return {
18358
18209
  mode,
18359
18210
  degraded,
18360
- wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : sent
18211
+ wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : recipients.size
18361
18212
  };
18362
18213
  }
18363
18214
  /**
@@ -18376,8 +18227,6 @@ var WebSocketManager = class {
18376
18227
  this.clientNamespace.clear();
18377
18228
  this.clientPrincipals.clear();
18378
18229
  this.clientPlacement.clear();
18379
- for (const ticket of this.routeTickets.values()) clearTimeout(ticket.timer);
18380
- this.routeTickets.clear();
18381
18230
  this.pendingPong.clear();
18382
18231
  }
18383
18232
  /**
@@ -18512,7 +18361,9 @@ function createRateLimitMiddleware(config) {
18512
18361
  error: {
18513
18362
  code: "RATE_LIMITED",
18514
18363
  message: "Too many requests",
18515
- 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 }
18516
18367
  }
18517
18368
  }),
18518
18369
  {
@@ -18584,6 +18435,35 @@ function relationReferencesInternalResource(relation, internalResourceNames) {
18584
18435
  function normalizeResourceEndpoint(endpoint) {
18585
18436
  return typeof endpoint === "string" ? [endpoint] : [...endpoint];
18586
18437
  }
18438
+ function payloadReferencesInternalResource(payload, internalResourceNames) {
18439
+ if (internalResourceNames.size === 0) {
18440
+ return false;
18441
+ }
18442
+ if (Array.isArray(payload)) {
18443
+ return payload.some((entry) => payloadReferencesInternalResource(entry, internalResourceNames));
18444
+ }
18445
+ if (!payload || typeof payload !== "object") {
18446
+ return false;
18447
+ }
18448
+ const record = payload;
18449
+ if (typeof record.resource === "string" && internalResourceNames.has(record.resource)) {
18450
+ return true;
18451
+ }
18452
+ if (Array.isArray(record.resources) && record.resources.some((resource) => typeof resource === "string" && internalResourceNames.has(resource))) {
18453
+ return true;
18454
+ }
18455
+ for (const key of ["mutations", "queries", "steps", "operations"]) {
18456
+ if (Array.isArray(record[key]) && payloadReferencesInternalResource(record[key], internalResourceNames)) {
18457
+ return true;
18458
+ }
18459
+ }
18460
+ for (const key of ["query", "mutation", "payload"]) {
18461
+ if (record[key] && payloadReferencesInternalResource(record[key], internalResourceNames)) {
18462
+ return true;
18463
+ }
18464
+ }
18465
+ return false;
18466
+ }
18587
18467
  function normalizePluginAuthorizationError(error, pluginName) {
18588
18468
  if (error && typeof error === "object") {
18589
18469
  const candidate = error;
@@ -19182,10 +19062,10 @@ async function createDatafnServer(config) {
19182
19062
  }
19183
19063
  return { ok: true };
19184
19064
  };
19185
- const withAuth = (action, handler, origin = "http", rest = false) => {
19065
+ const withAuth = (action, handler) => {
19186
19066
  return async (req, ctx) => {
19187
19067
  const enrichedCtx = createEnrichedContext(ctx);
19188
- const placementRequest = multiRegionRuntime?.placement && origin === "http" ? req.clone() : void 0;
19068
+ const placementRequest = multiRegionRuntime?.placement ? req.clone() : void 0;
19189
19069
  let payload = null;
19190
19070
  if (shuttingDown) {
19191
19071
  return completeDatafnResponse({
@@ -19314,36 +19194,7 @@ async function createDatafnServer(config) {
19314
19194
  }
19315
19195
  enrichedCtx.parsedBody = payload;
19316
19196
  }
19317
- let structuralPayload = payload;
19318
- if (rest) {
19319
- let resource;
19320
- try {
19321
- const segment = sanitizePathSegment(new URL(req.url).pathname.split("/")[3] ?? "");
19322
- if (!segment.ok) throw new Error("Invalid path segment");
19323
- resource = segment.value;
19324
- } catch {
19325
- return completeDatafnResponse({
19326
- action,
19327
- request: req,
19328
- context: enrichedCtx,
19329
- payload,
19330
- response: errorResponse({ code: "DFQL_INVALID", message: "Invalid path segment", details: { path: "resource" } }, 400)
19331
- });
19332
- }
19333
- structuralPayload = { resource };
19334
- }
19335
- const parsedProtocol = parseDatafnRequest(action, structuralPayload, { schema: validatedSchema });
19336
- if (!parsedProtocol.ok) {
19337
- return completeDatafnResponse({
19338
- action,
19339
- request: req,
19340
- context: enrichedCtx,
19341
- payload,
19342
- response: errorResponse(parsedProtocol.error, 400)
19343
- });
19344
- }
19345
- const selection = collectStructuralResourceSelectors(parsedProtocol.result);
19346
- if (selection.selectors.some((resource) => internalResourceNames.has(resource))) {
19197
+ if (payloadReferencesInternalResource(payload, internalResourceNames)) {
19347
19198
  await emitDataFnEvent({
19348
19199
  domain: "datafn",
19349
19200
  type: "datafn.authorization.denied",
@@ -19369,9 +19220,7 @@ async function createDatafnServer(config) {
19369
19220
  namespace: await extractNamespace(enrichedCtx),
19370
19221
  regionId: multiRegionRuntime.regionId,
19371
19222
  runtime: multiRegionRuntime.placement,
19372
- request: placementRequest,
19373
- scope: action,
19374
- trustedInternal: origin === "executor"
19223
+ request: placementRequest
19375
19224
  });
19376
19225
  } catch (error) {
19377
19226
  if (error instanceof DatafnRoutingError) {
@@ -19390,7 +19239,7 @@ async function createDatafnServer(config) {
19390
19239
  action,
19391
19240
  request: req,
19392
19241
  context: enrichedCtx,
19393
- payload: structuralPayload
19242
+ payload
19394
19243
  });
19395
19244
  if (!pluginAuthorization.ok) {
19396
19245
  await emitDataFnEvent({
@@ -19410,7 +19259,7 @@ async function createDatafnServer(config) {
19410
19259
  });
19411
19260
  }
19412
19261
  if (config.authorize) {
19413
- const authorized = await config.authorize(enrichedCtx, action, structuralPayload);
19262
+ const authorized = await config.authorize(enrichedCtx, action, payload);
19414
19263
  if (!authorized) {
19415
19264
  await emitDataFnEvent({
19416
19265
  domain: "datafn",
@@ -19485,7 +19334,7 @@ async function createDatafnServer(config) {
19485
19334
  try {
19486
19335
  const configuredContext = config.context;
19487
19336
  const executorContext = context ?? (typeof configuredContext === "function" ? await configuredContext(request) : configuredContext ?? {});
19488
- response = await withAuth(action, handler, "executor")(request, executorContext);
19337
+ response = await withAuth(action, handler)(request, executorContext);
19489
19338
  } catch {
19490
19339
  throw new DatafnExecutorError(
19491
19340
  { code: "INTERNAL", message: "Internal Server Error" },
@@ -19675,9 +19524,7 @@ async function createDatafnServer(config) {
19675
19524
  method: route.method,
19676
19525
  path: route.path,
19677
19526
  meta: route.meta,
19678
- // withAuth and the REST handler validate URL segments and complete DataFn response hooks.
19679
- decodeParams: false,
19680
- handler: withAuth(action, route.handler, "http", true)
19527
+ handler: withAuth(action, route.handler)
19681
19528
  });
19682
19529
  }
19683
19530
  }
@@ -19849,18 +19696,12 @@ async function createDatafnServer(config) {
19849
19696
  regionId: multiRegionRuntime.regionId,
19850
19697
  runtime: multiRegionRuntime.placement,
19851
19698
  request: handshakeRequest,
19852
- scope: "websocket",
19853
- trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion && !multiRegionRuntime.placement.routeTickets
19699
+ trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion
19854
19700
  });
19855
19701
  added = wsManager.addClient(client, {
19856
19702
  ...authContext,
19857
19703
  regionId: validated.placement.regionId,
19858
- routingEpoch: validated.placement.epoch,
19859
- routeTicket: validated.ticket ? {
19860
- id: validated.ticket.ticketId,
19861
- expiresAt: validated.ticket.expiresAt,
19862
- now: multiRegionRuntime.placement.routeTickets?.now
19863
- } : void 0
19704
+ routingEpoch: validated.placement.epoch
19864
19705
  });
19865
19706
  if (!added) return false;
19866
19707
  if (wsManager.getNamespaceFenceGeneration(authContext.namespace) !== fenceGeneration) {
@@ -19879,7 +19720,7 @@ async function createDatafnServer(config) {
19879
19720
  if (added) wsManager.removeClient(client);
19880
19721
  try {
19881
19722
  client.close(
19882
- error instanceof DatafnRoutingError ? error.code === "DATAFN_ROUTE_RATE_LIMITED" ? 4503 : error.code === "DATAFN_ROUTE_TICKET_EXPIRED" ? 4511 : error.code.startsWith("DATAFN_ROUTE_") ? 4403 : 4510 : 1011,
19723
+ error instanceof DatafnRoutingError ? 4510 : 1011,
19883
19724
  error instanceof Error ? error.message : "Placement validation failed"
19884
19725
  );
19885
19726
  } catch {
@@ -19887,7 +19728,6 @@ async function createDatafnServer(config) {
19887
19728
  return false;
19888
19729
  }
19889
19730
  },
19890
- revokeRouteTicket: (ticketId) => wsManager.revokeRouteTicket(ticketId),
19891
19731
  fenceNamespace: (namespace, minimumEpoch) => wsManager.fenceNamespace(namespace, minimumEpoch),
19892
19732
  removeClient: (client) => wsManager.removeClient(client),
19893
19733
  handleMessage: (client, data) => wsManager.handleMessage(client, data),
@@ -20825,685 +20665,6 @@ function bytesToBase64Url(bytes) {
20825
20665
  return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
20826
20666
  }
20827
20667
 
20828
- // src/placement-policy.ts
20829
- var CONTINENT_CENTERS = {
20830
- AF: [1.65, 17.68],
20831
- AN: [-82.86, 135],
20832
- AS: [34.05, 100.62],
20833
- EU: [54.53, 15.26],
20834
- NA: [54.53, -105.26],
20835
- OC: [-22.74, 140.02],
20836
- SA: [-14.24, -51.93]
20837
- };
20838
- function finiteCoordinate(value, minimum, maximum) {
20839
- return value !== void 0 && Number.isFinite(value) && value >= minimum && value <= maximum;
20840
- }
20841
- function normalizedCandidates(input) {
20842
- const context = {
20843
- location: input.location ?? null,
20844
- preferredRegionId: normalizedPreferredRegionId(input.preferredRegionId)
20845
- };
20846
- const seen = /* @__PURE__ */ new Set();
20847
- const candidates = input.candidates.map((candidate) => {
20848
- const regionId = candidate.regionId.trim();
20849
- if (!regionId) throw new Error("DATAFN_PLACEMENT_REGION_ID_REQUIRED");
20850
- if (seen.has(regionId)) {
20851
- throw new Error(`DATAFN_PLACEMENT_REGION_DUPLICATE: ${regionId}`);
20852
- }
20853
- seen.add(regionId);
20854
- return regionId === candidate.regionId ? candidate : { ...candidate, regionId };
20855
- });
20856
- return candidates.filter(
20857
- (candidate) => (input.constraints ?? []).every(
20858
- (constraint) => constraint(candidate, context)
20859
- )
20860
- );
20861
- }
20862
- function normalizedPreferredRegionId(preferredRegionId) {
20863
- return preferredRegionId?.trim() || null;
20864
- }
20865
- function compareRegionIds(left, right) {
20866
- return left < right ? -1 : left > right ? 1 : 0;
20867
- }
20868
- function sourceCoordinates(location) {
20869
- if (!location) return null;
20870
- if (finiteCoordinate(location.latitude, -90, 90) && finiteCoordinate(location.longitude, -180, 180)) {
20871
- return {
20872
- coordinates: [location.latitude, location.longitude],
20873
- source: "coordinates"
20874
- };
20875
- }
20876
- const continent = location.continent?.toUpperCase();
20877
- const coordinates = continent ? CONTINENT_CENTERS[continent] : void 0;
20878
- return coordinates ? { coordinates, source: "continent" } : null;
20879
- }
20880
- function radians(degrees) {
20881
- return degrees * Math.PI / 180;
20882
- }
20883
- function distanceKilometers(source, target) {
20884
- const sourceLatitude = radians(source[0]);
20885
- const targetLatitude = radians(target[0]);
20886
- const latitudeDelta = radians(target[0] - source[0]);
20887
- const longitudeDelta = radians(target[1] - source[1]);
20888
- const haversine = Math.sin(latitudeDelta / 2) ** 2 + Math.cos(sourceLatitude) * Math.cos(targetLatitude) * Math.sin(longitudeDelta / 2) ** 2;
20889
- const clampedHaversine = Math.min(1, Math.max(0, haversine));
20890
- return 6371 * 2 * Math.atan2(Math.sqrt(clampedHaversine), Math.sqrt(1 - clampedHaversine));
20891
- }
20892
- function rankDatafnPlacementRegions(input) {
20893
- const candidates = normalizedCandidates(input);
20894
- const source = sourceCoordinates(input.location ?? null);
20895
- if (!source) return [];
20896
- return candidates.flatMap((candidate) => {
20897
- const { latitude, longitude } = candidate;
20898
- return finiteCoordinate(latitude, -90, 90) && finiteCoordinate(longitude, -180, 180) ? [
20899
- {
20900
- regionId: candidate.regionId,
20901
- source: source.source,
20902
- distanceKilometers: distanceKilometers(source.coordinates, [
20903
- latitude,
20904
- longitude
20905
- ])
20906
- }
20907
- ] : [];
20908
- }).sort(
20909
- (left, right) => (left.distanceKilometers ?? 0) - (right.distanceKilometers ?? 0) || compareRegionIds(left.regionId, right.regionId)
20910
- );
20911
- }
20912
- function stableIndex(key, length) {
20913
- let hash = 2166136261;
20914
- for (const byte of new TextEncoder().encode(key)) {
20915
- hash ^= byte;
20916
- hash = Math.imul(hash, 16777619) >>> 0;
20917
- }
20918
- return hash % length;
20919
- }
20920
- function selectDatafnPlacementRegion(input) {
20921
- const candidates = normalizedCandidates(input);
20922
- const preferredRegionId = normalizedPreferredRegionId(
20923
- input.preferredRegionId
20924
- );
20925
- if (preferredRegionId && candidates.some((candidate) => candidate.regionId === preferredRegionId)) {
20926
- return { regionId: preferredRegionId, source: "preferred" };
20927
- }
20928
- const ranked = rankDatafnPlacementRegions({
20929
- candidates,
20930
- location: input.location ?? null
20931
- });
20932
- const nearest = ranked[0];
20933
- if (nearest) return nearest;
20934
- if (input.stableKey !== void 0 && candidates.length > 0) {
20935
- const stableCandidates = [...candidates].sort(
20936
- (left, right) => compareRegionIds(left.regionId, right.regionId)
20937
- );
20938
- const selected = stableCandidates[stableIndex(input.stableKey, stableCandidates.length)];
20939
- if (selected) {
20940
- return { regionId: selected.regionId, source: "stable-fallback" };
20941
- }
20942
- }
20943
- return null;
20944
- }
20945
- function coordinate(value, minimum, maximum) {
20946
- const parsed = typeof value === "number" ? value : Number(value);
20947
- return Number.isFinite(parsed) && parsed >= minimum && parsed <= maximum ? parsed : void 0;
20948
- }
20949
- function readDatafnCloudflarePlacementLocation(request) {
20950
- const cf = request.cf;
20951
- if (!cf) return null;
20952
- const latitude = coordinate(cf.latitude, -90, 90);
20953
- const longitude = coordinate(cf.longitude, -180, 180);
20954
- const continent = typeof cf.continent === "string" && /^[A-Za-z]{2}$/u.test(cf.continent) ? cf.continent.toUpperCase() : void 0;
20955
- if (latitude === void 0 && longitude === void 0 && !continent)
20956
- return null;
20957
- return {
20958
- ...latitude === void 0 ? {} : { latitude },
20959
- ...longitude === void 0 ? {} : { longitude },
20960
- ...continent ? { continent } : {}
20961
- };
20962
- }
20963
- var POSTGRES_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
20964
- var DATAFN_INTERNAL_TABLE_RE = /^__datafn_[a-z0-9_]+$/;
20965
- var LEGACY_PERMISSIONS_TABLE_RE = /^__datafn_permissions_(?!global$)[A-Za-z0-9_]+$/;
20966
- var POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL = `
20967
- SELECT c.table_name, c.column_name
20968
- FROM information_schema.columns AS c
20969
- JOIN information_schema.tables AS t
20970
- ON t.table_schema = c.table_schema
20971
- AND t.table_name = c.table_name
20972
- WHERE c.table_schema = current_schema()
20973
- AND t.table_type = 'BASE TABLE'
20974
- ORDER BY c.table_name, c.ordinal_position
20975
- `.trim();
20976
- var INTERNAL_ROLE_BY_TABLE = {
20977
- __datafn_meta: "sync_meta",
20978
- __datafn_changes: "sync_changes",
20979
- __datafn_idempotency: "idempotency",
20980
- __datafn_seed: "seed",
20981
- __datafn_permission_directory_outbox: "permission_directory_outbox"
20982
- };
20983
- var SHARING_RESOURCE_TABLES = [
20984
- {
20985
- logicalRole: "permissions_global",
20986
- relation: "__datafn_permissions_global"
20987
- },
20988
- {
20989
- logicalRole: "principal_memberships",
20990
- relation: "__datafn_principal_memberships"
20991
- },
20992
- {
20993
- logicalRole: "principal_hierarchy",
20994
- relation: "__datafn_principal_hierarchy"
20995
- }
20996
- ];
20997
- var SELECTOR_COLUMN = {
20998
- resource_ns: "__ns",
20999
- internal_namespace: "namespace"
21000
- };
21001
- var DatafnNamespaceStorageError = class extends Error {
21002
- constructor(input) {
21003
- super(input.message);
21004
- this.name = "DatafnNamespaceStorageError";
21005
- this.code = input.code;
21006
- this.details = input.details;
21007
- }
21008
- };
21009
- function fail(code, message, details) {
21010
- throw new DatafnNamespaceStorageError({ code, message, details });
21011
- }
21012
- function quotePostgresIdentifier(identifier) {
21013
- if (!POSTGRES_IDENTIFIER_RE.test(identifier)) {
21014
- fail(
21015
- "DATAFN_NAMESPACE_STORAGE_INVALID",
21016
- `Invalid PostgreSQL identifier "${identifier}"`,
21017
- { identifier }
21018
- );
21019
- }
21020
- return `"${identifier}"`;
21021
- }
21022
- function namespacedInternalTableRoles() {
21023
- return INTERNAL_ROLE_BY_TABLE;
21024
- }
21025
- function catalogByName(catalog) {
21026
- const map = /* @__PURE__ */ new Map();
21027
- for (const relation of catalog.relations) {
21028
- if (map.has(relation.name)) fail("DATAFN_NAMESPACE_STORAGE_INVALID", "Duplicate catalog relation.");
21029
- map.set(relation.name, relation);
21030
- }
21031
- return map;
21032
- }
21033
- function hasColumn(relation, column) {
21034
- return Boolean(relation?.columns.includes(column));
21035
- }
21036
- function isShareableResource(schema, resourceName) {
21037
- const resource = schema.resources.find((entry) => entry.name === resourceName);
21038
- if (!resource) return false;
21039
- const resolved = resolveCapabilities(schema.capabilities, resource.capabilities);
21040
- return resolved.some(
21041
- (entry) => typeof entry === "object" && entry !== null && "shareable" in entry
21042
- );
21043
- }
21044
- function schemaDerivedRelations(schema) {
21045
- const planned = [];
21046
- const resourceRole = namespaceStorageManifestEntry("resource");
21047
- schema.resources.forEach((resource, index) => {
21048
- planned.push({
21049
- logicalRole: "resource",
21050
- relation: resource.name,
21051
- requiredColumns: [
21052
- "id",
21053
- ...resource.fields.map((field) => field.name),
21054
- ...getCapabilityFields(resolveCapabilities(schema.capabilities, resource.capabilities)).map((field) => field.name)
21055
- ].map(physicalColumn),
21056
- copyOrder: resourceRole.copyOrder + index
21057
- });
21058
- });
21059
- const joinRole = namespaceStorageManifestEntry("join");
21060
- const joinTables = /* @__PURE__ */ new Set();
21061
- for (const relation of schema.relations ?? []) {
21062
- if (relation.type !== "many-many") continue;
21063
- const table = getRelationJoinTableName(relation);
21064
- if (joinTables.has(table)) continue;
21065
- joinTables.add(table);
21066
- planned.push({
21067
- logicalRole: "join",
21068
- relation: table,
21069
- requiredColumns: [
21070
- "id",
21071
- relation.joinColumns?.from ?? "from",
21072
- relation.joinColumns?.to ?? "to",
21073
- ...(relation.metadata ?? []).map((field) => field.name)
21074
- ].map(physicalColumn),
21075
- copyOrder: joinRole.copyOrder + joinTables.size
21076
- });
21077
- }
21078
- const legacyRole = namespaceStorageManifestEntry("permissions_legacy");
21079
- schema.resources.forEach((resource, index) => {
21080
- if (!isShareableResource(schema, resource.name)) return;
21081
- planned.push({
21082
- logicalRole: "permissions_legacy",
21083
- relation: getLegacyPermissionsTable(resource.name),
21084
- copyOrder: legacyRole.copyOrder + index
21085
- });
21086
- });
21087
- return planned;
21088
- }
21089
- function internalPlannedRelations() {
21090
- return Object.entries(INTERNAL_ROLE_BY_TABLE).map(([relation, logicalRole]) => ({
21091
- logicalRole,
21092
- relation,
21093
- copyOrder: namespaceStorageManifestEntry(logicalRole).copyOrder
21094
- }));
21095
- }
21096
- function sharingPlannedRelations() {
21097
- return SHARING_RESOURCE_TABLES.map((entry) => ({
21098
- logicalRole: entry.logicalRole,
21099
- relation: entry.relation,
21100
- copyOrder: namespaceStorageManifestEntry(entry.logicalRole).copyOrder
21101
- }));
21102
- }
21103
- function physicalColumn(name) {
21104
- return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
21105
- }
21106
- var SHARING_REQUIRED_COLUMNS = {
21107
- permissions_global: ["id", "resource_type", "resource_ns", "resource_id", "principal_id", "level", "grant_kind", "source_ref", "granted_by", "granted_at", "revoked_at"],
21108
- permissions_legacy: ["id", "resource_id", "user_id", "level", "granted_by", "granted_at", "revoked_at"],
21109
- principal_memberships: ["id", "namespace", "actor_id", "principal_id", "granted_at", "revoked_at"],
21110
- principal_hierarchy: ["id", "namespace", "principal_id", "parent_principal_id", "created_at", "revoked_at"]
21111
- };
21112
- function toEntry(planned, catalog) {
21113
- const manifest = namespaceStorageManifestEntry(planned.logicalRole);
21114
- const namespaceColumn = SELECTOR_COLUMN[manifest.selectorKind];
21115
- const relation = catalog.get(planned.relation);
21116
- if (!relation) {
21117
- if (manifest.presence === "schema-derived") {
21118
- fail(
21119
- "DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
21120
- `Required namespace storage relation "${planned.relation}" is missing from the installed schema.`,
21121
- { logicalRole: planned.logicalRole, relation: planned.relation }
21122
- );
21123
- }
21124
- return null;
21125
- }
21126
- if (!hasColumn(relation, namespaceColumn)) {
21127
- fail(
21128
- "DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
21129
- `Namespace storage relation "${planned.relation}" is missing selector column "${namespaceColumn}".`,
21130
- {
21131
- logicalRole: planned.logicalRole,
21132
- relation: planned.relation,
21133
- expectedColumn: namespaceColumn,
21134
- columns: relation.columns
21135
- }
21136
- );
21137
- }
21138
- const required = planned.requiredColumns ?? INTERNAL_TABLE_SCHEMAS[planned.relation]?.map((column) => column.name) ?? SHARING_REQUIRED_COLUMNS[planned.logicalRole] ?? ["id"];
21139
- const missing = required.filter((column) => !hasColumn(relation, column));
21140
- if (missing.length > 0) {
21141
- fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing required columns: ${missing.join(", ")}.`);
21142
- }
21143
- const hasIdentity = relation.indexes?.some((index) => index.unique && index.primary === true && index.columns.includes("id") && index.columns.every((column) => column === "id" || column === namespaceColumn));
21144
- if (!hasIdentity) {
21145
- fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing a validated unique identity index.`);
21146
- }
21147
- return {
21148
- logicalRole: planned.logicalRole,
21149
- ownership: "datafn",
21150
- relation: planned.relation,
21151
- quotedRelation: quotePostgresIdentifier(planned.relation),
21152
- namespaceColumn,
21153
- quotedNamespaceColumn: quotePostgresIdentifier(namespaceColumn),
21154
- operations: manifest.operations,
21155
- copyOrder: planned.copyOrder,
21156
- present: true
21157
- };
21158
- }
21159
- function isDatafnOwnedCatalogRelation(name, columns) {
21160
- if (columns.includes("__ns")) return true;
21161
- return DATAFN_INTERNAL_TABLE_RE.test(name) && columns.includes("namespace");
21162
- }
21163
- function unexpectedCatalogRelations(catalog, plannedNames) {
21164
- const unexpected = [];
21165
- for (const relation of catalog.relations) {
21166
- if (plannedNames.has(relation.name)) continue;
21167
- if (!isDatafnOwnedCatalogRelation(relation.name, relation.columns)) continue;
21168
- unexpected.push(relation.name);
21169
- }
21170
- return unexpected.sort();
21171
- }
21172
- function resolveNamespaceStoragePlan(input) {
21173
- try {
21174
- assertSupportedNamespaceStorageVersions({
21175
- manifestVersion: input.manifestVersion,
21176
- schemaVersion: input.schemaVersion
21177
- });
21178
- } catch (error) {
21179
- const code = error && typeof error === "object" && "code" in error ? String(error.code) : "DATAFN_NAMESPACE_STORAGE_VERSION_MISMATCH";
21180
- fail(
21181
- code,
21182
- error instanceof Error ? error.message : String(error)
21183
- );
21184
- }
21185
- if (input.catalog.dialect !== "postgres") {
21186
- fail(
21187
- "DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
21188
- `Namespace storage plans are supported for PostgreSQL only. Received dialect "${String(input.catalog.dialect)}".`
21189
- );
21190
- }
21191
- if (!isNamespaced(input.schema)) {
21192
- fail(
21193
- "DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
21194
- "Namespace storage plans require a namespaced DataFn schema."
21195
- );
21196
- }
21197
- const catalog = catalogByName(input.catalog);
21198
- const planned = [
21199
- ...schemaDerivedRelations(input.schema),
21200
- ...sharingPlannedRelations(),
21201
- ...internalPlannedRelations()
21202
- ];
21203
- for (const relation of input.catalog.relations) {
21204
- if (LEGACY_PERMISSIONS_TABLE_RE.test(relation.name) && !planned.some((entry) => entry.relation === relation.name)) {
21205
- planned.push({
21206
- logicalRole: "permissions_legacy",
21207
- relation: relation.name,
21208
- copyOrder: namespaceStorageManifestEntry("permissions_legacy").copyOrder + 50
21209
- });
21210
- }
21211
- }
21212
- const plannedNames = new Set(planned.map((entry) => entry.relation));
21213
- const unexpected = unexpectedCatalogRelations(input.catalog, plannedNames);
21214
- if (unexpected.length > 0) {
21215
- fail(
21216
- "DATAFN_NAMESPACE_STORAGE_UNKNOWN",
21217
- `Installed schema contains DataFn namespace storage that is not in the supported contract: ${unexpected.join(", ")}.`,
21218
- { relations: unexpected }
21219
- );
21220
- }
21221
- const entries = planned.map((entry) => toEntry(entry, catalog)).filter((entry) => entry !== null).sort((left, right) => {
21222
- if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
21223
- return left.relation.localeCompare(right.relation);
21224
- });
21225
- return {
21226
- manifestVersion: DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION,
21227
- schemaVersion: DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION,
21228
- dialect: "postgres",
21229
- entries
21230
- };
21231
- }
21232
- function selectNamespaceStorageEntries(plan, operation) {
21233
- const selected = plan.entries.filter((entry) => entry.operations.includes(operation));
21234
- if (operation === "cleanup") {
21235
- return [...selected].sort((left, right) => {
21236
- if (left.copyOrder !== right.copyOrder) return right.copyOrder - left.copyOrder;
21237
- return right.relation.localeCompare(left.relation);
21238
- });
21239
- }
21240
- return selected;
21241
- }
21242
- function composeNamespaceStoragePlan(plan, applicationEntries) {
21243
- const existing = new Set(plan.entries.map((entry) => entry.relation));
21244
- const composed = [...plan.entries];
21245
- for (const entry of applicationEntries) {
21246
- if (existing.has(entry.relation)) {
21247
- fail(
21248
- "DATAFN_NAMESPACE_STORAGE_INVALID",
21249
- `Application storage relation "${entry.relation}" collides with DataFn-owned storage.`,
21250
- { relation: entry.relation }
21251
- );
21252
- }
21253
- existing.add(entry.relation);
21254
- composed.push({
21255
- logicalRole: "application",
21256
- ownership: "application",
21257
- relation: entry.relation,
21258
- quotedRelation: quotePostgresIdentifier(entry.relation),
21259
- namespaceColumn: entry.namespaceColumn,
21260
- quotedNamespaceColumn: quotePostgresIdentifier(entry.namespaceColumn),
21261
- operations: entry.operations ?? ["copy", "verify", "cleanup", "fence", "backup"],
21262
- copyOrder: entry.copyOrder ?? 10,
21263
- present: true
21264
- });
21265
- }
21266
- composed.sort((left, right) => {
21267
- if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
21268
- return left.relation.localeCompare(right.relation);
21269
- });
21270
- return { ...plan, entries: composed };
21271
- }
21272
- var POSTGRES_NAMESPACE_STORAGE_INDEX_SQL = `
21273
- SELECT t.relname AS table_name,
21274
- array_agg(a.attname ORDER BY k.ordinality) AS index_columns,
21275
- i.indisunique AS index_unique, i.indisprimary AS index_primary
21276
- FROM pg_index i
21277
- JOIN pg_class t ON t.oid = i.indrelid
21278
- JOIN pg_namespace n ON n.oid = t.relnamespace
21279
- CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ordinality)
21280
- LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
21281
- WHERE n.nspname = current_schema() AND i.indisvalid AND i.indisready
21282
- AND i.indpred IS NULL AND i.indexprs IS NULL
21283
- AND k.ordinality <= i.indnkeyatts
21284
- GROUP BY t.relname, i.indexrelid, i.indisunique, i.indisprimary
21285
- ORDER BY t.relname, i.indexrelid
21286
- `.trim();
21287
- async function inspectPostgresNamespaceStorage(query) {
21288
- const rows = await query(POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL);
21289
- const indexRows = await query(POSTGRES_NAMESPACE_STORAGE_INDEX_SQL);
21290
- const relations = /* @__PURE__ */ new Map();
21291
- for (const row of rows) {
21292
- if (typeof row.column_name !== "string") fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", "Catalog column metadata is missing.");
21293
- const columns = relations.get(row.table_name) ?? [];
21294
- columns.push(row.column_name);
21295
- relations.set(row.table_name, columns);
21296
- }
21297
- return {
21298
- dialect: "postgres",
21299
- relations: [...relations.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([name, columns]) => ({ name, columns, indexes: indexRows.filter((row) => row.table_name === name && Array.isArray(row.index_columns) && typeof row.index_unique === "boolean").map((row) => ({ columns: row.index_columns, unique: row.index_unique, primary: row.index_primary === true })) }))
21300
- };
21301
- }
21302
- function assertInternalNamespaceStorageMetadataComplete() {
21303
- const namespaced = listNamespacedInternalTables();
21304
- const mapped = Object.keys(INTERNAL_ROLE_BY_TABLE).sort();
21305
- if (namespaced.join("\0") !== mapped.join("\0")) {
21306
- fail(
21307
- "DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
21308
- "Internal namespace tables are missing from the storage contract.",
21309
- {
21310
- namespacedInternalTables: namespaced,
21311
- mappedInternalTables: mapped,
21312
- schemas: Object.keys(INTERNAL_TABLE_SCHEMAS).sort()
21313
- }
21314
- );
21315
- }
21316
- }
21317
- async function drainNamespaceStorage(input) {
21318
- const drainEntries = selectNamespaceStorageEntries(input.plan, "drain");
21319
- if (drainEntries.length === 0) {
21320
- return { processed: 0, pending: 0 };
21321
- }
21322
- const unsupported = drainEntries.filter(
21323
- (entry) => entry.logicalRole !== "permission_directory_outbox"
21324
- );
21325
- if (unsupported.length > 0) {
21326
- fail(
21327
- "DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
21328
- `No drain implementation for logical roles: ${unsupported.map((entry) => entry.logicalRole).join(", ")}.`,
21329
- { roles: unsupported.map((entry) => entry.logicalRole) }
21330
- );
21331
- }
21332
- return drainNamespacePermissionDirectoryOutbox(
21333
- input.adapter,
21334
- input.namespace,
21335
- input.runtime,
21336
- input.logger
21337
- );
21338
- }
21339
- async function resolveAuthoritativeAncestorInactive(adapter, schema, resource, record, namespace, maxGraphNodes) {
21340
- const active = /* @__PURE__ */ new Set();
21341
- const resolved = /* @__PURE__ */ new Map();
21342
- let visited = 0;
21343
- async function visit(model, row, root = false) {
21344
- if (typeof row.id !== "string" || row.id.length === 0) {
21345
- throw new Error("Ancestor repair: malformed record identifier");
21346
- }
21347
- const key = JSON.stringify([model, row.id]);
21348
- if (active.has(key)) throw new Error("Ancestor repair: cycle detected");
21349
- if (resolved.has(key)) return resolved.get(key);
21350
- if (++visited > maxGraphNodes) throw new Error("Ancestor repair: maxGraphNodes exceeded");
21351
- active.add(key);
21352
- let inherited = false;
21353
- for (const relation of schema.relations ?? []) {
21354
- if (relation.inheritsInactive !== true || relation.type === "many-many") continue;
21355
- const dependent = relation.type === "many-one" ? relation.from : relation.to;
21356
- if (!endpointIncludes(dependent, model)) continue;
21357
- const endpoint = relation.type === "many-one" ? relation.to : relation.from;
21358
- const field = relation.type === "many-one" ? relationFkFieldForManyOne(relation) : relation.type === "htree" ? relation.fkField || relation.foreignKey || relation.inverse || "parentId" : relationFkFieldForOneMany(relation);
21359
- const parentId = row[field];
21360
- if (parentId === null || parentId === void 0 || parentId === "") continue;
21361
- if (typeof parentId !== "string") throw new Error("Ancestor repair: malformed parent link");
21362
- const resourceField = relation.fkResourceField || (relation.type === "htree" ? `${field.replace(/Id$/, "")}Resource` : `${(relation.type === "many-one" ? relation.relation || "target" : relation.inverse || relation.relation || "source").replace(/Id$/, "")}Resource`);
21363
- const discriminator = row[resourceField];
21364
- const inferred = resolveEndpointResource(endpoint, parentId, schema);
21365
- let parentResource = inferred;
21366
- if (endpointList(endpoint).length > 1 && discriminator != null) {
21367
- if (typeof discriminator !== "string" || !endpointIncludes(endpoint, discriminator) || inferred && inferred !== discriminator) {
21368
- throw new Error("Ancestor repair: invalid parent resource discriminator");
21369
- }
21370
- parentResource = discriminator;
21371
- }
21372
- if (!parentResource) throw new Error("Ancestor repair: ambiguous parent resource");
21373
- const parent = await adapter.findOne({
21374
- model: parentResource,
21375
- where: [{ field: "id", operator: "eq", value: parentId }],
21376
- namespace
21377
- });
21378
- if (!parent) throw new Error("Ancestor repair: missing parent record");
21379
- const parentInactive = await visit(parentResource, parent);
21380
- inherited = inherited || parentInactive;
21381
- }
21382
- active.delete(key);
21383
- const effective = inherited || row.isArchived === true || row.trashedAt != null;
21384
- resolved.set(key, effective);
21385
- return root ? inherited : effective;
21386
- }
21387
- return visit(resource, record, true);
21388
- }
21389
-
21390
- // src/execution/migration/ancestor-inactive.ts
21391
- var DEFAULT_BATCH_SIZE = 500;
21392
- function compareCodePoints(a, b) {
21393
- return a < b ? -1 : a > b ? 1 : 0;
21394
- }
21395
- function ancestorInactiveResources(schema) {
21396
- return [...getAncestorInactiveResources(schema.relations)].sort(compareCodePoints);
21397
- }
21398
- async function recomputeAncestorInactive(adapter, schema, options) {
21399
- const { namespace, dryRun = false, logger } = options;
21400
- const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
21401
- const maxGraphNodes = options.maxGraphNodes ?? 1e4;
21402
- for (const value of [batchSize, maxGraphNodes]) {
21403
- if (!Number.isSafeInteger(value) || value < 1) throw new Error("Ancestor repair: limits must be positive safe integers");
21404
- }
21405
- const resources = ancestorInactiveResources(schema);
21406
- if (resources.length === 0) return { scanned: 0, updated: 0, skipped: 0, nextCursor: null };
21407
- let cursor = options.cursor ?? { resource: resources[0], afterId: null };
21408
- let resourceIndex = resources.indexOf(cursor.resource);
21409
- if (resourceIndex === -1) {
21410
- throw new Error(`recomputeAncestorInactive: unknown cursor resource "${cursor.resource}"`);
21411
- }
21412
- let scanned = 0;
21413
- let updated = 0;
21414
- let skipped = 0;
21415
- let remaining = batchSize;
21416
- while (remaining > 0) {
21417
- const resource = resources[resourceIndex];
21418
- const requested = remaining;
21419
- const rows = await adapter.findMany({
21420
- model: resource,
21421
- where: cursor.afterId === null ? [] : [{ field: "id", operator: "gt", value: cursor.afterId }],
21422
- orderBy: [{ field: "id", direction: "asc" }],
21423
- limit: requested,
21424
- namespace
21425
- });
21426
- for (const row of rows) {
21427
- const id = row.id;
21428
- if (typeof id !== "string" || id.length === 0) throw new Error("Ancestor repair: malformed record identifier");
21429
- scanned += 1;
21430
- const next = await resolveAuthoritativeAncestorInactive(adapter, schema, resource, row, namespace, maxGraphNodes);
21431
- const previous = row[ANCESTOR_INACTIVE_FIELD];
21432
- if (previous !== next) {
21433
- if (dryRun) {
21434
- updated += 1;
21435
- } else {
21436
- const affected = await adapter.updateMany({
21437
- model: resource,
21438
- where: [
21439
- { field: "id", operator: "eq", value: id },
21440
- {
21441
- field: ANCESTOR_INACTIVE_FIELD,
21442
- operator: "eq",
21443
- value: previous ?? null
21444
- }
21445
- ],
21446
- data: { [ANCESTOR_INACTIVE_FIELD]: next },
21447
- namespace
21448
- });
21449
- updated += affected > 0 ? 1 : 0;
21450
- skipped += affected > 0 ? 0 : 1;
21451
- }
21452
- }
21453
- cursor = { resource, afterId: id };
21454
- }
21455
- remaining -= rows.length;
21456
- if (rows.length < requested) {
21457
- resourceIndex += 1;
21458
- if (resourceIndex >= resources.length) {
21459
- logger?.info("datafn.ancestorInactive.recompute.complete", { namespace, scanned, updated, skipped, dryRun });
21460
- return { scanned, updated, skipped, nextCursor: null };
21461
- }
21462
- cursor = { resource: resources[resourceIndex], afterId: null };
21463
- }
21464
- }
21465
- return { scanned, updated, skipped, nextCursor: cursor };
21466
- }
21467
- async function recomputeAncestorInactiveAll(adapter, schema, options) {
21468
- const maxSweeps = options.maxSweeps ?? 32;
21469
- if (!Number.isSafeInteger(maxSweeps) || maxSweeps < 1) throw new Error("Ancestor repair: maxSweeps must be a positive safe integer");
21470
- let scanned = 0;
21471
- let updated = 0;
21472
- let skipped = 0;
21473
- let sweeps = 0;
21474
- let converged = false;
21475
- while (sweeps < maxSweeps) {
21476
- sweeps += 1;
21477
- let sweepUpdated = 0;
21478
- let sweepSkipped = 0;
21479
- let cursor = null;
21480
- do {
21481
- const result = await recomputeAncestorInactive(adapter, schema, { ...options, cursor });
21482
- scanned += result.scanned;
21483
- sweepUpdated += result.updated;
21484
- sweepSkipped += result.skipped;
21485
- cursor = result.nextCursor;
21486
- } while (cursor !== null);
21487
- updated += sweepUpdated;
21488
- skipped += sweepSkipped;
21489
- if (sweepUpdated === 0 && sweepSkipped === 0) {
21490
- converged = true;
21491
- break;
21492
- }
21493
- if (options.dryRun) break;
21494
- }
21495
- if (!converged) {
21496
- options.logger?.warn("datafn.ancestorInactive.recompute.notConverged", {
21497
- namespace: options.namespace,
21498
- sweeps,
21499
- updated,
21500
- skipped,
21501
- dryRun: options.dryRun === true
21502
- });
21503
- }
21504
- return { scanned, updated, skipped, sweeps, converged };
21505
- }
21506
-
21507
- export { AtomicSequenceStore, ChainedSequenceStore, DATAFN_MULTI_REGION_CAPABILITY, DATAFN_ROUTING_ASSERTION_HEADER, DATAFN_ROUTING_INTERNAL_HEADERS, DatabaseSequenceStore, DatafnExecutorError, DatafnNamespaceStorageError, DatafnPublicLinkInputError, DatafnRoutingError, POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL, POSTGRES_NAMESPACE_STORAGE_INDEX_SQL, ancestorInactiveResources, assertInternalNamespaceStorageMetadataComplete, claimDatafnNamespacePlacement, composeNamespaceStoragePlan, createConditionalKvDatafnPlacementDirectory, createDatafnGatewayRouter, createDatafnHmacRouteTickets, createDatafnHmacRoutingAssertions, createDatafnMultiRegionPlugin, createDatafnPublicLinksPlugin, createDatafnRouteBootstrap, createDatafnServer, createMemoryDatafnPlacementDirectory, createMemoryDatafnRoutingReplayStore, createSequenceStore, datafn, datafnMultiRegionPlugin, drainNamespacePermissionDirectoryOutbox, drainNamespaceStorage, inspectPostgresNamespaceStorage, migrateDatafnNamespace, namespacedInternalTableRoles, quotePostgresIdentifier, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, readDatafnPublicLinkToken, readDatafnRouteTicket, recomputeAncestorInactive, recomputeAncestorInactiveAll, resolveDatafnPublicLink, resolveNamespaceStoragePlan, routeTicketError, selectDatafnPlacementRegion, selectNamespaceStorageEntries, validateDatafnPlacement, validateDatafnRouteTicket, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRegionalCors, withDatafnRoutingAssertion };
20668
+ export { AtomicSequenceStore, ChainedSequenceStore, DATAFN_MULTI_REGION_CAPABILITY, DATAFN_ROUTING_ASSERTION_HEADER, DATAFN_ROUTING_INTERNAL_HEADERS, DatabaseSequenceStore, DatafnExecutorError, DatafnPublicLinkInputError, DatafnRoutingError, claimDatafnNamespacePlacement, createConditionalKvDatafnPlacementDirectory, createDatafnGatewayRouter, createDatafnHmacRoutingAssertions, createDatafnMultiRegionPlugin, createDatafnPublicLinksPlugin, createDatafnServer, createMemoryDatafnPlacementDirectory, createMemoryDatafnRoutingReplayStore, createSequenceStore, datafn, datafnMultiRegionPlugin, migrateDatafnNamespace, readDatafnPublicLinkToken, resolveDatafnPublicLink, validateDatafnPlacement, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRoutingAssertion };
21508
20669
  //# sourceMappingURL=index.js.map
21509
20670
  //# sourceMappingURL=index.js.map