@vllnt/convex-quota 0.1.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/client/index.d.ts +59 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +58 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +32 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +38 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +100 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +4 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/mutations.d.ts +43 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +164 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +25 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +52 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +34 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +22 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +101 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +61 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +66 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +208 -0
- package/dist/shared.js.map +1 -0
- package/docs/API.md +94 -0
- package/llms-full.txt +1324 -0
- package/llms.txt +43 -0
- package/package.json +90 -0
- package/src/client/index.ts +179 -0
- package/src/client/types.ts +37 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +114 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/mutations.ts +219 -0
- package/src/component/queries.ts +72 -0
- package/src/component/schema.ts +22 -0
- package/src/component/validators.ts +89 -0
- package/src/shared.ts +324 -0
- package/src/test.ts +16 -0
package/src/shared.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/** Shared constants and window math used by both `client/` and `component/`. */
|
|
2
|
+
|
|
3
|
+
export const COMPONENT_NAME = "quota";
|
|
4
|
+
|
|
5
|
+
/** Default namespace when the host does not scope an allowance. */
|
|
6
|
+
export const DEFAULT_SCOPE = "global";
|
|
7
|
+
|
|
8
|
+
/** Length limit for opaque refs. */
|
|
9
|
+
export const MAX_REF_LENGTH = 256;
|
|
10
|
+
|
|
11
|
+
/** Default / max rows deleted per erase pass before the sweep reschedules. */
|
|
12
|
+
export const DEFAULT_ERASE_BATCH = 200;
|
|
13
|
+
export const MAX_ERASE_BATCH = 500;
|
|
14
|
+
|
|
15
|
+
export type CalendarPeriod = "day" | "month" | "week";
|
|
16
|
+
export type WeekStartsOn = "monday" | "sunday";
|
|
17
|
+
|
|
18
|
+
export type WindowSpec =
|
|
19
|
+
| {
|
|
20
|
+
durationMs: number;
|
|
21
|
+
kind: "epoch";
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
durationMs: number;
|
|
25
|
+
kind: "rolling";
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
kind: "calendar";
|
|
29
|
+
period: CalendarPeriod;
|
|
30
|
+
timeZone: string;
|
|
31
|
+
weekStartsOn?: WeekStartsOn;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ResolvedWindow = {
|
|
35
|
+
endAt: number;
|
|
36
|
+
periodKey: string;
|
|
37
|
+
startAt: number;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Stable policy identity; hosts must use a new key to change window policy. */
|
|
41
|
+
export function windowPolicyKey(spec: WindowSpec): string {
|
|
42
|
+
return spec.kind === "calendar"
|
|
43
|
+
? JSON.stringify([
|
|
44
|
+
spec.kind,
|
|
45
|
+
spec.period,
|
|
46
|
+
spec.timeZone,
|
|
47
|
+
spec.period === "week" ? (spec.weekStartsOn ?? "monday") : undefined,
|
|
48
|
+
])
|
|
49
|
+
: JSON.stringify([spec.kind, spec.durationMs]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const formatterCache = new Map<string, Intl.DateTimeFormat>();
|
|
53
|
+
|
|
54
|
+
function pad2(value: number): string {
|
|
55
|
+
return value.toString().padStart(2, "0");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function assertFinitePositive(
|
|
59
|
+
value: number,
|
|
60
|
+
code: string,
|
|
61
|
+
message: string,
|
|
62
|
+
): void {
|
|
63
|
+
if (!(value > 0 && Number.isSafeInteger(value))) {
|
|
64
|
+
throw new Error(`${code}:${message}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function formatterFor(timeZone: string): Intl.DateTimeFormat {
|
|
69
|
+
const cached = formatterCache.get(timeZone);
|
|
70
|
+
if (cached !== undefined) {
|
|
71
|
+
return cached;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const created = new Intl.DateTimeFormat("en-US", {
|
|
75
|
+
day: "2-digit",
|
|
76
|
+
hour: "2-digit",
|
|
77
|
+
hourCycle: "h23",
|
|
78
|
+
minute: "2-digit",
|
|
79
|
+
month: "2-digit",
|
|
80
|
+
second: "2-digit",
|
|
81
|
+
timeZone,
|
|
82
|
+
weekday: "short",
|
|
83
|
+
year: "numeric",
|
|
84
|
+
});
|
|
85
|
+
formatterCache.set(timeZone, created);
|
|
86
|
+
return created;
|
|
87
|
+
} catch {
|
|
88
|
+
throw new Error(`INVALID_TIME_ZONE: ${timeZone}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function partMap(
|
|
93
|
+
parts: readonly Intl.DateTimeFormatPart[],
|
|
94
|
+
): Record<string, string> {
|
|
95
|
+
return parts.reduce<Record<string, string>>((accumulator, part) => {
|
|
96
|
+
accumulator[part.type] = part.value;
|
|
97
|
+
return accumulator;
|
|
98
|
+
}, {});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Read calendar parts of `now` in `timeZone`. Throws if `timeZone` is not a
|
|
103
|
+
* valid IANA name.
|
|
104
|
+
*/
|
|
105
|
+
export function zonedParts(
|
|
106
|
+
now: number,
|
|
107
|
+
timeZone: string,
|
|
108
|
+
): {
|
|
109
|
+
day: number;
|
|
110
|
+
hour: number;
|
|
111
|
+
minute: number;
|
|
112
|
+
month: number;
|
|
113
|
+
second: number;
|
|
114
|
+
weekday: number;
|
|
115
|
+
year: number;
|
|
116
|
+
} {
|
|
117
|
+
const byType = partMap(formatterFor(timeZone).formatToParts(new Date(now)));
|
|
118
|
+
const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].indexOf(
|
|
119
|
+
String(byType.weekday),
|
|
120
|
+
);
|
|
121
|
+
return {
|
|
122
|
+
day: Number(byType.day),
|
|
123
|
+
hour: Number(byType.hour),
|
|
124
|
+
minute: Number(byType.minute),
|
|
125
|
+
month: Number(byType.month),
|
|
126
|
+
second: Number(byType.second),
|
|
127
|
+
weekday,
|
|
128
|
+
year: Number(byType.year),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type CivilDate = { day: number; month: number; year: number };
|
|
133
|
+
|
|
134
|
+
function stepMidnight(
|
|
135
|
+
guess: number,
|
|
136
|
+
{ day, month, year }: CivilDate,
|
|
137
|
+
timeZone: string,
|
|
138
|
+
): number {
|
|
139
|
+
const parts = zonedParts(guess, timeZone);
|
|
140
|
+
const asUtc = Date.UTC(
|
|
141
|
+
parts.year,
|
|
142
|
+
parts.month - 1,
|
|
143
|
+
parts.day,
|
|
144
|
+
parts.hour,
|
|
145
|
+
parts.minute,
|
|
146
|
+
parts.second,
|
|
147
|
+
);
|
|
148
|
+
const target = Date.UTC(year, month - 1, day);
|
|
149
|
+
const diff = asUtc - target;
|
|
150
|
+
if (diff === 0) {
|
|
151
|
+
return guess;
|
|
152
|
+
}
|
|
153
|
+
return guess - diff;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* UTC ms for the start of a civil date. Repeated midnight chooses the earlier
|
|
158
|
+
* occurrence; a skipped midnight/date advances through the timezone gap.
|
|
159
|
+
*/
|
|
160
|
+
export function utcMsForLocalMidnight(
|
|
161
|
+
{ day, month, year }: CivilDate,
|
|
162
|
+
timeZone: string,
|
|
163
|
+
): number {
|
|
164
|
+
const first = Date.UTC(year, month - 1, day);
|
|
165
|
+
const corrected = stepMidnight(first, { day, month, year }, timeZone);
|
|
166
|
+
const candidates = [-86_400_000, 0, 86_400_000].map((delta) =>
|
|
167
|
+
stepMidnight(corrected + delta, { day, month, year }, timeZone),
|
|
168
|
+
);
|
|
169
|
+
const exact = candidates.filter((candidate) => {
|
|
170
|
+
const parts = zonedParts(candidate, timeZone);
|
|
171
|
+
return (
|
|
172
|
+
parts.year === year &&
|
|
173
|
+
parts.month === month &&
|
|
174
|
+
parts.day === day &&
|
|
175
|
+
parts.hour === 0 &&
|
|
176
|
+
parts.minute === 0 &&
|
|
177
|
+
parts.second === 0
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
return exact.length > 0
|
|
181
|
+
? Math.min(...exact)
|
|
182
|
+
: Math.max(corrected, ...candidates);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function addDays(
|
|
186
|
+
{ day, month, year }: CivilDate,
|
|
187
|
+
delta: number,
|
|
188
|
+
): { day: number; month: number; year: number } {
|
|
189
|
+
const date = new Date(Date.UTC(year, month - 1, day + delta));
|
|
190
|
+
return {
|
|
191
|
+
day: date.getUTCDate(),
|
|
192
|
+
month: date.getUTCMonth() + 1,
|
|
193
|
+
year: date.getUTCFullYear(),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** ISO week (Monday start) for a civil date. */
|
|
198
|
+
export function isoWeek(
|
|
199
|
+
year: number,
|
|
200
|
+
month: number,
|
|
201
|
+
day: number,
|
|
202
|
+
): { week: number; year: number } {
|
|
203
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
204
|
+
const thursday = new Date(date);
|
|
205
|
+
thursday.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
|
|
206
|
+
const yearStart = new Date(Date.UTC(thursday.getUTCFullYear(), 0, 1));
|
|
207
|
+
const week = Math.ceil(
|
|
208
|
+
((thursday.getTime() - yearStart.getTime()) / 86_400_000 + 1) / 7,
|
|
209
|
+
);
|
|
210
|
+
return { week, year: thursday.getUTCFullYear() };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function resolveWeek(
|
|
214
|
+
parts: ReturnType<typeof zonedParts>,
|
|
215
|
+
timeZone: string,
|
|
216
|
+
weekStartsOn: WeekStartsOn,
|
|
217
|
+
): ResolvedWindow {
|
|
218
|
+
const sunday = weekStartsOn === "sunday";
|
|
219
|
+
const mondayDelta = parts.weekday === 0 ? -6 : 1 - parts.weekday;
|
|
220
|
+
const delta = sunday ? -parts.weekday : mondayDelta;
|
|
221
|
+
const start = addDays(parts, delta);
|
|
222
|
+
const end = addDays(start, 7);
|
|
223
|
+
const labelDate = sunday ? start : parts;
|
|
224
|
+
const label = isoWeek(labelDate.year, labelDate.month, labelDate.day);
|
|
225
|
+
return {
|
|
226
|
+
endAt: utcMsForLocalMidnight(end, timeZone),
|
|
227
|
+
periodKey: `${label.year.toString()}-W${pad2(label.week)}${sunday ? "-sun" : ""}`,
|
|
228
|
+
startAt: utcMsForLocalMidnight(start, timeZone),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function resolveCalendar(
|
|
233
|
+
now: number,
|
|
234
|
+
spec: Extract<WindowSpec, { kind: "calendar" }>,
|
|
235
|
+
): ResolvedWindow {
|
|
236
|
+
const { period, timeZone, weekStartsOn } = spec;
|
|
237
|
+
const parts = zonedParts(now, timeZone);
|
|
238
|
+
if (period === "week")
|
|
239
|
+
return resolveWeek(parts, timeZone, weekStartsOn ?? "monday");
|
|
240
|
+
const start = period === "day" ? parts : { ...parts, day: 1 };
|
|
241
|
+
const next =
|
|
242
|
+
period === "day"
|
|
243
|
+
? addDays(parts, 1)
|
|
244
|
+
: {
|
|
245
|
+
day: 1,
|
|
246
|
+
month: parts.month === 12 ? 1 : parts.month + 1,
|
|
247
|
+
year: parts.month === 12 ? parts.year + 1 : parts.year,
|
|
248
|
+
};
|
|
249
|
+
return {
|
|
250
|
+
endAt: utcMsForLocalMidnight(next, timeZone),
|
|
251
|
+
periodKey: `${parts.year.toString()}-${pad2(parts.month)}${period === "day" ? `-${pad2(parts.day)}` : ""}`,
|
|
252
|
+
startAt: utcMsForLocalMidnight(start, timeZone),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Resolve the current window for `spec` at `now`. For `rolling`, pass the
|
|
258
|
+
* stored `windowStartAt` to reuse an in-progress window until it elapses.
|
|
259
|
+
*/
|
|
260
|
+
function validateDuration(now: number, durationMs: number): void {
|
|
261
|
+
assertFinitePositive(
|
|
262
|
+
durationMs,
|
|
263
|
+
"INVALID_DURATION",
|
|
264
|
+
"durationMs must be a positive safe integer",
|
|
265
|
+
);
|
|
266
|
+
assertFinitePositive(
|
|
267
|
+
now + durationMs,
|
|
268
|
+
"INVALID_DURATION",
|
|
269
|
+
"window end must be a positive safe integer",
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function resolveWindow(
|
|
274
|
+
now: number,
|
|
275
|
+
spec: WindowSpec,
|
|
276
|
+
rollingStartAt?: number,
|
|
277
|
+
): ResolvedWindow {
|
|
278
|
+
if (spec.kind === "calendar") {
|
|
279
|
+
return resolveCalendar(now, spec);
|
|
280
|
+
}
|
|
281
|
+
validateDuration(now, spec.durationMs);
|
|
282
|
+
if (spec.kind === "epoch") {
|
|
283
|
+
const index = Math.floor(now / spec.durationMs);
|
|
284
|
+
const startAt = index * spec.durationMs;
|
|
285
|
+
return {
|
|
286
|
+
endAt: startAt + spec.durationMs,
|
|
287
|
+
periodKey: `e${index.toString()}`,
|
|
288
|
+
startAt,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
const startAt =
|
|
292
|
+
rollingStartAt !== undefined && now < rollingStartAt + spec.durationMs
|
|
293
|
+
? rollingStartAt
|
|
294
|
+
: now;
|
|
295
|
+
return {
|
|
296
|
+
endAt: startAt + spec.durationMs,
|
|
297
|
+
periodKey: `r${startAt.toString()}`,
|
|
298
|
+
startAt,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function interpretWindowError(error: unknown): {
|
|
303
|
+
code: "INVALID_DURATION" | "INVALID_TIME_ZONE";
|
|
304
|
+
message: string;
|
|
305
|
+
} {
|
|
306
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
307
|
+
if (message.startsWith("INVALID_DURATION:")) {
|
|
308
|
+
return {
|
|
309
|
+
code: "INVALID_DURATION",
|
|
310
|
+
message: message.slice("INVALID_DURATION:".length),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
code: "INVALID_TIME_ZONE",
|
|
315
|
+
message: message.replace(/^INVALID_TIME_ZONE:\s*/, ""),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function clampEraseBatch(batch: number): number {
|
|
320
|
+
if (!Number.isInteger(batch) || batch < 1) {
|
|
321
|
+
throw new Error("INVALID_BATCH: batch must be a positive integer");
|
|
322
|
+
}
|
|
323
|
+
return Math.min(batch, MAX_ERASE_BATCH);
|
|
324
|
+
}
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TestConvex } from "convex-test";
|
|
2
|
+
|
|
3
|
+
import schema from "./component/schema";
|
|
4
|
+
|
|
5
|
+
const modules = import.meta.glob("./component/**/*.ts");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Register this component with a `convex-test` instance so consuming apps can
|
|
9
|
+
* test integration: `import { register } from "@vllnt/convex-quota/test"`.
|
|
10
|
+
*/
|
|
11
|
+
export function register(
|
|
12
|
+
t: Pick<TestConvex<typeof schema>, "registerComponent">,
|
|
13
|
+
name = "quota",
|
|
14
|
+
): void {
|
|
15
|
+
t.registerComponent(name, schema, modules);
|
|
16
|
+
}
|