@gaganref/confect-alchemy 10.6.0-fork.22 → 10.6.0-fork.24

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 (38) hide show
  1. package/dist/Convex.d.ts +1 -0
  2. package/dist/Convex.d.ts.map +1 -1
  3. package/dist/Convex.js +3 -1
  4. package/dist/Convex.js.map +1 -1
  5. package/dist/ConvexClient.d.ts +39 -0
  6. package/dist/ConvexClient.d.ts.map +1 -1
  7. package/dist/ConvexClient.js +46 -4
  8. package/dist/ConvexClient.js.map +1 -1
  9. package/dist/EnvironmentVariables.d.ts +76 -21
  10. package/dist/EnvironmentVariables.d.ts.map +1 -1
  11. package/dist/EnvironmentVariables.js +188 -12
  12. package/dist/EnvironmentVariables.js.map +1 -1
  13. package/dist/Providers.d.ts.map +1 -1
  14. package/dist/Providers.js +9 -4
  15. package/dist/Providers.js.map +1 -1
  16. package/dist/UsageLimit.d.ts +140 -0
  17. package/dist/UsageLimit.d.ts.map +1 -0
  18. package/dist/UsageLimit.js +145 -0
  19. package/dist/UsageLimit.js.map +1 -0
  20. package/dist/internal/Lifecycle.js +1 -1
  21. package/dist/internal/Lifecycle.js.map +1 -1
  22. package/dist/internal/LocalBackend.d.ts +2 -0
  23. package/dist/internal/LocalBackend.d.ts.map +1 -1
  24. package/dist/internal/LocalBackend.js +2 -0
  25. package/dist/internal/LocalBackend.js.map +1 -1
  26. package/dist/internal/PushLock.d.ts +1 -0
  27. package/dist/internal/PushLock.d.ts.map +1 -1
  28. package/dist/internal/PushLock.js +3 -2
  29. package/dist/internal/PushLock.js.map +1 -1
  30. package/package.json +3 -3
  31. package/src/Convex.ts +6 -0
  32. package/src/ConvexClient.ts +121 -3
  33. package/src/EnvironmentVariables.ts +481 -16
  34. package/src/Providers.ts +6 -0
  35. package/src/UsageLimit.ts +253 -0
  36. package/src/internal/Lifecycle.ts +1 -1
  37. package/src/internal/LocalBackend.ts +2 -0
  38. package/src/internal/PushLock.ts +4 -1
@@ -1,7 +1,21 @@
1
1
  import { OwnedBySomeoneElse, Unowned } from "alchemy/AdoptPolicy";
2
2
  import { isResolved } from "alchemy/Diff";
3
+ import type { Input, InputProps } from "alchemy/Input";
4
+ import * as Output from "alchemy/Output";
3
5
  import * as Provider from "alchemy/Provider";
4
- import { Resource } from "alchemy/Resource";
6
+ import {
7
+ Resource,
8
+ type ResourceClass,
9
+ type ResourceConstructor,
10
+ } from "alchemy/Resource";
11
+ import * as Effectable from "effect/Effectable";
12
+ import * as Config from "effect/Config";
13
+ import * as Result from "effect/Result";
14
+ import * as Duration from "effect/Duration";
15
+ import * as Option from "effect/Option";
16
+ import * as Predicate from "effect/Predicate";
17
+ import * as Schedule from "effect/Schedule";
18
+ import { PushLock, PushLockError } from "./internal/PushLock.js";
5
19
  import * as Effect from "effect/Effect";
6
20
  import * as Layer from "effect/Layer";
7
21
  import * as FetchHttpClient from "effect/http/FetchHttpClient";
@@ -15,6 +29,32 @@ import * as Arr from "effect/Array";
15
29
  import * as Rec from "effect/Record";
16
30
 
17
31
  export interface EnvironmentVariablesProps {
32
+ /**
33
+ * Refuse live owned values that differ from both the last write and the
34
+ * desired value. Defaults to `overwrite`.
35
+ */
36
+ readonly driftPolicy?: "overwrite" | "error";
37
+ /**
38
+ * Adopt unowned desired names only when their live values match. Defaults to
39
+ * false.
40
+ */
41
+ readonly adoptMatching?: boolean;
42
+ /**
43
+ * Refuse live names that are neither owned nor desired. Defaults to `ignore`.
44
+ */
45
+ readonly unownedPolicy?: "ignore" | "error";
46
+ /**
47
+ * Share the deployment push lock for checks and writes. Absent by default.
48
+ */
49
+ readonly lock?: { readonly deploymentName: string };
50
+ /**
51
+ * Retry write conflicts only, with 1 to 10 attempts and a delay of 1 to 10000
52
+ * ms. Absent means one attempt.
53
+ */
54
+ readonly retry?: {
55
+ readonly maxAttempts: number;
56
+ readonly delay: Duration.Input;
57
+ };
18
58
  readonly url: string;
19
59
  readonly deployKey: Redacted.Redacted<string>;
20
60
  readonly variables: Readonly<
@@ -37,17 +77,277 @@ export type EnvironmentVariables = Resource<
37
77
  never,
38
78
  Providers
39
79
  >;
40
- export const EnvironmentVariables = Resource<EnvironmentVariables>(
80
+ const environmentVariables = Resource<EnvironmentVariables>(
41
81
  "Convex.EnvironmentVariables",
42
82
  );
43
83
 
84
+ // Alchemy skips provider reads for fresh resources with unresolved inputs.
85
+ export const EnvironmentVariables: ResourceClass<EnvironmentVariables> =
86
+ Object.assign(
87
+ new Proxy(environmentVariables, {
88
+ apply: <PropsReq>(
89
+ target: ResourceClass<EnvironmentVariables>,
90
+ _this: unknown,
91
+ args:
92
+ | [methods: object]
93
+ | [
94
+ id: string,
95
+ props:
96
+ | InputProps<EnvironmentVariablesProps>
97
+ | Effect.Effect<
98
+ InputProps<EnvironmentVariablesProps>,
99
+ never,
100
+ PropsReq
101
+ >,
102
+ ],
103
+ ) => {
104
+ if (args.length === 1)
105
+ return Object.assign(EnvironmentVariables, args[0]);
106
+ const [id, props] = args;
107
+ return target(
108
+ id,
109
+ (Effect.isEffect(props) ? props : Effect.succeed(props)).pipe(
110
+ Effect.flatMap((options) =>
111
+ Effect.fromResult(validateSafety(options)).pipe(
112
+ Effect.orDie,
113
+ Effect.as({
114
+ ...options,
115
+ ...(options.retry === undefined
116
+ ? {}
117
+ : { retry: checkedRetry(options.retry) }),
118
+ ...(options.lock === undefined
119
+ ? {}
120
+ : { lock: checkedLock(options.lock) }),
121
+ }),
122
+ ),
123
+ ),
124
+ ),
125
+ );
126
+ },
127
+ }),
128
+ Effectable.Prototype<
129
+ Effect.Effect<ResourceConstructor<EnvironmentVariables>>
130
+ >({
131
+ label: "Resource<Convex.EnvironmentVariables>",
132
+ evaluate: () => Effect.succeed(EnvironmentVariables),
133
+ }),
134
+ );
135
+
44
136
  export class EnvironmentTargetChange extends Schema.TaggedError<
45
137
  EnvironmentTargetChange,
46
138
  { readonly brand: unique symbol }
47
139
  >()("EnvironmentTargetChange", { url: Schema.String }) {}
48
140
 
141
+ export class EnvironmentVariableDrift extends Schema.TaggedError<
142
+ EnvironmentVariableDrift,
143
+ { readonly brand: unique symbol }
144
+ >()("EnvironmentVariableDrift", { variableName: Schema.String }) {
145
+ override get message() {
146
+ return `Environment variable ${this.variableName} differs from its last managed value`;
147
+ }
148
+ }
149
+
150
+ export class EnvironmentVariableUnowned extends Schema.TaggedError<
151
+ EnvironmentVariableUnowned,
152
+ { readonly brand: unique symbol }
153
+ >()("EnvironmentVariableUnowned", { variableName: Schema.String }) {
154
+ override get message() {
155
+ return `Environment variable ${this.variableName} is neither owned nor desired`;
156
+ }
157
+ }
158
+
159
+ export class EnvironmentVariablesInvalidOption extends Schema.TaggedError<
160
+ EnvironmentVariablesInvalidOption,
161
+ { readonly brand: unique symbol }
162
+ >()("EnvironmentVariablesInvalidOption", {
163
+ option: Schema.Literals(["maxAttempts", "delay", "deploymentName"]),
164
+ }) {
165
+ override get message() {
166
+ switch (this.option) {
167
+ case "maxAttempts":
168
+ return "Environment retry maxAttempts must be an integer from 1 to 10";
169
+ case "delay":
170
+ return "Environment retry delay must be from 1 to 10000 milliseconds";
171
+ case "deploymentName":
172
+ return "Environment lock deploymentName must contain lowercase letters or digits separated by single hyphens";
173
+ }
174
+ }
175
+ }
176
+
177
+ type SafetyOptions = {
178
+ readonly retry?: EnvironmentVariablesProps["retry"];
179
+ readonly lock?: EnvironmentVariablesProps["lock"];
180
+ };
181
+
182
+ const validateMaxAttempts = (
183
+ maxAttempts: number,
184
+ ): Result.Result<void, EnvironmentVariablesInvalidOption> =>
185
+ Number.isInteger(maxAttempts) && maxAttempts >= 1 && maxAttempts <= 10
186
+ ? Result.succeed(undefined)
187
+ : Result.fail(
188
+ EnvironmentVariablesInvalidOption.make({ option: "maxAttempts" }),
189
+ );
190
+
191
+ const validateDelay = (
192
+ delay: Duration.Input,
193
+ ): Result.Result<Duration.Duration, EnvironmentVariablesInvalidOption> => {
194
+ const parsed = Duration.fromInput(delay);
195
+ return Option.isSome(parsed) &&
196
+ Duration.toMillis(parsed.value) >= 1 &&
197
+ Duration.toMillis(parsed.value) <= 10000
198
+ ? Result.succeed(parsed.value)
199
+ : Result.fail(EnvironmentVariablesInvalidOption.make({ option: "delay" }));
200
+ };
201
+
202
+ const validateDeploymentName = (
203
+ deploymentName: string,
204
+ ): Result.Result<void, EnvironmentVariablesInvalidOption> =>
205
+ /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(deploymentName)
206
+ ? Result.succeed(undefined)
207
+ : Result.fail(
208
+ EnvironmentVariablesInvalidOption.make({ option: "deploymentName" }),
209
+ );
210
+
211
+ const validateSafety = (
212
+ options: Input<SafetyOptions>,
213
+ ): Result.Result<
214
+ Duration.Duration | undefined,
215
+ EnvironmentVariablesInvalidOption
216
+ > =>
217
+ Result.gen(function* () {
218
+ let duration: Duration.Duration | undefined;
219
+ if (
220
+ "retry" in options &&
221
+ options.retry !== undefined &&
222
+ "maxAttempts" in options.retry &&
223
+ "delay" in options.retry
224
+ ) {
225
+ if (isResolved(options.retry.maxAttempts))
226
+ yield* validateMaxAttempts(options.retry.maxAttempts);
227
+ if (isResolved(options.retry.delay))
228
+ duration = yield* validateDelay(options.retry.delay);
229
+ }
230
+ if (
231
+ "lock" in options &&
232
+ options.lock !== undefined &&
233
+ "deploymentName" in options.lock &&
234
+ isResolved(options.lock.deploymentName)
235
+ )
236
+ yield* validateDeploymentName(options.lock.deploymentName);
237
+ return duration;
238
+ });
239
+
240
+ const checkedInput = <A>(
241
+ input: Exclude<Input<A>, undefined>,
242
+ validate: (
243
+ value: A,
244
+ ) => Result.Result<unknown, EnvironmentVariablesInvalidOption>,
245
+ ): Exclude<Input<A>, undefined> => {
246
+ const check = (value: A) =>
247
+ Effect.fromResult(validate(value)).pipe(Effect.orDie, Effect.as(value));
248
+ if (Config.isConfig(input)) return input.pipe(Config.mapEffect(check));
249
+ return input;
250
+ };
251
+
252
+ const checkedRetry = (
253
+ input: Exclude<Input<SafetyOptions["retry"]>, undefined>,
254
+ ): Exclude<Input<SafetyOptions["retry"]>, undefined> => {
255
+ if ("maxAttempts" in input && "delay" in input)
256
+ return {
257
+ ...input,
258
+ maxAttempts: checkedInput(input.maxAttempts, validateMaxAttempts),
259
+ delay: checkedInput(input.delay, validateDelay),
260
+ };
261
+ return checkedInput(input, (retry) => validateSafety({ retry }));
262
+ };
263
+
264
+ const checkedLock = (
265
+ input: Exclude<Input<SafetyOptions["lock"]>, undefined>,
266
+ ): Exclude<Input<SafetyOptions["lock"]>, undefined> => {
267
+ if ("deploymentName" in input)
268
+ return {
269
+ ...input,
270
+ deploymentName: checkedInput(
271
+ input.deploymentName,
272
+ validateDeploymentName,
273
+ ),
274
+ };
275
+ return checkedInput(input, (lock) => validateSafety({ lock }));
276
+ };
277
+
278
+ const ownershipConflict = (variableName: string) =>
279
+ new OwnedBySomeoneElse({
280
+ message:
281
+ "An existing environment variable requires explicit adoption in a separate resource.",
282
+ resourceType: EnvironmentVariables.Type,
283
+ physicalName: variableName,
284
+ });
285
+
286
+ const optionsForDiff = (
287
+ olds: EnvironmentVariablesProps,
288
+ news: Input<EnvironmentVariablesProps>,
289
+ ) => {
290
+ if (!("variables" in news))
291
+ return { ...olds, desiredNames: undefined, desiredVariables: undefined };
292
+ return {
293
+ driftPolicy: isResolved(news.driftPolicy)
294
+ ? news.driftPolicy
295
+ : olds.driftPolicy,
296
+ adoptMatching: isResolved(news.adoptMatching)
297
+ ? news.adoptMatching
298
+ : olds.adoptMatching,
299
+ unownedPolicy: isResolved(news.unownedPolicy)
300
+ ? news.unownedPolicy
301
+ : olds.unownedPolicy,
302
+ lock: isResolved(news.lock) ? news.lock : olds.lock,
303
+ retry: isResolved(news.retry) ? news.retry : olds.retry,
304
+ desiredVariables: news.variables,
305
+ desiredNames:
306
+ !Output.isExpr(news.variables) &&
307
+ !Effect.isEffect(news.variables) &&
308
+ Predicate.isReadonlyObject(news.variables)
309
+ ? Rec.keys<string, unknown>(news.variables)
310
+ : undefined,
311
+ };
312
+ };
313
+
49
314
  export const provider = Effect.gen(function* () {
50
315
  const client = yield* ConvexClient.ConvexClient;
316
+ const lock = yield* Effect.serviceOption(PushLock);
317
+ const withSafety = Effect.fn("EnvironmentVariables.withSafety")(function* <
318
+ A,
319
+ E,
320
+ R,
321
+ >(
322
+ options: {
323
+ readonly lock?: EnvironmentVariablesProps["lock"];
324
+ readonly retry?: EnvironmentVariablesProps["retry"];
325
+ },
326
+ effect: Effect.Effect<A, E, R>,
327
+ ) {
328
+ const duration = yield* Effect.fromResult(validateSafety(options));
329
+ let attempt = effect;
330
+ if (options.retry !== undefined && duration !== undefined) {
331
+ const { maxAttempts } = options.retry;
332
+ attempt = effect.pipe(
333
+ Effect.retry({
334
+ times: maxAttempts - 1,
335
+ schedule: Schedule.spaced(duration),
336
+ while: (error) =>
337
+ Schema.is(ConvexClient.ConvexApiError)(error) &&
338
+ error.serverCode === "OptimisticConcurrencyControlFailure",
339
+ }),
340
+ );
341
+ }
342
+ if (options.lock === undefined) return yield* attempt;
343
+ if (Option.isNone(lock))
344
+ return yield* PushLockError.make({
345
+ deploymentName: options.lock.deploymentName,
346
+ reason: "configuration",
347
+ missing: "PushLock",
348
+ });
349
+ return yield* lock.value.withLock(options.lock.deploymentName, attempt);
350
+ });
51
351
  const redacted = (value: string | Redacted.Redacted<string>) =>
52
352
  typeof value === "string" ? Redacted.make(value) : value;
53
353
  const valueAt = (
@@ -63,8 +363,116 @@ export const provider = Effect.gen(function* () {
63
363
  !Rec.has(variables, name) ? [] : [[name, variables[name]]],
64
364
  ),
65
365
  );
66
- return {
67
- diff: Effect.fn("EnvironmentVariables.diff")(function* ({ news, output }) {
366
+ const checkDrift = (
367
+ policy: EnvironmentVariablesProps["driftPolicy"],
368
+ output: EnvironmentVariablesAttributes | undefined,
369
+ observed: ConvexClient.EnvironmentVariables,
370
+ desired: Input<EnvironmentVariablesProps["variables"]> | undefined,
371
+ ) => {
372
+ if (policy !== "error" || output === undefined) return Effect.void;
373
+ const desiredVariables =
374
+ Predicate.isReadonlyObject(desired) &&
375
+ !Output.isExpr(desired) &&
376
+ !Effect.isEffect(desired)
377
+ ? desired
378
+ : undefined;
379
+ for (const name of output.ownedNames) {
380
+ const previous = valueAt(output.variables, name);
381
+ const current = valueAt(observed, name);
382
+ const wanted = desiredVariables?.[name];
383
+ if (
384
+ current !== undefined &&
385
+ (desiredVariables === undefined ||
386
+ (wanted !== undefined && !isResolved(wanted)))
387
+ )
388
+ continue;
389
+ if (
390
+ current !== undefined &&
391
+ (typeof wanted === "string" || Redacted.isRedacted(wanted)) &&
392
+ Redacted.value(current) ===
393
+ (typeof wanted === "string" ? wanted : Redacted.value(wanted))
394
+ )
395
+ continue;
396
+ if (previous === undefined && current === undefined) continue;
397
+ if (
398
+ previous === undefined ||
399
+ current === undefined ||
400
+ Redacted.value(previous) !== Redacted.value(current)
401
+ )
402
+ return Effect.fail(
403
+ EnvironmentVariableDrift.make({ variableName: name }),
404
+ );
405
+ }
406
+ return Effect.void;
407
+ };
408
+ const checkUnowned = (
409
+ policy: EnvironmentVariablesProps["unownedPolicy"],
410
+ output: EnvironmentVariablesAttributes | undefined,
411
+ desiredNames: ReadonlyArray<string> | undefined,
412
+ observed: ConvexClient.EnvironmentVariables,
413
+ ) => {
414
+ if (policy !== "error" || desiredNames === undefined) return Effect.void;
415
+ for (const name of Rec.keys(observed)) {
416
+ if (
417
+ output?.ownedNames.includes(name) !== true &&
418
+ !desiredNames.includes(name)
419
+ )
420
+ return Effect.fail(
421
+ EnvironmentVariableUnowned.make({ variableName: name }),
422
+ );
423
+ }
424
+ return Effect.void;
425
+ };
426
+ const service = {
427
+ diff: Effect.fn("EnvironmentVariables.diff")(function* ({
428
+ olds,
429
+ news,
430
+ output,
431
+ }) {
432
+ const options = optionsForDiff(olds, news);
433
+ if (
434
+ output !== undefined &&
435
+ (options.driftPolicy === "error" ||
436
+ options.unownedPolicy === "error" ||
437
+ options.adoptMatching === true)
438
+ ) {
439
+ const observed = yield* client.listEnvironmentVariables(
440
+ output.url,
441
+ output.deployKey,
442
+ );
443
+ yield* checkDrift(
444
+ options.driftPolicy,
445
+ output,
446
+ observed,
447
+ options.desiredVariables,
448
+ );
449
+ if (
450
+ options.adoptMatching === true &&
451
+ Predicate.isReadonlyObject(options.desiredVariables) &&
452
+ !Output.isExpr(options.desiredVariables) &&
453
+ !Effect.isEffect(options.desiredVariables)
454
+ ) {
455
+ for (const [name, wanted] of Rec.toEntries(
456
+ options.desiredVariables,
457
+ )) {
458
+ const current = valueAt(observed, name);
459
+ if (
460
+ !output.ownedNames.includes(name) &&
461
+ current !== undefined &&
462
+ (typeof wanted === "string" || Redacted.isRedacted(wanted)) &&
463
+ Redacted.value(current) !==
464
+ (typeof wanted === "string" ? wanted : Redacted.value(wanted))
465
+ )
466
+ return yield* ownershipConflict(name);
467
+ }
468
+ }
469
+ yield* checkUnowned(
470
+ options.unownedPolicy,
471
+ output,
472
+ options.desiredNames,
473
+ observed,
474
+ );
475
+ }
68
476
  if (!isResolved(news) || output === undefined) return;
69
477
  if (news.url !== output.url)
70
478
  return yield* EnvironmentTargetChange.make({
@@ -96,11 +504,28 @@ export const provider = Effect.gen(function* () {
96
504
  url,
97
505
  olds.deployKey,
98
506
  );
507
+ yield* checkDrift(olds.driftPolicy, output, observed, olds.variables);
508
+ yield* checkUnowned(
509
+ olds.unownedPolicy,
510
+ output,
511
+ Rec.keys(olds.variables),
512
+ observed,
513
+ );
99
514
  const ownedNames = output?.ownedNames ?? Rec.keys(olds.variables);
100
515
  const variables = select(observed, ownedNames);
101
516
  if (output !== undefined)
102
517
  return { ...output, deployKey: olds.deployKey, variables };
103
518
  if (Rec.keys(variables).length === 0) return undefined;
519
+ if (olds.adoptMatching === true) {
520
+ for (const [name, value] of Rec.toEntries(variables)) {
521
+ if (
522
+ Redacted.value(value) !==
523
+ Redacted.value(redacted(olds.variables[name]))
524
+ )
525
+ return yield* ownershipConflict(name);
526
+ }
527
+ return undefined;
528
+ }
104
529
  return Unowned({
105
530
  url,
106
531
  deployKey: olds.deployKey,
@@ -124,8 +549,19 @@ export const provider = Effect.gen(function* () {
124
549
  news.url,
125
550
  news.deployKey,
126
551
  );
552
+ yield* checkDrift(news.driftPolicy, output, observed, news.variables);
553
+ yield* checkUnowned(
554
+ news.unownedPolicy,
555
+ output,
556
+ Rec.keys(news.variables),
557
+ observed,
558
+ );
127
559
  const ownedNames = Rec.keys(news.variables);
128
- if (output === undefined && (yield* shouldAdopt(fqn))) {
560
+ if (
561
+ output === undefined &&
562
+ news.adoptMatching !== true &&
563
+ (yield* shouldAdopt(fqn))
564
+ ) {
129
565
  const variables = select(observed, ownedNames);
130
566
  output = {
131
567
  url: news.url,
@@ -137,17 +573,29 @@ export const provider = Effect.gen(function* () {
137
573
  yield* checkpointCreate(fqn, instanceId, news, output);
138
574
  }
139
575
  const previousNames = output?.ownedNames ?? [];
576
+ let originals = output?.originals ?? {};
140
577
  for (const name of ownedNames) {
578
+ const current = valueAt(observed, name);
579
+ if (previousNames.includes(name) || current === undefined) continue;
141
580
  if (
142
- !previousNames.includes(name) &&
143
- valueAt(observed, name) !== undefined
144
- )
145
- return yield* new OwnedBySomeoneElse({
146
- message:
147
- "An existing environment variable requires explicit adoption in a separate resource.",
148
- resourceType: EnvironmentVariables.Type,
149
- physicalName: name,
150
- });
581
+ news.adoptMatching === true &&
582
+ Redacted.value(current) ===
583
+ Redacted.value(redacted(news.variables[name]))
584
+ ) {
585
+ originals = Rec.set(originals, name, current);
586
+ yield* Effect.logInfo(`Adopted environment variable ${name}`);
587
+ } else {
588
+ return yield* ownershipConflict(name);
589
+ }
590
+ }
591
+ if (output === undefined && news.adoptMatching === true) {
592
+ yield* checkpointCreate(fqn, instanceId, news, {
593
+ url: news.url,
594
+ deployKey: news.deployKey,
595
+ ownedNames,
596
+ variables: select(observed, ownedNames),
597
+ originals,
598
+ } satisfies EnvironmentVariablesAttributes);
151
599
  }
152
600
  const desired = Rec.fromEntries(
153
601
  Arr.map(Rec.toEntries(news.variables), ([name, value]) => [
@@ -191,14 +639,24 @@ export const provider = Effect.gen(function* () {
191
639
  deployKey: news.deployKey,
192
640
  ownedNames,
193
641
  variables: desired,
194
- originals: select(output?.originals ?? {}, ownedNames),
642
+ originals: select(originals, ownedNames),
195
643
  };
196
644
  }),
197
- delete: Effect.fn("EnvironmentVariables.delete")(function* ({ output }) {
645
+ delete: Effect.fn("EnvironmentVariables.delete")(function* ({
646
+ olds,
647
+ output,
648
+ }) {
198
649
  const observed = yield* client
199
650
  .listEnvironmentVariables(output.url, output.deployKey)
200
651
  .pipe(Effect.catchTag("ConvexNotFound", () => Effect.void));
201
652
  if (observed === undefined) return;
653
+ yield* checkDrift(olds.driftPolicy, output, observed, {});
654
+ yield* checkUnowned(
655
+ olds.unownedPolicy,
656
+ output,
657
+ Rec.keys(olds.variables),
658
+ observed,
659
+ );
202
660
  const changes: Array<{
203
661
  name: string;
204
662
  value?: Redacted.Redacted<string>;
@@ -220,6 +678,13 @@ export const provider = Effect.gen(function* () {
220
678
  .pipe(Effect.catchTag("ConvexNotFound", () => Effect.void));
221
679
  }),
222
680
  } satisfies Provider.ProviderServiceInput<EnvironmentVariables>;
681
+ return {
682
+ diff: (input) =>
683
+ withSafety(optionsForDiff(input.olds, input.news), service.diff(input)),
684
+ read: (input) => withSafety(input.olds, service.read(input)),
685
+ reconcile: (input) => withSafety(input.news, service.reconcile(input)),
686
+ delete: (input) => withSafety(input.olds, service.delete(input)),
687
+ } satisfies Provider.ProviderServiceInput<EnvironmentVariables>;
223
688
  });
224
689
 
225
690
  /**
package/src/Providers.ts CHANGED
@@ -9,6 +9,7 @@ import * as Deployment from "./Deployment.js";
9
9
  import * as DeployKey from "./DeployKey.js";
10
10
  import * as EnvironmentVariables from "./EnvironmentVariables.js";
11
11
  import * as Project from "./Project.js";
12
+ import * as UsageLimit from "./UsageLimit.js";
12
13
  import * as CommandRunner from "./internal/CommandRunner.js";
13
14
  import * as PushLock from "./internal/PushLock.js";
14
15
 
@@ -35,6 +36,7 @@ export const providers = (options?: {
35
36
  DeployKey.DeployKey,
36
37
  EnvironmentVariables.EnvironmentVariables,
37
38
  Code.Code,
39
+ UsageLimit.UsageLimit,
38
40
  ]),
39
41
  ).pipe(
40
42
  Layer.provide(
@@ -55,6 +57,10 @@ export const providers = (options?: {
55
57
  live: () => EnvironmentVariables.layer,
56
58
  local: () => EnvironmentVariables.localLayer,
57
59
  }),
60
+ ProviderLayer.dual(UsageLimit.UsageLimit, {
61
+ live: () => UsageLimit.layer,
62
+ local: () => UsageLimit.localLayer,
63
+ }),
58
64
  ProviderLayer.dual(Code.Code, {
59
65
  live: () => Code.layer,
60
66
  local: () => Code.localLayer,