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

main
ts 110 lines 4.27 KB
Raw
1 import * as AwsCredentials from "@distilled.cloud/aws/Credentials";
2 import * as AwsEndpoint from "@distilled.cloud/aws/Endpoint";
3 import type { RegionName } from "@distilled.cloud/aws/Region";
4 import * as s3 from "@distilled.cloud/aws/s3";
5 import * as accounts from "@distilled.cloud/cloudflare/accounts";
6 import * as user from "@distilled.cloud/cloudflare/user";
7 import { sha256 } from "alchemy/Util/sha256";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 import * as Option from "effect/Option";
11 import * as Redacted from "effect/Redacted";
12 import * as Stream from "effect/Stream";
13 import { Credentials } from "../../Credentials.ts";
14 import type { Bucket } from "../Bucket.ts";
15 import { liveEndpoint } from "../S3CredentialsBinding.ts";
16
17 /**
18 * The outcome of {@link abortMultipartUploads}: how many uploads were
19 * aborted, or why they could not be.
20 */
21 export type AbortMultipartUploadsResult = { aborted: number } | { skipped: string };
22
23 /**
24 * Best-effort: abort every incomplete multipart upload in a bucket.
25 *
26 * R2 refuses to delete a bucket that holds incomplete multipart uploads, and
27 * the Cloudflare REST API cannot list or abort them — only R2's S3 API can.
28 * S3 credentials derive from an API token (access key id = token id, secret
29 * = SHA-256 of the token value), so this only works when alchemy is
30 * authenticated with an API token that may write the bucket's objects. It
31 * never creates a token: with OAuth or a global API key, or when the token
32 * lacks the permission, it reports why it skipped instead of failing.
33 */
34 export const abortMultipartUploads = Effect.fn("R2.abortMultipartUploads")(
35 function* (accountId: string, bucketName: string, jurisdiction: Bucket.Jurisdiction) {
36 const resolved = yield* yield* Credentials;
37 if (resolved.type !== "apiToken") {
38 return {
39 skipped: `alchemy is authenticated with ${resolved.type}, not an API token`,
40 } as AbortMultipartUploadsResult;
41 }
42 // Account-owned and user-owned tokens verify on different routes.
43 const { id: accessKeyId } = yield* accounts
44 .verifyToken({ accountId })
45 .pipe(Effect.catch(() => user.verifyToken({})));
46 const secretAccessKey = yield* resolved.apiToken.pipe(Redacted.value, sha256);
47 const r2S3 = Layer.mergeAll(
48 Layer.succeed(
49 AwsCredentials.Credentials,
50 Effect.succeed({
51 accessKeyId: Redacted.make(accessKeyId),
52 secretAccessKey: Redacted.make(secretAccessKey),
53 sessionToken: undefined,
54 // R2 signs with the `auto` region.
55 region: "auto" as RegionName,
56 }),
57 ),
58 AwsEndpoint.of(liveEndpoint(accountId, jurisdiction)),
59 );
60
61 const aborted = yield* Stream.paginate(
62 {
63 keyMarker: undefined as string | undefined,
64 uploadIdMarker: undefined as string | undefined,
65 },
66 (markers) =>
67 s3
68 .listMultipartUploads({
69 Bucket: bucketName,
70 KeyMarker: markers.keyMarker,
71 UploadIdMarker: markers.uploadIdMarker,
72 })
73 .pipe(
74 Effect.map(
75 (page) =>
76 [
77 page.Uploads ?? [],
78 page.IsTruncated && (page.NextKeyMarker || page.NextUploadIdMarker)
79 ? Option.some({
80 keyMarker: page.NextKeyMarker,
81 uploadIdMarker: page.NextUploadIdMarker,
82 })
83 : Option.none(),
84 ] as const,
85 ),
86 ),
87 ).pipe(
88 Stream.filter(
89 (upload): upload is s3.MultipartUpload & { Key: string; UploadId: string } =>
90 upload.Key !== undefined && upload.UploadId !== undefined,
91 ),
92 Stream.mapEffect(
93 (upload) =>
94 s3
95 .abortMultipartUpload({
96 Bucket: bucketName,
97 Key: upload.Key,
98 UploadId: upload.UploadId,
99 })
100 // Completed or aborted by someone else in the meantime.
101 .pipe(Effect.catchTag("NoSuchUpload", () => Effect.void)),
102 { concurrency: 10 },
103 ),
104 Stream.runCount,
105 Effect.provide(r2S3),
106 );
107 return { aborted } as AbortMultipartUploadsResult;
108 },
109 Effect.catch((error) => Effect.succeed<AbortMultipartUploadsResult>({ skipped: error.message })),
110 );