@datafn/server 0.1.1 → 0.2.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/README.md +20 -0
- package/dist/index.cjs +1199 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +291 -5
- package/dist/index.d.ts +291 -5
- package/dist/index.js +1147 -79
- package/dist/index.js.map +1 -1
- package/package.json +8 -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, createPrivateKey, sign, createPublicKey, verify, 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,292 @@ 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
|
+
var routeKeyId = (value) => /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
6072
|
+
var routeSigningInput = (content) => Buffer.from(`datafn-regional-route-v1:${content}`);
|
|
6073
|
+
function createDatafnEd25519RouteTicketSigner(input) {
|
|
6074
|
+
if (!routeKeyId(input.activeKeyId)) throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6075
|
+
let key;
|
|
6076
|
+
try {
|
|
6077
|
+
key = typeof input.privateKey === "string" ? createPrivateKey(input.privateKey) : input.privateKey;
|
|
6078
|
+
} catch {
|
|
6079
|
+
throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6080
|
+
}
|
|
6081
|
+
if (key.type !== "private" || key.asymmetricKeyType !== "ed25519") throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6082
|
+
return {
|
|
6083
|
+
sign(claims) {
|
|
6084
|
+
assertClaims(claims);
|
|
6085
|
+
const header = Buffer.from(JSON.stringify({ typ: "datafn-route-v1", alg: "EdDSA", kid: input.activeKeyId })).toString("base64url");
|
|
6086
|
+
const payload = Buffer.from(JSON.stringify(claims)).toString("base64url");
|
|
6087
|
+
const content = `${header}.${payload}`;
|
|
6088
|
+
return `${content}.${sign(null, routeSigningInput(content), key).toString("base64url")}`;
|
|
6089
|
+
}
|
|
6090
|
+
};
|
|
6091
|
+
}
|
|
6092
|
+
function createDatafnEd25519RouteTicketVerifier(input) {
|
|
6093
|
+
const keys = new Map(Object.entries(input.publicKeys).map(([id, value]) => {
|
|
6094
|
+
if (!routeKeyId(id)) throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6095
|
+
if (typeof value === "string" && !value.startsWith("-----BEGIN PUBLIC KEY-----")) {
|
|
6096
|
+
throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6097
|
+
}
|
|
6098
|
+
let key;
|
|
6099
|
+
try {
|
|
6100
|
+
key = typeof value === "string" ? createPublicKey(value) : value;
|
|
6101
|
+
} catch {
|
|
6102
|
+
throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6103
|
+
}
|
|
6104
|
+
if (key.type !== "public" || key.asymmetricKeyType !== "ed25519") throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6105
|
+
return [id, key];
|
|
6106
|
+
}));
|
|
6107
|
+
if (keys.size === 0) throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6108
|
+
return {
|
|
6109
|
+
verify(ticket) {
|
|
6110
|
+
try {
|
|
6111
|
+
if (typeof ticket !== "string" || ticket.length > 16384 || !/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(ticket)) throw new Error();
|
|
6112
|
+
const [header, payload, signature] = ticket.split(".");
|
|
6113
|
+
const protectedHeader = JSON.parse(Buffer.from(header, "base64url").toString());
|
|
6114
|
+
const key = keys.get(protectedHeader.kid);
|
|
6115
|
+
if (!key || protectedHeader.typ !== "datafn-route-v1" || protectedHeader.alg !== "EdDSA") throw new Error();
|
|
6116
|
+
if (!verify(null, routeSigningInput(`${header}.${payload}`), key, Buffer.from(signature, "base64url"))) throw new Error();
|
|
6117
|
+
const claims = JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
6118
|
+
assertClaims(claims);
|
|
6119
|
+
return claims;
|
|
6120
|
+
} catch {
|
|
6121
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6122
|
+
}
|
|
6123
|
+
}
|
|
6124
|
+
};
|
|
6125
|
+
}
|
|
6126
|
+
function createDatafnRouteBootstrap(config) {
|
|
6127
|
+
const ttl = config.ttlMs ?? 6e4;
|
|
6128
|
+
if (!text(config.issuer) || !Number.isSafeInteger(ttl) || ttl < 1e3 || ttl > DATAFN_ROUTE_MAX_TTL_MS) {
|
|
6129
|
+
throw new Error("DATAFN_ROUTE_BOOTSTRAP_CONFIG_INVALID");
|
|
6130
|
+
}
|
|
6131
|
+
const now = config.now ?? Date.now;
|
|
6132
|
+
return async (request) => {
|
|
6133
|
+
try {
|
|
6134
|
+
if (request.method !== "POST") return new Response(null, { status: 405, headers: { allow: "POST", "cache-control": "no-store" } });
|
|
6135
|
+
const identity = await config.authenticate(request);
|
|
6136
|
+
if (!identity || !text(identity.subject) || !text(identity.namespace)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6137
|
+
let placement;
|
|
6138
|
+
try {
|
|
6139
|
+
placement = await config.directory.get(identity.namespace);
|
|
6140
|
+
} catch {
|
|
6141
|
+
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_UNAVAILABLE", message: "Placement unavailable", status: 503, retryable: true });
|
|
6142
|
+
}
|
|
6143
|
+
if (!placement || placement.namespace !== identity.namespace || placement.state === "tombstoned") {
|
|
6144
|
+
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_NOT_FOUND", message: "Placement not found", status: 404, retryable: false });
|
|
6145
|
+
}
|
|
6146
|
+
if (placement.state !== "active" || placement.migration) {
|
|
6147
|
+
throw new DatafnRoutingError({ code: "DATAFN_NAMESPACE_MOVING", message: "Placement is fenced", status: 409, retryable: true });
|
|
6148
|
+
}
|
|
6149
|
+
const permitted = [...await config.authorize(identity)];
|
|
6150
|
+
const endpoint = await config.resolveEndpoint(placement);
|
|
6151
|
+
const issuedAt = now();
|
|
6152
|
+
const expiresAt = Math.min(issuedAt + ttl, identity.expiresAt ?? Infinity);
|
|
6153
|
+
if (!Number.isSafeInteger(expiresAt) || expiresAt <= issuedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6154
|
+
const httpUrl = validateDatafnRegionalEndpoint(endpoint.httpUrl);
|
|
6155
|
+
const wsUrl = endpoint.wsUrl ? validateDatafnRegionalEndpoint(endpoint.wsUrl, true) : void 0;
|
|
6156
|
+
if (permitted.includes("websocket") && !wsUrl) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6157
|
+
const ticket = await config.signer.sign({
|
|
6158
|
+
version: 1,
|
|
6159
|
+
issuer: config.issuer,
|
|
6160
|
+
audience: endpoint.audience,
|
|
6161
|
+
subject: identity.subject,
|
|
6162
|
+
namespace: identity.namespace,
|
|
6163
|
+
regionId: placement.regionId,
|
|
6164
|
+
epoch: placement.epoch,
|
|
6165
|
+
scopes: permitted,
|
|
6166
|
+
issuedAt,
|
|
6167
|
+
notBefore: issuedAt,
|
|
6168
|
+
expiresAt,
|
|
6169
|
+
ticketId: randomUUID(),
|
|
6170
|
+
...identity.sessionBinding ? { sessionBinding: identity.sessionBinding } : {}
|
|
6171
|
+
});
|
|
6172
|
+
const descriptor = {
|
|
6173
|
+
version: 1,
|
|
6174
|
+
httpUrl,
|
|
6175
|
+
...wsUrl ? { wsUrl } : {},
|
|
6176
|
+
ticket,
|
|
6177
|
+
expiresAt,
|
|
6178
|
+
renewAfter: issuedAt + Math.floor((expiresAt - issuedAt) * 0.8)
|
|
6179
|
+
};
|
|
6180
|
+
emit(config, { type: "issued" });
|
|
6181
|
+
return Response.json(descriptor, { headers: { "cache-control": "no-store", pragma: "no-cache" } });
|
|
6182
|
+
} catch (error) {
|
|
6183
|
+
emit(config, { type: "rejected", code: error instanceof DatafnRoutingError ? error.code : "DATAFN_ROUTE_FORBIDDEN" });
|
|
6184
|
+
return (error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_FORBIDDEN")).toResponse();
|
|
6185
|
+
}
|
|
6186
|
+
};
|
|
6187
|
+
}
|
|
6188
|
+
function readDatafnRouteTicket(request, websocket = false) {
|
|
6189
|
+
const header = request.headers.get(DATAFN_ROUTE_TICKET_HEADER);
|
|
6190
|
+
const protocols = (request.headers.get("sec-websocket-protocol") ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
6191
|
+
const tickets = protocols.filter((s) => s.startsWith(DATAFN_ROUTE_WS_TICKET_PREFIX));
|
|
6192
|
+
if (tickets.length > 1 || header && tickets.length || !websocket && tickets.length || tickets.length && !protocols.includes(DATAFN_ROUTE_WS_PROTOCOL)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6193
|
+
return header ?? tickets[0]?.slice(DATAFN_ROUTE_WS_TICKET_PREFIX.length);
|
|
6194
|
+
}
|
|
6195
|
+
async function verifyDatafnRegionalTicketIdentity(input) {
|
|
6196
|
+
const { request, regionId, runtime } = input;
|
|
6197
|
+
try {
|
|
6198
|
+
const origin = request.headers.get("origin");
|
|
6199
|
+
if (origin && !runtime.allowedOrigins?.includes(origin)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6200
|
+
if (!runtime.authenticate && request.headers.has("cookie")) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6201
|
+
const token = readDatafnRouteTicket(request, input.websocket);
|
|
6202
|
+
if (!token) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6203
|
+
const claims = await runtime.verifier.verify(token);
|
|
6204
|
+
assertClaims(claims);
|
|
6205
|
+
const now = (runtime.now ?? Date.now)();
|
|
6206
|
+
const skew = runtime.clockSkewMs ?? 5e3;
|
|
6207
|
+
if (!Number.isFinite(skew) || skew < 0 || skew > 3e4) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6208
|
+
if (claims.expiresAt <= now) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6209
|
+
if (claims.issuedAt > now + skew || claims.notBefore > now + skew || claims.issuer !== runtime.issuer || claims.audience !== runtime.audience || claims.regionId !== regionId) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6210
|
+
return claims;
|
|
6211
|
+
} catch (error) {
|
|
6212
|
+
const safe = error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6213
|
+
emit(runtime, { type: "rejected", code: safe.code });
|
|
6214
|
+
throw safe;
|
|
6215
|
+
}
|
|
6216
|
+
}
|
|
6217
|
+
async function validateDatafnRouteTicket(input) {
|
|
6218
|
+
const { runtime } = input;
|
|
6219
|
+
const claims = await verifyDatafnRegionalTicketIdentity({
|
|
6220
|
+
request: input.request,
|
|
6221
|
+
regionId: input.regionId,
|
|
6222
|
+
runtime,
|
|
6223
|
+
websocket: input.scope === "websocket"
|
|
6224
|
+
});
|
|
6225
|
+
try {
|
|
6226
|
+
if (claims.namespace !== input.namespace || !claims.scopes.includes(input.scope)) {
|
|
6227
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6228
|
+
}
|
|
6229
|
+
let identity;
|
|
6230
|
+
if (runtime.authenticate) {
|
|
6231
|
+
identity = await runtime.authenticate(input.request);
|
|
6232
|
+
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)())) {
|
|
6233
|
+
throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6234
|
+
}
|
|
6235
|
+
if (identity.expiresAt !== void 0 && claims.expiresAt > identity.expiresAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6236
|
+
}
|
|
6237
|
+
if (runtime.isActive && !await runtime.isActive(claims)) throw routeTicketError("DATAFN_ROUTE_TICKET_REVOKED");
|
|
6238
|
+
if (runtime.allowRequest && !await runtime.allowRequest(claims)) throw routeTicketError("DATAFN_ROUTE_RATE_LIMITED");
|
|
6239
|
+
const admittedAt = (runtime.now ?? Date.now)();
|
|
6240
|
+
if (identity?.expiresAt !== void 0 && identity.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6241
|
+
if (claims.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6242
|
+
emit(runtime, { type: "accepted" });
|
|
6243
|
+
return claims;
|
|
6244
|
+
} catch (error) {
|
|
6245
|
+
const safe = error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6246
|
+
emit(runtime, { type: "rejected", code: safe.code });
|
|
6247
|
+
throw safe;
|
|
6248
|
+
}
|
|
6249
|
+
}
|
|
6250
|
+
function withDatafnRegionalCors(handler, options) {
|
|
6251
|
+
const origins = new Set(options.origins.map((origin) => {
|
|
6252
|
+
if (new URL(origin).origin !== origin || origin === "null") throw new Error("DATAFN_ROUTE_ORIGIN_INVALID");
|
|
6253
|
+
return origin;
|
|
6254
|
+
}));
|
|
6255
|
+
const allowedHeaders = /* @__PURE__ */ new Set([
|
|
6256
|
+
"content-type",
|
|
6257
|
+
"authorization",
|
|
6258
|
+
DATAFN_ROUTE_TICKET_HEADER,
|
|
6259
|
+
...(options.headers ?? []).map((header) => header.toLowerCase())
|
|
6260
|
+
]);
|
|
6261
|
+
const allowedMethods = ["GET", "POST", "PATCH", "DELETE"];
|
|
6262
|
+
return async (request) => {
|
|
6263
|
+
const origin = request.headers.get("origin");
|
|
6264
|
+
if (origin && !origins.has(origin)) return routeTicketError("DATAFN_ROUTE_FORBIDDEN").toResponse();
|
|
6265
|
+
if (request.headers.has("cookie")) return routeTicketError("DATAFN_ROUTE_FORBIDDEN").toResponse();
|
|
6266
|
+
if (request.method === "OPTIONS") {
|
|
6267
|
+
const method = request.headers.get("access-control-request-method");
|
|
6268
|
+
const headers2 = (request.headers.get("access-control-request-headers") ?? "").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
6269
|
+
if (!origin || !allowedMethods.includes(method ?? "") || headers2.some((h) => !allowedHeaders.has(h))) return new Response(null, { status: 403 });
|
|
6270
|
+
return new Response(null, { status: 204, headers: {
|
|
6271
|
+
"access-control-allow-origin": origin,
|
|
6272
|
+
"access-control-allow-methods": allowedMethods.join(", "),
|
|
6273
|
+
"access-control-allow-headers": [...allowedHeaders].join(", "),
|
|
6274
|
+
vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
|
|
6275
|
+
"cache-control": "no-store"
|
|
6276
|
+
} });
|
|
6277
|
+
}
|
|
6278
|
+
const response = await handler(request);
|
|
6279
|
+
const headers = new Headers(response.headers);
|
|
6280
|
+
headers.append("vary", "Origin");
|
|
6281
|
+
headers.delete("access-control-allow-credentials");
|
|
6282
|
+
headers.set("cache-control", "no-store");
|
|
6283
|
+
if (origin) headers.set("access-control-allow-origin", origin);
|
|
6284
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
6285
|
+
};
|
|
6286
|
+
}
|
|
6287
|
+
function emit(config, event) {
|
|
6288
|
+
try {
|
|
6289
|
+
config.onEvent?.(event);
|
|
6290
|
+
} catch {
|
|
6291
|
+
}
|
|
6292
|
+
}
|
|
5935
6293
|
var DATAFN_ROUTING_ASSERTION_HEADER = "x-datafn-routing-assertion";
|
|
5936
6294
|
var DATAFN_ROUTING_INTERNAL_HEADERS = [
|
|
5937
6295
|
DATAFN_ROUTING_ASSERTION_HEADER,
|
|
@@ -5946,9 +6304,13 @@ var DEFAULT_MIGRATION_RECOVERY_LEASE_MS = 3e4;
|
|
|
5946
6304
|
var DatafnRoutingError = class extends Error {
|
|
5947
6305
|
constructor(input) {
|
|
5948
6306
|
super(input.message);
|
|
6307
|
+
/** HTTP-shaped metadata so framework routers map an uncaught routing error to its status
|
|
6308
|
+
* (e.g. a ticket-only context provider rejection) instead of a generic 500. */
|
|
6309
|
+
this.isHttpError = true;
|
|
5949
6310
|
this.name = "DatafnRoutingError";
|
|
5950
6311
|
this.code = input.code;
|
|
5951
6312
|
this.status = input.status;
|
|
6313
|
+
this.statusCode = input.status;
|
|
5952
6314
|
this.retryable = input.retryable;
|
|
5953
6315
|
this.executionStarted = input.executionStarted ?? false;
|
|
5954
6316
|
this.placement = input.placement;
|
|
@@ -6083,6 +6445,28 @@ async function claimDatafnNamespacePlacement(input) {
|
|
|
6083
6445
|
async function validateDatafnPlacement(input) {
|
|
6084
6446
|
const startedAt = Date.now();
|
|
6085
6447
|
const requestId2 = input.request?.headers.get("x-request-id") ?? void 0;
|
|
6448
|
+
let ticket;
|
|
6449
|
+
const hasAssertion = Boolean(input.request?.headers.get(input.runtime.assertionHeader ?? DATAFN_ROUTING_ASSERTION_HEADER)?.trim());
|
|
6450
|
+
if (input.request) {
|
|
6451
|
+
const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
|
|
6452
|
+
if (token !== void 0 && (hasAssertion || !input.runtime.routeTickets)) {
|
|
6453
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6454
|
+
}
|
|
6455
|
+
if (input.runtime.routeTickets && (!hasAssertion || token !== void 0)) {
|
|
6456
|
+
if (DATAFN_ROUTING_INTERNAL_HEADERS.some((name) => input.request.headers.has(name))) {
|
|
6457
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6458
|
+
}
|
|
6459
|
+
ticket = await validateDatafnRouteTicket({
|
|
6460
|
+
request: input.request,
|
|
6461
|
+
namespace: input.namespace,
|
|
6462
|
+
regionId: input.regionId,
|
|
6463
|
+
scope: input.scope ?? "custom",
|
|
6464
|
+
runtime: input.runtime.routeTickets
|
|
6465
|
+
});
|
|
6466
|
+
}
|
|
6467
|
+
} else if (input.runtime.routeTickets && !input.trustedInternal) {
|
|
6468
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6469
|
+
}
|
|
6086
6470
|
let placement;
|
|
6087
6471
|
try {
|
|
6088
6472
|
placement = await input.runtime.directory.get(input.namespace);
|
|
@@ -6120,7 +6504,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6120
6504
|
retryable: false
|
|
6121
6505
|
});
|
|
6122
6506
|
}
|
|
6123
|
-
const assertion = await validateRoutingAssertion({
|
|
6507
|
+
const assertion = ticket ? void 0 : await validateRoutingAssertion({
|
|
6124
6508
|
namespace: input.namespace,
|
|
6125
6509
|
regionId: input.regionId,
|
|
6126
6510
|
placement,
|
|
@@ -6176,7 +6560,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6176
6560
|
internal: Boolean(assertion)
|
|
6177
6561
|
});
|
|
6178
6562
|
}
|
|
6179
|
-
if (assertion && assertion.epoch !== placement.epoch) {
|
|
6563
|
+
if (assertion && assertion.epoch !== placement.epoch || ticket && ticket.epoch !== placement.epoch) {
|
|
6180
6564
|
await emitRoutingEvent(input.runtime, {
|
|
6181
6565
|
type: "mismatch",
|
|
6182
6566
|
requestId: requestId2,
|
|
@@ -6189,14 +6573,17 @@ async function validateDatafnPlacement(input) {
|
|
|
6189
6573
|
});
|
|
6190
6574
|
throw new DatafnRoutingError({
|
|
6191
6575
|
code: "DATAFN_REGION_MISMATCH",
|
|
6192
|
-
message: "Routing
|
|
6576
|
+
message: "Routing grant uses a stale placement epoch",
|
|
6193
6577
|
status: 409,
|
|
6194
6578
|
retryable: true,
|
|
6195
6579
|
placement,
|
|
6196
|
-
internal:
|
|
6580
|
+
internal: Boolean(assertion)
|
|
6197
6581
|
});
|
|
6198
6582
|
}
|
|
6199
|
-
|
|
6583
|
+
if (ticket && ticket.expiresAt <= (input.runtime.routeTickets?.now ?? Date.now)()) {
|
|
6584
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6585
|
+
}
|
|
6586
|
+
return { placement, ...assertion ? { assertion } : {}, ...ticket ? { ticket } : {} };
|
|
6200
6587
|
}
|
|
6201
6588
|
function createDatafnGatewayRouter(config) {
|
|
6202
6589
|
const maxBodyBytes = validateDatafnRoutingBodyLimit(config.maxBodyBytes);
|
|
@@ -8627,12 +9014,16 @@ function buildReplaceRecord(schema, resourceName, existingRecord, newRecord, res
|
|
|
8627
9014
|
result[key] = existingRecord[key];
|
|
8628
9015
|
continue;
|
|
8629
9016
|
}
|
|
9017
|
+
if (field.readonly) {
|
|
9018
|
+
result[key] = Object.prototype.hasOwnProperty.call(newRecord, key) ? newRecord[key] : existingRecord[key];
|
|
9019
|
+
continue;
|
|
9020
|
+
}
|
|
8630
9021
|
if (Object.prototype.hasOwnProperty.call(newRecord, key)) {
|
|
8631
9022
|
result[key] = newRecord[key];
|
|
8632
9023
|
} else {
|
|
8633
9024
|
result[key] = field.default !== void 0 ? field.default : null;
|
|
8634
9025
|
}
|
|
8635
|
-
if (field.required && (result[key] ===
|
|
9026
|
+
if (field.required && (result[key] === void 0 || result[key] === null && !field.nullable)) {
|
|
8636
9027
|
return {
|
|
8637
9028
|
ok: false,
|
|
8638
9029
|
code: "DFQL_INVALID",
|
|
@@ -8719,8 +9110,7 @@ function isRecordEffectivelyInactive(record) {
|
|
|
8719
9110
|
return isRecordInactive(record) || record?.isAncestorInactive === true;
|
|
8720
9111
|
}
|
|
8721
9112
|
function resourceSupportsAncestorInactive(schema, resource) {
|
|
8722
|
-
|
|
8723
|
-
return resourceSchema?.fields.some((field) => field.name === "isAncestorInactive") === true;
|
|
9113
|
+
return resourceRequiresAncestorInactive(schema.relations, resource);
|
|
8724
9114
|
}
|
|
8725
9115
|
function dependentEndpoint(relation) {
|
|
8726
9116
|
return relation.type === "many-one" ? relation.from : relation.to;
|
|
@@ -10139,7 +10529,7 @@ function decideMergeMode(schema, resource, id, delta) {
|
|
|
10139
10529
|
for (const field of resourceDef.fields) {
|
|
10140
10530
|
if (!field.required) continue;
|
|
10141
10531
|
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;
|
|
10532
|
+
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && (delta[field.name] !== null || field.nullable === true);
|
|
10143
10533
|
const hasDefault = field.default !== void 0;
|
|
10144
10534
|
if (!hasProvided && !hasDefault) return { mode: "not_found" };
|
|
10145
10535
|
}
|
|
@@ -11878,6 +12268,64 @@ async function drainPermissionDirectoryOutbox(db, runtime, logger, limit = 100)
|
|
|
11878
12268
|
}
|
|
11879
12269
|
return { processed, pending: tasks.length - processed };
|
|
11880
12270
|
}
|
|
12271
|
+
async function drainNamespacePermissionDirectoryOutbox(db, namespace, runtime, logger, options = {}) {
|
|
12272
|
+
await ensurePermissionDirectoryOutbox(db);
|
|
12273
|
+
const limit = options.limit ?? 100;
|
|
12274
|
+
const maxRounds = options.maxRounds ?? 32;
|
|
12275
|
+
let processed = 0;
|
|
12276
|
+
for (let round = 0; round < maxRounds; round += 1) {
|
|
12277
|
+
const tasks = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12278
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12279
|
+
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12280
|
+
{ field: "next_attempt_at", op: "lte", value: (/* @__PURE__ */ new Date()).toISOString() }
|
|
12281
|
+
], {
|
|
12282
|
+
orderBy: "next_attempt_at",
|
|
12283
|
+
limit
|
|
12284
|
+
});
|
|
12285
|
+
if (tasks.length === 0) {
|
|
12286
|
+
break;
|
|
12287
|
+
}
|
|
12288
|
+
let roundProcessed = 0;
|
|
12289
|
+
for (const task of tasks) {
|
|
12290
|
+
const taskId = String(task.id);
|
|
12291
|
+
const selectedNextAttemptAt = String(task.next_attempt_at);
|
|
12292
|
+
const claimedNextAttemptAt = new Date(
|
|
12293
|
+
Date.now() + DRAIN_CLAIM_LEASE_MS
|
|
12294
|
+
).toISOString();
|
|
12295
|
+
const claimed = await db.internal.update(OUTBOX_TABLE, [
|
|
12296
|
+
{ field: "id", op: "eq", value: taskId },
|
|
12297
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12298
|
+
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12299
|
+
{ field: "next_attempt_at", op: "eq", value: selectedNextAttemptAt }
|
|
12300
|
+
], {
|
|
12301
|
+
next_attempt_at: claimedNextAttemptAt
|
|
12302
|
+
});
|
|
12303
|
+
if (claimed === 0) continue;
|
|
12304
|
+
if (await drainPermissionDirectorySync(
|
|
12305
|
+
db,
|
|
12306
|
+
taskId,
|
|
12307
|
+
runtime,
|
|
12308
|
+
logger,
|
|
12309
|
+
{ expectedNextAttemptAt: claimedNextAttemptAt }
|
|
12310
|
+
)) {
|
|
12311
|
+
processed += 1;
|
|
12312
|
+
roundProcessed += 1;
|
|
12313
|
+
}
|
|
12314
|
+
}
|
|
12315
|
+
if (roundProcessed === 0) {
|
|
12316
|
+
const remaining2 = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12317
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12318
|
+
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12319
|
+
], { limit: 1 });
|
|
12320
|
+
return { processed, pending: remaining2.length };
|
|
12321
|
+
}
|
|
12322
|
+
}
|
|
12323
|
+
const remaining = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12324
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12325
|
+
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12326
|
+
], { limit: 1 });
|
|
12327
|
+
return { processed, pending: remaining.length };
|
|
12328
|
+
}
|
|
11881
12329
|
|
|
11882
12330
|
// src/execution/mutation/execute.ts
|
|
11883
12331
|
var FAILED_SHARE_COMPENSATION_RESULT = /* @__PURE__ */ Symbol("failedShareCompensation");
|
|
@@ -11979,6 +12427,23 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
11979
12427
|
let additionalChanges = [];
|
|
11980
12428
|
let skipChangeTracking = false;
|
|
11981
12429
|
let failedShareCompensation = null;
|
|
12430
|
+
if ((mutation.operation === "insert" || mutation.operation === "merge" || mutation.operation === "replace") && mutation.record) {
|
|
12431
|
+
const systemField = findSystemFieldWrite(schema.relations, mutation.resource, mutation.record);
|
|
12432
|
+
if (systemField) {
|
|
12433
|
+
return {
|
|
12434
|
+
ok: false,
|
|
12435
|
+
mutationId: effectiveMutationId,
|
|
12436
|
+
affectedIds: [],
|
|
12437
|
+
errors: [{
|
|
12438
|
+
code: "DFQL_INVALID",
|
|
12439
|
+
message: `Field is read-only: ${systemField} on ${mutation.resource}`,
|
|
12440
|
+
path: `record.${systemField}`,
|
|
12441
|
+
retryable: false
|
|
12442
|
+
}],
|
|
12443
|
+
deduped: false
|
|
12444
|
+
};
|
|
12445
|
+
}
|
|
12446
|
+
}
|
|
11982
12447
|
try {
|
|
11983
12448
|
switch (mutation.operation) {
|
|
11984
12449
|
case "insert":
|
|
@@ -14438,7 +14903,7 @@ function compareCanonicalChangeEntries(a, b) {
|
|
|
14438
14903
|
function mergeCanonicalChangeStreamsBounded(streams, limit) {
|
|
14439
14904
|
const heads = streams.filter((stream) => stream.changes.length > 0).map((stream) => ({ resource: stream.resource, changes: stream.changes, index: 0 }));
|
|
14440
14905
|
const merged = [];
|
|
14441
|
-
const latestCursorPerResource =
|
|
14906
|
+
const latestCursorPerResource = /* @__PURE__ */ Object.create(null);
|
|
14442
14907
|
let peakWindow = heads.length;
|
|
14443
14908
|
while (heads.length > 0 && merged.length < limit) {
|
|
14444
14909
|
let bestIdx = 0;
|
|
@@ -14670,11 +15135,11 @@ async function executePullCanonical(request, schema, db, namespace, sequenceStor
|
|
|
14670
15135
|
const requestedResourceKeys = Object.keys(request.cursors).filter(
|
|
14671
15136
|
(resource) => resource !== ACTOR_FEED_CURSOR_KEY
|
|
14672
15137
|
);
|
|
14673
|
-
const records =
|
|
14674
|
-
const merged =
|
|
14675
|
-
const deleted =
|
|
14676
|
-
const joins =
|
|
14677
|
-
const cursors =
|
|
15138
|
+
const records = /* @__PURE__ */ Object.create(null);
|
|
15139
|
+
const merged = /* @__PURE__ */ Object.create(null);
|
|
15140
|
+
const deleted = /* @__PURE__ */ Object.create(null);
|
|
15141
|
+
const joins = /* @__PURE__ */ Object.create(null);
|
|
15142
|
+
const cursors = /* @__PURE__ */ Object.create(null);
|
|
14678
15143
|
for (const resource of requestedResourceKeys) {
|
|
14679
15144
|
if (!resource.startsWith("join_") && !resource.startsWith("__datafn_")) {
|
|
14680
15145
|
records[resource] = [];
|
|
@@ -15077,6 +15542,17 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15077
15542
|
const batchLocalSeen = /* @__PURE__ */ new Map();
|
|
15078
15543
|
async function executeMutationOp(mut, targetDb) {
|
|
15079
15544
|
const resolvedCapabilities = resolveCapabilitiesForResource2(schema, mut.resource);
|
|
15545
|
+
if ((mut.operation === "insert" || mut.operation === "merge" || mut.operation === "replace") && typeof mut.record === "object" && mut.record !== null) {
|
|
15546
|
+
const systemField = findSystemFieldWrite(schema.relations, mut.resource, mut.record);
|
|
15547
|
+
if (systemField) {
|
|
15548
|
+
return {
|
|
15549
|
+
ok: false,
|
|
15550
|
+
code: "DFQL_INVALID",
|
|
15551
|
+
message: `Field is read-only: ${systemField} on ${mut.resource}`,
|
|
15552
|
+
path: `record.${systemField}`
|
|
15553
|
+
};
|
|
15554
|
+
}
|
|
15555
|
+
}
|
|
15080
15556
|
switch (mut.operation) {
|
|
15081
15557
|
case "insert": {
|
|
15082
15558
|
const coercedData = applyDateCoercion(schema, mut.resource, mut.record || {}, logger);
|
|
@@ -17607,6 +18083,7 @@ var WebSocketManager = class {
|
|
|
17607
18083
|
* Reverse mapping: client → known principalIds for O(1) targeted index cleanup.
|
|
17608
18084
|
*/
|
|
17609
18085
|
this.clientPrincipals = /* @__PURE__ */ new Map();
|
|
18086
|
+
this.routeTickets = /* @__PURE__ */ new Map();
|
|
17610
18087
|
this.clientPlacement = /* @__PURE__ */ new Map();
|
|
17611
18088
|
/** Monotonic namespace fence generation closes the validation/registration race. */
|
|
17612
18089
|
this.namespaceFenceGeneration = /* @__PURE__ */ new Map();
|
|
@@ -17758,6 +18235,11 @@ var WebSocketManager = class {
|
|
|
17758
18235
|
*/
|
|
17759
18236
|
addClient(client, authContext) {
|
|
17760
18237
|
const { namespace } = authContext;
|
|
18238
|
+
if (this.clientNamespace.has(client)) return false;
|
|
18239
|
+
if (authContext.routeTicket && authContext.routeTicket.expiresAt <= (authContext.routeTicket.now ?? Date.now)()) {
|
|
18240
|
+
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18241
|
+
return false;
|
|
18242
|
+
}
|
|
17761
18243
|
if (this.closed) {
|
|
17762
18244
|
this.closeClientSafely(client, 1001, "Going Away", "ws-closed-admission");
|
|
17763
18245
|
return false;
|
|
@@ -17791,6 +18273,12 @@ var WebSocketManager = class {
|
|
|
17791
18273
|
...authContext.regionId ? { regionId: authContext.regionId } : {},
|
|
17792
18274
|
...Number.isSafeInteger(authContext.routingEpoch) ? { epoch: authContext.routingEpoch } : {}
|
|
17793
18275
|
});
|
|
18276
|
+
if (authContext.routeTicket) {
|
|
18277
|
+
const { id, expiresAt, now = Date.now } = authContext.routeTicket;
|
|
18278
|
+
const timer = setTimeout(() => this.expireRouteClient(client), Math.max(0, expiresAt - now()));
|
|
18279
|
+
timer.unref?.();
|
|
18280
|
+
this.routeTickets.set(client, { id, expiresAt, now, timer });
|
|
18281
|
+
}
|
|
17794
18282
|
if (this.heartbeatTimer === null) {
|
|
17795
18283
|
this.startHeartbeat();
|
|
17796
18284
|
}
|
|
@@ -17799,7 +18287,24 @@ var WebSocketManager = class {
|
|
|
17799
18287
|
/**
|
|
17800
18288
|
* Handle disconnection.
|
|
17801
18289
|
*/
|
|
18290
|
+
expireRouteClient(client) {
|
|
18291
|
+
this.removeClient(client);
|
|
18292
|
+
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18293
|
+
}
|
|
18294
|
+
revokeRouteTicket(ticketId) {
|
|
18295
|
+
let count = 0;
|
|
18296
|
+
for (const [client, ticket] of this.routeTickets) {
|
|
18297
|
+
if (ticket.id !== ticketId) continue;
|
|
18298
|
+
this.removeClient(client);
|
|
18299
|
+
this.closeClientSafely(client, 4403, "DATAFN_ROUTE_TICKET_REVOKED", "ws-ticket-revoked");
|
|
18300
|
+
count++;
|
|
18301
|
+
}
|
|
18302
|
+
return count;
|
|
18303
|
+
}
|
|
17802
18304
|
removeClient(client) {
|
|
18305
|
+
const ticket = this.routeTickets.get(client);
|
|
18306
|
+
if (ticket) clearTimeout(ticket.timer);
|
|
18307
|
+
this.routeTickets.delete(client);
|
|
17803
18308
|
const namespace = this.clientNamespace.get(client);
|
|
17804
18309
|
if (namespace !== void 0) {
|
|
17805
18310
|
this.removeTargetingIndexes(client, namespace);
|
|
@@ -17916,9 +18421,16 @@ var WebSocketManager = class {
|
|
|
17916
18421
|
principals: mode === "targeted" ? Array.from(requestedPrincipals).sort() : void 0
|
|
17917
18422
|
}
|
|
17918
18423
|
});
|
|
18424
|
+
let sent = 0;
|
|
17919
18425
|
for (const client of recipients) {
|
|
18426
|
+
const ticket = this.routeTickets.get(client);
|
|
18427
|
+
if (ticket && ticket.expiresAt <= ticket.now()) {
|
|
18428
|
+
this.expireRouteClient(client);
|
|
18429
|
+
continue;
|
|
18430
|
+
}
|
|
17920
18431
|
try {
|
|
17921
18432
|
client.send(msg);
|
|
18433
|
+
sent++;
|
|
17922
18434
|
} catch (e) {
|
|
17923
18435
|
this.logger?.error("Error sending to websocket client", { error: String(e), operation: "ws-broadcast" });
|
|
17924
18436
|
this.removeClient(client);
|
|
@@ -17928,7 +18440,7 @@ var WebSocketManager = class {
|
|
|
17928
18440
|
return {
|
|
17929
18441
|
mode,
|
|
17930
18442
|
degraded,
|
|
17931
|
-
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" :
|
|
18443
|
+
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : sent
|
|
17932
18444
|
};
|
|
17933
18445
|
}
|
|
17934
18446
|
/**
|
|
@@ -17947,6 +18459,8 @@ var WebSocketManager = class {
|
|
|
17947
18459
|
this.clientNamespace.clear();
|
|
17948
18460
|
this.clientPrincipals.clear();
|
|
17949
18461
|
this.clientPlacement.clear();
|
|
18462
|
+
for (const ticket of this.routeTickets.values()) clearTimeout(ticket.timer);
|
|
18463
|
+
this.routeTickets.clear();
|
|
17950
18464
|
this.pendingPong.clear();
|
|
17951
18465
|
}
|
|
17952
18466
|
/**
|
|
@@ -18153,35 +18667,6 @@ function relationReferencesInternalResource(relation, internalResourceNames) {
|
|
|
18153
18667
|
function normalizeResourceEndpoint(endpoint) {
|
|
18154
18668
|
return typeof endpoint === "string" ? [endpoint] : [...endpoint];
|
|
18155
18669
|
}
|
|
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
18670
|
function normalizePluginAuthorizationError(error, pluginName) {
|
|
18186
18671
|
if (error && typeof error === "object") {
|
|
18187
18672
|
const candidate = error;
|
|
@@ -18780,10 +19265,10 @@ async function createDatafnServer(config) {
|
|
|
18780
19265
|
}
|
|
18781
19266
|
return { ok: true };
|
|
18782
19267
|
};
|
|
18783
|
-
const withAuth = (action, handler) => {
|
|
19268
|
+
const withAuth = (action, handler, origin = "http", rest = false) => {
|
|
18784
19269
|
return async (req, ctx) => {
|
|
18785
19270
|
const enrichedCtx = createEnrichedContext(ctx);
|
|
18786
|
-
const placementRequest = multiRegionRuntime?.placement ? req.clone() : void 0;
|
|
19271
|
+
const placementRequest = multiRegionRuntime?.placement && origin === "http" ? req.clone() : void 0;
|
|
18787
19272
|
let payload = null;
|
|
18788
19273
|
if (shuttingDown) {
|
|
18789
19274
|
return completeDatafnResponse({
|
|
@@ -18912,7 +19397,36 @@ async function createDatafnServer(config) {
|
|
|
18912
19397
|
}
|
|
18913
19398
|
enrichedCtx.parsedBody = payload;
|
|
18914
19399
|
}
|
|
18915
|
-
|
|
19400
|
+
let structuralPayload = payload;
|
|
19401
|
+
if (rest) {
|
|
19402
|
+
let resource;
|
|
19403
|
+
try {
|
|
19404
|
+
const segment = sanitizePathSegment(new URL(req.url).pathname.split("/")[3] ?? "");
|
|
19405
|
+
if (!segment.ok) throw new Error("Invalid path segment");
|
|
19406
|
+
resource = segment.value;
|
|
19407
|
+
} catch {
|
|
19408
|
+
return completeDatafnResponse({
|
|
19409
|
+
action,
|
|
19410
|
+
request: req,
|
|
19411
|
+
context: enrichedCtx,
|
|
19412
|
+
payload,
|
|
19413
|
+
response: errorResponse({ code: "DFQL_INVALID", message: "Invalid path segment", details: { path: "resource" } }, 400)
|
|
19414
|
+
});
|
|
19415
|
+
}
|
|
19416
|
+
structuralPayload = { resource };
|
|
19417
|
+
}
|
|
19418
|
+
const parsedProtocol = parseDatafnRequest(action, structuralPayload, { schema: validatedSchema });
|
|
19419
|
+
if (!parsedProtocol.ok) {
|
|
19420
|
+
return completeDatafnResponse({
|
|
19421
|
+
action,
|
|
19422
|
+
request: req,
|
|
19423
|
+
context: enrichedCtx,
|
|
19424
|
+
payload,
|
|
19425
|
+
response: errorResponse(parsedProtocol.error, 400)
|
|
19426
|
+
});
|
|
19427
|
+
}
|
|
19428
|
+
const selection = collectStructuralResourceSelectors(parsedProtocol.result);
|
|
19429
|
+
if (selection.selectors.some((resource) => internalResourceNames.has(resource))) {
|
|
18916
19430
|
await emitDataFnEvent({
|
|
18917
19431
|
domain: "datafn",
|
|
18918
19432
|
type: "datafn.authorization.denied",
|
|
@@ -18938,7 +19452,9 @@ async function createDatafnServer(config) {
|
|
|
18938
19452
|
namespace: await extractNamespace(enrichedCtx),
|
|
18939
19453
|
regionId: multiRegionRuntime.regionId,
|
|
18940
19454
|
runtime: multiRegionRuntime.placement,
|
|
18941
|
-
request: placementRequest
|
|
19455
|
+
request: placementRequest,
|
|
19456
|
+
scope: action,
|
|
19457
|
+
trustedInternal: origin === "executor"
|
|
18942
19458
|
});
|
|
18943
19459
|
} catch (error) {
|
|
18944
19460
|
if (error instanceof DatafnRoutingError) {
|
|
@@ -18957,7 +19473,7 @@ async function createDatafnServer(config) {
|
|
|
18957
19473
|
action,
|
|
18958
19474
|
request: req,
|
|
18959
19475
|
context: enrichedCtx,
|
|
18960
|
-
payload
|
|
19476
|
+
payload: structuralPayload
|
|
18961
19477
|
});
|
|
18962
19478
|
if (!pluginAuthorization.ok) {
|
|
18963
19479
|
await emitDataFnEvent({
|
|
@@ -18977,7 +19493,7 @@ async function createDatafnServer(config) {
|
|
|
18977
19493
|
});
|
|
18978
19494
|
}
|
|
18979
19495
|
if (config.authorize) {
|
|
18980
|
-
const authorized = await config.authorize(enrichedCtx, action,
|
|
19496
|
+
const authorized = await config.authorize(enrichedCtx, action, structuralPayload);
|
|
18981
19497
|
if (!authorized) {
|
|
18982
19498
|
await emitDataFnEvent({
|
|
18983
19499
|
domain: "datafn",
|
|
@@ -19052,7 +19568,7 @@ async function createDatafnServer(config) {
|
|
|
19052
19568
|
try {
|
|
19053
19569
|
const configuredContext = config.context;
|
|
19054
19570
|
const executorContext = context ?? (typeof configuredContext === "function" ? await configuredContext(request) : configuredContext ?? {});
|
|
19055
|
-
response = await withAuth(action, handler)(request, executorContext);
|
|
19571
|
+
response = await withAuth(action, handler, "executor")(request, executorContext);
|
|
19056
19572
|
} catch {
|
|
19057
19573
|
throw new DatafnExecutorError(
|
|
19058
19574
|
{ code: "INTERNAL", message: "Internal Server Error" },
|
|
@@ -19242,7 +19758,9 @@ async function createDatafnServer(config) {
|
|
|
19242
19758
|
method: route.method,
|
|
19243
19759
|
path: route.path,
|
|
19244
19760
|
meta: route.meta,
|
|
19245
|
-
|
|
19761
|
+
// withAuth and the REST handler validate URL segments and complete DataFn response hooks.
|
|
19762
|
+
decodeParams: false,
|
|
19763
|
+
handler: withAuth(action, route.handler, "http", true)
|
|
19246
19764
|
});
|
|
19247
19765
|
}
|
|
19248
19766
|
}
|
|
@@ -19414,12 +19932,18 @@ async function createDatafnServer(config) {
|
|
|
19414
19932
|
regionId: multiRegionRuntime.regionId,
|
|
19415
19933
|
runtime: multiRegionRuntime.placement,
|
|
19416
19934
|
request: handshakeRequest,
|
|
19417
|
-
|
|
19935
|
+
scope: "websocket",
|
|
19936
|
+
trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion && !multiRegionRuntime.placement.routeTickets
|
|
19418
19937
|
});
|
|
19419
19938
|
added = wsManager.addClient(client, {
|
|
19420
19939
|
...authContext,
|
|
19421
19940
|
regionId: validated.placement.regionId,
|
|
19422
|
-
routingEpoch: validated.placement.epoch
|
|
19941
|
+
routingEpoch: validated.placement.epoch,
|
|
19942
|
+
routeTicket: validated.ticket ? {
|
|
19943
|
+
id: validated.ticket.ticketId,
|
|
19944
|
+
expiresAt: validated.ticket.expiresAt,
|
|
19945
|
+
now: multiRegionRuntime.placement.routeTickets?.now
|
|
19946
|
+
} : void 0
|
|
19423
19947
|
});
|
|
19424
19948
|
if (!added) return false;
|
|
19425
19949
|
if (wsManager.getNamespaceFenceGeneration(authContext.namespace) !== fenceGeneration) {
|
|
@@ -19438,7 +19962,7 @@ async function createDatafnServer(config) {
|
|
|
19438
19962
|
if (added) wsManager.removeClient(client);
|
|
19439
19963
|
try {
|
|
19440
19964
|
client.close(
|
|
19441
|
-
error instanceof DatafnRoutingError ? 4510 : 1011,
|
|
19965
|
+
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
19966
|
error instanceof Error ? error.message : "Placement validation failed"
|
|
19443
19967
|
);
|
|
19444
19968
|
} catch {
|
|
@@ -19446,6 +19970,7 @@ async function createDatafnServer(config) {
|
|
|
19446
19970
|
return false;
|
|
19447
19971
|
}
|
|
19448
19972
|
},
|
|
19973
|
+
revokeRouteTicket: (ticketId) => wsManager.revokeRouteTicket(ticketId),
|
|
19449
19974
|
fenceNamespace: (namespace, minimumEpoch) => wsManager.fenceNamespace(namespace, minimumEpoch),
|
|
19450
19975
|
removeClient: (client) => wsManager.removeClient(client),
|
|
19451
19976
|
handleMessage: (client, data) => wsManager.handleMessage(client, data),
|
|
@@ -20518,7 +21043,550 @@ function readDatafnCloudflarePlacementLocation(request) {
|
|
|
20518
21043
|
...continent ? { continent } : {}
|
|
20519
21044
|
};
|
|
20520
21045
|
}
|
|
21046
|
+
var POSTGRES_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
21047
|
+
var DATAFN_INTERNAL_TABLE_RE = /^__datafn_[a-z0-9_]+$/;
|
|
21048
|
+
var LEGACY_PERMISSIONS_TABLE_RE = /^__datafn_permissions_(?!global$)[A-Za-z0-9_]+$/;
|
|
21049
|
+
var POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL = `
|
|
21050
|
+
SELECT c.table_name, c.column_name
|
|
21051
|
+
FROM information_schema.columns AS c
|
|
21052
|
+
JOIN information_schema.tables AS t
|
|
21053
|
+
ON t.table_schema = c.table_schema
|
|
21054
|
+
AND t.table_name = c.table_name
|
|
21055
|
+
WHERE c.table_schema = current_schema()
|
|
21056
|
+
AND t.table_type = 'BASE TABLE'
|
|
21057
|
+
ORDER BY c.table_name, c.ordinal_position
|
|
21058
|
+
`.trim();
|
|
21059
|
+
var INTERNAL_ROLE_BY_TABLE = {
|
|
21060
|
+
__datafn_meta: "sync_meta",
|
|
21061
|
+
__datafn_changes: "sync_changes",
|
|
21062
|
+
__datafn_idempotency: "idempotency",
|
|
21063
|
+
__datafn_seed: "seed",
|
|
21064
|
+
__datafn_permission_directory_outbox: "permission_directory_outbox"
|
|
21065
|
+
};
|
|
21066
|
+
var SHARING_RESOURCE_TABLES = [
|
|
21067
|
+
{
|
|
21068
|
+
logicalRole: "permissions_global",
|
|
21069
|
+
relation: "__datafn_permissions_global"
|
|
21070
|
+
},
|
|
21071
|
+
{
|
|
21072
|
+
logicalRole: "principal_memberships",
|
|
21073
|
+
relation: "__datafn_principal_memberships"
|
|
21074
|
+
},
|
|
21075
|
+
{
|
|
21076
|
+
logicalRole: "principal_hierarchy",
|
|
21077
|
+
relation: "__datafn_principal_hierarchy"
|
|
21078
|
+
}
|
|
21079
|
+
];
|
|
21080
|
+
var SELECTOR_COLUMN = {
|
|
21081
|
+
resource_ns: "__ns",
|
|
21082
|
+
internal_namespace: "namespace"
|
|
21083
|
+
};
|
|
21084
|
+
var DatafnNamespaceStorageError = class extends Error {
|
|
21085
|
+
constructor(input) {
|
|
21086
|
+
super(input.message);
|
|
21087
|
+
this.name = "DatafnNamespaceStorageError";
|
|
21088
|
+
this.code = input.code;
|
|
21089
|
+
this.details = input.details;
|
|
21090
|
+
}
|
|
21091
|
+
};
|
|
21092
|
+
function fail(code, message, details) {
|
|
21093
|
+
throw new DatafnNamespaceStorageError({ code, message, details });
|
|
21094
|
+
}
|
|
21095
|
+
function quotePostgresIdentifier(identifier) {
|
|
21096
|
+
if (!POSTGRES_IDENTIFIER_RE.test(identifier)) {
|
|
21097
|
+
fail(
|
|
21098
|
+
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21099
|
+
`Invalid PostgreSQL identifier "${identifier}"`,
|
|
21100
|
+
{ identifier }
|
|
21101
|
+
);
|
|
21102
|
+
}
|
|
21103
|
+
return `"${identifier}"`;
|
|
21104
|
+
}
|
|
21105
|
+
function namespacedInternalTableRoles() {
|
|
21106
|
+
return INTERNAL_ROLE_BY_TABLE;
|
|
21107
|
+
}
|
|
21108
|
+
function catalogByName(catalog) {
|
|
21109
|
+
const map = /* @__PURE__ */ new Map();
|
|
21110
|
+
for (const relation of catalog.relations) {
|
|
21111
|
+
if (map.has(relation.name)) fail("DATAFN_NAMESPACE_STORAGE_INVALID", "Duplicate catalog relation.");
|
|
21112
|
+
map.set(relation.name, relation);
|
|
21113
|
+
}
|
|
21114
|
+
return map;
|
|
21115
|
+
}
|
|
21116
|
+
function hasColumn(relation, column) {
|
|
21117
|
+
return Boolean(relation?.columns.includes(column));
|
|
21118
|
+
}
|
|
21119
|
+
function isShareableResource(schema, resourceName) {
|
|
21120
|
+
const resource = schema.resources.find((entry) => entry.name === resourceName);
|
|
21121
|
+
if (!resource) return false;
|
|
21122
|
+
const resolved = resolveCapabilities(schema.capabilities, resource.capabilities);
|
|
21123
|
+
return resolved.some(
|
|
21124
|
+
(entry) => typeof entry === "object" && entry !== null && "shareable" in entry
|
|
21125
|
+
);
|
|
21126
|
+
}
|
|
21127
|
+
function schemaDerivedRelations(schema) {
|
|
21128
|
+
const planned = [];
|
|
21129
|
+
const resourceRole = namespaceStorageManifestEntry("resource");
|
|
21130
|
+
schema.resources.forEach((resource, index) => {
|
|
21131
|
+
planned.push({
|
|
21132
|
+
logicalRole: "resource",
|
|
21133
|
+
relation: resource.name,
|
|
21134
|
+
requiredColumns: [
|
|
21135
|
+
"id",
|
|
21136
|
+
...resource.fields.map((field) => field.name),
|
|
21137
|
+
...getCapabilityFields(resolveCapabilities(schema.capabilities, resource.capabilities)).map((field) => field.name)
|
|
21138
|
+
].map(physicalColumn),
|
|
21139
|
+
copyOrder: resourceRole.copyOrder + index
|
|
21140
|
+
});
|
|
21141
|
+
});
|
|
21142
|
+
const joinRole = namespaceStorageManifestEntry("join");
|
|
21143
|
+
const joinTables = /* @__PURE__ */ new Set();
|
|
21144
|
+
for (const relation of schema.relations ?? []) {
|
|
21145
|
+
if (relation.type !== "many-many") continue;
|
|
21146
|
+
const table = getRelationJoinTableName(relation);
|
|
21147
|
+
if (joinTables.has(table)) continue;
|
|
21148
|
+
joinTables.add(table);
|
|
21149
|
+
planned.push({
|
|
21150
|
+
logicalRole: "join",
|
|
21151
|
+
relation: table,
|
|
21152
|
+
requiredColumns: [
|
|
21153
|
+
"id",
|
|
21154
|
+
relation.joinColumns?.from ?? "from",
|
|
21155
|
+
relation.joinColumns?.to ?? "to",
|
|
21156
|
+
...(relation.metadata ?? []).map((field) => field.name)
|
|
21157
|
+
].map(physicalColumn),
|
|
21158
|
+
copyOrder: joinRole.copyOrder + joinTables.size
|
|
21159
|
+
});
|
|
21160
|
+
}
|
|
21161
|
+
const legacyRole = namespaceStorageManifestEntry("permissions_legacy");
|
|
21162
|
+
schema.resources.forEach((resource, index) => {
|
|
21163
|
+
if (!isShareableResource(schema, resource.name)) return;
|
|
21164
|
+
planned.push({
|
|
21165
|
+
logicalRole: "permissions_legacy",
|
|
21166
|
+
relation: getLegacyPermissionsTable(resource.name),
|
|
21167
|
+
copyOrder: legacyRole.copyOrder + index
|
|
21168
|
+
});
|
|
21169
|
+
});
|
|
21170
|
+
return planned;
|
|
21171
|
+
}
|
|
21172
|
+
function internalPlannedRelations() {
|
|
21173
|
+
return Object.entries(INTERNAL_ROLE_BY_TABLE).map(([relation, logicalRole]) => ({
|
|
21174
|
+
logicalRole,
|
|
21175
|
+
relation,
|
|
21176
|
+
copyOrder: namespaceStorageManifestEntry(logicalRole).copyOrder
|
|
21177
|
+
}));
|
|
21178
|
+
}
|
|
21179
|
+
function sharingPlannedRelations() {
|
|
21180
|
+
return SHARING_RESOURCE_TABLES.map((entry) => ({
|
|
21181
|
+
logicalRole: entry.logicalRole,
|
|
21182
|
+
relation: entry.relation,
|
|
21183
|
+
copyOrder: namespaceStorageManifestEntry(entry.logicalRole).copyOrder
|
|
21184
|
+
}));
|
|
21185
|
+
}
|
|
21186
|
+
function physicalColumn(name) {
|
|
21187
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
21188
|
+
}
|
|
21189
|
+
var SHARING_REQUIRED_COLUMNS = {
|
|
21190
|
+
permissions_global: ["id", "resource_type", "resource_ns", "resource_id", "principal_id", "level", "grant_kind", "source_ref", "granted_by", "granted_at", "revoked_at"],
|
|
21191
|
+
permissions_legacy: ["id", "resource_id", "user_id", "level", "granted_by", "granted_at", "revoked_at"],
|
|
21192
|
+
principal_memberships: ["id", "namespace", "actor_id", "principal_id", "granted_at", "revoked_at"],
|
|
21193
|
+
principal_hierarchy: ["id", "namespace", "principal_id", "parent_principal_id", "created_at", "revoked_at"]
|
|
21194
|
+
};
|
|
21195
|
+
function toEntry(planned, catalog) {
|
|
21196
|
+
const manifest = namespaceStorageManifestEntry(planned.logicalRole);
|
|
21197
|
+
const namespaceColumn = SELECTOR_COLUMN[manifest.selectorKind];
|
|
21198
|
+
const relation = catalog.get(planned.relation);
|
|
21199
|
+
if (!relation) {
|
|
21200
|
+
if (manifest.presence === "schema-derived") {
|
|
21201
|
+
fail(
|
|
21202
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21203
|
+
`Required namespace storage relation "${planned.relation}" is missing from the installed schema.`,
|
|
21204
|
+
{ logicalRole: planned.logicalRole, relation: planned.relation }
|
|
21205
|
+
);
|
|
21206
|
+
}
|
|
21207
|
+
return null;
|
|
21208
|
+
}
|
|
21209
|
+
if (!hasColumn(relation, namespaceColumn)) {
|
|
21210
|
+
fail(
|
|
21211
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21212
|
+
`Namespace storage relation "${planned.relation}" is missing selector column "${namespaceColumn}".`,
|
|
21213
|
+
{
|
|
21214
|
+
logicalRole: planned.logicalRole,
|
|
21215
|
+
relation: planned.relation,
|
|
21216
|
+
expectedColumn: namespaceColumn,
|
|
21217
|
+
columns: relation.columns
|
|
21218
|
+
}
|
|
21219
|
+
);
|
|
21220
|
+
}
|
|
21221
|
+
const required = planned.requiredColumns ?? INTERNAL_TABLE_SCHEMAS[planned.relation]?.map((column) => column.name) ?? SHARING_REQUIRED_COLUMNS[planned.logicalRole] ?? ["id"];
|
|
21222
|
+
const missing = required.filter((column) => !hasColumn(relation, column));
|
|
21223
|
+
if (missing.length > 0) {
|
|
21224
|
+
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing required columns: ${missing.join(", ")}.`);
|
|
21225
|
+
}
|
|
21226
|
+
const hasIdentity = relation.indexes?.some((index) => index.unique && index.primary === true && index.columns.includes("id") && index.columns.every((column) => column === "id" || column === namespaceColumn));
|
|
21227
|
+
if (!hasIdentity) {
|
|
21228
|
+
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing a validated unique identity index.`);
|
|
21229
|
+
}
|
|
21230
|
+
return {
|
|
21231
|
+
logicalRole: planned.logicalRole,
|
|
21232
|
+
ownership: "datafn",
|
|
21233
|
+
relation: planned.relation,
|
|
21234
|
+
quotedRelation: quotePostgresIdentifier(planned.relation),
|
|
21235
|
+
namespaceColumn,
|
|
21236
|
+
quotedNamespaceColumn: quotePostgresIdentifier(namespaceColumn),
|
|
21237
|
+
operations: manifest.operations,
|
|
21238
|
+
copyOrder: planned.copyOrder,
|
|
21239
|
+
present: true
|
|
21240
|
+
};
|
|
21241
|
+
}
|
|
21242
|
+
function isDatafnOwnedCatalogRelation(name, columns) {
|
|
21243
|
+
if (columns.includes("__ns")) return true;
|
|
21244
|
+
return DATAFN_INTERNAL_TABLE_RE.test(name) && columns.includes("namespace");
|
|
21245
|
+
}
|
|
21246
|
+
function unexpectedCatalogRelations(catalog, plannedNames) {
|
|
21247
|
+
const unexpected = [];
|
|
21248
|
+
for (const relation of catalog.relations) {
|
|
21249
|
+
if (plannedNames.has(relation.name)) continue;
|
|
21250
|
+
if (!isDatafnOwnedCatalogRelation(relation.name, relation.columns)) continue;
|
|
21251
|
+
unexpected.push(relation.name);
|
|
21252
|
+
}
|
|
21253
|
+
return unexpected.sort();
|
|
21254
|
+
}
|
|
21255
|
+
function resolveNamespaceStoragePlan(input) {
|
|
21256
|
+
try {
|
|
21257
|
+
assertSupportedNamespaceStorageVersions({
|
|
21258
|
+
manifestVersion: input.manifestVersion,
|
|
21259
|
+
schemaVersion: input.schemaVersion
|
|
21260
|
+
});
|
|
21261
|
+
} catch (error) {
|
|
21262
|
+
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "DATAFN_NAMESPACE_STORAGE_VERSION_MISMATCH";
|
|
21263
|
+
fail(
|
|
21264
|
+
code,
|
|
21265
|
+
error instanceof Error ? error.message : String(error)
|
|
21266
|
+
);
|
|
21267
|
+
}
|
|
21268
|
+
if (input.catalog.dialect !== "postgres") {
|
|
21269
|
+
fail(
|
|
21270
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21271
|
+
`Namespace storage plans are supported for PostgreSQL only. Received dialect "${String(input.catalog.dialect)}".`
|
|
21272
|
+
);
|
|
21273
|
+
}
|
|
21274
|
+
if (!isNamespaced(input.schema)) {
|
|
21275
|
+
fail(
|
|
21276
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21277
|
+
"Namespace storage plans require a namespaced DataFn schema."
|
|
21278
|
+
);
|
|
21279
|
+
}
|
|
21280
|
+
const catalog = catalogByName(input.catalog);
|
|
21281
|
+
const planned = [
|
|
21282
|
+
...schemaDerivedRelations(input.schema),
|
|
21283
|
+
...sharingPlannedRelations(),
|
|
21284
|
+
...internalPlannedRelations()
|
|
21285
|
+
];
|
|
21286
|
+
for (const relation of input.catalog.relations) {
|
|
21287
|
+
if (LEGACY_PERMISSIONS_TABLE_RE.test(relation.name) && !planned.some((entry) => entry.relation === relation.name)) {
|
|
21288
|
+
planned.push({
|
|
21289
|
+
logicalRole: "permissions_legacy",
|
|
21290
|
+
relation: relation.name,
|
|
21291
|
+
copyOrder: namespaceStorageManifestEntry("permissions_legacy").copyOrder + 50
|
|
21292
|
+
});
|
|
21293
|
+
}
|
|
21294
|
+
}
|
|
21295
|
+
const plannedNames = new Set(planned.map((entry) => entry.relation));
|
|
21296
|
+
const unexpected = unexpectedCatalogRelations(input.catalog, plannedNames);
|
|
21297
|
+
if (unexpected.length > 0) {
|
|
21298
|
+
fail(
|
|
21299
|
+
"DATAFN_NAMESPACE_STORAGE_UNKNOWN",
|
|
21300
|
+
`Installed schema contains DataFn namespace storage that is not in the supported contract: ${unexpected.join(", ")}.`,
|
|
21301
|
+
{ relations: unexpected }
|
|
21302
|
+
);
|
|
21303
|
+
}
|
|
21304
|
+
const entries = planned.map((entry) => toEntry(entry, catalog)).filter((entry) => entry !== null).sort((left, right) => {
|
|
21305
|
+
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21306
|
+
return left.relation.localeCompare(right.relation);
|
|
21307
|
+
});
|
|
21308
|
+
return {
|
|
21309
|
+
manifestVersion: DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION,
|
|
21310
|
+
schemaVersion: DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION,
|
|
21311
|
+
dialect: "postgres",
|
|
21312
|
+
entries
|
|
21313
|
+
};
|
|
21314
|
+
}
|
|
21315
|
+
function selectNamespaceStorageEntries(plan, operation) {
|
|
21316
|
+
const selected = plan.entries.filter((entry) => entry.operations.includes(operation));
|
|
21317
|
+
if (operation === "cleanup") {
|
|
21318
|
+
return [...selected].sort((left, right) => {
|
|
21319
|
+
if (left.copyOrder !== right.copyOrder) return right.copyOrder - left.copyOrder;
|
|
21320
|
+
return right.relation.localeCompare(left.relation);
|
|
21321
|
+
});
|
|
21322
|
+
}
|
|
21323
|
+
return selected;
|
|
21324
|
+
}
|
|
21325
|
+
function composeNamespaceStoragePlan(plan, applicationEntries) {
|
|
21326
|
+
const existing = new Set(plan.entries.map((entry) => entry.relation));
|
|
21327
|
+
const composed = [...plan.entries];
|
|
21328
|
+
for (const entry of applicationEntries) {
|
|
21329
|
+
if (existing.has(entry.relation)) {
|
|
21330
|
+
fail(
|
|
21331
|
+
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21332
|
+
`Application storage relation "${entry.relation}" collides with DataFn-owned storage.`,
|
|
21333
|
+
{ relation: entry.relation }
|
|
21334
|
+
);
|
|
21335
|
+
}
|
|
21336
|
+
existing.add(entry.relation);
|
|
21337
|
+
composed.push({
|
|
21338
|
+
logicalRole: "application",
|
|
21339
|
+
ownership: "application",
|
|
21340
|
+
relation: entry.relation,
|
|
21341
|
+
quotedRelation: quotePostgresIdentifier(entry.relation),
|
|
21342
|
+
namespaceColumn: entry.namespaceColumn,
|
|
21343
|
+
quotedNamespaceColumn: quotePostgresIdentifier(entry.namespaceColumn),
|
|
21344
|
+
operations: entry.operations ?? ["copy", "verify", "cleanup", "fence", "backup"],
|
|
21345
|
+
copyOrder: entry.copyOrder ?? 10,
|
|
21346
|
+
present: true
|
|
21347
|
+
});
|
|
21348
|
+
}
|
|
21349
|
+
composed.sort((left, right) => {
|
|
21350
|
+
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21351
|
+
return left.relation.localeCompare(right.relation);
|
|
21352
|
+
});
|
|
21353
|
+
return { ...plan, entries: composed };
|
|
21354
|
+
}
|
|
21355
|
+
var POSTGRES_NAMESPACE_STORAGE_INDEX_SQL = `
|
|
21356
|
+
SELECT t.relname AS table_name,
|
|
21357
|
+
array_agg(a.attname ORDER BY k.ordinality) AS index_columns,
|
|
21358
|
+
i.indisunique AS index_unique, i.indisprimary AS index_primary
|
|
21359
|
+
FROM pg_index i
|
|
21360
|
+
JOIN pg_class t ON t.oid = i.indrelid
|
|
21361
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
21362
|
+
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ordinality)
|
|
21363
|
+
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
|
21364
|
+
WHERE n.nspname = current_schema() AND i.indisvalid AND i.indisready
|
|
21365
|
+
AND i.indpred IS NULL AND i.indexprs IS NULL
|
|
21366
|
+
AND k.ordinality <= i.indnkeyatts
|
|
21367
|
+
GROUP BY t.relname, i.indexrelid, i.indisunique, i.indisprimary
|
|
21368
|
+
ORDER BY t.relname, i.indexrelid
|
|
21369
|
+
`.trim();
|
|
21370
|
+
async function inspectPostgresNamespaceStorage(query) {
|
|
21371
|
+
const rows = await query(POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL);
|
|
21372
|
+
const indexRows = await query(POSTGRES_NAMESPACE_STORAGE_INDEX_SQL);
|
|
21373
|
+
const relations = /* @__PURE__ */ new Map();
|
|
21374
|
+
for (const row of rows) {
|
|
21375
|
+
if (typeof row.column_name !== "string") fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", "Catalog column metadata is missing.");
|
|
21376
|
+
const columns = relations.get(row.table_name) ?? [];
|
|
21377
|
+
columns.push(row.column_name);
|
|
21378
|
+
relations.set(row.table_name, columns);
|
|
21379
|
+
}
|
|
21380
|
+
return {
|
|
21381
|
+
dialect: "postgres",
|
|
21382
|
+
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 })) }))
|
|
21383
|
+
};
|
|
21384
|
+
}
|
|
21385
|
+
function assertInternalNamespaceStorageMetadataComplete() {
|
|
21386
|
+
const namespaced = listNamespacedInternalTables();
|
|
21387
|
+
const mapped = Object.keys(INTERNAL_ROLE_BY_TABLE).sort();
|
|
21388
|
+
if (namespaced.join("\0") !== mapped.join("\0")) {
|
|
21389
|
+
fail(
|
|
21390
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21391
|
+
"Internal namespace tables are missing from the storage contract.",
|
|
21392
|
+
{
|
|
21393
|
+
namespacedInternalTables: namespaced,
|
|
21394
|
+
mappedInternalTables: mapped,
|
|
21395
|
+
schemas: Object.keys(INTERNAL_TABLE_SCHEMAS).sort()
|
|
21396
|
+
}
|
|
21397
|
+
);
|
|
21398
|
+
}
|
|
21399
|
+
}
|
|
21400
|
+
async function drainNamespaceStorage(input) {
|
|
21401
|
+
const drainEntries = selectNamespaceStorageEntries(input.plan, "drain");
|
|
21402
|
+
if (drainEntries.length === 0) {
|
|
21403
|
+
return { processed: 0, pending: 0 };
|
|
21404
|
+
}
|
|
21405
|
+
const unsupported = drainEntries.filter(
|
|
21406
|
+
(entry) => entry.logicalRole !== "permission_directory_outbox"
|
|
21407
|
+
);
|
|
21408
|
+
if (unsupported.length > 0) {
|
|
21409
|
+
fail(
|
|
21410
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21411
|
+
`No drain implementation for logical roles: ${unsupported.map((entry) => entry.logicalRole).join(", ")}.`,
|
|
21412
|
+
{ roles: unsupported.map((entry) => entry.logicalRole) }
|
|
21413
|
+
);
|
|
21414
|
+
}
|
|
21415
|
+
return drainNamespacePermissionDirectoryOutbox(
|
|
21416
|
+
input.adapter,
|
|
21417
|
+
input.namespace,
|
|
21418
|
+
input.runtime,
|
|
21419
|
+
input.logger
|
|
21420
|
+
);
|
|
21421
|
+
}
|
|
21422
|
+
async function resolveAuthoritativeAncestorInactive(adapter, schema, resource, record, namespace, maxGraphNodes) {
|
|
21423
|
+
const active = /* @__PURE__ */ new Set();
|
|
21424
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
21425
|
+
let visited = 0;
|
|
21426
|
+
async function visit(model, row, root = false) {
|
|
21427
|
+
if (typeof row.id !== "string" || row.id.length === 0) {
|
|
21428
|
+
throw new Error("Ancestor repair: malformed record identifier");
|
|
21429
|
+
}
|
|
21430
|
+
const key = JSON.stringify([model, row.id]);
|
|
21431
|
+
if (active.has(key)) throw new Error("Ancestor repair: cycle detected");
|
|
21432
|
+
if (resolved.has(key)) return resolved.get(key);
|
|
21433
|
+
if (++visited > maxGraphNodes) throw new Error("Ancestor repair: maxGraphNodes exceeded");
|
|
21434
|
+
active.add(key);
|
|
21435
|
+
let inherited = false;
|
|
21436
|
+
for (const relation of schema.relations ?? []) {
|
|
21437
|
+
if (relation.inheritsInactive !== true || relation.type === "many-many") continue;
|
|
21438
|
+
const dependent = relation.type === "many-one" ? relation.from : relation.to;
|
|
21439
|
+
if (!endpointIncludes(dependent, model)) continue;
|
|
21440
|
+
const endpoint = relation.type === "many-one" ? relation.to : relation.from;
|
|
21441
|
+
const field = relation.type === "many-one" ? relationFkFieldForManyOne(relation) : relation.type === "htree" ? relation.fkField || relation.foreignKey || relation.inverse || "parentId" : relationFkFieldForOneMany(relation);
|
|
21442
|
+
const parentId = row[field];
|
|
21443
|
+
if (parentId === null || parentId === void 0 || parentId === "") continue;
|
|
21444
|
+
if (typeof parentId !== "string") throw new Error("Ancestor repair: malformed parent link");
|
|
21445
|
+
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`);
|
|
21446
|
+
const discriminator = row[resourceField];
|
|
21447
|
+
const inferred = resolveEndpointResource(endpoint, parentId, schema);
|
|
21448
|
+
let parentResource = inferred;
|
|
21449
|
+
if (endpointList(endpoint).length > 1 && discriminator != null) {
|
|
21450
|
+
if (typeof discriminator !== "string" || !endpointIncludes(endpoint, discriminator) || inferred && inferred !== discriminator) {
|
|
21451
|
+
throw new Error("Ancestor repair: invalid parent resource discriminator");
|
|
21452
|
+
}
|
|
21453
|
+
parentResource = discriminator;
|
|
21454
|
+
}
|
|
21455
|
+
if (!parentResource) throw new Error("Ancestor repair: ambiguous parent resource");
|
|
21456
|
+
const parent = await adapter.findOne({
|
|
21457
|
+
model: parentResource,
|
|
21458
|
+
where: [{ field: "id", operator: "eq", value: parentId }],
|
|
21459
|
+
namespace
|
|
21460
|
+
});
|
|
21461
|
+
if (!parent) throw new Error("Ancestor repair: missing parent record");
|
|
21462
|
+
const parentInactive = await visit(parentResource, parent);
|
|
21463
|
+
inherited = inherited || parentInactive;
|
|
21464
|
+
}
|
|
21465
|
+
active.delete(key);
|
|
21466
|
+
const effective = inherited || row.isArchived === true || row.trashedAt != null;
|
|
21467
|
+
resolved.set(key, effective);
|
|
21468
|
+
return root ? inherited : effective;
|
|
21469
|
+
}
|
|
21470
|
+
return visit(resource, record, true);
|
|
21471
|
+
}
|
|
21472
|
+
|
|
21473
|
+
// src/execution/migration/ancestor-inactive.ts
|
|
21474
|
+
var DEFAULT_BATCH_SIZE = 500;
|
|
21475
|
+
function compareCodePoints(a, b) {
|
|
21476
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
21477
|
+
}
|
|
21478
|
+
function ancestorInactiveResources(schema) {
|
|
21479
|
+
return [...getAncestorInactiveResources(schema.relations)].sort(compareCodePoints);
|
|
21480
|
+
}
|
|
21481
|
+
async function recomputeAncestorInactive(adapter, schema, options) {
|
|
21482
|
+
const { namespace, dryRun = false, logger } = options;
|
|
21483
|
+
const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
21484
|
+
const maxGraphNodes = options.maxGraphNodes ?? 1e4;
|
|
21485
|
+
for (const value of [batchSize, maxGraphNodes]) {
|
|
21486
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new Error("Ancestor repair: limits must be positive safe integers");
|
|
21487
|
+
}
|
|
21488
|
+
const resources = ancestorInactiveResources(schema);
|
|
21489
|
+
if (resources.length === 0) return { scanned: 0, updated: 0, skipped: 0, nextCursor: null };
|
|
21490
|
+
let cursor = options.cursor ?? { resource: resources[0], afterId: null };
|
|
21491
|
+
let resourceIndex = resources.indexOf(cursor.resource);
|
|
21492
|
+
if (resourceIndex === -1) {
|
|
21493
|
+
throw new Error(`recomputeAncestorInactive: unknown cursor resource "${cursor.resource}"`);
|
|
21494
|
+
}
|
|
21495
|
+
let scanned = 0;
|
|
21496
|
+
let updated = 0;
|
|
21497
|
+
let skipped = 0;
|
|
21498
|
+
let remaining = batchSize;
|
|
21499
|
+
while (remaining > 0) {
|
|
21500
|
+
const resource = resources[resourceIndex];
|
|
21501
|
+
const requested = remaining;
|
|
21502
|
+
const rows = await adapter.findMany({
|
|
21503
|
+
model: resource,
|
|
21504
|
+
where: cursor.afterId === null ? [] : [{ field: "id", operator: "gt", value: cursor.afterId }],
|
|
21505
|
+
orderBy: [{ field: "id", direction: "asc" }],
|
|
21506
|
+
limit: requested,
|
|
21507
|
+
namespace
|
|
21508
|
+
});
|
|
21509
|
+
for (const row of rows) {
|
|
21510
|
+
const id = row.id;
|
|
21511
|
+
if (typeof id !== "string" || id.length === 0) throw new Error("Ancestor repair: malformed record identifier");
|
|
21512
|
+
scanned += 1;
|
|
21513
|
+
const next = await resolveAuthoritativeAncestorInactive(adapter, schema, resource, row, namespace, maxGraphNodes);
|
|
21514
|
+
const previous = row[ANCESTOR_INACTIVE_FIELD];
|
|
21515
|
+
if (previous !== next) {
|
|
21516
|
+
if (dryRun) {
|
|
21517
|
+
updated += 1;
|
|
21518
|
+
} else {
|
|
21519
|
+
const affected = await adapter.updateMany({
|
|
21520
|
+
model: resource,
|
|
21521
|
+
where: [
|
|
21522
|
+
{ field: "id", operator: "eq", value: id },
|
|
21523
|
+
{
|
|
21524
|
+
field: ANCESTOR_INACTIVE_FIELD,
|
|
21525
|
+
operator: "eq",
|
|
21526
|
+
value: previous ?? null
|
|
21527
|
+
}
|
|
21528
|
+
],
|
|
21529
|
+
data: { [ANCESTOR_INACTIVE_FIELD]: next },
|
|
21530
|
+
namespace
|
|
21531
|
+
});
|
|
21532
|
+
updated += affected > 0 ? 1 : 0;
|
|
21533
|
+
skipped += affected > 0 ? 0 : 1;
|
|
21534
|
+
}
|
|
21535
|
+
}
|
|
21536
|
+
cursor = { resource, afterId: id };
|
|
21537
|
+
}
|
|
21538
|
+
remaining -= rows.length;
|
|
21539
|
+
if (rows.length < requested) {
|
|
21540
|
+
resourceIndex += 1;
|
|
21541
|
+
if (resourceIndex >= resources.length) {
|
|
21542
|
+
logger?.info("datafn.ancestorInactive.recompute.complete", { namespace, scanned, updated, skipped, dryRun });
|
|
21543
|
+
return { scanned, updated, skipped, nextCursor: null };
|
|
21544
|
+
}
|
|
21545
|
+
cursor = { resource: resources[resourceIndex], afterId: null };
|
|
21546
|
+
}
|
|
21547
|
+
}
|
|
21548
|
+
return { scanned, updated, skipped, nextCursor: cursor };
|
|
21549
|
+
}
|
|
21550
|
+
async function recomputeAncestorInactiveAll(adapter, schema, options) {
|
|
21551
|
+
const maxSweeps = options.maxSweeps ?? 32;
|
|
21552
|
+
if (!Number.isSafeInteger(maxSweeps) || maxSweeps < 1) throw new Error("Ancestor repair: maxSweeps must be a positive safe integer");
|
|
21553
|
+
let scanned = 0;
|
|
21554
|
+
let updated = 0;
|
|
21555
|
+
let skipped = 0;
|
|
21556
|
+
let sweeps = 0;
|
|
21557
|
+
let converged = false;
|
|
21558
|
+
while (sweeps < maxSweeps) {
|
|
21559
|
+
sweeps += 1;
|
|
21560
|
+
let sweepUpdated = 0;
|
|
21561
|
+
let sweepSkipped = 0;
|
|
21562
|
+
let cursor = null;
|
|
21563
|
+
do {
|
|
21564
|
+
const result = await recomputeAncestorInactive(adapter, schema, { ...options, cursor });
|
|
21565
|
+
scanned += result.scanned;
|
|
21566
|
+
sweepUpdated += result.updated;
|
|
21567
|
+
sweepSkipped += result.skipped;
|
|
21568
|
+
cursor = result.nextCursor;
|
|
21569
|
+
} while (cursor !== null);
|
|
21570
|
+
updated += sweepUpdated;
|
|
21571
|
+
skipped += sweepSkipped;
|
|
21572
|
+
if (sweepUpdated === 0 && sweepSkipped === 0) {
|
|
21573
|
+
converged = true;
|
|
21574
|
+
break;
|
|
21575
|
+
}
|
|
21576
|
+
if (options.dryRun) break;
|
|
21577
|
+
}
|
|
21578
|
+
if (!converged) {
|
|
21579
|
+
options.logger?.warn("datafn.ancestorInactive.recompute.notConverged", {
|
|
21580
|
+
namespace: options.namespace,
|
|
21581
|
+
sweeps,
|
|
21582
|
+
updated,
|
|
21583
|
+
skipped,
|
|
21584
|
+
dryRun: options.dryRun === true
|
|
21585
|
+
});
|
|
21586
|
+
}
|
|
21587
|
+
return { scanned, updated, skipped, sweeps, converged };
|
|
21588
|
+
}
|
|
20521
21589
|
|
|
20522
|
-
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, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, readDatafnPublicLinkToken, resolveDatafnPublicLink, selectDatafnPlacementRegion, validateDatafnPlacement, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRoutingAssertion };
|
|
21590
|
+
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, createDatafnEd25519RouteTicketSigner, createDatafnEd25519RouteTicketVerifier, 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, verifyDatafnRegionalTicketIdentity, withDatafnPublicLinksSchema, withDatafnRegionalCors, withDatafnRoutingAssertion };
|
|
20523
21591
|
//# sourceMappingURL=index.js.map
|
|
20524
21592
|
//# sourceMappingURL=index.js.map
|