@datafn/server 0.1.1 → 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 +20 -0
- package/dist/index.cjs +1113 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -5
- package/dist/index.d.ts +268 -5
- package/dist/index.js +1064 -79
- package/dist/index.js.map +1 -1
- package/package.json +8 -5
package/dist/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ var db = require('@superfunctions/db');
|
|
|
6
6
|
var observability = require('@superfunctions/observability');
|
|
7
7
|
var crypto$1 = require('crypto');
|
|
8
8
|
|
|
9
|
-
// src/
|
|
9
|
+
// src/index.ts
|
|
10
10
|
|
|
11
11
|
// src/execution/internal-tables.ts
|
|
12
12
|
var META_COLUMNS = [
|
|
@@ -56,6 +56,9 @@ var INTERNAL_TABLE_SCHEMAS = {
|
|
|
56
56
|
__datafn_seed: SEED_COLUMNS,
|
|
57
57
|
__datafn_permission_directory_outbox: PERMISSION_DIRECTORY_OUTBOX_COLUMNS
|
|
58
58
|
};
|
|
59
|
+
function listNamespacedInternalTables() {
|
|
60
|
+
return Object.entries(INTERNAL_TABLE_SCHEMAS).filter(([, columns]) => columns.some((column) => column.name === "namespace")).map(([name]) => name).sort();
|
|
61
|
+
}
|
|
59
62
|
async function ensureInternalTable(adapter, tableName) {
|
|
60
63
|
const columns = INTERNAL_TABLE_SCHEMAS[tableName];
|
|
61
64
|
if (!columns) {
|
|
@@ -472,8 +475,8 @@ function createSequenceStore(config) {
|
|
|
472
475
|
// src/http/json.ts
|
|
473
476
|
async function parseJsonBody(req) {
|
|
474
477
|
try {
|
|
475
|
-
const
|
|
476
|
-
if (!
|
|
478
|
+
const text2 = await req.text();
|
|
479
|
+
if (!text2 || text2.trim() === "") {
|
|
477
480
|
return {
|
|
478
481
|
ok: false,
|
|
479
482
|
error: {
|
|
@@ -483,7 +486,7 @@ async function parseJsonBody(req) {
|
|
|
483
486
|
}
|
|
484
487
|
};
|
|
485
488
|
}
|
|
486
|
-
const data = JSON.parse(
|
|
489
|
+
const data = JSON.parse(text2);
|
|
487
490
|
return { ok: true, data };
|
|
488
491
|
} catch (error) {
|
|
489
492
|
return {
|
|
@@ -733,6 +736,13 @@ function validateRecordKeys(index, resourceName, record, basePath, mode = "write
|
|
|
733
736
|
for (const key of Object.keys(record)) {
|
|
734
737
|
if (key === "id") continue;
|
|
735
738
|
if (!fields.has(key)) {
|
|
739
|
+
if (mode === "write" && index.fieldsByResource.get(resourceName)?.has(key)) {
|
|
740
|
+
return vErr(
|
|
741
|
+
"DFQL_INVALID",
|
|
742
|
+
`Field is read-only: ${key} on ${resourceName}`,
|
|
743
|
+
`${basePath}.${key}`
|
|
744
|
+
);
|
|
745
|
+
}
|
|
736
746
|
return vErr(
|
|
737
747
|
"DFQL_UNKNOWN_FIELD",
|
|
738
748
|
`Unknown field: ${key} on ${resourceName}`,
|
|
@@ -1021,13 +1031,32 @@ function validateMutation(mutation, index, basePath = "$", opts) {
|
|
|
1021
1031
|
`${basePath}.record.${fieldName}`
|
|
1022
1032
|
);
|
|
1023
1033
|
}
|
|
1034
|
+
if (fieldDef.type === "date" && (fieldDef.min !== void 0 || fieldDef.max !== void 0)) {
|
|
1035
|
+
const epoch = core.toBoundsEpochMs(value);
|
|
1036
|
+
if (Number.isFinite(epoch)) {
|
|
1037
|
+
if (fieldDef.min !== void 0 && epoch < fieldDef.min) {
|
|
1038
|
+
return vErr(
|
|
1039
|
+
"DFQL_INVALID",
|
|
1040
|
+
`Field '${fieldName}' must be on or after ${core.formatBoundEpochMs(fieldDef.min)}`,
|
|
1041
|
+
`${basePath}.record.${fieldName}`
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
if (fieldDef.max !== void 0 && epoch > fieldDef.max) {
|
|
1045
|
+
return vErr(
|
|
1046
|
+
"DFQL_INVALID",
|
|
1047
|
+
`Field '${fieldName}' must be on or before ${core.formatBoundEpochMs(fieldDef.max)}`,
|
|
1048
|
+
`${basePath}.record.${fieldName}`
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1024
1053
|
}
|
|
1025
1054
|
if (m.operation === "insert" || m.operation === "replace") {
|
|
1026
1055
|
const resource2 = resourceResult.result;
|
|
1027
1056
|
const record2 = m.record;
|
|
1028
1057
|
for (const field of resource2.fields) {
|
|
1029
1058
|
if (field.required && field.default === void 0 && field.name !== "id" && !field.readonly) {
|
|
1030
|
-
if (!(field.name in record2) || record2[field.name] ===
|
|
1059
|
+
if (!(field.name in record2) || record2[field.name] === void 0 || record2[field.name] === null && !field.nullable) {
|
|
1031
1060
|
return vErr(
|
|
1032
1061
|
"DFQL_INVALID",
|
|
1033
1062
|
`Required field missing: ${field.name}`,
|
|
@@ -3275,6 +3304,22 @@ function filterAncestorInactiveRecords(records, match, metadata) {
|
|
|
3275
3304
|
}
|
|
3276
3305
|
return records.filter((record) => record.isAncestorInactive !== true);
|
|
3277
3306
|
}
|
|
3307
|
+
function isPlainRecord(value) {
|
|
3308
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
3309
|
+
return false;
|
|
3310
|
+
}
|
|
3311
|
+
const prototype = Object.getPrototypeOf(value);
|
|
3312
|
+
return prototype === Object.prototype || prototype === null;
|
|
3313
|
+
}
|
|
3314
|
+
function applyOmitToArrayValue(value, omit) {
|
|
3315
|
+
if (Array.isArray(value)) {
|
|
3316
|
+
return value.map((item) => applyOmitToArrayValue(item, omit));
|
|
3317
|
+
}
|
|
3318
|
+
if (isPlainRecord(value)) {
|
|
3319
|
+
return applyOmit(value, omit);
|
|
3320
|
+
}
|
|
3321
|
+
return value;
|
|
3322
|
+
}
|
|
3278
3323
|
function applyOmit(record, omit) {
|
|
3279
3324
|
if (!omit || omit.length === 0) {
|
|
3280
3325
|
return record;
|
|
@@ -3283,13 +3328,8 @@ function applyOmit(record, omit) {
|
|
|
3283
3328
|
for (const [key, value] of Object.entries(record)) {
|
|
3284
3329
|
if (key === "id" || !omit.includes(key)) {
|
|
3285
3330
|
if (Array.isArray(value)) {
|
|
3286
|
-
result[key] = value.map((item) =>
|
|
3287
|
-
|
|
3288
|
-
return applyOmit(item, omit);
|
|
3289
|
-
}
|
|
3290
|
-
return item;
|
|
3291
|
-
});
|
|
3292
|
-
} else if (typeof value === "object" && value !== null && key !== "$relation_metadata") {
|
|
3331
|
+
result[key] = value.map((item) => applyOmitToArrayValue(item, omit));
|
|
3332
|
+
} else if (isPlainRecord(value) && key !== "$relation_metadata") {
|
|
3293
3333
|
result[key] = applyOmit(value, omit);
|
|
3294
3334
|
} else {
|
|
3295
3335
|
result[key] = value;
|
|
@@ -3390,6 +3430,7 @@ function getFkFieldsToOmit(schema, targetResource) {
|
|
|
3390
3430
|
function materializeSelect(record, resource, select, schema, store, omit, metadata) {
|
|
3391
3431
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
3392
3432
|
if (!resourceSchema) return { id: record.id };
|
|
3433
|
+
record = core.stripNullsForNonNullableFields(record, resourceSchema);
|
|
3393
3434
|
const fkFieldsToOmit = getFkFieldsToOmit(schema, resource);
|
|
3394
3435
|
const effectiveOmit = omit ? [...omit, ...Array.from(fkFieldsToOmit)] : Array.from(fkFieldsToOmit);
|
|
3395
3436
|
if (!select || select.length === 0) {
|
|
@@ -3498,7 +3539,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3498
3539
|
),
|
|
3499
3540
|
htreeRelation,
|
|
3500
3541
|
metadata
|
|
3501
|
-
).map(
|
|
3542
|
+
).map(
|
|
3543
|
+
(c) => applyOmit(core.stripNullsForNonNullableFields(c, resourceSchema), omit)
|
|
3544
|
+
);
|
|
3502
3545
|
} else if (parsed.directive === "**") {
|
|
3503
3546
|
result[parsed.baseName] = filterAncestorInactiveRecords(
|
|
3504
3547
|
htreeChildren(
|
|
@@ -3510,7 +3553,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3510
3553
|
),
|
|
3511
3554
|
htreeRelation,
|
|
3512
3555
|
metadata
|
|
3513
|
-
).map(
|
|
3556
|
+
).map(
|
|
3557
|
+
(d) => applyOmit(core.stripNullsForNonNullableFields(d, resourceSchema), omit)
|
|
3558
|
+
);
|
|
3514
3559
|
}
|
|
3515
3560
|
includedFields.add(parsed.baseName);
|
|
3516
3561
|
} else if (parsed.baseName === "parent") {
|
|
@@ -3519,7 +3564,9 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3519
3564
|
resource,
|
|
3520
3565
|
htreeRelation.relation,
|
|
3521
3566
|
store
|
|
3522
|
-
).map(
|
|
3567
|
+
).map(
|
|
3568
|
+
(a) => applyOmit(core.stripNullsForNonNullableFields(a, resourceSchema), omit)
|
|
3569
|
+
);
|
|
3523
3570
|
includedFields.add(parsed.baseName);
|
|
3524
3571
|
}
|
|
3525
3572
|
continue;
|
|
@@ -3815,8 +3862,10 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3815
3862
|
}
|
|
3816
3863
|
const aggregations = query.aggregations || {};
|
|
3817
3864
|
let results = [];
|
|
3865
|
+
const samples = /* @__PURE__ */ new WeakMap();
|
|
3818
3866
|
for (const [, groupRecords] of groups.entries()) {
|
|
3819
3867
|
const row = {};
|
|
3868
|
+
samples.set(row, groupRecords[0]);
|
|
3820
3869
|
groupBy.forEach((field) => {
|
|
3821
3870
|
const sample = groupRecords[0];
|
|
3822
3871
|
const val = resolveValue(
|
|
@@ -3827,7 +3876,9 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3827
3876
|
query.resource,
|
|
3828
3877
|
resourceSchema
|
|
3829
3878
|
);
|
|
3830
|
-
|
|
3879
|
+
if (val !== void 0) {
|
|
3880
|
+
row[field] = val;
|
|
3881
|
+
}
|
|
3831
3882
|
});
|
|
3832
3883
|
for (const group of temporalGroups) {
|
|
3833
3884
|
row[group.alias] = core.resolveTemporalBucketValue(groupRecords[0], group);
|
|
@@ -3867,7 +3918,24 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3867
3918
|
paginated.length = limit;
|
|
3868
3919
|
}
|
|
3869
3920
|
return {
|
|
3870
|
-
groups: paginated
|
|
3921
|
+
groups: paginated.map((row) => {
|
|
3922
|
+
const output = { ...row };
|
|
3923
|
+
for (const field of groupBy) {
|
|
3924
|
+
if (Object.prototype.hasOwnProperty.call(aggregations, field)) continue;
|
|
3925
|
+
if (output[field] === null && resolveValue(
|
|
3926
|
+
samples.get(row),
|
|
3927
|
+
field,
|
|
3928
|
+
schema,
|
|
3929
|
+
store,
|
|
3930
|
+
query.resource,
|
|
3931
|
+
resourceSchema,
|
|
3932
|
+
true
|
|
3933
|
+
) === void 0) {
|
|
3934
|
+
delete output[field];
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
return output;
|
|
3938
|
+
}),
|
|
3871
3939
|
nextCursor
|
|
3872
3940
|
};
|
|
3873
3941
|
}
|
|
@@ -3896,9 +3964,10 @@ function orderGroupedResults(groups, sortTerms) {
|
|
|
3896
3964
|
return 0;
|
|
3897
3965
|
});
|
|
3898
3966
|
}
|
|
3899
|
-
function resolveValue(record, path, schema, store, resourceName, precomputedResource) {
|
|
3967
|
+
function resolveValue(record, path, schema, store, resourceName, precomputedResource, normalize = false) {
|
|
3900
3968
|
if (!path.includes(".")) {
|
|
3901
|
-
|
|
3969
|
+
const definition2 = precomputedResource?.fields.find((field) => field.name === path);
|
|
3970
|
+
return normalize && record[path] === null && definition2 && definition2.nullable !== true ? void 0 : record[path];
|
|
3902
3971
|
}
|
|
3903
3972
|
const parts = path.split(".");
|
|
3904
3973
|
const resource = precomputedResource ?? schema.resources.find((r) => r.name === resourceName);
|
|
@@ -3936,7 +4005,8 @@ function resolveValue(record, path, schema, store, resourceName, precomputedReso
|
|
|
3936
4005
|
}
|
|
3937
4006
|
}
|
|
3938
4007
|
const lastField = parts[parts.length - 1];
|
|
3939
|
-
|
|
4008
|
+
const definition = schema.resources.find((entry) => entry.name === currentResource)?.fields.find((field) => field.name === lastField);
|
|
4009
|
+
return normalize && currentRecord[lastField] === null && definition && definition.nullable !== true ? void 0 : currentRecord[lastField];
|
|
3940
4010
|
}
|
|
3941
4011
|
var DFQL_TO_ADAPTER_OP = {
|
|
3942
4012
|
// $-prefixed DFQL operators
|
|
@@ -5166,6 +5236,7 @@ async function pushdownCore(query, schema, db, namespace) {
|
|
|
5166
5236
|
function projectFieldsPushdown(record, resource, schema, select, omit) {
|
|
5167
5237
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
5168
5238
|
if (!resourceSchema) return { id: record.id };
|
|
5239
|
+
record = core.stripNullsForNonNullableFields(record, resourceSchema);
|
|
5169
5240
|
const resolvedCapabilities = core.resolveCapabilities(
|
|
5170
5241
|
schema.capabilities,
|
|
5171
5242
|
resourceSchema.capabilities
|
|
@@ -5934,6 +6005,213 @@ var AuthResolverCache = class {
|
|
|
5934
6005
|
function createAuthResolverCache() {
|
|
5935
6006
|
return new AuthResolverCache();
|
|
5936
6007
|
}
|
|
6008
|
+
var scopes = /* @__PURE__ */ new Set([
|
|
6009
|
+
"query",
|
|
6010
|
+
"search",
|
|
6011
|
+
"mutation",
|
|
6012
|
+
"transact",
|
|
6013
|
+
"seed",
|
|
6014
|
+
"clone",
|
|
6015
|
+
"pull",
|
|
6016
|
+
"push",
|
|
6017
|
+
"reconcile",
|
|
6018
|
+
"custom",
|
|
6019
|
+
"websocket"
|
|
6020
|
+
]);
|
|
6021
|
+
var text = (value) => typeof value === "string" && value.trim().length > 0 && value.length <= 1024;
|
|
6022
|
+
function routeTicketError(code) {
|
|
6023
|
+
return new DatafnRoutingError({
|
|
6024
|
+
code,
|
|
6025
|
+
message: code,
|
|
6026
|
+
status: code === "DATAFN_ROUTE_RATE_LIMITED" ? 429 : code === "DATAFN_ROUTE_FORBIDDEN" ? 403 : 401,
|
|
6027
|
+
retryable: code === "DATAFN_ROUTE_TICKET_EXPIRED" || code === "DATAFN_ROUTE_RATE_LIMITED",
|
|
6028
|
+
executionStarted: false
|
|
6029
|
+
});
|
|
6030
|
+
}
|
|
6031
|
+
function assertClaims(value) {
|
|
6032
|
+
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 > core.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)) {
|
|
6033
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6034
|
+
}
|
|
6035
|
+
}
|
|
6036
|
+
function createDatafnHmacRouteTickets(input) {
|
|
6037
|
+
const keys = new Map(Object.entries(input.keys).map(([id, key]) => {
|
|
6038
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(id) || Buffer.byteLength(key) < 32) {
|
|
6039
|
+
throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6040
|
+
}
|
|
6041
|
+
return [id, Buffer.from(key)];
|
|
6042
|
+
}));
|
|
6043
|
+
if (!keys.has(input.activeKeyId)) throw new Error("DATAFN_ROUTE_ACTIVE_KEY_MISSING");
|
|
6044
|
+
const signature = (content, key) => crypto$1.createHmac("sha256", key).update(`datafn-regional-route-v1:${content}`).digest();
|
|
6045
|
+
return {
|
|
6046
|
+
sign(claims) {
|
|
6047
|
+
assertClaims(claims);
|
|
6048
|
+
const header = Buffer.from(JSON.stringify({ typ: "datafn-route-v1", alg: "HS256", kid: input.activeKeyId })).toString("base64url");
|
|
6049
|
+
const payload = Buffer.from(JSON.stringify(claims)).toString("base64url");
|
|
6050
|
+
const content = `${header}.${payload}`;
|
|
6051
|
+
return `${content}.${signature(content, keys.get(input.activeKeyId)).toString("base64url")}`;
|
|
6052
|
+
},
|
|
6053
|
+
verify(ticket) {
|
|
6054
|
+
try {
|
|
6055
|
+
if (typeof ticket !== "string" || ticket.length > 16384 || !/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(ticket)) throw new Error();
|
|
6056
|
+
const [header, payload, mac] = ticket.split(".");
|
|
6057
|
+
const protectedHeader = JSON.parse(Buffer.from(header, "base64url").toString());
|
|
6058
|
+
const key = keys.get(protectedHeader.kid);
|
|
6059
|
+
if (!key || protectedHeader.typ !== "datafn-route-v1" || protectedHeader.alg !== "HS256") throw new Error();
|
|
6060
|
+
const expected = signature(`${header}.${payload}`, key);
|
|
6061
|
+
const actual = Buffer.from(mac, "base64url");
|
|
6062
|
+
if (actual.length !== expected.length || !crypto$1.timingSafeEqual(actual, expected)) throw new Error();
|
|
6063
|
+
const claims = JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
6064
|
+
assertClaims(claims);
|
|
6065
|
+
return claims;
|
|
6066
|
+
} catch {
|
|
6067
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6068
|
+
}
|
|
6069
|
+
}
|
|
6070
|
+
};
|
|
6071
|
+
}
|
|
6072
|
+
function createDatafnRouteBootstrap(config) {
|
|
6073
|
+
const ttl = config.ttlMs ?? 6e4;
|
|
6074
|
+
if (!text(config.issuer) || !Number.isSafeInteger(ttl) || ttl < 1e3 || ttl > core.DATAFN_ROUTE_MAX_TTL_MS) {
|
|
6075
|
+
throw new Error("DATAFN_ROUTE_BOOTSTRAP_CONFIG_INVALID");
|
|
6076
|
+
}
|
|
6077
|
+
const now = config.now ?? Date.now;
|
|
6078
|
+
return async (request) => {
|
|
6079
|
+
try {
|
|
6080
|
+
if (request.method !== "POST") return new Response(null, { status: 405, headers: { allow: "POST", "cache-control": "no-store" } });
|
|
6081
|
+
const identity = await config.authenticate(request);
|
|
6082
|
+
if (!identity || !text(identity.subject) || !text(identity.namespace)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6083
|
+
let placement;
|
|
6084
|
+
try {
|
|
6085
|
+
placement = await config.directory.get(identity.namespace);
|
|
6086
|
+
} catch {
|
|
6087
|
+
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_UNAVAILABLE", message: "Placement unavailable", status: 503, retryable: true });
|
|
6088
|
+
}
|
|
6089
|
+
if (!placement || placement.namespace !== identity.namespace || placement.state === "tombstoned") {
|
|
6090
|
+
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_NOT_FOUND", message: "Placement not found", status: 404, retryable: false });
|
|
6091
|
+
}
|
|
6092
|
+
if (placement.state !== "active" || placement.migration) {
|
|
6093
|
+
throw new DatafnRoutingError({ code: "DATAFN_NAMESPACE_MOVING", message: "Placement is fenced", status: 409, retryable: true });
|
|
6094
|
+
}
|
|
6095
|
+
const permitted = [...await config.authorize(identity)];
|
|
6096
|
+
const endpoint = await config.resolveEndpoint(placement);
|
|
6097
|
+
const issuedAt = now();
|
|
6098
|
+
const expiresAt = Math.min(issuedAt + ttl, identity.expiresAt ?? Infinity);
|
|
6099
|
+
if (!Number.isSafeInteger(expiresAt) || expiresAt <= issuedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6100
|
+
const httpUrl = core.validateDatafnRegionalEndpoint(endpoint.httpUrl);
|
|
6101
|
+
const wsUrl = endpoint.wsUrl ? core.validateDatafnRegionalEndpoint(endpoint.wsUrl, true) : void 0;
|
|
6102
|
+
if (permitted.includes("websocket") && !wsUrl) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6103
|
+
const ticket = await config.signer.sign({
|
|
6104
|
+
version: 1,
|
|
6105
|
+
issuer: config.issuer,
|
|
6106
|
+
audience: endpoint.audience,
|
|
6107
|
+
subject: identity.subject,
|
|
6108
|
+
namespace: identity.namespace,
|
|
6109
|
+
regionId: placement.regionId,
|
|
6110
|
+
epoch: placement.epoch,
|
|
6111
|
+
scopes: permitted,
|
|
6112
|
+
issuedAt,
|
|
6113
|
+
notBefore: issuedAt,
|
|
6114
|
+
expiresAt,
|
|
6115
|
+
ticketId: crypto$1.randomUUID(),
|
|
6116
|
+
...identity.sessionBinding ? { sessionBinding: identity.sessionBinding } : {}
|
|
6117
|
+
});
|
|
6118
|
+
const descriptor = {
|
|
6119
|
+
version: 1,
|
|
6120
|
+
httpUrl,
|
|
6121
|
+
...wsUrl ? { wsUrl } : {},
|
|
6122
|
+
ticket,
|
|
6123
|
+
expiresAt,
|
|
6124
|
+
renewAfter: issuedAt + Math.floor((expiresAt - issuedAt) * 0.8)
|
|
6125
|
+
};
|
|
6126
|
+
emit(config, { type: "issued" });
|
|
6127
|
+
return Response.json(descriptor, { headers: { "cache-control": "no-store", pragma: "no-cache" } });
|
|
6128
|
+
} catch (error) {
|
|
6129
|
+
emit(config, { type: "rejected", code: error instanceof DatafnRoutingError ? error.code : "DATAFN_ROUTE_FORBIDDEN" });
|
|
6130
|
+
return (error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_FORBIDDEN")).toResponse();
|
|
6131
|
+
}
|
|
6132
|
+
};
|
|
6133
|
+
}
|
|
6134
|
+
function readDatafnRouteTicket(request, websocket = false) {
|
|
6135
|
+
const header = request.headers.get(core.DATAFN_ROUTE_TICKET_HEADER);
|
|
6136
|
+
const protocols = (request.headers.get("sec-websocket-protocol") ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
6137
|
+
const tickets = protocols.filter((s) => s.startsWith(core.DATAFN_ROUTE_WS_TICKET_PREFIX));
|
|
6138
|
+
if (tickets.length > 1 || header && tickets.length || !websocket && tickets.length || tickets.length && !protocols.includes(core.DATAFN_ROUTE_WS_PROTOCOL)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6139
|
+
return header ?? tickets[0]?.slice(core.DATAFN_ROUTE_WS_TICKET_PREFIX.length);
|
|
6140
|
+
}
|
|
6141
|
+
async function validateDatafnRouteTicket(input) {
|
|
6142
|
+
const { runtime } = input;
|
|
6143
|
+
try {
|
|
6144
|
+
const origin = input.request.headers.get("origin");
|
|
6145
|
+
if (origin && !runtime.allowedOrigins?.includes(origin)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6146
|
+
const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
|
|
6147
|
+
if (!token) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6148
|
+
const claims = await runtime.verifier.verify(token);
|
|
6149
|
+
assertClaims(claims);
|
|
6150
|
+
const now = (runtime.now ?? Date.now)();
|
|
6151
|
+
const skew = runtime.clockSkewMs ?? 5e3;
|
|
6152
|
+
if (!Number.isFinite(skew) || skew < 0 || skew > 3e4) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6153
|
+
if (claims.expiresAt <= now) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6154
|
+
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");
|
|
6155
|
+
const identity = await runtime.authenticate(input.request);
|
|
6156
|
+
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)())) {
|
|
6157
|
+
throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6158
|
+
}
|
|
6159
|
+
if (identity.expiresAt !== void 0 && claims.expiresAt > identity.expiresAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6160
|
+
if (runtime.isActive && !await runtime.isActive(claims)) throw routeTicketError("DATAFN_ROUTE_TICKET_REVOKED");
|
|
6161
|
+
if (runtime.allowRequest && !await runtime.allowRequest(claims)) throw routeTicketError("DATAFN_ROUTE_RATE_LIMITED");
|
|
6162
|
+
const admittedAt = (runtime.now ?? Date.now)();
|
|
6163
|
+
if (identity.expiresAt !== void 0 && identity.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6164
|
+
if (claims.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6165
|
+
emit(runtime, { type: "accepted" });
|
|
6166
|
+
return claims;
|
|
6167
|
+
} catch (error) {
|
|
6168
|
+
const safe = error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6169
|
+
emit(runtime, { type: "rejected", code: safe.code });
|
|
6170
|
+
throw safe;
|
|
6171
|
+
}
|
|
6172
|
+
}
|
|
6173
|
+
function withDatafnRegionalCors(handler, options) {
|
|
6174
|
+
const origins = new Set(options.origins.map((origin) => {
|
|
6175
|
+
if (new URL(origin).origin !== origin || origin === "null") throw new Error("DATAFN_ROUTE_ORIGIN_INVALID");
|
|
6176
|
+
return origin;
|
|
6177
|
+
}));
|
|
6178
|
+
const allowedHeaders = /* @__PURE__ */ new Set([
|
|
6179
|
+
"content-type",
|
|
6180
|
+
"authorization",
|
|
6181
|
+
core.DATAFN_ROUTE_TICKET_HEADER,
|
|
6182
|
+
...(options.headers ?? []).map((header) => header.toLowerCase())
|
|
6183
|
+
]);
|
|
6184
|
+
const allowedMethods = ["GET", "POST", "PATCH", "DELETE"];
|
|
6185
|
+
return async (request) => {
|
|
6186
|
+
const origin = request.headers.get("origin");
|
|
6187
|
+
if (origin && !origins.has(origin)) return routeTicketError("DATAFN_ROUTE_FORBIDDEN").toResponse();
|
|
6188
|
+
if (request.method === "OPTIONS") {
|
|
6189
|
+
const method = request.headers.get("access-control-request-method");
|
|
6190
|
+
const headers2 = (request.headers.get("access-control-request-headers") ?? "").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
6191
|
+
if (!origin || !allowedMethods.includes(method ?? "") || headers2.some((h) => !allowedHeaders.has(h))) return new Response(null, { status: 403 });
|
|
6192
|
+
return new Response(null, { status: 204, headers: {
|
|
6193
|
+
"access-control-allow-origin": origin,
|
|
6194
|
+
"access-control-allow-methods": allowedMethods.join(", "),
|
|
6195
|
+
"access-control-allow-headers": [...allowedHeaders].join(", "),
|
|
6196
|
+
vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
|
|
6197
|
+
"cache-control": "no-store"
|
|
6198
|
+
} });
|
|
6199
|
+
}
|
|
6200
|
+
const response = await handler(request);
|
|
6201
|
+
const headers = new Headers(response.headers);
|
|
6202
|
+
headers.append("vary", "Origin");
|
|
6203
|
+
headers.delete("access-control-allow-credentials");
|
|
6204
|
+
headers.set("cache-control", "no-store");
|
|
6205
|
+
if (origin) headers.set("access-control-allow-origin", origin);
|
|
6206
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
6207
|
+
};
|
|
6208
|
+
}
|
|
6209
|
+
function emit(config, event) {
|
|
6210
|
+
try {
|
|
6211
|
+
config.onEvent?.(event);
|
|
6212
|
+
} catch {
|
|
6213
|
+
}
|
|
6214
|
+
}
|
|
5937
6215
|
var DATAFN_ROUTING_ASSERTION_HEADER = "x-datafn-routing-assertion";
|
|
5938
6216
|
var DATAFN_ROUTING_INTERNAL_HEADERS = [
|
|
5939
6217
|
DATAFN_ROUTING_ASSERTION_HEADER,
|
|
@@ -6085,6 +6363,28 @@ async function claimDatafnNamespacePlacement(input) {
|
|
|
6085
6363
|
async function validateDatafnPlacement(input) {
|
|
6086
6364
|
const startedAt = Date.now();
|
|
6087
6365
|
const requestId2 = input.request?.headers.get("x-request-id") ?? void 0;
|
|
6366
|
+
let ticket;
|
|
6367
|
+
const hasAssertion = Boolean(input.request?.headers.get(input.runtime.assertionHeader ?? DATAFN_ROUTING_ASSERTION_HEADER)?.trim());
|
|
6368
|
+
if (input.request) {
|
|
6369
|
+
const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
|
|
6370
|
+
if (token !== void 0 && (hasAssertion || !input.runtime.routeTickets)) {
|
|
6371
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6372
|
+
}
|
|
6373
|
+
if (input.runtime.routeTickets && (!hasAssertion || token !== void 0)) {
|
|
6374
|
+
if (DATAFN_ROUTING_INTERNAL_HEADERS.some((name) => input.request.headers.has(name))) {
|
|
6375
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6376
|
+
}
|
|
6377
|
+
ticket = await validateDatafnRouteTicket({
|
|
6378
|
+
request: input.request,
|
|
6379
|
+
namespace: input.namespace,
|
|
6380
|
+
regionId: input.regionId,
|
|
6381
|
+
scope: input.scope ?? "custom",
|
|
6382
|
+
runtime: input.runtime.routeTickets
|
|
6383
|
+
});
|
|
6384
|
+
}
|
|
6385
|
+
} else if (input.runtime.routeTickets && !input.trustedInternal) {
|
|
6386
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6387
|
+
}
|
|
6088
6388
|
let placement;
|
|
6089
6389
|
try {
|
|
6090
6390
|
placement = await input.runtime.directory.get(input.namespace);
|
|
@@ -6122,7 +6422,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6122
6422
|
retryable: false
|
|
6123
6423
|
});
|
|
6124
6424
|
}
|
|
6125
|
-
const assertion = await validateRoutingAssertion({
|
|
6425
|
+
const assertion = ticket ? void 0 : await validateRoutingAssertion({
|
|
6126
6426
|
namespace: input.namespace,
|
|
6127
6427
|
regionId: input.regionId,
|
|
6128
6428
|
placement,
|
|
@@ -6178,7 +6478,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6178
6478
|
internal: Boolean(assertion)
|
|
6179
6479
|
});
|
|
6180
6480
|
}
|
|
6181
|
-
if (assertion && assertion.epoch !== placement.epoch) {
|
|
6481
|
+
if (assertion && assertion.epoch !== placement.epoch || ticket && ticket.epoch !== placement.epoch) {
|
|
6182
6482
|
await emitRoutingEvent(input.runtime, {
|
|
6183
6483
|
type: "mismatch",
|
|
6184
6484
|
requestId: requestId2,
|
|
@@ -6191,14 +6491,17 @@ async function validateDatafnPlacement(input) {
|
|
|
6191
6491
|
});
|
|
6192
6492
|
throw new DatafnRoutingError({
|
|
6193
6493
|
code: "DATAFN_REGION_MISMATCH",
|
|
6194
|
-
message: "Routing
|
|
6494
|
+
message: "Routing grant uses a stale placement epoch",
|
|
6195
6495
|
status: 409,
|
|
6196
6496
|
retryable: true,
|
|
6197
6497
|
placement,
|
|
6198
|
-
internal:
|
|
6498
|
+
internal: Boolean(assertion)
|
|
6199
6499
|
});
|
|
6200
6500
|
}
|
|
6201
|
-
|
|
6501
|
+
if (ticket && ticket.expiresAt <= (input.runtime.routeTickets?.now ?? Date.now)()) {
|
|
6502
|
+
throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6503
|
+
}
|
|
6504
|
+
return { placement, ...assertion ? { assertion } : {}, ...ticket ? { ticket } : {} };
|
|
6202
6505
|
}
|
|
6203
6506
|
function createDatafnGatewayRouter(config) {
|
|
6204
6507
|
const maxBodyBytes = validateDatafnRoutingBodyLimit(config.maxBodyBytes);
|
|
@@ -8629,12 +8932,16 @@ function buildReplaceRecord(schema, resourceName, existingRecord, newRecord, res
|
|
|
8629
8932
|
result[key] = existingRecord[key];
|
|
8630
8933
|
continue;
|
|
8631
8934
|
}
|
|
8935
|
+
if (field.readonly) {
|
|
8936
|
+
result[key] = Object.prototype.hasOwnProperty.call(newRecord, key) ? newRecord[key] : existingRecord[key];
|
|
8937
|
+
continue;
|
|
8938
|
+
}
|
|
8632
8939
|
if (Object.prototype.hasOwnProperty.call(newRecord, key)) {
|
|
8633
8940
|
result[key] = newRecord[key];
|
|
8634
8941
|
} else {
|
|
8635
8942
|
result[key] = field.default !== void 0 ? field.default : null;
|
|
8636
8943
|
}
|
|
8637
|
-
if (field.required && (result[key] ===
|
|
8944
|
+
if (field.required && (result[key] === void 0 || result[key] === null && !field.nullable)) {
|
|
8638
8945
|
return {
|
|
8639
8946
|
ok: false,
|
|
8640
8947
|
code: "DFQL_INVALID",
|
|
@@ -8721,8 +9028,7 @@ function isRecordEffectivelyInactive(record) {
|
|
|
8721
9028
|
return isRecordInactive(record) || record?.isAncestorInactive === true;
|
|
8722
9029
|
}
|
|
8723
9030
|
function resourceSupportsAncestorInactive(schema, resource) {
|
|
8724
|
-
|
|
8725
|
-
return resourceSchema?.fields.some((field) => field.name === "isAncestorInactive") === true;
|
|
9031
|
+
return core.resourceRequiresAncestorInactive(schema.relations, resource);
|
|
8726
9032
|
}
|
|
8727
9033
|
function dependentEndpoint(relation) {
|
|
8728
9034
|
return relation.type === "many-one" ? relation.from : relation.to;
|
|
@@ -10141,7 +10447,7 @@ function decideMergeMode(schema, resource, id, delta) {
|
|
|
10141
10447
|
for (const field of resourceDef.fields) {
|
|
10142
10448
|
if (!field.required) continue;
|
|
10143
10449
|
if (field.name === "id" || field.name === "__ns") continue;
|
|
10144
|
-
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && delta[field.name] !== null;
|
|
10450
|
+
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && (delta[field.name] !== null || field.nullable === true);
|
|
10145
10451
|
const hasDefault = field.default !== void 0;
|
|
10146
10452
|
if (!hasProvided && !hasDefault) return { mode: "not_found" };
|
|
10147
10453
|
}
|
|
@@ -11880,6 +12186,64 @@ async function drainPermissionDirectoryOutbox(db, runtime, logger, limit = 100)
|
|
|
11880
12186
|
}
|
|
11881
12187
|
return { processed, pending: tasks.length - processed };
|
|
11882
12188
|
}
|
|
12189
|
+
async function drainNamespacePermissionDirectoryOutbox(db, namespace, runtime, logger, options = {}) {
|
|
12190
|
+
await ensurePermissionDirectoryOutbox(db);
|
|
12191
|
+
const limit = options.limit ?? 100;
|
|
12192
|
+
const maxRounds = options.maxRounds ?? 32;
|
|
12193
|
+
let processed = 0;
|
|
12194
|
+
for (let round = 0; round < maxRounds; round += 1) {
|
|
12195
|
+
const tasks = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12196
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12197
|
+
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12198
|
+
{ field: "next_attempt_at", op: "lte", value: (/* @__PURE__ */ new Date()).toISOString() }
|
|
12199
|
+
], {
|
|
12200
|
+
orderBy: "next_attempt_at",
|
|
12201
|
+
limit
|
|
12202
|
+
});
|
|
12203
|
+
if (tasks.length === 0) {
|
|
12204
|
+
break;
|
|
12205
|
+
}
|
|
12206
|
+
let roundProcessed = 0;
|
|
12207
|
+
for (const task of tasks) {
|
|
12208
|
+
const taskId = String(task.id);
|
|
12209
|
+
const selectedNextAttemptAt = String(task.next_attempt_at);
|
|
12210
|
+
const claimedNextAttemptAt = new Date(
|
|
12211
|
+
Date.now() + DRAIN_CLAIM_LEASE_MS
|
|
12212
|
+
).toISOString();
|
|
12213
|
+
const claimed = await db.internal.update(OUTBOX_TABLE, [
|
|
12214
|
+
{ field: "id", op: "eq", value: taskId },
|
|
12215
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12216
|
+
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12217
|
+
{ field: "next_attempt_at", op: "eq", value: selectedNextAttemptAt }
|
|
12218
|
+
], {
|
|
12219
|
+
next_attempt_at: claimedNextAttemptAt
|
|
12220
|
+
});
|
|
12221
|
+
if (claimed === 0) continue;
|
|
12222
|
+
if (await drainPermissionDirectorySync(
|
|
12223
|
+
db,
|
|
12224
|
+
taskId,
|
|
12225
|
+
runtime,
|
|
12226
|
+
logger,
|
|
12227
|
+
{ expectedNextAttemptAt: claimedNextAttemptAt }
|
|
12228
|
+
)) {
|
|
12229
|
+
processed += 1;
|
|
12230
|
+
roundProcessed += 1;
|
|
12231
|
+
}
|
|
12232
|
+
}
|
|
12233
|
+
if (roundProcessed === 0) {
|
|
12234
|
+
const remaining2 = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12235
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12236
|
+
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12237
|
+
], { limit: 1 });
|
|
12238
|
+
return { processed, pending: remaining2.length };
|
|
12239
|
+
}
|
|
12240
|
+
}
|
|
12241
|
+
const remaining = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12242
|
+
{ field: "namespace", op: "eq", value: namespace },
|
|
12243
|
+
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12244
|
+
], { limit: 1 });
|
|
12245
|
+
return { processed, pending: remaining.length };
|
|
12246
|
+
}
|
|
11883
12247
|
|
|
11884
12248
|
// src/execution/mutation/execute.ts
|
|
11885
12249
|
var FAILED_SHARE_COMPENSATION_RESULT = /* @__PURE__ */ Symbol("failedShareCompensation");
|
|
@@ -11981,6 +12345,23 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
11981
12345
|
let additionalChanges = [];
|
|
11982
12346
|
let skipChangeTracking = false;
|
|
11983
12347
|
let failedShareCompensation = null;
|
|
12348
|
+
if ((mutation.operation === "insert" || mutation.operation === "merge" || mutation.operation === "replace") && mutation.record) {
|
|
12349
|
+
const systemField = core.findSystemFieldWrite(schema.relations, mutation.resource, mutation.record);
|
|
12350
|
+
if (systemField) {
|
|
12351
|
+
return {
|
|
12352
|
+
ok: false,
|
|
12353
|
+
mutationId: effectiveMutationId,
|
|
12354
|
+
affectedIds: [],
|
|
12355
|
+
errors: [{
|
|
12356
|
+
code: "DFQL_INVALID",
|
|
12357
|
+
message: `Field is read-only: ${systemField} on ${mutation.resource}`,
|
|
12358
|
+
path: `record.${systemField}`,
|
|
12359
|
+
retryable: false
|
|
12360
|
+
}],
|
|
12361
|
+
deduped: false
|
|
12362
|
+
};
|
|
12363
|
+
}
|
|
12364
|
+
}
|
|
11984
12365
|
try {
|
|
11985
12366
|
switch (mutation.operation) {
|
|
11986
12367
|
case "insert":
|
|
@@ -14440,7 +14821,7 @@ function compareCanonicalChangeEntries(a, b) {
|
|
|
14440
14821
|
function mergeCanonicalChangeStreamsBounded(streams, limit) {
|
|
14441
14822
|
const heads = streams.filter((stream) => stream.changes.length > 0).map((stream) => ({ resource: stream.resource, changes: stream.changes, index: 0 }));
|
|
14442
14823
|
const merged = [];
|
|
14443
|
-
const latestCursorPerResource =
|
|
14824
|
+
const latestCursorPerResource = /* @__PURE__ */ Object.create(null);
|
|
14444
14825
|
let peakWindow = heads.length;
|
|
14445
14826
|
while (heads.length > 0 && merged.length < limit) {
|
|
14446
14827
|
let bestIdx = 0;
|
|
@@ -14672,11 +15053,11 @@ async function executePullCanonical(request, schema, db, namespace, sequenceStor
|
|
|
14672
15053
|
const requestedResourceKeys = Object.keys(request.cursors).filter(
|
|
14673
15054
|
(resource) => resource !== ACTOR_FEED_CURSOR_KEY
|
|
14674
15055
|
);
|
|
14675
|
-
const records =
|
|
14676
|
-
const merged =
|
|
14677
|
-
const deleted =
|
|
14678
|
-
const joins =
|
|
14679
|
-
const cursors =
|
|
15056
|
+
const records = /* @__PURE__ */ Object.create(null);
|
|
15057
|
+
const merged = /* @__PURE__ */ Object.create(null);
|
|
15058
|
+
const deleted = /* @__PURE__ */ Object.create(null);
|
|
15059
|
+
const joins = /* @__PURE__ */ Object.create(null);
|
|
15060
|
+
const cursors = /* @__PURE__ */ Object.create(null);
|
|
14680
15061
|
for (const resource of requestedResourceKeys) {
|
|
14681
15062
|
if (!resource.startsWith("join_") && !resource.startsWith("__datafn_")) {
|
|
14682
15063
|
records[resource] = [];
|
|
@@ -15079,6 +15460,17 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15079
15460
|
const batchLocalSeen = /* @__PURE__ */ new Map();
|
|
15080
15461
|
async function executeMutationOp(mut, targetDb) {
|
|
15081
15462
|
const resolvedCapabilities = resolveCapabilitiesForResource2(schema, mut.resource);
|
|
15463
|
+
if ((mut.operation === "insert" || mut.operation === "merge" || mut.operation === "replace") && typeof mut.record === "object" && mut.record !== null) {
|
|
15464
|
+
const systemField = core.findSystemFieldWrite(schema.relations, mut.resource, mut.record);
|
|
15465
|
+
if (systemField) {
|
|
15466
|
+
return {
|
|
15467
|
+
ok: false,
|
|
15468
|
+
code: "DFQL_INVALID",
|
|
15469
|
+
message: `Field is read-only: ${systemField} on ${mut.resource}`,
|
|
15470
|
+
path: `record.${systemField}`
|
|
15471
|
+
};
|
|
15472
|
+
}
|
|
15473
|
+
}
|
|
15082
15474
|
switch (mut.operation) {
|
|
15083
15475
|
case "insert": {
|
|
15084
15476
|
const coercedData = applyDateCoercion(schema, mut.resource, mut.record || {}, logger);
|
|
@@ -17609,6 +18001,7 @@ var WebSocketManager = class {
|
|
|
17609
18001
|
* Reverse mapping: client → known principalIds for O(1) targeted index cleanup.
|
|
17610
18002
|
*/
|
|
17611
18003
|
this.clientPrincipals = /* @__PURE__ */ new Map();
|
|
18004
|
+
this.routeTickets = /* @__PURE__ */ new Map();
|
|
17612
18005
|
this.clientPlacement = /* @__PURE__ */ new Map();
|
|
17613
18006
|
/** Monotonic namespace fence generation closes the validation/registration race. */
|
|
17614
18007
|
this.namespaceFenceGeneration = /* @__PURE__ */ new Map();
|
|
@@ -17760,6 +18153,11 @@ var WebSocketManager = class {
|
|
|
17760
18153
|
*/
|
|
17761
18154
|
addClient(client, authContext) {
|
|
17762
18155
|
const { namespace } = authContext;
|
|
18156
|
+
if (this.clientNamespace.has(client)) return false;
|
|
18157
|
+
if (authContext.routeTicket && authContext.routeTicket.expiresAt <= (authContext.routeTicket.now ?? Date.now)()) {
|
|
18158
|
+
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18159
|
+
return false;
|
|
18160
|
+
}
|
|
17763
18161
|
if (this.closed) {
|
|
17764
18162
|
this.closeClientSafely(client, 1001, "Going Away", "ws-closed-admission");
|
|
17765
18163
|
return false;
|
|
@@ -17793,6 +18191,12 @@ var WebSocketManager = class {
|
|
|
17793
18191
|
...authContext.regionId ? { regionId: authContext.regionId } : {},
|
|
17794
18192
|
...Number.isSafeInteger(authContext.routingEpoch) ? { epoch: authContext.routingEpoch } : {}
|
|
17795
18193
|
});
|
|
18194
|
+
if (authContext.routeTicket) {
|
|
18195
|
+
const { id, expiresAt, now = Date.now } = authContext.routeTicket;
|
|
18196
|
+
const timer = setTimeout(() => this.expireRouteClient(client), Math.max(0, expiresAt - now()));
|
|
18197
|
+
timer.unref?.();
|
|
18198
|
+
this.routeTickets.set(client, { id, expiresAt, now, timer });
|
|
18199
|
+
}
|
|
17796
18200
|
if (this.heartbeatTimer === null) {
|
|
17797
18201
|
this.startHeartbeat();
|
|
17798
18202
|
}
|
|
@@ -17801,7 +18205,24 @@ var WebSocketManager = class {
|
|
|
17801
18205
|
/**
|
|
17802
18206
|
* Handle disconnection.
|
|
17803
18207
|
*/
|
|
18208
|
+
expireRouteClient(client) {
|
|
18209
|
+
this.removeClient(client);
|
|
18210
|
+
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18211
|
+
}
|
|
18212
|
+
revokeRouteTicket(ticketId) {
|
|
18213
|
+
let count = 0;
|
|
18214
|
+
for (const [client, ticket] of this.routeTickets) {
|
|
18215
|
+
if (ticket.id !== ticketId) continue;
|
|
18216
|
+
this.removeClient(client);
|
|
18217
|
+
this.closeClientSafely(client, 4403, "DATAFN_ROUTE_TICKET_REVOKED", "ws-ticket-revoked");
|
|
18218
|
+
count++;
|
|
18219
|
+
}
|
|
18220
|
+
return count;
|
|
18221
|
+
}
|
|
17804
18222
|
removeClient(client) {
|
|
18223
|
+
const ticket = this.routeTickets.get(client);
|
|
18224
|
+
if (ticket) clearTimeout(ticket.timer);
|
|
18225
|
+
this.routeTickets.delete(client);
|
|
17805
18226
|
const namespace = this.clientNamespace.get(client);
|
|
17806
18227
|
if (namespace !== void 0) {
|
|
17807
18228
|
this.removeTargetingIndexes(client, namespace);
|
|
@@ -17918,9 +18339,16 @@ var WebSocketManager = class {
|
|
|
17918
18339
|
principals: mode === "targeted" ? Array.from(requestedPrincipals).sort() : void 0
|
|
17919
18340
|
}
|
|
17920
18341
|
});
|
|
18342
|
+
let sent = 0;
|
|
17921
18343
|
for (const client of recipients) {
|
|
18344
|
+
const ticket = this.routeTickets.get(client);
|
|
18345
|
+
if (ticket && ticket.expiresAt <= ticket.now()) {
|
|
18346
|
+
this.expireRouteClient(client);
|
|
18347
|
+
continue;
|
|
18348
|
+
}
|
|
17922
18349
|
try {
|
|
17923
18350
|
client.send(msg);
|
|
18351
|
+
sent++;
|
|
17924
18352
|
} catch (e) {
|
|
17925
18353
|
this.logger?.error("Error sending to websocket client", { error: String(e), operation: "ws-broadcast" });
|
|
17926
18354
|
this.removeClient(client);
|
|
@@ -17930,7 +18358,7 @@ var WebSocketManager = class {
|
|
|
17930
18358
|
return {
|
|
17931
18359
|
mode,
|
|
17932
18360
|
degraded,
|
|
17933
|
-
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" :
|
|
18361
|
+
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : sent
|
|
17934
18362
|
};
|
|
17935
18363
|
}
|
|
17936
18364
|
/**
|
|
@@ -17949,6 +18377,8 @@ var WebSocketManager = class {
|
|
|
17949
18377
|
this.clientNamespace.clear();
|
|
17950
18378
|
this.clientPrincipals.clear();
|
|
17951
18379
|
this.clientPlacement.clear();
|
|
18380
|
+
for (const ticket of this.routeTickets.values()) clearTimeout(ticket.timer);
|
|
18381
|
+
this.routeTickets.clear();
|
|
17952
18382
|
this.pendingPong.clear();
|
|
17953
18383
|
}
|
|
17954
18384
|
/**
|
|
@@ -18155,35 +18585,6 @@ function relationReferencesInternalResource(relation, internalResourceNames) {
|
|
|
18155
18585
|
function normalizeResourceEndpoint(endpoint) {
|
|
18156
18586
|
return typeof endpoint === "string" ? [endpoint] : [...endpoint];
|
|
18157
18587
|
}
|
|
18158
|
-
function payloadReferencesInternalResource(payload, internalResourceNames) {
|
|
18159
|
-
if (internalResourceNames.size === 0) {
|
|
18160
|
-
return false;
|
|
18161
|
-
}
|
|
18162
|
-
if (Array.isArray(payload)) {
|
|
18163
|
-
return payload.some((entry) => payloadReferencesInternalResource(entry, internalResourceNames));
|
|
18164
|
-
}
|
|
18165
|
-
if (!payload || typeof payload !== "object") {
|
|
18166
|
-
return false;
|
|
18167
|
-
}
|
|
18168
|
-
const record = payload;
|
|
18169
|
-
if (typeof record.resource === "string" && internalResourceNames.has(record.resource)) {
|
|
18170
|
-
return true;
|
|
18171
|
-
}
|
|
18172
|
-
if (Array.isArray(record.resources) && record.resources.some((resource) => typeof resource === "string" && internalResourceNames.has(resource))) {
|
|
18173
|
-
return true;
|
|
18174
|
-
}
|
|
18175
|
-
for (const key of ["mutations", "queries", "steps", "operations"]) {
|
|
18176
|
-
if (Array.isArray(record[key]) && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18177
|
-
return true;
|
|
18178
|
-
}
|
|
18179
|
-
}
|
|
18180
|
-
for (const key of ["query", "mutation", "payload"]) {
|
|
18181
|
-
if (record[key] && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18182
|
-
return true;
|
|
18183
|
-
}
|
|
18184
|
-
}
|
|
18185
|
-
return false;
|
|
18186
|
-
}
|
|
18187
18588
|
function normalizePluginAuthorizationError(error, pluginName) {
|
|
18188
18589
|
if (error && typeof error === "object") {
|
|
18189
18590
|
const candidate = error;
|
|
@@ -18782,10 +19183,10 @@ async function createDatafnServer(config) {
|
|
|
18782
19183
|
}
|
|
18783
19184
|
return { ok: true };
|
|
18784
19185
|
};
|
|
18785
|
-
const withAuth = (action, handler) => {
|
|
19186
|
+
const withAuth = (action, handler, origin = "http", rest = false) => {
|
|
18786
19187
|
return async (req, ctx) => {
|
|
18787
19188
|
const enrichedCtx = createEnrichedContext(ctx);
|
|
18788
|
-
const placementRequest = multiRegionRuntime?.placement ? req.clone() : void 0;
|
|
19189
|
+
const placementRequest = multiRegionRuntime?.placement && origin === "http" ? req.clone() : void 0;
|
|
18789
19190
|
let payload = null;
|
|
18790
19191
|
if (shuttingDown) {
|
|
18791
19192
|
return completeDatafnResponse({
|
|
@@ -18914,7 +19315,36 @@ async function createDatafnServer(config) {
|
|
|
18914
19315
|
}
|
|
18915
19316
|
enrichedCtx.parsedBody = payload;
|
|
18916
19317
|
}
|
|
18917
|
-
|
|
19318
|
+
let structuralPayload = payload;
|
|
19319
|
+
if (rest) {
|
|
19320
|
+
let resource;
|
|
19321
|
+
try {
|
|
19322
|
+
const segment = sanitizePathSegment(new URL(req.url).pathname.split("/")[3] ?? "");
|
|
19323
|
+
if (!segment.ok) throw new Error("Invalid path segment");
|
|
19324
|
+
resource = segment.value;
|
|
19325
|
+
} catch {
|
|
19326
|
+
return completeDatafnResponse({
|
|
19327
|
+
action,
|
|
19328
|
+
request: req,
|
|
19329
|
+
context: enrichedCtx,
|
|
19330
|
+
payload,
|
|
19331
|
+
response: errorResponse({ code: "DFQL_INVALID", message: "Invalid path segment", details: { path: "resource" } }, 400)
|
|
19332
|
+
});
|
|
19333
|
+
}
|
|
19334
|
+
structuralPayload = { resource };
|
|
19335
|
+
}
|
|
19336
|
+
const parsedProtocol = core.parseDatafnRequest(action, structuralPayload, { schema: validatedSchema });
|
|
19337
|
+
if (!parsedProtocol.ok) {
|
|
19338
|
+
return completeDatafnResponse({
|
|
19339
|
+
action,
|
|
19340
|
+
request: req,
|
|
19341
|
+
context: enrichedCtx,
|
|
19342
|
+
payload,
|
|
19343
|
+
response: errorResponse(parsedProtocol.error, 400)
|
|
19344
|
+
});
|
|
19345
|
+
}
|
|
19346
|
+
const selection = core.collectStructuralResourceSelectors(parsedProtocol.result);
|
|
19347
|
+
if (selection.selectors.some((resource) => internalResourceNames.has(resource))) {
|
|
18918
19348
|
await emitDataFnEvent({
|
|
18919
19349
|
domain: "datafn",
|
|
18920
19350
|
type: "datafn.authorization.denied",
|
|
@@ -18940,7 +19370,9 @@ async function createDatafnServer(config) {
|
|
|
18940
19370
|
namespace: await extractNamespace(enrichedCtx),
|
|
18941
19371
|
regionId: multiRegionRuntime.regionId,
|
|
18942
19372
|
runtime: multiRegionRuntime.placement,
|
|
18943
|
-
request: placementRequest
|
|
19373
|
+
request: placementRequest,
|
|
19374
|
+
scope: action,
|
|
19375
|
+
trustedInternal: origin === "executor"
|
|
18944
19376
|
});
|
|
18945
19377
|
} catch (error) {
|
|
18946
19378
|
if (error instanceof DatafnRoutingError) {
|
|
@@ -18959,7 +19391,7 @@ async function createDatafnServer(config) {
|
|
|
18959
19391
|
action,
|
|
18960
19392
|
request: req,
|
|
18961
19393
|
context: enrichedCtx,
|
|
18962
|
-
payload
|
|
19394
|
+
payload: structuralPayload
|
|
18963
19395
|
});
|
|
18964
19396
|
if (!pluginAuthorization.ok) {
|
|
18965
19397
|
await emitDataFnEvent({
|
|
@@ -18979,7 +19411,7 @@ async function createDatafnServer(config) {
|
|
|
18979
19411
|
});
|
|
18980
19412
|
}
|
|
18981
19413
|
if (config.authorize) {
|
|
18982
|
-
const authorized = await config.authorize(enrichedCtx, action,
|
|
19414
|
+
const authorized = await config.authorize(enrichedCtx, action, structuralPayload);
|
|
18983
19415
|
if (!authorized) {
|
|
18984
19416
|
await emitDataFnEvent({
|
|
18985
19417
|
domain: "datafn",
|
|
@@ -19054,7 +19486,7 @@ async function createDatafnServer(config) {
|
|
|
19054
19486
|
try {
|
|
19055
19487
|
const configuredContext = config.context;
|
|
19056
19488
|
const executorContext = context ?? (typeof configuredContext === "function" ? await configuredContext(request) : configuredContext ?? {});
|
|
19057
|
-
response = await withAuth(action, handler)(request, executorContext);
|
|
19489
|
+
response = await withAuth(action, handler, "executor")(request, executorContext);
|
|
19058
19490
|
} catch {
|
|
19059
19491
|
throw new DatafnExecutorError(
|
|
19060
19492
|
{ code: "INTERNAL", message: "Internal Server Error" },
|
|
@@ -19244,7 +19676,9 @@ async function createDatafnServer(config) {
|
|
|
19244
19676
|
method: route.method,
|
|
19245
19677
|
path: route.path,
|
|
19246
19678
|
meta: route.meta,
|
|
19247
|
-
|
|
19679
|
+
// withAuth and the REST handler validate URL segments and complete DataFn response hooks.
|
|
19680
|
+
decodeParams: false,
|
|
19681
|
+
handler: withAuth(action, route.handler, "http", true)
|
|
19248
19682
|
});
|
|
19249
19683
|
}
|
|
19250
19684
|
}
|
|
@@ -19416,12 +19850,18 @@ async function createDatafnServer(config) {
|
|
|
19416
19850
|
regionId: multiRegionRuntime.regionId,
|
|
19417
19851
|
runtime: multiRegionRuntime.placement,
|
|
19418
19852
|
request: handshakeRequest,
|
|
19419
|
-
|
|
19853
|
+
scope: "websocket",
|
|
19854
|
+
trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion && !multiRegionRuntime.placement.routeTickets
|
|
19420
19855
|
});
|
|
19421
19856
|
added = wsManager.addClient(client, {
|
|
19422
19857
|
...authContext,
|
|
19423
19858
|
regionId: validated.placement.regionId,
|
|
19424
|
-
routingEpoch: validated.placement.epoch
|
|
19859
|
+
routingEpoch: validated.placement.epoch,
|
|
19860
|
+
routeTicket: validated.ticket ? {
|
|
19861
|
+
id: validated.ticket.ticketId,
|
|
19862
|
+
expiresAt: validated.ticket.expiresAt,
|
|
19863
|
+
now: multiRegionRuntime.placement.routeTickets?.now
|
|
19864
|
+
} : void 0
|
|
19425
19865
|
});
|
|
19426
19866
|
if (!added) return false;
|
|
19427
19867
|
if (wsManager.getNamespaceFenceGeneration(authContext.namespace) !== fenceGeneration) {
|
|
@@ -19440,7 +19880,7 @@ async function createDatafnServer(config) {
|
|
|
19440
19880
|
if (added) wsManager.removeClient(client);
|
|
19441
19881
|
try {
|
|
19442
19882
|
client.close(
|
|
19443
|
-
error instanceof DatafnRoutingError ? 4510 : 1011,
|
|
19883
|
+
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,
|
|
19444
19884
|
error instanceof Error ? error.message : "Placement validation failed"
|
|
19445
19885
|
);
|
|
19446
19886
|
} catch {
|
|
@@ -19448,6 +19888,7 @@ async function createDatafnServer(config) {
|
|
|
19448
19888
|
return false;
|
|
19449
19889
|
}
|
|
19450
19890
|
},
|
|
19891
|
+
revokeRouteTicket: (ticketId) => wsManager.revokeRouteTicket(ticketId),
|
|
19451
19892
|
fenceNamespace: (namespace, minimumEpoch) => wsManager.fenceNamespace(namespace, minimumEpoch),
|
|
19452
19893
|
removeClient: (client) => wsManager.removeClient(client),
|
|
19453
19894
|
handleMessage: (client, data) => wsManager.handleMessage(client, data),
|
|
@@ -20520,7 +20961,582 @@ function readDatafnCloudflarePlacementLocation(request) {
|
|
|
20520
20961
|
...continent ? { continent } : {}
|
|
20521
20962
|
};
|
|
20522
20963
|
}
|
|
20964
|
+
var POSTGRES_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
20965
|
+
var DATAFN_INTERNAL_TABLE_RE = /^__datafn_[a-z0-9_]+$/;
|
|
20966
|
+
var LEGACY_PERMISSIONS_TABLE_RE = /^__datafn_permissions_(?!global$)[A-Za-z0-9_]+$/;
|
|
20967
|
+
var POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL = `
|
|
20968
|
+
SELECT c.table_name, c.column_name
|
|
20969
|
+
FROM information_schema.columns AS c
|
|
20970
|
+
JOIN information_schema.tables AS t
|
|
20971
|
+
ON t.table_schema = c.table_schema
|
|
20972
|
+
AND t.table_name = c.table_name
|
|
20973
|
+
WHERE c.table_schema = current_schema()
|
|
20974
|
+
AND t.table_type = 'BASE TABLE'
|
|
20975
|
+
ORDER BY c.table_name, c.ordinal_position
|
|
20976
|
+
`.trim();
|
|
20977
|
+
var INTERNAL_ROLE_BY_TABLE = {
|
|
20978
|
+
__datafn_meta: "sync_meta",
|
|
20979
|
+
__datafn_changes: "sync_changes",
|
|
20980
|
+
__datafn_idempotency: "idempotency",
|
|
20981
|
+
__datafn_seed: "seed",
|
|
20982
|
+
__datafn_permission_directory_outbox: "permission_directory_outbox"
|
|
20983
|
+
};
|
|
20984
|
+
var SHARING_RESOURCE_TABLES = [
|
|
20985
|
+
{
|
|
20986
|
+
logicalRole: "permissions_global",
|
|
20987
|
+
relation: "__datafn_permissions_global"
|
|
20988
|
+
},
|
|
20989
|
+
{
|
|
20990
|
+
logicalRole: "principal_memberships",
|
|
20991
|
+
relation: "__datafn_principal_memberships"
|
|
20992
|
+
},
|
|
20993
|
+
{
|
|
20994
|
+
logicalRole: "principal_hierarchy",
|
|
20995
|
+
relation: "__datafn_principal_hierarchy"
|
|
20996
|
+
}
|
|
20997
|
+
];
|
|
20998
|
+
var SELECTOR_COLUMN = {
|
|
20999
|
+
resource_ns: "__ns",
|
|
21000
|
+
internal_namespace: "namespace"
|
|
21001
|
+
};
|
|
21002
|
+
var DatafnNamespaceStorageError = class extends Error {
|
|
21003
|
+
constructor(input) {
|
|
21004
|
+
super(input.message);
|
|
21005
|
+
this.name = "DatafnNamespaceStorageError";
|
|
21006
|
+
this.code = input.code;
|
|
21007
|
+
this.details = input.details;
|
|
21008
|
+
}
|
|
21009
|
+
};
|
|
21010
|
+
function fail(code, message, details) {
|
|
21011
|
+
throw new DatafnNamespaceStorageError({ code, message, details });
|
|
21012
|
+
}
|
|
21013
|
+
function quotePostgresIdentifier(identifier) {
|
|
21014
|
+
if (!POSTGRES_IDENTIFIER_RE.test(identifier)) {
|
|
21015
|
+
fail(
|
|
21016
|
+
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21017
|
+
`Invalid PostgreSQL identifier "${identifier}"`,
|
|
21018
|
+
{ identifier }
|
|
21019
|
+
);
|
|
21020
|
+
}
|
|
21021
|
+
return `"${identifier}"`;
|
|
21022
|
+
}
|
|
21023
|
+
function namespacedInternalTableRoles() {
|
|
21024
|
+
return INTERNAL_ROLE_BY_TABLE;
|
|
21025
|
+
}
|
|
21026
|
+
function catalogByName(catalog) {
|
|
21027
|
+
const map = /* @__PURE__ */ new Map();
|
|
21028
|
+
for (const relation of catalog.relations) {
|
|
21029
|
+
if (map.has(relation.name)) fail("DATAFN_NAMESPACE_STORAGE_INVALID", "Duplicate catalog relation.");
|
|
21030
|
+
map.set(relation.name, relation);
|
|
21031
|
+
}
|
|
21032
|
+
return map;
|
|
21033
|
+
}
|
|
21034
|
+
function hasColumn(relation, column) {
|
|
21035
|
+
return Boolean(relation?.columns.includes(column));
|
|
21036
|
+
}
|
|
21037
|
+
function isShareableResource(schema, resourceName) {
|
|
21038
|
+
const resource = schema.resources.find((entry) => entry.name === resourceName);
|
|
21039
|
+
if (!resource) return false;
|
|
21040
|
+
const resolved = core.resolveCapabilities(schema.capabilities, resource.capabilities);
|
|
21041
|
+
return resolved.some(
|
|
21042
|
+
(entry) => typeof entry === "object" && entry !== null && "shareable" in entry
|
|
21043
|
+
);
|
|
21044
|
+
}
|
|
21045
|
+
function schemaDerivedRelations(schema) {
|
|
21046
|
+
const planned = [];
|
|
21047
|
+
const resourceRole = core.namespaceStorageManifestEntry("resource");
|
|
21048
|
+
schema.resources.forEach((resource, index) => {
|
|
21049
|
+
planned.push({
|
|
21050
|
+
logicalRole: "resource",
|
|
21051
|
+
relation: resource.name,
|
|
21052
|
+
requiredColumns: [
|
|
21053
|
+
"id",
|
|
21054
|
+
...resource.fields.map((field) => field.name),
|
|
21055
|
+
...core.getCapabilityFields(core.resolveCapabilities(schema.capabilities, resource.capabilities)).map((field) => field.name)
|
|
21056
|
+
].map(physicalColumn),
|
|
21057
|
+
copyOrder: resourceRole.copyOrder + index
|
|
21058
|
+
});
|
|
21059
|
+
});
|
|
21060
|
+
const joinRole = core.namespaceStorageManifestEntry("join");
|
|
21061
|
+
const joinTables = /* @__PURE__ */ new Set();
|
|
21062
|
+
for (const relation of schema.relations ?? []) {
|
|
21063
|
+
if (relation.type !== "many-many") continue;
|
|
21064
|
+
const table = core.getRelationJoinTableName(relation);
|
|
21065
|
+
if (joinTables.has(table)) continue;
|
|
21066
|
+
joinTables.add(table);
|
|
21067
|
+
planned.push({
|
|
21068
|
+
logicalRole: "join",
|
|
21069
|
+
relation: table,
|
|
21070
|
+
requiredColumns: [
|
|
21071
|
+
"id",
|
|
21072
|
+
relation.joinColumns?.from ?? "from",
|
|
21073
|
+
relation.joinColumns?.to ?? "to",
|
|
21074
|
+
...(relation.metadata ?? []).map((field) => field.name)
|
|
21075
|
+
].map(physicalColumn),
|
|
21076
|
+
copyOrder: joinRole.copyOrder + joinTables.size
|
|
21077
|
+
});
|
|
21078
|
+
}
|
|
21079
|
+
const legacyRole = core.namespaceStorageManifestEntry("permissions_legacy");
|
|
21080
|
+
schema.resources.forEach((resource, index) => {
|
|
21081
|
+
if (!isShareableResource(schema, resource.name)) return;
|
|
21082
|
+
planned.push({
|
|
21083
|
+
logicalRole: "permissions_legacy",
|
|
21084
|
+
relation: getLegacyPermissionsTable(resource.name),
|
|
21085
|
+
copyOrder: legacyRole.copyOrder + index
|
|
21086
|
+
});
|
|
21087
|
+
});
|
|
21088
|
+
return planned;
|
|
21089
|
+
}
|
|
21090
|
+
function internalPlannedRelations() {
|
|
21091
|
+
return Object.entries(INTERNAL_ROLE_BY_TABLE).map(([relation, logicalRole]) => ({
|
|
21092
|
+
logicalRole,
|
|
21093
|
+
relation,
|
|
21094
|
+
copyOrder: core.namespaceStorageManifestEntry(logicalRole).copyOrder
|
|
21095
|
+
}));
|
|
21096
|
+
}
|
|
21097
|
+
function sharingPlannedRelations() {
|
|
21098
|
+
return SHARING_RESOURCE_TABLES.map((entry) => ({
|
|
21099
|
+
logicalRole: entry.logicalRole,
|
|
21100
|
+
relation: entry.relation,
|
|
21101
|
+
copyOrder: core.namespaceStorageManifestEntry(entry.logicalRole).copyOrder
|
|
21102
|
+
}));
|
|
21103
|
+
}
|
|
21104
|
+
function physicalColumn(name) {
|
|
21105
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
21106
|
+
}
|
|
21107
|
+
var SHARING_REQUIRED_COLUMNS = {
|
|
21108
|
+
permissions_global: ["id", "resource_type", "resource_ns", "resource_id", "principal_id", "level", "grant_kind", "source_ref", "granted_by", "granted_at", "revoked_at"],
|
|
21109
|
+
permissions_legacy: ["id", "resource_id", "user_id", "level", "granted_by", "granted_at", "revoked_at"],
|
|
21110
|
+
principal_memberships: ["id", "namespace", "actor_id", "principal_id", "granted_at", "revoked_at"],
|
|
21111
|
+
principal_hierarchy: ["id", "namespace", "principal_id", "parent_principal_id", "created_at", "revoked_at"]
|
|
21112
|
+
};
|
|
21113
|
+
function toEntry(planned, catalog) {
|
|
21114
|
+
const manifest = core.namespaceStorageManifestEntry(planned.logicalRole);
|
|
21115
|
+
const namespaceColumn = SELECTOR_COLUMN[manifest.selectorKind];
|
|
21116
|
+
const relation = catalog.get(planned.relation);
|
|
21117
|
+
if (!relation) {
|
|
21118
|
+
if (manifest.presence === "schema-derived") {
|
|
21119
|
+
fail(
|
|
21120
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21121
|
+
`Required namespace storage relation "${planned.relation}" is missing from the installed schema.`,
|
|
21122
|
+
{ logicalRole: planned.logicalRole, relation: planned.relation }
|
|
21123
|
+
);
|
|
21124
|
+
}
|
|
21125
|
+
return null;
|
|
21126
|
+
}
|
|
21127
|
+
if (!hasColumn(relation, namespaceColumn)) {
|
|
21128
|
+
fail(
|
|
21129
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21130
|
+
`Namespace storage relation "${planned.relation}" is missing selector column "${namespaceColumn}".`,
|
|
21131
|
+
{
|
|
21132
|
+
logicalRole: planned.logicalRole,
|
|
21133
|
+
relation: planned.relation,
|
|
21134
|
+
expectedColumn: namespaceColumn,
|
|
21135
|
+
columns: relation.columns
|
|
21136
|
+
}
|
|
21137
|
+
);
|
|
21138
|
+
}
|
|
21139
|
+
const required = planned.requiredColumns ?? INTERNAL_TABLE_SCHEMAS[planned.relation]?.map((column) => column.name) ?? SHARING_REQUIRED_COLUMNS[planned.logicalRole] ?? ["id"];
|
|
21140
|
+
const missing = required.filter((column) => !hasColumn(relation, column));
|
|
21141
|
+
if (missing.length > 0) {
|
|
21142
|
+
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing required columns: ${missing.join(", ")}.`);
|
|
21143
|
+
}
|
|
21144
|
+
const hasIdentity = relation.indexes?.some((index) => index.unique && index.primary === true && index.columns.includes("id") && index.columns.every((column) => column === "id" || column === namespaceColumn));
|
|
21145
|
+
if (!hasIdentity) {
|
|
21146
|
+
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing a validated unique identity index.`);
|
|
21147
|
+
}
|
|
21148
|
+
return {
|
|
21149
|
+
logicalRole: planned.logicalRole,
|
|
21150
|
+
ownership: "datafn",
|
|
21151
|
+
relation: planned.relation,
|
|
21152
|
+
quotedRelation: quotePostgresIdentifier(planned.relation),
|
|
21153
|
+
namespaceColumn,
|
|
21154
|
+
quotedNamespaceColumn: quotePostgresIdentifier(namespaceColumn),
|
|
21155
|
+
operations: manifest.operations,
|
|
21156
|
+
copyOrder: planned.copyOrder,
|
|
21157
|
+
present: true
|
|
21158
|
+
};
|
|
21159
|
+
}
|
|
21160
|
+
function isDatafnOwnedCatalogRelation(name, columns) {
|
|
21161
|
+
if (columns.includes("__ns")) return true;
|
|
21162
|
+
return DATAFN_INTERNAL_TABLE_RE.test(name) && columns.includes("namespace");
|
|
21163
|
+
}
|
|
21164
|
+
function unexpectedCatalogRelations(catalog, plannedNames) {
|
|
21165
|
+
const unexpected = [];
|
|
21166
|
+
for (const relation of catalog.relations) {
|
|
21167
|
+
if (plannedNames.has(relation.name)) continue;
|
|
21168
|
+
if (!isDatafnOwnedCatalogRelation(relation.name, relation.columns)) continue;
|
|
21169
|
+
unexpected.push(relation.name);
|
|
21170
|
+
}
|
|
21171
|
+
return unexpected.sort();
|
|
21172
|
+
}
|
|
21173
|
+
function resolveNamespaceStoragePlan(input) {
|
|
21174
|
+
try {
|
|
21175
|
+
core.assertSupportedNamespaceStorageVersions({
|
|
21176
|
+
manifestVersion: input.manifestVersion,
|
|
21177
|
+
schemaVersion: input.schemaVersion
|
|
21178
|
+
});
|
|
21179
|
+
} catch (error) {
|
|
21180
|
+
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "DATAFN_NAMESPACE_STORAGE_VERSION_MISMATCH";
|
|
21181
|
+
fail(
|
|
21182
|
+
code,
|
|
21183
|
+
error instanceof Error ? error.message : String(error)
|
|
21184
|
+
);
|
|
21185
|
+
}
|
|
21186
|
+
if (input.catalog.dialect !== "postgres") {
|
|
21187
|
+
fail(
|
|
21188
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21189
|
+
`Namespace storage plans are supported for PostgreSQL only. Received dialect "${String(input.catalog.dialect)}".`
|
|
21190
|
+
);
|
|
21191
|
+
}
|
|
21192
|
+
if (!core.isNamespaced(input.schema)) {
|
|
21193
|
+
fail(
|
|
21194
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21195
|
+
"Namespace storage plans require a namespaced DataFn schema."
|
|
21196
|
+
);
|
|
21197
|
+
}
|
|
21198
|
+
const catalog = catalogByName(input.catalog);
|
|
21199
|
+
const planned = [
|
|
21200
|
+
...schemaDerivedRelations(input.schema),
|
|
21201
|
+
...sharingPlannedRelations(),
|
|
21202
|
+
...internalPlannedRelations()
|
|
21203
|
+
];
|
|
21204
|
+
for (const relation of input.catalog.relations) {
|
|
21205
|
+
if (LEGACY_PERMISSIONS_TABLE_RE.test(relation.name) && !planned.some((entry) => entry.relation === relation.name)) {
|
|
21206
|
+
planned.push({
|
|
21207
|
+
logicalRole: "permissions_legacy",
|
|
21208
|
+
relation: relation.name,
|
|
21209
|
+
copyOrder: core.namespaceStorageManifestEntry("permissions_legacy").copyOrder + 50
|
|
21210
|
+
});
|
|
21211
|
+
}
|
|
21212
|
+
}
|
|
21213
|
+
const plannedNames = new Set(planned.map((entry) => entry.relation));
|
|
21214
|
+
const unexpected = unexpectedCatalogRelations(input.catalog, plannedNames);
|
|
21215
|
+
if (unexpected.length > 0) {
|
|
21216
|
+
fail(
|
|
21217
|
+
"DATAFN_NAMESPACE_STORAGE_UNKNOWN",
|
|
21218
|
+
`Installed schema contains DataFn namespace storage that is not in the supported contract: ${unexpected.join(", ")}.`,
|
|
21219
|
+
{ relations: unexpected }
|
|
21220
|
+
);
|
|
21221
|
+
}
|
|
21222
|
+
const entries = planned.map((entry) => toEntry(entry, catalog)).filter((entry) => entry !== null).sort((left, right) => {
|
|
21223
|
+
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21224
|
+
return left.relation.localeCompare(right.relation);
|
|
21225
|
+
});
|
|
21226
|
+
return {
|
|
21227
|
+
manifestVersion: core.DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION,
|
|
21228
|
+
schemaVersion: core.DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION,
|
|
21229
|
+
dialect: "postgres",
|
|
21230
|
+
entries
|
|
21231
|
+
};
|
|
21232
|
+
}
|
|
21233
|
+
function selectNamespaceStorageEntries(plan, operation) {
|
|
21234
|
+
const selected = plan.entries.filter((entry) => entry.operations.includes(operation));
|
|
21235
|
+
if (operation === "cleanup") {
|
|
21236
|
+
return [...selected].sort((left, right) => {
|
|
21237
|
+
if (left.copyOrder !== right.copyOrder) return right.copyOrder - left.copyOrder;
|
|
21238
|
+
return right.relation.localeCompare(left.relation);
|
|
21239
|
+
});
|
|
21240
|
+
}
|
|
21241
|
+
return selected;
|
|
21242
|
+
}
|
|
21243
|
+
function composeNamespaceStoragePlan(plan, applicationEntries) {
|
|
21244
|
+
const existing = new Set(plan.entries.map((entry) => entry.relation));
|
|
21245
|
+
const composed = [...plan.entries];
|
|
21246
|
+
for (const entry of applicationEntries) {
|
|
21247
|
+
if (existing.has(entry.relation)) {
|
|
21248
|
+
fail(
|
|
21249
|
+
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21250
|
+
`Application storage relation "${entry.relation}" collides with DataFn-owned storage.`,
|
|
21251
|
+
{ relation: entry.relation }
|
|
21252
|
+
);
|
|
21253
|
+
}
|
|
21254
|
+
existing.add(entry.relation);
|
|
21255
|
+
composed.push({
|
|
21256
|
+
logicalRole: "application",
|
|
21257
|
+
ownership: "application",
|
|
21258
|
+
relation: entry.relation,
|
|
21259
|
+
quotedRelation: quotePostgresIdentifier(entry.relation),
|
|
21260
|
+
namespaceColumn: entry.namespaceColumn,
|
|
21261
|
+
quotedNamespaceColumn: quotePostgresIdentifier(entry.namespaceColumn),
|
|
21262
|
+
operations: entry.operations ?? ["copy", "verify", "cleanup", "fence", "backup"],
|
|
21263
|
+
copyOrder: entry.copyOrder ?? 10,
|
|
21264
|
+
present: true
|
|
21265
|
+
});
|
|
21266
|
+
}
|
|
21267
|
+
composed.sort((left, right) => {
|
|
21268
|
+
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21269
|
+
return left.relation.localeCompare(right.relation);
|
|
21270
|
+
});
|
|
21271
|
+
return { ...plan, entries: composed };
|
|
21272
|
+
}
|
|
21273
|
+
var POSTGRES_NAMESPACE_STORAGE_INDEX_SQL = `
|
|
21274
|
+
SELECT t.relname AS table_name,
|
|
21275
|
+
array_agg(a.attname ORDER BY k.ordinality) AS index_columns,
|
|
21276
|
+
i.indisunique AS index_unique, i.indisprimary AS index_primary
|
|
21277
|
+
FROM pg_index i
|
|
21278
|
+
JOIN pg_class t ON t.oid = i.indrelid
|
|
21279
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
21280
|
+
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ordinality)
|
|
21281
|
+
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
|
21282
|
+
WHERE n.nspname = current_schema() AND i.indisvalid AND i.indisready
|
|
21283
|
+
AND i.indpred IS NULL AND i.indexprs IS NULL
|
|
21284
|
+
AND k.ordinality <= i.indnkeyatts
|
|
21285
|
+
GROUP BY t.relname, i.indexrelid, i.indisunique, i.indisprimary
|
|
21286
|
+
ORDER BY t.relname, i.indexrelid
|
|
21287
|
+
`.trim();
|
|
21288
|
+
async function inspectPostgresNamespaceStorage(query) {
|
|
21289
|
+
const rows = await query(POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL);
|
|
21290
|
+
const indexRows = await query(POSTGRES_NAMESPACE_STORAGE_INDEX_SQL);
|
|
21291
|
+
const relations = /* @__PURE__ */ new Map();
|
|
21292
|
+
for (const row of rows) {
|
|
21293
|
+
if (typeof row.column_name !== "string") fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", "Catalog column metadata is missing.");
|
|
21294
|
+
const columns = relations.get(row.table_name) ?? [];
|
|
21295
|
+
columns.push(row.column_name);
|
|
21296
|
+
relations.set(row.table_name, columns);
|
|
21297
|
+
}
|
|
21298
|
+
return {
|
|
21299
|
+
dialect: "postgres",
|
|
21300
|
+
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 })) }))
|
|
21301
|
+
};
|
|
21302
|
+
}
|
|
21303
|
+
function assertInternalNamespaceStorageMetadataComplete() {
|
|
21304
|
+
const namespaced = listNamespacedInternalTables();
|
|
21305
|
+
const mapped = Object.keys(INTERNAL_ROLE_BY_TABLE).sort();
|
|
21306
|
+
if (namespaced.join("\0") !== mapped.join("\0")) {
|
|
21307
|
+
fail(
|
|
21308
|
+
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21309
|
+
"Internal namespace tables are missing from the storage contract.",
|
|
21310
|
+
{
|
|
21311
|
+
namespacedInternalTables: namespaced,
|
|
21312
|
+
mappedInternalTables: mapped,
|
|
21313
|
+
schemas: Object.keys(INTERNAL_TABLE_SCHEMAS).sort()
|
|
21314
|
+
}
|
|
21315
|
+
);
|
|
21316
|
+
}
|
|
21317
|
+
}
|
|
21318
|
+
async function drainNamespaceStorage(input) {
|
|
21319
|
+
const drainEntries = selectNamespaceStorageEntries(input.plan, "drain");
|
|
21320
|
+
if (drainEntries.length === 0) {
|
|
21321
|
+
return { processed: 0, pending: 0 };
|
|
21322
|
+
}
|
|
21323
|
+
const unsupported = drainEntries.filter(
|
|
21324
|
+
(entry) => entry.logicalRole !== "permission_directory_outbox"
|
|
21325
|
+
);
|
|
21326
|
+
if (unsupported.length > 0) {
|
|
21327
|
+
fail(
|
|
21328
|
+
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21329
|
+
`No drain implementation for logical roles: ${unsupported.map((entry) => entry.logicalRole).join(", ")}.`,
|
|
21330
|
+
{ roles: unsupported.map((entry) => entry.logicalRole) }
|
|
21331
|
+
);
|
|
21332
|
+
}
|
|
21333
|
+
return drainNamespacePermissionDirectoryOutbox(
|
|
21334
|
+
input.adapter,
|
|
21335
|
+
input.namespace,
|
|
21336
|
+
input.runtime,
|
|
21337
|
+
input.logger
|
|
21338
|
+
);
|
|
21339
|
+
}
|
|
21340
|
+
async function resolveAuthoritativeAncestorInactive(adapter, schema, resource, record, namespace, maxGraphNodes) {
|
|
21341
|
+
const active = /* @__PURE__ */ new Set();
|
|
21342
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
21343
|
+
let visited = 0;
|
|
21344
|
+
async function visit(model, row, root = false) {
|
|
21345
|
+
if (typeof row.id !== "string" || row.id.length === 0) {
|
|
21346
|
+
throw new Error("Ancestor repair: malformed record identifier");
|
|
21347
|
+
}
|
|
21348
|
+
const key = JSON.stringify([model, row.id]);
|
|
21349
|
+
if (active.has(key)) throw new Error("Ancestor repair: cycle detected");
|
|
21350
|
+
if (resolved.has(key)) return resolved.get(key);
|
|
21351
|
+
if (++visited > maxGraphNodes) throw new Error("Ancestor repair: maxGraphNodes exceeded");
|
|
21352
|
+
active.add(key);
|
|
21353
|
+
let inherited = false;
|
|
21354
|
+
for (const relation of schema.relations ?? []) {
|
|
21355
|
+
if (relation.inheritsInactive !== true || relation.type === "many-many") continue;
|
|
21356
|
+
const dependent = relation.type === "many-one" ? relation.from : relation.to;
|
|
21357
|
+
if (!core.endpointIncludes(dependent, model)) continue;
|
|
21358
|
+
const endpoint = relation.type === "many-one" ? relation.to : relation.from;
|
|
21359
|
+
const field = relation.type === "many-one" ? core.relationFkFieldForManyOne(relation) : relation.type === "htree" ? relation.fkField || relation.foreignKey || relation.inverse || "parentId" : core.relationFkFieldForOneMany(relation);
|
|
21360
|
+
const parentId = row[field];
|
|
21361
|
+
if (parentId === null || parentId === void 0 || parentId === "") continue;
|
|
21362
|
+
if (typeof parentId !== "string") throw new Error("Ancestor repair: malformed parent link");
|
|
21363
|
+
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`);
|
|
21364
|
+
const discriminator = row[resourceField];
|
|
21365
|
+
const inferred = core.resolveEndpointResource(endpoint, parentId, schema);
|
|
21366
|
+
let parentResource = inferred;
|
|
21367
|
+
if (core.endpointList(endpoint).length > 1 && discriminator != null) {
|
|
21368
|
+
if (typeof discriminator !== "string" || !core.endpointIncludes(endpoint, discriminator) || inferred && inferred !== discriminator) {
|
|
21369
|
+
throw new Error("Ancestor repair: invalid parent resource discriminator");
|
|
21370
|
+
}
|
|
21371
|
+
parentResource = discriminator;
|
|
21372
|
+
}
|
|
21373
|
+
if (!parentResource) throw new Error("Ancestor repair: ambiguous parent resource");
|
|
21374
|
+
const parent = await adapter.findOne({
|
|
21375
|
+
model: parentResource,
|
|
21376
|
+
where: [{ field: "id", operator: "eq", value: parentId }],
|
|
21377
|
+
namespace
|
|
21378
|
+
});
|
|
21379
|
+
if (!parent) throw new Error("Ancestor repair: missing parent record");
|
|
21380
|
+
const parentInactive = await visit(parentResource, parent);
|
|
21381
|
+
inherited = inherited || parentInactive;
|
|
21382
|
+
}
|
|
21383
|
+
active.delete(key);
|
|
21384
|
+
const effective = inherited || row.isArchived === true || row.trashedAt != null;
|
|
21385
|
+
resolved.set(key, effective);
|
|
21386
|
+
return root ? inherited : effective;
|
|
21387
|
+
}
|
|
21388
|
+
return visit(resource, record, true);
|
|
21389
|
+
}
|
|
21390
|
+
|
|
21391
|
+
// src/execution/migration/ancestor-inactive.ts
|
|
21392
|
+
var DEFAULT_BATCH_SIZE = 500;
|
|
21393
|
+
function compareCodePoints(a, b) {
|
|
21394
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
21395
|
+
}
|
|
21396
|
+
function ancestorInactiveResources(schema) {
|
|
21397
|
+
return [...core.getAncestorInactiveResources(schema.relations)].sort(compareCodePoints);
|
|
21398
|
+
}
|
|
21399
|
+
async function recomputeAncestorInactive(adapter, schema, options) {
|
|
21400
|
+
const { namespace, dryRun = false, logger } = options;
|
|
21401
|
+
const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
21402
|
+
const maxGraphNodes = options.maxGraphNodes ?? 1e4;
|
|
21403
|
+
for (const value of [batchSize, maxGraphNodes]) {
|
|
21404
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new Error("Ancestor repair: limits must be positive safe integers");
|
|
21405
|
+
}
|
|
21406
|
+
const resources = ancestorInactiveResources(schema);
|
|
21407
|
+
if (resources.length === 0) return { scanned: 0, updated: 0, skipped: 0, nextCursor: null };
|
|
21408
|
+
let cursor = options.cursor ?? { resource: resources[0], afterId: null };
|
|
21409
|
+
let resourceIndex = resources.indexOf(cursor.resource);
|
|
21410
|
+
if (resourceIndex === -1) {
|
|
21411
|
+
throw new Error(`recomputeAncestorInactive: unknown cursor resource "${cursor.resource}"`);
|
|
21412
|
+
}
|
|
21413
|
+
let scanned = 0;
|
|
21414
|
+
let updated = 0;
|
|
21415
|
+
let skipped = 0;
|
|
21416
|
+
let remaining = batchSize;
|
|
21417
|
+
while (remaining > 0) {
|
|
21418
|
+
const resource = resources[resourceIndex];
|
|
21419
|
+
const requested = remaining;
|
|
21420
|
+
const rows = await adapter.findMany({
|
|
21421
|
+
model: resource,
|
|
21422
|
+
where: cursor.afterId === null ? [] : [{ field: "id", operator: "gt", value: cursor.afterId }],
|
|
21423
|
+
orderBy: [{ field: "id", direction: "asc" }],
|
|
21424
|
+
limit: requested,
|
|
21425
|
+
namespace
|
|
21426
|
+
});
|
|
21427
|
+
for (const row of rows) {
|
|
21428
|
+
const id = row.id;
|
|
21429
|
+
if (typeof id !== "string" || id.length === 0) throw new Error("Ancestor repair: malformed record identifier");
|
|
21430
|
+
scanned += 1;
|
|
21431
|
+
const next = await resolveAuthoritativeAncestorInactive(adapter, schema, resource, row, namespace, maxGraphNodes);
|
|
21432
|
+
const previous = row[core.ANCESTOR_INACTIVE_FIELD];
|
|
21433
|
+
if (previous !== next) {
|
|
21434
|
+
if (dryRun) {
|
|
21435
|
+
updated += 1;
|
|
21436
|
+
} else {
|
|
21437
|
+
const affected = await adapter.updateMany({
|
|
21438
|
+
model: resource,
|
|
21439
|
+
where: [
|
|
21440
|
+
{ field: "id", operator: "eq", value: id },
|
|
21441
|
+
{
|
|
21442
|
+
field: core.ANCESTOR_INACTIVE_FIELD,
|
|
21443
|
+
operator: "eq",
|
|
21444
|
+
value: previous ?? null
|
|
21445
|
+
}
|
|
21446
|
+
],
|
|
21447
|
+
data: { [core.ANCESTOR_INACTIVE_FIELD]: next },
|
|
21448
|
+
namespace
|
|
21449
|
+
});
|
|
21450
|
+
updated += affected > 0 ? 1 : 0;
|
|
21451
|
+
skipped += affected > 0 ? 0 : 1;
|
|
21452
|
+
}
|
|
21453
|
+
}
|
|
21454
|
+
cursor = { resource, afterId: id };
|
|
21455
|
+
}
|
|
21456
|
+
remaining -= rows.length;
|
|
21457
|
+
if (rows.length < requested) {
|
|
21458
|
+
resourceIndex += 1;
|
|
21459
|
+
if (resourceIndex >= resources.length) {
|
|
21460
|
+
logger?.info("datafn.ancestorInactive.recompute.complete", { namespace, scanned, updated, skipped, dryRun });
|
|
21461
|
+
return { scanned, updated, skipped, nextCursor: null };
|
|
21462
|
+
}
|
|
21463
|
+
cursor = { resource: resources[resourceIndex], afterId: null };
|
|
21464
|
+
}
|
|
21465
|
+
}
|
|
21466
|
+
return { scanned, updated, skipped, nextCursor: cursor };
|
|
21467
|
+
}
|
|
21468
|
+
async function recomputeAncestorInactiveAll(adapter, schema, options) {
|
|
21469
|
+
const maxSweeps = options.maxSweeps ?? 32;
|
|
21470
|
+
if (!Number.isSafeInteger(maxSweeps) || maxSweeps < 1) throw new Error("Ancestor repair: maxSweeps must be a positive safe integer");
|
|
21471
|
+
let scanned = 0;
|
|
21472
|
+
let updated = 0;
|
|
21473
|
+
let skipped = 0;
|
|
21474
|
+
let sweeps = 0;
|
|
21475
|
+
let converged = false;
|
|
21476
|
+
while (sweeps < maxSweeps) {
|
|
21477
|
+
sweeps += 1;
|
|
21478
|
+
let sweepUpdated = 0;
|
|
21479
|
+
let sweepSkipped = 0;
|
|
21480
|
+
let cursor = null;
|
|
21481
|
+
do {
|
|
21482
|
+
const result = await recomputeAncestorInactive(adapter, schema, { ...options, cursor });
|
|
21483
|
+
scanned += result.scanned;
|
|
21484
|
+
sweepUpdated += result.updated;
|
|
21485
|
+
sweepSkipped += result.skipped;
|
|
21486
|
+
cursor = result.nextCursor;
|
|
21487
|
+
} while (cursor !== null);
|
|
21488
|
+
updated += sweepUpdated;
|
|
21489
|
+
skipped += sweepSkipped;
|
|
21490
|
+
if (sweepUpdated === 0 && sweepSkipped === 0) {
|
|
21491
|
+
converged = true;
|
|
21492
|
+
break;
|
|
21493
|
+
}
|
|
21494
|
+
if (options.dryRun) break;
|
|
21495
|
+
}
|
|
21496
|
+
if (!converged) {
|
|
21497
|
+
options.logger?.warn("datafn.ancestorInactive.recompute.notConverged", {
|
|
21498
|
+
namespace: options.namespace,
|
|
21499
|
+
sweeps,
|
|
21500
|
+
updated,
|
|
21501
|
+
skipped,
|
|
21502
|
+
dryRun: options.dryRun === true
|
|
21503
|
+
});
|
|
21504
|
+
}
|
|
21505
|
+
return { scanned, updated, skipped, sweeps, converged };
|
|
21506
|
+
}
|
|
20523
21507
|
|
|
21508
|
+
Object.defineProperty(exports, "DATAFN_REQUEST_ACTIONS", {
|
|
21509
|
+
enumerable: true,
|
|
21510
|
+
get: function () { return core.DATAFN_REQUEST_ACTIONS; }
|
|
21511
|
+
});
|
|
21512
|
+
Object.defineProperty(exports, "DATAFN_REQUEST_PROTOCOL_VERSION", {
|
|
21513
|
+
enumerable: true,
|
|
21514
|
+
get: function () { return core.DATAFN_REQUEST_PROTOCOL_VERSION; }
|
|
21515
|
+
});
|
|
21516
|
+
Object.defineProperty(exports, "DATAFN_REQUEST_PROTOCOL_VERSIONS", {
|
|
21517
|
+
enumerable: true,
|
|
21518
|
+
get: function () { return core.DATAFN_REQUEST_PROTOCOL_VERSIONS; }
|
|
21519
|
+
});
|
|
21520
|
+
Object.defineProperty(exports, "DATAFN_STRUCTURAL_SELECTOR_POSITIONS", {
|
|
21521
|
+
enumerable: true,
|
|
21522
|
+
get: function () { return core.DATAFN_STRUCTURAL_SELECTOR_POSITIONS; }
|
|
21523
|
+
});
|
|
21524
|
+
Object.defineProperty(exports, "collectStructuralResourceSelectors", {
|
|
21525
|
+
enumerable: true,
|
|
21526
|
+
get: function () { return core.collectStructuralResourceSelectors; }
|
|
21527
|
+
});
|
|
21528
|
+
Object.defineProperty(exports, "extractStructuralResourceSelectors", {
|
|
21529
|
+
enumerable: true,
|
|
21530
|
+
get: function () { return core.extractStructuralResourceSelectors; }
|
|
21531
|
+
});
|
|
21532
|
+
Object.defineProperty(exports, "isDatafnRequestAction", {
|
|
21533
|
+
enumerable: true,
|
|
21534
|
+
get: function () { return core.isDatafnRequestAction; }
|
|
21535
|
+
});
|
|
21536
|
+
Object.defineProperty(exports, "parseDatafnRequest", {
|
|
21537
|
+
enumerable: true,
|
|
21538
|
+
get: function () { return core.parseDatafnRequest; }
|
|
21539
|
+
});
|
|
20524
21540
|
exports.AtomicSequenceStore = AtomicSequenceStore;
|
|
20525
21541
|
exports.ChainedSequenceStore = ChainedSequenceStore;
|
|
20526
21542
|
exports.DATAFN_MULTI_REGION_CAPABILITY = DATAFN_MULTI_REGION_CAPABILITY;
|
|
@@ -20528,29 +21544,50 @@ exports.DATAFN_ROUTING_ASSERTION_HEADER = DATAFN_ROUTING_ASSERTION_HEADER;
|
|
|
20528
21544
|
exports.DATAFN_ROUTING_INTERNAL_HEADERS = DATAFN_ROUTING_INTERNAL_HEADERS;
|
|
20529
21545
|
exports.DatabaseSequenceStore = DatabaseSequenceStore;
|
|
20530
21546
|
exports.DatafnExecutorError = DatafnExecutorError;
|
|
21547
|
+
exports.DatafnNamespaceStorageError = DatafnNamespaceStorageError;
|
|
20531
21548
|
exports.DatafnPublicLinkInputError = DatafnPublicLinkInputError;
|
|
20532
21549
|
exports.DatafnRoutingError = DatafnRoutingError;
|
|
21550
|
+
exports.POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL = POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL;
|
|
21551
|
+
exports.POSTGRES_NAMESPACE_STORAGE_INDEX_SQL = POSTGRES_NAMESPACE_STORAGE_INDEX_SQL;
|
|
21552
|
+
exports.ancestorInactiveResources = ancestorInactiveResources;
|
|
21553
|
+
exports.assertInternalNamespaceStorageMetadataComplete = assertInternalNamespaceStorageMetadataComplete;
|
|
20533
21554
|
exports.claimDatafnNamespacePlacement = claimDatafnNamespacePlacement;
|
|
21555
|
+
exports.composeNamespaceStoragePlan = composeNamespaceStoragePlan;
|
|
20534
21556
|
exports.createConditionalKvDatafnPlacementDirectory = createConditionalKvDatafnPlacementDirectory;
|
|
20535
21557
|
exports.createDatafnGatewayRouter = createDatafnGatewayRouter;
|
|
21558
|
+
exports.createDatafnHmacRouteTickets = createDatafnHmacRouteTickets;
|
|
20536
21559
|
exports.createDatafnHmacRoutingAssertions = createDatafnHmacRoutingAssertions;
|
|
20537
21560
|
exports.createDatafnMultiRegionPlugin = createDatafnMultiRegionPlugin;
|
|
20538
21561
|
exports.createDatafnPublicLinksPlugin = createDatafnPublicLinksPlugin;
|
|
21562
|
+
exports.createDatafnRouteBootstrap = createDatafnRouteBootstrap;
|
|
20539
21563
|
exports.createDatafnServer = createDatafnServer;
|
|
20540
21564
|
exports.createMemoryDatafnPlacementDirectory = createMemoryDatafnPlacementDirectory;
|
|
20541
21565
|
exports.createMemoryDatafnRoutingReplayStore = createMemoryDatafnRoutingReplayStore;
|
|
20542
21566
|
exports.createSequenceStore = createSequenceStore;
|
|
20543
21567
|
exports.datafn = datafn;
|
|
20544
21568
|
exports.datafnMultiRegionPlugin = datafnMultiRegionPlugin;
|
|
21569
|
+
exports.drainNamespacePermissionDirectoryOutbox = drainNamespacePermissionDirectoryOutbox;
|
|
21570
|
+
exports.drainNamespaceStorage = drainNamespaceStorage;
|
|
21571
|
+
exports.inspectPostgresNamespaceStorage = inspectPostgresNamespaceStorage;
|
|
20545
21572
|
exports.migrateDatafnNamespace = migrateDatafnNamespace;
|
|
21573
|
+
exports.namespacedInternalTableRoles = namespacedInternalTableRoles;
|
|
21574
|
+
exports.quotePostgresIdentifier = quotePostgresIdentifier;
|
|
20546
21575
|
exports.rankDatafnPlacementRegions = rankDatafnPlacementRegions;
|
|
20547
21576
|
exports.readDatafnCloudflarePlacementLocation = readDatafnCloudflarePlacementLocation;
|
|
20548
21577
|
exports.readDatafnPublicLinkToken = readDatafnPublicLinkToken;
|
|
21578
|
+
exports.readDatafnRouteTicket = readDatafnRouteTicket;
|
|
21579
|
+
exports.recomputeAncestorInactive = recomputeAncestorInactive;
|
|
21580
|
+
exports.recomputeAncestorInactiveAll = recomputeAncestorInactiveAll;
|
|
20549
21581
|
exports.resolveDatafnPublicLink = resolveDatafnPublicLink;
|
|
21582
|
+
exports.resolveNamespaceStoragePlan = resolveNamespaceStoragePlan;
|
|
21583
|
+
exports.routeTicketError = routeTicketError;
|
|
20550
21584
|
exports.selectDatafnPlacementRegion = selectDatafnPlacementRegion;
|
|
21585
|
+
exports.selectNamespaceStorageEntries = selectNamespaceStorageEntries;
|
|
20551
21586
|
exports.validateDatafnPlacement = validateDatafnPlacement;
|
|
21587
|
+
exports.validateDatafnRouteTicket = validateDatafnRouteTicket;
|
|
20552
21588
|
exports.validateDatafnRoutingBodyLimit = validateDatafnRoutingBodyLimit;
|
|
20553
21589
|
exports.withDatafnPublicLinksSchema = withDatafnPublicLinksSchema;
|
|
21590
|
+
exports.withDatafnRegionalCors = withDatafnRegionalCors;
|
|
20554
21591
|
exports.withDatafnRoutingAssertion = withDatafnRoutingAssertion;
|
|
20555
21592
|
//# sourceMappingURL=index.cjs.map
|
|
20556
21593
|
//# sourceMappingURL=index.cjs.map
|