@datafn/server 0.1.3 → 0.1.4-maint.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -48
- package/dist/index.cjs +124 -1254
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -269
- package/dist/index.d.ts +5 -269
- package/dist/index.js +127 -1202
- package/dist/index.js.map +1 -1
- package/package.json +7 -14
- package/dist/placement.cjs +0 -143
- package/dist/placement.cjs.map +0 -1
- package/dist/placement.d.cts +0 -49
- package/dist/placement.d.ts +0 -49
- package/dist/placement.js +0 -139
- package/dist/placement.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { DATAFN_REQUEST_ACTIONS, DATAFN_REQUEST_PROTOCOL_VERSION, DATAFN_REQUEST_PROTOCOL_VERSIONS, DATAFN_STRUCTURAL_SELECTOR_POSITIONS, collectStructuralResourceSelectors, extractStructuralResourceSelectors, isDatafnRequestAction, parseDatafnRequest } from '@datafn/core';
|
|
1
|
+
import { validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, hasTemporalGrouping, resolveCapabilities, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1, getCapabilityFields, runBeforeHook, getTemporalGroupAliases, getTemporalClauses, parseSortTerm, endpointIncludes, runAfterHook, checkPrototypePollution, validateFieldValue, TIMEZONE_CHANGE_RESOURCE_NAME, createTimezoneResolver, normalizeTemporalQuery, evaluateFilter as evaluateFilter$1, findRelationMatch, endpointList, relationTargetEndpoint, sortRecords, relationKeyFor, getRelationJoinTableName, resolveEndpointResource, resourceNameFromId, normalizeRelationFkRecord, parseSelectToken, getJoinStoreKey, OP_REMAP, parseSortTerms as parseSortTerms$1, getTemporalGroups, resolveTemporalBucketValue, calculateAggregation, normalizeRelationPayload, relationFkFieldForManyOne, firstEndpoint, RELATION_CAPABILITY_FIELD_DEFS, coerceDateFieldsToEpoch } from '@datafn/core';
|
|
3
2
|
import { createRouter, createObservabilityMiddleware, executeMiddlewareChain } from '@superfunctions/http';
|
|
4
3
|
import { wrapWithRowLevelNamespace, instrumentAdapter, instrumentKVStore } from '@superfunctions/db';
|
|
5
4
|
import { normalizeObservability } from '@superfunctions/observability';
|
|
6
|
-
import {
|
|
5
|
+
import { createHash, createHmac, timingSafeEqual, randomUUID } from 'crypto';
|
|
7
6
|
|
|
8
|
-
// src/
|
|
7
|
+
// src/server.ts
|
|
9
8
|
|
|
10
9
|
// src/execution/internal-tables.ts
|
|
11
10
|
var META_COLUMNS = [
|
|
@@ -55,9 +54,6 @@ var INTERNAL_TABLE_SCHEMAS = {
|
|
|
55
54
|
__datafn_seed: SEED_COLUMNS,
|
|
56
55
|
__datafn_permission_directory_outbox: PERMISSION_DIRECTORY_OUTBOX_COLUMNS
|
|
57
56
|
};
|
|
58
|
-
function listNamespacedInternalTables() {
|
|
59
|
-
return Object.entries(INTERNAL_TABLE_SCHEMAS).filter(([, columns]) => columns.some((column) => column.name === "namespace")).map(([name]) => name).sort();
|
|
60
|
-
}
|
|
61
57
|
async function ensureInternalTable(adapter, tableName) {
|
|
62
58
|
const columns = INTERNAL_TABLE_SCHEMAS[tableName];
|
|
63
59
|
if (!columns) {
|
|
@@ -474,8 +470,8 @@ function createSequenceStore(config) {
|
|
|
474
470
|
// src/http/json.ts
|
|
475
471
|
async function parseJsonBody(req) {
|
|
476
472
|
try {
|
|
477
|
-
const
|
|
478
|
-
if (!
|
|
473
|
+
const text = await req.text();
|
|
474
|
+
if (!text || text.trim() === "") {
|
|
479
475
|
return {
|
|
480
476
|
ok: false,
|
|
481
477
|
error: {
|
|
@@ -485,7 +481,7 @@ async function parseJsonBody(req) {
|
|
|
485
481
|
}
|
|
486
482
|
};
|
|
487
483
|
}
|
|
488
|
-
const data = JSON.parse(
|
|
484
|
+
const data = JSON.parse(text);
|
|
489
485
|
return { ok: true, data };
|
|
490
486
|
} catch (error) {
|
|
491
487
|
return {
|
|
@@ -735,13 +731,6 @@ function validateRecordKeys(index, resourceName, record, basePath, mode = "write
|
|
|
735
731
|
for (const key of Object.keys(record)) {
|
|
736
732
|
if (key === "id") continue;
|
|
737
733
|
if (!fields.has(key)) {
|
|
738
|
-
if (mode === "write" && index.fieldsByResource.get(resourceName)?.has(key)) {
|
|
739
|
-
return vErr(
|
|
740
|
-
"DFQL_INVALID",
|
|
741
|
-
`Field is read-only: ${key} on ${resourceName}`,
|
|
742
|
-
`${basePath}.${key}`
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
734
|
return vErr(
|
|
746
735
|
"DFQL_UNKNOWN_FIELD",
|
|
747
736
|
`Unknown field: ${key} on ${resourceName}`,
|
|
@@ -1030,32 +1019,13 @@ function validateMutation(mutation, index, basePath = "$", opts) {
|
|
|
1030
1019
|
`${basePath}.record.${fieldName}`
|
|
1031
1020
|
);
|
|
1032
1021
|
}
|
|
1033
|
-
if (fieldDef.type === "date" && (fieldDef.min !== void 0 || fieldDef.max !== void 0)) {
|
|
1034
|
-
const epoch = toBoundsEpochMs(value);
|
|
1035
|
-
if (Number.isFinite(epoch)) {
|
|
1036
|
-
if (fieldDef.min !== void 0 && epoch < fieldDef.min) {
|
|
1037
|
-
return vErr(
|
|
1038
|
-
"DFQL_INVALID",
|
|
1039
|
-
`Field '${fieldName}' must be on or after ${formatBoundEpochMs(fieldDef.min)}`,
|
|
1040
|
-
`${basePath}.record.${fieldName}`
|
|
1041
|
-
);
|
|
1042
|
-
}
|
|
1043
|
-
if (fieldDef.max !== void 0 && epoch > fieldDef.max) {
|
|
1044
|
-
return vErr(
|
|
1045
|
-
"DFQL_INVALID",
|
|
1046
|
-
`Field '${fieldName}' must be on or before ${formatBoundEpochMs(fieldDef.max)}`,
|
|
1047
|
-
`${basePath}.record.${fieldName}`
|
|
1048
|
-
);
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
1022
|
}
|
|
1053
1023
|
if (m.operation === "insert" || m.operation === "replace") {
|
|
1054
1024
|
const resource2 = resourceResult.result;
|
|
1055
1025
|
const record2 = m.record;
|
|
1056
1026
|
for (const field of resource2.fields) {
|
|
1057
1027
|
if (field.required && field.default === void 0 && field.name !== "id" && !field.readonly) {
|
|
1058
|
-
if (!(field.name in record2) || record2[field.name] ===
|
|
1028
|
+
if (!(field.name in record2) || record2[field.name] === null || record2[field.name] === void 0) {
|
|
1059
1029
|
return vErr(
|
|
1060
1030
|
"DFQL_INVALID",
|
|
1061
1031
|
`Required field missing: ${field.name}`,
|
|
@@ -3303,22 +3273,6 @@ function filterAncestorInactiveRecords(records, match, metadata) {
|
|
|
3303
3273
|
}
|
|
3304
3274
|
return records.filter((record) => record.isAncestorInactive !== true);
|
|
3305
3275
|
}
|
|
3306
|
-
function isPlainRecord(value) {
|
|
3307
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
3308
|
-
return false;
|
|
3309
|
-
}
|
|
3310
|
-
const prototype = Object.getPrototypeOf(value);
|
|
3311
|
-
return prototype === Object.prototype || prototype === null;
|
|
3312
|
-
}
|
|
3313
|
-
function applyOmitToArrayValue(value, omit) {
|
|
3314
|
-
if (Array.isArray(value)) {
|
|
3315
|
-
return value.map((item) => applyOmitToArrayValue(item, omit));
|
|
3316
|
-
}
|
|
3317
|
-
if (isPlainRecord(value)) {
|
|
3318
|
-
return applyOmit(value, omit);
|
|
3319
|
-
}
|
|
3320
|
-
return value;
|
|
3321
|
-
}
|
|
3322
3276
|
function applyOmit(record, omit) {
|
|
3323
3277
|
if (!omit || omit.length === 0) {
|
|
3324
3278
|
return record;
|
|
@@ -3327,8 +3281,13 @@ function applyOmit(record, omit) {
|
|
|
3327
3281
|
for (const [key, value] of Object.entries(record)) {
|
|
3328
3282
|
if (key === "id" || !omit.includes(key)) {
|
|
3329
3283
|
if (Array.isArray(value)) {
|
|
3330
|
-
result[key] = value.map((item) =>
|
|
3331
|
-
|
|
3284
|
+
result[key] = value.map((item) => {
|
|
3285
|
+
if (typeof item === "object" && item !== null) {
|
|
3286
|
+
return applyOmit(item, omit);
|
|
3287
|
+
}
|
|
3288
|
+
return item;
|
|
3289
|
+
});
|
|
3290
|
+
} else if (typeof value === "object" && value !== null && key !== "$relation_metadata") {
|
|
3332
3291
|
result[key] = applyOmit(value, omit);
|
|
3333
3292
|
} else {
|
|
3334
3293
|
result[key] = value;
|
|
@@ -3429,7 +3388,6 @@ function getFkFieldsToOmit(schema, targetResource) {
|
|
|
3429
3388
|
function materializeSelect(record, resource, select, schema, store, omit, metadata) {
|
|
3430
3389
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
3431
3390
|
if (!resourceSchema) return { id: record.id };
|
|
3432
|
-
record = stripNullsForNonNullableFields(record, resourceSchema);
|
|
3433
3391
|
const fkFieldsToOmit = getFkFieldsToOmit(schema, resource);
|
|
3434
3392
|
const effectiveOmit = omit ? [...omit, ...Array.from(fkFieldsToOmit)] : Array.from(fkFieldsToOmit);
|
|
3435
3393
|
if (!select || select.length === 0) {
|
|
@@ -3538,9 +3496,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3538
3496
|
),
|
|
3539
3497
|
htreeRelation,
|
|
3540
3498
|
metadata
|
|
3541
|
-
).map(
|
|
3542
|
-
(c) => applyOmit(stripNullsForNonNullableFields(c, resourceSchema), omit)
|
|
3543
|
-
);
|
|
3499
|
+
).map((c) => applyOmit(c, omit));
|
|
3544
3500
|
} else if (parsed.directive === "**") {
|
|
3545
3501
|
result[parsed.baseName] = filterAncestorInactiveRecords(
|
|
3546
3502
|
htreeChildren(
|
|
@@ -3552,9 +3508,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3552
3508
|
),
|
|
3553
3509
|
htreeRelation,
|
|
3554
3510
|
metadata
|
|
3555
|
-
).map(
|
|
3556
|
-
(d) => applyOmit(stripNullsForNonNullableFields(d, resourceSchema), omit)
|
|
3557
|
-
);
|
|
3511
|
+
).map((d) => applyOmit(d, omit));
|
|
3558
3512
|
}
|
|
3559
3513
|
includedFields.add(parsed.baseName);
|
|
3560
3514
|
} else if (parsed.baseName === "parent") {
|
|
@@ -3563,9 +3517,7 @@ function materializeSelect(record, resource, select, schema, store, omit, metada
|
|
|
3563
3517
|
resource,
|
|
3564
3518
|
htreeRelation.relation,
|
|
3565
3519
|
store
|
|
3566
|
-
).map(
|
|
3567
|
-
(a) => applyOmit(stripNullsForNonNullableFields(a, resourceSchema), omit)
|
|
3568
|
-
);
|
|
3520
|
+
).map((a) => applyOmit(a, omit));
|
|
3569
3521
|
includedFields.add(parsed.baseName);
|
|
3570
3522
|
}
|
|
3571
3523
|
continue;
|
|
@@ -3861,10 +3813,8 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3861
3813
|
}
|
|
3862
3814
|
const aggregations = query.aggregations || {};
|
|
3863
3815
|
let results = [];
|
|
3864
|
-
const samples = /* @__PURE__ */ new WeakMap();
|
|
3865
3816
|
for (const [, groupRecords] of groups.entries()) {
|
|
3866
3817
|
const row = {};
|
|
3867
|
-
samples.set(row, groupRecords[0]);
|
|
3868
3818
|
groupBy.forEach((field) => {
|
|
3869
3819
|
const sample = groupRecords[0];
|
|
3870
3820
|
const val = resolveValue(
|
|
@@ -3875,9 +3825,7 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3875
3825
|
query.resource,
|
|
3876
3826
|
resourceSchema
|
|
3877
3827
|
);
|
|
3878
|
-
|
|
3879
|
-
row[field] = val;
|
|
3880
|
-
}
|
|
3828
|
+
row[field] = val;
|
|
3881
3829
|
});
|
|
3882
3830
|
for (const group of temporalGroups) {
|
|
3883
3831
|
row[group.alias] = resolveTemporalBucketValue(groupRecords[0], group);
|
|
@@ -3917,24 +3865,7 @@ function executeAggregateQuery(query, records, schema, store, temporal) {
|
|
|
3917
3865
|
paginated.length = limit;
|
|
3918
3866
|
}
|
|
3919
3867
|
return {
|
|
3920
|
-
groups: paginated
|
|
3921
|
-
const output = { ...row };
|
|
3922
|
-
for (const field of groupBy) {
|
|
3923
|
-
if (Object.prototype.hasOwnProperty.call(aggregations, field)) continue;
|
|
3924
|
-
if (output[field] === null && resolveValue(
|
|
3925
|
-
samples.get(row),
|
|
3926
|
-
field,
|
|
3927
|
-
schema,
|
|
3928
|
-
store,
|
|
3929
|
-
query.resource,
|
|
3930
|
-
resourceSchema,
|
|
3931
|
-
true
|
|
3932
|
-
) === void 0) {
|
|
3933
|
-
delete output[field];
|
|
3934
|
-
}
|
|
3935
|
-
}
|
|
3936
|
-
return output;
|
|
3937
|
-
}),
|
|
3868
|
+
groups: paginated,
|
|
3938
3869
|
nextCursor
|
|
3939
3870
|
};
|
|
3940
3871
|
}
|
|
@@ -3963,10 +3894,9 @@ function orderGroupedResults(groups, sortTerms) {
|
|
|
3963
3894
|
return 0;
|
|
3964
3895
|
});
|
|
3965
3896
|
}
|
|
3966
|
-
function resolveValue(record, path, schema, store, resourceName, precomputedResource
|
|
3897
|
+
function resolveValue(record, path, schema, store, resourceName, precomputedResource) {
|
|
3967
3898
|
if (!path.includes(".")) {
|
|
3968
|
-
|
|
3969
|
-
return normalize && record[path] === null && definition2 && definition2.nullable !== true ? void 0 : record[path];
|
|
3899
|
+
return record[path];
|
|
3970
3900
|
}
|
|
3971
3901
|
const parts = path.split(".");
|
|
3972
3902
|
const resource = precomputedResource ?? schema.resources.find((r) => r.name === resourceName);
|
|
@@ -4004,8 +3934,7 @@ function resolveValue(record, path, schema, store, resourceName, precomputedReso
|
|
|
4004
3934
|
}
|
|
4005
3935
|
}
|
|
4006
3936
|
const lastField = parts[parts.length - 1];
|
|
4007
|
-
|
|
4008
|
-
return normalize && currentRecord[lastField] === null && definition && definition.nullable !== true ? void 0 : currentRecord[lastField];
|
|
3937
|
+
return currentRecord[lastField];
|
|
4009
3938
|
}
|
|
4010
3939
|
var DFQL_TO_ADAPTER_OP = {
|
|
4011
3940
|
// $-prefixed DFQL operators
|
|
@@ -5235,7 +5164,6 @@ async function pushdownCore(query, schema, db, namespace) {
|
|
|
5235
5164
|
function projectFieldsPushdown(record, resource, schema, select, omit) {
|
|
5236
5165
|
const resourceSchema = schema.resources.find((r) => r.name === resource);
|
|
5237
5166
|
if (!resourceSchema) return { id: record.id };
|
|
5238
|
-
record = stripNullsForNonNullableFields(record, resourceSchema);
|
|
5239
5167
|
const resolvedCapabilities = resolveCapabilities(
|
|
5240
5168
|
schema.capabilities,
|
|
5241
5169
|
resourceSchema.capabilities
|
|
@@ -6004,213 +5932,6 @@ var AuthResolverCache = class {
|
|
|
6004
5932
|
function createAuthResolverCache() {
|
|
6005
5933
|
return new AuthResolverCache();
|
|
6006
5934
|
}
|
|
6007
|
-
var scopes = /* @__PURE__ */ new Set([
|
|
6008
|
-
"query",
|
|
6009
|
-
"search",
|
|
6010
|
-
"mutation",
|
|
6011
|
-
"transact",
|
|
6012
|
-
"seed",
|
|
6013
|
-
"clone",
|
|
6014
|
-
"pull",
|
|
6015
|
-
"push",
|
|
6016
|
-
"reconcile",
|
|
6017
|
-
"custom",
|
|
6018
|
-
"websocket"
|
|
6019
|
-
]);
|
|
6020
|
-
var text = (value) => typeof value === "string" && value.trim().length > 0 && value.length <= 1024;
|
|
6021
|
-
function routeTicketError(code) {
|
|
6022
|
-
return new DatafnRoutingError({
|
|
6023
|
-
code,
|
|
6024
|
-
message: code,
|
|
6025
|
-
status: code === "DATAFN_ROUTE_RATE_LIMITED" ? 429 : code === "DATAFN_ROUTE_FORBIDDEN" ? 403 : 401,
|
|
6026
|
-
retryable: code === "DATAFN_ROUTE_TICKET_EXPIRED" || code === "DATAFN_ROUTE_RATE_LIMITED",
|
|
6027
|
-
executionStarted: false
|
|
6028
|
-
});
|
|
6029
|
-
}
|
|
6030
|
-
function assertClaims(value) {
|
|
6031
|
-
if (!value || value.version !== 1 || ![value.issuer, value.audience, value.subject, value.namespace, value.regionId, value.ticketId].every(text) || !Number.isSafeInteger(value.epoch) || value.epoch < 1 || ![value.issuedAt, value.notBefore, value.expiresAt].every(Number.isSafeInteger) || value.notBefore < value.issuedAt || value.notBefore >= value.expiresAt || value.expiresAt - value.issuedAt > DATAFN_ROUTE_MAX_TTL_MS || !Array.isArray(value.scopes) || value.scopes.length === 0 || value.scopes.some((scope) => !scopes.has(scope)) || new Set(value.scopes).size !== value.scopes.length || value.sessionBinding !== void 0 && !text(value.sessionBinding)) {
|
|
6032
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6033
|
-
}
|
|
6034
|
-
}
|
|
6035
|
-
function createDatafnHmacRouteTickets(input) {
|
|
6036
|
-
const keys = new Map(Object.entries(input.keys).map(([id, key]) => {
|
|
6037
|
-
if (!/^[A-Za-z0-9_-]{1,128}$/.test(id) || Buffer.byteLength(key) < 32) {
|
|
6038
|
-
throw new Error("DATAFN_ROUTE_KEY_INVALID");
|
|
6039
|
-
}
|
|
6040
|
-
return [id, Buffer.from(key)];
|
|
6041
|
-
}));
|
|
6042
|
-
if (!keys.has(input.activeKeyId)) throw new Error("DATAFN_ROUTE_ACTIVE_KEY_MISSING");
|
|
6043
|
-
const signature = (content, key) => createHmac("sha256", key).update(`datafn-regional-route-v1:${content}`).digest();
|
|
6044
|
-
return {
|
|
6045
|
-
sign(claims) {
|
|
6046
|
-
assertClaims(claims);
|
|
6047
|
-
const header = Buffer.from(JSON.stringify({ typ: "datafn-route-v1", alg: "HS256", kid: input.activeKeyId })).toString("base64url");
|
|
6048
|
-
const payload = Buffer.from(JSON.stringify(claims)).toString("base64url");
|
|
6049
|
-
const content = `${header}.${payload}`;
|
|
6050
|
-
return `${content}.${signature(content, keys.get(input.activeKeyId)).toString("base64url")}`;
|
|
6051
|
-
},
|
|
6052
|
-
verify(ticket) {
|
|
6053
|
-
try {
|
|
6054
|
-
if (typeof ticket !== "string" || ticket.length > 16384 || !/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(ticket)) throw new Error();
|
|
6055
|
-
const [header, payload, mac] = ticket.split(".");
|
|
6056
|
-
const protectedHeader = JSON.parse(Buffer.from(header, "base64url").toString());
|
|
6057
|
-
const key = keys.get(protectedHeader.kid);
|
|
6058
|
-
if (!key || protectedHeader.typ !== "datafn-route-v1" || protectedHeader.alg !== "HS256") throw new Error();
|
|
6059
|
-
const expected = signature(`${header}.${payload}`, key);
|
|
6060
|
-
const actual = Buffer.from(mac, "base64url");
|
|
6061
|
-
if (actual.length !== expected.length || !timingSafeEqual(actual, expected)) throw new Error();
|
|
6062
|
-
const claims = JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
6063
|
-
assertClaims(claims);
|
|
6064
|
-
return claims;
|
|
6065
|
-
} catch {
|
|
6066
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6067
|
-
}
|
|
6068
|
-
}
|
|
6069
|
-
};
|
|
6070
|
-
}
|
|
6071
|
-
function createDatafnRouteBootstrap(config) {
|
|
6072
|
-
const ttl = config.ttlMs ?? 6e4;
|
|
6073
|
-
if (!text(config.issuer) || !Number.isSafeInteger(ttl) || ttl < 1e3 || ttl > DATAFN_ROUTE_MAX_TTL_MS) {
|
|
6074
|
-
throw new Error("DATAFN_ROUTE_BOOTSTRAP_CONFIG_INVALID");
|
|
6075
|
-
}
|
|
6076
|
-
const now = config.now ?? Date.now;
|
|
6077
|
-
return async (request) => {
|
|
6078
|
-
try {
|
|
6079
|
-
if (request.method !== "POST") return new Response(null, { status: 405, headers: { allow: "POST", "cache-control": "no-store" } });
|
|
6080
|
-
const identity = await config.authenticate(request);
|
|
6081
|
-
if (!identity || !text(identity.subject) || !text(identity.namespace)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6082
|
-
let placement;
|
|
6083
|
-
try {
|
|
6084
|
-
placement = await config.directory.get(identity.namespace);
|
|
6085
|
-
} catch {
|
|
6086
|
-
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_UNAVAILABLE", message: "Placement unavailable", status: 503, retryable: true });
|
|
6087
|
-
}
|
|
6088
|
-
if (!placement || placement.namespace !== identity.namespace || placement.state === "tombstoned") {
|
|
6089
|
-
throw new DatafnRoutingError({ code: "DATAFN_PLACEMENT_NOT_FOUND", message: "Placement not found", status: 404, retryable: false });
|
|
6090
|
-
}
|
|
6091
|
-
if (placement.state !== "active" || placement.migration) {
|
|
6092
|
-
throw new DatafnRoutingError({ code: "DATAFN_NAMESPACE_MOVING", message: "Placement is fenced", status: 409, retryable: true });
|
|
6093
|
-
}
|
|
6094
|
-
const permitted = [...await config.authorize(identity)];
|
|
6095
|
-
const endpoint = await config.resolveEndpoint(placement);
|
|
6096
|
-
const issuedAt = now();
|
|
6097
|
-
const expiresAt = Math.min(issuedAt + ttl, identity.expiresAt ?? Infinity);
|
|
6098
|
-
if (!Number.isSafeInteger(expiresAt) || expiresAt <= issuedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6099
|
-
const httpUrl = validateDatafnRegionalEndpoint(endpoint.httpUrl);
|
|
6100
|
-
const wsUrl = endpoint.wsUrl ? validateDatafnRegionalEndpoint(endpoint.wsUrl, true) : void 0;
|
|
6101
|
-
if (permitted.includes("websocket") && !wsUrl) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6102
|
-
const ticket = await config.signer.sign({
|
|
6103
|
-
version: 1,
|
|
6104
|
-
issuer: config.issuer,
|
|
6105
|
-
audience: endpoint.audience,
|
|
6106
|
-
subject: identity.subject,
|
|
6107
|
-
namespace: identity.namespace,
|
|
6108
|
-
regionId: placement.regionId,
|
|
6109
|
-
epoch: placement.epoch,
|
|
6110
|
-
scopes: permitted,
|
|
6111
|
-
issuedAt,
|
|
6112
|
-
notBefore: issuedAt,
|
|
6113
|
-
expiresAt,
|
|
6114
|
-
ticketId: randomUUID(),
|
|
6115
|
-
...identity.sessionBinding ? { sessionBinding: identity.sessionBinding } : {}
|
|
6116
|
-
});
|
|
6117
|
-
const descriptor = {
|
|
6118
|
-
version: 1,
|
|
6119
|
-
httpUrl,
|
|
6120
|
-
...wsUrl ? { wsUrl } : {},
|
|
6121
|
-
ticket,
|
|
6122
|
-
expiresAt,
|
|
6123
|
-
renewAfter: issuedAt + Math.floor((expiresAt - issuedAt) * 0.8)
|
|
6124
|
-
};
|
|
6125
|
-
emit(config, { type: "issued" });
|
|
6126
|
-
return Response.json(descriptor, { headers: { "cache-control": "no-store", pragma: "no-cache" } });
|
|
6127
|
-
} catch (error) {
|
|
6128
|
-
emit(config, { type: "rejected", code: error instanceof DatafnRoutingError ? error.code : "DATAFN_ROUTE_FORBIDDEN" });
|
|
6129
|
-
return (error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_FORBIDDEN")).toResponse();
|
|
6130
|
-
}
|
|
6131
|
-
};
|
|
6132
|
-
}
|
|
6133
|
-
function readDatafnRouteTicket(request, websocket = false) {
|
|
6134
|
-
const header = request.headers.get(DATAFN_ROUTE_TICKET_HEADER);
|
|
6135
|
-
const protocols = (request.headers.get("sec-websocket-protocol") ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
6136
|
-
const tickets = protocols.filter((s) => s.startsWith(DATAFN_ROUTE_WS_TICKET_PREFIX));
|
|
6137
|
-
if (tickets.length > 1 || header && tickets.length || !websocket && tickets.length || tickets.length && !protocols.includes(DATAFN_ROUTE_WS_PROTOCOL)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6138
|
-
return header ?? tickets[0]?.slice(DATAFN_ROUTE_WS_TICKET_PREFIX.length);
|
|
6139
|
-
}
|
|
6140
|
-
async function validateDatafnRouteTicket(input) {
|
|
6141
|
-
const { runtime } = input;
|
|
6142
|
-
try {
|
|
6143
|
-
const origin = input.request.headers.get("origin");
|
|
6144
|
-
if (origin && !runtime.allowedOrigins?.includes(origin)) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6145
|
-
const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
|
|
6146
|
-
if (!token) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6147
|
-
const claims = await runtime.verifier.verify(token);
|
|
6148
|
-
assertClaims(claims);
|
|
6149
|
-
const now = (runtime.now ?? Date.now)();
|
|
6150
|
-
const skew = runtime.clockSkewMs ?? 5e3;
|
|
6151
|
-
if (!Number.isFinite(skew) || skew < 0 || skew > 3e4) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6152
|
-
if (claims.expiresAt <= now) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6153
|
-
if (claims.issuedAt > now + skew || claims.notBefore > now + skew || claims.issuer !== runtime.issuer || claims.audience !== runtime.audience || claims.regionId !== input.regionId || claims.namespace !== input.namespace || !claims.scopes.includes(input.scope)) throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6154
|
-
const identity = await runtime.authenticate(input.request);
|
|
6155
|
-
if (!identity || identity.subject !== claims.subject || identity.namespace !== claims.namespace || identity.sessionBinding !== claims.sessionBinding || identity.expiresAt !== void 0 && (!Number.isFinite(identity.expiresAt) || identity.expiresAt <= (runtime.now ?? Date.now)())) {
|
|
6156
|
-
throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6157
|
-
}
|
|
6158
|
-
if (identity.expiresAt !== void 0 && claims.expiresAt > identity.expiresAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6159
|
-
if (runtime.isActive && !await runtime.isActive(claims)) throw routeTicketError("DATAFN_ROUTE_TICKET_REVOKED");
|
|
6160
|
-
if (runtime.allowRequest && !await runtime.allowRequest(claims)) throw routeTicketError("DATAFN_ROUTE_RATE_LIMITED");
|
|
6161
|
-
const admittedAt = (runtime.now ?? Date.now)();
|
|
6162
|
-
if (identity.expiresAt !== void 0 && identity.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_FORBIDDEN");
|
|
6163
|
-
if (claims.expiresAt <= admittedAt) throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6164
|
-
emit(runtime, { type: "accepted" });
|
|
6165
|
-
return claims;
|
|
6166
|
-
} catch (error) {
|
|
6167
|
-
const safe = error instanceof DatafnRoutingError ? error : routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6168
|
-
emit(runtime, { type: "rejected", code: safe.code });
|
|
6169
|
-
throw safe;
|
|
6170
|
-
}
|
|
6171
|
-
}
|
|
6172
|
-
function withDatafnRegionalCors(handler, options) {
|
|
6173
|
-
const origins = new Set(options.origins.map((origin) => {
|
|
6174
|
-
if (new URL(origin).origin !== origin || origin === "null") throw new Error("DATAFN_ROUTE_ORIGIN_INVALID");
|
|
6175
|
-
return origin;
|
|
6176
|
-
}));
|
|
6177
|
-
const allowedHeaders = /* @__PURE__ */ new Set([
|
|
6178
|
-
"content-type",
|
|
6179
|
-
"authorization",
|
|
6180
|
-
DATAFN_ROUTE_TICKET_HEADER,
|
|
6181
|
-
...(options.headers ?? []).map((header) => header.toLowerCase())
|
|
6182
|
-
]);
|
|
6183
|
-
const allowedMethods = ["GET", "POST", "PATCH", "DELETE"];
|
|
6184
|
-
return async (request) => {
|
|
6185
|
-
const origin = request.headers.get("origin");
|
|
6186
|
-
if (origin && !origins.has(origin)) return routeTicketError("DATAFN_ROUTE_FORBIDDEN").toResponse();
|
|
6187
|
-
if (request.method === "OPTIONS") {
|
|
6188
|
-
const method = request.headers.get("access-control-request-method");
|
|
6189
|
-
const headers2 = (request.headers.get("access-control-request-headers") ?? "").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
6190
|
-
if (!origin || !allowedMethods.includes(method ?? "") || headers2.some((h) => !allowedHeaders.has(h))) return new Response(null, { status: 403 });
|
|
6191
|
-
return new Response(null, { status: 204, headers: {
|
|
6192
|
-
"access-control-allow-origin": origin,
|
|
6193
|
-
"access-control-allow-methods": allowedMethods.join(", "),
|
|
6194
|
-
"access-control-allow-headers": [...allowedHeaders].join(", "),
|
|
6195
|
-
vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
|
|
6196
|
-
"cache-control": "no-store"
|
|
6197
|
-
} });
|
|
6198
|
-
}
|
|
6199
|
-
const response = await handler(request);
|
|
6200
|
-
const headers = new Headers(response.headers);
|
|
6201
|
-
headers.append("vary", "Origin");
|
|
6202
|
-
headers.delete("access-control-allow-credentials");
|
|
6203
|
-
headers.set("cache-control", "no-store");
|
|
6204
|
-
if (origin) headers.set("access-control-allow-origin", origin);
|
|
6205
|
-
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
6206
|
-
};
|
|
6207
|
-
}
|
|
6208
|
-
function emit(config, event) {
|
|
6209
|
-
try {
|
|
6210
|
-
config.onEvent?.(event);
|
|
6211
|
-
} catch {
|
|
6212
|
-
}
|
|
6213
|
-
}
|
|
6214
5935
|
var DATAFN_ROUTING_ASSERTION_HEADER = "x-datafn-routing-assertion";
|
|
6215
5936
|
var DATAFN_ROUTING_INTERNAL_HEADERS = [
|
|
6216
5937
|
DATAFN_ROUTING_ASSERTION_HEADER,
|
|
@@ -6362,28 +6083,6 @@ async function claimDatafnNamespacePlacement(input) {
|
|
|
6362
6083
|
async function validateDatafnPlacement(input) {
|
|
6363
6084
|
const startedAt = Date.now();
|
|
6364
6085
|
const requestId2 = input.request?.headers.get("x-request-id") ?? void 0;
|
|
6365
|
-
let ticket;
|
|
6366
|
-
const hasAssertion = Boolean(input.request?.headers.get(input.runtime.assertionHeader ?? DATAFN_ROUTING_ASSERTION_HEADER)?.trim());
|
|
6367
|
-
if (input.request) {
|
|
6368
|
-
const token = readDatafnRouteTicket(input.request, input.scope === "websocket");
|
|
6369
|
-
if (token !== void 0 && (hasAssertion || !input.runtime.routeTickets)) {
|
|
6370
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6371
|
-
}
|
|
6372
|
-
if (input.runtime.routeTickets && (!hasAssertion || token !== void 0)) {
|
|
6373
|
-
if (DATAFN_ROUTING_INTERNAL_HEADERS.some((name) => input.request.headers.has(name))) {
|
|
6374
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6375
|
-
}
|
|
6376
|
-
ticket = await validateDatafnRouteTicket({
|
|
6377
|
-
request: input.request,
|
|
6378
|
-
namespace: input.namespace,
|
|
6379
|
-
regionId: input.regionId,
|
|
6380
|
-
scope: input.scope ?? "custom",
|
|
6381
|
-
runtime: input.runtime.routeTickets
|
|
6382
|
-
});
|
|
6383
|
-
}
|
|
6384
|
-
} else if (input.runtime.routeTickets && !input.trustedInternal) {
|
|
6385
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_INVALID");
|
|
6386
|
-
}
|
|
6387
6086
|
let placement;
|
|
6388
6087
|
try {
|
|
6389
6088
|
placement = await input.runtime.directory.get(input.namespace);
|
|
@@ -6421,7 +6120,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6421
6120
|
retryable: false
|
|
6422
6121
|
});
|
|
6423
6122
|
}
|
|
6424
|
-
const assertion =
|
|
6123
|
+
const assertion = await validateRoutingAssertion({
|
|
6425
6124
|
namespace: input.namespace,
|
|
6426
6125
|
regionId: input.regionId,
|
|
6427
6126
|
placement,
|
|
@@ -6477,7 +6176,7 @@ async function validateDatafnPlacement(input) {
|
|
|
6477
6176
|
internal: Boolean(assertion)
|
|
6478
6177
|
});
|
|
6479
6178
|
}
|
|
6480
|
-
if (assertion && assertion.epoch !== placement.epoch
|
|
6179
|
+
if (assertion && assertion.epoch !== placement.epoch) {
|
|
6481
6180
|
await emitRoutingEvent(input.runtime, {
|
|
6482
6181
|
type: "mismatch",
|
|
6483
6182
|
requestId: requestId2,
|
|
@@ -6490,17 +6189,14 @@ async function validateDatafnPlacement(input) {
|
|
|
6490
6189
|
});
|
|
6491
6190
|
throw new DatafnRoutingError({
|
|
6492
6191
|
code: "DATAFN_REGION_MISMATCH",
|
|
6493
|
-
message: "Routing
|
|
6192
|
+
message: "Routing assertion uses a stale placement epoch",
|
|
6494
6193
|
status: 409,
|
|
6495
6194
|
retryable: true,
|
|
6496
6195
|
placement,
|
|
6497
|
-
internal:
|
|
6196
|
+
internal: true
|
|
6498
6197
|
});
|
|
6499
6198
|
}
|
|
6500
|
-
|
|
6501
|
-
throw routeTicketError("DATAFN_ROUTE_TICKET_EXPIRED");
|
|
6502
|
-
}
|
|
6503
|
-
return { placement, ...assertion ? { assertion } : {}, ...ticket ? { ticket } : {} };
|
|
6199
|
+
return { placement, ...assertion ? { assertion } : {} };
|
|
6504
6200
|
}
|
|
6505
6201
|
function createDatafnGatewayRouter(config) {
|
|
6506
6202
|
const maxBodyBytes = validateDatafnRoutingBodyLimit(config.maxBodyBytes);
|
|
@@ -8456,8 +8152,9 @@ async function recordChangesWithRetry(changeTracking, entries, maxRetries = 2, l
|
|
|
8456
8152
|
}
|
|
8457
8153
|
}
|
|
8458
8154
|
}
|
|
8155
|
+
var preparedNamespaces = /* @__PURE__ */ new WeakMap();
|
|
8459
8156
|
var ChangeTrackingService = class _ChangeTrackingService {
|
|
8460
|
-
constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false) {
|
|
8157
|
+
constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false, namespaceMetaEnsured = false) {
|
|
8461
8158
|
this.db = db;
|
|
8462
8159
|
this.namespace = namespace;
|
|
8463
8160
|
this.sequenceStore = sequenceStore;
|
|
@@ -8465,8 +8162,10 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8465
8162
|
this.logger = logger;
|
|
8466
8163
|
this.metaEnsured = false;
|
|
8467
8164
|
this.changesEnsured = false;
|
|
8165
|
+
this.namespaceMetaEnsured = false;
|
|
8468
8166
|
this.metaEnsured = metaEnsured;
|
|
8469
8167
|
this.changesEnsured = changesEnsured;
|
|
8168
|
+
this.namespaceMetaEnsured = namespaceMetaEnsured;
|
|
8470
8169
|
}
|
|
8471
8170
|
/** Initialize every durable table before an enclosing transaction begins. */
|
|
8472
8171
|
async ensureReady() {
|
|
@@ -8481,6 +8180,48 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8481
8180
|
await ensureInternalTable(this.db, "__datafn_meta");
|
|
8482
8181
|
this.metaEnsured = true;
|
|
8483
8182
|
}
|
|
8183
|
+
await this.ensureNamespaceMeta();
|
|
8184
|
+
}
|
|
8185
|
+
/**
|
|
8186
|
+
* Creates this namespace's `__datafn_meta` row before an enclosing
|
|
8187
|
+
* transaction begins. Sequence allocation otherwise creates the row inside
|
|
8188
|
+
* the transaction on a namespace's first write, and when two first writes
|
|
8189
|
+
* race, the losing INSERT fails with a unique-key conflict. On PostgreSQL that
|
|
8190
|
+
* aborts the whole transaction, so the recovery read that follows fails with
|
|
8191
|
+
* "current transaction is aborted". Outside a transaction the conflict is
|
|
8192
|
+
* harmless: the row exists either way, and later allocations only read and
|
|
8193
|
+
* CAS-update it. Transaction-bound instances (from `withDb`) never prepare
|
|
8194
|
+
* the row; the base instance does so before the transaction opens.
|
|
8195
|
+
*/
|
|
8196
|
+
async ensureNamespaceMeta() {
|
|
8197
|
+
if (this.namespaceMetaEnsured) {
|
|
8198
|
+
return;
|
|
8199
|
+
}
|
|
8200
|
+
let prepared = preparedNamespaces.get(this.db);
|
|
8201
|
+
if (!prepared) {
|
|
8202
|
+
prepared = /* @__PURE__ */ new Set();
|
|
8203
|
+
preparedNamespaces.set(this.db, prepared);
|
|
8204
|
+
}
|
|
8205
|
+
if (prepared.has(this.namespace)) {
|
|
8206
|
+
this.namespaceMetaEnsured = true;
|
|
8207
|
+
return;
|
|
8208
|
+
}
|
|
8209
|
+
const where = [{ field: "namespace", op: "eq", value: this.namespace }];
|
|
8210
|
+
if (!await this.db.internal.findOne("__datafn_meta", where)) {
|
|
8211
|
+
try {
|
|
8212
|
+
await this.db.internal.create("__datafn_meta", {
|
|
8213
|
+
id: `meta:${this.namespace}`,
|
|
8214
|
+
namespace: this.namespace,
|
|
8215
|
+
next_server_seq: 1
|
|
8216
|
+
});
|
|
8217
|
+
} catch (error) {
|
|
8218
|
+
if (!await this.db.internal.findOne("__datafn_meta", where)) {
|
|
8219
|
+
throw error;
|
|
8220
|
+
}
|
|
8221
|
+
}
|
|
8222
|
+
}
|
|
8223
|
+
prepared.add(this.namespace);
|
|
8224
|
+
this.namespaceMetaEnsured = true;
|
|
8484
8225
|
}
|
|
8485
8226
|
/**
|
|
8486
8227
|
* REL-011: Create a new ChangeTrackingService with a different db adapter.
|
|
@@ -8496,7 +8237,8 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8496
8237
|
emitChanges ? this.onChange : void 0,
|
|
8497
8238
|
this.logger,
|
|
8498
8239
|
this.metaEnsured,
|
|
8499
|
-
this.changesEnsured
|
|
8240
|
+
this.changesEnsured,
|
|
8241
|
+
true
|
|
8500
8242
|
);
|
|
8501
8243
|
}
|
|
8502
8244
|
/**
|
|
@@ -8931,16 +8673,12 @@ function buildReplaceRecord(schema, resourceName, existingRecord, newRecord, res
|
|
|
8931
8673
|
result[key] = existingRecord[key];
|
|
8932
8674
|
continue;
|
|
8933
8675
|
}
|
|
8934
|
-
if (field.readonly) {
|
|
8935
|
-
result[key] = Object.prototype.hasOwnProperty.call(newRecord, key) ? newRecord[key] : existingRecord[key];
|
|
8936
|
-
continue;
|
|
8937
|
-
}
|
|
8938
8676
|
if (Object.prototype.hasOwnProperty.call(newRecord, key)) {
|
|
8939
8677
|
result[key] = newRecord[key];
|
|
8940
8678
|
} else {
|
|
8941
8679
|
result[key] = field.default !== void 0 ? field.default : null;
|
|
8942
8680
|
}
|
|
8943
|
-
if (field.required && (result[key] ===
|
|
8681
|
+
if (field.required && (result[key] === null || result[key] === void 0)) {
|
|
8944
8682
|
return {
|
|
8945
8683
|
ok: false,
|
|
8946
8684
|
code: "DFQL_INVALID",
|
|
@@ -9027,7 +8765,8 @@ function isRecordEffectivelyInactive(record) {
|
|
|
9027
8765
|
return isRecordInactive(record) || record?.isAncestorInactive === true;
|
|
9028
8766
|
}
|
|
9029
8767
|
function resourceSupportsAncestorInactive(schema, resource) {
|
|
9030
|
-
|
|
8768
|
+
const resourceSchema = schema.resources.find((candidate) => candidate.name === resource);
|
|
8769
|
+
return resourceSchema?.fields.some((field) => field.name === "isAncestorInactive") === true;
|
|
9031
8770
|
}
|
|
9032
8771
|
function dependentEndpoint(relation) {
|
|
9033
8772
|
return relation.type === "many-one" ? relation.from : relation.to;
|
|
@@ -10446,7 +10185,7 @@ function decideMergeMode(schema, resource, id, delta) {
|
|
|
10446
10185
|
for (const field of resourceDef.fields) {
|
|
10447
10186
|
if (!field.required) continue;
|
|
10448
10187
|
if (field.name === "id" || field.name === "__ns") continue;
|
|
10449
|
-
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 &&
|
|
10188
|
+
const hasProvided = Object.prototype.hasOwnProperty.call(delta, field.name) && delta[field.name] !== void 0 && delta[field.name] !== null;
|
|
10450
10189
|
const hasDefault = field.default !== void 0;
|
|
10451
10190
|
if (!hasProvided && !hasDefault) return { mode: "not_found" };
|
|
10452
10191
|
}
|
|
@@ -12185,64 +11924,6 @@ async function drainPermissionDirectoryOutbox(db, runtime, logger, limit = 100)
|
|
|
12185
11924
|
}
|
|
12186
11925
|
return { processed, pending: tasks.length - processed };
|
|
12187
11926
|
}
|
|
12188
|
-
async function drainNamespacePermissionDirectoryOutbox(db, namespace, runtime, logger, options = {}) {
|
|
12189
|
-
await ensurePermissionDirectoryOutbox(db);
|
|
12190
|
-
const limit = options.limit ?? 100;
|
|
12191
|
-
const maxRounds = options.maxRounds ?? 32;
|
|
12192
|
-
let processed = 0;
|
|
12193
|
-
for (let round = 0; round < maxRounds; round += 1) {
|
|
12194
|
-
const tasks = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12195
|
-
{ field: "namespace", op: "eq", value: namespace },
|
|
12196
|
-
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12197
|
-
{ field: "next_attempt_at", op: "lte", value: (/* @__PURE__ */ new Date()).toISOString() }
|
|
12198
|
-
], {
|
|
12199
|
-
orderBy: "next_attempt_at",
|
|
12200
|
-
limit
|
|
12201
|
-
});
|
|
12202
|
-
if (tasks.length === 0) {
|
|
12203
|
-
break;
|
|
12204
|
-
}
|
|
12205
|
-
let roundProcessed = 0;
|
|
12206
|
-
for (const task of tasks) {
|
|
12207
|
-
const taskId = String(task.id);
|
|
12208
|
-
const selectedNextAttemptAt = String(task.next_attempt_at);
|
|
12209
|
-
const claimedNextAttemptAt = new Date(
|
|
12210
|
-
Date.now() + DRAIN_CLAIM_LEASE_MS
|
|
12211
|
-
).toISOString();
|
|
12212
|
-
const claimed = await db.internal.update(OUTBOX_TABLE, [
|
|
12213
|
-
{ field: "id", op: "eq", value: taskId },
|
|
12214
|
-
{ field: "namespace", op: "eq", value: namespace },
|
|
12215
|
-
{ field: "region_id", op: "eq", value: runtime.regionId },
|
|
12216
|
-
{ field: "next_attempt_at", op: "eq", value: selectedNextAttemptAt }
|
|
12217
|
-
], {
|
|
12218
|
-
next_attempt_at: claimedNextAttemptAt
|
|
12219
|
-
});
|
|
12220
|
-
if (claimed === 0) continue;
|
|
12221
|
-
if (await drainPermissionDirectorySync(
|
|
12222
|
-
db,
|
|
12223
|
-
taskId,
|
|
12224
|
-
runtime,
|
|
12225
|
-
logger,
|
|
12226
|
-
{ expectedNextAttemptAt: claimedNextAttemptAt }
|
|
12227
|
-
)) {
|
|
12228
|
-
processed += 1;
|
|
12229
|
-
roundProcessed += 1;
|
|
12230
|
-
}
|
|
12231
|
-
}
|
|
12232
|
-
if (roundProcessed === 0) {
|
|
12233
|
-
const remaining2 = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12234
|
-
{ field: "namespace", op: "eq", value: namespace },
|
|
12235
|
-
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12236
|
-
], { limit: 1 });
|
|
12237
|
-
return { processed, pending: remaining2.length };
|
|
12238
|
-
}
|
|
12239
|
-
}
|
|
12240
|
-
const remaining = await db.internal.findMany(OUTBOX_TABLE, [
|
|
12241
|
-
{ field: "namespace", op: "eq", value: namespace },
|
|
12242
|
-
{ field: "region_id", op: "eq", value: runtime.regionId }
|
|
12243
|
-
], { limit: 1 });
|
|
12244
|
-
return { processed, pending: remaining.length };
|
|
12245
|
-
}
|
|
12246
11927
|
|
|
12247
11928
|
// src/execution/mutation/execute.ts
|
|
12248
11929
|
var FAILED_SHARE_COMPENSATION_RESULT = /* @__PURE__ */ Symbol("failedShareCompensation");
|
|
@@ -12344,23 +12025,6 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12344
12025
|
let additionalChanges = [];
|
|
12345
12026
|
let skipChangeTracking = false;
|
|
12346
12027
|
let failedShareCompensation = null;
|
|
12347
|
-
if ((mutation.operation === "insert" || mutation.operation === "merge" || mutation.operation === "replace") && mutation.record) {
|
|
12348
|
-
const systemField = findSystemFieldWrite(schema.relations, mutation.resource, mutation.record);
|
|
12349
|
-
if (systemField) {
|
|
12350
|
-
return {
|
|
12351
|
-
ok: false,
|
|
12352
|
-
mutationId: effectiveMutationId,
|
|
12353
|
-
affectedIds: [],
|
|
12354
|
-
errors: [{
|
|
12355
|
-
code: "DFQL_INVALID",
|
|
12356
|
-
message: `Field is read-only: ${systemField} on ${mutation.resource}`,
|
|
12357
|
-
path: `record.${systemField}`,
|
|
12358
|
-
retryable: false
|
|
12359
|
-
}],
|
|
12360
|
-
deduped: false
|
|
12361
|
-
};
|
|
12362
|
-
}
|
|
12363
|
-
}
|
|
12364
12028
|
try {
|
|
12365
12029
|
switch (mutation.operation) {
|
|
12366
12030
|
case "insert":
|
|
@@ -14820,7 +14484,7 @@ function compareCanonicalChangeEntries(a, b) {
|
|
|
14820
14484
|
function mergeCanonicalChangeStreamsBounded(streams, limit) {
|
|
14821
14485
|
const heads = streams.filter((stream) => stream.changes.length > 0).map((stream) => ({ resource: stream.resource, changes: stream.changes, index: 0 }));
|
|
14822
14486
|
const merged = [];
|
|
14823
|
-
const latestCursorPerResource =
|
|
14487
|
+
const latestCursorPerResource = {};
|
|
14824
14488
|
let peakWindow = heads.length;
|
|
14825
14489
|
while (heads.length > 0 && merged.length < limit) {
|
|
14826
14490
|
let bestIdx = 0;
|
|
@@ -15052,11 +14716,11 @@ async function executePullCanonical(request, schema, db, namespace, sequenceStor
|
|
|
15052
14716
|
const requestedResourceKeys = Object.keys(request.cursors).filter(
|
|
15053
14717
|
(resource) => resource !== ACTOR_FEED_CURSOR_KEY
|
|
15054
14718
|
);
|
|
15055
|
-
const records =
|
|
15056
|
-
const merged =
|
|
15057
|
-
const deleted =
|
|
15058
|
-
const joins =
|
|
15059
|
-
const cursors =
|
|
14719
|
+
const records = {};
|
|
14720
|
+
const merged = {};
|
|
14721
|
+
const deleted = {};
|
|
14722
|
+
const joins = {};
|
|
14723
|
+
const cursors = {};
|
|
15060
14724
|
for (const resource of requestedResourceKeys) {
|
|
15061
14725
|
if (!resource.startsWith("join_") && !resource.startsWith("__datafn_")) {
|
|
15062
14726
|
records[resource] = [];
|
|
@@ -15459,17 +15123,6 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15459
15123
|
const batchLocalSeen = /* @__PURE__ */ new Map();
|
|
15460
15124
|
async function executeMutationOp(mut, targetDb) {
|
|
15461
15125
|
const resolvedCapabilities = resolveCapabilitiesForResource2(schema, mut.resource);
|
|
15462
|
-
if ((mut.operation === "insert" || mut.operation === "merge" || mut.operation === "replace") && typeof mut.record === "object" && mut.record !== null) {
|
|
15463
|
-
const systemField = findSystemFieldWrite(schema.relations, mut.resource, mut.record);
|
|
15464
|
-
if (systemField) {
|
|
15465
|
-
return {
|
|
15466
|
-
ok: false,
|
|
15467
|
-
code: "DFQL_INVALID",
|
|
15468
|
-
message: `Field is read-only: ${systemField} on ${mut.resource}`,
|
|
15469
|
-
path: `record.${systemField}`
|
|
15470
|
-
};
|
|
15471
|
-
}
|
|
15472
|
-
}
|
|
15473
15126
|
switch (mut.operation) {
|
|
15474
15127
|
case "insert": {
|
|
15475
15128
|
const coercedData = applyDateCoercion(schema, mut.resource, mut.record || {}, logger);
|
|
@@ -18000,7 +17653,6 @@ var WebSocketManager = class {
|
|
|
18000
17653
|
* Reverse mapping: client → known principalIds for O(1) targeted index cleanup.
|
|
18001
17654
|
*/
|
|
18002
17655
|
this.clientPrincipals = /* @__PURE__ */ new Map();
|
|
18003
|
-
this.routeTickets = /* @__PURE__ */ new Map();
|
|
18004
17656
|
this.clientPlacement = /* @__PURE__ */ new Map();
|
|
18005
17657
|
/** Monotonic namespace fence generation closes the validation/registration race. */
|
|
18006
17658
|
this.namespaceFenceGeneration = /* @__PURE__ */ new Map();
|
|
@@ -18152,11 +17804,6 @@ var WebSocketManager = class {
|
|
|
18152
17804
|
*/
|
|
18153
17805
|
addClient(client, authContext) {
|
|
18154
17806
|
const { namespace } = authContext;
|
|
18155
|
-
if (this.clientNamespace.has(client)) return false;
|
|
18156
|
-
if (authContext.routeTicket && authContext.routeTicket.expiresAt <= (authContext.routeTicket.now ?? Date.now)()) {
|
|
18157
|
-
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18158
|
-
return false;
|
|
18159
|
-
}
|
|
18160
17807
|
if (this.closed) {
|
|
18161
17808
|
this.closeClientSafely(client, 1001, "Going Away", "ws-closed-admission");
|
|
18162
17809
|
return false;
|
|
@@ -18190,12 +17837,6 @@ var WebSocketManager = class {
|
|
|
18190
17837
|
...authContext.regionId ? { regionId: authContext.regionId } : {},
|
|
18191
17838
|
...Number.isSafeInteger(authContext.routingEpoch) ? { epoch: authContext.routingEpoch } : {}
|
|
18192
17839
|
});
|
|
18193
|
-
if (authContext.routeTicket) {
|
|
18194
|
-
const { id, expiresAt, now = Date.now } = authContext.routeTicket;
|
|
18195
|
-
const timer = setTimeout(() => this.expireRouteClient(client), Math.max(0, expiresAt - now()));
|
|
18196
|
-
timer.unref?.();
|
|
18197
|
-
this.routeTickets.set(client, { id, expiresAt, now, timer });
|
|
18198
|
-
}
|
|
18199
17840
|
if (this.heartbeatTimer === null) {
|
|
18200
17841
|
this.startHeartbeat();
|
|
18201
17842
|
}
|
|
@@ -18204,24 +17845,7 @@ var WebSocketManager = class {
|
|
|
18204
17845
|
/**
|
|
18205
17846
|
* Handle disconnection.
|
|
18206
17847
|
*/
|
|
18207
|
-
expireRouteClient(client) {
|
|
18208
|
-
this.removeClient(client);
|
|
18209
|
-
this.closeClientSafely(client, 4511, "DATAFN_ROUTE_TICKET_EXPIRED", "ws-ticket-expired");
|
|
18210
|
-
}
|
|
18211
|
-
revokeRouteTicket(ticketId) {
|
|
18212
|
-
let count = 0;
|
|
18213
|
-
for (const [client, ticket] of this.routeTickets) {
|
|
18214
|
-
if (ticket.id !== ticketId) continue;
|
|
18215
|
-
this.removeClient(client);
|
|
18216
|
-
this.closeClientSafely(client, 4403, "DATAFN_ROUTE_TICKET_REVOKED", "ws-ticket-revoked");
|
|
18217
|
-
count++;
|
|
18218
|
-
}
|
|
18219
|
-
return count;
|
|
18220
|
-
}
|
|
18221
17848
|
removeClient(client) {
|
|
18222
|
-
const ticket = this.routeTickets.get(client);
|
|
18223
|
-
if (ticket) clearTimeout(ticket.timer);
|
|
18224
|
-
this.routeTickets.delete(client);
|
|
18225
17849
|
const namespace = this.clientNamespace.get(client);
|
|
18226
17850
|
if (namespace !== void 0) {
|
|
18227
17851
|
this.removeTargetingIndexes(client, namespace);
|
|
@@ -18338,16 +17962,9 @@ var WebSocketManager = class {
|
|
|
18338
17962
|
principals: mode === "targeted" ? Array.from(requestedPrincipals).sort() : void 0
|
|
18339
17963
|
}
|
|
18340
17964
|
});
|
|
18341
|
-
let sent = 0;
|
|
18342
17965
|
for (const client of recipients) {
|
|
18343
|
-
const ticket = this.routeTickets.get(client);
|
|
18344
|
-
if (ticket && ticket.expiresAt <= ticket.now()) {
|
|
18345
|
-
this.expireRouteClient(client);
|
|
18346
|
-
continue;
|
|
18347
|
-
}
|
|
18348
17966
|
try {
|
|
18349
17967
|
client.send(msg);
|
|
18350
|
-
sent++;
|
|
18351
17968
|
} catch (e) {
|
|
18352
17969
|
this.logger?.error("Error sending to websocket client", { error: String(e), operation: "ws-broadcast" });
|
|
18353
17970
|
this.removeClient(client);
|
|
@@ -18357,7 +17974,7 @@ var WebSocketManager = class {
|
|
|
18357
17974
|
return {
|
|
18358
17975
|
mode,
|
|
18359
17976
|
degraded,
|
|
18360
|
-
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" :
|
|
17977
|
+
wokenClients: mode === "namespace-broadcast" ? "all-in-namespace" : recipients.size
|
|
18361
17978
|
};
|
|
18362
17979
|
}
|
|
18363
17980
|
/**
|
|
@@ -18376,8 +17993,6 @@ var WebSocketManager = class {
|
|
|
18376
17993
|
this.clientNamespace.clear();
|
|
18377
17994
|
this.clientPrincipals.clear();
|
|
18378
17995
|
this.clientPlacement.clear();
|
|
18379
|
-
for (const ticket of this.routeTickets.values()) clearTimeout(ticket.timer);
|
|
18380
|
-
this.routeTickets.clear();
|
|
18381
17996
|
this.pendingPong.clear();
|
|
18382
17997
|
}
|
|
18383
17998
|
/**
|
|
@@ -18584,6 +18199,35 @@ function relationReferencesInternalResource(relation, internalResourceNames) {
|
|
|
18584
18199
|
function normalizeResourceEndpoint(endpoint) {
|
|
18585
18200
|
return typeof endpoint === "string" ? [endpoint] : [...endpoint];
|
|
18586
18201
|
}
|
|
18202
|
+
function payloadReferencesInternalResource(payload, internalResourceNames) {
|
|
18203
|
+
if (internalResourceNames.size === 0) {
|
|
18204
|
+
return false;
|
|
18205
|
+
}
|
|
18206
|
+
if (Array.isArray(payload)) {
|
|
18207
|
+
return payload.some((entry) => payloadReferencesInternalResource(entry, internalResourceNames));
|
|
18208
|
+
}
|
|
18209
|
+
if (!payload || typeof payload !== "object") {
|
|
18210
|
+
return false;
|
|
18211
|
+
}
|
|
18212
|
+
const record = payload;
|
|
18213
|
+
if (typeof record.resource === "string" && internalResourceNames.has(record.resource)) {
|
|
18214
|
+
return true;
|
|
18215
|
+
}
|
|
18216
|
+
if (Array.isArray(record.resources) && record.resources.some((resource) => typeof resource === "string" && internalResourceNames.has(resource))) {
|
|
18217
|
+
return true;
|
|
18218
|
+
}
|
|
18219
|
+
for (const key of ["mutations", "queries", "steps", "operations"]) {
|
|
18220
|
+
if (Array.isArray(record[key]) && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18221
|
+
return true;
|
|
18222
|
+
}
|
|
18223
|
+
}
|
|
18224
|
+
for (const key of ["query", "mutation", "payload"]) {
|
|
18225
|
+
if (record[key] && payloadReferencesInternalResource(record[key], internalResourceNames)) {
|
|
18226
|
+
return true;
|
|
18227
|
+
}
|
|
18228
|
+
}
|
|
18229
|
+
return false;
|
|
18230
|
+
}
|
|
18587
18231
|
function normalizePluginAuthorizationError(error, pluginName) {
|
|
18588
18232
|
if (error && typeof error === "object") {
|
|
18589
18233
|
const candidate = error;
|
|
@@ -19182,10 +18826,10 @@ async function createDatafnServer(config) {
|
|
|
19182
18826
|
}
|
|
19183
18827
|
return { ok: true };
|
|
19184
18828
|
};
|
|
19185
|
-
const withAuth = (action, handler
|
|
18829
|
+
const withAuth = (action, handler) => {
|
|
19186
18830
|
return async (req, ctx) => {
|
|
19187
18831
|
const enrichedCtx = createEnrichedContext(ctx);
|
|
19188
|
-
const placementRequest = multiRegionRuntime?.placement
|
|
18832
|
+
const placementRequest = multiRegionRuntime?.placement ? req.clone() : void 0;
|
|
19189
18833
|
let payload = null;
|
|
19190
18834
|
if (shuttingDown) {
|
|
19191
18835
|
return completeDatafnResponse({
|
|
@@ -19314,36 +18958,7 @@ async function createDatafnServer(config) {
|
|
|
19314
18958
|
}
|
|
19315
18959
|
enrichedCtx.parsedBody = payload;
|
|
19316
18960
|
}
|
|
19317
|
-
|
|
19318
|
-
if (rest) {
|
|
19319
|
-
let resource;
|
|
19320
|
-
try {
|
|
19321
|
-
const segment = sanitizePathSegment(new URL(req.url).pathname.split("/")[3] ?? "");
|
|
19322
|
-
if (!segment.ok) throw new Error("Invalid path segment");
|
|
19323
|
-
resource = segment.value;
|
|
19324
|
-
} catch {
|
|
19325
|
-
return completeDatafnResponse({
|
|
19326
|
-
action,
|
|
19327
|
-
request: req,
|
|
19328
|
-
context: enrichedCtx,
|
|
19329
|
-
payload,
|
|
19330
|
-
response: errorResponse({ code: "DFQL_INVALID", message: "Invalid path segment", details: { path: "resource" } }, 400)
|
|
19331
|
-
});
|
|
19332
|
-
}
|
|
19333
|
-
structuralPayload = { resource };
|
|
19334
|
-
}
|
|
19335
|
-
const parsedProtocol = parseDatafnRequest(action, structuralPayload, { schema: validatedSchema });
|
|
19336
|
-
if (!parsedProtocol.ok) {
|
|
19337
|
-
return completeDatafnResponse({
|
|
19338
|
-
action,
|
|
19339
|
-
request: req,
|
|
19340
|
-
context: enrichedCtx,
|
|
19341
|
-
payload,
|
|
19342
|
-
response: errorResponse(parsedProtocol.error, 400)
|
|
19343
|
-
});
|
|
19344
|
-
}
|
|
19345
|
-
const selection = collectStructuralResourceSelectors(parsedProtocol.result);
|
|
19346
|
-
if (selection.selectors.some((resource) => internalResourceNames.has(resource))) {
|
|
18961
|
+
if (payloadReferencesInternalResource(payload, internalResourceNames)) {
|
|
19347
18962
|
await emitDataFnEvent({
|
|
19348
18963
|
domain: "datafn",
|
|
19349
18964
|
type: "datafn.authorization.denied",
|
|
@@ -19369,9 +18984,7 @@ async function createDatafnServer(config) {
|
|
|
19369
18984
|
namespace: await extractNamespace(enrichedCtx),
|
|
19370
18985
|
regionId: multiRegionRuntime.regionId,
|
|
19371
18986
|
runtime: multiRegionRuntime.placement,
|
|
19372
|
-
request: placementRequest
|
|
19373
|
-
scope: action,
|
|
19374
|
-
trustedInternal: origin === "executor"
|
|
18987
|
+
request: placementRequest
|
|
19375
18988
|
});
|
|
19376
18989
|
} catch (error) {
|
|
19377
18990
|
if (error instanceof DatafnRoutingError) {
|
|
@@ -19390,7 +19003,7 @@ async function createDatafnServer(config) {
|
|
|
19390
19003
|
action,
|
|
19391
19004
|
request: req,
|
|
19392
19005
|
context: enrichedCtx,
|
|
19393
|
-
payload
|
|
19006
|
+
payload
|
|
19394
19007
|
});
|
|
19395
19008
|
if (!pluginAuthorization.ok) {
|
|
19396
19009
|
await emitDataFnEvent({
|
|
@@ -19410,7 +19023,7 @@ async function createDatafnServer(config) {
|
|
|
19410
19023
|
});
|
|
19411
19024
|
}
|
|
19412
19025
|
if (config.authorize) {
|
|
19413
|
-
const authorized = await config.authorize(enrichedCtx, action,
|
|
19026
|
+
const authorized = await config.authorize(enrichedCtx, action, payload);
|
|
19414
19027
|
if (!authorized) {
|
|
19415
19028
|
await emitDataFnEvent({
|
|
19416
19029
|
domain: "datafn",
|
|
@@ -19485,7 +19098,7 @@ async function createDatafnServer(config) {
|
|
|
19485
19098
|
try {
|
|
19486
19099
|
const configuredContext = config.context;
|
|
19487
19100
|
const executorContext = context ?? (typeof configuredContext === "function" ? await configuredContext(request) : configuredContext ?? {});
|
|
19488
|
-
response = await withAuth(action, handler
|
|
19101
|
+
response = await withAuth(action, handler)(request, executorContext);
|
|
19489
19102
|
} catch {
|
|
19490
19103
|
throw new DatafnExecutorError(
|
|
19491
19104
|
{ code: "INTERNAL", message: "Internal Server Error" },
|
|
@@ -19675,9 +19288,7 @@ async function createDatafnServer(config) {
|
|
|
19675
19288
|
method: route.method,
|
|
19676
19289
|
path: route.path,
|
|
19677
19290
|
meta: route.meta,
|
|
19678
|
-
|
|
19679
|
-
decodeParams: false,
|
|
19680
|
-
handler: withAuth(action, route.handler, "http", true)
|
|
19291
|
+
handler: withAuth(action, route.handler)
|
|
19681
19292
|
});
|
|
19682
19293
|
}
|
|
19683
19294
|
}
|
|
@@ -19849,18 +19460,12 @@ async function createDatafnServer(config) {
|
|
|
19849
19460
|
regionId: multiRegionRuntime.regionId,
|
|
19850
19461
|
runtime: multiRegionRuntime.placement,
|
|
19851
19462
|
request: handshakeRequest,
|
|
19852
|
-
|
|
19853
|
-
trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion && !multiRegionRuntime.placement.routeTickets
|
|
19463
|
+
trustedInternal: !handshakeRequest && !multiRegionRuntime.placement.requireRoutingAssertion
|
|
19854
19464
|
});
|
|
19855
19465
|
added = wsManager.addClient(client, {
|
|
19856
19466
|
...authContext,
|
|
19857
19467
|
regionId: validated.placement.regionId,
|
|
19858
|
-
routingEpoch: validated.placement.epoch
|
|
19859
|
-
routeTicket: validated.ticket ? {
|
|
19860
|
-
id: validated.ticket.ticketId,
|
|
19861
|
-
expiresAt: validated.ticket.expiresAt,
|
|
19862
|
-
now: multiRegionRuntime.placement.routeTickets?.now
|
|
19863
|
-
} : void 0
|
|
19468
|
+
routingEpoch: validated.placement.epoch
|
|
19864
19469
|
});
|
|
19865
19470
|
if (!added) return false;
|
|
19866
19471
|
if (wsManager.getNamespaceFenceGeneration(authContext.namespace) !== fenceGeneration) {
|
|
@@ -19879,7 +19484,7 @@ async function createDatafnServer(config) {
|
|
|
19879
19484
|
if (added) wsManager.removeClient(client);
|
|
19880
19485
|
try {
|
|
19881
19486
|
client.close(
|
|
19882
|
-
error instanceof DatafnRoutingError ?
|
|
19487
|
+
error instanceof DatafnRoutingError ? 4510 : 1011,
|
|
19883
19488
|
error instanceof Error ? error.message : "Placement validation failed"
|
|
19884
19489
|
);
|
|
19885
19490
|
} catch {
|
|
@@ -19887,7 +19492,6 @@ async function createDatafnServer(config) {
|
|
|
19887
19492
|
return false;
|
|
19888
19493
|
}
|
|
19889
19494
|
},
|
|
19890
|
-
revokeRouteTicket: (ticketId) => wsManager.revokeRouteTicket(ticketId),
|
|
19891
19495
|
fenceNamespace: (namespace, minimumEpoch) => wsManager.fenceNamespace(namespace, minimumEpoch),
|
|
19892
19496
|
removeClient: (client) => wsManager.removeClient(client),
|
|
19893
19497
|
handleMessage: (client, data) => wsManager.handleMessage(client, data),
|
|
@@ -20825,685 +20429,6 @@ function bytesToBase64Url(bytes) {
|
|
|
20825
20429
|
return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
20826
20430
|
}
|
|
20827
20431
|
|
|
20828
|
-
|
|
20829
|
-
var CONTINENT_CENTERS = {
|
|
20830
|
-
AF: [1.65, 17.68],
|
|
20831
|
-
AN: [-82.86, 135],
|
|
20832
|
-
AS: [34.05, 100.62],
|
|
20833
|
-
EU: [54.53, 15.26],
|
|
20834
|
-
NA: [54.53, -105.26],
|
|
20835
|
-
OC: [-22.74, 140.02],
|
|
20836
|
-
SA: [-14.24, -51.93]
|
|
20837
|
-
};
|
|
20838
|
-
function finiteCoordinate(value, minimum, maximum) {
|
|
20839
|
-
return value !== void 0 && Number.isFinite(value) && value >= minimum && value <= maximum;
|
|
20840
|
-
}
|
|
20841
|
-
function normalizedCandidates(input) {
|
|
20842
|
-
const context = {
|
|
20843
|
-
location: input.location ?? null,
|
|
20844
|
-
preferredRegionId: normalizedPreferredRegionId(input.preferredRegionId)
|
|
20845
|
-
};
|
|
20846
|
-
const seen = /* @__PURE__ */ new Set();
|
|
20847
|
-
const candidates = input.candidates.map((candidate) => {
|
|
20848
|
-
const regionId = candidate.regionId.trim();
|
|
20849
|
-
if (!regionId) throw new Error("DATAFN_PLACEMENT_REGION_ID_REQUIRED");
|
|
20850
|
-
if (seen.has(regionId)) {
|
|
20851
|
-
throw new Error(`DATAFN_PLACEMENT_REGION_DUPLICATE: ${regionId}`);
|
|
20852
|
-
}
|
|
20853
|
-
seen.add(regionId);
|
|
20854
|
-
return regionId === candidate.regionId ? candidate : { ...candidate, regionId };
|
|
20855
|
-
});
|
|
20856
|
-
return candidates.filter(
|
|
20857
|
-
(candidate) => (input.constraints ?? []).every(
|
|
20858
|
-
(constraint) => constraint(candidate, context)
|
|
20859
|
-
)
|
|
20860
|
-
);
|
|
20861
|
-
}
|
|
20862
|
-
function normalizedPreferredRegionId(preferredRegionId) {
|
|
20863
|
-
return preferredRegionId?.trim() || null;
|
|
20864
|
-
}
|
|
20865
|
-
function compareRegionIds(left, right) {
|
|
20866
|
-
return left < right ? -1 : left > right ? 1 : 0;
|
|
20867
|
-
}
|
|
20868
|
-
function sourceCoordinates(location) {
|
|
20869
|
-
if (!location) return null;
|
|
20870
|
-
if (finiteCoordinate(location.latitude, -90, 90) && finiteCoordinate(location.longitude, -180, 180)) {
|
|
20871
|
-
return {
|
|
20872
|
-
coordinates: [location.latitude, location.longitude],
|
|
20873
|
-
source: "coordinates"
|
|
20874
|
-
};
|
|
20875
|
-
}
|
|
20876
|
-
const continent = location.continent?.toUpperCase();
|
|
20877
|
-
const coordinates = continent ? CONTINENT_CENTERS[continent] : void 0;
|
|
20878
|
-
return coordinates ? { coordinates, source: "continent" } : null;
|
|
20879
|
-
}
|
|
20880
|
-
function radians(degrees) {
|
|
20881
|
-
return degrees * Math.PI / 180;
|
|
20882
|
-
}
|
|
20883
|
-
function distanceKilometers(source, target) {
|
|
20884
|
-
const sourceLatitude = radians(source[0]);
|
|
20885
|
-
const targetLatitude = radians(target[0]);
|
|
20886
|
-
const latitudeDelta = radians(target[0] - source[0]);
|
|
20887
|
-
const longitudeDelta = radians(target[1] - source[1]);
|
|
20888
|
-
const haversine = Math.sin(latitudeDelta / 2) ** 2 + Math.cos(sourceLatitude) * Math.cos(targetLatitude) * Math.sin(longitudeDelta / 2) ** 2;
|
|
20889
|
-
const clampedHaversine = Math.min(1, Math.max(0, haversine));
|
|
20890
|
-
return 6371 * 2 * Math.atan2(Math.sqrt(clampedHaversine), Math.sqrt(1 - clampedHaversine));
|
|
20891
|
-
}
|
|
20892
|
-
function rankDatafnPlacementRegions(input) {
|
|
20893
|
-
const candidates = normalizedCandidates(input);
|
|
20894
|
-
const source = sourceCoordinates(input.location ?? null);
|
|
20895
|
-
if (!source) return [];
|
|
20896
|
-
return candidates.flatMap((candidate) => {
|
|
20897
|
-
const { latitude, longitude } = candidate;
|
|
20898
|
-
return finiteCoordinate(latitude, -90, 90) && finiteCoordinate(longitude, -180, 180) ? [
|
|
20899
|
-
{
|
|
20900
|
-
regionId: candidate.regionId,
|
|
20901
|
-
source: source.source,
|
|
20902
|
-
distanceKilometers: distanceKilometers(source.coordinates, [
|
|
20903
|
-
latitude,
|
|
20904
|
-
longitude
|
|
20905
|
-
])
|
|
20906
|
-
}
|
|
20907
|
-
] : [];
|
|
20908
|
-
}).sort(
|
|
20909
|
-
(left, right) => (left.distanceKilometers ?? 0) - (right.distanceKilometers ?? 0) || compareRegionIds(left.regionId, right.regionId)
|
|
20910
|
-
);
|
|
20911
|
-
}
|
|
20912
|
-
function stableIndex(key, length) {
|
|
20913
|
-
let hash = 2166136261;
|
|
20914
|
-
for (const byte of new TextEncoder().encode(key)) {
|
|
20915
|
-
hash ^= byte;
|
|
20916
|
-
hash = Math.imul(hash, 16777619) >>> 0;
|
|
20917
|
-
}
|
|
20918
|
-
return hash % length;
|
|
20919
|
-
}
|
|
20920
|
-
function selectDatafnPlacementRegion(input) {
|
|
20921
|
-
const candidates = normalizedCandidates(input);
|
|
20922
|
-
const preferredRegionId = normalizedPreferredRegionId(
|
|
20923
|
-
input.preferredRegionId
|
|
20924
|
-
);
|
|
20925
|
-
if (preferredRegionId && candidates.some((candidate) => candidate.regionId === preferredRegionId)) {
|
|
20926
|
-
return { regionId: preferredRegionId, source: "preferred" };
|
|
20927
|
-
}
|
|
20928
|
-
const ranked = rankDatafnPlacementRegions({
|
|
20929
|
-
candidates,
|
|
20930
|
-
location: input.location ?? null
|
|
20931
|
-
});
|
|
20932
|
-
const nearest = ranked[0];
|
|
20933
|
-
if (nearest) return nearest;
|
|
20934
|
-
if (input.stableKey !== void 0 && candidates.length > 0) {
|
|
20935
|
-
const stableCandidates = [...candidates].sort(
|
|
20936
|
-
(left, right) => compareRegionIds(left.regionId, right.regionId)
|
|
20937
|
-
);
|
|
20938
|
-
const selected = stableCandidates[stableIndex(input.stableKey, stableCandidates.length)];
|
|
20939
|
-
if (selected) {
|
|
20940
|
-
return { regionId: selected.regionId, source: "stable-fallback" };
|
|
20941
|
-
}
|
|
20942
|
-
}
|
|
20943
|
-
return null;
|
|
20944
|
-
}
|
|
20945
|
-
function coordinate(value, minimum, maximum) {
|
|
20946
|
-
const parsed = typeof value === "number" ? value : Number(value);
|
|
20947
|
-
return Number.isFinite(parsed) && parsed >= minimum && parsed <= maximum ? parsed : void 0;
|
|
20948
|
-
}
|
|
20949
|
-
function readDatafnCloudflarePlacementLocation(request) {
|
|
20950
|
-
const cf = request.cf;
|
|
20951
|
-
if (!cf) return null;
|
|
20952
|
-
const latitude = coordinate(cf.latitude, -90, 90);
|
|
20953
|
-
const longitude = coordinate(cf.longitude, -180, 180);
|
|
20954
|
-
const continent = typeof cf.continent === "string" && /^[A-Za-z]{2}$/u.test(cf.continent) ? cf.continent.toUpperCase() : void 0;
|
|
20955
|
-
if (latitude === void 0 && longitude === void 0 && !continent)
|
|
20956
|
-
return null;
|
|
20957
|
-
return {
|
|
20958
|
-
...latitude === void 0 ? {} : { latitude },
|
|
20959
|
-
...longitude === void 0 ? {} : { longitude },
|
|
20960
|
-
...continent ? { continent } : {}
|
|
20961
|
-
};
|
|
20962
|
-
}
|
|
20963
|
-
var POSTGRES_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
20964
|
-
var DATAFN_INTERNAL_TABLE_RE = /^__datafn_[a-z0-9_]+$/;
|
|
20965
|
-
var LEGACY_PERMISSIONS_TABLE_RE = /^__datafn_permissions_(?!global$)[A-Za-z0-9_]+$/;
|
|
20966
|
-
var POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL = `
|
|
20967
|
-
SELECT c.table_name, c.column_name
|
|
20968
|
-
FROM information_schema.columns AS c
|
|
20969
|
-
JOIN information_schema.tables AS t
|
|
20970
|
-
ON t.table_schema = c.table_schema
|
|
20971
|
-
AND t.table_name = c.table_name
|
|
20972
|
-
WHERE c.table_schema = current_schema()
|
|
20973
|
-
AND t.table_type = 'BASE TABLE'
|
|
20974
|
-
ORDER BY c.table_name, c.ordinal_position
|
|
20975
|
-
`.trim();
|
|
20976
|
-
var INTERNAL_ROLE_BY_TABLE = {
|
|
20977
|
-
__datafn_meta: "sync_meta",
|
|
20978
|
-
__datafn_changes: "sync_changes",
|
|
20979
|
-
__datafn_idempotency: "idempotency",
|
|
20980
|
-
__datafn_seed: "seed",
|
|
20981
|
-
__datafn_permission_directory_outbox: "permission_directory_outbox"
|
|
20982
|
-
};
|
|
20983
|
-
var SHARING_RESOURCE_TABLES = [
|
|
20984
|
-
{
|
|
20985
|
-
logicalRole: "permissions_global",
|
|
20986
|
-
relation: "__datafn_permissions_global"
|
|
20987
|
-
},
|
|
20988
|
-
{
|
|
20989
|
-
logicalRole: "principal_memberships",
|
|
20990
|
-
relation: "__datafn_principal_memberships"
|
|
20991
|
-
},
|
|
20992
|
-
{
|
|
20993
|
-
logicalRole: "principal_hierarchy",
|
|
20994
|
-
relation: "__datafn_principal_hierarchy"
|
|
20995
|
-
}
|
|
20996
|
-
];
|
|
20997
|
-
var SELECTOR_COLUMN = {
|
|
20998
|
-
resource_ns: "__ns",
|
|
20999
|
-
internal_namespace: "namespace"
|
|
21000
|
-
};
|
|
21001
|
-
var DatafnNamespaceStorageError = class extends Error {
|
|
21002
|
-
constructor(input) {
|
|
21003
|
-
super(input.message);
|
|
21004
|
-
this.name = "DatafnNamespaceStorageError";
|
|
21005
|
-
this.code = input.code;
|
|
21006
|
-
this.details = input.details;
|
|
21007
|
-
}
|
|
21008
|
-
};
|
|
21009
|
-
function fail(code, message, details) {
|
|
21010
|
-
throw new DatafnNamespaceStorageError({ code, message, details });
|
|
21011
|
-
}
|
|
21012
|
-
function quotePostgresIdentifier(identifier) {
|
|
21013
|
-
if (!POSTGRES_IDENTIFIER_RE.test(identifier)) {
|
|
21014
|
-
fail(
|
|
21015
|
-
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21016
|
-
`Invalid PostgreSQL identifier "${identifier}"`,
|
|
21017
|
-
{ identifier }
|
|
21018
|
-
);
|
|
21019
|
-
}
|
|
21020
|
-
return `"${identifier}"`;
|
|
21021
|
-
}
|
|
21022
|
-
function namespacedInternalTableRoles() {
|
|
21023
|
-
return INTERNAL_ROLE_BY_TABLE;
|
|
21024
|
-
}
|
|
21025
|
-
function catalogByName(catalog) {
|
|
21026
|
-
const map = /* @__PURE__ */ new Map();
|
|
21027
|
-
for (const relation of catalog.relations) {
|
|
21028
|
-
if (map.has(relation.name)) fail("DATAFN_NAMESPACE_STORAGE_INVALID", "Duplicate catalog relation.");
|
|
21029
|
-
map.set(relation.name, relation);
|
|
21030
|
-
}
|
|
21031
|
-
return map;
|
|
21032
|
-
}
|
|
21033
|
-
function hasColumn(relation, column) {
|
|
21034
|
-
return Boolean(relation?.columns.includes(column));
|
|
21035
|
-
}
|
|
21036
|
-
function isShareableResource(schema, resourceName) {
|
|
21037
|
-
const resource = schema.resources.find((entry) => entry.name === resourceName);
|
|
21038
|
-
if (!resource) return false;
|
|
21039
|
-
const resolved = resolveCapabilities(schema.capabilities, resource.capabilities);
|
|
21040
|
-
return resolved.some(
|
|
21041
|
-
(entry) => typeof entry === "object" && entry !== null && "shareable" in entry
|
|
21042
|
-
);
|
|
21043
|
-
}
|
|
21044
|
-
function schemaDerivedRelations(schema) {
|
|
21045
|
-
const planned = [];
|
|
21046
|
-
const resourceRole = namespaceStorageManifestEntry("resource");
|
|
21047
|
-
schema.resources.forEach((resource, index) => {
|
|
21048
|
-
planned.push({
|
|
21049
|
-
logicalRole: "resource",
|
|
21050
|
-
relation: resource.name,
|
|
21051
|
-
requiredColumns: [
|
|
21052
|
-
"id",
|
|
21053
|
-
...resource.fields.map((field) => field.name),
|
|
21054
|
-
...getCapabilityFields(resolveCapabilities(schema.capabilities, resource.capabilities)).map((field) => field.name)
|
|
21055
|
-
].map(physicalColumn),
|
|
21056
|
-
copyOrder: resourceRole.copyOrder + index
|
|
21057
|
-
});
|
|
21058
|
-
});
|
|
21059
|
-
const joinRole = namespaceStorageManifestEntry("join");
|
|
21060
|
-
const joinTables = /* @__PURE__ */ new Set();
|
|
21061
|
-
for (const relation of schema.relations ?? []) {
|
|
21062
|
-
if (relation.type !== "many-many") continue;
|
|
21063
|
-
const table = getRelationJoinTableName(relation);
|
|
21064
|
-
if (joinTables.has(table)) continue;
|
|
21065
|
-
joinTables.add(table);
|
|
21066
|
-
planned.push({
|
|
21067
|
-
logicalRole: "join",
|
|
21068
|
-
relation: table,
|
|
21069
|
-
requiredColumns: [
|
|
21070
|
-
"id",
|
|
21071
|
-
relation.joinColumns?.from ?? "from",
|
|
21072
|
-
relation.joinColumns?.to ?? "to",
|
|
21073
|
-
...(relation.metadata ?? []).map((field) => field.name)
|
|
21074
|
-
].map(physicalColumn),
|
|
21075
|
-
copyOrder: joinRole.copyOrder + joinTables.size
|
|
21076
|
-
});
|
|
21077
|
-
}
|
|
21078
|
-
const legacyRole = namespaceStorageManifestEntry("permissions_legacy");
|
|
21079
|
-
schema.resources.forEach((resource, index) => {
|
|
21080
|
-
if (!isShareableResource(schema, resource.name)) return;
|
|
21081
|
-
planned.push({
|
|
21082
|
-
logicalRole: "permissions_legacy",
|
|
21083
|
-
relation: getLegacyPermissionsTable(resource.name),
|
|
21084
|
-
copyOrder: legacyRole.copyOrder + index
|
|
21085
|
-
});
|
|
21086
|
-
});
|
|
21087
|
-
return planned;
|
|
21088
|
-
}
|
|
21089
|
-
function internalPlannedRelations() {
|
|
21090
|
-
return Object.entries(INTERNAL_ROLE_BY_TABLE).map(([relation, logicalRole]) => ({
|
|
21091
|
-
logicalRole,
|
|
21092
|
-
relation,
|
|
21093
|
-
copyOrder: namespaceStorageManifestEntry(logicalRole).copyOrder
|
|
21094
|
-
}));
|
|
21095
|
-
}
|
|
21096
|
-
function sharingPlannedRelations() {
|
|
21097
|
-
return SHARING_RESOURCE_TABLES.map((entry) => ({
|
|
21098
|
-
logicalRole: entry.logicalRole,
|
|
21099
|
-
relation: entry.relation,
|
|
21100
|
-
copyOrder: namespaceStorageManifestEntry(entry.logicalRole).copyOrder
|
|
21101
|
-
}));
|
|
21102
|
-
}
|
|
21103
|
-
function physicalColumn(name) {
|
|
21104
|
-
return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
21105
|
-
}
|
|
21106
|
-
var SHARING_REQUIRED_COLUMNS = {
|
|
21107
|
-
permissions_global: ["id", "resource_type", "resource_ns", "resource_id", "principal_id", "level", "grant_kind", "source_ref", "granted_by", "granted_at", "revoked_at"],
|
|
21108
|
-
permissions_legacy: ["id", "resource_id", "user_id", "level", "granted_by", "granted_at", "revoked_at"],
|
|
21109
|
-
principal_memberships: ["id", "namespace", "actor_id", "principal_id", "granted_at", "revoked_at"],
|
|
21110
|
-
principal_hierarchy: ["id", "namespace", "principal_id", "parent_principal_id", "created_at", "revoked_at"]
|
|
21111
|
-
};
|
|
21112
|
-
function toEntry(planned, catalog) {
|
|
21113
|
-
const manifest = namespaceStorageManifestEntry(planned.logicalRole);
|
|
21114
|
-
const namespaceColumn = SELECTOR_COLUMN[manifest.selectorKind];
|
|
21115
|
-
const relation = catalog.get(planned.relation);
|
|
21116
|
-
if (!relation) {
|
|
21117
|
-
if (manifest.presence === "schema-derived") {
|
|
21118
|
-
fail(
|
|
21119
|
-
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21120
|
-
`Required namespace storage relation "${planned.relation}" is missing from the installed schema.`,
|
|
21121
|
-
{ logicalRole: planned.logicalRole, relation: planned.relation }
|
|
21122
|
-
);
|
|
21123
|
-
}
|
|
21124
|
-
return null;
|
|
21125
|
-
}
|
|
21126
|
-
if (!hasColumn(relation, namespaceColumn)) {
|
|
21127
|
-
fail(
|
|
21128
|
-
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21129
|
-
`Namespace storage relation "${planned.relation}" is missing selector column "${namespaceColumn}".`,
|
|
21130
|
-
{
|
|
21131
|
-
logicalRole: planned.logicalRole,
|
|
21132
|
-
relation: planned.relation,
|
|
21133
|
-
expectedColumn: namespaceColumn,
|
|
21134
|
-
columns: relation.columns
|
|
21135
|
-
}
|
|
21136
|
-
);
|
|
21137
|
-
}
|
|
21138
|
-
const required = planned.requiredColumns ?? INTERNAL_TABLE_SCHEMAS[planned.relation]?.map((column) => column.name) ?? SHARING_REQUIRED_COLUMNS[planned.logicalRole] ?? ["id"];
|
|
21139
|
-
const missing = required.filter((column) => !hasColumn(relation, column));
|
|
21140
|
-
if (missing.length > 0) {
|
|
21141
|
-
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing required columns: ${missing.join(", ")}.`);
|
|
21142
|
-
}
|
|
21143
|
-
const hasIdentity = relation.indexes?.some((index) => index.unique && index.primary === true && index.columns.includes("id") && index.columns.every((column) => column === "id" || column === namespaceColumn));
|
|
21144
|
-
if (!hasIdentity) {
|
|
21145
|
-
fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", `Relation "${planned.relation}" is missing a validated unique identity index.`);
|
|
21146
|
-
}
|
|
21147
|
-
return {
|
|
21148
|
-
logicalRole: planned.logicalRole,
|
|
21149
|
-
ownership: "datafn",
|
|
21150
|
-
relation: planned.relation,
|
|
21151
|
-
quotedRelation: quotePostgresIdentifier(planned.relation),
|
|
21152
|
-
namespaceColumn,
|
|
21153
|
-
quotedNamespaceColumn: quotePostgresIdentifier(namespaceColumn),
|
|
21154
|
-
operations: manifest.operations,
|
|
21155
|
-
copyOrder: planned.copyOrder,
|
|
21156
|
-
present: true
|
|
21157
|
-
};
|
|
21158
|
-
}
|
|
21159
|
-
function isDatafnOwnedCatalogRelation(name, columns) {
|
|
21160
|
-
if (columns.includes("__ns")) return true;
|
|
21161
|
-
return DATAFN_INTERNAL_TABLE_RE.test(name) && columns.includes("namespace");
|
|
21162
|
-
}
|
|
21163
|
-
function unexpectedCatalogRelations(catalog, plannedNames) {
|
|
21164
|
-
const unexpected = [];
|
|
21165
|
-
for (const relation of catalog.relations) {
|
|
21166
|
-
if (plannedNames.has(relation.name)) continue;
|
|
21167
|
-
if (!isDatafnOwnedCatalogRelation(relation.name, relation.columns)) continue;
|
|
21168
|
-
unexpected.push(relation.name);
|
|
21169
|
-
}
|
|
21170
|
-
return unexpected.sort();
|
|
21171
|
-
}
|
|
21172
|
-
function resolveNamespaceStoragePlan(input) {
|
|
21173
|
-
try {
|
|
21174
|
-
assertSupportedNamespaceStorageVersions({
|
|
21175
|
-
manifestVersion: input.manifestVersion,
|
|
21176
|
-
schemaVersion: input.schemaVersion
|
|
21177
|
-
});
|
|
21178
|
-
} catch (error) {
|
|
21179
|
-
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "DATAFN_NAMESPACE_STORAGE_VERSION_MISMATCH";
|
|
21180
|
-
fail(
|
|
21181
|
-
code,
|
|
21182
|
-
error instanceof Error ? error.message : String(error)
|
|
21183
|
-
);
|
|
21184
|
-
}
|
|
21185
|
-
if (input.catalog.dialect !== "postgres") {
|
|
21186
|
-
fail(
|
|
21187
|
-
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21188
|
-
`Namespace storage plans are supported for PostgreSQL only. Received dialect "${String(input.catalog.dialect)}".`
|
|
21189
|
-
);
|
|
21190
|
-
}
|
|
21191
|
-
if (!isNamespaced(input.schema)) {
|
|
21192
|
-
fail(
|
|
21193
|
-
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21194
|
-
"Namespace storage plans require a namespaced DataFn schema."
|
|
21195
|
-
);
|
|
21196
|
-
}
|
|
21197
|
-
const catalog = catalogByName(input.catalog);
|
|
21198
|
-
const planned = [
|
|
21199
|
-
...schemaDerivedRelations(input.schema),
|
|
21200
|
-
...sharingPlannedRelations(),
|
|
21201
|
-
...internalPlannedRelations()
|
|
21202
|
-
];
|
|
21203
|
-
for (const relation of input.catalog.relations) {
|
|
21204
|
-
if (LEGACY_PERMISSIONS_TABLE_RE.test(relation.name) && !planned.some((entry) => entry.relation === relation.name)) {
|
|
21205
|
-
planned.push({
|
|
21206
|
-
logicalRole: "permissions_legacy",
|
|
21207
|
-
relation: relation.name,
|
|
21208
|
-
copyOrder: namespaceStorageManifestEntry("permissions_legacy").copyOrder + 50
|
|
21209
|
-
});
|
|
21210
|
-
}
|
|
21211
|
-
}
|
|
21212
|
-
const plannedNames = new Set(planned.map((entry) => entry.relation));
|
|
21213
|
-
const unexpected = unexpectedCatalogRelations(input.catalog, plannedNames);
|
|
21214
|
-
if (unexpected.length > 0) {
|
|
21215
|
-
fail(
|
|
21216
|
-
"DATAFN_NAMESPACE_STORAGE_UNKNOWN",
|
|
21217
|
-
`Installed schema contains DataFn namespace storage that is not in the supported contract: ${unexpected.join(", ")}.`,
|
|
21218
|
-
{ relations: unexpected }
|
|
21219
|
-
);
|
|
21220
|
-
}
|
|
21221
|
-
const entries = planned.map((entry) => toEntry(entry, catalog)).filter((entry) => entry !== null).sort((left, right) => {
|
|
21222
|
-
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21223
|
-
return left.relation.localeCompare(right.relation);
|
|
21224
|
-
});
|
|
21225
|
-
return {
|
|
21226
|
-
manifestVersion: DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION,
|
|
21227
|
-
schemaVersion: DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION,
|
|
21228
|
-
dialect: "postgres",
|
|
21229
|
-
entries
|
|
21230
|
-
};
|
|
21231
|
-
}
|
|
21232
|
-
function selectNamespaceStorageEntries(plan, operation) {
|
|
21233
|
-
const selected = plan.entries.filter((entry) => entry.operations.includes(operation));
|
|
21234
|
-
if (operation === "cleanup") {
|
|
21235
|
-
return [...selected].sort((left, right) => {
|
|
21236
|
-
if (left.copyOrder !== right.copyOrder) return right.copyOrder - left.copyOrder;
|
|
21237
|
-
return right.relation.localeCompare(left.relation);
|
|
21238
|
-
});
|
|
21239
|
-
}
|
|
21240
|
-
return selected;
|
|
21241
|
-
}
|
|
21242
|
-
function composeNamespaceStoragePlan(plan, applicationEntries) {
|
|
21243
|
-
const existing = new Set(plan.entries.map((entry) => entry.relation));
|
|
21244
|
-
const composed = [...plan.entries];
|
|
21245
|
-
for (const entry of applicationEntries) {
|
|
21246
|
-
if (existing.has(entry.relation)) {
|
|
21247
|
-
fail(
|
|
21248
|
-
"DATAFN_NAMESPACE_STORAGE_INVALID",
|
|
21249
|
-
`Application storage relation "${entry.relation}" collides with DataFn-owned storage.`,
|
|
21250
|
-
{ relation: entry.relation }
|
|
21251
|
-
);
|
|
21252
|
-
}
|
|
21253
|
-
existing.add(entry.relation);
|
|
21254
|
-
composed.push({
|
|
21255
|
-
logicalRole: "application",
|
|
21256
|
-
ownership: "application",
|
|
21257
|
-
relation: entry.relation,
|
|
21258
|
-
quotedRelation: quotePostgresIdentifier(entry.relation),
|
|
21259
|
-
namespaceColumn: entry.namespaceColumn,
|
|
21260
|
-
quotedNamespaceColumn: quotePostgresIdentifier(entry.namespaceColumn),
|
|
21261
|
-
operations: entry.operations ?? ["copy", "verify", "cleanup", "fence", "backup"],
|
|
21262
|
-
copyOrder: entry.copyOrder ?? 10,
|
|
21263
|
-
present: true
|
|
21264
|
-
});
|
|
21265
|
-
}
|
|
21266
|
-
composed.sort((left, right) => {
|
|
21267
|
-
if (left.copyOrder !== right.copyOrder) return left.copyOrder - right.copyOrder;
|
|
21268
|
-
return left.relation.localeCompare(right.relation);
|
|
21269
|
-
});
|
|
21270
|
-
return { ...plan, entries: composed };
|
|
21271
|
-
}
|
|
21272
|
-
var POSTGRES_NAMESPACE_STORAGE_INDEX_SQL = `
|
|
21273
|
-
SELECT t.relname AS table_name,
|
|
21274
|
-
array_agg(a.attname ORDER BY k.ordinality) AS index_columns,
|
|
21275
|
-
i.indisunique AS index_unique, i.indisprimary AS index_primary
|
|
21276
|
-
FROM pg_index i
|
|
21277
|
-
JOIN pg_class t ON t.oid = i.indrelid
|
|
21278
|
-
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
21279
|
-
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ordinality)
|
|
21280
|
-
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
|
21281
|
-
WHERE n.nspname = current_schema() AND i.indisvalid AND i.indisready
|
|
21282
|
-
AND i.indpred IS NULL AND i.indexprs IS NULL
|
|
21283
|
-
AND k.ordinality <= i.indnkeyatts
|
|
21284
|
-
GROUP BY t.relname, i.indexrelid, i.indisunique, i.indisprimary
|
|
21285
|
-
ORDER BY t.relname, i.indexrelid
|
|
21286
|
-
`.trim();
|
|
21287
|
-
async function inspectPostgresNamespaceStorage(query) {
|
|
21288
|
-
const rows = await query(POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL);
|
|
21289
|
-
const indexRows = await query(POSTGRES_NAMESPACE_STORAGE_INDEX_SQL);
|
|
21290
|
-
const relations = /* @__PURE__ */ new Map();
|
|
21291
|
-
for (const row of rows) {
|
|
21292
|
-
if (typeof row.column_name !== "string") fail("DATAFN_NAMESPACE_STORAGE_INCOMPLETE", "Catalog column metadata is missing.");
|
|
21293
|
-
const columns = relations.get(row.table_name) ?? [];
|
|
21294
|
-
columns.push(row.column_name);
|
|
21295
|
-
relations.set(row.table_name, columns);
|
|
21296
|
-
}
|
|
21297
|
-
return {
|
|
21298
|
-
dialect: "postgres",
|
|
21299
|
-
relations: [...relations.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([name, columns]) => ({ name, columns, indexes: indexRows.filter((row) => row.table_name === name && Array.isArray(row.index_columns) && typeof row.index_unique === "boolean").map((row) => ({ columns: row.index_columns, unique: row.index_unique, primary: row.index_primary === true })) }))
|
|
21300
|
-
};
|
|
21301
|
-
}
|
|
21302
|
-
function assertInternalNamespaceStorageMetadataComplete() {
|
|
21303
|
-
const namespaced = listNamespacedInternalTables();
|
|
21304
|
-
const mapped = Object.keys(INTERNAL_ROLE_BY_TABLE).sort();
|
|
21305
|
-
if (namespaced.join("\0") !== mapped.join("\0")) {
|
|
21306
|
-
fail(
|
|
21307
|
-
"DATAFN_NAMESPACE_STORAGE_INCOMPLETE",
|
|
21308
|
-
"Internal namespace tables are missing from the storage contract.",
|
|
21309
|
-
{
|
|
21310
|
-
namespacedInternalTables: namespaced,
|
|
21311
|
-
mappedInternalTables: mapped,
|
|
21312
|
-
schemas: Object.keys(INTERNAL_TABLE_SCHEMAS).sort()
|
|
21313
|
-
}
|
|
21314
|
-
);
|
|
21315
|
-
}
|
|
21316
|
-
}
|
|
21317
|
-
async function drainNamespaceStorage(input) {
|
|
21318
|
-
const drainEntries = selectNamespaceStorageEntries(input.plan, "drain");
|
|
21319
|
-
if (drainEntries.length === 0) {
|
|
21320
|
-
return { processed: 0, pending: 0 };
|
|
21321
|
-
}
|
|
21322
|
-
const unsupported = drainEntries.filter(
|
|
21323
|
-
(entry) => entry.logicalRole !== "permission_directory_outbox"
|
|
21324
|
-
);
|
|
21325
|
-
if (unsupported.length > 0) {
|
|
21326
|
-
fail(
|
|
21327
|
-
"DATAFN_NAMESPACE_STORAGE_UNSUPPORTED",
|
|
21328
|
-
`No drain implementation for logical roles: ${unsupported.map((entry) => entry.logicalRole).join(", ")}.`,
|
|
21329
|
-
{ roles: unsupported.map((entry) => entry.logicalRole) }
|
|
21330
|
-
);
|
|
21331
|
-
}
|
|
21332
|
-
return drainNamespacePermissionDirectoryOutbox(
|
|
21333
|
-
input.adapter,
|
|
21334
|
-
input.namespace,
|
|
21335
|
-
input.runtime,
|
|
21336
|
-
input.logger
|
|
21337
|
-
);
|
|
21338
|
-
}
|
|
21339
|
-
async function resolveAuthoritativeAncestorInactive(adapter, schema, resource, record, namespace, maxGraphNodes) {
|
|
21340
|
-
const active = /* @__PURE__ */ new Set();
|
|
21341
|
-
const resolved = /* @__PURE__ */ new Map();
|
|
21342
|
-
let visited = 0;
|
|
21343
|
-
async function visit(model, row, root = false) {
|
|
21344
|
-
if (typeof row.id !== "string" || row.id.length === 0) {
|
|
21345
|
-
throw new Error("Ancestor repair: malformed record identifier");
|
|
21346
|
-
}
|
|
21347
|
-
const key = JSON.stringify([model, row.id]);
|
|
21348
|
-
if (active.has(key)) throw new Error("Ancestor repair: cycle detected");
|
|
21349
|
-
if (resolved.has(key)) return resolved.get(key);
|
|
21350
|
-
if (++visited > maxGraphNodes) throw new Error("Ancestor repair: maxGraphNodes exceeded");
|
|
21351
|
-
active.add(key);
|
|
21352
|
-
let inherited = false;
|
|
21353
|
-
for (const relation of schema.relations ?? []) {
|
|
21354
|
-
if (relation.inheritsInactive !== true || relation.type === "many-many") continue;
|
|
21355
|
-
const dependent = relation.type === "many-one" ? relation.from : relation.to;
|
|
21356
|
-
if (!endpointIncludes(dependent, model)) continue;
|
|
21357
|
-
const endpoint = relation.type === "many-one" ? relation.to : relation.from;
|
|
21358
|
-
const field = relation.type === "many-one" ? relationFkFieldForManyOne(relation) : relation.type === "htree" ? relation.fkField || relation.foreignKey || relation.inverse || "parentId" : relationFkFieldForOneMany(relation);
|
|
21359
|
-
const parentId = row[field];
|
|
21360
|
-
if (parentId === null || parentId === void 0 || parentId === "") continue;
|
|
21361
|
-
if (typeof parentId !== "string") throw new Error("Ancestor repair: malformed parent link");
|
|
21362
|
-
const resourceField = relation.fkResourceField || (relation.type === "htree" ? `${field.replace(/Id$/, "")}Resource` : `${(relation.type === "many-one" ? relation.relation || "target" : relation.inverse || relation.relation || "source").replace(/Id$/, "")}Resource`);
|
|
21363
|
-
const discriminator = row[resourceField];
|
|
21364
|
-
const inferred = resolveEndpointResource(endpoint, parentId, schema);
|
|
21365
|
-
let parentResource = inferred;
|
|
21366
|
-
if (endpointList(endpoint).length > 1 && discriminator != null) {
|
|
21367
|
-
if (typeof discriminator !== "string" || !endpointIncludes(endpoint, discriminator) || inferred && inferred !== discriminator) {
|
|
21368
|
-
throw new Error("Ancestor repair: invalid parent resource discriminator");
|
|
21369
|
-
}
|
|
21370
|
-
parentResource = discriminator;
|
|
21371
|
-
}
|
|
21372
|
-
if (!parentResource) throw new Error("Ancestor repair: ambiguous parent resource");
|
|
21373
|
-
const parent = await adapter.findOne({
|
|
21374
|
-
model: parentResource,
|
|
21375
|
-
where: [{ field: "id", operator: "eq", value: parentId }],
|
|
21376
|
-
namespace
|
|
21377
|
-
});
|
|
21378
|
-
if (!parent) throw new Error("Ancestor repair: missing parent record");
|
|
21379
|
-
const parentInactive = await visit(parentResource, parent);
|
|
21380
|
-
inherited = inherited || parentInactive;
|
|
21381
|
-
}
|
|
21382
|
-
active.delete(key);
|
|
21383
|
-
const effective = inherited || row.isArchived === true || row.trashedAt != null;
|
|
21384
|
-
resolved.set(key, effective);
|
|
21385
|
-
return root ? inherited : effective;
|
|
21386
|
-
}
|
|
21387
|
-
return visit(resource, record, true);
|
|
21388
|
-
}
|
|
21389
|
-
|
|
21390
|
-
// src/execution/migration/ancestor-inactive.ts
|
|
21391
|
-
var DEFAULT_BATCH_SIZE = 500;
|
|
21392
|
-
function compareCodePoints(a, b) {
|
|
21393
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
21394
|
-
}
|
|
21395
|
-
function ancestorInactiveResources(schema) {
|
|
21396
|
-
return [...getAncestorInactiveResources(schema.relations)].sort(compareCodePoints);
|
|
21397
|
-
}
|
|
21398
|
-
async function recomputeAncestorInactive(adapter, schema, options) {
|
|
21399
|
-
const { namespace, dryRun = false, logger } = options;
|
|
21400
|
-
const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
21401
|
-
const maxGraphNodes = options.maxGraphNodes ?? 1e4;
|
|
21402
|
-
for (const value of [batchSize, maxGraphNodes]) {
|
|
21403
|
-
if (!Number.isSafeInteger(value) || value < 1) throw new Error("Ancestor repair: limits must be positive safe integers");
|
|
21404
|
-
}
|
|
21405
|
-
const resources = ancestorInactiveResources(schema);
|
|
21406
|
-
if (resources.length === 0) return { scanned: 0, updated: 0, skipped: 0, nextCursor: null };
|
|
21407
|
-
let cursor = options.cursor ?? { resource: resources[0], afterId: null };
|
|
21408
|
-
let resourceIndex = resources.indexOf(cursor.resource);
|
|
21409
|
-
if (resourceIndex === -1) {
|
|
21410
|
-
throw new Error(`recomputeAncestorInactive: unknown cursor resource "${cursor.resource}"`);
|
|
21411
|
-
}
|
|
21412
|
-
let scanned = 0;
|
|
21413
|
-
let updated = 0;
|
|
21414
|
-
let skipped = 0;
|
|
21415
|
-
let remaining = batchSize;
|
|
21416
|
-
while (remaining > 0) {
|
|
21417
|
-
const resource = resources[resourceIndex];
|
|
21418
|
-
const requested = remaining;
|
|
21419
|
-
const rows = await adapter.findMany({
|
|
21420
|
-
model: resource,
|
|
21421
|
-
where: cursor.afterId === null ? [] : [{ field: "id", operator: "gt", value: cursor.afterId }],
|
|
21422
|
-
orderBy: [{ field: "id", direction: "asc" }],
|
|
21423
|
-
limit: requested,
|
|
21424
|
-
namespace
|
|
21425
|
-
});
|
|
21426
|
-
for (const row of rows) {
|
|
21427
|
-
const id = row.id;
|
|
21428
|
-
if (typeof id !== "string" || id.length === 0) throw new Error("Ancestor repair: malformed record identifier");
|
|
21429
|
-
scanned += 1;
|
|
21430
|
-
const next = await resolveAuthoritativeAncestorInactive(adapter, schema, resource, row, namespace, maxGraphNodes);
|
|
21431
|
-
const previous = row[ANCESTOR_INACTIVE_FIELD];
|
|
21432
|
-
if (previous !== next) {
|
|
21433
|
-
if (dryRun) {
|
|
21434
|
-
updated += 1;
|
|
21435
|
-
} else {
|
|
21436
|
-
const affected = await adapter.updateMany({
|
|
21437
|
-
model: resource,
|
|
21438
|
-
where: [
|
|
21439
|
-
{ field: "id", operator: "eq", value: id },
|
|
21440
|
-
{
|
|
21441
|
-
field: ANCESTOR_INACTIVE_FIELD,
|
|
21442
|
-
operator: "eq",
|
|
21443
|
-
value: previous ?? null
|
|
21444
|
-
}
|
|
21445
|
-
],
|
|
21446
|
-
data: { [ANCESTOR_INACTIVE_FIELD]: next },
|
|
21447
|
-
namespace
|
|
21448
|
-
});
|
|
21449
|
-
updated += affected > 0 ? 1 : 0;
|
|
21450
|
-
skipped += affected > 0 ? 0 : 1;
|
|
21451
|
-
}
|
|
21452
|
-
}
|
|
21453
|
-
cursor = { resource, afterId: id };
|
|
21454
|
-
}
|
|
21455
|
-
remaining -= rows.length;
|
|
21456
|
-
if (rows.length < requested) {
|
|
21457
|
-
resourceIndex += 1;
|
|
21458
|
-
if (resourceIndex >= resources.length) {
|
|
21459
|
-
logger?.info("datafn.ancestorInactive.recompute.complete", { namespace, scanned, updated, skipped, dryRun });
|
|
21460
|
-
return { scanned, updated, skipped, nextCursor: null };
|
|
21461
|
-
}
|
|
21462
|
-
cursor = { resource: resources[resourceIndex], afterId: null };
|
|
21463
|
-
}
|
|
21464
|
-
}
|
|
21465
|
-
return { scanned, updated, skipped, nextCursor: cursor };
|
|
21466
|
-
}
|
|
21467
|
-
async function recomputeAncestorInactiveAll(adapter, schema, options) {
|
|
21468
|
-
const maxSweeps = options.maxSweeps ?? 32;
|
|
21469
|
-
if (!Number.isSafeInteger(maxSweeps) || maxSweeps < 1) throw new Error("Ancestor repair: maxSweeps must be a positive safe integer");
|
|
21470
|
-
let scanned = 0;
|
|
21471
|
-
let updated = 0;
|
|
21472
|
-
let skipped = 0;
|
|
21473
|
-
let sweeps = 0;
|
|
21474
|
-
let converged = false;
|
|
21475
|
-
while (sweeps < maxSweeps) {
|
|
21476
|
-
sweeps += 1;
|
|
21477
|
-
let sweepUpdated = 0;
|
|
21478
|
-
let sweepSkipped = 0;
|
|
21479
|
-
let cursor = null;
|
|
21480
|
-
do {
|
|
21481
|
-
const result = await recomputeAncestorInactive(adapter, schema, { ...options, cursor });
|
|
21482
|
-
scanned += result.scanned;
|
|
21483
|
-
sweepUpdated += result.updated;
|
|
21484
|
-
sweepSkipped += result.skipped;
|
|
21485
|
-
cursor = result.nextCursor;
|
|
21486
|
-
} while (cursor !== null);
|
|
21487
|
-
updated += sweepUpdated;
|
|
21488
|
-
skipped += sweepSkipped;
|
|
21489
|
-
if (sweepUpdated === 0 && sweepSkipped === 0) {
|
|
21490
|
-
converged = true;
|
|
21491
|
-
break;
|
|
21492
|
-
}
|
|
21493
|
-
if (options.dryRun) break;
|
|
21494
|
-
}
|
|
21495
|
-
if (!converged) {
|
|
21496
|
-
options.logger?.warn("datafn.ancestorInactive.recompute.notConverged", {
|
|
21497
|
-
namespace: options.namespace,
|
|
21498
|
-
sweeps,
|
|
21499
|
-
updated,
|
|
21500
|
-
skipped,
|
|
21501
|
-
dryRun: options.dryRun === true
|
|
21502
|
-
});
|
|
21503
|
-
}
|
|
21504
|
-
return { scanned, updated, skipped, sweeps, converged };
|
|
21505
|
-
}
|
|
21506
|
-
|
|
21507
|
-
export { AtomicSequenceStore, ChainedSequenceStore, DATAFN_MULTI_REGION_CAPABILITY, DATAFN_ROUTING_ASSERTION_HEADER, DATAFN_ROUTING_INTERNAL_HEADERS, DatabaseSequenceStore, DatafnExecutorError, DatafnNamespaceStorageError, DatafnPublicLinkInputError, DatafnRoutingError, POSTGRES_NAMESPACE_STORAGE_CATALOG_SQL, POSTGRES_NAMESPACE_STORAGE_INDEX_SQL, ancestorInactiveResources, assertInternalNamespaceStorageMetadataComplete, claimDatafnNamespacePlacement, composeNamespaceStoragePlan, createConditionalKvDatafnPlacementDirectory, createDatafnGatewayRouter, createDatafnHmacRouteTickets, createDatafnHmacRoutingAssertions, createDatafnMultiRegionPlugin, createDatafnPublicLinksPlugin, createDatafnRouteBootstrap, createDatafnServer, createMemoryDatafnPlacementDirectory, createMemoryDatafnRoutingReplayStore, createSequenceStore, datafn, datafnMultiRegionPlugin, drainNamespacePermissionDirectoryOutbox, drainNamespaceStorage, inspectPostgresNamespaceStorage, migrateDatafnNamespace, namespacedInternalTableRoles, quotePostgresIdentifier, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, readDatafnPublicLinkToken, readDatafnRouteTicket, recomputeAncestorInactive, recomputeAncestorInactiveAll, resolveDatafnPublicLink, resolveNamespaceStoragePlan, routeTicketError, selectDatafnPlacementRegion, selectNamespaceStorageEntries, validateDatafnPlacement, validateDatafnRouteTicket, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRegionalCors, withDatafnRoutingAssertion };
|
|
20432
|
+
export { AtomicSequenceStore, ChainedSequenceStore, DATAFN_MULTI_REGION_CAPABILITY, DATAFN_ROUTING_ASSERTION_HEADER, DATAFN_ROUTING_INTERNAL_HEADERS, DatabaseSequenceStore, DatafnExecutorError, DatafnPublicLinkInputError, DatafnRoutingError, claimDatafnNamespacePlacement, createConditionalKvDatafnPlacementDirectory, createDatafnGatewayRouter, createDatafnHmacRoutingAssertions, createDatafnMultiRegionPlugin, createDatafnPublicLinksPlugin, createDatafnServer, createMemoryDatafnPlacementDirectory, createMemoryDatafnRoutingReplayStore, createSequenceStore, datafn, datafnMultiRegionPlugin, migrateDatafnNamespace, readDatafnPublicLinkToken, resolveDatafnPublicLink, validateDatafnPlacement, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRoutingAssertion };
|
|
21508
20433
|
//# sourceMappingURL=index.js.map
|
|
21509
20434
|
//# sourceMappingURL=index.js.map
|