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, 06:00 UTC
| 1 | import * as intel from "@distilled.cloud/cloudflare/intel"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Predicate from "effect/Predicate"; |
| 7 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | |
| 10 | const TypeId = "Cloudflare.Intel.IndicatorFeedPermission" as const; |
| 11 | type TypeId = typeof TypeId; |
| 12 | |
| 13 | export interface IndicatorFeedPermissionProps { |
| 14 | /** |
| 15 | * The ID of the indicator feed to grant access to — e.g. `feed.feedId`. |
| 16 | * Immutable — changing it triggers a replacement. |
| 17 | */ |
| 18 | feedId: number; |
| 19 | /** |
| 20 | * The Cloudflare account tag of the consumer account being granted |
| 21 | * access to the feed. Immutable — changing it triggers a replacement. |
| 22 | */ |
| 23 | accountTag: string; |
| 24 | } |
| 25 | |
| 26 | export interface IndicatorFeedPermissionAttributes { |
| 27 | /** The ID of the indicator feed the grant is on. */ |
| 28 | feedId: number; |
| 29 | /** The Cloudflare account tag of the consumer account. */ |
| 30 | accountTag: string; |
| 31 | /** The Cloudflare account that owns the feed (the granting account). */ |
| 32 | accountId: string; |
| 33 | } |
| 34 | |
| 35 | export type IndicatorFeedPermission = Resource< |
| 36 | TypeId, |
| 37 | IndicatorFeedPermissionProps, |
| 38 | IndicatorFeedPermissionAttributes, |
| 39 | never, |
| 40 | Providers |
| 41 | >; |
| 42 | |
| 43 | /** |
| 44 | * A permission grant on a Cloudflare custom Indicator Feed, giving another |
| 45 | * Cloudflare account access to consume the feed. |
| 46 | * |
| 47 | * This is an existence-only resource: it has no mutable aspects beyond its |
| 48 | * identity (feed + consumer account tag), so changing either property |
| 49 | * triggers a replacement. Cloudflare's add/remove endpoints are idempotent |
| 50 | * PUTs, so reconcile and delete are simple ensure/remove calls. |
| 51 | * |
| 52 | * Cloudflare exposes no API to list the grantees of a feed from the |
| 53 | * provider side (the permissions "view" endpoint lists feeds the *calling* |
| 54 | * account can consume), so `read` reports the last known state. |
| 55 | * ### Granting Access |
| 56 | * **Example:** Grant a consumer account access to a feed |
| 57 | * ```typescript |
| 58 | * const feed = yield* Cloudflare.Intel.IndicatorFeed("threat-feed", { |
| 59 | * description: "Indicators observed by our honeypots", |
| 60 | * }); |
| 61 | * |
| 62 | * yield* Cloudflare.Intel.IndicatorFeedPermission("partner-access", { |
| 63 | * feedId: feed.feedId, |
| 64 | * accountTag: "023e105f4ecef8ad9ca31a8372d0c353", |
| 65 | * }); |
| 66 | * ``` |
| 67 | * |
| 68 | * @see https://developers.cloudflare.com/security-center/indicator-feeds/ |
| 69 | * |
| 70 | * @resource |
| 71 | * @product Intel |
| 72 | * @category Observability & Analytics |
| 73 | */ |
| 74 | export const IndicatorFeedPermission = Resource<IndicatorFeedPermission>(TypeId); |
| 75 | |
| 76 | /** |
| 77 | * Returns true if the given value is an IndicatorFeedPermission resource. |
| 78 | */ |
| 79 | export const isIndicatorFeedPermission = (value: unknown): value is IndicatorFeedPermission => |
| 80 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 81 | |
| 82 | export const IndicatorFeedPermissionProvider = () => |
| 83 | Provider.succeed(IndicatorFeedPermission, { |
| 84 | stables: ["feedId", "accountTag", "accountId"], |
| 85 | |
| 86 | // Non-listable: a grant is keyed by {feedId, accountTag} from the |
| 87 | // granting account's side, and Cloudflare exposes no provider-side API |
| 88 | // to enumerate a feed's grantees. The only related collection op |
| 89 | // (`listIndicatorFeedPermissions`, the `/permissions/view` endpoint) |
| 90 | // returns feeds the *calling* account can consume — feed metadata |
| 91 | // (id/name/flags) with no `accountTag` — which is the inverse |
| 92 | // relationship and cannot reconstruct the `read` Attributes shape. |
| 93 | list: () => Effect.succeed<IndicatorFeedPermissionAttributes[]>([]), |
| 94 | |
| 95 | diff: Effect.fn(function* ({ olds, news }) { |
| 96 | if (!isResolved(news)) return undefined; |
| 97 | // Identity change — both props are the resource's identity, so any |
| 98 | // change is a replacement. Compare only once both sides are concrete. |
| 99 | if (typeof olds?.feedId === "number" && olds.feedId !== news.feedId) { |
| 100 | return { action: "replace" } as const; |
| 101 | } |
| 102 | if (typeof olds?.accountTag === "string" && olds.accountTag !== news.accountTag) { |
| 103 | return { action: "replace" } as const; |
| 104 | } |
| 105 | return undefined; |
| 106 | }), |
| 107 | |
| 108 | read: Effect.fn(function* ({ output }) { |
| 109 | // Cloudflare exposes no provider-side API to list a feed's grantees |
| 110 | // (the "view" endpoint is consumer-side), so the recorded output is |
| 111 | // the best observation available. The identity is fully |
| 112 | // user-specified and the add PUT is idempotent, so reconcile |
| 113 | // re-asserts the grant regardless. |
| 114 | return output; |
| 115 | }), |
| 116 | |
| 117 | reconcile: Effect.fn(function* ({ news, output }) { |
| 118 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 119 | // Inputs have been resolved to concrete values by Plan. |
| 120 | const feedId = news.feedId as number; |
| 121 | const accountTag = news.accountTag; |
| 122 | |
| 123 | // Ensure — the add endpoint is an idempotent PUT: granting an |
| 124 | // already-granted account succeeds, so there is no race to tolerate |
| 125 | // and no observe step possible (no provider-side list API). |
| 126 | yield* intel.createIndicatorFeedPermission({ |
| 127 | accountId: output?.accountId ?? accountId, |
| 128 | feedId, |
| 129 | accountTag, |
| 130 | }); |
| 131 | |
| 132 | return { |
| 133 | feedId, |
| 134 | accountTag, |
| 135 | accountId: output?.accountId ?? accountId, |
| 136 | }; |
| 137 | }), |
| 138 | |
| 139 | delete: Effect.fn(function* ({ output }) { |
| 140 | // Idempotent PUT — removing a grant that is already gone succeeds. |
| 141 | // A feed deleted out-of-band (or never visible) surfaces as the |
| 142 | // typed IndicatorFeedNotFound, which also means the grant is gone. |
| 143 | yield* intel |
| 144 | .deleteIndicatorFeedPermission({ |
| 145 | accountId: output.accountId, |
| 146 | feedId: output.feedId, |
| 147 | accountTag: output.accountTag, |
| 148 | }) |
| 149 | .pipe(Effect.catchTag("IndicatorFeedNotFound", () => Effect.void)); |
| 150 | }), |
| 151 | }); |