durability 5.2.0 → 5.3.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 +28 -13
- package/dist/alarms/index.js +18 -21
- package/dist/fanout/index.js +40 -53
- package/dist/log/index.d.ts +12 -17
- package/dist/log/index.js +59 -53
- package/dist/memoize/index.js +9 -10
- package/dist/migrations.js +48 -52
- package/dist/operations/index.js +22 -28
- package/dist/outbox/index.js +20 -31
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/schedule/index.d.ts +13 -1
- package/dist/schedule/index.js +73 -60
- package/dist/scheduler/index.d.ts +19 -1
- package/dist/scheduler/index.js +53 -54
- package/dist/semaphore/client.js +6 -5
- package/dist/semaphore/protocol.d.ts +446 -234
- package/dist/semaphore/protocol.js +46 -61
- package/dist/semaphore/queue-store.d.ts +58 -52
- package/dist/semaphore/queue-store.js +55 -57
- package/dist/semaphore/queue.js +2 -15
- package/dist/semaphore/server.js +24 -14
- package/dist/semaphore/store.d.ts +30 -25
- package/dist/semaphore/store.js +31 -34
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/shared-timer.d.ts +27 -0
- package/dist/shared-timer.js +191 -0
- package/dist/socket/client.js +25 -22
- package/dist/storage.js +9 -9
- package/dist/topology/client.js +29 -15
- package/dist/topology/index.js +18 -32
- package/dist/topology/snapshot.d.ts +20 -40
- package/dist/topology/snapshot.js +18 -28
- package/dist/tracker/index.js +15 -12
- package/package.json +4 -4
package/dist/storage.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
export const channelClause = (table) => table.channel === undefined ? '' : ' AND channel = ?';
|
|
3
3
|
export const channelBindings = (table) => table.channel === undefined ? [] : [table.channel];
|
|
4
4
|
// Keep existing memo owners on the default channel, without restricting record IDs.
|
|
5
5
|
export const tableOwner = (table) => table.channel === undefined || table.channel === 'default'
|
|
6
6
|
? table.name
|
|
7
7
|
: JSON.stringify([table.name, table.channel]);
|
|
8
|
-
const attemptRowSchema =
|
|
9
|
-
attempt:
|
|
8
|
+
const attemptRowSchema = v.object({
|
|
9
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
10
10
|
});
|
|
11
11
|
// Every write is conditional on the record's generation and pending status, so a
|
|
12
12
|
// stale execution can never overwrite a replacement or a later attempt.
|
|
@@ -17,7 +17,7 @@ export const pendingGeneration = (table) => `${table.key} = ? AND generation_id
|
|
|
17
17
|
*/
|
|
18
18
|
export const claimAttempt = (sql, table, key, generation, maxAttempts, nextAttemptAt) => {
|
|
19
19
|
const deadlineAssignment = nextAttemptAt === undefined ? '' : ', next_attempt_at = ?';
|
|
20
|
-
return
|
|
20
|
+
return v.parse(v.optional(attemptRowSchema), sql
|
|
21
21
|
.exec(`UPDATE ${table.name}
|
|
22
22
|
SET attempt = attempt + 1${deadlineAssignment}
|
|
23
23
|
WHERE ${pendingGeneration(table)} AND attempt < ?
|
|
@@ -41,15 +41,15 @@ export const deleteAttempt = (sql, table, ref) => sql
|
|
|
41
41
|
RETURNING ${table.key}`, ref.key, ref.generation, ...channelBindings(table), ref.attempt)
|
|
42
42
|
.toArray().length > 0;
|
|
43
43
|
/** Fails a generation that has used every attempt and returns its attempt count. */
|
|
44
|
-
export const exhaustAttempts = (sql, table, key, generation, maxAttempts, error) =>
|
|
44
|
+
export const exhaustAttempts = (sql, table, key, generation, maxAttempts, error) => v.parse(v.optional(attemptRowSchema), sql
|
|
45
45
|
.exec(`UPDATE ${table.name}
|
|
46
46
|
SET status = 'failed', last_error = ?, last_error_name = ?
|
|
47
47
|
WHERE ${pendingGeneration(table)} AND attempt >= ?
|
|
48
48
|
RETURNING attempt`, error.message, error.name, key, generation, ...channelBindings(table), maxAttempts)
|
|
49
49
|
.toArray()[0])?.attempt;
|
|
50
|
-
const createdCursorSchema =
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const createdCursorSchema = v.strictTuple([
|
|
51
|
+
v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
52
|
+
v.string(),
|
|
53
53
|
]);
|
|
54
54
|
/**
|
|
55
55
|
* Lists records in creation order, then by key, filtered by status and by the
|
|
@@ -58,7 +58,7 @@ const createdCursorSchema = z.tuple([
|
|
|
58
58
|
export const listCreated = (sql, table, nameColumn, query) => {
|
|
59
59
|
const [createdAt, key] = query.after === undefined
|
|
60
60
|
? [-1, '']
|
|
61
|
-
:
|
|
61
|
+
: v.parse(createdCursorSchema, JSON.parse(query.after));
|
|
62
62
|
const status = query.status ?? null;
|
|
63
63
|
const name = query.name ?? null;
|
|
64
64
|
const found = sql
|
package/dist/topology/client.js
CHANGED
|
@@ -74,32 +74,46 @@ _TopologyClient_config = new WeakMap(), _TopologyClient_readTimeoutMs = new Weak
|
|
|
74
74
|
const previous = __classPrivateFieldGet(this, _TopologyClient_cache, "f").get(group);
|
|
75
75
|
const result = await __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_read).call(this, group);
|
|
76
76
|
const now = Date.now();
|
|
77
|
+
let failure;
|
|
77
78
|
if ('snapshot' in result) {
|
|
78
79
|
const { snapshot } = result;
|
|
79
80
|
// The store is eventually consistent; a lagging replica must not move readers backwards.
|
|
80
81
|
// Versions only compare within one coordinator: a group moved to another starts over.
|
|
81
|
-
const
|
|
82
|
-
previous.
|
|
83
|
-
previous.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
82
|
+
const outdated = previous?.newestSeen !== undefined &&
|
|
83
|
+
previous.newestSeen.coordinator === snapshot.coordinator &&
|
|
84
|
+
previous.newestSeen.version > snapshot.version;
|
|
85
|
+
if (!outdated || previous.view !== undefined) {
|
|
86
|
+
const view = outdated
|
|
87
|
+
? previous.view
|
|
88
|
+
: { ...snapshot, source: 'published', fetchedAt: now };
|
|
89
|
+
const expiresAt = now + jittered(__classPrivateFieldGet(this, _TopologyClient_ttlMs, "f"));
|
|
90
|
+
__classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_remember).call(this, group, {
|
|
91
|
+
view,
|
|
92
|
+
newestSeen: {
|
|
93
|
+
coordinator: view.coordinator,
|
|
94
|
+
version: view.version,
|
|
95
|
+
},
|
|
96
|
+
expiresAt,
|
|
97
|
+
staleUntil: expiresAt + (__classPrivateFieldGet(this, _TopologyClient_config, "f").staleIfErrorMs ?? 0),
|
|
98
|
+
});
|
|
99
|
+
return view;
|
|
100
|
+
}
|
|
101
|
+
// Only a failed read leaves a newer version seen without a view, so the
|
|
102
|
+
// older snapshot answers the same way that read did.
|
|
103
|
+
failure = previous.failure;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
failure = result.failure;
|
|
94
107
|
}
|
|
95
108
|
const entry = {
|
|
96
109
|
// A missing group is not an error, so nothing stale may be served for it.
|
|
97
|
-
...(
|
|
110
|
+
...(failure.reason === 'missing' || !previous?.view
|
|
98
111
|
? {}
|
|
99
112
|
: { view: previous.view }),
|
|
113
|
+
...(previous?.newestSeen ? { newestSeen: previous.newestSeen } : {}),
|
|
100
114
|
staleUntil: previous?.staleUntil ?? 0,
|
|
101
115
|
expiresAt: now + jittered(__classPrivateFieldGet(this, _TopologyClient_negativeTtlMs, "f")),
|
|
102
|
-
failure
|
|
116
|
+
failure,
|
|
103
117
|
};
|
|
104
118
|
__classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_remember).call(this, group, entry);
|
|
105
119
|
return __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_fallback).call(this, group, entry);
|
package/dist/topology/index.js
CHANGED
|
@@ -10,7 +10,7 @@ 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 _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
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { reportFailure, TopologyError } from '../errors.js';
|
|
15
15
|
import { durabilityTopologyMigrations } from './migrations.js';
|
|
16
16
|
import { migrateCapability } from '../migrations.js';
|
|
@@ -29,15 +29,15 @@ const publicationChannelFor = (ownerChannel) => ownerChannel === defaultChannel
|
|
|
29
29
|
const stateChannelFor = (ownerChannel, state) => ownerChannel === defaultChannel
|
|
30
30
|
? `${outboxChannelPrefix}.${state}`
|
|
31
31
|
: JSON.stringify([outboxChannelPrefix, ownerChannel, 'state', state]);
|
|
32
|
-
const groupRowSchema =
|
|
33
|
-
group_id:
|
|
34
|
-
version:
|
|
35
|
-
members:
|
|
36
|
-
deleted:
|
|
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)]),
|
|
37
37
|
});
|
|
38
|
-
const stateRowSchema =
|
|
39
|
-
channel:
|
|
40
|
-
value:
|
|
38
|
+
const stateRowSchema = v.object({
|
|
39
|
+
channel: v.string(),
|
|
40
|
+
value: v.string(),
|
|
41
41
|
});
|
|
42
42
|
class TopologyTimeoutError extends Error {
|
|
43
43
|
constructor(label, timeoutMs) {
|
|
@@ -45,20 +45,6 @@ class TopologyTimeoutError extends Error {
|
|
|
45
45
|
this.name = 'TopologyTimeoutError';
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
const withTimeout = async (work, timeoutMs, label) => {
|
|
49
|
-
let timer;
|
|
50
|
-
try {
|
|
51
|
-
return await Promise.race([
|
|
52
|
-
work,
|
|
53
|
-
new Promise((_resolve, reject) => {
|
|
54
|
-
timer = setTimeout(() => reject(new TopologyTimeoutError(label, timeoutMs)), timeoutMs);
|
|
55
|
-
}),
|
|
56
|
-
]);
|
|
57
|
-
}
|
|
58
|
-
finally {
|
|
59
|
-
clearTimeout(timer);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
48
|
// setTimeout overflows past this, and the outbox enforces the same ceiling.
|
|
63
49
|
const maxTimeoutMs = 2 ** 31 - 1;
|
|
64
50
|
const timeoutOf = (options, fallback) => {
|
|
@@ -92,7 +78,7 @@ const forgetWrite = (sql, ownerChannel, id) => {
|
|
|
92
78
|
const outstandingWritesUntil = (sql, ownerChannel, group, now) => {
|
|
93
79
|
sql.exec(`DELETE FROM durability_topology_writes
|
|
94
80
|
WHERE owner_channel = ? AND group_id = ? AND land_by <= ?`, ownerChannel, group, now);
|
|
95
|
-
const { until } =
|
|
81
|
+
const { until } = v.parse(v.object({ until: v.nullable(v.pipe(v.number(), v.safeInteger())) }), sql
|
|
96
82
|
.exec(`SELECT MAX(land_by) AS until FROM durability_topology_writes
|
|
97
83
|
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
98
84
|
.toArray()[0]);
|
|
@@ -107,16 +93,16 @@ const deliveryKey = (group, member) => JSON.stringify([group, member]);
|
|
|
107
93
|
const isThenable = (value) => value !== null &&
|
|
108
94
|
typeof value === 'object' &&
|
|
109
95
|
typeof Reflect.get(value, 'then') === 'function';
|
|
110
|
-
const readGroup = (sql, ownerChannel, group) =>
|
|
96
|
+
const readGroup = (sql, ownerChannel, group) => v.parse(v.optional(groupRowSchema), sql
|
|
111
97
|
.exec(`SELECT group_id, version, members, deleted
|
|
112
98
|
FROM durability_topology_groups
|
|
113
99
|
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
114
100
|
.toArray()[0]);
|
|
115
|
-
const readStates = (sql, ownerChannel, group) =>
|
|
101
|
+
const readStates = (sql, ownerChannel, group) => v.parse(v.array(stateRowSchema), sql
|
|
116
102
|
.exec(`SELECT channel, value FROM durability_topology_states
|
|
117
103
|
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
118
104
|
.toArray());
|
|
119
|
-
const parseMembers = (row) =>
|
|
105
|
+
const parseMembers = (row) => v.parse(membersSchema, JSON.parse(row.members));
|
|
120
106
|
const inRole = (member, role) => role === 'read' || member.state === 'active';
|
|
121
107
|
/**
|
|
122
108
|
* Experimental. Holds the membership of shard groups inside one coordinator
|
|
@@ -335,7 +321,7 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
335
321
|
? publisher.delete(publication.deleted.group, publication.deleted.version)
|
|
336
322
|
: publisher.write(publication.snapshot, encodeSnapshot(publication.snapshot)));
|
|
337
323
|
try {
|
|
338
|
-
await withTimeout(write, timeoutMs, 'publish');
|
|
324
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").timer.withTimeout(write, timeoutMs, () => new TopologyTimeoutError('publish', timeoutMs));
|
|
339
325
|
}
|
|
340
326
|
catch (error) {
|
|
341
327
|
if (error instanceof TopologyTimeoutError) {
|
|
@@ -394,7 +380,7 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
394
380
|
};
|
|
395
381
|
try {
|
|
396
382
|
// Members ignore versions they have passed, so an abandoned delivery landing late is harmless.
|
|
397
|
-
await withTimeout(Promise.resolve().then(() => channel.deliver(member, update)), timeoutMs, 'delivery');
|
|
383
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").timer.withTimeout(Promise.resolve().then(() => channel.deliver(member, update)), timeoutMs, () => new TopologyTimeoutError('delivery', timeoutMs));
|
|
398
384
|
}
|
|
399
385
|
catch (error) {
|
|
400
386
|
reportFailure('durability.topology.delivery_failed', { group, channel: name, member: member.id, attempt: entry.attempt }, error);
|
|
@@ -426,11 +412,11 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
426
412
|
const joinedAt = new Map(before.map((member) => [member.id, member.joinedAt]));
|
|
427
413
|
const seen = new Set();
|
|
428
414
|
const members = inputs.map((input) => {
|
|
429
|
-
const parsed =
|
|
415
|
+
const parsed = v.safeParse(memberInputSchema, input);
|
|
430
416
|
if (!parsed.success) {
|
|
431
|
-
throw new TopologyError(`Invalid member: ${parsed.
|
|
417
|
+
throw new TopologyError(`Invalid member: ${v.summarize(parsed.issues)}`);
|
|
432
418
|
}
|
|
433
|
-
const { id, state, locationHint, meta } = parsed.
|
|
419
|
+
const { id, state, locationHint, meta } = parsed.output;
|
|
434
420
|
if (seen.has(id)) {
|
|
435
421
|
throw new TopologyError(`Duplicate member "${id}"`);
|
|
436
422
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
/** `active` members are in the write and read sets; `draining` members only in the read set. */
|
|
3
3
|
export type TopologyMemberState = 'active' | 'draining';
|
|
4
4
|
type MetaField<Meta> = undefined extends Meta ? {
|
|
@@ -25,45 +25,25 @@ export type TopologySnapshot<Meta = undefined> = {
|
|
|
25
25
|
/** Sorted by ID. */
|
|
26
26
|
members: readonly TopologyMember<Meta>[];
|
|
27
27
|
};
|
|
28
|
-
export declare const memberInputSchema:
|
|
29
|
-
id:
|
|
30
|
-
state:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
meta
|
|
46
|
-
},
|
|
47
|
-
export declare const membersSchema: z.ZodArray<z.ZodObject<{
|
|
48
|
-
id: z.ZodString;
|
|
49
|
-
state: z.ZodEnum<{
|
|
50
|
-
active: "active";
|
|
51
|
-
draining: "draining";
|
|
52
|
-
}>;
|
|
53
|
-
locationHint: z.ZodOptional<z.ZodEnum<{
|
|
54
|
-
wnam: "wnam";
|
|
55
|
-
enam: "enam";
|
|
56
|
-
sam: "sam";
|
|
57
|
-
weur: "weur";
|
|
58
|
-
eeur: "eeur";
|
|
59
|
-
apac: "apac";
|
|
60
|
-
oc: "oc";
|
|
61
|
-
afr: "afr";
|
|
62
|
-
me: "me";
|
|
63
|
-
}>>;
|
|
64
|
-
meta: z.ZodOptional<z.ZodJSONSchema>;
|
|
65
|
-
joinedAt: z.ZodNumber;
|
|
66
|
-
}, z.core.$strip>>;
|
|
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">]>;
|
|
67
47
|
export declare const encodeSnapshot: <Meta>(snapshot: TopologySnapshot<Meta>) => string;
|
|
68
48
|
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
69
49
|
export declare const decodeSnapshot: <Meta>(encoded: string) => TopologySnapshot<Meta>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { jsonSchema } from '../serialization.js';
|
|
2
3
|
const locationHints = [
|
|
3
4
|
'wnam',
|
|
4
5
|
'enam',
|
|
@@ -11,38 +12,27 @@ const locationHints = [
|
|
|
11
12
|
'me',
|
|
12
13
|
];
|
|
13
14
|
// Unknown keys are stripped, so current members (which carry `joinedAt`) can be returned as input.
|
|
14
|
-
export const memberInputSchema =
|
|
15
|
-
id:
|
|
16
|
-
state:
|
|
17
|
-
locationHint:
|
|
18
|
-
meta:
|
|
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),
|
|
19
20
|
});
|
|
20
|
-
const memberSchema =
|
|
21
|
-
|
|
21
|
+
const memberSchema = v.object({
|
|
22
|
+
...memberInputSchema.entries,
|
|
23
|
+
joinedAt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
22
24
|
});
|
|
23
|
-
export const membersSchema = memberSchema
|
|
24
|
-
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
message: 'Members must be sorted by ID and unique',
|
|
31
|
-
path: [index, 'id'],
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
const encodedSchema = z.object({
|
|
37
|
-
format: z.literal(1),
|
|
38
|
-
group: z.string().min(1),
|
|
39
|
-
version: z.number().int().positive(),
|
|
40
|
-
coordinator: z.string().min(1),
|
|
41
|
-
members: membersSchema.min(1),
|
|
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)),
|
|
42
32
|
});
|
|
43
33
|
export const encodeSnapshot = (snapshot) => JSON.stringify({ format: 1, ...snapshot });
|
|
44
34
|
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
45
35
|
export const decodeSnapshot = (encoded) => {
|
|
46
|
-
const { format: _format, ...snapshot } =
|
|
36
|
+
const { format: _format, ...snapshot } = v.parse(encodedSchema, JSON.parse(encoded));
|
|
47
37
|
return snapshot;
|
|
48
38
|
};
|
package/dist/tracker/index.js
CHANGED
|
@@ -9,14 +9,15 @@ 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 _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
|
|
13
|
-
import
|
|
12
|
+
var _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_timer, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceMethods } from '../dev.js';
|
|
15
|
+
import { SharedTimer } from '../shared-timer.js';
|
|
15
16
|
import { trackedSourceOf, } from './source.js';
|
|
16
|
-
const limitSchema =
|
|
17
|
-
const cursorSchema =
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const limitSchema = v.pipe(v.number(), v.safeInteger(), v.minValue(1), v.maxValue(100));
|
|
18
|
+
const cursorSchema = v.strictTuple([
|
|
19
|
+
v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
20
|
+
v.nullable(v.string()),
|
|
20
21
|
]);
|
|
21
22
|
const resultFor = (entry, record) => record?.generation === entry.generation
|
|
22
23
|
? record.state
|
|
@@ -54,6 +55,8 @@ export class DurabilityTracker {
|
|
|
54
55
|
constructor(config) {
|
|
55
56
|
_DurabilityTracker_instances.add(this);
|
|
56
57
|
_DurabilityTracker_sources.set(this, void 0);
|
|
58
|
+
// Every wait holds a timeout, and nothing limits how many waits are open.
|
|
59
|
+
_DurabilityTracker_timer.set(this, new SharedTimer());
|
|
57
60
|
__classPrivateFieldSet(this, _DurabilityTracker_sources, Object.entries(config.sources).map(([key, input]) => [
|
|
58
61
|
key,
|
|
59
62
|
trackedSourceOf(input) ?? input,
|
|
@@ -86,12 +89,12 @@ export class DurabilityTracker {
|
|
|
86
89
|
return __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_handle).call(this, resolved, itemFor(key, record));
|
|
87
90
|
}
|
|
88
91
|
}
|
|
89
|
-
_DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
|
|
90
|
-
const limit =
|
|
92
|
+
_DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_timer = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
|
|
93
|
+
const limit = v.parse(limitSchema, query.limit ?? 50);
|
|
91
94
|
const candidates = query.source === undefined ? __classPrivateFieldGet(this, _DurabilityTracker_sources, "f") : [__classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_source).call(this, query.source)];
|
|
92
95
|
const [start, startCursor] = query.cursor === undefined
|
|
93
96
|
? [0, null]
|
|
94
|
-
:
|
|
97
|
+
: v.parse(cursorSchema, JSON.parse(query.cursor));
|
|
95
98
|
const found = [];
|
|
96
99
|
for (const [offset, [key, source]] of candidates.slice(start).entries()) {
|
|
97
100
|
const index = start + offset;
|
|
@@ -135,13 +138,13 @@ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new W
|
|
|
135
138
|
return new Promise((resolve, reject) => {
|
|
136
139
|
let done = false;
|
|
137
140
|
let unwatch;
|
|
138
|
-
let
|
|
141
|
+
let cancelTimeout;
|
|
139
142
|
const finish = (settle) => {
|
|
140
143
|
if (done) {
|
|
141
144
|
return;
|
|
142
145
|
}
|
|
143
146
|
done = true;
|
|
144
|
-
|
|
147
|
+
cancelTimeout?.();
|
|
145
148
|
unwatch?.();
|
|
146
149
|
settle();
|
|
147
150
|
};
|
|
@@ -164,7 +167,7 @@ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new W
|
|
|
164
167
|
unwatch?.();
|
|
165
168
|
return;
|
|
166
169
|
}
|
|
167
|
-
|
|
170
|
+
cancelTimeout = __classPrivateFieldGet(this, _DurabilityTracker_timer, "f").schedule(() => {
|
|
168
171
|
read().then((result) => finish(() => resolve(result)), fail);
|
|
169
172
|
}, timeoutMs);
|
|
170
173
|
check();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "durability",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
@@ -63,9 +63,9 @@
|
|
|
63
63
|
],
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@standard-schema/spec": "^1.1.0",
|
|
66
|
-
"
|
|
67
|
-
"@durability/
|
|
68
|
-
"@durability/
|
|
66
|
+
"valibot": "^1.5.0",
|
|
67
|
+
"@durability/storage": "^0.1.1",
|
|
68
|
+
"@durability/transforms": "^0.6.2"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
71
|
"@cloudflare/workers-types": "^4.20260702.1"
|