@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
ts 44 lines 1.69 KB
Raw
1 import crypto from "node:crypto";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import * as accounts from "@distilled.cloud/cloudflare/accounts";
4 import * as user from "@distilled.cloud/cloudflare/user";
5 import * as Effect from "effect/Effect";
6 import * as Redacted from "effect/Redacted";
7 import * as Schedule from "effect/Schedule";
8
9 export { PageView, PageViews, pageViewFields } from "./pipelines-stream.ts";
10
11 /** The active API token (live tests run with an apiToken profile). */
12 export const apiToken = Effect.gen(function* () {
13 const creds = yield* yield* CloudflareEnvironment;
14 if (creds.type !== "apiToken") {
15 return yield* Effect.die(new Error("Basin Pipelines tests require an apiToken profile"));
16 }
17 return creds.apiToken;
18 });
19
20 /**
21 * R2 S3-compatible credentials derived from the active API token: the
22 * access key id is the token id, the secret the SHA-256 hex of its value.
23 */
24 export const r2Credentials = Effect.gen(function* () {
25 const token = yield* apiToken;
26 const { accountId } = yield* yield* CloudflareEnvironment;
27 // Account-owned tokens (`cfat_…`) verify against the account route.
28 const verified = yield* Redacted.value(token).startsWith("cfat_")
29 ? accounts.verifyToken({ accountId })
30 : user.verifyToken({}).pipe(
31 Effect.retry({
32 while: (e) => e._tag === "Forbidden",
33 schedule: Schedule.exponential("500 millis"),
34 times: 8,
35 }),
36 );
37 const secretAccessKey = yield* Effect.sync(() =>
38 crypto.createHash("sha256").update(Redacted.value(token)).digest("hex"),
39 );
40 return {
41 accessKeyId: Redacted.make(verified.id),
42 secretAccessKey: Redacted.make(secretAccessKey),
43 };
44 });