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
@@ -1,6 +1,6 @@
1
- import type { AnyDurabilityAlarms, DurabilityAlarms } from './durability-alarms.js';
2
- import type { AnyDurability, Durability, OperationResults } from './durability.js';
3
- import { type Names, type TrackedEntry, type TrackedResults, type TrackedSource, type TrackedState, type TrackedStatus, type UntypedSource } from './tracked-source.js';
1
+ import type { AnyDurabilityAlarms, DurabilityAlarms } from '../alarms/index.js';
2
+ import type { AnyDurability, Durability, OperationResults } from '../operations/index.js';
3
+ import { type Names, type TrackedEntry, type TrackedResults, type TrackedSource, type TrackedState, type TrackedStatus, type UntypedSource } from './source.js';
4
4
  /** A tracked state, or `not_found` when the record or its generation is gone. */
5
5
  export type TrackedResult<Result> = TrackedState<Result> | {
6
6
  status: 'not_found';
@@ -10,13 +10,13 @@ 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 _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
13
- import { z } from 'zod';
14
- import { traceMethods } from './dev.js';
15
- import { trackedSourceOf, } from './tracked-source.js';
16
- const limitSchema = z.number().int().min(1).max(100);
17
- const cursorSchema = z.tuple([
18
- z.number().int().nonnegative(),
19
- z.string().nullable(),
13
+ import * as v from 'valibot';
14
+ import { traceMethods } from '../dev.js';
15
+ import { trackedSourceOf, } from './source.js';
16
+ const limitSchema = v.pipe(v.number(), v.safeInteger(), v.minValue(1), v.maxValue(100));
17
+ const cursorSchema = v.strictTuple([
18
+ v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
19
+ v.nullable(v.string()),
20
20
  ]);
21
21
  const resultFor = (entry, record) => record?.generation === entry.generation
22
22
  ? record.state
@@ -87,11 +87,11 @@ export class DurabilityTracker {
87
87
  }
88
88
  }
89
89
  _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
90
- const limit = limitSchema.parse(query.limit ?? 50);
90
+ const limit = v.parse(limitSchema, query.limit ?? 50);
91
91
  const candidates = query.source === undefined ? __classPrivateFieldGet(this, _DurabilityTracker_sources, "f") : [__classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_source).call(this, query.source)];
92
92
  const [start, startCursor] = query.cursor === undefined
93
93
  ? [0, null]
94
- : cursorSchema.parse(JSON.parse(query.cursor));
94
+ : v.parse(cursorSchema, JSON.parse(query.cursor));
95
95
  const found = [];
96
96
  for (const [offset, [key, source]] of candidates.slice(start).entries()) {
97
97
  const index = start + offset;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durability",
3
- "version": "5.1.0",
3
+ "version": "5.2.1",
4
4
  "description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
5
5
  "type": "module",
6
6
  "license": "ISC",
@@ -16,24 +16,24 @@
16
16
  "import": "./dist/index.js"
17
17
  },
18
18
  "./client": {
19
- "types": "./dist/client.d.ts",
20
- "import": "./dist/client.js"
19
+ "types": "./dist/socket/client.d.ts",
20
+ "import": "./dist/socket/client.js"
21
21
  },
22
22
  "./experimental": {
23
23
  "types": "./dist/experimental.d.ts",
24
24
  "import": "./dist/experimental.js"
25
25
  },
26
26
  "./routing": {
27
- "types": "./dist/routing.d.ts",
28
- "import": "./dist/routing.js"
27
+ "types": "./dist/routing/index.d.ts",
28
+ "import": "./dist/routing/index.js"
29
29
  },
30
30
  "./socket": {
31
- "types": "./dist/socket.d.ts",
32
- "import": "./dist/socket.js"
31
+ "types": "./dist/socket/index.d.ts",
32
+ "import": "./dist/socket/index.js"
33
33
  },
34
34
  "./experimental/semaphore": {
35
- "types": "./dist/semaphore.d.ts",
36
- "import": "./dist/semaphore.js"
35
+ "types": "./dist/semaphore/index.d.ts",
36
+ "import": "./dist/semaphore/index.js"
37
37
  },
38
38
  "./utils": {
39
39
  "types": "./dist/utils.d.ts",
@@ -63,9 +63,9 @@
63
63
  ],
64
64
  "dependencies": {
65
65
  "@standard-schema/spec": "^1.1.0",
66
- "zod": "^4.4.3",
66
+ "valibot": "^1.5.0",
67
67
  "@durability/storage": "^0.1.1",
68
- "@durability/transforms": "^0.6.0"
68
+ "@durability/transforms": "^0.6.1"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "@cloudflare/workers-types": "^4.20260702.1"
@@ -1,231 +0,0 @@
1
- import { z } from 'zod';
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: z.ZodObject<{
7
- grantId: z.ZodString;
8
- claimed: z.ZodNumber;
9
- released: z.ZodNumber;
10
- }, z.core.$strip>;
11
- export type GrantReport = z.infer<typeof grantReportSchema>;
12
- /** Cumulative counts for one lease the client holds. */
13
- export declare const leaseReportSchema: z.ZodObject<{
14
- leaseId: z.ZodString;
15
- drawn: z.ZodNumber;
16
- released: z.ZodNumber;
17
- retired: z.ZodOptional<z.ZodBoolean>;
18
- }, z.core.$strip>;
19
- export type LeaseReport = z.infer<typeof leaseReportSchema>;
20
- export declare const leaseGrantSchema: z.ZodObject<{
21
- leaseId: z.ZodString;
22
- group: z.ZodString;
23
- permits: z.ZodNumber;
24
- expiresAt: z.ZodNumber;
25
- }, z.core.$strip>;
26
- export type LeaseGrant = z.infer<typeof leaseGrantSchema>;
27
- export declare const permitRequestSchema: z.ZodObject<{
28
- type: z.ZodLiteral<"request">;
29
- v: z.ZodLiteral<1>;
30
- clientId: z.ZodString;
31
- demand: z.ZodArray<z.ZodObject<{
32
- group: z.ZodString;
33
- count: z.ZodNumber;
34
- oldestAt: z.ZodNumber;
35
- }, z.core.$strip>>;
36
- grants: z.ZodArray<z.ZodObject<{
37
- grantId: z.ZodString;
38
- claimed: z.ZodNumber;
39
- released: z.ZodNumber;
40
- }, z.core.$strip>>;
41
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
42
- leaseId: z.ZodString;
43
- drawn: z.ZodNumber;
44
- released: z.ZodNumber;
45
- retired: z.ZodOptional<z.ZodBoolean>;
46
- }, z.core.$strip>>>;
47
- }, z.core.$strip>;
48
- export type PermitRequest = z.infer<typeof permitRequestSchema>;
49
- export declare const permitSyncSchema: z.ZodObject<{
50
- type: z.ZodLiteral<"sync">;
51
- v: z.ZodLiteral<1>;
52
- clientId: z.ZodString;
53
- grants: z.ZodArray<z.ZodObject<{
54
- grantId: z.ZodString;
55
- claimed: z.ZodNumber;
56
- released: z.ZodNumber;
57
- }, z.core.$strip>>;
58
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
59
- leaseId: z.ZodString;
60
- drawn: z.ZodNumber;
61
- released: z.ZodNumber;
62
- retired: z.ZodOptional<z.ZodBoolean>;
63
- }, z.core.$strip>>>;
64
- }, z.core.$strip>;
65
- export type PermitSync = z.infer<typeof permitSyncSchema>;
66
- export declare const semaphoreRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
67
- type: z.ZodLiteral<"request">;
68
- v: z.ZodLiteral<1>;
69
- clientId: z.ZodString;
70
- demand: z.ZodArray<z.ZodObject<{
71
- group: z.ZodString;
72
- count: z.ZodNumber;
73
- oldestAt: z.ZodNumber;
74
- }, z.core.$strip>>;
75
- grants: z.ZodArray<z.ZodObject<{
76
- grantId: z.ZodString;
77
- claimed: z.ZodNumber;
78
- released: z.ZodNumber;
79
- }, z.core.$strip>>;
80
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
81
- leaseId: z.ZodString;
82
- drawn: z.ZodNumber;
83
- released: z.ZodNumber;
84
- retired: z.ZodOptional<z.ZodBoolean>;
85
- }, z.core.$strip>>>;
86
- }, z.core.$strip>, z.ZodObject<{
87
- type: z.ZodLiteral<"sync">;
88
- v: z.ZodLiteral<1>;
89
- clientId: z.ZodString;
90
- grants: z.ZodArray<z.ZodObject<{
91
- grantId: z.ZodString;
92
- claimed: z.ZodNumber;
93
- released: z.ZodNumber;
94
- }, z.core.$strip>>;
95
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
96
- leaseId: z.ZodString;
97
- drawn: z.ZodNumber;
98
- released: z.ZodNumber;
99
- retired: z.ZodOptional<z.ZodBoolean>;
100
- }, z.core.$strip>>>;
101
- }, z.core.$strip>], "type">;
102
- export type SemaphoreRequest = z.infer<typeof semaphoreRequestSchema>;
103
- export declare const semaphoreBlockedReasonSchema: z.ZodEnum<{
104
- limit: "limit";
105
- group_limit: "group_limit";
106
- limit_resolution_error: "limit_resolution_error";
107
- not_permitted: "not_permitted";
108
- }>;
109
- export type SemaphoreBlockedReason = z.infer<typeof semaphoreBlockedReasonSchema>;
110
- export declare const grantResultSchema: z.ZodObject<{
111
- grantId: z.ZodString;
112
- claimed: z.ZodNumber;
113
- status: z.ZodEnum<{
114
- not_found: "not_found";
115
- ok: "ok";
116
- expired: "expired";
117
- }>;
118
- }, z.core.$strip>;
119
- export type GrantResult = z.infer<typeof grantResultSchema>;
120
- export declare const permitGrantSchema: z.ZodObject<{
121
- type: z.ZodLiteral<"grant">;
122
- v: z.ZodLiteral<1>;
123
- grants: z.ZodArray<z.ZodObject<{
124
- grantId: z.ZodString;
125
- group: z.ZodString;
126
- permits: z.ZodNumber;
127
- }, z.core.$strip>>;
128
- blocked: z.ZodArray<z.ZodObject<{
129
- group: z.ZodString;
130
- reason: z.ZodEnum<{
131
- limit: "limit";
132
- group_limit: "group_limit";
133
- limit_resolution_error: "limit_resolution_error";
134
- not_permitted: "not_permitted";
135
- }>;
136
- }, z.core.$strip>>;
137
- nextRequestAt: z.ZodNumber;
138
- grantResults: z.ZodArray<z.ZodObject<{
139
- grantId: z.ZodString;
140
- claimed: z.ZodNumber;
141
- status: z.ZodEnum<{
142
- not_found: "not_found";
143
- ok: "ok";
144
- expired: "expired";
145
- }>;
146
- }, z.core.$strip>>;
147
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
148
- leaseId: z.ZodString;
149
- group: z.ZodString;
150
- permits: z.ZodNumber;
151
- expiresAt: z.ZodNumber;
152
- }, z.core.$strip>>>;
153
- contended: z.ZodOptional<z.ZodBoolean>;
154
- }, z.core.$strip>;
155
- export type PermitGrant = z.infer<typeof permitGrantSchema>;
156
- export declare const permitSyncResultSchema: z.ZodObject<{
157
- type: z.ZodLiteral<"synced">;
158
- v: z.ZodLiteral<1>;
159
- grantResults: z.ZodArray<z.ZodObject<{
160
- grantId: z.ZodString;
161
- claimed: z.ZodNumber;
162
- status: z.ZodEnum<{
163
- not_found: "not_found";
164
- ok: "ok";
165
- expired: "expired";
166
- }>;
167
- }, z.core.$strip>>;
168
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
169
- leaseId: z.ZodString;
170
- group: z.ZodString;
171
- permits: z.ZodNumber;
172
- expiresAt: z.ZodNumber;
173
- }, z.core.$strip>>>;
174
- contended: z.ZodOptional<z.ZodBoolean>;
175
- }, z.core.$strip>;
176
- export type PermitSyncResult = z.infer<typeof permitSyncResultSchema>;
177
- export declare const semaphoreResponseSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
178
- type: z.ZodLiteral<"grant">;
179
- v: z.ZodLiteral<1>;
180
- grants: z.ZodArray<z.ZodObject<{
181
- grantId: z.ZodString;
182
- group: z.ZodString;
183
- permits: z.ZodNumber;
184
- }, z.core.$strip>>;
185
- blocked: z.ZodArray<z.ZodObject<{
186
- group: z.ZodString;
187
- reason: z.ZodEnum<{
188
- limit: "limit";
189
- group_limit: "group_limit";
190
- limit_resolution_error: "limit_resolution_error";
191
- not_permitted: "not_permitted";
192
- }>;
193
- }, z.core.$strip>>;
194
- nextRequestAt: z.ZodNumber;
195
- grantResults: z.ZodArray<z.ZodObject<{
196
- grantId: z.ZodString;
197
- claimed: z.ZodNumber;
198
- status: z.ZodEnum<{
199
- not_found: "not_found";
200
- ok: "ok";
201
- expired: "expired";
202
- }>;
203
- }, z.core.$strip>>;
204
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
205
- leaseId: z.ZodString;
206
- group: z.ZodString;
207
- permits: z.ZodNumber;
208
- expiresAt: z.ZodNumber;
209
- }, z.core.$strip>>>;
210
- contended: z.ZodOptional<z.ZodBoolean>;
211
- }, z.core.$strip>, z.ZodObject<{
212
- type: z.ZodLiteral<"synced">;
213
- v: z.ZodLiteral<1>;
214
- grantResults: z.ZodArray<z.ZodObject<{
215
- grantId: z.ZodString;
216
- claimed: z.ZodNumber;
217
- status: z.ZodEnum<{
218
- not_found: "not_found";
219
- ok: "ok";
220
- expired: "expired";
221
- }>;
222
- }, z.core.$strip>>;
223
- leases: z.ZodOptional<z.ZodArray<z.ZodObject<{
224
- leaseId: z.ZodString;
225
- group: z.ZodString;
226
- permits: z.ZodNumber;
227
- expiresAt: z.ZodNumber;
228
- }, z.core.$strip>>>;
229
- contended: z.ZodOptional<z.ZodBoolean>;
230
- }, z.core.$strip>], "type">;
231
- export type SemaphoreResponse = z.infer<typeof semaphoreResponseSchema>;
@@ -1,106 +0,0 @@
1
- import { z } from 'zod';
2
- /**
3
- * Messages between a semaphore client and the Durable Object holding the
4
- * limit. Both sides tolerate unknown fields so mixed deployments keep working;
5
- * new fields must be optional.
6
- */
7
- const nonNegative = z.number().int().nonnegative();
8
- const nonEmpty = z.string().min(1);
9
- /** Bounds one message's work so a client cannot monopolize the holder. */
10
- export const maxDemandEntries = 100;
11
- export const maxGrantReports = 200;
12
- /** Cumulative counts for one grant. Replaying a report can never free capacity twice. */
13
- export const grantReportSchema = z.object({
14
- grantId: nonEmpty,
15
- /** Permits of this grant the client has used, as a high-water mark. */
16
- claimed: nonNegative,
17
- /** Claimed permits the client has returned, as a high-water mark. */
18
- released: nonNegative,
19
- });
20
- /** Cumulative counts for one lease the client holds. */
21
- export const leaseReportSchema = z.object({
22
- leaseId: nonEmpty,
23
- /** Permits drawn locally so far, as a high-water mark. */
24
- drawn: nonNegative,
25
- /** Drawn permits returned, as a high-water mark. */
26
- released: nonNegative,
27
- /** The client stopped drawing: the lease's undrawn permits are returned. */
28
- retired: z.boolean().optional(),
29
- });
30
- export const leaseGrantSchema = z.object({
31
- leaseId: nonEmpty,
32
- group: z.string(),
33
- permits: nonNegative,
34
- /** `expiresAt <= now` retires the lease: stop drawing immediately. */
35
- expiresAt: nonNegative,
36
- });
37
- export const permitRequestSchema = z.object({
38
- type: z.literal('request'),
39
- v: z.literal(1),
40
- clientId: nonEmpty,
41
- /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
42
- demand: z
43
- .object({
44
- group: z.string(),
45
- count: z.number().int().positive(),
46
- oldestAt: nonNegative,
47
- })
48
- .array()
49
- .max(maxDemandEntries),
50
- grants: grantReportSchema.array().max(maxGrantReports),
51
- leases: leaseReportSchema.array().max(maxGrantReports).optional(),
52
- });
53
- export const permitSyncSchema = z.object({
54
- type: z.literal('sync'),
55
- v: z.literal(1),
56
- clientId: nonEmpty,
57
- grants: grantReportSchema.array().max(maxGrantReports),
58
- leases: leaseReportSchema.array().max(maxGrantReports).optional(),
59
- });
60
- export const semaphoreRequestSchema = z.discriminatedUnion('type', [
61
- permitRequestSchema,
62
- permitSyncSchema,
63
- ]);
64
- export const semaphoreBlockedReasonSchema = z.enum([
65
- 'group_limit',
66
- 'limit',
67
- 'limit_resolution_error',
68
- 'not_permitted',
69
- ]);
70
- export const grantResultSchema = z.object({
71
- grantId: nonEmpty,
72
- /** Server-clamped claim high-water mark; the client admits only up to this. */
73
- claimed: nonNegative,
74
- status: z.enum(['ok', 'not_found', 'expired']),
75
- });
76
- export const permitGrantSchema = z.object({
77
- type: z.literal('grant'),
78
- v: z.literal(1),
79
- grants: z
80
- .object({
81
- grantId: nonEmpty,
82
- group: z.string(),
83
- permits: z.number().int().positive(),
84
- })
85
- .array(),
86
- blocked: z
87
- .object({ group: z.string(), reason: semaphoreBlockedReasonSchema })
88
- .array(),
89
- /** The client must not send another request before this. */
90
- nextRequestAt: nonNegative,
91
- grantResults: grantResultSchema.array(),
92
- leases: leaseGrantSchema.array().optional(),
93
- /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
94
- contended: z.boolean().optional(),
95
- });
96
- export const permitSyncResultSchema = z.object({
97
- type: z.literal('synced'),
98
- v: z.literal(1),
99
- grantResults: grantResultSchema.array(),
100
- leases: leaseGrantSchema.array().optional(),
101
- contended: z.boolean().optional(),
102
- });
103
- export const semaphoreResponseSchema = z.discriminatedUnion('type', [
104
- permitGrantSchema,
105
- permitSyncResultSchema,
106
- ]);
@@ -1,95 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const queueItemRowSchema: z.ZodObject<{
3
- id: z.ZodString;
4
- key: z.ZodString;
5
- grp: z.ZodString;
6
- payload: z.ZodString;
7
- status: z.ZodEnum<{
8
- queued: "queued";
9
- claiming: "claiming";
10
- admitted: "admitted";
11
- }>;
12
- grant_id: z.ZodNullable<z.ZodString>;
13
- lease_id: z.ZodNullable<z.ZodString>;
14
- created_at: z.ZodNumber;
15
- admitted_at: z.ZodNullable<z.ZodNumber>;
16
- notified: z.ZodNumber;
17
- attempt: z.ZodNumber;
18
- next_attempt_at: z.ZodNumber;
19
- }, z.core.$strip>;
20
- export type QueueItemRow = z.infer<typeof queueItemRowSchema>;
21
- export declare const queueGrantRowSchema: z.ZodObject<{
22
- grant_id: z.ZodString;
23
- key: z.ZodString;
24
- grp: z.ZodString;
25
- permits: z.ZodNumber;
26
- claimed: z.ZodNumber;
27
- confirmed: z.ZodNumber;
28
- admitted: z.ZodNumber;
29
- released: z.ZodNumber;
30
- created_at: z.ZodNumber;
31
- }, z.core.$strip>;
32
- export type QueueGrantRow = z.infer<typeof queueGrantRowSchema>;
33
- export declare const queueKeyRowSchema: z.ZodObject<{
34
- key: z.ZodString;
35
- next_request_at: z.ZodNumber;
36
- queued: z.ZodNumber;
37
- }, z.core.$strip>;
38
- export type QueueKeyRow = z.infer<typeof queueKeyRowSchema>;
39
- export declare const queueLeaseRowSchema: z.ZodObject<{
40
- lease_id: z.ZodString;
41
- key: z.ZodString;
42
- grp: z.ZodString;
43
- permits: z.ZodNumber;
44
- drawn: z.ZodNumber;
45
- released: z.ZodNumber;
46
- created_at: z.ZodNumber;
47
- expires_at: z.ZodNumber;
48
- renew_at: z.ZodNumber;
49
- renew_ms: z.ZodNumber;
50
- confirmed: z.ZodNumber;
51
- }, z.core.$strip>;
52
- export type QueueLeaseRow = z.infer<typeof queueLeaseRowSchema>;
53
- /**
54
- * A semaphore client's queue: items, the grants it holds per key, and per-key
55
- * request pacing. Due work is a key whose request time has passed with items
56
- * queued, an admitted item whose `admit()` notification is outstanding, or a
57
- * lease the holder must hear about again.
58
- */
59
- export declare class QueueStore {
60
- private readonly sql;
61
- constructor(sql: SqlStorage);
62
- getItem(id: string): QueueItemRow | undefined;
63
- insertItem(row: QueueItemRow): boolean;
64
- updateItem(row: QueueItemRow): void;
65
- removeItem(id: string): void;
66
- listItems(key: string, status: QueueItemRow['status'], limit: number): QueueItemRow[];
67
- listItemsByGrant(grantId: string): QueueItemRow[];
68
- /** Notified items whose `finished` check is due; `next_attempt_at` holds the check time. */
69
- listCheckDue(now: number, limit: number): QueueItemRow[];
70
- listUnnotified(now: number, limit: number): QueueItemRow[];
71
- countItems(): Record<QueueItemRow['status'], number>;
72
- getGrant(grantId: string): QueueGrantRow | undefined;
73
- insertGrant(row: QueueGrantRow): boolean;
74
- updateGrant(row: QueueGrantRow): void;
75
- removeGrant(grantId: string): void;
76
- listGrants(key: string): QueueGrantRow[];
77
- getLease(leaseId: string): QueueLeaseRow | undefined;
78
- insertLease(row: QueueLeaseRow): boolean;
79
- updateLease(row: QueueLeaseRow): void;
80
- /**
81
- * Leases the holder must hear about again: those with permits in use, and
82
- * stopped ones it has not settled. Both stay counted at the holder until
83
- * reported, so a lost report must be repeated.
84
- */
85
- listRenewalsDue(now: number, limit: number): QueueLeaseRow[];
86
- /** Drops settled leases that stopped drawing before `before`. */
87
- removeSpentLeases(before: number): void;
88
- listLeases(key: string): QueueLeaseRow[];
89
- getKey(key: string): QueueKeyRow | undefined;
90
- upsertKey(row: QueueKeyRow): void;
91
- removeKey(key: string): void;
92
- listDueKeys(now: number): QueueKeyRow[];
93
- nextPendingAt(checksFinished: boolean): number | undefined;
94
- private items;
95
- }