@datafn/server 0.1.1 → 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 -28
- package/dist/index.cjs +48 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +49 -139
- package/dist/index.js.map +1 -1
- package/package.json +3 -7
- 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.d.cts
CHANGED
|
@@ -4,7 +4,6 @@ import { DatafnSchema, DatafnPlugin as DatafnPlugin$1 } from '@datafn/core/types
|
|
|
4
4
|
import { Route, Router } from '@superfunctions/http';
|
|
5
5
|
import { ConditionalKVStoreAdapter, IndexedDirectoryStoreAdapter, Adapter, AtomicKVStoreAdapter, RuntimeStores, RowLevelNamespaceConfig } from '@superfunctions/db';
|
|
6
6
|
import { DatafnError } from '@datafn/core/errors';
|
|
7
|
-
export { DatafnPlacementConstraint, DatafnPlacementConstraintContext, DatafnPlacementDecision, DatafnPlacementDecisionSource, DatafnPlacementLocation, DatafnPlacementRankingInput, DatafnPlacementRegionCandidate, DatafnPlacementSelectionInput, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, selectDatafnPlacementRegion } from './placement.cjs';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* SearchProvider interface for server-side search integration.
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { DatafnSchema, DatafnPlugin as DatafnPlugin$1 } from '@datafn/core/types
|
|
|
4
4
|
import { Route, Router } from '@superfunctions/http';
|
|
5
5
|
import { ConditionalKVStoreAdapter, IndexedDirectoryStoreAdapter, Adapter, AtomicKVStoreAdapter, RuntimeStores, RowLevelNamespaceConfig } from '@superfunctions/db';
|
|
6
6
|
import { DatafnError } from '@datafn/core/errors';
|
|
7
|
-
export { DatafnPlacementConstraint, DatafnPlacementConstraintContext, DatafnPlacementDecision, DatafnPlacementDecisionSource, DatafnPlacementLocation, DatafnPlacementRankingInput, DatafnPlacementRegionCandidate, DatafnPlacementSelectionInput, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, selectDatafnPlacementRegion } from './placement.js';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* SearchProvider interface for server-side search integration.
|
package/dist/index.js
CHANGED
|
@@ -8152,8 +8152,9 @@ async function recordChangesWithRetry(changeTracking, entries, maxRetries = 2, l
|
|
|
8152
8152
|
}
|
|
8153
8153
|
}
|
|
8154
8154
|
}
|
|
8155
|
+
var preparedNamespaces = /* @__PURE__ */ new WeakMap();
|
|
8155
8156
|
var ChangeTrackingService = class _ChangeTrackingService {
|
|
8156
|
-
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) {
|
|
8157
8158
|
this.db = db;
|
|
8158
8159
|
this.namespace = namespace;
|
|
8159
8160
|
this.sequenceStore = sequenceStore;
|
|
@@ -8161,8 +8162,10 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8161
8162
|
this.logger = logger;
|
|
8162
8163
|
this.metaEnsured = false;
|
|
8163
8164
|
this.changesEnsured = false;
|
|
8165
|
+
this.namespaceMetaEnsured = false;
|
|
8164
8166
|
this.metaEnsured = metaEnsured;
|
|
8165
8167
|
this.changesEnsured = changesEnsured;
|
|
8168
|
+
this.namespaceMetaEnsured = namespaceMetaEnsured;
|
|
8166
8169
|
}
|
|
8167
8170
|
/** Initialize every durable table before an enclosing transaction begins. */
|
|
8168
8171
|
async ensureReady() {
|
|
@@ -8177,6 +8180,48 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8177
8180
|
await ensureInternalTable(this.db, "__datafn_meta");
|
|
8178
8181
|
this.metaEnsured = true;
|
|
8179
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;
|
|
8180
8225
|
}
|
|
8181
8226
|
/**
|
|
8182
8227
|
* REL-011: Create a new ChangeTrackingService with a different db adapter.
|
|
@@ -8192,7 +8237,8 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8192
8237
|
emitChanges ? this.onChange : void 0,
|
|
8193
8238
|
this.logger,
|
|
8194
8239
|
this.metaEnsured,
|
|
8195
|
-
this.changesEnsured
|
|
8240
|
+
this.changesEnsured,
|
|
8241
|
+
true
|
|
8196
8242
|
);
|
|
8197
8243
|
}
|
|
8198
8244
|
/**
|
|
@@ -20383,142 +20429,6 @@ function bytesToBase64Url(bytes) {
|
|
|
20383
20429
|
return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
20384
20430
|
}
|
|
20385
20431
|
|
|
20386
|
-
|
|
20387
|
-
var CONTINENT_CENTERS = {
|
|
20388
|
-
AF: [1.65, 17.68],
|
|
20389
|
-
AN: [-82.86, 135],
|
|
20390
|
-
AS: [34.05, 100.62],
|
|
20391
|
-
EU: [54.53, 15.26],
|
|
20392
|
-
NA: [54.53, -105.26],
|
|
20393
|
-
OC: [-22.74, 140.02],
|
|
20394
|
-
SA: [-14.24, -51.93]
|
|
20395
|
-
};
|
|
20396
|
-
function finiteCoordinate(value, minimum, maximum) {
|
|
20397
|
-
return value !== void 0 && Number.isFinite(value) && value >= minimum && value <= maximum;
|
|
20398
|
-
}
|
|
20399
|
-
function normalizedCandidates(input) {
|
|
20400
|
-
const context = {
|
|
20401
|
-
location: input.location ?? null,
|
|
20402
|
-
preferredRegionId: normalizedPreferredRegionId(input.preferredRegionId)
|
|
20403
|
-
};
|
|
20404
|
-
const seen = /* @__PURE__ */ new Set();
|
|
20405
|
-
const candidates = input.candidates.map((candidate) => {
|
|
20406
|
-
const regionId = candidate.regionId.trim();
|
|
20407
|
-
if (!regionId) throw new Error("DATAFN_PLACEMENT_REGION_ID_REQUIRED");
|
|
20408
|
-
if (seen.has(regionId)) {
|
|
20409
|
-
throw new Error(`DATAFN_PLACEMENT_REGION_DUPLICATE: ${regionId}`);
|
|
20410
|
-
}
|
|
20411
|
-
seen.add(regionId);
|
|
20412
|
-
return regionId === candidate.regionId ? candidate : { ...candidate, regionId };
|
|
20413
|
-
});
|
|
20414
|
-
return candidates.filter(
|
|
20415
|
-
(candidate) => (input.constraints ?? []).every(
|
|
20416
|
-
(constraint) => constraint(candidate, context)
|
|
20417
|
-
)
|
|
20418
|
-
);
|
|
20419
|
-
}
|
|
20420
|
-
function normalizedPreferredRegionId(preferredRegionId) {
|
|
20421
|
-
return preferredRegionId?.trim() || null;
|
|
20422
|
-
}
|
|
20423
|
-
function compareRegionIds(left, right) {
|
|
20424
|
-
return left < right ? -1 : left > right ? 1 : 0;
|
|
20425
|
-
}
|
|
20426
|
-
function sourceCoordinates(location) {
|
|
20427
|
-
if (!location) return null;
|
|
20428
|
-
if (finiteCoordinate(location.latitude, -90, 90) && finiteCoordinate(location.longitude, -180, 180)) {
|
|
20429
|
-
return {
|
|
20430
|
-
coordinates: [location.latitude, location.longitude],
|
|
20431
|
-
source: "coordinates"
|
|
20432
|
-
};
|
|
20433
|
-
}
|
|
20434
|
-
const continent = location.continent?.toUpperCase();
|
|
20435
|
-
const coordinates = continent ? CONTINENT_CENTERS[continent] : void 0;
|
|
20436
|
-
return coordinates ? { coordinates, source: "continent" } : null;
|
|
20437
|
-
}
|
|
20438
|
-
function radians(degrees) {
|
|
20439
|
-
return degrees * Math.PI / 180;
|
|
20440
|
-
}
|
|
20441
|
-
function distanceKilometers(source, target) {
|
|
20442
|
-
const sourceLatitude = radians(source[0]);
|
|
20443
|
-
const targetLatitude = radians(target[0]);
|
|
20444
|
-
const latitudeDelta = radians(target[0] - source[0]);
|
|
20445
|
-
const longitudeDelta = radians(target[1] - source[1]);
|
|
20446
|
-
const haversine = Math.sin(latitudeDelta / 2) ** 2 + Math.cos(sourceLatitude) * Math.cos(targetLatitude) * Math.sin(longitudeDelta / 2) ** 2;
|
|
20447
|
-
const clampedHaversine = Math.min(1, Math.max(0, haversine));
|
|
20448
|
-
return 6371 * 2 * Math.atan2(Math.sqrt(clampedHaversine), Math.sqrt(1 - clampedHaversine));
|
|
20449
|
-
}
|
|
20450
|
-
function rankDatafnPlacementRegions(input) {
|
|
20451
|
-
const candidates = normalizedCandidates(input);
|
|
20452
|
-
const source = sourceCoordinates(input.location ?? null);
|
|
20453
|
-
if (!source) return [];
|
|
20454
|
-
return candidates.flatMap((candidate) => {
|
|
20455
|
-
const { latitude, longitude } = candidate;
|
|
20456
|
-
return finiteCoordinate(latitude, -90, 90) && finiteCoordinate(longitude, -180, 180) ? [
|
|
20457
|
-
{
|
|
20458
|
-
regionId: candidate.regionId,
|
|
20459
|
-
source: source.source,
|
|
20460
|
-
distanceKilometers: distanceKilometers(source.coordinates, [
|
|
20461
|
-
latitude,
|
|
20462
|
-
longitude
|
|
20463
|
-
])
|
|
20464
|
-
}
|
|
20465
|
-
] : [];
|
|
20466
|
-
}).sort(
|
|
20467
|
-
(left, right) => (left.distanceKilometers ?? 0) - (right.distanceKilometers ?? 0) || compareRegionIds(left.regionId, right.regionId)
|
|
20468
|
-
);
|
|
20469
|
-
}
|
|
20470
|
-
function stableIndex(key, length) {
|
|
20471
|
-
let hash = 2166136261;
|
|
20472
|
-
for (const byte of new TextEncoder().encode(key)) {
|
|
20473
|
-
hash ^= byte;
|
|
20474
|
-
hash = Math.imul(hash, 16777619) >>> 0;
|
|
20475
|
-
}
|
|
20476
|
-
return hash % length;
|
|
20477
|
-
}
|
|
20478
|
-
function selectDatafnPlacementRegion(input) {
|
|
20479
|
-
const candidates = normalizedCandidates(input);
|
|
20480
|
-
const preferredRegionId = normalizedPreferredRegionId(
|
|
20481
|
-
input.preferredRegionId
|
|
20482
|
-
);
|
|
20483
|
-
if (preferredRegionId && candidates.some((candidate) => candidate.regionId === preferredRegionId)) {
|
|
20484
|
-
return { regionId: preferredRegionId, source: "preferred" };
|
|
20485
|
-
}
|
|
20486
|
-
const ranked = rankDatafnPlacementRegions({
|
|
20487
|
-
candidates,
|
|
20488
|
-
location: input.location ?? null
|
|
20489
|
-
});
|
|
20490
|
-
const nearest = ranked[0];
|
|
20491
|
-
if (nearest) return nearest;
|
|
20492
|
-
if (input.stableKey !== void 0 && candidates.length > 0) {
|
|
20493
|
-
const stableCandidates = [...candidates].sort(
|
|
20494
|
-
(left, right) => compareRegionIds(left.regionId, right.regionId)
|
|
20495
|
-
);
|
|
20496
|
-
const selected = stableCandidates[stableIndex(input.stableKey, stableCandidates.length)];
|
|
20497
|
-
if (selected) {
|
|
20498
|
-
return { regionId: selected.regionId, source: "stable-fallback" };
|
|
20499
|
-
}
|
|
20500
|
-
}
|
|
20501
|
-
return null;
|
|
20502
|
-
}
|
|
20503
|
-
function coordinate(value, minimum, maximum) {
|
|
20504
|
-
const parsed = typeof value === "number" ? value : Number(value);
|
|
20505
|
-
return Number.isFinite(parsed) && parsed >= minimum && parsed <= maximum ? parsed : void 0;
|
|
20506
|
-
}
|
|
20507
|
-
function readDatafnCloudflarePlacementLocation(request) {
|
|
20508
|
-
const cf = request.cf;
|
|
20509
|
-
if (!cf) return null;
|
|
20510
|
-
const latitude = coordinate(cf.latitude, -90, 90);
|
|
20511
|
-
const longitude = coordinate(cf.longitude, -180, 180);
|
|
20512
|
-
const continent = typeof cf.continent === "string" && /^[A-Za-z]{2}$/u.test(cf.continent) ? cf.continent.toUpperCase() : void 0;
|
|
20513
|
-
if (latitude === void 0 && longitude === void 0 && !continent)
|
|
20514
|
-
return null;
|
|
20515
|
-
return {
|
|
20516
|
-
...latitude === void 0 ? {} : { latitude },
|
|
20517
|
-
...longitude === void 0 ? {} : { longitude },
|
|
20518
|
-
...continent ? { continent } : {}
|
|
20519
|
-
};
|
|
20520
|
-
}
|
|
20521
|
-
|
|
20522
|
-
export { AtomicSequenceStore, ChainedSequenceStore, DATAFN_MULTI_REGION_CAPABILITY, DATAFN_ROUTING_ASSERTION_HEADER, DATAFN_ROUTING_INTERNAL_HEADERS, DatabaseSequenceStore, DatafnExecutorError, DatafnPublicLinkInputError, DatafnRoutingError, claimDatafnNamespacePlacement, createConditionalKvDatafnPlacementDirectory, createDatafnGatewayRouter, createDatafnHmacRoutingAssertions, createDatafnMultiRegionPlugin, createDatafnPublicLinksPlugin, createDatafnServer, createMemoryDatafnPlacementDirectory, createMemoryDatafnRoutingReplayStore, createSequenceStore, datafn, datafnMultiRegionPlugin, migrateDatafnNamespace, rankDatafnPlacementRegions, readDatafnCloudflarePlacementLocation, readDatafnPublicLinkToken, resolveDatafnPublicLink, selectDatafnPlacementRegion, validateDatafnPlacement, validateDatafnRoutingBodyLimit, withDatafnPublicLinksSchema, withDatafnRoutingAssertion };
|
|
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 };
|
|
20523
20433
|
//# sourceMappingURL=index.js.map
|
|
20524
20434
|
//# sourceMappingURL=index.js.map
|