@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.
Files changed (68) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE +21 -0
  3. package/README.md +158 -0
  4. package/dist/client/index.d.ts +59 -0
  5. package/dist/client/index.d.ts.map +1 -0
  6. package/dist/client/index.js +58 -0
  7. package/dist/client/index.js.map +1 -0
  8. package/dist/client/types.d.ts +32 -0
  9. package/dist/client/types.d.ts.map +1 -0
  10. package/dist/client/types.js +3 -0
  11. package/dist/client/types.js.map +1 -0
  12. package/dist/component/_generated/api.d.ts +38 -0
  13. package/dist/component/_generated/api.d.ts.map +1 -0
  14. package/dist/component/_generated/api.js +31 -0
  15. package/dist/component/_generated/api.js.map +1 -0
  16. package/dist/component/_generated/component.d.ts +100 -0
  17. package/dist/component/_generated/component.d.ts.map +1 -0
  18. package/dist/component/_generated/component.js +11 -0
  19. package/dist/component/_generated/component.js.map +1 -0
  20. package/dist/component/_generated/dataModel.d.ts +46 -0
  21. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  22. package/dist/component/_generated/dataModel.js +11 -0
  23. package/dist/component/_generated/dataModel.js.map +1 -0
  24. package/dist/component/_generated/server.d.ts +133 -0
  25. package/dist/component/_generated/server.d.ts.map +1 -0
  26. package/dist/component/_generated/server.js +80 -0
  27. package/dist/component/_generated/server.js.map +1 -0
  28. package/dist/component/convex.config.d.ts +3 -0
  29. package/dist/component/convex.config.d.ts.map +1 -0
  30. package/dist/component/convex.config.js +4 -0
  31. package/dist/component/convex.config.js.map +1 -0
  32. package/dist/component/mutations.d.ts +43 -0
  33. package/dist/component/mutations.d.ts.map +1 -0
  34. package/dist/component/mutations.js +164 -0
  35. package/dist/component/mutations.js.map +1 -0
  36. package/dist/component/queries.d.ts +25 -0
  37. package/dist/component/queries.d.ts.map +1 -0
  38. package/dist/component/queries.js +52 -0
  39. package/dist/component/queries.js.map +1 -0
  40. package/dist/component/schema.d.ts +34 -0
  41. package/dist/component/schema.d.ts.map +1 -0
  42. package/dist/component/schema.js +22 -0
  43. package/dist/component/schema.js.map +1 -0
  44. package/dist/component/validators.d.ts +101 -0
  45. package/dist/component/validators.d.ts.map +1 -0
  46. package/dist/component/validators.js +61 -0
  47. package/dist/component/validators.js.map +1 -0
  48. package/dist/shared.d.ts +66 -0
  49. package/dist/shared.d.ts.map +1 -0
  50. package/dist/shared.js +208 -0
  51. package/dist/shared.js.map +1 -0
  52. package/docs/API.md +94 -0
  53. package/llms-full.txt +1324 -0
  54. package/llms.txt +43 -0
  55. package/package.json +90 -0
  56. package/src/client/index.ts +179 -0
  57. package/src/client/types.ts +37 -0
  58. package/src/component/_generated/api.ts +54 -0
  59. package/src/component/_generated/component.ts +114 -0
  60. package/src/component/_generated/dataModel.ts +60 -0
  61. package/src/component/_generated/server.ts +169 -0
  62. package/src/component/convex.config.ts +5 -0
  63. package/src/component/mutations.ts +219 -0
  64. package/src/component/queries.ts +72 -0
  65. package/src/component/schema.ts +22 -0
  66. package/src/component/validators.ts +89 -0
  67. package/src/shared.ts +324 -0
  68. package/src/test.ts +16 -0
package/llms.txt ADDED
@@ -0,0 +1,43 @@
1
+ # @vllnt/convex-quota
2
+
3
+ > Experimental scheduled-reset per-subject allowances; not yet released.
4
+
5
+ Targets Node >=22.12.0 and convex@^1.45.0. One sandboxed allowance row per
6
+ (scope, subjectRef, key) stores used, limit, periodKey, policyKey and window times.
7
+ Calendar windows support IANA timezones and day/week/month resets; rolling windows
8
+ start on first allowed consume; epoch windows align to Unix epoch. A key's window
9
+ policy cannot change (POLICY_MISMATCH); limits may change without resetting usage.
10
+ Refs/scopes are 1..256 UTF-16 code units. Amounts/limits are positive safe integers.
11
+ Durations are positive safe integer milliseconds with safe end timestamps.
12
+
13
+ consume(ctx, subjectRef, key, limit, window, opts?) returns allowed, used,
14
+ remaining, limit, periodKey, resetsAt; opts is {amount?, scope?}.
15
+ remaining(ctx, subjectRef, key, limit, window, scope?) returns the same without
16
+ allowed. It is a query-execution snapshot: subscriptions do not refresh just
17
+ because time passes. consume always rechecks server time.
18
+ refund(ctx, subjectRef, key, amount, periodKey, scope?) returns refunded, used,
19
+ remaining. Requires matching unexpired stored period; clamps usage at zero.
20
+ Consume and refund are NOT idempotent; hosts must deduplicate events. A period
21
+ label is not a receipt and can be reused after erasure.
22
+ eraseSubject(ctx, subjectRef, scope?, batch?) returns this pass's deletion count;
23
+ batches default to 200 and cap at 500, scheduling continuation when full. Hosts
24
+ must stop concurrent writes while cleanup drains. No tombstones or audit history.
25
+
26
+ Host owns auth, authorization and all policy. Multiple named mounts isolate data;
27
+ runtime scopes isolate indexed reads/erasure within a mount. Official
28
+ @convex-dev/rate-limiter already has fixed windows, token buckets, readable checks
29
+ and React hooks; prefer it for general throttling. This experiment's narrower
30
+ calendar/timezone/refund boundary still needs independent consumer justification.
31
+ Publication is blocked; passing tests alone does not establish adoption or readiness.
32
+
33
+ ## Docs
34
+
35
+ - [README](README.md): Setup, behavior, security, multi-mount and limitations
36
+ - [API Reference](docs/API.md): Exact signatures, results and error semantics
37
+ - [Changelog](CHANGELOG.md): Unreleased changes and development schema migration
38
+ - [Contributing](CONTRIBUTING.md): Development workflow
39
+ - [Example wrapper](example/convex/example.ts): Unauthenticated test fixtures, not production endpoints
40
+ - [Tests](example/convex/example.test.ts): convex-test mock integration tests, not real OCC proof
41
+ - [Security Policy](SECURITY.md): Reporting
42
+ - [License](LICENSE): MIT
43
+ - [Full source bundle](llms-full.txt): Generated docs and source
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@vllnt/convex-quota",
3
+ "version": "0.1.0-canary.0",
4
+ "description": "Scheduled-reset per-subject allowances as a Convex component — calendar, rolling, or epoch windows with readable remaining",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "packageManager": "pnpm@9.15.4",
8
+ "files": [
9
+ "dist",
10
+ "src",
11
+ "docs/API.md",
12
+ "CHANGELOG.md",
13
+ "llms.txt",
14
+ "llms-full.txt"
15
+ ],
16
+ "exports": {
17
+ "./package.json": "./package.json",
18
+ ".": {
19
+ "types": "./dist/client/index.d.ts",
20
+ "default": "./dist/client/index.js"
21
+ },
22
+ "./test": "./src/test.ts",
23
+ "./_generated/component.js": {
24
+ "types": "./dist/component/_generated/component.d.ts"
25
+ },
26
+ "./_generated/component": {
27
+ "types": "./dist/component/_generated/component.d.ts"
28
+ },
29
+ "./convex.config": {
30
+ "types": "./dist/component/convex.config.d.ts",
31
+ "default": "./dist/component/convex.config.js"
32
+ },
33
+ "./convex.config.js": {
34
+ "types": "./dist/component/convex.config.d.ts",
35
+ "default": "./dist/component/convex.config.js"
36
+ }
37
+ },
38
+ "types": "./dist/client/index.d.ts",
39
+ "module": "./dist/client/index.js",
40
+ "scripts": {},
41
+ "peerDependencies": {
42
+ "convex": "^1.45.0"
43
+ },
44
+ "devDependencies": {
45
+ "@edge-runtime/vm": "^5.0.0",
46
+ "@typescript/native": "npm:typescript@7.0.2",
47
+ "@vitest/coverage-v8": "^4.1.11",
48
+ "@vllnt/eslint-config": "2.0.0",
49
+ "@vllnt/typescript": "2.0.0",
50
+ "convex": "^1.45.0",
51
+ "convex-test": "^0.0.56",
52
+ "eslint": "^9.39.5",
53
+ "prettier": "^3.9.6",
54
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
55
+ "vite": "^8.2.2",
56
+ "vitest": "^4.1.11"
57
+ },
58
+ "homepage": "https://github.com/vllnt/convex-quota#readme",
59
+ "bugs": {
60
+ "url": "https://github.com/vllnt/convex-quota/issues"
61
+ },
62
+ "author": {
63
+ "name": "bntvllnt",
64
+ "url": "https://bntvllnt.com"
65
+ },
66
+ "funding": {
67
+ "type": "github",
68
+ "url": "https://github.com/sponsors/bntvllnt"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "https://github.com/vllnt/convex-quota"
73
+ },
74
+ "engines": {
75
+ "node": ">=22.12.0"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public",
79
+ "registry": "https://registry.npmjs.org",
80
+ "tag": "canary"
81
+ },
82
+ "sideEffects": false,
83
+ "keywords": [
84
+ "convex",
85
+ "convex-component",
86
+ "quota",
87
+ "rate-limit",
88
+ "allowance"
89
+ ]
90
+ }
@@ -0,0 +1,179 @@
1
+ import type {
2
+ FunctionArgs,
3
+ FunctionReference,
4
+ FunctionReturnType,
5
+ } from "convex/server";
6
+
7
+ import { DEFAULT_SCOPE } from "../shared.js";
8
+
9
+ import type {
10
+ ConsumeOptions,
11
+ ConsumeResult,
12
+ QuotaOptions,
13
+ RefundResult,
14
+ RemainingState,
15
+ WindowSpec,
16
+ } from "./types.js";
17
+
18
+ export type QuotaComponent = {
19
+ mutations: {
20
+ consume: FunctionReference<
21
+ "mutation",
22
+ "internal",
23
+ {
24
+ amount: number;
25
+ key: string;
26
+ limit: number;
27
+ scope: string;
28
+ subjectRef: string;
29
+ window: WindowSpec;
30
+ },
31
+ ConsumeResult
32
+ >;
33
+ eraseSubject: FunctionReference<
34
+ "mutation",
35
+ "internal",
36
+ { batch?: number; scope: string; subjectRef: string },
37
+ number
38
+ >;
39
+ refund: FunctionReference<
40
+ "mutation",
41
+ "internal",
42
+ {
43
+ amount: number;
44
+ key: string;
45
+ periodKey: string;
46
+ scope: string;
47
+ subjectRef: string;
48
+ },
49
+ RefundResult
50
+ >;
51
+ };
52
+ queries: {
53
+ remaining: FunctionReference<
54
+ "query",
55
+ "internal",
56
+ {
57
+ key: string;
58
+ limit: number;
59
+ scope: string;
60
+ subjectRef: string;
61
+ window: WindowSpec;
62
+ },
63
+ RemainingState
64
+ >;
65
+ };
66
+ };
67
+
68
+ type RunQueryCtx = {
69
+ runQuery<TQuery extends FunctionReference<"query", "internal">>(
70
+ reference: TQuery,
71
+ arguments_: FunctionArgs<TQuery>,
72
+ ): Promise<FunctionReturnType<TQuery>>;
73
+ };
74
+
75
+ type RunMutationCtx = {
76
+ runMutation<TMutation extends FunctionReference<"mutation", "internal">>(
77
+ reference: TMutation,
78
+ arguments_: FunctionArgs<TMutation>,
79
+ ): Promise<FunctionReturnType<TMutation>>;
80
+ };
81
+
82
+ /**
83
+ * Consumer-facing client for the scheduled-reset quota ledger. The host owns
84
+ * auth and meaning; it passes an opaque `subjectRef` + `key` and a window spec.
85
+ * Never take `limit` from an end-user. Time is server-sourced. Consume is not
86
+ * idempotent — wrap with `@vllnt/convex-idempotency` if retries must not double-count.
87
+ */
88
+ export class Quota {
89
+ private readonly defaultScope: string;
90
+
91
+ constructor(
92
+ private readonly component: QuotaComponent,
93
+ options: QuotaOptions = {},
94
+ ) {
95
+ this.defaultScope = options.defaultScope ?? DEFAULT_SCOPE;
96
+ }
97
+
98
+ private scopeOf(scope?: string): string {
99
+ return scope ?? this.defaultScope;
100
+ }
101
+
102
+ // eslint-disable-next-line max-params -- Preserve the published positional client contract.
103
+ consume(
104
+ ctx: RunMutationCtx,
105
+ subjectRef: string,
106
+ key: string,
107
+ limit: number,
108
+ window: WindowSpec,
109
+ options: ConsumeOptions = {},
110
+ ): Promise<ConsumeResult> {
111
+ return ctx.runMutation(this.component.mutations.consume, {
112
+ amount: options.amount ?? 1,
113
+ key,
114
+ limit,
115
+ scope: this.scopeOf(options.scope),
116
+ subjectRef,
117
+ window,
118
+ });
119
+ }
120
+
121
+ // eslint-disable-next-line max-params -- Preserve the published positional client contract.
122
+ remaining(
123
+ ctx: RunQueryCtx,
124
+ subjectRef: string,
125
+ key: string,
126
+ limit: number,
127
+ window: WindowSpec,
128
+ scope?: string,
129
+ ): Promise<RemainingState> {
130
+ return ctx.runQuery(this.component.queries.remaining, {
131
+ key,
132
+ limit,
133
+ scope: this.scopeOf(scope),
134
+ subjectRef,
135
+ window,
136
+ });
137
+ }
138
+
139
+ // eslint-disable-next-line max-params -- Preserve the published positional client contract.
140
+ refund(
141
+ ctx: RunMutationCtx,
142
+ subjectRef: string,
143
+ key: string,
144
+ amount: number,
145
+ periodKey: string,
146
+ scope?: string,
147
+ ): Promise<RefundResult> {
148
+ return ctx.runMutation(this.component.mutations.refund, {
149
+ amount,
150
+ key,
151
+ periodKey,
152
+ scope: this.scopeOf(scope),
153
+ subjectRef,
154
+ });
155
+ }
156
+
157
+ // eslint-disable-next-line max-params -- Preserve the published positional client contract.
158
+ eraseSubject(
159
+ ctx: RunMutationCtx,
160
+ subjectRef: string,
161
+ scope?: string,
162
+ batch?: number,
163
+ ): Promise<number> {
164
+ return ctx.runMutation(this.component.mutations.eraseSubject, {
165
+ batch,
166
+ scope: this.scopeOf(scope),
167
+ subjectRef,
168
+ });
169
+ }
170
+ }
171
+
172
+ export {
173
+ type ConsumeOptions,
174
+ type ConsumeResult,
175
+ type QuotaOptions,
176
+ type RefundResult,
177
+ type RemainingState,
178
+ type WindowSpec,
179
+ } from "./types.js";
@@ -0,0 +1,37 @@
1
+ /** Public TypeScript surface for the quota client. */
2
+
3
+ export type ConsumeResult = {
4
+ allowed: boolean;
5
+ limit: number;
6
+ periodKey: string;
7
+ remaining: number;
8
+ resetsAt: number;
9
+ used: number;
10
+ };
11
+
12
+ export type RemainingState = {
13
+ limit: number;
14
+ periodKey: string;
15
+ remaining: number;
16
+ resetsAt: number;
17
+ used: number;
18
+ };
19
+
20
+ export type RefundResult = {
21
+ refunded: boolean;
22
+ remaining: number;
23
+ used: number;
24
+ };
25
+
26
+ export type QuotaOptions = {
27
+ /** Namespace applied when a call omits `scope`. Default `"global"`. */
28
+ defaultScope?: string;
29
+ };
30
+
31
+ export type ConsumeOptions = {
32
+ /** Units to consume. Default `1`. */
33
+ amount?: number;
34
+ scope?: string;
35
+ };
36
+
37
+ export { type WindowSpec } from "../shared.js";
@@ -0,0 +1,54 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated `api` utility.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type * as mutations from "../mutations.js";
12
+ import type * as queries from "../queries.js";
13
+ import type * as validators from "../validators.js";
14
+
15
+ import type {
16
+ ApiFromModules,
17
+ FilterApi,
18
+ FunctionReference,
19
+ } from "convex/server";
20
+ import { anyApi, componentsGeneric } from "convex/server";
21
+
22
+ const fullApi: ApiFromModules<{
23
+ mutations: typeof mutations;
24
+ queries: typeof queries;
25
+ validators: typeof validators;
26
+ }> = anyApi as any;
27
+
28
+ /**
29
+ * A utility for referencing Convex functions in your app's public API.
30
+ *
31
+ * Usage:
32
+ * ```js
33
+ * const myFunctionReference = api.myModule.myFunction;
34
+ * ```
35
+ */
36
+ export const api: FilterApi<
37
+ typeof fullApi,
38
+ FunctionReference<any, "public">
39
+ > = anyApi as any;
40
+
41
+ /**
42
+ * A utility for referencing Convex functions in your app's internal API.
43
+ *
44
+ * Usage:
45
+ * ```js
46
+ * const myFunctionReference = internal.myModule.myFunction;
47
+ * ```
48
+ */
49
+ export const internal: FilterApi<
50
+ typeof fullApi,
51
+ FunctionReference<any, "internal">
52
+ > = anyApi as any;
53
+
54
+ export const components = componentsGeneric() as unknown as {};
@@ -0,0 +1,114 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated `ComponentApi` utility.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type { FunctionReference } from "convex/server";
12
+
13
+ /**
14
+ * A utility for referencing a Convex component's exposed API.
15
+ *
16
+ * Useful when expecting a parameter like `components.myComponent`.
17
+ * Usage:
18
+ * ```ts
19
+ * async function myFunction(ctx: QueryCtx, component: ComponentApi) {
20
+ * return ctx.runQuery(component.someFile.someQuery, { ...args });
21
+ * }
22
+ * ```
23
+ */
24
+ export type ComponentApi<Name extends string | undefined = string | undefined> =
25
+ {
26
+ mutations: {
27
+ consume: FunctionReference<
28
+ "mutation",
29
+ "internal",
30
+ {
31
+ amount: number;
32
+ key: string;
33
+ limit: number;
34
+ scope: string;
35
+ subjectRef: string;
36
+ window:
37
+ | {
38
+ kind: "calendar";
39
+ period: "day" | "week" | "month";
40
+ timeZone: string;
41
+ weekStartsOn?: "monday" | "sunday";
42
+ }
43
+ | { durationMs: number; kind: "rolling" }
44
+ | { durationMs: number; kind: "epoch" };
45
+ },
46
+ | {
47
+ allowed: true;
48
+ limit: number;
49
+ periodKey: string;
50
+ remaining: number;
51
+ resetsAt: number;
52
+ used: number;
53
+ }
54
+ | {
55
+ allowed: false;
56
+ limit: number;
57
+ periodKey: string;
58
+ remaining: number;
59
+ resetsAt: number;
60
+ used: number;
61
+ },
62
+ Name
63
+ >;
64
+ eraseSubject: FunctionReference<
65
+ "mutation",
66
+ "internal",
67
+ { batch?: number; scope: string; subjectRef: string },
68
+ number,
69
+ Name
70
+ >;
71
+ refund: FunctionReference<
72
+ "mutation",
73
+ "internal",
74
+ {
75
+ amount: number;
76
+ key: string;
77
+ periodKey: string;
78
+ scope: string;
79
+ subjectRef: string;
80
+ },
81
+ { refunded: boolean; remaining: number; used: number },
82
+ Name
83
+ >;
84
+ };
85
+ queries: {
86
+ remaining: FunctionReference<
87
+ "query",
88
+ "internal",
89
+ {
90
+ key: string;
91
+ limit: number;
92
+ scope: string;
93
+ subjectRef: string;
94
+ window:
95
+ | {
96
+ kind: "calendar";
97
+ period: "day" | "week" | "month";
98
+ timeZone: string;
99
+ weekStartsOn?: "monday" | "sunday";
100
+ }
101
+ | { durationMs: number; kind: "rolling" }
102
+ | { durationMs: number; kind: "epoch" };
103
+ },
104
+ {
105
+ limit: number;
106
+ periodKey: string;
107
+ remaining: number;
108
+ resetsAt: number;
109
+ used: number;
110
+ },
111
+ Name
112
+ >;
113
+ };
114
+ };
@@ -0,0 +1,60 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated data model types.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type {
12
+ DataModelFromSchemaDefinition,
13
+ DocumentByName,
14
+ TableNamesInDataModel,
15
+ SystemTableNames,
16
+ } from "convex/server";
17
+ import type { GenericId } from "convex/values";
18
+ import schema from "../schema.js";
19
+
20
+ /**
21
+ * The names of all of your Convex tables.
22
+ */
23
+ export type TableNames = TableNamesInDataModel<DataModel>;
24
+
25
+ /**
26
+ * The type of a document stored in Convex.
27
+ *
28
+ * @typeParam TableName - A string literal type of the table name (like "users").
29
+ */
30
+ export type Doc<TableName extends TableNames> = DocumentByName<
31
+ DataModel,
32
+ TableName
33
+ >;
34
+
35
+ /**
36
+ * An identifier for a document in Convex.
37
+ *
38
+ * Convex documents are uniquely identified by their `Id`, which is accessible
39
+ * on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
40
+ *
41
+ * Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
42
+ *
43
+ * IDs are just strings at runtime, but this type can be used to distinguish them from other
44
+ * strings when type checking.
45
+ *
46
+ * @typeParam TableName - A string literal type of the table name (like "users").
47
+ */
48
+ export type Id<TableName extends TableNames | SystemTableNames> =
49
+ GenericId<TableName>;
50
+
51
+ /**
52
+ * A type describing your Convex data model.
53
+ *
54
+ * This type includes information about what tables you have, the type of
55
+ * documents stored in those tables, and the indexes defined on them.
56
+ *
57
+ * This type is used to parameterize methods like `queryGeneric` and
58
+ * `mutationGeneric` to make them type-safe.
59
+ */
60
+ export type DataModel = DataModelFromSchemaDefinition<typeof schema>;