@datafn/server 0.1.0 → 0.1.3
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/README.md +48 -0
- package/dist/index.cjs +1252 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +269 -5
- package/dist/index.d.ts +269 -5
- package/dist/index.js +1200 -79
- package/dist/index.js.map +1 -1
- package/dist/placement.cjs +143 -0
- package/dist/placement.cjs.map +1 -0
- package/dist/placement.d.cts +49 -0
- package/dist/placement.d.ts +49 -0
- package/dist/placement.js +139 -0
- package/dist/placement.js.map +1 -0
- package/package.json +13 -5
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, hasTemporalGrouping, resolveCapabilities, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1,
|
|
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';
|
|
2
3
|
import { createRouter, createObservabilityMiddleware, executeMiddlewareChain } from '@superfunctions/http';
|
|
3
4
|
import { wrapWithRowLevelNamespace, instrumentAdapter, instrumentKVStore } from '@superfunctions/db';
|
|
4
5
|
import { normalizeObservability } from '@superfunctions/observability';
|
|
5
|
-
import {
|
|
6
|
+
import { timingSafeEqual, randomUUID, createHash, createHmac } from 'crypto';
|
|
6
7
|
|
|
7
|
-
// src/
|
|
8
|
+
// src/index.ts
|
|
8
9
|
|
|
9
10
|
// src/execution/internal-tables.ts
|
|
10
11
|
var META_COLUMNS = [
|
|
@@ -54,6 +55,9 @@ var INTERNAL_TABLE_SCHEMAS = {
|
|
|
54
55
|
__datafn_seed: SEED_COLUMNS,
|
|
55
56
|
__datafn_permission_directory_outbox: PERMISSION_DIRECTORY_OUTBOX_COLUMNS
|
|
56
57
|
};
|
|
58
|
+
function listNamespacedInternalTables() {
|
|
59
|
+
return Object.entries(INTERNAL_TABLE_SCHEMAS).filter(([, columns]) => columns.some((column) => column.name === "namespace")).map(([name]) => name).sort();
|
|
60
|
+
}
|
|
57
61
|
async function ensureInternalTable(adapter, tableName) {
|
|
58
62
|
const columns = INTERNAL_TABLE_SCHEMAS[tableName];
|
|
59
63
|
if (!columns) {
|
|
@@ -470,8 +474,8 @@ function createSequenceStore(config) {
|
|
|
470
474
|
// src/http/json.ts
|
|
471
475
|
async function parseJsonBody(req) {
|
|
472
476
|
try {
|
|
473
|
-
const
|
|
474
|
-
if (!
|
|
477
|
+
const text2 = await req.text();
|
|
478
|
+
if (!text2 || text2.trim() === "") {
|
|
475
479
|
return {
|
|
476
480
|
ok: false,
|
|
477
481
|
error: {
|
|
@@ -481,7 +485,7 @@ async function parseJsonBody(req) {
|
|
|
481
485
|
}
|
|
482
486
|
};
|
|
483
487
|
}
|
|
484
|
-
const data = JSON.parse(
|
|
488
|
+
const data = JSON.parse(text2);
|
|
485
489
|
return { ok: true, data };
|
|
486
490
|
} catch (error) {
|
|
487
491
|
return {
|
|
@@ -731,6 +735,13 @@ function validateRecordKeys(index, resourceName, record, basePath, mode = "write
|
|
|
731
735
|
for (const key of Object.keys(record)) {
|
|
732
736
|
if (key === "id") continue;
|
|
733
737
|
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
|
+
}
|
|
734
745
|
return vErr(
|
|
735
746
|
"DFQL_UNKNOWN_FIELD",
|
|
736
747
|
`Unknown field: ${key} on ${resourceName}`,
|
|
@@ -1019,13 +1030,32 @@ function validateMutation(mutation, index, basePath = "$", opts) {
|
|
|
1019
1030
|
`${basePath}.record.${fieldName}`
|
|
1020
1031
|
);
|
|
1021
1032
|
}
|
|
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
|
+
}
|
|
1022
1052
|
}
|
|
1023
1053
|
if (m.operation === "insert" || m.operation === "replace") {
|
|
1024
1054
|
const resource2 = resourceResult.result;
|
|
1025
1055
|
const record2 = m.record;
|
|
1026
1056
|
for (const field of resource2.fields) {
|
|
1027
1057
|
if (field.required && field.default === void 0 && field.name !== "id" && !field.readonly) {
|
|
1028
|
-
if (!(field.name in record2) || record2[field.name] ===
|
|
1058
|
+
if (!(field.name in record2) || record2[field.name] === void 0 || record2[field.name] === null && !field.nullable) {
|
|
1029
1059
|
return vErr(
|
|
1030
1060
|
"DFQL_INVALID",
|
|
1031
1061
|
`Required field missing: ${field.name}`,
|
|
@@ -3273,6 +3303,22 @@ function filterAncestorInactiveRecords(records, match, metadata) {
|
|
|
3273
3303
|
}
|
|
3274
3304
|
return records.filter((record) => record.isAncestorInactive !== true);
|
|
3275
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
|
+
}
|
|
3276
3322
|
function applyOmit(record, omit) {
|
|
3277
3323
|
if (!omit || omit.length === 0) {
|
|
3278
3324
|
return record;
|
|
@@ -3281,13 +3327,8 @@ function applyOmit(record, omit) {
|
|
|
3281
3327
|
for (const [key, value] of Object.entries(record)) {
|
|
3282
3328
|
if (key === "id" || !omit.includes(key)) {
|
|
3283
3329
|
if (Array.isArray(value)) {
|
|
3284
|
-
result[key] = value.map((item) =>
|
|
3285
|
-
|
|
3286
|
-
return applyOmit(item, omit);
|
|
3287
|
-
}
|
|
3288
|
-
return item;
|
|
3289
|
-
});
|
|
3290
|
-
} else if (typeof value === "object" && value !== null && key !== "$relation_metadata") {
|
|
3330
|
+
result[key] = value.map((item) => applyOmitToArrayValue(item, omit));
|
|
3331
|
+
} else if (isPlainRecord(value) && key !== "$relation_metadata") {
|
|
3291
3332
|
result[key] = applyOmit(value, omit);
|
|
3292
3333
|
} else {
|
|
3293
3334
|
result[key] = value;
|
|
@@ -3388,6 +3429,7 @@ function getFkFieldsToOmit(schema, targetResource) {
|
|
|
3388
3429
|
function materializeSelect(record, resource, select, schema, store, omit, metadata) {
|
|
3389
3430
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
3390
3431
|
if (!resourceSchema) return { id: record.id };
|
|
3432
|
+
record = stripNullsForNonNullableFields(record, resourceSchema);
|
|
3391
3433
|
const fkFieldsToOmit = getFkFieldsToOmit(schema, resource);
|
|
3392
3434
|
const effectiveOmit = omit ? [...omit, ...Array.from(fkFieldsToOmit)] : Array.from(fkFieldsToOmit);
|
|
3393
3435
|
if (!select || select.length === 0) {
|
|
@@ -3496,7 +3538,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3496
3538
|
),
|
|
3497
3539
|
htreeRelation,
|
|
3498
3540
|
metadata
|
|
3499
|
-
).map(
|
|
3541
|
+
).map(
|
|
3542
|
+
(c) => applyOmit(stripNullsForNonNullableFields(c, resourceSchema), omit)
|
|
3543
|
+
);
|
|
3500
3544
|
} else if (parsed.directive === "**") {
|
|
3501
3545
|
result[parsed.baseName] = filterAncestorInactiveRecords(
|
|
3502
3546
|
htreeChildren(
|
|
@@ -3508,7 +3552,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3508
3552
|
),
|
|
3509
3553
|
htreeRelation,
|
|
3510
3554
|
metadata
|
|
3511
|
-
).map(
|
|
3555
|
+
).map(
|
|
3556
|
+
(d) => applyOmit(stripNullsForNonNullableFields(d, resourceSchema), omit)
|
|
3557
|
+
);
|
|
3512
3558
|
}
|
|
3513
3559
|
includedFields.add(parsed.baseName);
|
|
3514
3560
|
} else if (parsed.baseName === "parent") {
|
|
@@ -3517,7 +3563,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3517
3563
|
resource,
|
|
3518
3564
|
htreeRelation.relation,
|
|
3519
3565
|
store
|
|
3520
|
-
).map(
|
|
3566
|
+
).map(
|
|
3567
|
+
(a) => applyOmit(stripNullsForNonNullableFields(a, resourceSchema), omit)
|
|
3568
|
+
);
|
|
3521
3569
|
includedFields.add(parsed.baseName);
|
|
3522
3570
|
}
|
|
3523
3571
|
continue;
|
|
@@ -3813,8 +3861,10 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3813
3861
|
}
|
|
3814
3862
|
const aggregations = query.aggregations || {};
|
|
3815
3863
|
let results = [];
|
|
3864
|
+
const samples = /* @__PURE__ */ new WeakMap();
|
|
3816
3865
|
for (const [, groupRecords] of groups.entries()) {
|
|
3817
3866
|
const row = {};
|
|
3867
|
+
samples.set(row, groupRecords[0]);
|
|
3818
3868
|
groupBy.forEach((field) => {
|
|
3819
3869
|
const sample = groupRecords[0];
|
|
3820
3870
|
const val = resolveValue(
|
|
@@ -3825,7 +3875,9 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3825
3875
|
query.resource,
|
|
3826
3876
|
resourceSchema
|
|
3827
3877
|
);
|
|
3828
|
-
|
|
3878
|
+
if (val !== void 0) {
|
|
3879
|
+
row[field] = val;
|
|
3880
|
+
}
|
|
3829
3881
|
});
|
|
3830
3882
|
for (const group of temporalGroups) {
|
|
3831
3883
|
row[group.alias] = resolveTemporalBucketValue(groupRecords[0], group);
|
|
@@ -3865,7 +3917,24 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3865
3917
|
paginated.length = limit;
|
|
3866
3918
|
}
|
|
3867
3919
|
return {
|
|
3868
|
-
groups: paginated
|
|
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
|
+
}),
|
|
3869
3938
|
nextCursor
|
|
3870
3939
|
};
|
|
3871
3940
|
}
|
|
@@ -3894,9 +3963,10 @@ function orderGroupedResults(groups, sortTerms) {
|
|
|
3894
3963
|
return 0;
|
|
3895
3964
|
});
|
|
3896
3965
|
}
|
|
3897
|
-
function resolveValue(record, path, schema, store, resourceName, precomputedResource) {
|
|
3966
|
+
function resolveValue(record, path, schema, store, resourceName, precomputedResource, normalize = false) {
|
|
3898
3967
|
if (!path.includes(".")) {
|
|
3899
|
-
|
|
3968
|
+
const definition2 = precomputedResource?.fields.find((field) => field.name === path);
|
|
3969
|
+
return normalize && record[path] === null && definition2 && definition2.nullable !== true ? void 0 : record[path];
|
|
3900
3970
|
}
|
|
3901
3971
|
const parts = path.split(".");
|
|
3902
3972
|
const resource = precomputedResource ?? schema.resources.find((r) => r.name === resourceName);
|
|
@@ -3934,7 +4004,8 @@ function resolveValue(record, path, schema, store, resourceName, precomputedReso
|
|
|
3934
4004
|
}
|
|
3935
4005
|
}
|
|
3936
4006
|
const lastField = parts[parts.length - 1];
|
|
3937
|
-
|
|
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];
|
|
3938
4009
|
}
|
|
3939
4010
|
var DFQL_TO_ADAPTER_OP = {
|
|
3940
4011
|
// $-prefixed DFQL operators
|
|
@@ -5164,6 +5235,7 @@ async function pushdownCore(query, schema, db, namespace) {
|
|
|
5164
5235
|
function projectFieldsPushdown(record, resource, schema, select, omit) {
|
|
5165
5236
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
5166
5237
|
if (!resourceSchema) return { id: record.id };
|
|
5238
|
+
record = stripNullsForNonNullableFields(record, resourceSchema);
|
|
5167
5239
|
const resolvedCapabilities = resolveCapabilities(
|
|
5168
5240
|
schema.capabilities,
|
|
5169
5241
|
resourceSchema.capabilities
|
|
@@ -5932,6 +6004,213 @@ var AuthResolverCache = class {
|
|
|
5932
6004
|
function createAuthResolverCache() {
|
|
5933
6005
|
return new AuthResolverCache();
|
|
5934
6006
|
}
|
|
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
|
+
}
|
|
5935
6214
|
var DATAFN_ROUTING_ASSERTION_HEADER = "x-datafn-routing-assertion";
|
|
5936
6215
|
var DATAFN_ROUTING_INTERNAL_HEADERS = [
|
|
5937
6216
|
DATAFN_ROUTING_ASSERTION_HEADER,
|
|
@@ -6083,6 +6362,28 @@ async function claimDatafnNamespacePlacement(input) {
|
|
|
6083
6362
|
async function validateDatafnPlacement(input) {
|
|
6084
6363
|
const startedAt = Date.now();
|
|
6085
6364
|
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
|
+
}
|
|
6086
6387
|
let placement;
|
|
6087
6388
|
try {
|
|
6088
6389
|
placement = await input.runtime.directory.get(input.namespace);
|
|
@@ -6120,7 +6421,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6120
6421
|
retryable: false
|
|
6121
6422
|
});
|
|
6122
6423
|
}
|
|
6123
|
-
const assertion = await validateRoutingAssertion({
|
|
6424
|
+
const assertion = ticket ? void 0 : await validateRoutingAssertion({
|
|
6124
6425
|
namespace: input.namespace,
|
|
6125
6426
|
regionId: input.regionId,
|
|
6126
6427
|
placement,
|
|
@@ -6176,7 +6477,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6176
6477
|
internal: Boolean(assertion)
|
|
6177
6478
|
});
|
|
6178
6479
|
}
|
|
6179
|
-
if (assertion && assertion.epoch !== placement.epoch) {
|
|
6480
|
+
if (assertion && assertion.epoch !== placement.epoch || ticket && ticket.epoch !== placement.epoch) {
|
|
6180
6481
|
await emitRoutingEvent(input.runtime, {
|
|
6181
6482
|
type: "mismatch",
|
|
6182
6483
|
requestId: requestId2,
|
|
@@ -6189,14 +6490,17 @@ async function validateDatafnPlacement(input) {
|
|
|
6189
6490
|
});
|
|
6190
6491
|
throw new DatafnRoutingError({
|
|
6191
6492
|
code: "DATAFN_REGION_MISMATCH",
|
|
6192
|
-
message: "Routing
|
|
6493
|
+
message: "Routing grant uses a stale placement epoch",
|
|
6193
6494
|
status: 409,
|
|
6194
6495
|
retryable: true,
|
|
6195
6496
|
placement,
|
|
6196
|
-
internal:
|
|
6497
|
+
internal: Boolean(assertion)
|
|
6197
6498
|
});
|
|
6198
6499
|
}
|
|
6199
|
-
|
|
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 } : {} };
|
|
6200
6504
|
}
|
|
6201
6505
|
function createDatafnGatewayRouter(config) {
|
|
6202
6506
|
const maxBodyBytes = validateDatafnRoutingBodyLimit(config.maxBodyBytes);
|
|
@@ -8627,12 +8931,16 @@ function buildReplaceRecord(schema, resourceName, existingRecord, newRecord, res
|
|
|
8627
8931
|
result[key] = existingRecord[key];
|
|
8628
8932
|
continue;
|
|
8629
8933
|
}
|
|
8934
|
+
if (field.readonly) {
|
|
8935
|
+
result[key] = Object.prototype.hasOwnProperty.call(newRecord, key) ? newRecord[key] : existingRecord[key];
|
|
8936
|
+
continue;
|
|
8937
|
+
}
|
|
8630
8938
|
if (Object.prototype.hasOwnProperty.call(newRecord, key)) {
|
|
8631
8939
|
result[key] = newRecord[key];
|
|
8632
8940
|
} else {
|
|
8633
8941
|
result[key] = field.default !== void 0 ? field.default : null;
|
|
8634
8942
|
}
|
|
8635
|
-
if (field.required && (result[key] ===
|
|
8943
|
+
if (field.required && (result[key] === void 0 || result[key] === null && !field.nullable)) {
|
|
8636
8944
|
return {
|
|
8637
8945
|
ok: false,
|
|
8638
8946
|
code: "DFQL_INVALID",
|
|
@@ -8719,8 +9027,7 @@ function isRecordEffectivelyInactive(record) {
|
|
|
8719
9027
|
return isRecordInactive(record) || record?.isAncestorInactive === true;
|
|
8720
9028
|
}
|
|
8721
9029
|
function resourceSupportsAncestorInactive(schema, resource) {
|
|
8722
|
-
|
|
8723
|
-
return resourceSchema?.fields.some((field) => field.name === "isAncestorInactive") === true;
|
|
9030
|
+
return resourceRequiresAncestorInactive(schema.relations, resource);
|
|
8724
9031
|
}
|
|
8725
9032
|
function dependentEndpoint(relation) {
|
|
8726
9033
|
return relation.type === "many-one" ? relation.from : relation.to;
|
|
@@ -10139,7 +10446,7 @@ function decideMergeMode(schema, resource, id, delta) {
|
|
|
10139
10446
|
for (const field of resourceDef.fields) {
|
|
10140
10447
|
if (!field.required) continue;
|
|
10141
10448
|
if (field.name === "id" || field.name === "__ns") continue;
|
|
10142
|
-
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && delta[field.name] !== null;
|
|
10449
|
+
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && (delta[field.name] !== null || field.nullable === true);
|
|
10143
10450
|
const hasDefault = field.default !== void 0;
|
|
10144
10451
|
if (!hasProvided && !hasDefault) return { mode: "not_found" };
|
|
10145
10452
|
}
|
|
@@ -11878,6 +12185,64 @@ async function drainPermissionDirectoryOutbox(db, runtime, logger, limit = 100)
|
|
|
11878
12185
|
}
|
|
11879
12186
|
return { processed, pending: tasks.length - processed };
|
|
11880
12187
|
}
|
|
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
|
+
}
|
|
11881
12246
|
|
|
11882
12247
|
// src/execution/mutation/execute.ts
|
|
11883
12248
|
var FAILED_SHARE_COMPENSATION_RESULT = /* @__PURE__ */ Symbol("failedShareCompensation");
|
|
@@ -11979,6 +12344,23 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
11979
12344
|
let additionalChanges = [];
|
|
11980
12345
|
let skipChangeTracking = false;
|
|
11981
12346
|
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
|
+
}
|
|
11982
12364
|
try {
|
|
11983
12365
|
switch (mutation.operation) {
|
|
11984
12366
|
case "insert":
|
|
@@ -14438,7 +14820,7 @@ function compareCanonicalChangeEntries(a, b) {
|
|
|
14438
14820
|
function mergeCanonicalChangeStreamsBounded(streams, limit) {
|
|
14439
14821
|
const heads = streams.filter((stream) => stream.changes.length > 0).map((stream) => ({ resource: stream.resource, changes: stream.changes, index: 0 }));
|
|
14440
14822
|
const merged = [];
|
|
14441
|
-
const latestCursorPerResource =
|
|
14823
|
+
const latestCursorPerResource = /* @__PURE__ */ Object.create(null);
|
|
14442
14824
|
let peakWindow = heads.length;
|
|
14443
14825
|
while (heads.length > 0 && merged.length < limit) {
|
|
14444
14826
|
let bestIdx = 0;
|
|
@@ -14670,11 +15052,11 @@ async function executePullCanonical(request, schema, db, namespace, sequenceStor
|
|
|
14670
15052
|
const requestedResourceKeys = Object.keys(request.cursors).filter(
|
|
14671
15053
|
(resource) => resource !== ACTOR_FEED_CURSOR_KEY
|
|
14672
15054
|
);
|
|
14673
|
-
const records =
|
|
14674
|
-
const merged =
|
|
14675
|
-
const deleted =
|
|
14676
|
-
const joins =
|
|
14677
|
-
const cursors =
|
|
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);
|
|
14678
15060
|
for (const resource of requestedResourceKeys) {
|
|
14679
15061
|
if (!resource.startsWith("join_") && !resource.startsWith("__datafn_")) {
|
|
14680
15062
|
records[resource] = [];
|
|
@@ -15077,6 +15459,17 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15077
15459
|
const batchLocalSeen = /* @__PURE__ */ new Map();
|
|
15078
15460
|
async function executeMutationOp(mut, targetDb) {
|
|
15079
15461
|
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
|
+
}
|
|
15080
15473
|
switch (mut.operation) {
|
|
15081
15474
|
case "insert": {
|
|
15082
15475
|
const coercedData = applyDateCoercion(schema, mut.resource, mut.record || {}, logger);
|
|
@@ -17607,6 +18000,7 @@ var WebSocketManager = class {
|
|
|
17607
18000
|
* Reverse mapping: client → known principalIds for O(1) targeted index cleanup.
|
|
17608
18001
|
*/
|
|
17609
18002
|
this.clientPrincipals = /* @__PURE__ */ new Map();
|
|
18003
|
+
this.routeTickets = /* @__PURE__ */ new Map();
|
|
17610
18004
|
this.clientPlacement = /* @__PURE__ */ new Map();
|
|
17611
18005
|
/** Monotonic namespace fence generation closes the validation/registration race. */
|
|
17612
18006
|
this.namespaceFenceGeneration = /* @__PURE__ */ new Map();
|
|
@@ -17758,6 +18152,11 @@ var WebSocketManager = class {
|
|
|
17758
18152
|
*/
|
|
17759
18153
|
addClient(client, authContext) {
|
|
17760
18154
|
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
|
+
}
|
|
17761
18160
|
if (this.closed) {
|
|
17762
18161
|
this.closeClientSafely(client, 1001, "Going Away", "ws-closed-admission");
|
|
17763
18162
|
return false;
|
|
@@ -17791,6 +18190,12 @@ var WebSocketManager = class {
|
|
|
17791
18190
|
...authContext.regionId ? { regionId: authContext.regionId } : {},
|
|
17792
18191
|
...Number.isSafeInteger(authContext.routingEpoch) ? { epoch: authContext.routingEpoch } : {}
|
|
17793
18192
|
});
|
|
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
|
+
}
|
|
17794
18199
|
if (this.heartbeatTimer === null) {
|
|
17795
18200
|
this.startHeartbeat();
|
|
17796
18201
|
}
|
|
@@ -17799,7 +18204,24 @@ var WebSocketManager = class {
|
|
|
17799
18204
|
/**
|
|
17800
18205
|
* Handle disconnection.
|
|
17801
18206
|
*/
|
|
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
|
+
}
|
|
17802
18221
|
removeClient(client) {
|
|
18222
|
+
const ticket = this.routeTickets.get(client);
|
|
18223
|
+
if (ticket) clearTimeout(ticket.timer);
|
|
18224
|
+
this.routeTickets.delete(client);
|
|
17803
18225
|
const namespace = this.clientNamespace.get(client);
|
|
17804
18226
|
if (namespace !== void 0) {
|
|
17805
18227
|
this.removeTargetingIndexes(client, namespace);
|
|
@@ -17916,9 +18338,16 @@ var WebSocketManager = class {
|
|
|
17916
18338
|
principals: mode === "targeted" ? Array.from(requestedPrincipals).sort() : void 0
|
|
17917
18339
|
}
|
|
17918
18340
|
});
|
|
18341
|
+
let sent = 0;
|
|
17919
18342
|
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
|
+
}
|
|
17920
18348
|
try {
|
|
17921
18349
|
client.send(msg);
|
|
18350
|
+
sent++;
|
|
17922
18351
|
} catch (e) {
|
|
17923
18352
|
this.logger?.error("Error sending to websocket client", { error: String(e), operation: "ws-broadcast" });
|
|
17924
18353
|
this.removeClient(client);
|
|
@@ -17928,7 +18357,7 @@ var WebSocketManager = class {
|
|
|
17928
18357
|
return {
|
|
17929
18358
|
mode,
|
|
17930
18359
|
degraded,
|
|
17931
|
-
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" :
|
|
18360
|
+
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : sent
|
|
17932
18361
|
};
|
|
17933
18362
|
}
|
|
17934
18363
|
/**
|
|
@@ -17947,6 +18376,8 @@ var WebSocketManager = class {
|
|
|
17947
18376
|
this.clientNamespace.clear();
|
|
17948
18377
|
this.clientPrincipals.clear();
|
|
17949
18378
|
this.clientPlacement.clear();
|
|
18379
|
+
for (const ticket of this.routeTickets.values()) clearTimeout(ticket.timer);
|
|
18380
|
+
this.routeTickets.clear();
|
|
17950
18381
|
this.pendingPong.clear();
|
|
17951
18382
|
}
|
|
17952
18383
|
/**
|
|
@@ -18153,35 +18584,6 @@ function relationReferencesInternalResource(relation, internalResourceNames) {
|
|
|
18153
18584
|
function normalizeResourceEndpoint(endpoint) {
|
|
18154
18585
|
return typeof endpoint === "string" ? [endpoint] : [...endpoint];
|
|
18155
18586
|
}
|
|
18156
|
-
function payloadReferencesInternalResource(payload, internalResourceNames) {
|
|
18157
|
-
if (internalResourceNames.size === 0) {
|
|
18158
|
-
return false;
|
|
18159
|
-
}
|
|
18160
|
-
if (Array.isArray(payload)) {
|
|
18161
|
-
return payload.some((entry) => payloadReferencesInternalResource(entry, internalResourceNames));
|
|
18162
|
-
}
|
|
18163
|
-
if (!payload || typeof payload !== "object") {
|
|
18164
|
-
return false;
|
|
18165
|
-
}
|
|
18166
|
-
const record = payload;
|
|
18167
|
-
if (typeof record.resource === "string" && internalResourceNames.has(record.resource)) {
|
|
18168
|
-
return true;
|
|
18169
|
-
}
|
|
18170
|
-
if (Array.isArray(record.resources) && record.resources.some((resource) => typeof resource === "string" && internalResourceNames.has(resource))) {
|
|
18171
|
-
return true;
|
|
18172
|
-
}
|
|
18173
|
-
for (const key of ["mutations", "queries", "steps", "operations"]) {
|
|
18174
|
-
if (Array.isArray(record[key]) && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18175
|
-
return true;
|
|
18176
|
-
}
|
|
18177
|
-
}
|
|
18178
|
-
for (const key of ["query", "mutation", "payload"]) {
|
|
18179
|
-
if (record[key] && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18180
|
-
return true;
|
|
18181
|
-
}
|
|
18182
|
-
}
|
|
18183
|
-
return false;
|
|
18184
|
-
}
|
|
18185
18587
|
function normalizePluginAuthorizationError(error, pluginName) {
|
|
18186
18588
|
if (error && typeof error === "object") {
|
|
18187
18589
|
const candidate = error;
|
|
@@ -18780,10 +19182,10 @@ async function createDatafnServer(config) {
|
|
|
18780
19182
|
}
|
|
18781
19183
|
return { ok: true };
|
|
18782
19184
|
};
|
|
18783
|
-
const withAuth = (action, handler) => {
|
|
19185
|
+
const withAuth = (action, handler, origin = "http", rest = false) => {
|
|
18784
19186
|
return async (req, ctx) => {
|
|
18785
19187
|
const enrichedCtx = createEnrichedContext(ctx);
|
|
18786
|
-
const placementRequest = multiRegionRuntime?.placement ? req.clone() : void 0;
|
|
19188
|
+
const placementRequest = multiRegionRuntime?.placement && origin === "http" ? req.clone() : void 0;
|
|
18787
19189
|
let payload = null;
|
|
18788
19190
|
if (shuttingDown) {
|
|
18789
19191
|
return completeDatafnResponse({
|
|
@@ -18912,7 +19314,36 @@ async function createDatafnServer(config) {
|
|
|
18912
19314
|
}
|
|
18913
19315
|
enrichedCtx.parsedBody = payload;
|
|
18914
19316
|
}
|
|
18915
|
-
|
|
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))) {
|
|
18916
19347
|
await emitDataFnEvent({
|
|
18917
19348
|
domain: "datafn",
|
|
18918
19349
|
type: "datafn.authorization.denied",
|
|
@@ -18938,7 +19369,9 @@ async function createDatafnServer(config) {
|
|
|
18938
19369
|
namespace: await extractNamespace(enrichedCtx),
|
|
18939
19370
|
regionId: multiRegionRuntime.regionId,
|
|
18940
19371
|
runtime: multiRegionRuntime.placement,
|
|
18941
|
-
request: placementRequest
|
|
19372
|
+
request: placementRequest,
|
|
19373
|
+
scope: action,
|
|
19374
|
+
trustedInternal: origin === "executor"
|
|
18942
19375
|
});
|
|
18943
19376
|
} catch (error) {
|
|
18944
19377
|
if (error instanceof DatafnRoutingError) {
|
|
@@ -18957,7 +19390,7 @@ async function createDatafnServer(config) {
|
|
|
18957
19390
|
action,
|
|
18958
19391
|
request: req,
|
|
18959
19392
|
context: enrichedCtx,
|
|
18960
|
-
payload
|
|
19393
|
+
payload: structuralPayload
|
|
18961
19394
|
});
|
|
18962
19395
|
if (!pluginAuthorization.ok) {
|
|
18963
19396
|
await emitDataFnEvent({
|
|
@@ -18977,7 +19410,7 @@ async function createDatafnServer(config) {
|
|
|
18977
19410
|
});
|
|
18978
19411
|
}
|
|
18979
19412
|
if (config.authorize) {
|
|
18980
|
-
const authorized = await config.authorize(enrichedCtx, action,
|
|
19413
|
+
const authorized = await config.authorize(enrichedCtx, action, structuralPayload);
|
|
18981
19414
|
if (!authorized) {
|
|
18982
19415
|
await emitDataFnEvent({
|
|
18983
19416
|
domain: "datafn",
|
|
@@ -19052,7 +19485,7 @@ async function createDatafnServer(config) {
|
|
|
19052
19485
|
try {
|
|
19053
19486
|
const configuredContext = config.context;
|
|
19054
19487
|
const executorContext = context ?? (typeof configuredContext === "function" ? await configuredContext(request) : configuredContext ?? {});
|
|
19055
|
-
response = await withAuth(action, handler)(request, executorContext);
|
|
19488
|
+
response = await withAuth(action, handler, "executor")(request, executorContext);
|
|
19056
19489
|
} catch {
|
|
19057
19490
|
throw new DatafnExecutorError(
|
|
19058
19491
|
{ code: "INTERNAL", message: "Internal Server Error" },
|
|
@@ -19242,7 +19675,9 @@ async function createDatafnServer(config) {
|
|
|
19242
19675
|
method: route.method,
|
|
19243
19676
|
path: route.path,
|
|
19244
19677
|
meta: route.meta,
|
|
19245
|
-
|
|
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)
|
|
19246
19681
|
});
|
|
19247
19682
|
}
|
|
19248
19683
|
}
|
|
@@ -19414,12 +19849,18 @@ async function createDatafnServer(config) {
|
|
|
19414
19849
|
regionId: multiRegionRuntime.regionId,
|
|
19415
19850
|
runtime: multiRegionRuntime.placement,
|
|
19416
19851
|
request: handshakeRequest,
|
|
19417
|
-
|
|
19852
|
+
scope: "websocket",
|
|
19853
|
+
trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion && !multiRegionRuntime.placement.routeTickets
|
|
19418
19854
|
});
|
|
19419
19855
|
added = wsManager.addClient(client, {
|
|
19420
19856
|
...authContext,
|
|
19421
19857
|
regionId: validated.placement.regionId,
|
|
19422
|
-
routingEpoch: validated.placement.epoch
|
|
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
|
|
19423
19864
|
});
|
|
19424
19865
|
if (!added) return false;
|
|
19425
19866
|
if (wsManager.getNamespaceFenceGeneration(authContext.namespace) !== fenceGeneration) {
|
|
@@ -19438,7 +19879,7 @@ async function createDatafnServer(config) {
|
|
|
19438
19879
|
if (added) wsManager.removeClient(client);
|
|
19439
19880
|
try {
|
|
19440
19881
|
client.close(
|
|
19441
|
-
error instanceof DatafnRoutingError ? 4510 : 1011,
|
|
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,
|
|
19442
19883
|
error instanceof Error ? error.message : "Placement validation failed"
|
|
19443
19884
|
);
|
|
19444
19885
|
} catch {
|
|
@@ -19446,6 +19887,7 @@ async function createDatafnServer(config) {
|
|
|
19446
19887
|
return false;
|
|
19447
19888
|
}
|
|
19448
19889
|
},
|
|
19890
|
+
revokeRouteTicket: (ticketId) => wsManager.revokeRouteTicket(ticketId),
|
|
19449
19891
|
fenceNamespace: (namespace, minimumEpoch) => wsManager.fenceNamespace(namespace, minimumEpoch),
|
|
19450
19892
|
removeClient: (client) => wsManager.removeClient(client),
|
|
19451
19893
|
handleMessage: (client, data) => wsManager.handleMessage(client, data),
|
|
@@ -20383,6 +20825,685 @@ function bytesToBase64Url(bytes) {
|
|
|
20383
20825
|
return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
20384
20826
|
}
|
|
20385
20827
|
|
|
20386
|
-
|
|
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 };
|
|
20387
21508
|
//# sourceMappingURL=index.js.map
|
|
20388
21509
|
//# sourceMappingURL=index.js.map
|