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

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 (99) 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.map +1 -1
  72. package/dist/internal/MountInit.js +4 -6
  73. package/dist/internal/MountInit.js.map +1 -1
  74. package/dist/internal/PushLock.d.ts +1 -0
  75. package/dist/internal/PushLock.d.ts.map +1 -1
  76. package/dist/internal/PushLock.js +4 -3
  77. package/dist/internal/PushLock.js.map +1 -1
  78. package/dist/internal/ResourceIdentity.js +1 -1
  79. package/dist/internal/Zip.js +1 -1
  80. package/package.json +4 -4
  81. package/src/Backend.ts +422 -12
  82. package/src/Code.ts +13 -0
  83. package/src/Confect.ts +7 -1
  84. package/src/Convex.ts +24 -1
  85. package/src/ConvexClient.ts +364 -31
  86. package/src/CustomDomain.ts +453 -0
  87. package/src/Deployment.ts +74 -12
  88. package/src/EnvironmentVariables.ts +505 -19
  89. package/src/LogStream.ts +662 -0
  90. package/src/Providers.ts +18 -0
  91. package/src/UsageLimit.ts +253 -0
  92. package/src/internal/CodeDeployment.ts +115 -0
  93. package/src/internal/CommandRunner.ts +182 -1
  94. package/src/internal/FunctionsDirectory.ts +27 -0
  95. package/src/internal/Lifecycle.ts +21 -1
  96. package/src/internal/LocalBackend.ts +9 -0
  97. package/src/internal/LogStream.ts +154 -0
  98. package/src/internal/MountInit.ts +3 -13
  99. package/src/internal/PushLock.ts +4 -1
@@ -0,0 +1,253 @@
1
+ import type {
2
+ UsageLimitMetric,
3
+ UsageLimitType,
4
+ UsageLimitWindow,
5
+ } from "@convex-dev/platform/deploymentApi";
6
+ import { OwnedBySomeoneElse, Unowned } from "alchemy/AdoptPolicy";
7
+ import { isResolved } from "alchemy/Diff";
8
+ import * as Provider from "alchemy/Provider";
9
+ import { Resource } from "alchemy/Resource";
10
+ import * as Array from "effect/Array";
11
+ import * as Effect from "effect/Effect";
12
+ import * as Option from "effect/Option";
13
+ import * as Schema from "effect/Schema";
14
+
15
+ import { ConvexClient, type UsageLimitInput } from "./ConvexClient.js";
16
+ import type { Providers } from "./Providers.js";
17
+ import { checkpointCreate, shouldAdopt } from "./internal/Lifecycle.js";
18
+
19
+ export interface UsageLimitProps {
20
+ readonly deployment: { readonly name: string; readonly url: string };
21
+ readonly metric: UsageLimitMetric;
22
+ readonly window: UsageLimitWindow;
23
+ readonly limit: number;
24
+ readonly limitType?: UsageLimitType;
25
+ readonly enabled?: boolean;
26
+ }
27
+
28
+ export interface UsageLimitAttributes {
29
+ readonly deployment: { readonly name: string; readonly url: string };
30
+ readonly id: string;
31
+ readonly metric: UsageLimitMetric;
32
+ readonly window: UsageLimitWindow;
33
+ readonly limit: number;
34
+ readonly limitType: UsageLimitType;
35
+ readonly enabled: boolean;
36
+ }
37
+
38
+ export type UsageLimit = Resource<
39
+ "Convex.UsageLimit",
40
+ UsageLimitProps,
41
+ UsageLimitAttributes,
42
+ never,
43
+ Providers
44
+ >;
45
+ export const UsageLimit = Resource<UsageLimit>("Convex.UsageLimit");
46
+
47
+ export class UsageLimitTargetChange extends Schema.TaggedError<
48
+ UsageLimitTargetChange,
49
+ { readonly brand: unique symbol }
50
+ >()("UsageLimitTargetChange", { deploymentName: Schema.String }) {}
51
+
52
+ export class AmbiguousUsageLimit extends Schema.TaggedError<
53
+ AmbiguousUsageLimit,
54
+ { readonly brand: unique symbol }
55
+ >()("AmbiguousUsageLimit", {
56
+ deploymentName: Schema.String,
57
+ metric: Schema.String,
58
+ window: Schema.String,
59
+ limitType: Schema.String,
60
+ limitIds: Schema.Array(Schema.String),
61
+ }) {
62
+ override get message() {
63
+ return `Usage limit ${this.metric}/${this.window}/${this.limitType} on ${this.deploymentName} matches multiple ids: ${Array.join(this.limitIds, ", ")}.`;
64
+ }
65
+ }
66
+
67
+ export class InvalidUsageLimit extends Schema.TaggedError<
68
+ InvalidUsageLimit,
69
+ { readonly brand: unique symbol }
70
+ >()("InvalidUsageLimit", { limit: Schema.String }) {
71
+ override get message() {
72
+ return `Usage limit ${this.limit} must be a positive safe integer.`;
73
+ }
74
+ }
75
+
76
+ export class UnsupportedLocalUsageLimit extends Schema.TaggedError<
77
+ UnsupportedLocalUsageLimit,
78
+ { readonly brand: unique symbol }
79
+ >()("UnsupportedLocalUsageLimit", {}) {
80
+ override get message() {
81
+ return "Declare Convex.UsageLimit only for cloud stages.";
82
+ }
83
+ }
84
+
85
+ const validateLimit = Effect.fn("UsageLimit.validateLimit")(function* (
86
+ limit: number,
87
+ ) {
88
+ if (!Number.isSafeInteger(limit) || limit < 1)
89
+ return yield* InvalidUsageLimit.make({ limit: String(limit) });
90
+ });
91
+
92
+ const desired = (props: UsageLimitProps): UsageLimitInput => ({
93
+ metric: props.metric,
94
+ window: props.window,
95
+ limit: props.limit,
96
+ limitType: props.limitType ?? "warning",
97
+ enabled: props.enabled ?? true,
98
+ });
99
+ const sameTarget = (current: UsageLimitAttributes, props: UsageLimitProps) =>
100
+ current.deployment.name === props.deployment.name;
101
+ const sameConfig = (current: UsageLimitInput, input: UsageLimitInput) =>
102
+ current.metric === input.metric &&
103
+ current.window === input.window &&
104
+ current.limitType === input.limitType &&
105
+ current.limit === input.limit &&
106
+ current.enabled === input.enabled;
107
+
108
+ const ownershipConflict = (
109
+ input: Pick<UsageLimitInput, "metric" | "window" | "limitType">,
110
+ ) => {
111
+ const physicalName = `${input.metric}/${input.window}/${input.limitType}`;
112
+ return new OwnedBySomeoneElse({
113
+ message: `Usage limit ${physicalName} already exists on this deployment. Declare one limit per metric, window and type. Re-run with --adopt or adopt(true) to take it over.`,
114
+ resourceType: UsageLimit.Type,
115
+ physicalName,
116
+ });
117
+ };
118
+
119
+ export const provider = Effect.gen(function* () {
120
+ const client = yield* ConvexClient;
121
+ const find = Effect.fn("UsageLimit.find")(function* (
122
+ output: UsageLimitAttributes,
123
+ url = output.deployment.url,
124
+ ) {
125
+ const limits = yield* client
126
+ .listUsageLimits(url)
127
+ .pipe(Effect.catchTag("ConvexNotFound", () => Effect.succeed([])));
128
+ return Option.getOrUndefined(
129
+ Array.findFirst(limits, (limit) => limit.id === output.id),
130
+ );
131
+ });
132
+ const findMatching = Effect.fn("UsageLimit.findMatching")(function* (
133
+ deployment: UsageLimitProps["deployment"],
134
+ input: Pick<UsageLimitInput, "metric" | "window" | "limitType">,
135
+ ) {
136
+ const limits = yield* client
137
+ .listUsageLimits(deployment.url)
138
+ .pipe(Effect.catchTag("ConvexNotFound", () => Effect.succeed([])));
139
+ const matches = Array.filter(
140
+ limits,
141
+ (limit) =>
142
+ limit.metric === input.metric &&
143
+ limit.window === input.window &&
144
+ limit.limitType === input.limitType,
145
+ );
146
+ if (matches.length > 1)
147
+ return yield* AmbiguousUsageLimit.make({
148
+ deploymentName: deployment.name,
149
+ ...input,
150
+ limitIds: Array.map(matches, (limit) => limit.id),
151
+ });
152
+ return matches[0];
153
+ });
154
+ return {
155
+ diff: Effect.fn("UsageLimit.diff")(function* ({ news, output }) {
156
+ if (!isResolved(news)) return;
157
+ yield* validateLimit(news.limit);
158
+ if (output === undefined) return;
159
+ if (!sameTarget(output, news)) return { action: "replace" };
160
+ const current = yield* find(output, news.deployment.url);
161
+ if (
162
+ output.deployment.url !== news.deployment.url ||
163
+ current === undefined ||
164
+ !sameConfig(current, desired(news))
165
+ )
166
+ return { action: "update" };
167
+ }),
168
+ read: Effect.fn("UsageLimit.read")(function* ({ fqn, olds, output }) {
169
+ if (output === undefined) {
170
+ if (olds.limit !== undefined) yield* validateLimit(olds.limit);
171
+ if (
172
+ olds.deployment === undefined ||
173
+ olds.metric === undefined ||
174
+ olds.window === undefined
175
+ )
176
+ return;
177
+ const input = {
178
+ metric: olds.metric,
179
+ window: olds.window,
180
+ limitType: olds.limitType ?? "warning",
181
+ };
182
+ const current = yield* findMatching(olds.deployment, input);
183
+ if (current !== undefined && !(yield* shouldAdopt(fqn)))
184
+ yield* Effect.logWarning(ownershipConflict(input).message);
185
+ return current === undefined
186
+ ? undefined
187
+ : Unowned({ deployment: olds.deployment, ...current });
188
+ }
189
+ const current = yield* find(output);
190
+ return current === undefined
191
+ ? undefined
192
+ : { deployment: output.deployment, ...current };
193
+ }),
194
+ reconcile: Effect.fn("UsageLimit.reconcile")(function* ({
195
+ fqn,
196
+ instanceId,
197
+ news,
198
+ output,
199
+ }) {
200
+ yield* validateLimit(news.limit);
201
+ if (output === undefined) yield* checkpointCreate(fqn, instanceId, news);
202
+ if (output !== undefined && !sameTarget(output, news))
203
+ return yield* UsageLimitTargetChange.make({
204
+ deploymentName: output.deployment.name,
205
+ });
206
+ const input = desired(news);
207
+ let current =
208
+ output === undefined
209
+ ? undefined
210
+ : yield* find(output, news.deployment.url);
211
+ if (current === undefined) {
212
+ const existing = yield* findMatching(news.deployment, input);
213
+ if (existing !== undefined && !(yield* shouldAdopt(fqn)))
214
+ return yield* ownershipConflict(input);
215
+ current = existing;
216
+ }
217
+ if (current !== undefined) {
218
+ yield* checkpointCreate(fqn, instanceId, news, {
219
+ deployment: news.deployment,
220
+ ...current,
221
+ });
222
+ const updated = sameConfig(current, input)
223
+ ? current
224
+ : yield* client.updateUsageLimit(
225
+ news.deployment.url,
226
+ current.id,
227
+ input,
228
+ );
229
+ return { deployment: news.deployment, ...updated };
230
+ }
231
+ const created = yield* client.createUsageLimit(
232
+ news.deployment.url,
233
+ input,
234
+ );
235
+ const attributes = { deployment: news.deployment, ...created };
236
+ yield* checkpointCreate(fqn, instanceId, news, attributes);
237
+ return attributes;
238
+ }),
239
+ delete: Effect.fn("UsageLimit.delete")(function* ({ output }) {
240
+ yield* client
241
+ .deleteUsageLimit(output.deployment.url, output.id)
242
+ .pipe(Effect.catchTag("ConvexNotFound", () => Effect.void));
243
+ }),
244
+ } satisfies Provider.ProviderServiceInput<UsageLimit>;
245
+ });
246
+
247
+ export const localProvider = Effect.succeed({
248
+ reconcile: () => Effect.fail(UnsupportedLocalUsageLimit.make({})),
249
+ delete: () => Effect.void,
250
+ } satisfies Provider.ProviderServiceInput<UsageLimit>);
251
+
252
+ export const layer = Provider.effect(UsageLimit, provider);
253
+ export const localLayer = Provider.effect(UsageLimit, localProvider);
@@ -1,12 +1,17 @@
1
+ import * as HealthModule from "@confect/core/HealthModule";
1
2
  import type { ProviderMode } from "alchemy/ProviderMode";
2
3
  import * as Effect from "effect/Effect";
4
+ import * as FileSystem from "effect/FileSystem";
5
+ import * as Option from "effect/Option";
3
6
  import * as Path from "effect/Path";
4
7
  import * as Redacted from "effect/Redacted";
5
8
  import * as Result from "effect/Result";
6
9
  import * as Schema from "effect/Schema";
7
10
  import type { CodeProps } from "../Code.js";
11
+ import { ConvexClient } from "../ConvexClient.js";
8
12
  import { CommandRunner, type PushTarget } from "./CommandRunner.js";
9
13
  import * as Loopback from "./Loopback.js";
14
+ import { functionsDirectory } from "./FunctionsDirectory.js";
10
15
  import * as MountInit from "./MountInit.js";
11
16
  import { PushLock } from "./PushLock.js";
12
17
 
@@ -17,6 +22,93 @@ export class InvalidDeploymentTarget extends Schema.TaggedError<
17
22
  reason: Schema.Literals(["deployment", "deployKey"]),
18
23
  }) {}
19
24
 
25
+ export class HealthCheckFailed extends Schema.TaggedError<
26
+ HealthCheckFailed,
27
+ { readonly brand: unique symbol }
28
+ >()("HealthCheckFailed", {
29
+ expected: Schema.String,
30
+ answered: Schema.optional(Schema.String),
31
+ detail: Schema.optional(Schema.String),
32
+ }) {
33
+ override get message() {
34
+ return this.answered === undefined
35
+ ? `health: the pushed code gave no manifest digest (${this.detail ?? "no answer"}); expected ${this.expected}`
36
+ : `health: the pushed code was generated with ${this.answered}, not ${this.expected}`;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Prepare's health module is edited, missing, or generated for another
42
+ * manifest. Nothing was pushed.
43
+ */
44
+ export class StaleManifest extends Schema.TaggedError<
45
+ StaleManifest,
46
+ { readonly brand: unique symbol }
47
+ >()("StaleManifest", {
48
+ expected: Schema.String,
49
+ reason: Schema.Literals(["DifferentDigest", "NotGenerated"]),
50
+ generated: Schema.optionalKey(Schema.String),
51
+ healthModulePath: Schema.optionalKey(Schema.String),
52
+ }) {
53
+ override get message() {
54
+ return this.reason === "NotGenerated"
55
+ ? `The health module ${this.healthModulePath ?? "at the configured functions directory"} is not as codegen writes it, so nothing was pushed. Do not edit or format the file; regenerate it with confect codegen. The CLI that generates it and the Alchemy package that reads it must be on the same Confect release.`
56
+ : `prepare generated manifest ${this.generated}, but the stack imported manifest ${this.expected}, so nothing was pushed. Run confect codegen, commit the generated files and deploy again.`;
57
+ }
58
+ }
59
+
60
+ const HEALTH = "confectHealth:digest";
61
+
62
+ /**
63
+ * The digest prepare wrote into the health module, which is what the push would
64
+ * deploy; none when the module is missing or unreadable.
65
+ */
66
+ const generatedDigest = Effect.fn("Code.generatedDigest")(
67
+ function* (cwd: string) {
68
+ const fs = yield* FileSystem.FileSystem;
69
+ const path = yield* Path.Path;
70
+ const functions = yield* functionsDirectory(cwd);
71
+ const file = path.join(cwd, functions, "confectHealth.ts");
72
+ const digest = yield* fs
73
+ .readFileString(file)
74
+ .pipe(
75
+ Effect.map(HealthModule.read),
76
+ Effect.orElseSucceed(Option.none<string>),
77
+ );
78
+ return { path: file, digest };
79
+ },
80
+ Effect.orElseSucceed(() => ({
81
+ path: undefined,
82
+ digest: Option.none<string>(),
83
+ })),
84
+ );
85
+
86
+ /**
87
+ * Ask the pushed code which manifest digest codegen wrote into it, and fail
88
+ * unless it is `expected`.
89
+ */
90
+ const checkHealth = Effect.fn("Code.checkHealth")(function* (
91
+ url: string,
92
+ key: Redacted.Redacted<string>,
93
+ expected: string,
94
+ ) {
95
+ const client = yield* ConvexClient;
96
+ const answer = yield* client.runFunction(url, key, HEALTH, {}).pipe(
97
+ Effect.mapError((error) =>
98
+ HealthCheckFailed.make({ expected, detail: error._tag }),
99
+ ),
100
+ Effect.timeoutOrElse({
101
+ duration: "30 seconds",
102
+ orElse: () =>
103
+ Effect.fail(HealthCheckFailed.make({ expected, detail: "timeout" })),
104
+ }),
105
+ );
106
+ if (typeof answer !== "string")
107
+ return yield* HealthCheckFailed.make({ expected, detail: "not a string" });
108
+ if (answer !== expected)
109
+ return yield* HealthCheckFailed.make({ expected, answered: answer });
110
+ });
111
+
20
112
  const deploymentName = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
21
113
 
22
114
  const cloudTarget = (
@@ -83,6 +175,23 @@ export const deploy = Effect.fn("Code.deploy")(function* (
83
175
  Effect.gen(function* () {
84
176
  if (props.prepare !== undefined)
85
177
  yield* runner.prepare(props.prepare, props.cwd);
178
+ if (props.manifestDigest !== undefined) {
179
+ const { path: file, digest: generated } = yield* generatedDigest(
180
+ path.resolve(props.cwd ?? "."),
181
+ );
182
+ if (Option.getOrUndefined(generated) !== props.manifestDigest)
183
+ return yield* StaleManifest.make({
184
+ expected: props.manifestDigest,
185
+ ...(file === undefined ? {} : { healthModulePath: file }),
186
+ reason: Option.isNone(generated)
187
+ ? "NotGenerated"
188
+ : "DifferentDigest",
189
+ ...Option.match(generated, {
190
+ onNone: () => ({}),
191
+ onSome: (digest) => ({ generated: digest }),
192
+ }),
193
+ });
194
+ }
86
195
  yield* runner.deploy(target, props.cwd);
87
196
  // WORKAROUND: Convex has no component `init` or install `args` in a
88
197
  // stable release, so each push runs every mount's `init` itself, inside
@@ -95,6 +204,12 @@ export const deploy = Effect.fn("Code.deploy")(function* (
95
204
  props.deployKey,
96
205
  path.resolve(props.cwd ?? "."),
97
206
  );
207
+ if (props.manifestDigest !== undefined)
208
+ yield* checkHealth(
209
+ props.deployment.url,
210
+ props.deployKey,
211
+ props.manifestDigest,
212
+ );
98
213
  }),
99
214
  );
100
215
  return { name: props.deployment.name, url: props.deployment.url };
@@ -1,12 +1,19 @@
1
+ import * as Arr from "effect/Array";
2
+ import * as Str from "effect/String";
1
3
  import * as Config from "effect/Config";
2
4
  import * as Context from "effect/Context";
5
+ import * as Deferred from "effect/Deferred";
3
6
  import * as Effect from "effect/Effect";
4
7
  import * as FileSystem from "effect/FileSystem";
5
8
  import * as Layer from "effect/Layer";
6
9
  import * as Path from "effect/Path";
7
10
  import * as Redacted from "effect/Redacted";
11
+ import * as Result from "effect/Result";
8
12
  import * as Schema from "effect/Schema";
13
+ import * as Stream from "effect/Stream";
9
14
  import { ChildProcess, ChildProcessSpawner } from "effect/process";
15
+ import { Buffer } from "node:buffer";
16
+ import { stripVTControlCharacters } from "node:util";
10
17
 
11
18
  export interface PrepareCommand {
12
19
  readonly command: string;
@@ -42,7 +49,20 @@ export class CommandError extends Schema.TaggedError<
42
49
  operation: Schema.Literals(["prepare", "deploy"]),
43
50
  reason: Schema.Literals(["configuration", "spawn", "exit"]),
44
51
  exitCode: Schema.optionalKey(Schema.Finite),
45
- }) {}
52
+ signal: Schema.optionalKey(Schema.String),
53
+ stdout: Schema.optionalKey(Schema.String),
54
+ stderr: Schema.optionalKey(Schema.String),
55
+ }) {
56
+ override get message(): string {
57
+ const status =
58
+ this.signal === undefined
59
+ ? this.exitCode === undefined
60
+ ? this.reason
61
+ : `exit code ${this.exitCode}`
62
+ : `signal ${this.signal}`;
63
+ return `${this.operation} command failed (${status})${this.stdout === undefined ? "" : `\nstdout:\n${this.stdout}`}${this.stderr === undefined ? "" : `\nstderr:\n${this.stderr}`}`;
64
+ }
65
+ }
46
66
 
47
67
  export class CommandRunner extends Context.Service<
48
68
  CommandRunner,
@@ -124,6 +144,92 @@ const packageBin = Effect.fn("CommandRunner.packageBin")(function* (
124
144
  }
125
145
  });
126
146
 
147
+ const outputTailSize = 8192;
148
+ const redaction = "<REDACTED>";
149
+ // Short needles match common diagnostic text rather than useful credentials.
150
+ const minimumCredentialLength = 8;
151
+
152
+ const outputTail = <E>(
153
+ stream: Stream.Stream<Uint8Array, E>,
154
+ secrets: ReadonlyArray<string>,
155
+ ) => {
156
+ const credentials = Arr.flatMap(
157
+ Arr.filter(secrets, (value) => value.length >= minimumCredentialLength),
158
+ (value: string) => {
159
+ const bytes = Buffer.from(value);
160
+ const base64 = Arr.flatMap(
161
+ ["base64", "base64url"] as const,
162
+ (encoding) => [
163
+ Str.replace(/=+$/, "")(bytes.toString(encoding)),
164
+ ...Arr.map([0, 1, 2], (offset) =>
165
+ // The first and last partial sextets depend on bytes outside the key.
166
+ Str.slice(
167
+ Math.ceil((offset * 8) / 6),
168
+ Math.floor(((offset + bytes.length) * 8) / 6),
169
+ )(Buffer.concat([Buffer.alloc(offset), bytes]).toString(encoding)),
170
+ ),
171
+ ],
172
+ );
173
+ return [value, ...base64];
174
+ },
175
+ );
176
+ const percentEncoded = Arr.map(
177
+ Arr.filter(secrets, (value) => value.length >= minimumCredentialLength),
178
+ (value) =>
179
+ Arr.join(
180
+ Arr.map(
181
+ Str.split(/(%[0-9A-F]{2})/)(encodeURIComponent(value)),
182
+ (part) =>
183
+ Str.startsWith("%")(part)
184
+ ? Arr.join(
185
+ Arr.map(Str.split("")(part), (char) =>
186
+ /[A-F]/.test(char)
187
+ ? `[${char}${Str.toLowerCase(char)}]`
188
+ : char,
189
+ ),
190
+ "",
191
+ )
192
+ : Str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")(part),
193
+ ),
194
+ "",
195
+ ),
196
+ );
197
+ return stream.pipe(
198
+ Stream.decodeText(),
199
+ Stream.runFold(
200
+ () => ({ text: "", truncated: false }),
201
+ (tail, chunk) => {
202
+ const text = tail.text + chunk;
203
+ return {
204
+ text: Str.slice(-outputTailSize)(text),
205
+ truncated: tail.truncated || text.length > outputTailSize,
206
+ };
207
+ },
208
+ ),
209
+ Effect.map(({ text, truncated }) => {
210
+ text = stripVTControlCharacters(text);
211
+ if (truncated) {
212
+ // A clipped token may be an unrecognised deploy key. Drop it whole.
213
+ text = Str.replace(/^\S*/, "")(text);
214
+ }
215
+ const pattern = new RegExp(
216
+ Arr.join(
217
+ [
218
+ "(?:prod|dev|preview|project|custom):[^\\s|]+\\|[^\\s\\\\\"'&)]+",
219
+ ...percentEncoded,
220
+ ...Arr.map(credentials, (value) =>
221
+ Str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")(value),
222
+ ),
223
+ ],
224
+ "|",
225
+ ),
226
+ "g",
227
+ );
228
+ return Str.slice(-outputTailSize)(Str.replace(pattern, redaction)(text));
229
+ }),
230
+ );
231
+ };
232
+
127
233
  /**
128
234
  * @internal
129
235
  */
@@ -139,7 +245,77 @@ export const layer = Layer.effect(
139
245
  args: ReadonlyArray<string>,
140
246
  cwd: string,
141
247
  env: Record<string, string | undefined>,
248
+ credentials: ReadonlyArray<string> = [],
142
249
  ) {
250
+ if (operation === "deploy") {
251
+ return yield* Effect.scoped(
252
+ Effect.gen(function* () {
253
+ const child = yield* spawner.spawn(
254
+ ChildProcess.make(command, args, {
255
+ cwd,
256
+ env,
257
+ extendEnv: false,
258
+ stdin: "ignore",
259
+ stdout: "pipe",
260
+ stderr: "pipe",
261
+ }),
262
+ );
263
+ const exited = yield* Deferred.make<void>();
264
+ // Descendants can inherit pipes and keep them open after the CLI exits.
265
+ const drainDeadline = Deferred.await(exited).pipe(
266
+ Effect.andThen(Effect.sleep("1 second")),
267
+ );
268
+ const [code, stdout, stderr] = yield* Effect.all(
269
+ [
270
+ child.exitCode.pipe(
271
+ Effect.result,
272
+ Effect.tap(() => Deferred.succeed(exited, undefined)),
273
+ ),
274
+ outputTail(
275
+ child.stdout.pipe(Stream.interruptWhen(drainDeadline)),
276
+ credentials,
277
+ ),
278
+ outputTail(
279
+ child.stderr.pipe(Stream.interruptWhen(drainDeadline)),
280
+ credentials,
281
+ ),
282
+ ],
283
+ { concurrency: "unbounded" },
284
+ );
285
+ if (Result.isFailure(code)) {
286
+ // The spawner exposes a terminating signal only in its cause message.
287
+ const cause = code.failure.cause;
288
+ const signal =
289
+ cause instanceof Error
290
+ ? /^Process interrupted due to receipt of signal: '(SIG[A-Z0-9]+)'$/.exec(
291
+ cause.message,
292
+ )?.[1]
293
+ : undefined;
294
+ return yield* CommandError.make({
295
+ operation,
296
+ reason: "exit",
297
+ ...(signal === undefined ? {} : { signal }),
298
+ stdout,
299
+ stderr,
300
+ });
301
+ }
302
+ if (code.success !== 0)
303
+ return yield* CommandError.make({
304
+ operation,
305
+ reason: "exit",
306
+ exitCode: Number(code.success),
307
+ stdout,
308
+ stderr,
309
+ });
310
+ }),
311
+ ).pipe(
312
+ Effect.mapError((error) =>
313
+ Schema.is(CommandError)(error)
314
+ ? error
315
+ : CommandError.make({ operation, reason: "spawn" }),
316
+ ),
317
+ );
318
+ }
143
319
  const code = yield* spawner
144
320
  .exitCode(
145
321
  ChildProcess.make(command, args, {
@@ -234,6 +410,11 @@ export const layer = Layer.effect(
234
410
  LOCALAPPDATA: temp,
235
411
  XDG_CONFIG_HOME: temp,
236
412
  },
413
+ [
414
+ Redacted.value(
415
+ target._tag === "Cloud" ? target.deployKey : target.adminKey,
416
+ ),
417
+ ],
237
418
  );
238
419
  }).pipe(
239
420
  Effect.mapError((error) =>
@@ -0,0 +1,27 @@
1
+ import * as Effect from "effect/Effect";
2
+ import * as FileSystem from "effect/FileSystem";
3
+ import * as Path from "effect/Path";
4
+ import * as Schema from "effect/Schema";
5
+
6
+ const ConvexJson = Schema.fromJsonString(
7
+ Schema.Struct({ functions: Schema.optional(Schema.String) }),
8
+ );
9
+
10
+ /**
11
+ * The backend's Convex directory relative to `cwd`, which `convex.json` may
12
+ * move from `convex`.
13
+ *
14
+ * @internal
15
+ */
16
+ export const functionsDirectory = Effect.fn("FunctionsDirectory.resolve")(
17
+ function* (cwd: string) {
18
+ const fs = yield* FileSystem.FileSystem;
19
+ const path = yield* Path.Path;
20
+ const config = path.join(cwd, "convex.json");
21
+ if (!(yield* fs.exists(config))) return "convex";
22
+ const { functions } = yield* fs
23
+ .readFileString(config)
24
+ .pipe(Effect.flatMap(Schema.decodeEffect(ConvexJson)));
25
+ return functions ?? "convex";
26
+ },
27
+ );
@@ -26,7 +26,7 @@ export const checkpointCreate = Effect.fn("Lifecycle.checkpointCreate")(
26
26
  row.instanceId !== instanceId
27
27
  )
28
28
  return yield* LifecycleStateError.make({ fqn });
29
- if (row.status !== "creating") return;
29
+ if (row.status !== "creating" && row.status !== "replacing") return;
30
30
  yield* store.set({
31
31
  ...key,
32
32
  value: {
@@ -38,6 +38,26 @@ export const checkpointCreate = Effect.fn("Lifecycle.checkpointCreate")(
38
38
  },
39
39
  );
40
40
 
41
+ export const checkpointUpdate = Effect.fn("Lifecycle.checkpointUpdate")(
42
+ function* (fqn: string, instanceId: string, output: object) {
43
+ const stack = yield* Effect.serviceOption(Stack);
44
+ if (Option.isNone(stack)) return;
45
+ const state = yield* Effect.serviceOption(State);
46
+ if (Option.isNone(state)) return yield* LifecycleStateError.make({ fqn });
47
+ const store = yield* state.value;
48
+ const key = { stack: stack.value.name, stage: stack.value.stage, fqn };
49
+ const row = yield* store.get(key);
50
+ if (
51
+ row === undefined ||
52
+ row.kind === "action" ||
53
+ row.instanceId !== instanceId
54
+ )
55
+ return yield* LifecycleStateError.make({ fqn });
56
+ if (row.status !== "updating") return;
57
+ yield* store.set({ ...key, value: { ...row, attr: output } });
58
+ },
59
+ );
60
+
41
61
  export const shouldAdopt = Effect.fn("Lifecycle.shouldAdopt")(function* (
42
62
  fqn: string,
43
63
  ) {