1import * as dax from "@distilled.cloud/aws/dax";
2import * as Effect from "effect/Effect";
3
4/**
5 * Convert DAX's `[{ Key, Value }]` tag list into a plain record, dropping any
6 * entry missing a key or value.
7 */
8export const toTagRecord = (
9 tags: Array<{ Key?: string; Value?: string }> | undefined,
10): Record<string, string> =>
11 Object.fromEntries(
12 (tags ?? [])
13 .filter(
14 (tag): tag is { Key: string; Value: string } =>
15 typeof tag.Key === "string" && typeof tag.Value === "string",
16 )
17 .map((tag) => [tag.Key, tag.Value]),
18 );
19
20/**
21 * Read the observed tags for a DAX cluster by ARN. A cluster that is
22 * mid-transition (creating/modifying) transiently rejects `listTags` with
23 * InvalidClusterStateFault; treat any failure as "no observed tags" so tag
24 * reconciliation still runs on the next pass.
25 */
26export const readDaxTags = Effect.fn(function* (arn: string) {
27 const response = yield* dax
28 .listTags({ ResourceName: arn })
29 .pipe(Effect.catch(() => Effect.succeed(undefined)));
30 return toTagRecord(response?.Tags);
31});
32
33/** True when two string sets are equal ignoring order and duplicates. */
34export const sameStringSet = (
35 a: readonly string[] | undefined,
36 b: readonly string[] | undefined,
37): boolean => {
38 const left = [...new Set(a ?? [])].sort();
39 const right = [...new Set(b ?? [])].sort();
40 return left.length === right.length && left.every((v, i) => v === right[i]);
41};