@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, 06:00 UTC

main
ts 87 lines 2.97 KB
Raw
1 import type * as iam from "@distilled.cloud/aws/iam";
2 import { toWireDays, toWireSeconds } from "alchemy/Util/Duration";
3 import * as Redacted from "effect/Redacted";
4 import type { PolicyDocument } from "./Policy.ts";
5
6 /**
7 * Convert an optional `Duration.Input` prop to whole wire seconds.
8 *
9 * @deprecated Import {@link toWireSeconds} from `Util/Duration.ts` directly.
10 * Kept as an alias because several other AWS services still import it from
11 * here.
12 */
13 export const durationToSeconds = toWireSeconds;
14
15 /**
16 * Convert an optional `Duration.Input` prop to whole wire days.
17 *
18 * @deprecated Import {@link toWireDays} from `Util/Duration.ts` directly.
19 * Kept as an alias because several other AWS services still import it from
20 * here.
21 */
22 export const durationToDays = toWireDays;
23
24 export const toTagRecord = (
25 tags: Array<{ Key?: string; Value?: string }> | undefined,
26 ): Record<string, string> =>
27 Object.fromEntries(
28 (tags ?? [])
29 .filter(
30 (tag): tag is { Key: string; Value: string } =>
31 typeof tag.Key === "string" && typeof tag.Value === "string",
32 )
33 .map((tag) => [tag.Key, tag.Value]),
34 );
35
36 const decodePolicyString = (value: string) => {
37 try {
38 return decodeURIComponent(value);
39 } catch {
40 return value;
41 }
42 };
43
44 export const parsePolicyDocument = (value: string | undefined): PolicyDocument | undefined => {
45 if (!value) {
46 return undefined;
47 }
48
49 const decoded = decodePolicyString(value);
50 return JSON.parse(decoded) as PolicyDocument;
51 };
52
53 export const stringifyPolicyDocument = (value: PolicyDocument) => JSON.stringify(value);
54
55 export const normalizeIamPath = (value: string | undefined) => {
56 const path = value ?? "/";
57 const withLeadingSlash = path.startsWith("/") ? path : `/${path}`;
58 return withLeadingSlash.endsWith("/") ? withLeadingSlash : `${withLeadingSlash}/`;
59 };
60
61 export const policyArnFromParts = ({
62 accountId,
63 path,
64 policyName,
65 }: {
66 accountId: string;
67 path: string | undefined;
68 policyName: string;
69 }) => `arn:aws:iam::${accountId}:policy${normalizeIamPath(path)}${policyName}`;
70
71 export const oldestNondefaultPolicyVersion = (versions: iam.PolicyVersion[] | undefined) =>
72 [...(versions ?? [])]
73 .filter((version) => !version.IsDefaultVersion && version.VersionId)
74 .sort((a, b) => (a.CreateDate?.getTime() ?? 0) - (b.CreateDate?.getTime() ?? 0))[0];
75
76 export const toRedactedString = (
77 value: string | Redacted.Redacted<string> | undefined,
78 ): Redacted.Redacted<string> | undefined =>
79 value === undefined ? undefined : typeof value === "string" ? Redacted.make(value) : value;
80
81 export const toRedactedBytes = (
82 value: Uint8Array<ArrayBufferLike> | Redacted.Redacted<Uint8Array<ArrayBufferLike>> | undefined,
83 ): Redacted.Redacted<Uint8Array<ArrayBufferLike>> | undefined =>
84 value === undefined ? undefined : value instanceof Uint8Array ? Redacted.make(value) : value;
85
86 export const unwrapRedactedString = (value: string | Redacted.Redacted<string>): string =>
87 typeof value === "string" ? value : Redacted.value(value);