The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,106 lines 39.2 KB
Raw
1 import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2";
2 import { Credentials } from "@distilled.cloud/gcp/Credentials";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import type * as Bundle from "alchemy/Bundle/Bundle";
5 import { deepEqual, isResolved } from "alchemy/Diff";
6 import { createPhysicalName } from "alchemy/PhysicalName";
7 import { Platform, type Main, type PlatformProps } from "alchemy/Platform";
8 import * as Provider from "alchemy/Provider";
9 import { Resource, type ResourceBinding } from "alchemy/Resource";
10 import { type ServerHost } from "alchemy/Runtime/Process";
11 import { packEnvValue } from "alchemy/RuntimeContext";
12 import { tagRecord } from "alchemy/Tags";
13 import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 import * as Schedule from "effect/Schedule";
16 import * as Stream from "effect/Stream";
17 import { GcpEnvironment } from "../Environment.ts";
18 import { retryActAs, type AppliedIamGrant, type GcpHostBinding } from "../Host.ts";
19 import { createGcpHostRuntimeContext, type GcpHostRuntimeContext } from "../HostContext.ts";
20 import {
21 alchemyRuntimeEnv,
22 isManagedServiceAccount,
23 releaseHostIdentity,
24 resolveHostIdentity,
25 } from "../HostRuntime.ts";
26 import {
27 createInternalLabels,
28 diffLabels,
29 hasAlchemyLabels,
30 stripInternalLabels,
31 toLabels,
32 } from "../Labels.ts";
33 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
34 import type { Providers } from "../Providers.ts";
35 import {
36 DEFAULT_NODE_RUNTIME,
37 FUNCTION_ENTRY_POINT,
38 makeFunctionSource,
39 } from "./FunctionSource.ts";
40
41 const DEFAULT_ENVIRONMENT = "GEN_2";
42 const DEFAULT_RUNTIME = "nodejs20";
43 const MAX_NAME_LENGTH = 63;
44
45 export type StorageSource = {
46 /** GCS bucket that holds the source archive. */
47 bucket?: string;
48 /** Object name of the source archive (typically a `.zip`). */
49 object?: string;
50 /** Object generation. Omit to use the latest generation. */
51 generation?: string;
52 /** Signed upload URL returned by `generateUploadUrl` for 1st gen. */
53 sourceUploadUrl?: string;
54 };
55
56 export type RepoSource = {
57 /** Cloud Source Repository name. */
58 repoName?: string;
59 /** Branch regex (RE2). */
60 branchName?: string;
61 /** Tag regex (RE2). */
62 tagName?: string;
63 /** Commit SHA to build. */
64 commitSha?: string;
65 /** Directory relative to the repo root. */
66 dir?: string;
67 /** Project that owns the repository. Defaults to the function's project. */
68 projectId?: string;
69 };
70
71 export type Source = {
72 /** Source archive in Cloud Storage. */
73 storageSource?: StorageSource;
74 /** Source in Cloud Source Repositories. */
75 repoSource?: RepoSource;
76 /** GitHub URI. 1st gen only. */
77 gitUri?: string;
78 };
79
80 export type BuildConfig = {
81 /**
82 * Runtime to build and run (e.g. `nodejs20`, `python312`). Required on
83 * create.
84 * @default "nodejs20"
85 */
86 runtime?: string;
87 /**
88 * Exported function in the source to execute. Defaults to the function
89 * id.
90 */
91 entryPoint?: string;
92 /** Location of the function source. Required on create. */
93 source?: Source;
94 /** Build-time environment variables. */
95 environmentVariables?: Record<string, string>;
96 /**
97 * Cloud Build worker pool
98 * (`projects/{project}/locations/{location}/workerPools/{workerPool}`).
99 */
100 workerPool?: string;
101 /**
102 * Artifact Registry Docker repository
103 * (`projects/{project}/locations/{location}/repositories/{repository}`).
104 */
105 dockerRepository?: string;
106 /**
107 * Service account used for the build
108 * (`projects/{project}/serviceAccounts/{email}`).
109 */
110 serviceAccount?: string;
111 };
112
113 export type SecretEnvVar = {
114 /** Secret id (not the full resource name). */
115 secret?: string;
116 /** Environment variable name. */
117 key?: string;
118 /** Project that owns the secret. Defaults to the function's project. */
119 projectId?: string;
120 /** Secret version (`latest` or a number). */
121 version?: string;
122 };
123
124 export type SecretVolume = {
125 /** Secret id (not the full resource name). */
126 secret?: string;
127 /** Mount path inside the container (e.g. `/etc/secrets`). */
128 mountPath?: string;
129 /** Project that owns the secret. Defaults to the function's project. */
130 projectId?: string;
131 /** Versions to mount. Defaults to `latest` as a file named after the secret. */
132 versions?: Array<{ path?: string; version?: string }>;
133 };
134
135 export type DirectVpcNetworkInterface = {
136 /** VPC network name or URL. */
137 network?: string;
138 /** Subnetwork name or URL. */
139 subnetwork?: string;
140 /** Network tags applied to the function. */
141 tags?: string[];
142 };
143
144 export type ServiceConfig = {
145 /**
146 * Memory available to each instance (e.g. `"256Mi"`, `"512M"`).
147 * @default "256M"
148 */
149 availableMemory?: string;
150 /** vCPU per instance (e.g. `"1"`). Derived from memory when omitted. */
151 availableCpu?: string;
152 /**
153 * Request timeout in seconds.
154 * @default 60
155 */
156 timeoutSeconds?: number;
157 /** Minimum idle instances. */
158 minInstanceCount?: number;
159 /** Maximum concurrent instances. */
160 maxInstanceCount?: number;
161 /**
162 * Max concurrent requests per instance.
163 * @default 1
164 */
165 maxInstanceRequestConcurrency?: number;
166 /** Runtime environment variables. */
167 environmentVariables?: Record<string, string>;
168 /**
169 * Ingress (`ALLOW_ALL`, `ALLOW_INTERNAL_ONLY`,
170 * `ALLOW_INTERNAL_AND_GCLB`).
171 */
172 ingressSettings?: cloudfunctions.ServiceConfigIngressSettingsEnum | (string & {});
173 /**
174 * Serverless VPC Access connector
175 * (`projects/{project}/locations/{location}/connectors/{connector}`).
176 */
177 vpcConnector?: string;
178 /** Traffic sent through the VPC connector. */
179 vpcConnectorEgressSettings?:
180 | cloudfunctions.ServiceConfigVpcConnectorEgressSettingsEnum
181 | (string & {});
182 /** Runtime service account email. */
183 serviceAccountEmail?: string;
184 /**
185 * Route 100% of traffic to the latest revision.
186 * @default true
187 */
188 allTrafficOnLatestRevision?: boolean;
189 /** Secret Manager values exposed as environment variables. */
190 secretEnvironmentVariables?: SecretEnvVar[];
191 /** Secret Manager values mounted as files. */
192 secretVolumes?: SecretVolume[];
193 /** Direct VPC egress setting. */
194 directVpcEgress?: cloudfunctions.ServiceConfigDirectVpcEgressEnum | (string & {});
195 /** Direct VPC network interfaces. Mutually exclusive with `vpcConnector`. */
196 directVpcNetworkInterface?: DirectVpcNetworkInterface[];
197 };
198
199 export type EventFilter = {
200 /** CloudEvents attribute name. */
201 attribute?: string;
202 /** Value to match. */
203 value?: string;
204 /** Match operator. Only `match-path-pattern` is allowed. */
205 operator?: string;
206 };
207
208 export type EventTrigger = {
209 /**
210 * Event type (e.g. `google.cloud.pubsub.topic.v1.messagePublished`).
211 */
212 eventType?: string;
213 /**
214 * Pub/Sub topic used as the transport
215 * (`projects/{project}/topics/{topic}`).
216 */
217 pubsubTopic?: string;
218 /** Service account email used to invoke the function. */
219 serviceAccountEmail?: string;
220 /** Retry failed deliveries. */
221 retryPolicy?: cloudfunctions.EventTriggerRetryPolicyEnum | (string & {});
222 /** CloudEvents attribute filters. */
223 eventFilters?: EventFilter[];
224 /**
225 * Region that receives events. Defaults to the function location.
226 */
227 triggerRegion?: string;
228 /**
229 * Eventarc channel
230 * (`projects/{project}/locations/{location}/channels/{channel}`) for partner events.
231 */
232 channel?: string;
233 /** 1st gen hostname of the event service (e.g. `storage.googleapis.com`). */
234 service?: string;
235 };
236
237 export type FunctionProps = PlatformProps & {
238 /**
239 * Function id (the `{function}` segment of
240 * `projects/{project}/locations/{location}/functions/{function}`).
241 * If omitted, a unique name is generated from the stack, stage, and
242 * logical id. 4-63 characters, matching `[a-z][a-z0-9-]*`. Immutable — changing
243 * it replaces the function.
244 */
245 functionId?: string;
246 /**
247 * Region (`us-central1`, `europe-west1`, …). Immutable — changing it
248 * replaces the function. `US-CENTRAL1` is accepted and normalized to
249 * `us-central1`.
250 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
251 */
252 location?: string;
253 /**
254 * Generation (`GEN_1` or `GEN_2`). Immutable — changing it replaces the
255 * function.
256 * @default "GEN_2"
257 */
258 environment?: cloudfunctions.Cloudfunctions_FunctionEnvironmentEnum | (string & {});
259 /** User-provided description. */
260 description?: string;
261 /**
262 * User labels. Alchemy ownership labels are merged in automatically.
263 */
264 labels?: Record<string, string>;
265 /**
266 * Cloud KMS key used to encrypt function resources
267 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
268 */
269 kmsKeyName?: string;
270 /** Build step that turns source into a container. */
271 buildConfig?: BuildConfig;
272 /** Cloud Run service settings for the deployed function. */
273 serviceConfig?: ServiceConfig;
274 /** Eventarc trigger. Omit for an HTTPS function. */
275 eventTrigger?: EventTrigger;
276 /**
277 * Entry module of an Effect-native function. Alchemy bundles it for
278 * Node.js, uploads the archive, and serves the impl's `fetch` (and any
279 * event-source listeners) through the Functions Framework. Bindings
280 * attach env + IAM onto the runtime service account. Replaces
281 * `buildConfig.source` / `entryPoint`; `buildConfig.runtime` defaults to
282 * `nodejs22`.
283 */
284 main?: string;
285 /**
286 * Named export to load from `main`.
287 * @default "default"
288 */
289 handler?: string;
290 /**
291 * Additional environment variables for the Effect-native function.
292 * Merged after binding-injected `env`.
293 */
294 env?: Record<string, any>;
295 /** Bundler configuration for `main`. */
296 build?: Bundle.BundleConfig;
297 };
298
299 export type Function = Resource<
300 "GCP.CloudFunctions.Function",
301 FunctionProps,
302 {
303 /** Full resource name `projects/{project}/locations/{location}/functions/{function}`. */
304 name: string;
305 /** Function id (last path segment). */
306 functionId: string;
307 /** Project id. */
308 project: string;
309 /** Location id (`us-central1`, …). */
310 location: string;
311 /** Generation (`GEN_1` or `GEN_2`). */
312 environment: string;
313 /** User-provided description. */
314 description: string | undefined;
315 /** User labels (Alchemy ownership labels stripped). */
316 labels: Record<string, string>;
317 /** HTTPS URL of the function. */
318 url: string | undefined;
319 /** Server-reported state (`ACTIVE`, `DEPLOYING`, `FAILED`, …). */
320 state: string | undefined;
321 /** KMS key used for encryption, if any. */
322 kmsKeyName: string | undefined;
323 /** Runtime (e.g. `nodejs20`). */
324 runtime: string | undefined;
325 /** Source entry point. */
326 entryPoint: string | undefined;
327 /** Cloud Run service resource name. */
328 service: string | undefined;
329 /** Cloud Run service URI. */
330 uri: string | undefined;
331 /** Memory limit. */
332 availableMemory: string | undefined;
333 /** Request timeout in seconds. */
334 timeoutSeconds: number | undefined;
335 /** Minimum instance count. */
336 minInstanceCount: number | undefined;
337 /** Maximum instance count. */
338 maxInstanceCount: number | undefined;
339 /** Runtime service account email. */
340 serviceAccountEmail: string | undefined;
341 /**
342 * Numeric unique ID of the runtime service account: the `sub` of the
343 * Google ID tokens the host fetches. An AWS binding's IAM role trusts
344 * it, so the host reaches AWS without an access key.
345 */
346 serviceAccountUniqueId: string | undefined;
347 /** Eventarc trigger resource name, if any. */
348 trigger: string | undefined;
349 /** RFC3339 creation timestamp. */
350 createTime: string | undefined;
351 /** RFC3339 last-update timestamp. */
352 updateTime: string | undefined;
353 /** True when Alchemy minted the per-host runtime service account. */
354 managedServiceAccount: boolean;
355 /** IAM roles bindings granted to the runtime service account. */
356 iamGrants: AppliedIamGrant[];
357 /** Hash of the bundled `main` program (Effect-native only). */
358 codeHash: string | undefined;
359 },
360 GcpHostBinding,
361 Providers
362 >;
363
364 export type FunctionRuntimeContext = GcpHostRuntimeContext;
365 export type FunctionServices = Credentials | GcpEnvironment | ServerHost;
366 export type FunctionShape = Main<FunctionServices>;
367
368 /**
369 * A Google Cloud Function (2nd gen) that builds source into a Cloud Run
370 * service.
371 *
372 * Changing `functionId`, `location`, or `environment` replaces the function.
373 * Source, runtime, service settings, labels, and event triggers update
374 * in place.
375 *
376 * ### Creating a Function
377 * **Example:** Generated name
378 * ```typescript
379 * const hello = yield* GCP.CloudFunctions.Function("Hello", {
380 * buildConfig: {
381 * runtime: "nodejs20",
382 * entryPoint: "helloHttp",
383 * source: {
384 * storageSource: {
385 * bucket: "my-source",
386 * object: "hello.zip",
387 * },
388 * },
389 * },
390 * });
391 * ```
392 *
393 * **Example:** Explicit id, labels, and memory
394 * ```typescript
395 * const hello = yield* GCP.CloudFunctions.Function("Hello", {
396 * functionId: "order-http",
397 * location: "us-central1",
398 * description: "HTTP handler",
399 * labels: { env: "prod" },
400 * buildConfig: {
401 * runtime: "nodejs20",
402 * entryPoint: "helloHttp",
403 * source: {
404 * storageSource: { bucket: "my-source", object: "hello.zip" },
405 * },
406 * },
407 * serviceConfig: {
408 * availableMemory: "256Mi",
409 * timeoutSeconds: 60,
410 * maxInstanceCount: 10,
411 * },
412 * });
413 * ```
414 *
415 * ### Event Triggers
416 * **Example:** Pub/Sub trigger
417 * ```typescript
418 * const consumer = yield* GCP.CloudFunctions.Function("Consume", {
419 * buildConfig: {
420 * runtime: "nodejs20",
421 * entryPoint: "onMessage",
422 * source: {
423 * storageSource: { bucket: "my-source", object: "consumer.zip" },
424 * },
425 * },
426 * eventTrigger: {
427 * eventType: "google.cloud.pubsub.topic.v1.messagePublished",
428 * pubsubTopic: topic.name,
429 * },
430 * });
431 * ```
432 *
433 * ### Reading a Function
434 * **Example:** Get the live function
435 * ```typescript
436 * const getFunction = yield* GCP.CloudFunctions.GetFunction(hello);
437 * const live = yield* getFunction();
438 * ```
439 *
440 * ### Downloading Source
441 * **Example:** Signed download URL
442 * ```typescript
443 * const download = yield* GCP.CloudFunctions.GenerateDownloadUrl(hello);
444 * const { downloadUrl } = yield* download();
445 * ```
446 *
447 * ### Effect-native Functions
448 * **Example:** HTTP function with a Pub/Sub binding
449 * ```typescript
450 * export class Hello extends GCP.CloudFunctions.Function<Hello>()(
451 * "Hello",
452 * { main: import.meta.url },
453 * Effect.gen(function* () {
454 * const publish = yield* GCP.PubSub.Publish(Events);
455 * return {
456 * fetch: Effect.gen(function* () {
457 * yield* publish({ body: { messages: [{ data: btoa("hi") }] } }).pipe(
458 * Effect.orDie,
459 * );
460 * return HttpServerResponse.text("sent");
461 * }),
462 * };
463 * }).pipe(Effect.provide(GCP.PubSub.PublishHttp)),
464 * ) {}
465 * ```
466 *
467 * **Example:** Consume a topic
468 * ```typescript
469 * yield* GCP.PubSub.consumeTopicMessages(orders, (messages) =>
470 * messages.pipe(Stream.runForEach(({ message }) => Effect.log(message))),
471 * );
472 * // …provided with Effect.provide(GCP.CloudFunctions.TopicEventSource)
473 * ```
474 *
475 * With `main`, Alchemy bundles the program for Node.js (`nodejs22` by
476 * default), uploads it, and serves it through the Functions Framework;
477 * `buildConfig.source` and `entryPoint` are managed for you. The runtime
478 * authenticates to GCP as the function's service account.
479 *
480 * @resource
481 * @category CloudFunctions
482 */
483 export const Function: Platform<Function, FunctionServices, FunctionShape, FunctionRuntimeContext> =
484 Platform("GCP.CloudFunctions.Function", {
485 createRuntimeContext: createGcpHostRuntimeContext("GCP.CloudFunctions.Function") as (
486 id: string,
487 ) => FunctionRuntimeContext,
488 });
489
490 export class FunctionNotResolved extends Data.TaggedError(
491 "GCP.CloudFunctions.FunctionNotResolved",
492 )<{
493 resourceName: string;
494 }> {}
495
496 export class FunctionStillExists extends Data.TaggedError(
497 "GCP.CloudFunctions.FunctionStillExists",
498 )<{
499 resourceName: string;
500 }> {}
501
502 export class FunctionDeployFailed extends Data.TaggedError(
503 "GCP.CloudFunctions.FunctionDeployFailed",
504 )<{
505 resourceName: string;
506 state: string;
507 message: string;
508 }> {}
509
510 const lastSegment = (value: string) => {
511 const trimmed = value.replace(/\/+$/, "");
512 const parts = trimmed.split("/");
513 return parts[parts.length - 1] || trimmed;
514 };
515
516 const normalizeLocation = (location: string | undefined, fallback: string) =>
517 lastSegment(location ?? fallback).toLowerCase();
518
519 const normalizeEnvironment = (environment: string | undefined) => {
520 const value = (environment ?? DEFAULT_ENVIRONMENT).toUpperCase();
521 return value === "ENVIRONMENT_UNSPECIFIED" ? DEFAULT_ENVIRONMENT : value;
522 };
523
524 const resourceName = (project: string, location: string, functionId: string) =>
525 `projects/${project}/locations/${location}/functions/${functionId}`;
526
527 const parseName = (name: string, fallbackLocation: string) => {
528 const parts = name.split("/").filter((part) => part.length > 0);
529 const functionsAt = parts.lastIndexOf("functions");
530 const locationsAt = parts.lastIndexOf("locations");
531 const projectsAt = parts.lastIndexOf("projects");
532 return {
533 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
534 location:
535 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation,
536 functionId:
537 functionsAt >= 0 && parts[functionsAt + 1] ? parts[functionsAt + 1]! : lastSegment(name),
538 };
539 };
540
541 const userLabels = (
542 labels: Record<string, string | undefined> | null | undefined,
543 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
544
545 const toId = (id: string, functionId: string | undefined, existing?: string) =>
546 Effect.gen(function* () {
547 return (
548 functionId ??
549 existing ??
550 (yield* createPhysicalName({
551 id,
552 maxLength: MAX_NAME_LENGTH,
553 lowercase: true,
554 }))
555 );
556 });
557
558 const stable = (value: unknown) => JSON.stringify(value ?? null);
559
560 const envMap = (value: Record<string, string | undefined> | null | undefined) => tagRecord(value);
561
562 const normalizeGeneration = (generation: string | undefined) =>
563 generation === undefined || generation === "0" ? "" : generation;
564
565 const sanitizeSource = (source: Source | undefined): Source | undefined => {
566 if (source === undefined) return undefined;
567 const storage = source.storageSource;
568 if (storage === undefined) return source;
569 const generation = normalizeGeneration(storage.generation);
570 return {
571 ...source,
572 storageSource: {
573 bucket: storage.bucket,
574 object: storage.object,
575 sourceUploadUrl: storage.sourceUploadUrl,
576 generation: generation === "" ? undefined : generation,
577 },
578 };
579 };
580
581 const sameSource = (desired?: Source, observed?: Source) => {
582 const desiredStorage = desired?.storageSource;
583 const observedStorage = observed?.storageSource;
584 const desiredRepo = desired?.repoSource;
585 const observedRepo = observed?.repoSource;
586 const desiredGeneration = normalizeGeneration(desiredStorage?.generation);
587 const observedGeneration = normalizeGeneration(observedStorage?.generation);
588 const generationEqual = desiredGeneration === "" || desiredGeneration === observedGeneration;
589 return (
590 (desired?.gitUri ?? "") === (observed?.gitUri ?? "") &&
591 (desiredStorage?.bucket ?? "") === (observedStorage?.bucket ?? "") &&
592 (desiredStorage?.object ?? "") === (observedStorage?.object ?? "") &&
593 (desiredStorage?.sourceUploadUrl ?? "") === (observedStorage?.sourceUploadUrl ?? "") &&
594 generationEqual &&
595 (desiredRepo?.repoName ?? "") === (observedRepo?.repoName ?? "") &&
596 (desiredRepo?.branchName ?? "") === (observedRepo?.branchName ?? "") &&
597 (desiredRepo?.tagName ?? "") === (observedRepo?.tagName ?? "") &&
598 (desiredRepo?.commitSha ?? "") === (observedRepo?.commitSha ?? "") &&
599 (desiredRepo?.dir ?? "") === (observedRepo?.dir ?? "") &&
600 (desiredRepo?.projectId ?? "") === (observedRepo?.projectId ?? "")
601 );
602 };
603
604 const HOST_TYPE = "GCP.CloudFunctions.Function";
605
606 const toAttrs = (
607 fn: cloudfunctions.Cloudfunctions_Function,
608 project: string,
609 region: string,
610 extras: {
611 iamGrants?: AppliedIamGrant[];
612 codeHash?: string;
613 serviceAccountUniqueId?: string;
614 } = {},
615 ): Function["Attributes"] => {
616 const name = fn.name ?? "";
617 const parsed = parseName(name, region);
618 return {
619 name,
620 functionId: parsed.functionId,
621 project: parsed.project || project,
622 location: parsed.location,
623 environment: normalizeEnvironment(fn.environment),
624 description: fn.description,
625 labels: userLabels(fn.labels),
626 url: fn.url,
627 state: fn.state,
628 kmsKeyName: fn.kmsKeyName,
629 runtime: fn.buildConfig?.runtime,
630 entryPoint: fn.buildConfig?.entryPoint,
631 service: fn.serviceConfig?.service,
632 uri: fn.serviceConfig?.uri,
633 availableMemory: fn.serviceConfig?.availableMemory,
634 timeoutSeconds: fn.serviceConfig?.timeoutSeconds,
635 minInstanceCount: fn.serviceConfig?.minInstanceCount,
636 maxInstanceCount: fn.serviceConfig?.maxInstanceCount,
637 serviceAccountEmail: fn.serviceConfig?.serviceAccountEmail,
638 trigger: fn.eventTrigger?.trigger,
639 createTime: fn.createTime,
640 updateTime: fn.updateTime,
641 managedServiceAccount: isManagedServiceAccount({
642 project: parsed.project || project,
643 hostType: HOST_TYPE,
644 resourceName: name,
645 serviceAccount: fn.serviceConfig?.serviceAccountEmail,
646 }),
647 serviceAccountUniqueId: extras.serviceAccountUniqueId,
648 iamGrants: extras.iamGrants ?? [],
649 codeHash: extras.codeHash,
650 };
651 };
652
653 const getByName = (name: string) =>
654 cloudfunctions
655 .getProjectsLocationsFunctions({ name })
656 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
657
658 const failedMessage = (fn: cloudfunctions.Cloudfunctions_Function) =>
659 fn.stateMessages
660 ?.map((item) => item.message)
661 .filter((item): item is string => typeof item === "string")
662 .join("; ") || "function is in FAILED state";
663
664 /**
665 * A gen2 create/update builds the source and rolls out a Cloud Run revision
666 * (2–4 minutes; slow builds up to ~10). ALREADY_EXISTS (code 6) with
667 * `alreadyExistsOk` and NOT_FOUND (code 5) with `notFoundOk` count as
668 * success (create/delete races).
669 */
670 const waitForOperation = (
671 operation: cloudfunctions.Operation,
672 options?: { notFoundOk?: boolean; alreadyExistsOk?: boolean },
673 ) =>
674 waitForGcpOperation(
675 operation,
676 (name) => cloudfunctions.getProjectsLocationsOperations({ name }),
677 { budget: "15 minutes", interval: "10 seconds" },
678 ).pipe(
679 Effect.catchIf(
680 (error) =>
681 error._tag === "GCP.OperationFailed" &&
682 ((options?.alreadyExistsOk === true && error.code === 6) ||
683 (options?.notFoundOk === true && error.code === 5)),
684 () => Effect.succeed(operation),
685 ),
686 );
687
688 const waitUntilReady = (name: string) =>
689 getByName(name).pipe(
690 Effect.filterOrFail(
691 (fn): fn is cloudfunctions.Cloudfunctions_Function => fn !== undefined,
692 () => new FunctionNotResolved({ resourceName: name }),
693 ),
694 Effect.filterOrFail(
695 (fn) => fn.state !== "FAILED",
696 (fn) =>
697 new FunctionDeployFailed({
698 resourceName: name,
699 state: fn.state ?? "FAILED",
700 message: failedMessage(fn),
701 }),
702 ),
703 Effect.filterOrFail(
704 (fn) => fn.state === "ACTIVE" || fn.state === undefined,
705 () => new FunctionNotResolved({ resourceName: name }),
706 ),
707 Effect.retry({
708 while: (error) => error._tag === "GCP.CloudFunctions.FunctionNotResolved",
709 times: 8,
710 schedule: Schedule.spaced("2 seconds"),
711 }),
712 );
713
714 const waitUntilGone = (name: string) =>
715 getByName(name).pipe(
716 Effect.flatMap((fn) =>
717 fn === undefined ? Effect.void : Effect.fail(new FunctionStillExists({ resourceName: name })),
718 ),
719 Effect.retry({
720 while: (error) => error._tag === "GCP.CloudFunctions.FunctionStillExists",
721 times: 10,
722 schedule: Schedule.spaced("3 seconds"),
723 }),
724 );
725
726 const mergeBuildConfig = (
727 news: BuildConfig | undefined,
728 observed: cloudfunctions.BuildConfig | undefined,
729 ): cloudfunctions.BuildConfig => ({
730 runtime: news?.runtime ?? observed?.runtime ?? DEFAULT_RUNTIME,
731 entryPoint: news?.entryPoint ?? observed?.entryPoint,
732 source: sanitizeSource(news?.source) ?? observed?.source,
733 environmentVariables: news?.environmentVariables ?? envMap(observed?.environmentVariables),
734 workerPool: news?.workerPool ?? observed?.workerPool,
735 dockerRepository: news?.dockerRepository ?? observed?.dockerRepository,
736 serviceAccount: news?.serviceAccount ?? observed?.serviceAccount,
737 });
738
739 const sameBuildConfig = (
740 desired: cloudfunctions.BuildConfig,
741 observed: cloudfunctions.BuildConfig | undefined,
742 ) =>
743 (desired.runtime ?? "") === (observed?.runtime ?? "") &&
744 (desired.entryPoint ?? "") === (observed?.entryPoint ?? "") &&
745 (desired.workerPool ?? "") === (observed?.workerPool ?? "") &&
746 (desired.dockerRepository ?? "") === (observed?.dockerRepository ?? "") &&
747 (desired.serviceAccount ?? "") === (observed?.serviceAccount ?? "") &&
748 deepEqual(envMap(desired.environmentVariables), envMap(observed?.environmentVariables), {
749 stripNullish: true,
750 }) &&
751 sameSource(desired.source, observed?.source);
752
753 const eventTriggerKey = (trigger: EventTrigger | undefined) => {
754 if (trigger === undefined) return "";
755 const filters = [...(trigger.eventFilters ?? [])]
756 .map((filter) => ({
757 attribute: filter.attribute ?? "",
758 value: filter.value ?? "",
759 operator: filter.operator ?? "",
760 }))
761 .sort((a, b) => a.attribute.localeCompare(b.attribute));
762 return stable({
763 eventType: trigger.eventType ?? "",
764 pubsubTopic: trigger.pubsubTopic ?? "",
765 serviceAccountEmail: trigger.serviceAccountEmail ?? "",
766 retryPolicy: trigger.retryPolicy ?? "",
767 triggerRegion: trigger.triggerRegion ?? "",
768 channel: trigger.channel ?? "",
769 service: trigger.service ?? "",
770 filters,
771 });
772 };
773
774 const observedEventTrigger = (
775 trigger: cloudfunctions.EventTrigger | undefined,
776 ): EventTrigger | undefined => {
777 if (trigger === undefined) return undefined;
778 return {
779 eventType: trigger.eventType,
780 pubsubTopic: trigger.pubsubTopic,
781 serviceAccountEmail: trigger.serviceAccountEmail,
782 retryPolicy: trigger.retryPolicy,
783 eventFilters: trigger.eventFilters,
784 triggerRegion: trigger.triggerRegion,
785 channel: trigger.channel,
786 service: trigger.service,
787 };
788 };
789
790 export const FunctionProvider = () =>
791 Provider.succeed(Function, {
792 stables: ["name", "functionId", "project", "location", "environment", "createTime"],
793
794 diff: Effect.fn(function* ({ news, olds, output }) {
795 if (!isResolved(news)) return undefined;
796 const env = yield* GcpEnvironment.current;
797
798 const previousId = olds?.functionId ?? output?.functionId;
799 const nextId = news.functionId ?? previousId;
800 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
801 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
802 const previousEnvironment = normalizeEnvironment(olds?.environment ?? output?.environment);
803 const nextEnvironment = normalizeEnvironment(news.environment ?? output?.environment);
804
805 const replace =
806 (previousId !== undefined &&
807 news.functionId !== undefined &&
808 news.functionId !== previousId) ||
809 previousLocation !== nextLocation ||
810 previousEnvironment !== nextEnvironment;
811
812 if (replace) {
813 return {
814 action: "replace" as const,
815 deleteFirst: previousId !== undefined && nextId === previousId,
816 };
817 }
818 // A code-only change leaves the props untouched; hash the bundle so
819 // it still surfaces as an update.
820 if (output !== undefined && news.main !== undefined) {
821 const source = yield* makeFunctionSource;
822 const { codeHash } = yield* source.bundle({
823 main: news.main,
824 handler: news.handler,
825 build: news.build,
826 isExternal: news.isExternal,
827 });
828 if (codeHash !== output.codeHash) {
829 return { action: "update" as const };
830 }
831 }
832 return undefined;
833 }),
834
835 read: Effect.fn(function* ({ id, olds, output }) {
836 const env = yield* GcpEnvironment.current;
837 const functionId = yield* toId(id, olds?.functionId, output?.functionId);
838 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
839 const name = output?.name ?? resourceName(env.project, location, functionId);
840 const existing = yield* getByName(name);
841 if (existing === undefined) return undefined;
842 const attrs = toAttrs(existing, env.project, env.region, {
843 iamGrants: output?.iamGrants,
844 codeHash: output?.codeHash,
845 serviceAccountUniqueId: output?.serviceAccountUniqueId,
846 });
847 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
848 }),
849
850 list: () =>
851 Effect.gen(function* () {
852 const env = yield* GcpEnvironment.current;
853 return yield* cloudfunctions.listProjectsLocationsFunctions
854 .pages({
855 parent: `projects/${env.project}/locations/-`,
856 pageSize: 1000,
857 })
858 .pipe(
859 Stream.flatMap((page) => Stream.fromIterable(page.functions ?? [])),
860 Stream.filter((fn) =>
861 Object.keys(fn.labels ?? {}).some((key) => key.startsWith("alchemy-")),
862 ),
863 Stream.map((fn) => toAttrs(fn, env.project, env.region)),
864 Stream.runCollect,
865 Effect.map((chunk) => Array.from(chunk)),
866 );
867 }),
868
869 reconcile: Effect.fn(function* ({ id, news, output, bindings }) {
870 const env = yield* GcpEnvironment.current;
871 const functionId = yield* toId(id, news.functionId, output?.functionId);
872 const location = normalizeLocation(news.location ?? output?.location, env.region);
873 const environment = normalizeEnvironment(news.environment ?? output?.environment);
874 const name = resourceName(env.project, location, functionId);
875 const desiredLabels = {
876 ...toLabels(news.labels),
877 ...(yield* createInternalLabels(id)),
878 };
879 const identity = yield* resolveHostIdentity({
880 project: env.project,
881 hostType: HOST_TYPE,
882 resourceName: name,
883 userServiceAccount: news.serviceConfig?.serviceAccountEmail,
884 effectNative: news.main !== undefined,
885 bindings: bindings as ResourceBinding<GcpHostBinding>[],
886 output: output && {
887 ...output,
888 serviceAccount: output.serviceAccountEmail,
889 },
890 });
891 const serviceAccount = identity.serviceAccount;
892 const collected = identity;
893 const runtimeEnv: Record<string, unknown> = {
894 ...(news.main !== undefined ? yield* alchemyRuntimeEnv : {}),
895 ...collected.env,
896 ...news.env,
897 };
898 const serviceConfig = {
899 ...news.serviceConfig,
900 serviceAccountEmail: serviceAccount,
901 environmentVariables: {
902 ...Object.fromEntries(
903 Object.entries(runtimeEnv).map(([key, value]) => [key, packEnvValue(value)]),
904 ),
905 ...news.serviceConfig?.environmentVariables,
906 },
907 };
908
909 let current = yield* getByName(name);
910 if (current?.state === "DELETING") {
911 yield* waitUntilGone(name);
912 current = undefined;
913 }
914
915 // Effect-native: bundle `main`, and upload a new archive only when
916 // the bundle changed (reusing the deployed source otherwise).
917 let codeHash: string | undefined;
918 let buildConfig = news.buildConfig;
919 if (news.main !== undefined) {
920 const source = yield* makeFunctionSource;
921 const bundled = yield* source
922 .bundle({
923 main: news.main,
924 handler: news.handler,
925 build: news.build,
926 isExternal: news.isExternal,
927 })
928 .pipe(Effect.onError(() => identity.cleanup));
929 codeHash = bundled.codeHash;
930 const deployed = current?.buildConfig?.source?.storageSource;
931 const storageSource =
932 current !== undefined && output?.codeHash === codeHash && deployed !== undefined
933 ? deployed
934 : yield* source
935 .upload({
936 parent: `projects/${env.project}/locations/${location}`,
937 files: bundled.files,
938 kmsKeyName: news.kmsKeyName,
939 })
940 .pipe(Effect.onError(() => identity.cleanup));
941 buildConfig = {
942 ...news.buildConfig,
943 runtime: news.buildConfig?.runtime ?? DEFAULT_NODE_RUNTIME,
944 entryPoint: FUNCTION_ENTRY_POINT,
945 source: { storageSource },
946 };
947 }
948
949 if (current === undefined) {
950 const created = yield* cloudfunctions
951 .createProjectsLocationsFunctions({
952 parent: `projects/${env.project}/locations/${location}`,
953 functionId,
954 body: {
955 name,
956 description: news.description,
957 labels: desiredLabels,
958 kmsKeyName: news.kmsKeyName,
959 environment,
960 buildConfig: mergeBuildConfig(buildConfig, undefined),
961 serviceConfig,
962 eventTrigger: news.eventTrigger,
963 },
964 })
965 .pipe(
966 retryActAs,
967 Effect.catchTag("Conflict", () => Effect.undefined),
968 Effect.onError(() => identity.cleanup),
969 );
970 if (created !== undefined) {
971 yield* waitForOperation(created, { alreadyExistsOk: true });
972 }
973 current = yield* waitUntilReady(name);
974 }
975 if (current === undefined) {
976 return yield* new FunctionNotResolved({ resourceName: name });
977 }
978
979 const observedLabels = tagRecord(current.labels);
980 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
981 const labelsChanged = upsert.length > 0 || removed.length > 0;
982 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
983 const kmsChanged =
984 news.kmsKeyName !== undefined && (current.kmsKeyName ?? "") !== news.kmsKeyName;
985
986 const desiredBuild =
987 buildConfig !== undefined ? mergeBuildConfig(buildConfig, current.buildConfig) : undefined;
988 const buildConfigChanged =
989 desiredBuild !== undefined && !sameBuildConfig(desiredBuild, current.buildConfig);
990
991 const desiredService = serviceConfig;
992 const serviceConfigChanged =
993 desiredService !== undefined &&
994 ((desiredService.availableMemory !== undefined &&
995 (current.serviceConfig?.availableMemory ?? "") !== desiredService.availableMemory) ||
996 (desiredService.availableCpu !== undefined &&
997 (current.serviceConfig?.availableCpu ?? "") !== desiredService.availableCpu) ||
998 (desiredService.timeoutSeconds !== undefined &&
999 (current.serviceConfig?.timeoutSeconds ?? 60) !== desiredService.timeoutSeconds) ||
1000 (desiredService.minInstanceCount !== undefined &&
1001 (current.serviceConfig?.minInstanceCount ?? 0) !== desiredService.minInstanceCount) ||
1002 (desiredService.maxInstanceCount !== undefined &&
1003 (current.serviceConfig?.maxInstanceCount ?? 0) !== desiredService.maxInstanceCount) ||
1004 (desiredService.maxInstanceRequestConcurrency !== undefined &&
1005 (current.serviceConfig?.maxInstanceRequestConcurrency ?? 1) !==
1006 desiredService.maxInstanceRequestConcurrency) ||
1007 (desiredService.environmentVariables !== undefined &&
1008 !deepEqual(
1009 envMap(desiredService.environmentVariables),
1010 envMap(current.serviceConfig?.environmentVariables),
1011 { stripNullish: true },
1012 )) ||
1013 (desiredService.ingressSettings !== undefined &&
1014 (current.serviceConfig?.ingressSettings ?? "") !== desiredService.ingressSettings) ||
1015 (desiredService.vpcConnector !== undefined &&
1016 (current.serviceConfig?.vpcConnector ?? "") !== desiredService.vpcConnector) ||
1017 (desiredService.vpcConnectorEgressSettings !== undefined &&
1018 (current.serviceConfig?.vpcConnectorEgressSettings ?? "") !==
1019 desiredService.vpcConnectorEgressSettings) ||
1020 (desiredService.serviceAccountEmail !== undefined &&
1021 (current.serviceConfig?.serviceAccountEmail ?? "") !==
1022 desiredService.serviceAccountEmail) ||
1023 (desiredService.allTrafficOnLatestRevision !== undefined &&
1024 (current.serviceConfig?.allTrafficOnLatestRevision ?? true) !==
1025 desiredService.allTrafficOnLatestRevision) ||
1026 (desiredService.secretEnvironmentVariables !== undefined &&
1027 stable(desiredService.secretEnvironmentVariables) !==
1028 stable(current.serviceConfig?.secretEnvironmentVariables)) ||
1029 (desiredService.secretVolumes !== undefined &&
1030 stable(desiredService.secretVolumes) !==
1031 stable(current.serviceConfig?.secretVolumes)) ||
1032 (desiredService.directVpcEgress !== undefined &&
1033 (current.serviceConfig?.directVpcEgress ?? "") !== desiredService.directVpcEgress) ||
1034 (desiredService.directVpcNetworkInterface !== undefined &&
1035 stable(desiredService.directVpcNetworkInterface) !==
1036 stable(current.serviceConfig?.directVpcNetworkInterface)));
1037
1038 const eventTriggerChanged =
1039 news.eventTrigger !== undefined &&
1040 eventTriggerKey(news.eventTrigger) !==
1041 eventTriggerKey(observedEventTrigger(current.eventTrigger));
1042
1043 if (
1044 labelsChanged ||
1045 descriptionChanged ||
1046 kmsChanged ||
1047 buildConfigChanged ||
1048 serviceConfigChanged ||
1049 eventTriggerChanged
1050 ) {
1051 const updateMask = [
1052 labelsChanged ? "labels" : undefined,
1053 descriptionChanged ? "description" : undefined,
1054 kmsChanged ? "kmsKeyName" : undefined,
1055 buildConfigChanged ? "buildConfig" : undefined,
1056 serviceConfigChanged ? "serviceConfig" : undefined,
1057 eventTriggerChanged ? "eventTrigger" : undefined,
1058 ].filter((field): field is string => field !== undefined);
1059
1060 const operation = yield* cloudfunctions.patchProjectsLocationsFunctions({
1061 name,
1062 updateMask: updateMask.join(","),
1063 body: {
1064 name,
1065 labels: desiredLabels,
1066 description: news.description,
1067 kmsKeyName: news.kmsKeyName ?? current.kmsKeyName,
1068 buildConfig: desiredBuild ?? current.buildConfig,
1069 serviceConfig: {
1070 ...current.serviceConfig,
1071 ...desiredService,
1072 service: undefined,
1073 uri: undefined,
1074 revision: undefined,
1075 },
1076 eventTrigger: news.eventTrigger ?? current.eventTrigger,
1077 },
1078 });
1079 yield* waitForOperation(operation);
1080 current = yield* waitUntilReady(name);
1081 }
1082 if (current === undefined) {
1083 return yield* new FunctionNotResolved({ resourceName: name });
1084 }
1085
1086 return toAttrs(current, env.project, env.region, {
1087 iamGrants: identity.grants,
1088 codeHash,
1089 serviceAccountUniqueId: identity.uniqueId,
1090 });
1091 }),
1092
1093 delete: Effect.fn(function* ({ output }) {
1094 const operation = yield* cloudfunctions
1095 .deleteProjectsLocationsFunctions({ name: output.name })
1096 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
1097 if (operation !== undefined) {
1098 yield* waitForOperation(operation, { notFoundOk: true });
1099 }
1100 yield* waitUntilGone(output.name);
1101 yield* releaseHostIdentity({
1102 ...output,
1103 serviceAccount: output.serviceAccountEmail,
1104 });
1105 }),
1106 });