durability 5.2.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.
@@ -1,25 +1,25 @@
1
- import { z } from 'zod';
1
+ import * as v from 'valibot';
2
2
  /**
3
3
  * Messages between a semaphore client and the Durable Object holding the
4
4
  * limit. Default clients keep version 1. Named queues use version 2 so old
5
5
  * holders reject them before allocating grants or leases under the wrong
6
6
  * identity. Extra fields remain tolerated within each version.
7
7
  */
8
- const nonNegative = z.number().int().nonnegative();
9
- const nonEmpty = z.string().min(1);
10
- const protocolVersion = z.union([z.literal(1), z.literal(2)]);
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
11
  const versionMatchesChannel = (message) => message.v === (message.clientChannel === undefined ? 1 : 2);
12
12
  const channelVersionError = 'Client channel does not match semaphore protocol version';
13
13
  /**
14
14
  * Which queue of the client object sent the message. Version 1 omits it;
15
15
  * version 2 requires it. Older clients therefore keep their identity.
16
16
  */
17
- const clientChannel = nonEmpty.optional();
17
+ const clientChannel = v.optional(nonEmpty);
18
18
  /** Bounds one message's work so a client cannot monopolize the holder. */
19
19
  export const maxDemandEntries = 100;
20
20
  export const maxGrantReports = 200;
21
21
  /** Cumulative counts for one grant. Replaying a report can never free capacity twice. */
22
- export const grantReportSchema = z.object({
22
+ export const grantReportSchema = v.object({
23
23
  grantId: nonEmpty,
24
24
  /** Permits of this grant the client has used, as a high-water mark. */
25
25
  claimed: nonNegative,
@@ -27,103 +27,88 @@ export const grantReportSchema = z.object({
27
27
  released: nonNegative,
28
28
  });
29
29
  /** Cumulative counts for one lease the client holds. */
30
- export const leaseReportSchema = z.object({
30
+ export const leaseReportSchema = v.object({
31
31
  leaseId: nonEmpty,
32
32
  /** Permits drawn locally so far, as a high-water mark. */
33
33
  drawn: nonNegative,
34
34
  /** Drawn permits returned, as a high-water mark. */
35
35
  released: nonNegative,
36
36
  /** The client stopped drawing: the lease's undrawn permits are returned. */
37
- retired: z.boolean().optional(),
37
+ retired: v.optional(v.boolean()),
38
38
  });
39
- export const leaseGrantSchema = z.object({
39
+ export const leaseGrantSchema = v.object({
40
40
  leaseId: nonEmpty,
41
- group: z.string(),
41
+ group: v.string(),
42
42
  permits: nonNegative,
43
43
  /** `expiresAt <= now` retires the lease: stop drawing immediately. */
44
44
  expiresAt: nonNegative,
45
45
  });
46
- export const permitRequestSchema = z
47
- .object({
48
- type: z.literal('request'),
46
+ export const permitRequestSchema = v.pipe(v.object({
47
+ type: v.literal('request'),
49
48
  v: protocolVersion,
50
49
  clientId: nonEmpty,
51
50
  clientChannel,
52
51
  /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
53
- demand: z
54
- .object({
55
- group: z.string(),
56
- count: z.number().int().positive(),
52
+ demand: v.pipe(v.array(v.object({
53
+ group: v.string(),
54
+ count: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
57
55
  oldestAt: nonNegative,
58
- })
59
- .array()
60
- .max(maxDemandEntries),
61
- grants: grantReportSchema.array().max(maxGrantReports),
62
- leases: leaseReportSchema.array().max(maxGrantReports).optional(),
63
- })
64
- .refine(versionMatchesChannel, channelVersionError);
65
- export const permitSyncSchema = z
66
- .object({
67
- type: z.literal('sync'),
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'),
68
62
  v: protocolVersion,
69
63
  clientId: nonEmpty,
70
64
  clientChannel,
71
- grants: grantReportSchema.array().max(maxGrantReports),
72
- leases: leaseReportSchema.array().max(maxGrantReports).optional(),
73
- })
74
- .refine(versionMatchesChannel, channelVersionError);
75
- export const semaphoreRequestSchema = z.discriminatedUnion('type', [
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([
76
69
  permitRequestSchema,
77
70
  permitSyncSchema,
78
71
  ]);
79
- export const semaphoreBlockedReasonSchema = z.enum([
72
+ export const semaphoreBlockedReasonSchema = v.picklist([
80
73
  'group_limit',
81
74
  'limit',
82
75
  'limit_resolution_error',
83
76
  'not_permitted',
84
77
  ]);
85
- export const grantResultSchema = z.object({
78
+ export const grantResultSchema = v.object({
86
79
  grantId: nonEmpty,
87
80
  /** Server-clamped claim high-water mark; the client admits only up to this. */
88
81
  claimed: nonNegative,
89
- status: z.enum(['ok', 'not_found', 'expired']),
82
+ status: v.picklist(['ok', 'not_found', 'expired']),
90
83
  });
91
- export const permitGrantSchema = z
92
- .object({
93
- type: z.literal('grant'),
84
+ export const permitGrantSchema = v.pipe(v.object({
85
+ type: v.literal('grant'),
94
86
  v: protocolVersion,
95
- grants: z
96
- .object({
87
+ grants: v.array(v.object({
97
88
  grantId: nonEmpty,
98
- group: z.string(),
99
- permits: z.number().int().positive(),
100
- })
101
- .array(),
102
- blocked: z
103
- .object({ group: z.string(), reason: semaphoreBlockedReasonSchema })
104
- .array(),
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 })),
105
93
  /** The client must not send another request before this. */
106
94
  nextRequestAt: nonNegative,
107
- grantResults: grantResultSchema.array(),
108
- leases: leaseGrantSchema.array().optional(),
95
+ grantResults: v.array(grantResultSchema),
96
+ leases: v.optional(v.array(leaseGrantSchema)),
109
97
  /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
110
- contended: z.boolean().optional(),
98
+ contended: v.optional(v.boolean()),
111
99
  /** Echoes the request's client channel in version 2. */
112
100
  clientChannel,
113
- })
114
- .refine(versionMatchesChannel, channelVersionError);
115
- export const permitSyncResultSchema = z
116
- .object({
117
- type: z.literal('synced'),
101
+ }), v.check(versionMatchesChannel, channelVersionError));
102
+ export const permitSyncResultSchema = v.pipe(v.object({
103
+ type: v.literal('synced'),
118
104
  v: protocolVersion,
119
- grantResults: grantResultSchema.array(),
120
- leases: leaseGrantSchema.array().optional(),
121
- contended: z.boolean().optional(),
105
+ grantResults: v.array(grantResultSchema),
106
+ leases: v.optional(v.array(leaseGrantSchema)),
107
+ contended: v.optional(v.boolean()),
122
108
  /** Echoes the sync's `clientChannel`, as for a grant. */
123
109
  clientChannel,
124
- })
125
- .refine(versionMatchesChannel, channelVersionError);
126
- export const semaphoreResponseSchema = z.discriminatedUnion('type', [
110
+ }), v.check(versionMatchesChannel, channelVersionError));
111
+ export const semaphoreResponseSchema = v.union([
127
112
  permitGrantSchema,
128
113
  permitSyncResultSchema,
129
114
  ]);
@@ -1,55 +1,61 @@
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>;
1
+ import * as v from 'valibot';
2
+ export declare const queueItemRowSchema: v.ObjectSchema<{
3
+ readonly id: v.StringSchema<undefined>;
4
+ readonly key: v.StringSchema<undefined>;
5
+ readonly grp: v.StringSchema<undefined>;
6
+ readonly payload: v.StringSchema<undefined>;
7
+ /** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
8
+ readonly status: v.PicklistSchema<["queued", "claiming", "admitted"], undefined>;
9
+ readonly grant_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
10
+ readonly lease_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
11
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
12
+ readonly admitted_at: v.NullableSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
13
+ /** 0 until `admit()` has returned for this item. */
14
+ readonly notified: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
15
+ readonly attempt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
16
+ readonly next_attempt_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
17
+ }, undefined>;
18
+ export type QueueItemRow = v.InferOutput<typeof queueItemRowSchema>;
19
+ export declare const queueGrantRowSchema: v.ObjectSchema<{
20
+ readonly grant_id: v.StringSchema<undefined>;
21
+ readonly key: v.StringSchema<undefined>;
22
+ readonly grp: v.StringSchema<undefined>;
23
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
24
+ /** Permits counted against this grant locally, as a high-water mark. */
25
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
26
+ /** Claims the holder has confirmed. */
27
+ readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
28
+ /** Items handed to `admit()` from this grant; never derived from live rows. */
29
+ readonly admitted: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
30
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
31
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
32
+ }, undefined>;
33
+ export type QueueGrantRow = v.InferOutput<typeof queueGrantRowSchema>;
34
+ export declare const queueKeyRowSchema: v.ObjectSchema<{
35
+ readonly key: v.StringSchema<undefined>;
36
+ /** Earliest time the next permit request for this key may be sent. */
37
+ readonly next_request_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
38
+ readonly queued: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
39
+ }, undefined>;
40
+ export type QueueKeyRow = v.InferOutput<typeof queueKeyRowSchema>;
41
+ export declare const queueLeaseRowSchema: v.ObjectSchema<{
42
+ readonly lease_id: v.StringSchema<undefined>;
43
+ readonly key: v.StringSchema<undefined>;
44
+ readonly grp: v.StringSchema<undefined>;
45
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
46
+ /** Permits drawn so far; a drawn permit is never drawable again. */
47
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
48
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
49
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
50
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
51
+ /** When to report the lease again while it has permits in use, so the holder keeps it. */
52
+ readonly renew_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
53
+ /** Half the lease's lifetime at the holder, from its last refresh. */
54
+ readonly renew_ms: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
55
+ /** 1 once the holder has settled the stopped lease; until then it is reported again. */
56
+ readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
57
+ }, undefined>;
58
+ export type QueueLeaseRow = v.InferOutput<typeof queueLeaseRowSchema>;
53
59
  /**
54
60
  * One queue channel of a semaphore client: items, the grants it holds per
55
61
  * key, and per-key request pacing. Every query is scoped to the channel, so
@@ -1,63 +1,63 @@
1
- import { z } from 'zod';
2
- export const queueItemRowSchema = z.object({
3
- id: z.string(),
4
- key: z.string(),
5
- grp: z.string(),
6
- payload: z.string(),
1
+ import * as v from 'valibot';
2
+ export const queueItemRowSchema = v.object({
3
+ id: v.string(),
4
+ key: v.string(),
5
+ grp: v.string(),
6
+ payload: v.string(),
7
7
  /** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
8
- status: z.enum(['queued', 'claiming', 'admitted']),
9
- grant_id: z.string().nullable(),
10
- lease_id: z.string().nullable(),
11
- created_at: z.number().int().nonnegative(),
12
- admitted_at: z.number().int().nonnegative().nullable(),
8
+ status: v.picklist(['queued', 'claiming', 'admitted']),
9
+ grant_id: v.nullable(v.string()),
10
+ lease_id: v.nullable(v.string()),
11
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
12
+ admitted_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
13
13
  /** 0 until `admit()` has returned for this item. */
14
- notified: z.number().int().min(0).max(1),
15
- attempt: z.number().int().nonnegative(),
16
- next_attempt_at: z.number().int().nonnegative(),
14
+ notified: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
15
+ attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
16
+ next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
17
17
  });
18
- export const queueGrantRowSchema = z.object({
19
- grant_id: z.string(),
20
- key: z.string(),
21
- grp: z.string(),
22
- permits: z.number().int().nonnegative(),
18
+ export const queueGrantRowSchema = v.object({
19
+ grant_id: v.string(),
20
+ key: v.string(),
21
+ grp: v.string(),
22
+ permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
23
23
  /** Permits counted against this grant locally, as a high-water mark. */
24
- claimed: z.number().int().nonnegative(),
24
+ claimed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
25
25
  /** Claims the holder has confirmed. */
26
- confirmed: z.number().int().nonnegative(),
26
+ confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
27
27
  /** Items handed to `admit()` from this grant; never derived from live rows. */
28
- admitted: z.number().int().nonnegative(),
29
- released: z.number().int().nonnegative(),
30
- created_at: z.number().int().nonnegative(),
28
+ admitted: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
29
+ released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
30
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
31
31
  });
32
- export const queueKeyRowSchema = z.object({
33
- key: z.string(),
32
+ export const queueKeyRowSchema = v.object({
33
+ key: v.string(),
34
34
  /** Earliest time the next permit request for this key may be sent. */
35
- next_request_at: z.number().int().nonnegative(),
36
- queued: z.number().int().nonnegative(),
35
+ next_request_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
36
+ queued: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
37
37
  });
38
- export const queueLeaseRowSchema = z.object({
39
- lease_id: z.string(),
40
- key: z.string(),
41
- grp: z.string(),
42
- permits: z.number().int().nonnegative(),
38
+ export const queueLeaseRowSchema = v.object({
39
+ lease_id: v.string(),
40
+ key: v.string(),
41
+ grp: v.string(),
42
+ permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
43
43
  /** Permits drawn so far; a drawn permit is never drawable again. */
44
- drawn: z.number().int().nonnegative(),
45
- released: z.number().int().nonnegative(),
46
- created_at: z.number().int().nonnegative(),
47
- expires_at: z.number().int().nonnegative(),
44
+ drawn: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
45
+ released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
46
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
47
+ expires_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
48
48
  /** When to report the lease again while it has permits in use, so the holder keeps it. */
49
- renew_at: z.number().int().nonnegative(),
49
+ renew_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
50
50
  /** Half the lease's lifetime at the holder, from its last refresh. */
51
- renew_ms: z.number().int().positive(),
51
+ renew_ms: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
52
52
  /** 1 once the holder has settled the stopped lease; until then it is reported again. */
53
- confirmed: z.number().int().min(0).max(1),
53
+ confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
54
54
  });
55
- const queueItemColumns = queueItemRowSchema.keyof().options;
56
- const queueGrantColumns = queueGrantRowSchema.keyof().options;
57
- const queueLeaseColumns = queueLeaseRowSchema.keyof().options;
58
- const statusCountSchema = z.object({
59
- status: queueItemRowSchema.shape.status,
60
- count: z.number().int().nonnegative(),
55
+ const queueItemColumns = v.keyof(queueItemRowSchema).options;
56
+ const queueGrantColumns = v.keyof(queueGrantRowSchema).options;
57
+ const queueLeaseColumns = v.keyof(queueLeaseRowSchema).options;
58
+ const statusCountSchema = v.object({
59
+ status: queueItemRowSchema.entries.status,
60
+ count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
61
61
  });
62
62
  /**
63
63
  * One queue channel of a semaphore client: items, the grants it holds per
@@ -121,7 +121,7 @@ export class QueueStore {
121
121
  }
122
122
  countItems() {
123
123
  const counts = { queued: 0, claiming: 0, admitted: 0 };
124
- for (const row of statusCountSchema.array().parse(this.sql
124
+ for (const row of v.parse(v.array(statusCountSchema), this.sql
125
125
  .exec(`SELECT status, COUNT(*) AS count FROM durability_semaphore_queue_items
126
126
  WHERE channel = ? GROUP BY status`, this.channel)
127
127
  .toArray())) {
@@ -130,7 +130,7 @@ export class QueueStore {
130
130
  return counts;
131
131
  }
132
132
  getGrant(grantId) {
133
- return queueGrantRowSchema.optional().parse(this.sql
133
+ return v.parse(v.optional(queueGrantRowSchema), this.sql
134
134
  .exec(`SELECT * FROM durability_semaphore_queue_grants
135
135
  WHERE channel = ? AND grant_id = ? LIMIT 1`, this.channel, grantId)
136
136
  .toArray()[0]);
@@ -152,13 +152,13 @@ export class QueueStore {
152
152
  this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE channel = ? AND grant_id = ?', this.channel, grantId);
153
153
  }
154
154
  listGrants(key) {
155
- return queueGrantRowSchema.array().parse(this.sql
155
+ return v.parse(v.array(queueGrantRowSchema), this.sql
156
156
  .exec(`SELECT * FROM durability_semaphore_queue_grants
157
157
  WHERE channel = ? AND key = ? ORDER BY created_at ASC, grant_id ASC`, this.channel, key)
158
158
  .toArray());
159
159
  }
160
160
  getLease(leaseId) {
161
- return queueLeaseRowSchema.optional().parse(this.sql
161
+ return v.parse(v.optional(queueLeaseRowSchema), this.sql
162
162
  .exec(`SELECT * FROM durability_semaphore_queue_leases
163
163
  WHERE channel = ? AND lease_id = ? LIMIT 1`, this.channel, leaseId)
164
164
  .toArray()[0]);
@@ -183,7 +183,7 @@ export class QueueStore {
183
183
  * reported, so a lost report must be repeated.
184
184
  */
185
185
  listRenewalsDue(now, limit) {
186
- return queueLeaseRowSchema.array().parse(this.sql
186
+ return v.parse(v.array(queueLeaseRowSchema), this.sql
187
187
  .exec(`SELECT * FROM durability_semaphore_queue_leases
188
188
  WHERE channel = ? AND confirmed = 0 AND renew_at <= ?
189
189
  AND (released < drawn OR expires_at <= ?)
@@ -197,13 +197,13 @@ export class QueueStore {
197
197
  AND released >= drawn`, this.channel, before);
198
198
  }
199
199
  listLeases(key) {
200
- return queueLeaseRowSchema.array().parse(this.sql
200
+ return v.parse(v.array(queueLeaseRowSchema), this.sql
201
201
  .exec(`SELECT * FROM durability_semaphore_queue_leases
202
202
  WHERE channel = ? AND key = ? ORDER BY created_at ASC, lease_id ASC`, this.channel, key)
203
203
  .toArray());
204
204
  }
205
205
  getKey(key) {
206
- return queueKeyRowSchema.optional().parse(this.sql
206
+ return v.parse(v.optional(queueKeyRowSchema), this.sql
207
207
  .exec(`SELECT * FROM durability_semaphore_queue_keys
208
208
  WHERE channel = ? AND key = ? LIMIT 1`, this.channel, key)
209
209
  .toArray()[0]);
@@ -221,7 +221,7 @@ export class QueueStore {
221
221
  this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE channel = ? AND key = ?', this.channel, key);
222
222
  }
223
223
  listDueKeys(now) {
224
- return queueKeyRowSchema.array().parse(this.sql
224
+ return v.parse(v.array(queueKeyRowSchema), this.sql
225
225
  .exec(`SELECT * FROM durability_semaphore_queue_keys
226
226
  WHERE channel = ? AND queued > 0 AND next_request_at <= ?
227
227
  ORDER BY next_request_at ASC`, this.channel, now)
@@ -253,8 +253,6 @@ export class QueueStore {
253
253
  return typeof at === 'number' ? at : undefined;
254
254
  }
255
255
  items(query, ...bindings) {
256
- return queueItemRowSchema
257
- .array()
258
- .parse(this.sql.exec(query, ...bindings).toArray());
256
+ return v.parse(v.array(queueItemRowSchema), this.sql.exec(query, ...bindings).toArray());
259
257
  }
260
258
  }
@@ -10,6 +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 _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending, _DurabilitySemaphoreServer_roundTimer, _DurabilitySemaphoreServer_round, _DurabilitySemaphoreServer_lastRoundAt, _DurabilitySemaphoreServer_contended, _DurabilitySemaphoreServer_grantRoom, _DurabilitySemaphoreServer_scheduleRound, _DurabilitySemaphoreServer_runRound, _DurabilitySemaphoreServer_allocate, _DurabilitySemaphoreServer_sync, _DurabilitySemaphoreServer_applyReports, _DurabilitySemaphoreServer_reclaim, _DurabilitySemaphoreServer_reclaimExpired, _DurabilitySemaphoreServer_resolveLimit, _DurabilitySemaphoreServer_groupPolicy;
13
+ import * as v from 'valibot';
13
14
  import { traceLifecycle, traceMethods } from '../dev.js';
14
15
  import { SemaphoreProtocolError, reportFailure } from '../errors.js';
15
16
  import { durabilitySemaphoreMigrations } from './migrations.js';
@@ -139,14 +140,14 @@ export class DurabilitySemaphoreServer {
139
140
  * fails and the client asks again.
140
141
  */
141
142
  handle(message) {
142
- const parsed = semaphoreRequestSchema.safeParse(message);
143
+ const parsed = v.safeParse(semaphoreRequestSchema, message);
143
144
  if (!parsed.success) {
144
145
  return Promise.reject(new SemaphoreProtocolError('Malformed semaphore message'));
145
146
  }
146
- if (parsed.data.type === 'sync') {
147
- return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.data);
147
+ if (parsed.output.type === 'sync') {
148
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.output);
148
149
  }
149
- const request = parsed.data;
150
+ const request = parsed.output;
150
151
  return new Promise((resolve, reject) => {
151
152
  __classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({
152
153
  request,
@@ -1,29 +1,34 @@
1
- import { z } from 'zod';
2
- export declare const grantRowSchema: z.ZodObject<{
3
- grant_id: z.ZodString;
4
- client_id: z.ZodString;
5
- client_channel: z.ZodString;
6
- grp: z.ZodString;
7
- permits: z.ZodNumber;
8
- claimed: z.ZodNumber;
9
- released: z.ZodNumber;
10
- created_at: z.ZodNumber;
11
- expires_at: z.ZodNumber;
12
- }, z.core.$strip>;
13
- export type GrantRow = z.infer<typeof grantRowSchema>;
1
+ import * as v from 'valibot';
2
+ export declare const grantRowSchema: v.ObjectSchema<{
3
+ readonly grant_id: v.StringSchema<undefined>;
4
+ readonly client_id: v.StringSchema<undefined>;
5
+ /** The client's queue channel; `clientId` alone does not identify a client. */
6
+ readonly client_channel: v.StringSchema<undefined>;
7
+ readonly grp: v.StringSchema<undefined>;
8
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
9
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
10
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
11
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
12
+ /** Unclaimed permits are reclaimed once this passes. */
13
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
14
+ }, undefined>;
15
+ export type GrantRow = v.InferOutput<typeof grantRowSchema>;
14
16
  /** Permits a client may draw locally without a round trip; held by the holder. */
15
- export declare const leaseRowSchema: z.ZodObject<{
16
- lease_id: z.ZodString;
17
- client_id: z.ZodString;
18
- client_channel: z.ZodString;
19
- grp: z.ZodString;
20
- permits: z.ZodNumber;
21
- released: z.ZodNumber;
22
- created_at: z.ZodNumber;
23
- expires_at: z.ZodNumber;
24
- retired: z.ZodNumber;
25
- }, z.core.$strip>;
26
- export type LeaseRow = z.infer<typeof leaseRowSchema>;
17
+ export declare const leaseRowSchema: v.ObjectSchema<{
18
+ readonly lease_id: v.StringSchema<undefined>;
19
+ readonly client_id: v.StringSchema<undefined>;
20
+ readonly client_channel: v.StringSchema<undefined>;
21
+ readonly grp: v.StringSchema<undefined>;
22
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
23
+ /** Drawn permits the client has returned, as a high-water mark. */
24
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
25
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
26
+ /** Refreshed by every report; the lease is swept once this passes. */
27
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
28
+ /** 1 once no more permits may be drawn; footprint stays until released. */
29
+ readonly retired: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
30
+ }, undefined>;
31
+ export type LeaseRow = v.InferOutput<typeof leaseRowSchema>;
27
32
  /**
28
33
  * Permits one semaphore channel has handed out. Every query is scoped to the
29
34
  * channel, so semaphores sharing an object never see each other's grants or