@gaganref/confect-alchemy 10.6.0-fork.3 → 10.6.0-fork.31

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 (100) hide show
  1. package/dist/Backend.d.ts +64 -5
  2. package/dist/Backend.d.ts.map +1 -1
  3. package/dist/Backend.js +187 -3
  4. package/dist/Backend.js.map +1 -1
  5. package/dist/Code.d.ts +14 -2
  6. package/dist/Code.d.ts.map +1 -1
  7. package/dist/Code.js +2 -2
  8. package/dist/Code.js.map +1 -1
  9. package/dist/Confect.d.ts +1 -1
  10. package/dist/Confect.d.ts.map +1 -1
  11. package/dist/Confect.js +8 -3
  12. package/dist/Confect.js.map +1 -1
  13. package/dist/Convex.d.ts +4 -1
  14. package/dist/Convex.d.ts.map +1 -1
  15. package/dist/Convex.js +10 -1
  16. package/dist/Convex.js.map +1 -1
  17. package/dist/ConvexClient.d.ts +92 -3
  18. package/dist/ConvexClient.d.ts.map +1 -1
  19. package/dist/ConvexClient.js +149 -27
  20. package/dist/ConvexClient.js.map +1 -1
  21. package/dist/CustomDomain.d.ts +156 -0
  22. package/dist/CustomDomain.d.ts.map +1 -0
  23. package/dist/CustomDomain.js +255 -0
  24. package/dist/CustomDomain.js.map +1 -0
  25. package/dist/DeployKey.js +3 -3
  26. package/dist/Deployment.d.ts +29 -3
  27. package/dist/Deployment.d.ts.map +1 -1
  28. package/dist/Deployment.js +39 -15
  29. package/dist/Deployment.js.map +1 -1
  30. package/dist/EnvironmentVariables.d.ts +76 -21
  31. package/dist/EnvironmentVariables.d.ts.map +1 -1
  32. package/dist/EnvironmentVariables.js +203 -19
  33. package/dist/EnvironmentVariables.js.map +1 -1
  34. package/dist/LogStream.d.ts +134 -0
  35. package/dist/LogStream.d.ts.map +1 -0
  36. package/dist/LogStream.js +324 -0
  37. package/dist/LogStream.js.map +1 -0
  38. package/dist/Project.js +2 -2
  39. package/dist/Providers.d.ts.map +1 -1
  40. package/dist/Providers.js +30 -15
  41. package/dist/Providers.js.map +1 -1
  42. package/dist/UsageLimit.d.ts +140 -0
  43. package/dist/UsageLimit.d.ts.map +1 -0
  44. package/dist/UsageLimit.js +145 -0
  45. package/dist/UsageLimit.js.map +1 -0
  46. package/dist/internal/CodeDeployment.d.ts +28 -1
  47. package/dist/internal/CodeDeployment.d.ts.map +1 -1
  48. package/dist/internal/CodeDeployment.js +85 -2
  49. package/dist/internal/CodeDeployment.js.map +1 -1
  50. package/dist/internal/CommandRunner.d.ts +4 -0
  51. package/dist/internal/CommandRunner.d.ts.map +1 -1
  52. package/dist/internal/CommandRunner.js +92 -6
  53. package/dist/internal/CommandRunner.js.map +1 -1
  54. package/dist/internal/FileLock.js +1 -1
  55. package/dist/internal/FunctionsDirectory.d.ts +12 -0
  56. package/dist/internal/FunctionsDirectory.d.ts.map +1 -0
  57. package/dist/internal/FunctionsDirectory.js +22 -0
  58. package/dist/internal/FunctionsDirectory.js.map +1 -0
  59. package/dist/internal/Lifecycle.d.ts +1 -0
  60. package/dist/internal/Lifecycle.d.ts.map +1 -1
  61. package/dist/internal/Lifecycle.js +24 -2
  62. package/dist/internal/Lifecycle.js.map +1 -1
  63. package/dist/internal/LocalBackend.d.ts +9 -0
  64. package/dist/internal/LocalBackend.d.ts.map +1 -1
  65. package/dist/internal/LocalBackend.js +14 -5
  66. package/dist/internal/LocalBackend.js.map +1 -1
  67. package/dist/internal/LogStream.d.ts +115 -0
  68. package/dist/internal/LogStream.d.ts.map +1 -0
  69. package/dist/internal/LogStream.js +123 -0
  70. package/dist/internal/LogStream.js.map +1 -0
  71. package/dist/internal/MountInit.d.ts +4 -5
  72. package/dist/internal/MountInit.d.ts.map +1 -1
  73. package/dist/internal/MountInit.js +17 -36
  74. package/dist/internal/MountInit.js.map +1 -1
  75. package/dist/internal/PushLock.d.ts +1 -0
  76. package/dist/internal/PushLock.d.ts.map +1 -1
  77. package/dist/internal/PushLock.js +4 -3
  78. package/dist/internal/PushLock.js.map +1 -1
  79. package/dist/internal/ResourceIdentity.js +1 -1
  80. package/dist/internal/Zip.js +1 -1
  81. package/package.json +4 -4
  82. package/src/Backend.ts +422 -12
  83. package/src/Code.ts +13 -0
  84. package/src/Confect.ts +7 -1
  85. package/src/Convex.ts +24 -1
  86. package/src/ConvexClient.ts +364 -31
  87. package/src/CustomDomain.ts +453 -0
  88. package/src/Deployment.ts +74 -12
  89. package/src/EnvironmentVariables.ts +505 -19
  90. package/src/LogStream.ts +662 -0
  91. package/src/Providers.ts +18 -0
  92. package/src/UsageLimit.ts +253 -0
  93. package/src/internal/CodeDeployment.ts +115 -0
  94. package/src/internal/CommandRunner.ts +182 -1
  95. package/src/internal/FunctionsDirectory.ts +27 -0
  96. package/src/internal/Lifecycle.ts +21 -1
  97. package/src/internal/LocalBackend.ts +9 -0
  98. package/src/internal/LogStream.ts +154 -0
  99. package/src/internal/MountInit.ts +24 -65
  100. package/src/internal/PushLock.ts +4 -1
@@ -0,0 +1,662 @@
1
+ import { deepEqual, isResolved } from "alchemy/Diff";
2
+ import type { InputProps } from "alchemy/Input";
3
+ import * as Output from "alchemy/Output";
4
+ import * as Array from "effect/Array";
5
+ import * as Provider from "alchemy/Provider";
6
+ import {
7
+ Resource,
8
+ type ResourceClass,
9
+ type ResourceConstructor,
10
+ } from "alchemy/Resource";
11
+ import { Stack } from "alchemy/Stack";
12
+ import { State } from "alchemy/State/State";
13
+ import * as Effect from "effect/Effect";
14
+ import * as Effectable from "effect/Effectable";
15
+ import * as Layer from "effect/Layer";
16
+ import * as Option from "effect/Option";
17
+ import * as Redacted from "effect/Redacted";
18
+ import * as Record from "effect/Record";
19
+ import * as Result from "effect/Result";
20
+ import * as Schema from "effect/Schema";
21
+ import * as FetchHttpClient from "effect/http/FetchHttpClient";
22
+
23
+ import * as ConvexClient from "./ConvexClient.js";
24
+ import type { Providers } from "./Providers.js";
25
+ import { checkpointCreate } from "./internal/Lifecycle.js";
26
+ import { Topics, type Config as LiveConfig } from "./internal/LogStream.js";
27
+
28
+ type Config<A = ConvexClient.CreateLogStreamInput> = A extends {
29
+ readonly logStreamType: infer K;
30
+ }
31
+ ? { readonly kind: K } & Omit<A, "logStreamType">
32
+ : never;
33
+ type Keys<A> = A extends unknown ? keyof A : never;
34
+ type StrictUnion<A, All = A> = A extends unknown
35
+ ? A & { readonly [K in Exclude<Keys<All>, keyof A>]?: never }
36
+ : never;
37
+ type NonEmptySelection<A> = A extends readonly (infer T)[]
38
+ ? readonly [T, ...T[]]
39
+ : A;
40
+ type NonEmptyTopics<A> = {
41
+ readonly [K in keyof A]: K extends "topics" ? NonEmptySelection<A[K]> : A[K];
42
+ };
43
+
44
+ export type LogStreamProps = StrictUnion<
45
+ NonEmptyTopics<
46
+ Extract<Config, { readonly kind: "axiom" | "sentry" | "webhook" }>
47
+ >
48
+ > & {
49
+ readonly deploymentUrl: string;
50
+ readonly deployKey: Redacted.Redacted<string>;
51
+ };
52
+
53
+ export interface LogStreamAttributes {
54
+ readonly id: string;
55
+ readonly kind: LogStreamProps["kind"];
56
+ readonly hmacSecret?: Redacted.Redacted<string>;
57
+ }
58
+
59
+ export type LogStream = Resource<
60
+ "Convex.LogStream",
61
+ LogStreamProps,
62
+ LogStreamAttributes,
63
+ never,
64
+ Providers
65
+ >;
66
+ const logStream = Resource<LogStream>("Convex.LogStream");
67
+
68
+ // Validate topics and computed URLs before Alchemy checkpoints props.
69
+ export const LogStream: ResourceClass<LogStream> = Object.assign(
70
+ new Proxy(logStream, {
71
+ apply: <PropsReq>(
72
+ target: ResourceClass<LogStream>,
73
+ _this: unknown,
74
+ args:
75
+ | [methods: object]
76
+ | [
77
+ id: string,
78
+ props:
79
+ | InputProps<LogStreamProps>
80
+ | Effect.Effect<InputProps<LogStreamProps>, never, PropsReq>,
81
+ ],
82
+ ) => {
83
+ if (args.length === 1) return Object.assign(LogStream, args[0]);
84
+ const [id, props] = args;
85
+ return target(
86
+ id,
87
+ (Effect.isEffect(props) ? props : Effect.succeed(props)).pipe(
88
+ Effect.flatMap(checkedProps),
89
+ ),
90
+ );
91
+ },
92
+ }),
93
+ Effectable.Prototype<Effect.Effect<ResourceConstructor<LogStream>>>({
94
+ label: "Resource<Convex.LogStream>",
95
+ evaluate: () => Effect.succeed(LogStream),
96
+ }),
97
+ );
98
+
99
+ export class LogStreamNotEnabled extends Schema.TaggedError<
100
+ LogStreamNotEnabled,
101
+ { readonly brand: unique symbol }
102
+ >()("LogStreamNotEnabled", {
103
+ kind: Schema.String,
104
+ message: Schema.String,
105
+ }) {}
106
+
107
+ export class LogStreamRecoveryRequired extends Schema.TaggedError<
108
+ LogStreamRecoveryRequired,
109
+ { readonly brand: unique symbol }
110
+ >()("LogStreamRecoveryRequired", {
111
+ kind: Schema.String,
112
+ ids: Schema.Array(Schema.String),
113
+ message: Schema.String,
114
+ }) {}
115
+
116
+ export class LogStreamDrift extends Schema.TaggedError<
117
+ LogStreamDrift,
118
+ { readonly brand: unique symbol }
119
+ >()("LogStreamDrift", {
120
+ id: Schema.String,
121
+ kind: Schema.String,
122
+ message: Schema.String,
123
+ }) {}
124
+
125
+ export class LogStreamInvalidUrl extends Schema.TaggedError<
126
+ LogStreamInvalidUrl,
127
+ { readonly brand: unique symbol }
128
+ >()("LogStreamInvalidUrl", { message: Schema.String }) {}
129
+
130
+ export class LogStreamUrlRequiresRedacted extends Schema.TaggedError<
131
+ LogStreamUrlRequiresRedacted,
132
+ { readonly brand: unique symbol }
133
+ >()("LogStreamUrlRequiresRedacted", { message: Schema.String }) {}
134
+
135
+ export class LogStreamEmptyTopics extends Schema.TaggedError<
136
+ LogStreamEmptyTopics,
137
+ { readonly brand: unique symbol }
138
+ >()("LogStreamEmptyTopics", {
139
+ kind: Schema.String,
140
+ message: Schema.String,
141
+ }) {}
142
+
143
+ const validateTopics = (kind: string, topics: unknown) =>
144
+ Array.isArray(topics) && topics.length === 0
145
+ ? Result.fail(
146
+ LogStreamEmptyTopics.make({
147
+ kind,
148
+ message:
149
+ "Select at least one log topic or omit topics to subscribe to all topics.",
150
+ }),
151
+ )
152
+ : Result.succeed(undefined);
153
+
154
+ const parseUrl = (value: string) =>
155
+ Result.try({
156
+ try: () => new URL(value),
157
+ catch: () =>
158
+ LogStreamInvalidUrl.make({
159
+ message:
160
+ "The log stream URL could not be parsed. Supply an absolute URL.",
161
+ }),
162
+ });
163
+
164
+ const normalizeUrl = (value: string) =>
165
+ parseUrl(value).pipe(Result.map((url) => url.href));
166
+
167
+ const validateWebhookUrl = (value: string | Redacted.Redacted<string>) =>
168
+ parseUrl(Redacted.isRedacted(value) ? Redacted.value(value) : value).pipe(
169
+ Result.flatMap((url) =>
170
+ !Redacted.isRedacted(value) &&
171
+ (url.username !== "" || url.password !== "")
172
+ ? Result.fail(
173
+ LogStreamUrlRequiresRedacted.make({
174
+ message:
175
+ "The webhook URL contains credentials. Pass the URL as Redacted to keep credentials out of plans and state.",
176
+ }),
177
+ )
178
+ : Result.succeed(undefined),
179
+ ),
180
+ );
181
+
182
+ type WebhookUrl = Extract<LogStreamProps, { readonly kind: "webhook" }>["url"];
183
+ const checkedProps = <P extends InputProps<LogStreamProps>>(
184
+ options: P,
185
+ ): Effect.Effect<P> =>
186
+ Effect.gen(function* () {
187
+ if (
188
+ (options.kind === "axiom" || options.kind === "webhook") &&
189
+ "topics" in options
190
+ )
191
+ yield* Effect.fromResult(
192
+ validateTopics(options.kind, options.topics),
193
+ ).pipe(Effect.orDie);
194
+ return "url" in options && Output.isOutput(options.url)
195
+ ? {
196
+ ...options,
197
+ url: options.url.pipe(
198
+ Output.mapEffect((value: WebhookUrl | undefined) =>
199
+ value === undefined
200
+ ? Effect.succeed(value)
201
+ : Effect.fromResult(validateWebhookUrl(value)).pipe(
202
+ Effect.orDie,
203
+ Effect.as(value),
204
+ ),
205
+ ),
206
+ ),
207
+ }
208
+ : options;
209
+ });
210
+
211
+ const normalizeTopics = (
212
+ topics: readonly (typeof Topics)[number][] | null | undefined,
213
+ ) =>
214
+ topics == null
215
+ ? topics
216
+ : Array.filter(Topics, (topic) => Array.contains(topics, topic));
217
+
218
+ const input = (props: Config) => {
219
+ if ("topics" in props) {
220
+ const result = validateTopics(props.kind, props.topics);
221
+ if (Result.isFailure(result)) return Effect.fail(result.failure);
222
+ }
223
+ switch (props.kind) {
224
+ case "axiom":
225
+ return Effect.succeed({
226
+ logStreamType: props.kind,
227
+ apiKey: props.apiKey,
228
+ datasetName: props.datasetName,
229
+ attributes: props.attributes,
230
+ ingestUrl: props.ingestUrl ?? null,
231
+ topics: props.topics ?? null,
232
+ });
233
+ case "sentry":
234
+ return Effect.succeed({
235
+ logStreamType: props.kind,
236
+ dsn: props.dsn,
237
+ tags: props.tags ?? null,
238
+ });
239
+ case "webhook":
240
+ return Effect.fromResult(validateWebhookUrl(props.url)).pipe(
241
+ Effect.as({
242
+ logStreamType: props.kind,
243
+ url: props.url,
244
+ format: props.format,
245
+ topics: props.topics ?? null,
246
+ }),
247
+ );
248
+ case "datadog":
249
+ case "postHogLogs":
250
+ case "postHogErrorTracking":
251
+ case "s3Export":
252
+ return Effect.fail(
253
+ LogStreamNotEnabled.make({
254
+ kind: props.kind,
255
+ message: "This log stream kind is not switched on.",
256
+ }),
257
+ );
258
+ }
259
+ };
260
+
261
+ const recover = (
262
+ kind: LogStreamProps["kind"],
263
+ ids: readonly string[],
264
+ reason: string,
265
+ ) =>
266
+ LogStreamRecoveryRequired.make({
267
+ kind,
268
+ ids,
269
+ message: `${reason} Fix the stream or ownership record manually before continuing.`,
270
+ });
271
+
272
+ const identify = Effect.fn("LogStream.identify")(function* (
273
+ output: LogStreamAttributes,
274
+ live: ConvexClient.LogStreamConfig,
275
+ ) {
276
+ const config = Redacted.value(live);
277
+ if (config.id !== output.id || config.logStreamType !== output.kind)
278
+ return yield* recover(
279
+ output.kind,
280
+ [output.id],
281
+ "The response identifies a different log stream.",
282
+ );
283
+ });
284
+
285
+ const drift = (output: LogStreamAttributes) =>
286
+ LogStreamDrift.make({
287
+ id: output.id,
288
+ kind: output.kind,
289
+ message:
290
+ "Readable log stream fields differ from the previous and desired configuration. An earlier update may not have finished. If so, apply that same change again before changing or reverting it. If the stream was edited outside the stack, restore those fields or change the stack to match.",
291
+ });
292
+
293
+ const readable = (
294
+ config: Effect.Success<ReturnType<typeof input>> | typeof LiveConfig.Type,
295
+ ): Result.Result<Readonly<Record<string, unknown>>, LogStreamInvalidUrl> => {
296
+ switch (config.logStreamType) {
297
+ case "axiom": {
298
+ const base = {
299
+ logStreamType: config.logStreamType,
300
+ datasetName: config.datasetName,
301
+ attributes: config.attributes,
302
+ topics: normalizeTopics(config.topics),
303
+ };
304
+ return config.ingestUrl == null
305
+ ? Result.succeed({
306
+ ...base,
307
+ ...(config.ingestUrl === undefined ? {} : { ingestUrl: null }),
308
+ })
309
+ : Result.map(normalizeUrl(config.ingestUrl), (ingestUrl) => ({
310
+ ...base,
311
+ ingestUrl,
312
+ }));
313
+ }
314
+ case "sentry":
315
+ return Result.succeed({
316
+ logStreamType: config.logStreamType,
317
+ ...(config.tags === undefined ? {} : { tags: config.tags }),
318
+ });
319
+ case "webhook":
320
+ return Result.map(
321
+ normalizeUrl(
322
+ Redacted.isRedacted(config.url)
323
+ ? Redacted.value(config.url)
324
+ : config.url,
325
+ ),
326
+ (url) => ({
327
+ logStreamType: config.logStreamType,
328
+ url,
329
+ format: config.format,
330
+ topics: normalizeTopics(config.topics),
331
+ }),
332
+ );
333
+ default:
334
+ return Result.succeed({ logStreamType: config.logStreamType });
335
+ }
336
+ };
337
+
338
+ const differences = Effect.fn("LogStream.differences")(function* (
339
+ props: LogStreamProps,
340
+ live: ConvexClient.LogStreamConfig,
341
+ ) {
342
+ const desired = yield* input(props);
343
+ // Compare only fields Convex returns, excluding its generated webhook secret.
344
+ const expected = yield* Effect.fromResult(readable(desired));
345
+ const actual = yield* live.pipe(Redacted.value, readable, Effect.fromResult);
346
+ return Array.filter(
347
+ Array.dedupe([...Record.keys(expected), ...Record.keys(actual)]),
348
+ (key) =>
349
+ !deepEqual(
350
+ { value: expected[key] },
351
+ { value: actual[key] },
352
+ { stripNullish: true },
353
+ ),
354
+ );
355
+ });
356
+
357
+ const validate = Effect.fn("LogStream.validate")(function* (
358
+ props: LogStreamProps,
359
+ output: LogStreamAttributes,
360
+ live: ConvexClient.LogStreamConfig,
361
+ ) {
362
+ yield* identify(output, live);
363
+ if ((yield* differences(props, live)).length > 0) return yield* drift(output);
364
+ });
365
+
366
+ const resourceState = Effect.fn("LogStream.resourceState")(function* (
367
+ fqn: string,
368
+ instanceId: string,
369
+ ) {
370
+ const stack = yield* Effect.serviceOption(Stack);
371
+ const state = yield* Effect.serviceOption(State);
372
+ if (Option.isNone(stack) || Option.isNone(state)) return;
373
+ const store = yield* state.value;
374
+ const row = yield* store.get({
375
+ stack: stack.value.name,
376
+ stage: stack.value.stage,
377
+ fqn,
378
+ });
379
+ return row !== undefined &&
380
+ row.kind !== "action" &&
381
+ row.instanceId === instanceId
382
+ ? row
383
+ : undefined;
384
+ });
385
+
386
+ const attributes = (
387
+ live: ConvexClient.LogStreamConfig,
388
+ ): Pick<LogStreamAttributes, "hmacSecret"> => {
389
+ const config = Redacted.value(live);
390
+ return config.logStreamType === "webhook"
391
+ ? { hmacSecret: Redacted.make(config.hmacSecret) }
392
+ : {};
393
+ };
394
+
395
+ export const provider = Effect.gen(function* () {
396
+ const client = yield* ConvexClient.ConvexClient;
397
+ const get = Effect.fn("LogStream.get")(function* (
398
+ props: LogStreamProps,
399
+ id: string,
400
+ ) {
401
+ return yield* client
402
+ .getLogStream(props.deploymentUrl, props.deployKey, id)
403
+ .pipe(Effect.catchTag("ConvexNotFound", () => Effect.void));
404
+ });
405
+ const observe = Effect.fn("LogStream.observe")(function* (
406
+ props: LogStreamProps,
407
+ output: LogStreamAttributes,
408
+ ) {
409
+ const live = yield* get(props, output.id);
410
+ if (live === undefined)
411
+ return yield* recover(
412
+ output.kind,
413
+ [output.id],
414
+ "The recorded log stream was deleted.",
415
+ );
416
+ yield* identify(output, live);
417
+ return live;
418
+ });
419
+ const cold = Effect.fn("LogStream.cold")(function* (props: LogStreamProps) {
420
+ yield* input(props);
421
+ const streams = yield* client.listLogStreams(
422
+ props.deploymentUrl,
423
+ props.deployKey,
424
+ );
425
+ const matches = Array.filter(
426
+ streams,
427
+ (stream) =>
428
+ Redacted.value(stream).logStreamType === props.kind &&
429
+ Redacted.value(stream).status.type !== "deleting",
430
+ );
431
+ if (matches.length > 0)
432
+ return yield* recover(
433
+ props.kind,
434
+ Array.map(matches, (stream) => Redacted.value(stream).id),
435
+ "A stream of this kind exists without an ownership record. Another resource in this stack may already manage this kind. A backend holds one stream per kind.",
436
+ );
437
+ return streams;
438
+ });
439
+ return {
440
+ diff: Effect.fn("LogStream.diff")(function* ({
441
+ fqn,
442
+ instanceId,
443
+ olds,
444
+ news,
445
+ output,
446
+ }) {
447
+ const kind: unknown = "kind" in news ? news.kind : undefined;
448
+ switch (kind) {
449
+ case "datadog":
450
+ case "postHogLogs":
451
+ case "postHogErrorTracking":
452
+ case "s3Export":
453
+ return yield* LogStreamNotEnabled.make({
454
+ kind,
455
+ message: "This log stream kind is not switched on.",
456
+ });
457
+ }
458
+ if (kind === "webhook" && "url" in news && typeof news.url === "string")
459
+ yield* Effect.fromResult(validateWebhookUrl(news.url));
460
+ if ((kind === "axiom" || kind === "webhook") && "topics" in news)
461
+ yield* Effect.fromResult(validateTopics(kind, news.topics));
462
+ const live =
463
+ output === undefined ? undefined : yield* observe(olds, output);
464
+ const changed = live === undefined ? [] : yield* differences(olds, live);
465
+ if (!isResolved(news)) {
466
+ if (changed.length > 0) {
467
+ yield* Effect.logWarning(
468
+ `Log stream ${output?.id} differs in fields ${Array.join(changed, ", ")}. Apply will check for an unfinished update or an external edit.`,
469
+ );
470
+ return { action: "update" };
471
+ }
472
+ return;
473
+ }
474
+ yield* input(news);
475
+ if (output === undefined) {
476
+ yield* cold(news);
477
+ return;
478
+ }
479
+ if (
480
+ news.kind !== output.kind ||
481
+ news.deploymentUrl !== olds.deploymentUrl
482
+ ) {
483
+ yield* cold(news);
484
+ return { action: "replace" };
485
+ }
486
+ const row = yield* resourceState(fqn, instanceId);
487
+ const clean = row?.status === "created" || row?.status === "updated";
488
+ if (changed.length > 0 && live !== undefined) {
489
+ if ((yield* differences(news, live)).length > 0 && clean)
490
+ return yield* drift(output);
491
+ yield* Effect.logWarning(
492
+ `Log stream ${output.id} differs in fields ${Array.join(changed, ", ")}. Apply will check for an unfinished update or an external edit.`,
493
+ );
494
+ return { action: "update" };
495
+ }
496
+ if (
497
+ (news.kind !== "webhook" && row !== undefined && !clean) ||
498
+ !deepEqual(news, olds) ||
499
+ (live !== undefined &&
500
+ !deepEqual(attributes(live).hmacSecret, output.hmacSecret))
501
+ )
502
+ return { action: "update" };
503
+ }),
504
+ read: Effect.fn("LogStream.read")(function* ({ olds, output }) {
505
+ if (output !== undefined) {
506
+ const live = yield* observe(olds, output);
507
+ yield* validate(olds, output, live);
508
+ return { id: output.id, kind: output.kind, ...attributes(live) };
509
+ }
510
+ if (isResolved(olds)) yield* cold(olds);
511
+ return undefined;
512
+ }),
513
+ reconcile: Effect.fn("LogStream.reconcile")(function* ({
514
+ fqn,
515
+ instanceId,
516
+ news,
517
+ olds,
518
+ output,
519
+ }) {
520
+ const config = yield* input(news);
521
+ if (output !== undefined) {
522
+ if (olds === undefined) {
523
+ const row = yield* resourceState(fqn, instanceId);
524
+ if (
525
+ row?.status === "creating" &&
526
+ row.attr?.id === output.id &&
527
+ row.attr?.kind === output.kind &&
528
+ row.props.deploymentUrl === news.deploymentUrl &&
529
+ news.kind === output.kind
530
+ ) {
531
+ const live = yield* observe(news, output);
532
+ yield* validate(news, output, live);
533
+ return { id: output.id, kind: output.kind, ...attributes(live) };
534
+ }
535
+ return yield* recover(
536
+ output.kind,
537
+ [output.id],
538
+ "The prior ownership props are missing.",
539
+ );
540
+ }
541
+ const observed = yield* observe(olds, output);
542
+ if (
543
+ news.kind !== output.kind ||
544
+ news.deploymentUrl !== olds.deploymentUrl
545
+ )
546
+ return yield* recover(
547
+ output.kind,
548
+ [output.id],
549
+ "A kind or deployment change needs replacement.",
550
+ );
551
+ const desiredChanges = yield* differences(news, observed);
552
+ if (
553
+ desiredChanges.length > 0 &&
554
+ (yield* differences(olds, observed)).length > 0
555
+ )
556
+ return yield* drift(output);
557
+ if (news.kind === "webhook" && desiredChanges.length === 0)
558
+ return { id: output.id, kind: output.kind, ...attributes(observed) };
559
+ yield* client.updateLogStream(
560
+ news.deploymentUrl,
561
+ news.deployKey,
562
+ output.id,
563
+ config,
564
+ );
565
+ const live = yield* get(news, output.id);
566
+ if (live === undefined)
567
+ return yield* recover(
568
+ news.kind,
569
+ [output.id],
570
+ "Update succeeded but the stream could not be read.",
571
+ );
572
+ const updated = {
573
+ id: output.id,
574
+ kind: output.kind,
575
+ ...attributes(live),
576
+ };
577
+ yield* validate(news, updated, live);
578
+ return updated;
579
+ }
580
+ yield* cold(news);
581
+ yield* checkpointCreate(fqn, instanceId, news);
582
+ const response = yield* client
583
+ .createLogStream(news.deploymentUrl, news.deployKey, config)
584
+ .pipe(
585
+ Effect.catchTag("ConvexConflict", () =>
586
+ Effect.gen(function* () {
587
+ const streams = yield* cold(news);
588
+ const deleting = Array.filter(streams, (stream) => {
589
+ const live = Redacted.value(stream);
590
+ return (
591
+ live.logStreamType === news.kind &&
592
+ live.status.type === "deleting"
593
+ );
594
+ });
595
+ if (deleting.length > 0)
596
+ return yield* LogStreamRecoveryRequired.make({
597
+ kind: news.kind,
598
+ ids: Array.map(
599
+ deleting,
600
+ (stream) => Redacted.value(stream).id,
601
+ ),
602
+ message:
603
+ "A stream of this kind is still deleting. Run the deploy again once deletion finishes.",
604
+ });
605
+ return yield* recover(
606
+ news.kind,
607
+ [],
608
+ "A concurrent create conflicted with this stream.",
609
+ );
610
+ }),
611
+ ),
612
+ );
613
+ const created = Redacted.value(response);
614
+ const owned: LogStreamAttributes = {
615
+ id: created.id,
616
+ kind: news.kind,
617
+ ...(created.logStreamType === "webhook"
618
+ ? { hmacSecret: Redacted.make(created.hmacSecret) }
619
+ : {}),
620
+ };
621
+ yield* checkpointCreate(fqn, instanceId, news, owned);
622
+ if (created.logStreamType !== news.kind)
623
+ return yield* recover(
624
+ news.kind,
625
+ [created.id],
626
+ "Create returned a different stream kind.",
627
+ );
628
+ const live = yield* get(news, created.id);
629
+ if (live === undefined)
630
+ return yield* recover(
631
+ news.kind,
632
+ [created.id],
633
+ "Create succeeded but the stream could not be read.",
634
+ );
635
+ const createdOutput: LogStreamAttributes = {
636
+ id: created.id,
637
+ kind: news.kind,
638
+ ...attributes(live),
639
+ };
640
+ yield* validate(news, createdOutput, live);
641
+ yield* checkpointCreate(fqn, instanceId, news, createdOutput);
642
+ return createdOutput;
643
+ }),
644
+ delete: Effect.fn("LogStream.delete")(function* ({ olds, output }) {
645
+ const live = yield* get(olds, output.id);
646
+ if (live === undefined) return;
647
+ yield* identify(output, live);
648
+ yield* client
649
+ .deleteLogStream(olds.deploymentUrl, olds.deployKey, output.id)
650
+ .pipe(Effect.catchTag("ConvexNotFound", () => Effect.void));
651
+ }),
652
+ } satisfies Provider.ProviderServiceInput<LogStream>;
653
+ });
654
+
655
+ export const layer = Provider.effect(LogStream, provider);
656
+ export const localLayer = Provider.effect(LogStream, provider).pipe(
657
+ Layer.provide(
658
+ ConvexClient.layer({ mode: "local" }).pipe(
659
+ Layer.provide(FetchHttpClient.layer),
660
+ ),
661
+ ),
662
+ );
package/src/Providers.ts CHANGED
@@ -4,11 +4,14 @@ import type * as Redacted from "effect/Redacted";
4
4
  import * as Layer from "effect/Layer";
5
5
  import * as FetchHttpClient from "effect/http/FetchHttpClient";
6
6
  import * as Code from "./Code.js";
7
+ import * as CustomDomain from "./CustomDomain.js";
7
8
  import * as ConvexClient from "./ConvexClient.js";
8
9
  import * as Deployment from "./Deployment.js";
9
10
  import * as DeployKey from "./DeployKey.js";
10
11
  import * as EnvironmentVariables from "./EnvironmentVariables.js";
11
12
  import * as Project from "./Project.js";
13
+ import * as LogStream from "./LogStream.js";
14
+ import * as UsageLimit from "./UsageLimit.js";
12
15
  import * as CommandRunner from "./internal/CommandRunner.js";
13
16
  import * as PushLock from "./internal/PushLock.js";
14
17
 
@@ -35,6 +38,9 @@ export const providers = (options?: {
35
38
  DeployKey.DeployKey,
36
39
  EnvironmentVariables.EnvironmentVariables,
37
40
  Code.Code,
41
+ CustomDomain.CustomDomain,
42
+ LogStream.LogStream,
43
+ UsageLimit.UsageLimit,
38
44
  ]),
39
45
  ).pipe(
40
46
  Layer.provide(
@@ -55,10 +61,22 @@ export const providers = (options?: {
55
61
  live: () => EnvironmentVariables.layer,
56
62
  local: () => EnvironmentVariables.localLayer,
57
63
  }),
64
+ ProviderLayer.dual(CustomDomain.CustomDomain, {
65
+ live: () => CustomDomain.layer,
66
+ local: () => CustomDomain.localLayer,
67
+ }),
68
+ ProviderLayer.dual(UsageLimit.UsageLimit, {
69
+ live: () => UsageLimit.layer,
70
+ local: () => UsageLimit.localLayer,
71
+ }),
58
72
  ProviderLayer.dual(Code.Code, {
59
73
  live: () => Code.layer,
60
74
  local: () => Code.localLayer,
61
75
  }),
76
+ ProviderLayer.dual(LogStream.LogStream, {
77
+ live: () => LogStream.layer,
78
+ local: () => LogStream.localLayer,
79
+ }),
62
80
  ),
63
81
  ),
64
82
  Layer.provide(