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