durability 5.1.0 → 5.2.1
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 +154 -21
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +85 -74
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/experimental.d.ts +10 -10
- package/dist/experimental.js +7 -7
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +91 -83
- package/dist/fanout/migrations.d.ts +9 -0
- package/dist/fanout/migrations.js +181 -0
- package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +13 -8
- package/dist/{log.d.ts → log/index.d.ts} +27 -20
- package/dist/{log.js → log/index.js} +200 -166
- package/dist/log/migrations.d.ts +18 -0
- package/dist/log/migrations.js +297 -0
- package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
- package/dist/{memoize.js → memoize/index.js} +23 -21
- package/dist/memoize/migrations.d.ts +10 -0
- package/dist/memoize/migrations.js +21 -0
- package/dist/migrations.d.ts +0 -111
- package/dist/migrations.js +12 -628
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +76 -70
- package/dist/operations/migrations.d.ts +10 -0
- package/dist/operations/migrations.js +150 -0
- package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
- package/dist/{outbox.js → outbox/index.js} +35 -77
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/{routing-client.js → routing/client.js} +1 -1
- package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
- package/dist/{routing.js → routing/index.js} +3 -3
- package/dist/{load.d.ts → routing/load.d.ts} +1 -1
- package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
- package/dist/{schedule.js → schedule/index.js} +97 -83
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
- package/dist/{scheduler.js → scheduler/index.js} +308 -70
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
- package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
- package/dist/{semaphore.js → semaphore/index.js} +7 -7
- package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
- package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
- package/dist/semaphore/migrations.d.ts +18 -0
- package/dist/semaphore/migrations.js +444 -0
- package/dist/semaphore/protocol.d.ts +451 -0
- package/dist/semaphore/protocol.js +114 -0
- package/dist/semaphore/queue-store.d.ts +107 -0
- package/dist/semaphore/queue-store.js +258 -0
- package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
- package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
- package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
- package/dist/{semaphore-server.js → semaphore/server.js} +56 -28
- package/dist/semaphore/store.d.ts +53 -0
- package/dist/semaphore/store.js +107 -0
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/{client.d.ts → socket/client.d.ts} +3 -3
- package/dist/{client.js → socket/client.js} +4 -4
- package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
- package/dist/{socket.js → socket/index.js} +66 -20
- package/dist/socket/migrations.d.ts +9 -0
- package/dist/socket/migrations.js +57 -0
- package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
- package/dist/storage.d.ts +4 -0
- package/dist/storage.js +22 -15
- package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
- package/dist/{topology-client.js → topology/client.js} +3 -3
- package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
- package/dist/{topology.js → topology/index.js} +106 -72
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/topology/snapshot.d.ts +50 -0
- package/dist/topology/snapshot.js +38 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +9 -9
- package/package.json +11 -11
- package/dist/semaphore-protocol.d.ts +0 -231
- package/dist/semaphore-protocol.js +0 -106
- package/dist/semaphore-queue-store.d.ts +0 -95
- package/dist/semaphore-queue-store.js +0 -252
- package/dist/semaphore-store.d.ts +0 -43
- package/dist/semaphore-store.js +0 -108
- package/dist/topology-snapshot.d.ts +0 -70
- package/dist/topology-snapshot.js +0 -48
- /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
- /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
- /package/dist/{load.js → routing/load.js} +0 -0
- /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
- /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type TopologyFallbackReason } from '
|
|
2
|
-
import { type TopologyMemberInput, type TopologySnapshot } from './
|
|
1
|
+
import { type TopologyFallbackReason } from '../errors.js';
|
|
2
|
+
import { type TopologyMemberInput, type TopologySnapshot } from './snapshot.js';
|
|
3
3
|
export type TopologySource = 'published' | 'stale' | 'seed' | 'fallback';
|
|
4
4
|
export type TopologyView<Meta = undefined> = TopologySnapshot<Meta> & {
|
|
5
5
|
source: TopologySource;
|
|
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _TopologyClient_instances, _TopologyClient_config, _TopologyClient_readTimeoutMs, _TopologyClient_ttlMs, _TopologyClient_negativeTtlMs, _TopologyClient_cache, _TopologyClient_inflight, _TopologyClient_load, _TopologyClient_read, _TopologyClient_fallback, _TopologyClient_remember;
|
|
13
|
-
import { TopologyUnavailableError, } from '
|
|
14
|
-
import { assertPositiveInteger } from '
|
|
15
|
-
import { decodeSnapshot, } from './
|
|
13
|
+
import { TopologyUnavailableError, } from '../errors.js';
|
|
14
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
15
|
+
import { decodeSnapshot, } from './snapshot.js';
|
|
16
16
|
// High-cardinality groups (one per tenant) must not grow an isolate without bound; the least recently used go first.
|
|
17
17
|
const maxCachedGroups = 1024;
|
|
18
18
|
class ReadTimeoutError extends Error {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type DurabilityOutboxEvent } from '
|
|
2
|
-
import { type SchedulerAttachment } from '
|
|
3
|
-
import { TopologyClient, type TopologyClientConfig } from './
|
|
4
|
-
import { type TopologyMember, type TopologyMemberInput, type TopologySnapshot } from './
|
|
1
|
+
import { type DurabilityOutboxEvent } from '../outbox/index.js';
|
|
2
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
3
|
+
import { TopologyClient, type TopologyClientConfig } from './client.js';
|
|
4
|
+
import { type TopologyMember, type TopologyMemberInput, type TopologySnapshot } from './snapshot.js';
|
|
5
5
|
/** `write` channels reach active members; `read` channels reach draining members too. */
|
|
6
6
|
export type TopologyRole = 'write' | 'read';
|
|
7
7
|
/** Publication and sync never give up: a stale topology must still converge. */
|
|
@@ -32,11 +32,21 @@ export type TopologyChannel<Value, Meta> = TopologyRetryOptions & {
|
|
|
32
32
|
deliver(member: TopologyMember<Meta>, update: TopologyStateUpdate<Value>): unknown;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
|
-
* Publication and sync run on outbox channels
|
|
36
|
-
* group,
|
|
35
|
+
* Publication and sync run on outbox channels keyed by group and by
|
|
36
|
+
* `[group, member]`. On the default instance channel these are
|
|
37
|
+
* `durability.topology` and `durability.topology.<state>`; on another instance
|
|
38
|
+
* channel they are JSON arrays: `["durability.topology", <channel>,
|
|
39
|
+
* "publication"]` and `["durability.topology", <channel>, "state", <state>]`.
|
|
37
40
|
*/
|
|
38
41
|
export type DurabilityTopologyEvent = DurabilityOutboxEvent;
|
|
39
42
|
export type DurabilityTopologyConfig<Meta, States extends Record<string, unknown>> = SchedulerAttachment & TopologyRetryOptions & {
|
|
43
|
+
/**
|
|
44
|
+
* Stable instance ID. Topologies on one scheduler share storage,
|
|
45
|
+
* partitioned by channel; each owns its groups, states, publisher, and
|
|
46
|
+
* deliveries, so the same group name in two channels is two groups.
|
|
47
|
+
* Defaults to `default`.
|
|
48
|
+
*/
|
|
49
|
+
channel?: string;
|
|
40
50
|
/** Without a publisher, membership is only visible inside this object. */
|
|
41
51
|
publish?: TopologyPublisher<Meta>;
|
|
42
52
|
states?: {
|
|
@@ -66,7 +76,7 @@ export declare class DurabilityTopology<Meta = undefined, States extends Record<
|
|
|
66
76
|
/** A read-only client for stateless Workers. It never contacts the coordinator. */
|
|
67
77
|
static client<Meta = undefined>(config: TopologyClientConfig<Meta>): TopologyClient<Meta>;
|
|
68
78
|
/** Reverts only the topology tables; outbox channels migrateCapability with `DurabilityOutbox.migrate`. */
|
|
69
|
-
static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("
|
|
79
|
+
static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("../migrations.js").DurabilityMigrationResult;
|
|
70
80
|
alarm(info?: AlarmInvocationInfo): Promise<void>;
|
|
71
81
|
/**
|
|
72
82
|
* Applies one change in a single transaction. `next` runs inside it and must
|
|
@@ -9,26 +9,35 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
|
|
13
|
-
import
|
|
14
|
-
import { reportFailure, TopologyError } from '
|
|
15
|
-
import { durabilityTopologyMigrations
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
var _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_ownerChannel, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
|
|
13
|
+
import * as v from 'valibot';
|
|
14
|
+
import { reportFailure, TopologyError } from '../errors.js';
|
|
15
|
+
import { durabilityTopologyMigrations } from './migrations.js';
|
|
16
|
+
import { migrateCapability } from '../migrations.js';
|
|
17
|
+
import { DurabilityOutbox, discardWithin, publishNow, stageWithin, } from '../outbox/index.js';
|
|
18
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
19
|
+
import { assertChannel, defaultChannel, DurabilityScheduler, engineFor, } from '../scheduler/index.js';
|
|
20
|
+
import { canonical, deserialize, serialize } from '../serialization.js';
|
|
21
|
+
import { TopologyClient } from './client.js';
|
|
22
|
+
import { encodeSnapshot, memberInputSchema, membersSchema, } from './snapshot.js';
|
|
23
|
+
const outboxChannelPrefix = 'durability.topology';
|
|
24
|
+
// The default instance keeps the names it used before instance channels
|
|
25
|
+
// existed, so outbox rows staged by an older version are still delivered.
|
|
26
|
+
const publicationChannelFor = (ownerChannel) => ownerChannel === defaultChannel
|
|
27
|
+
? outboxChannelPrefix
|
|
28
|
+
: JSON.stringify([outboxChannelPrefix, ownerChannel, 'publication']);
|
|
29
|
+
const stateChannelFor = (ownerChannel, state) => ownerChannel === defaultChannel
|
|
30
|
+
? `${outboxChannelPrefix}.${state}`
|
|
31
|
+
: JSON.stringify([outboxChannelPrefix, ownerChannel, 'state', state]);
|
|
32
|
+
const groupRowSchema = v.object({
|
|
33
|
+
group_id: v.string(),
|
|
34
|
+
version: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
35
|
+
members: v.string(),
|
|
36
|
+
deleted: v.union([v.literal(0), v.literal(1)]),
|
|
28
37
|
});
|
|
29
|
-
const stateRowSchema =
|
|
30
|
-
channel:
|
|
31
|
-
value:
|
|
38
|
+
const stateRowSchema = v.object({
|
|
39
|
+
channel: v.string(),
|
|
40
|
+
value: v.string(),
|
|
32
41
|
});
|
|
33
42
|
class TopologyTimeoutError extends Error {
|
|
34
43
|
constructor(label, timeoutMs) {
|
|
@@ -76,15 +85,16 @@ const outboxOptions = (timeoutMs, options, fallback) => {
|
|
|
76
85
|
* land. KV has no conditional write, so some bound is required.
|
|
77
86
|
*/
|
|
78
87
|
const landingWindowMs = 5 * 60000;
|
|
79
|
-
const forgetWrite = (sql, id) => {
|
|
80
|
-
sql.exec('DELETE FROM durability_topology_writes WHERE id = ?', id);
|
|
88
|
+
const forgetWrite = (sql, ownerChannel, id) => {
|
|
89
|
+
sql.exec('DELETE FROM durability_topology_writes WHERE owner_channel = ? AND id = ?', ownerChannel, id);
|
|
81
90
|
};
|
|
82
91
|
/** Drops expired records and returns when the group's last outstanding write can no longer land. */
|
|
83
|
-
const outstandingWritesUntil = (sql, group, now) => {
|
|
84
|
-
sql.exec(
|
|
85
|
-
|
|
92
|
+
const outstandingWritesUntil = (sql, ownerChannel, group, now) => {
|
|
93
|
+
sql.exec(`DELETE FROM durability_topology_writes
|
|
94
|
+
WHERE owner_channel = ? AND group_id = ? AND land_by <= ?`, ownerChannel, group, now);
|
|
95
|
+
const { until } = v.parse(v.object({ until: v.nullable(v.pipe(v.number(), v.safeInteger())) }), sql
|
|
86
96
|
.exec(`SELECT MAX(land_by) AS until FROM durability_topology_writes
|
|
87
|
-
WHERE group_id = ?`, group)
|
|
97
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
88
98
|
.toArray()[0]);
|
|
89
99
|
return until ?? undefined;
|
|
90
100
|
};
|
|
@@ -97,15 +107,16 @@ const deliveryKey = (group, member) => JSON.stringify([group, member]);
|
|
|
97
107
|
const isThenable = (value) => value !== null &&
|
|
98
108
|
typeof value === 'object' &&
|
|
99
109
|
typeof Reflect.get(value, 'then') === 'function';
|
|
100
|
-
const readGroup = (sql, group) =>
|
|
110
|
+
const readGroup = (sql, ownerChannel, group) => v.parse(v.optional(groupRowSchema), sql
|
|
101
111
|
.exec(`SELECT group_id, version, members, deleted
|
|
102
|
-
FROM durability_topology_groups
|
|
112
|
+
FROM durability_topology_groups
|
|
113
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
103
114
|
.toArray()[0]);
|
|
104
|
-
const readStates = (sql, group) =>
|
|
115
|
+
const readStates = (sql, ownerChannel, group) => v.parse(v.array(stateRowSchema), sql
|
|
105
116
|
.exec(`SELECT channel, value FROM durability_topology_states
|
|
106
|
-
WHERE group_id = ?`, group)
|
|
117
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
107
118
|
.toArray());
|
|
108
|
-
const parseMembers = (row) =>
|
|
119
|
+
const parseMembers = (row) => v.parse(membersSchema, JSON.parse(row.members));
|
|
109
120
|
const inRole = (member, role) => role === 'read' || member.state === 'active';
|
|
110
121
|
/**
|
|
111
122
|
* Experimental. Holds the membership of shard groups inside one coordinator
|
|
@@ -117,9 +128,11 @@ export class DurabilityTopology {
|
|
|
117
128
|
constructor(config) {
|
|
118
129
|
_DurabilityTopology_instances.add(this);
|
|
119
130
|
_DurabilityTopology_engine.set(this, void 0);
|
|
131
|
+
_DurabilityTopology_ownerChannel.set(this, void 0);
|
|
120
132
|
_DurabilityTopology_coordinator.set(this, void 0);
|
|
121
133
|
_DurabilityTopology_publication.set(this, void 0);
|
|
122
134
|
_DurabilityTopology_channels.set(this, new Map());
|
|
135
|
+
const ownerChannel = assertChannel('DurabilityTopology', config.channel ?? defaultChannel);
|
|
123
136
|
// The outboxes must share this helper's engine, so a private scheduler is
|
|
124
137
|
// created explicitly instead of letting each attach its own.
|
|
125
138
|
const scheduler = config.scheduler ?? new DurabilityScheduler(config);
|
|
@@ -128,37 +141,51 @@ export class DurabilityTopology {
|
|
|
128
141
|
if (coordinator === undefined) {
|
|
129
142
|
throw new TopologyError('DurabilityTopology needs the Durable Object context, which carries its ID');
|
|
130
143
|
}
|
|
144
|
+
__classPrivateFieldSet(this, _DurabilityTopology_ownerChannel, ownerChannel, "f");
|
|
131
145
|
__classPrivateFieldSet(this, _DurabilityTopology_coordinator, coordinator, "f");
|
|
132
146
|
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").migrateSchema('topology', durabilityTopologyMigrations);
|
|
147
|
+
const publisher = config.publish;
|
|
148
|
+
if (publisher !== undefined &&
|
|
149
|
+
(typeof publisher.write !== 'function' ||
|
|
150
|
+
typeof publisher.delete !== 'function')) {
|
|
151
|
+
throw new TopologyError('publish needs write and delete functions');
|
|
152
|
+
}
|
|
153
|
+
const publication = publisher === undefined
|
|
154
|
+
? undefined
|
|
155
|
+
: { publisher, timeoutMs: timeoutOf(publisher, config) };
|
|
156
|
+
const states = config.states ?? {};
|
|
157
|
+
const channels = Object.entries(states).map(([name, channel]) => {
|
|
158
|
+
if (!name || typeof channel.deliver !== 'function') {
|
|
159
|
+
throw new TopologyError(`Invalid topology channel "${name}"`);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
name,
|
|
163
|
+
channel,
|
|
164
|
+
timeoutMs: timeoutOf(channel, config),
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
// Validate before claiming so config validation failures leave the channel
|
|
168
|
+
// free. Even without outboxes, an instance still owns its channel's groups.
|
|
169
|
+
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").claimChannel('durability_topology_groups', ownerChannel, 'DurabilityTopology');
|
|
133
170
|
// The outbox only emits outbox events, which is all this hook accepts.
|
|
134
171
|
const onLifecycleEvent = config.onLifecycleEvent;
|
|
135
172
|
const lifecycle = onLifecycleEvent === undefined ? {} : { onLifecycleEvent };
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (typeof publisher.write !== 'function' ||
|
|
139
|
-
typeof publisher.delete !== 'function') {
|
|
140
|
-
throw new TopologyError('publish needs write and delete functions');
|
|
141
|
-
}
|
|
142
|
-
const timeoutMs = timeoutOf(publisher, config);
|
|
173
|
+
if (publication !== undefined) {
|
|
174
|
+
const { timeoutMs } = publication;
|
|
143
175
|
__classPrivateFieldSet(this, _DurabilityTopology_publication, new DurabilityOutbox({
|
|
144
176
|
scheduler,
|
|
145
|
-
channel:
|
|
146
|
-
...outboxOptions(timeoutMs, publisher, config),
|
|
177
|
+
channel: publicationChannelFor(ownerChannel),
|
|
178
|
+
...outboxOptions(timeoutMs, publication.publisher, config),
|
|
147
179
|
...lifecycle,
|
|
148
|
-
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publisher, entries, timeoutMs),
|
|
180
|
+
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publication.publisher, entries, timeoutMs),
|
|
149
181
|
}), "f");
|
|
150
182
|
}
|
|
151
|
-
const
|
|
152
|
-
for (const [name, channel] of Object.entries(channels)) {
|
|
153
|
-
if (!name || typeof channel.deliver !== 'function') {
|
|
154
|
-
throw new TopologyError(`Invalid topology channel "${name}"`);
|
|
155
|
-
}
|
|
156
|
-
const timeoutMs = timeoutOf(channel, config);
|
|
183
|
+
for (const { name, channel, timeoutMs } of channels) {
|
|
157
184
|
__classPrivateFieldGet(this, _DurabilityTopology_channels, "f").set(name, {
|
|
158
185
|
role: channel.members ?? 'write',
|
|
159
186
|
outbox: new DurabilityOutbox({
|
|
160
187
|
scheduler,
|
|
161
|
-
channel:
|
|
188
|
+
channel: stateChannelFor(ownerChannel, name),
|
|
162
189
|
...outboxOptions(timeoutMs, channel, config),
|
|
163
190
|
...lifecycle,
|
|
164
191
|
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_deliver).call(this, name, channel, entries, timeoutMs),
|
|
@@ -188,10 +215,10 @@ export class DurabilityTopology {
|
|
|
188
215
|
async update(group, next) {
|
|
189
216
|
assertGroup(group);
|
|
190
217
|
const snapshot = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
191
|
-
const row = readGroup(sql, group);
|
|
218
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
192
219
|
const live = row?.deleted === 0 ? row : undefined;
|
|
193
220
|
const before = live ? parseMembers(live) : [];
|
|
194
|
-
const stateRows = readStates(sql, group);
|
|
221
|
+
const stateRows = readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
195
222
|
const states = Object.fromEntries(stateRows.map((state) => [state.channel, deserialize(state.value)]));
|
|
196
223
|
// A copy, so an updater that mutates `current` cannot make a change look like a no-op.
|
|
197
224
|
const draft = next({ members: structuredClone(before), states });
|
|
@@ -215,13 +242,13 @@ export class DurabilityTopology {
|
|
|
215
242
|
};
|
|
216
243
|
if (changed) {
|
|
217
244
|
sql.exec(`INSERT INTO durability_topology_groups
|
|
218
|
-
(group_id, version, members, deleted, updated_at)
|
|
219
|
-
VALUES (?, ?, ?, 0, ?)
|
|
220
|
-
ON CONFLICT(group_id) DO UPDATE SET
|
|
245
|
+
(owner_channel, group_id, version, members, deleted, updated_at)
|
|
246
|
+
VALUES (?, ?, ?, ?, 0, ?)
|
|
247
|
+
ON CONFLICT(owner_channel, group_id) DO UPDATE SET
|
|
221
248
|
version = excluded.version,
|
|
222
249
|
members = excluded.members,
|
|
223
250
|
deleted = 0,
|
|
224
|
-
updated_at = excluded.updated_at`, group, version, JSON.stringify(after), now);
|
|
251
|
+
updated_at = excluded.updated_at`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, version, JSON.stringify(after), now);
|
|
225
252
|
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, { snapshot: current });
|
|
226
253
|
}
|
|
227
254
|
const changedChannels = __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_writeStates).call(this, sql, group, changes, stateRows);
|
|
@@ -239,15 +266,16 @@ export class DurabilityTopology {
|
|
|
239
266
|
async delete(group) {
|
|
240
267
|
assertGroup(group);
|
|
241
268
|
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
242
|
-
const row = readGroup(sql, group);
|
|
269
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
243
270
|
if (row?.deleted !== 0) {
|
|
244
271
|
return;
|
|
245
272
|
}
|
|
246
273
|
const version = row.version + 1;
|
|
247
274
|
sql.exec(`UPDATE durability_topology_groups
|
|
248
275
|
SET version = ?, members = '[]', deleted = 1, updated_at = ?
|
|
249
|
-
WHERE group_id = ?`, version, Date.now(), group);
|
|
250
|
-
sql.exec(
|
|
276
|
+
WHERE owner_channel = ? AND group_id = ?`, version, Date.now(), __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
277
|
+
sql.exec(`DELETE FROM durability_topology_states
|
|
278
|
+
WHERE owner_channel = ? AND group_id = ?`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
251
279
|
for (const member of parseMembers(row)) {
|
|
252
280
|
for (const channel of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f").values()) {
|
|
253
281
|
discardWithin(channel.outbox, sql, deliveryKey(group, member.id));
|
|
@@ -260,7 +288,7 @@ export class DurabilityTopology {
|
|
|
260
288
|
/** Coordinator-local read. Readers elsewhere use the published copy. */
|
|
261
289
|
snapshot(group) {
|
|
262
290
|
assertGroup(group);
|
|
263
|
-
const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, group);
|
|
291
|
+
const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
264
292
|
if (row?.deleted !== 0) {
|
|
265
293
|
return undefined;
|
|
266
294
|
}
|
|
@@ -272,7 +300,7 @@ export class DurabilityTopology {
|
|
|
272
300
|
};
|
|
273
301
|
}
|
|
274
302
|
}
|
|
275
|
-
_DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
|
|
303
|
+
_DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_ownerChannel = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
|
|
276
304
|
return __classPrivateFieldGet(this, _DurabilityTopology_publication, "f")
|
|
277
305
|
? publishNow(__classPrivateFieldGet(this, _DurabilityTopology_publication, "f"), group)
|
|
278
306
|
: Promise.resolve(true);
|
|
@@ -296,8 +324,9 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
296
324
|
}));
|
|
297
325
|
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
298
326
|
for (const { entry, id } of writes) {
|
|
299
|
-
sql.exec(`INSERT INTO durability_topology_writes
|
|
300
|
-
|
|
327
|
+
sql.exec(`INSERT INTO durability_topology_writes
|
|
328
|
+
(owner_channel, id, group_id, land_by)
|
|
329
|
+
VALUES (?, ?, ?, ?)`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id, entry.key, landBy);
|
|
301
330
|
}
|
|
302
331
|
});
|
|
303
332
|
await Promise.all(writes.map(async ({ entry, id }) => {
|
|
@@ -314,15 +343,15 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
314
343
|
}
|
|
315
344
|
else {
|
|
316
345
|
// A rejected write settled before any newer write of this group started.
|
|
317
|
-
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, id));
|
|
346
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id));
|
|
318
347
|
}
|
|
319
348
|
reportFailure('durability.topology.publish_failed', { group: entry.key, attempt: entry.attempt }, error);
|
|
320
349
|
entry.retry();
|
|
321
350
|
return;
|
|
322
351
|
}
|
|
323
352
|
const outstandingUntil = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
324
|
-
forgetWrite(sql, id);
|
|
325
|
-
return outstandingWritesUntil(sql, entry.key, Date.now());
|
|
353
|
+
forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
|
|
354
|
+
return outstandingWritesUntil(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), entry.key, Date.now());
|
|
326
355
|
});
|
|
327
356
|
if (outstandingUntil !== undefined) {
|
|
328
357
|
entry.retry(Math.min(outstandingUntil - Date.now(), maxTimeoutMs));
|
|
@@ -333,13 +362,13 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
333
362
|
const settled = write
|
|
334
363
|
.then(() => undefined, () => undefined)
|
|
335
364
|
.then(() => __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
336
|
-
forgetWrite(sql, id);
|
|
365
|
+
forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
|
|
337
366
|
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stageLatest).call(this, sql, group);
|
|
338
367
|
}))
|
|
339
368
|
.catch(onError);
|
|
340
369
|
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").waitUntil(settled, onError);
|
|
341
370
|
}, _DurabilityTopology_stageLatest = function _DurabilityTopology_stageLatest(sql, group) {
|
|
342
|
-
const row = readGroup(sql, group);
|
|
371
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
343
372
|
if (!row) {
|
|
344
373
|
return;
|
|
345
374
|
}
|
|
@@ -397,11 +426,11 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
397
426
|
const joinedAt = new Map(before.map((member) => [member.id, member.joinedAt]));
|
|
398
427
|
const seen = new Set();
|
|
399
428
|
const members = inputs.map((input) => {
|
|
400
|
-
const parsed =
|
|
429
|
+
const parsed = v.safeParse(memberInputSchema, input);
|
|
401
430
|
if (!parsed.success) {
|
|
402
|
-
throw new TopologyError(`Invalid member: ${parsed.
|
|
431
|
+
throw new TopologyError(`Invalid member: ${v.summarize(parsed.issues)}`);
|
|
403
432
|
}
|
|
404
|
-
const { id, state, locationHint, meta } = parsed.
|
|
433
|
+
const { id, state, locationHint, meta } = parsed.output;
|
|
405
434
|
if (seen.has(id)) {
|
|
406
435
|
throw new TopologyError(`Duplicate member "${id}"`);
|
|
407
436
|
}
|
|
@@ -430,14 +459,19 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
430
459
|
if (previous !== undefined && canonical(previous) === canonical(stored)) {
|
|
431
460
|
continue;
|
|
432
461
|
}
|
|
433
|
-
sql.exec(`INSERT INTO durability_topology_states
|
|
434
|
-
|
|
435
|
-
|
|
462
|
+
sql.exec(`INSERT INTO durability_topology_states
|
|
463
|
+
(owner_channel, group_id, channel, value)
|
|
464
|
+
VALUES (?, ?, ?, ?)
|
|
465
|
+
ON CONFLICT(owner_channel, group_id, channel) DO UPDATE SET
|
|
466
|
+
value = excluded.value`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, channel, stored);
|
|
436
467
|
changed.add(channel);
|
|
437
468
|
}
|
|
438
469
|
return changed;
|
|
439
470
|
}, _DurabilityTopology_stageDeliveries = function _DurabilityTopology_stageDeliveries(sql, group, topologyVersion, before, after, changed) {
|
|
440
|
-
const values = new Map(readStates(sql, group).map((row) => [
|
|
471
|
+
const values = new Map(readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group).map((row) => [
|
|
472
|
+
row.channel,
|
|
473
|
+
row.value,
|
|
474
|
+
]));
|
|
441
475
|
for (const [name, channel] of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f")) {
|
|
442
476
|
const previous = new Set(before
|
|
443
477
|
.filter((member) => inRole(member, channel.role))
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the experimental topology coordinator.
|
|
3
|
+
* Publication and sync ride on outbox channels. Applied automatically on
|
|
4
|
+
* construction.
|
|
5
|
+
*/
|
|
6
|
+
export declare const durabilityTopologyMigrations: {
|
|
7
|
+
name: string;
|
|
8
|
+
up: string;
|
|
9
|
+
down: string;
|
|
10
|
+
}[];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the experimental topology coordinator.
|
|
3
|
+
* Publication and sync ride on outbox channels. Applied automatically on
|
|
4
|
+
* construction.
|
|
5
|
+
*/
|
|
6
|
+
export const durabilityTopologyMigrations = [
|
|
7
|
+
{
|
|
8
|
+
name: 'durability_0011_create_topology',
|
|
9
|
+
up: `
|
|
10
|
+
CREATE TABLE IF NOT EXISTS durability_topology_groups (
|
|
11
|
+
group_id TEXT PRIMARY KEY,
|
|
12
|
+
version INTEGER NOT NULL,
|
|
13
|
+
members TEXT NOT NULL,
|
|
14
|
+
deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
|
|
15
|
+
updated_at INTEGER NOT NULL
|
|
16
|
+
);
|
|
17
|
+
CREATE TABLE IF NOT EXISTS durability_topology_states (
|
|
18
|
+
group_id TEXT NOT NULL,
|
|
19
|
+
channel TEXT NOT NULL,
|
|
20
|
+
value TEXT NOT NULL,
|
|
21
|
+
PRIMARY KEY (group_id, channel)
|
|
22
|
+
);
|
|
23
|
+
CREATE TABLE IF NOT EXISTS durability_topology_writes (
|
|
24
|
+
id TEXT PRIMARY KEY,
|
|
25
|
+
group_id TEXT NOT NULL,
|
|
26
|
+
land_by INTEGER NOT NULL
|
|
27
|
+
);
|
|
28
|
+
CREATE INDEX IF NOT EXISTS durability_topology_writes_group_idx
|
|
29
|
+
ON durability_topology_writes (group_id, land_by);
|
|
30
|
+
`,
|
|
31
|
+
down: `
|
|
32
|
+
DROP INDEX IF EXISTS durability_topology_writes_group_idx;
|
|
33
|
+
DROP TABLE IF EXISTS durability_topology_writes;
|
|
34
|
+
DROP TABLE IF EXISTS durability_topology_states;
|
|
35
|
+
DROP TABLE IF EXISTS durability_topology_groups;
|
|
36
|
+
`,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
// `owner_channel` is the topology instance; the existing `channel` column
|
|
40
|
+
// of durability_topology_states stays the state name.
|
|
41
|
+
name: 'durability_0022_topology_channels',
|
|
42
|
+
up: `
|
|
43
|
+
ALTER TABLE durability_topology_groups RENAME TO durability_topology_groups_before_channels;
|
|
44
|
+
CREATE TABLE durability_topology_groups (
|
|
45
|
+
group_id TEXT NOT NULL,
|
|
46
|
+
version INTEGER NOT NULL,
|
|
47
|
+
members TEXT NOT NULL,
|
|
48
|
+
deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
|
|
49
|
+
updated_at INTEGER NOT NULL,
|
|
50
|
+
owner_channel TEXT NOT NULL DEFAULT 'default',
|
|
51
|
+
PRIMARY KEY (owner_channel, group_id)
|
|
52
|
+
);
|
|
53
|
+
INSERT INTO durability_topology_groups
|
|
54
|
+
(group_id, version, members, deleted, updated_at, owner_channel)
|
|
55
|
+
SELECT group_id, version, members, deleted, updated_at, 'default'
|
|
56
|
+
FROM durability_topology_groups_before_channels;
|
|
57
|
+
DROP TABLE durability_topology_groups_before_channels;
|
|
58
|
+
|
|
59
|
+
ALTER TABLE durability_topology_states RENAME TO durability_topology_states_before_channels;
|
|
60
|
+
CREATE TABLE durability_topology_states (
|
|
61
|
+
group_id TEXT NOT NULL,
|
|
62
|
+
channel TEXT NOT NULL,
|
|
63
|
+
value TEXT NOT NULL,
|
|
64
|
+
owner_channel TEXT NOT NULL DEFAULT 'default',
|
|
65
|
+
PRIMARY KEY (owner_channel, group_id, channel)
|
|
66
|
+
);
|
|
67
|
+
INSERT INTO durability_topology_states
|
|
68
|
+
(group_id, channel, value, owner_channel)
|
|
69
|
+
SELECT group_id, channel, value, 'default'
|
|
70
|
+
FROM durability_topology_states_before_channels;
|
|
71
|
+
DROP TABLE durability_topology_states_before_channels;
|
|
72
|
+
|
|
73
|
+
ALTER TABLE durability_topology_writes RENAME TO durability_topology_writes_before_channels;
|
|
74
|
+
CREATE TABLE durability_topology_writes (
|
|
75
|
+
id TEXT NOT NULL,
|
|
76
|
+
group_id TEXT NOT NULL,
|
|
77
|
+
land_by INTEGER NOT NULL,
|
|
78
|
+
owner_channel TEXT NOT NULL DEFAULT 'default',
|
|
79
|
+
PRIMARY KEY (owner_channel, id)
|
|
80
|
+
);
|
|
81
|
+
INSERT INTO durability_topology_writes
|
|
82
|
+
(id, group_id, land_by, owner_channel)
|
|
83
|
+
SELECT id, group_id, land_by, 'default'
|
|
84
|
+
FROM durability_topology_writes_before_channels;
|
|
85
|
+
DROP TABLE durability_topology_writes_before_channels;
|
|
86
|
+
CREATE INDEX durability_topology_writes_group_idx
|
|
87
|
+
ON durability_topology_writes (owner_channel, group_id, land_by);
|
|
88
|
+
`,
|
|
89
|
+
down: `
|
|
90
|
+
CREATE TABLE durability_topology_channel_guard (
|
|
91
|
+
owner_channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (owner_channel = 'default')
|
|
92
|
+
);
|
|
93
|
+
INSERT INTO durability_topology_channel_guard
|
|
94
|
+
SELECT owner_channel FROM durability_topology_groups
|
|
95
|
+
UNION SELECT owner_channel FROM durability_topology_states
|
|
96
|
+
UNION SELECT owner_channel FROM durability_topology_writes;
|
|
97
|
+
DROP TABLE durability_topology_channel_guard;
|
|
98
|
+
|
|
99
|
+
ALTER TABLE durability_topology_groups RENAME TO durability_topology_groups_with_channels;
|
|
100
|
+
CREATE TABLE durability_topology_groups (
|
|
101
|
+
group_id TEXT PRIMARY KEY,
|
|
102
|
+
version INTEGER NOT NULL,
|
|
103
|
+
members TEXT NOT NULL,
|
|
104
|
+
deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
|
|
105
|
+
updated_at INTEGER NOT NULL
|
|
106
|
+
);
|
|
107
|
+
INSERT INTO durability_topology_groups
|
|
108
|
+
SELECT group_id, version, members, deleted, updated_at
|
|
109
|
+
FROM durability_topology_groups_with_channels;
|
|
110
|
+
DROP TABLE durability_topology_groups_with_channels;
|
|
111
|
+
|
|
112
|
+
ALTER TABLE durability_topology_states RENAME TO durability_topology_states_with_channels;
|
|
113
|
+
CREATE TABLE durability_topology_states (
|
|
114
|
+
group_id TEXT NOT NULL,
|
|
115
|
+
channel TEXT NOT NULL,
|
|
116
|
+
value TEXT NOT NULL,
|
|
117
|
+
PRIMARY KEY (group_id, channel)
|
|
118
|
+
);
|
|
119
|
+
INSERT INTO durability_topology_states
|
|
120
|
+
SELECT group_id, channel, value
|
|
121
|
+
FROM durability_topology_states_with_channels;
|
|
122
|
+
DROP TABLE durability_topology_states_with_channels;
|
|
123
|
+
|
|
124
|
+
ALTER TABLE durability_topology_writes RENAME TO durability_topology_writes_with_channels;
|
|
125
|
+
CREATE TABLE durability_topology_writes (
|
|
126
|
+
id TEXT PRIMARY KEY,
|
|
127
|
+
group_id TEXT NOT NULL,
|
|
128
|
+
land_by INTEGER NOT NULL
|
|
129
|
+
);
|
|
130
|
+
INSERT INTO durability_topology_writes
|
|
131
|
+
SELECT id, group_id, land_by
|
|
132
|
+
FROM durability_topology_writes_with_channels;
|
|
133
|
+
DROP TABLE durability_topology_writes_with_channels;
|
|
134
|
+
CREATE INDEX durability_topology_writes_group_idx
|
|
135
|
+
ON durability_topology_writes (group_id, land_by);
|
|
136
|
+
`,
|
|
137
|
+
},
|
|
138
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
/** `active` members are in the write and read sets; `draining` members only in the read set. */
|
|
3
|
+
export type TopologyMemberState = 'active' | 'draining';
|
|
4
|
+
type MetaField<Meta> = undefined extends Meta ? {
|
|
5
|
+
meta?: Meta;
|
|
6
|
+
} : {
|
|
7
|
+
meta: Meta;
|
|
8
|
+
};
|
|
9
|
+
/** A member as the application declares it. */
|
|
10
|
+
export type TopologyMemberInput<Meta = undefined> = {
|
|
11
|
+
/** The member's Durable Object name. */
|
|
12
|
+
id: string;
|
|
13
|
+
state: TopologyMemberState;
|
|
14
|
+
locationHint?: DurableObjectLocationHint;
|
|
15
|
+
} & MetaField<Meta>;
|
|
16
|
+
export type TopologyMember<Meta = undefined> = TopologyMemberInput<Meta> & {
|
|
17
|
+
joinedAt: number;
|
|
18
|
+
};
|
|
19
|
+
export type TopologySnapshot<Meta = undefined> = {
|
|
20
|
+
group: string;
|
|
21
|
+
/** Increases on every membership change, and keeps increasing across deletes. */
|
|
22
|
+
version: number;
|
|
23
|
+
/** ID of the coordinator Durable Object that published this snapshot. */
|
|
24
|
+
coordinator: string;
|
|
25
|
+
/** Sorted by ID. */
|
|
26
|
+
members: readonly TopologyMember<Meta>[];
|
|
27
|
+
};
|
|
28
|
+
export declare const memberInputSchema: v.ObjectSchema<{
|
|
29
|
+
readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
|
|
30
|
+
readonly state: v.PicklistSchema<["active", "draining"], undefined>;
|
|
31
|
+
readonly locationHint: v.OptionalSchema<v.PicklistSchema<readonly ["wnam", "enam", "sam", "weur", "eeur", "apac", "oc", "afr", "me"], undefined>, undefined>;
|
|
32
|
+
readonly meta: v.OptionalSchema<v.CustomSchema<import("../serialization.js").Json, v.ErrorMessage<v.CustomIssue> | undefined>, undefined>;
|
|
33
|
+
}, undefined>;
|
|
34
|
+
export declare const membersSchema: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
|
|
35
|
+
readonly joinedAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
36
|
+
readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
|
|
37
|
+
readonly state: v.PicklistSchema<["active", "draining"], undefined>;
|
|
38
|
+
readonly locationHint: v.OptionalSchema<v.PicklistSchema<readonly ["wnam", "enam", "sam", "weur", "eeur", "apac", "oc", "afr", "me"], undefined>, undefined>;
|
|
39
|
+
readonly meta: v.OptionalSchema<v.CustomSchema<import("../serialization.js").Json, v.ErrorMessage<v.CustomIssue> | undefined>, undefined>;
|
|
40
|
+
}, undefined>, undefined>, v.CheckAction<{
|
|
41
|
+
joinedAt: number;
|
|
42
|
+
id: string;
|
|
43
|
+
state: "active" | "draining";
|
|
44
|
+
locationHint?: "wnam" | "enam" | "sam" | "weur" | "eeur" | "apac" | "oc" | "afr" | "me" | undefined;
|
|
45
|
+
meta?: import("../serialization.js").Json | undefined;
|
|
46
|
+
}[], "Members must be sorted by ID and unique">]>;
|
|
47
|
+
export declare const encodeSnapshot: <Meta>(snapshot: TopologySnapshot<Meta>) => string;
|
|
48
|
+
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
49
|
+
export declare const decodeSnapshot: <Meta>(encoded: string) => TopologySnapshot<Meta>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { jsonSchema } from '../serialization.js';
|
|
3
|
+
const locationHints = [
|
|
4
|
+
'wnam',
|
|
5
|
+
'enam',
|
|
6
|
+
'sam',
|
|
7
|
+
'weur',
|
|
8
|
+
'eeur',
|
|
9
|
+
'apac',
|
|
10
|
+
'oc',
|
|
11
|
+
'afr',
|
|
12
|
+
'me',
|
|
13
|
+
];
|
|
14
|
+
// Unknown keys are stripped, so current members (which carry `joinedAt`) can be returned as input.
|
|
15
|
+
export const memberInputSchema = v.object({
|
|
16
|
+
id: v.pipe(v.string(), v.minLength(1)),
|
|
17
|
+
state: v.picklist(['active', 'draining']),
|
|
18
|
+
locationHint: v.optional(v.picklist(locationHints)),
|
|
19
|
+
meta: v.optional(jsonSchema),
|
|
20
|
+
});
|
|
21
|
+
const memberSchema = v.object({
|
|
22
|
+
...memberInputSchema.entries,
|
|
23
|
+
joinedAt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
24
|
+
});
|
|
25
|
+
export const membersSchema = v.pipe(v.array(memberSchema), v.check((members) => members.every((member, index) => index === 0 || members[index - 1].id < member.id), 'Members must be sorted by ID and unique'));
|
|
26
|
+
const encodedSchema = v.object({
|
|
27
|
+
format: v.literal(1),
|
|
28
|
+
group: v.pipe(v.string(), v.minLength(1)),
|
|
29
|
+
version: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
30
|
+
coordinator: v.pipe(v.string(), v.minLength(1)),
|
|
31
|
+
members: v.pipe(membersSchema, v.minLength(1)),
|
|
32
|
+
});
|
|
33
|
+
export const encodeSnapshot = (snapshot) => JSON.stringify({ format: 1, ...snapshot });
|
|
34
|
+
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
35
|
+
export const decodeSnapshot = (encoded) => {
|
|
36
|
+
const { format: _format, ...snapshot } = v.parse(encodedSchema, JSON.parse(encoded));
|
|
37
|
+
return snapshot;
|
|
38
|
+
};
|