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.
Files changed (98) hide show
  1. package/README.md +154 -21
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
  3. package/dist/{durability-alarms.js → alarms/index.js} +85 -74
  4. package/dist/alarms/migrations.d.ts +9 -0
  5. package/dist/alarms/migrations.js +133 -0
  6. package/dist/experimental.d.ts +10 -10
  7. package/dist/experimental.js +7 -7
  8. package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
  9. package/dist/{fanout.js → fanout/index.js} +91 -83
  10. package/dist/fanout/migrations.d.ts +9 -0
  11. package/dist/fanout/migrations.js +181 -0
  12. package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
  13. package/dist/index.d.ts +14 -9
  14. package/dist/index.js +13 -8
  15. package/dist/{log.d.ts → log/index.d.ts} +27 -20
  16. package/dist/{log.js → log/index.js} +200 -166
  17. package/dist/log/migrations.d.ts +18 -0
  18. package/dist/log/migrations.js +297 -0
  19. package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
  20. package/dist/{memoize.js → memoize/index.js} +23 -21
  21. package/dist/memoize/migrations.d.ts +10 -0
  22. package/dist/memoize/migrations.js +21 -0
  23. package/dist/migrations.d.ts +0 -111
  24. package/dist/migrations.js +12 -628
  25. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  26. package/dist/{durability.js → operations/index.js} +76 -70
  27. package/dist/operations/migrations.d.ts +10 -0
  28. package/dist/operations/migrations.js +150 -0
  29. package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
  30. package/dist/{outbox.js → outbox/index.js} +35 -77
  31. package/dist/outbox/migrations.d.ts +9 -0
  32. package/dist/outbox/migrations.js +43 -0
  33. package/dist/policy.d.ts +19 -19
  34. package/dist/policy.js +13 -12
  35. package/dist/{routing-client.js → routing/client.js} +1 -1
  36. package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
  37. package/dist/{routing.js → routing/index.js} +3 -3
  38. package/dist/{load.d.ts → routing/load.d.ts} +1 -1
  39. package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
  40. package/dist/{schedule.js → schedule/index.js} +97 -83
  41. package/dist/schedule/migrations.d.ts +9 -0
  42. package/dist/schedule/migrations.js +118 -0
  43. package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
  44. package/dist/{scheduler.js → scheduler/index.js} +308 -70
  45. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  46. package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
  47. package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
  48. package/dist/{semaphore.js → semaphore/index.js} +7 -7
  49. package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
  50. package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
  51. package/dist/semaphore/migrations.d.ts +18 -0
  52. package/dist/semaphore/migrations.js +444 -0
  53. package/dist/semaphore/protocol.d.ts +451 -0
  54. package/dist/semaphore/protocol.js +114 -0
  55. package/dist/semaphore/queue-store.d.ts +107 -0
  56. package/dist/semaphore/queue-store.js +258 -0
  57. package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
  58. package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
  59. package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
  60. package/dist/{semaphore-server.js → semaphore/server.js} +56 -28
  61. package/dist/semaphore/store.d.ts +53 -0
  62. package/dist/semaphore/store.js +107 -0
  63. package/dist/serialization.d.ts +5 -0
  64. package/dist/serialization.js +41 -16
  65. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  66. package/dist/{client.js → socket/client.js} +4 -4
  67. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  68. package/dist/{socket.js → socket/index.js} +66 -20
  69. package/dist/socket/migrations.d.ts +9 -0
  70. package/dist/socket/migrations.js +57 -0
  71. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  72. package/dist/storage.d.ts +4 -0
  73. package/dist/storage.js +22 -15
  74. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  75. package/dist/{topology-client.js → topology/client.js} +3 -3
  76. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  77. package/dist/{topology.js → topology/index.js} +106 -72
  78. package/dist/topology/migrations.d.ts +10 -0
  79. package/dist/topology/migrations.js +138 -0
  80. package/dist/topology/snapshot.d.ts +50 -0
  81. package/dist/topology/snapshot.js +38 -0
  82. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  83. package/dist/{tracker.js → tracker/index.js} +9 -9
  84. package/package.json +11 -11
  85. package/dist/semaphore-protocol.d.ts +0 -231
  86. package/dist/semaphore-protocol.js +0 -106
  87. package/dist/semaphore-queue-store.d.ts +0 -95
  88. package/dist/semaphore-queue-store.js +0 -252
  89. package/dist/semaphore-store.d.ts +0 -43
  90. package/dist/semaphore-store.js +0 -108
  91. package/dist/topology-snapshot.d.ts +0 -70
  92. package/dist/topology-snapshot.js +0 -48
  93. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  94. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  95. /package/dist/{load.js → routing/load.js} +0 -0
  96. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  97. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  98. /package/dist/{tracked-source.js → tracker/source.js} +0 -0
@@ -0,0 +1,451 @@
1
+ import * as v from 'valibot';
2
+ /** Bounds one message's work so a client cannot monopolize the holder. */
3
+ export declare const maxDemandEntries = 100;
4
+ export declare const maxGrantReports = 200;
5
+ /** Cumulative counts for one grant. Replaying a report can never free capacity twice. */
6
+ export declare const grantReportSchema: v.ObjectSchema<{
7
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
8
+ /** Permits of this grant the client has used, as a high-water mark. */
9
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
10
+ /** Claimed permits the client has returned, as a high-water mark. */
11
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
12
+ }, undefined>;
13
+ export type GrantReport = v.InferOutput<typeof grantReportSchema>;
14
+ /** Cumulative counts for one lease the client holds. */
15
+ export declare const leaseReportSchema: v.ObjectSchema<{
16
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
17
+ /** Permits drawn locally so far, as a high-water mark. */
18
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
19
+ /** Drawn permits returned, as a high-water mark. */
20
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
21
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
22
+ readonly retired: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
23
+ }, undefined>;
24
+ export type LeaseReport = v.InferOutput<typeof leaseReportSchema>;
25
+ export declare const leaseGrantSchema: v.ObjectSchema<{
26
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
27
+ readonly group: v.StringSchema<undefined>;
28
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
29
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
30
+ readonly expiresAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
31
+ }, undefined>;
32
+ export type LeaseGrant = v.InferOutput<typeof leaseGrantSchema>;
33
+ export declare const permitRequestSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
34
+ readonly type: v.LiteralSchema<"request", undefined>;
35
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
36
+ readonly clientId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
37
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
38
+ /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
39
+ readonly demand: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
40
+ readonly group: v.StringSchema<undefined>;
41
+ readonly count: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
42
+ readonly oldestAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
43
+ }, undefined>, undefined>, v.MaxLengthAction<{
44
+ group: string;
45
+ count: number;
46
+ oldestAt: number;
47
+ }[], 100, undefined>]>;
48
+ readonly grants: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
49
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
50
+ /** Permits of this grant the client has used, as a high-water mark. */
51
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
52
+ /** Claimed permits the client has returned, as a high-water mark. */
53
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
54
+ }, undefined>, undefined>, v.MaxLengthAction<{
55
+ grantId: string;
56
+ claimed: number;
57
+ released: number;
58
+ }[], 200, undefined>]>;
59
+ readonly leases: v.OptionalSchema<v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
60
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
61
+ /** Permits drawn locally so far, as a high-water mark. */
62
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
63
+ /** Drawn permits returned, as a high-water mark. */
64
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
65
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
66
+ readonly retired: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
67
+ }, undefined>, undefined>, v.MaxLengthAction<{
68
+ leaseId: string;
69
+ drawn: number;
70
+ released: number;
71
+ retired?: boolean | undefined;
72
+ }[], 200, undefined>]>, undefined>;
73
+ }, undefined>, v.CheckAction<{
74
+ type: "request";
75
+ v: 1 | 2;
76
+ clientId: string;
77
+ clientChannel?: string | undefined;
78
+ demand: {
79
+ group: string;
80
+ count: number;
81
+ oldestAt: number;
82
+ }[];
83
+ grants: {
84
+ grantId: string;
85
+ claimed: number;
86
+ released: number;
87
+ }[];
88
+ leases?: {
89
+ leaseId: string;
90
+ drawn: number;
91
+ released: number;
92
+ retired?: boolean | undefined;
93
+ }[] | undefined;
94
+ }, "Client channel does not match semaphore protocol version">]>;
95
+ export type PermitRequest = v.InferOutput<typeof permitRequestSchema>;
96
+ export declare const permitSyncSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
97
+ readonly type: v.LiteralSchema<"sync", undefined>;
98
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
99
+ readonly clientId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
100
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
101
+ readonly grants: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
102
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
103
+ /** Permits of this grant the client has used, as a high-water mark. */
104
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
105
+ /** Claimed permits the client has returned, as a high-water mark. */
106
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
107
+ }, undefined>, undefined>, v.MaxLengthAction<{
108
+ grantId: string;
109
+ claimed: number;
110
+ released: number;
111
+ }[], 200, undefined>]>;
112
+ readonly leases: v.OptionalSchema<v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
113
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
114
+ /** Permits drawn locally so far, as a high-water mark. */
115
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
116
+ /** Drawn permits returned, as a high-water mark. */
117
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
118
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
119
+ readonly retired: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
120
+ }, undefined>, undefined>, v.MaxLengthAction<{
121
+ leaseId: string;
122
+ drawn: number;
123
+ released: number;
124
+ retired?: boolean | undefined;
125
+ }[], 200, undefined>]>, undefined>;
126
+ }, undefined>, v.CheckAction<{
127
+ type: "sync";
128
+ v: 1 | 2;
129
+ clientId: string;
130
+ clientChannel?: string | undefined;
131
+ grants: {
132
+ grantId: string;
133
+ claimed: number;
134
+ released: number;
135
+ }[];
136
+ leases?: {
137
+ leaseId: string;
138
+ drawn: number;
139
+ released: number;
140
+ retired?: boolean | undefined;
141
+ }[] | undefined;
142
+ }, "Client channel does not match semaphore protocol version">]>;
143
+ export type PermitSync = v.InferOutput<typeof permitSyncSchema>;
144
+ export declare const semaphoreRequestSchema: v.UnionSchema<[v.SchemaWithPipe<readonly [v.ObjectSchema<{
145
+ readonly type: v.LiteralSchema<"request", undefined>;
146
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
147
+ readonly clientId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
148
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
149
+ /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
150
+ readonly demand: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
151
+ readonly group: v.StringSchema<undefined>;
152
+ readonly count: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
153
+ readonly oldestAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
154
+ }, undefined>, undefined>, v.MaxLengthAction<{
155
+ group: string;
156
+ count: number;
157
+ oldestAt: number;
158
+ }[], 100, undefined>]>;
159
+ readonly grants: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
160
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
161
+ /** Permits of this grant the client has used, as a high-water mark. */
162
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
163
+ /** Claimed permits the client has returned, as a high-water mark. */
164
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
165
+ }, undefined>, undefined>, v.MaxLengthAction<{
166
+ grantId: string;
167
+ claimed: number;
168
+ released: number;
169
+ }[], 200, undefined>]>;
170
+ readonly leases: v.OptionalSchema<v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
171
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
172
+ /** Permits drawn locally so far, as a high-water mark. */
173
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
174
+ /** Drawn permits returned, as a high-water mark. */
175
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
176
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
177
+ readonly retired: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
178
+ }, undefined>, undefined>, v.MaxLengthAction<{
179
+ leaseId: string;
180
+ drawn: number;
181
+ released: number;
182
+ retired?: boolean | undefined;
183
+ }[], 200, undefined>]>, undefined>;
184
+ }, undefined>, v.CheckAction<{
185
+ type: "request";
186
+ v: 1 | 2;
187
+ clientId: string;
188
+ clientChannel?: string | undefined;
189
+ demand: {
190
+ group: string;
191
+ count: number;
192
+ oldestAt: number;
193
+ }[];
194
+ grants: {
195
+ grantId: string;
196
+ claimed: number;
197
+ released: number;
198
+ }[];
199
+ leases?: {
200
+ leaseId: string;
201
+ drawn: number;
202
+ released: number;
203
+ retired?: boolean | undefined;
204
+ }[] | undefined;
205
+ }, "Client channel does not match semaphore protocol version">]>, v.SchemaWithPipe<readonly [v.ObjectSchema<{
206
+ readonly type: v.LiteralSchema<"sync", undefined>;
207
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
208
+ readonly clientId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
209
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
210
+ readonly grants: v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
211
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
212
+ /** Permits of this grant the client has used, as a high-water mark. */
213
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
214
+ /** Claimed permits the client has returned, as a high-water mark. */
215
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
216
+ }, undefined>, undefined>, v.MaxLengthAction<{
217
+ grantId: string;
218
+ claimed: number;
219
+ released: number;
220
+ }[], 200, undefined>]>;
221
+ readonly leases: v.OptionalSchema<v.SchemaWithPipe<readonly [v.ArraySchema<v.ObjectSchema<{
222
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
223
+ /** Permits drawn locally so far, as a high-water mark. */
224
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
225
+ /** Drawn permits returned, as a high-water mark. */
226
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
227
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
228
+ readonly retired: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
229
+ }, undefined>, undefined>, v.MaxLengthAction<{
230
+ leaseId: string;
231
+ drawn: number;
232
+ released: number;
233
+ retired?: boolean | undefined;
234
+ }[], 200, undefined>]>, undefined>;
235
+ }, undefined>, v.CheckAction<{
236
+ type: "sync";
237
+ v: 1 | 2;
238
+ clientId: string;
239
+ clientChannel?: string | undefined;
240
+ grants: {
241
+ grantId: string;
242
+ claimed: number;
243
+ released: number;
244
+ }[];
245
+ leases?: {
246
+ leaseId: string;
247
+ drawn: number;
248
+ released: number;
249
+ retired?: boolean | undefined;
250
+ }[] | undefined;
251
+ }, "Client channel does not match semaphore protocol version">]>], undefined>;
252
+ export type SemaphoreRequest = v.InferOutput<typeof semaphoreRequestSchema>;
253
+ export declare const semaphoreBlockedReasonSchema: v.PicklistSchema<["group_limit", "limit", "limit_resolution_error", "not_permitted"], undefined>;
254
+ export type SemaphoreBlockedReason = v.InferOutput<typeof semaphoreBlockedReasonSchema>;
255
+ export declare const grantResultSchema: v.ObjectSchema<{
256
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
257
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
258
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
259
+ readonly status: v.PicklistSchema<["ok", "not_found", "expired"], undefined>;
260
+ }, undefined>;
261
+ export type GrantResult = v.InferOutput<typeof grantResultSchema>;
262
+ export declare const permitGrantSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
263
+ readonly type: v.LiteralSchema<"grant", undefined>;
264
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
265
+ readonly grants: v.ArraySchema<v.ObjectSchema<{
266
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
267
+ readonly group: v.StringSchema<undefined>;
268
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
269
+ }, undefined>, undefined>;
270
+ readonly blocked: v.ArraySchema<v.ObjectSchema<{
271
+ readonly group: v.StringSchema<undefined>;
272
+ readonly reason: v.PicklistSchema<["group_limit", "limit", "limit_resolution_error", "not_permitted"], undefined>;
273
+ }, undefined>, undefined>;
274
+ /** The client must not send another request before this. */
275
+ readonly nextRequestAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
276
+ readonly grantResults: v.ArraySchema<v.ObjectSchema<{
277
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
278
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
279
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
280
+ readonly status: v.PicklistSchema<["ok", "not_found", "expired"], undefined>;
281
+ }, undefined>, undefined>;
282
+ readonly leases: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
283
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
284
+ readonly group: v.StringSchema<undefined>;
285
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
286
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
287
+ readonly expiresAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
288
+ }, undefined>, undefined>, undefined>;
289
+ /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
290
+ readonly contended: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
291
+ /** Echoes the request's client channel in version 2. */
292
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
293
+ }, undefined>, v.CheckAction<{
294
+ type: "grant";
295
+ v: 1 | 2;
296
+ grants: {
297
+ grantId: string;
298
+ group: string;
299
+ permits: number;
300
+ }[];
301
+ blocked: {
302
+ group: string;
303
+ reason: "limit" | "group_limit" | "limit_resolution_error" | "not_permitted";
304
+ }[];
305
+ nextRequestAt: number;
306
+ grantResults: {
307
+ grantId: string;
308
+ claimed: number;
309
+ status: "not_found" | "ok" | "expired";
310
+ }[];
311
+ leases?: {
312
+ leaseId: string;
313
+ group: string;
314
+ permits: number;
315
+ expiresAt: number;
316
+ }[] | undefined;
317
+ contended?: boolean | undefined;
318
+ clientChannel?: string | undefined;
319
+ }, "Client channel does not match semaphore protocol version">]>;
320
+ export type PermitGrant = v.InferOutput<typeof permitGrantSchema>;
321
+ export declare const permitSyncResultSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
322
+ readonly type: v.LiteralSchema<"synced", undefined>;
323
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
324
+ readonly grantResults: v.ArraySchema<v.ObjectSchema<{
325
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
326
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
327
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
328
+ readonly status: v.PicklistSchema<["ok", "not_found", "expired"], undefined>;
329
+ }, undefined>, undefined>;
330
+ readonly leases: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
331
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
332
+ readonly group: v.StringSchema<undefined>;
333
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
334
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
335
+ readonly expiresAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
336
+ }, undefined>, undefined>, undefined>;
337
+ readonly contended: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
338
+ /** Echoes the sync's `clientChannel`, as for a grant. */
339
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
340
+ }, undefined>, v.CheckAction<{
341
+ type: "synced";
342
+ v: 1 | 2;
343
+ grantResults: {
344
+ grantId: string;
345
+ claimed: number;
346
+ status: "not_found" | "ok" | "expired";
347
+ }[];
348
+ leases?: {
349
+ leaseId: string;
350
+ group: string;
351
+ permits: number;
352
+ expiresAt: number;
353
+ }[] | undefined;
354
+ contended?: boolean | undefined;
355
+ clientChannel?: string | undefined;
356
+ }, "Client channel does not match semaphore protocol version">]>;
357
+ export type PermitSyncResult = v.InferOutput<typeof permitSyncResultSchema>;
358
+ export declare const semaphoreResponseSchema: v.UnionSchema<[v.SchemaWithPipe<readonly [v.ObjectSchema<{
359
+ readonly type: v.LiteralSchema<"grant", undefined>;
360
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
361
+ readonly grants: v.ArraySchema<v.ObjectSchema<{
362
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
363
+ readonly group: v.StringSchema<undefined>;
364
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
365
+ }, undefined>, undefined>;
366
+ readonly blocked: v.ArraySchema<v.ObjectSchema<{
367
+ readonly group: v.StringSchema<undefined>;
368
+ readonly reason: v.PicklistSchema<["group_limit", "limit", "limit_resolution_error", "not_permitted"], undefined>;
369
+ }, undefined>, undefined>;
370
+ /** The client must not send another request before this. */
371
+ readonly nextRequestAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
372
+ readonly grantResults: v.ArraySchema<v.ObjectSchema<{
373
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
374
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
375
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
376
+ readonly status: v.PicklistSchema<["ok", "not_found", "expired"], undefined>;
377
+ }, undefined>, undefined>;
378
+ readonly leases: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
379
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
380
+ readonly group: v.StringSchema<undefined>;
381
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
382
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
383
+ readonly expiresAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
384
+ }, undefined>, undefined>, undefined>;
385
+ /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
386
+ readonly contended: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
387
+ /** Echoes the request's client channel in version 2. */
388
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
389
+ }, undefined>, v.CheckAction<{
390
+ type: "grant";
391
+ v: 1 | 2;
392
+ grants: {
393
+ grantId: string;
394
+ group: string;
395
+ permits: number;
396
+ }[];
397
+ blocked: {
398
+ group: string;
399
+ reason: "limit" | "group_limit" | "limit_resolution_error" | "not_permitted";
400
+ }[];
401
+ nextRequestAt: number;
402
+ grantResults: {
403
+ grantId: string;
404
+ claimed: number;
405
+ status: "not_found" | "ok" | "expired";
406
+ }[];
407
+ leases?: {
408
+ leaseId: string;
409
+ group: string;
410
+ permits: number;
411
+ expiresAt: number;
412
+ }[] | undefined;
413
+ contended?: boolean | undefined;
414
+ clientChannel?: string | undefined;
415
+ }, "Client channel does not match semaphore protocol version">]>, v.SchemaWithPipe<readonly [v.ObjectSchema<{
416
+ readonly type: v.LiteralSchema<"synced", undefined>;
417
+ readonly v: v.PicklistSchema<[1, 2], undefined>;
418
+ readonly grantResults: v.ArraySchema<v.ObjectSchema<{
419
+ readonly grantId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
420
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
421
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
422
+ readonly status: v.PicklistSchema<["ok", "not_found", "expired"], undefined>;
423
+ }, undefined>, undefined>;
424
+ readonly leases: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
425
+ readonly leaseId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
426
+ readonly group: v.StringSchema<undefined>;
427
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
428
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
429
+ readonly expiresAt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
430
+ }, undefined>, undefined>, undefined>;
431
+ readonly contended: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
432
+ /** Echoes the sync's `clientChannel`, as for a grant. */
433
+ readonly clientChannel: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
434
+ }, undefined>, v.CheckAction<{
435
+ type: "synced";
436
+ v: 1 | 2;
437
+ grantResults: {
438
+ grantId: string;
439
+ claimed: number;
440
+ status: "not_found" | "ok" | "expired";
441
+ }[];
442
+ leases?: {
443
+ leaseId: string;
444
+ group: string;
445
+ permits: number;
446
+ expiresAt: number;
447
+ }[] | undefined;
448
+ contended?: boolean | undefined;
449
+ clientChannel?: string | undefined;
450
+ }, "Client channel does not match semaphore protocol version">]>], undefined>;
451
+ export type SemaphoreResponse = v.InferOutput<typeof semaphoreResponseSchema>;
@@ -0,0 +1,114 @@
1
+ import * as v from 'valibot';
2
+ /**
3
+ * Messages between a semaphore client and the Durable Object holding the
4
+ * limit. Default clients keep version 1. Named queues use version 2 so old
5
+ * holders reject them before allocating grants or leases under the wrong
6
+ * identity. Extra fields remain tolerated within each version.
7
+ */
8
+ const nonNegative = v.pipe(v.number(), v.safeInteger(), v.minValue(0));
9
+ const nonEmpty = v.pipe(v.string(), v.minLength(1));
10
+ const protocolVersion = v.picklist([1, 2]);
11
+ const versionMatchesChannel = (message) => message.v === (message.clientChannel === undefined ? 1 : 2);
12
+ const channelVersionError = 'Client channel does not match semaphore protocol version';
13
+ /**
14
+ * Which queue of the client object sent the message. Version 1 omits it;
15
+ * version 2 requires it. Older clients therefore keep their identity.
16
+ */
17
+ const clientChannel = v.optional(nonEmpty);
18
+ /** Bounds one message's work so a client cannot monopolize the holder. */
19
+ export const maxDemandEntries = 100;
20
+ export const maxGrantReports = 200;
21
+ /** Cumulative counts for one grant. Replaying a report can never free capacity twice. */
22
+ export const grantReportSchema = v.object({
23
+ grantId: nonEmpty,
24
+ /** Permits of this grant the client has used, as a high-water mark. */
25
+ claimed: nonNegative,
26
+ /** Claimed permits the client has returned, as a high-water mark. */
27
+ released: nonNegative,
28
+ });
29
+ /** Cumulative counts for one lease the client holds. */
30
+ export const leaseReportSchema = v.object({
31
+ leaseId: nonEmpty,
32
+ /** Permits drawn locally so far, as a high-water mark. */
33
+ drawn: nonNegative,
34
+ /** Drawn permits returned, as a high-water mark. */
35
+ released: nonNegative,
36
+ /** The client stopped drawing: the lease's undrawn permits are returned. */
37
+ retired: v.optional(v.boolean()),
38
+ });
39
+ export const leaseGrantSchema = v.object({
40
+ leaseId: nonEmpty,
41
+ group: v.string(),
42
+ permits: nonNegative,
43
+ /** `expiresAt <= now` retires the lease: stop drawing immediately. */
44
+ expiresAt: nonNegative,
45
+ });
46
+ export const permitRequestSchema = v.pipe(v.object({
47
+ type: v.literal('request'),
48
+ v: protocolVersion,
49
+ clientId: nonEmpty,
50
+ clientChannel,
51
+ /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
52
+ demand: v.pipe(v.array(v.object({
53
+ group: v.string(),
54
+ count: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
55
+ oldestAt: nonNegative,
56
+ })), v.maxLength(maxDemandEntries)),
57
+ grants: v.pipe(v.array(grantReportSchema), v.maxLength(maxGrantReports)),
58
+ leases: v.optional(v.pipe(v.array(leaseReportSchema), v.maxLength(maxGrantReports))),
59
+ }), v.check(versionMatchesChannel, channelVersionError));
60
+ export const permitSyncSchema = v.pipe(v.object({
61
+ type: v.literal('sync'),
62
+ v: protocolVersion,
63
+ clientId: nonEmpty,
64
+ clientChannel,
65
+ grants: v.pipe(v.array(grantReportSchema), v.maxLength(maxGrantReports)),
66
+ leases: v.optional(v.pipe(v.array(leaseReportSchema), v.maxLength(maxGrantReports))),
67
+ }), v.check(versionMatchesChannel, channelVersionError));
68
+ export const semaphoreRequestSchema = v.union([
69
+ permitRequestSchema,
70
+ permitSyncSchema,
71
+ ]);
72
+ export const semaphoreBlockedReasonSchema = v.picklist([
73
+ 'group_limit',
74
+ 'limit',
75
+ 'limit_resolution_error',
76
+ 'not_permitted',
77
+ ]);
78
+ export const grantResultSchema = v.object({
79
+ grantId: nonEmpty,
80
+ /** Server-clamped claim high-water mark; the client admits only up to this. */
81
+ claimed: nonNegative,
82
+ status: v.picklist(['ok', 'not_found', 'expired']),
83
+ });
84
+ export const permitGrantSchema = v.pipe(v.object({
85
+ type: v.literal('grant'),
86
+ v: protocolVersion,
87
+ grants: v.array(v.object({
88
+ grantId: nonEmpty,
89
+ group: v.string(),
90
+ permits: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
91
+ })),
92
+ blocked: v.array(v.object({ group: v.string(), reason: semaphoreBlockedReasonSchema })),
93
+ /** The client must not send another request before this. */
94
+ nextRequestAt: nonNegative,
95
+ grantResults: v.array(grantResultSchema),
96
+ leases: v.optional(v.array(leaseGrantSchema)),
97
+ /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
98
+ contended: v.optional(v.boolean()),
99
+ /** Echoes the request's client channel in version 2. */
100
+ clientChannel,
101
+ }), v.check(versionMatchesChannel, channelVersionError));
102
+ export const permitSyncResultSchema = v.pipe(v.object({
103
+ type: v.literal('synced'),
104
+ v: protocolVersion,
105
+ grantResults: v.array(grantResultSchema),
106
+ leases: v.optional(v.array(leaseGrantSchema)),
107
+ contended: v.optional(v.boolean()),
108
+ /** Echoes the sync's `clientChannel`, as for a grant. */
109
+ clientChannel,
110
+ }), v.check(versionMatchesChannel, channelVersionError));
111
+ export const semaphoreResponseSchema = v.union([
112
+ permitGrantSchema,
113
+ permitSyncResultSchema,
114
+ ]);