import * as mediapackagev2 from "@distilled.cloud/aws/mediapackagev2";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import { createPhysicalName } from "alchemy/PhysicalName";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import type { Providers } from "../Providers.ts";
import {
deleteChannelWithEndpoints,
listGroupChannels,
retryWhileMpConflict,
syncMpTags,
toMpTagRecord,
} from "./internal.ts";
export interface ChannelGroupProps {
/**
* Name of the channel group. Must be unique within the account/region and
* match `^[a-zA-Z0-9_-]+
.repos/alchemy-effect/packages/aws/src/MediaPackageV2/ChannelGroup.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated.
* Changing the name replaces the channel group.
*/
channelGroupName?: string;
/**
* Optional description of the channel group (up to 1024 characters).
*/
description?: string;
/**
* User-defined tags for the channel group. Merged with internal Alchemy
* tags.
*/
tags?: Record<string, string>;
}
export interface ChannelGroup extends Resource<
"AWS.MediaPackageV2.ChannelGroup",
ChannelGroupProps,
{
/** Name of the channel group. */
channelGroupName: string;
/** ARN of the channel group. */
channelGroupArn: string;
/** Shared egress domain that serves all origin endpoints in the group. */
egressDomain: string;
},
never,
Providers
> {}
/**
* An AWS Elemental MediaPackage v2 channel group — the top-level container
* for channels and origin endpoints. All channels and origin endpoints in a
* group share one egress domain, giving downstream players and CDNs
* predictable URLs for stream delivery.
*
* ### Creating a Channel Group
* **Example:** Basic Channel Group
* ```typescript
* import * as MediaPackageV2 from "@alchemy.run/aws/MediaPackageV2";
*
* const group = yield* MediaPackageV2.ChannelGroup("Live");
* ```
*
* **Example:** Channel Group with Description and Tags
* ```typescript
* const group = yield* MediaPackageV2.ChannelGroup("Live", {
* description: "Live sports streams",
* tags: { team: "media" },
* });
* ```
*
* ### Egress Domain
* **Example:** Use the shared egress domain
* ```typescript
* const group = yield* MediaPackageV2.ChannelGroup("Live");
* // e.g. abcde.egress.xyz.mediapackagev2.us-east-1.amazonaws.com
* const domain = group.egressDomain;
* ```
*
* @resource
*/
export const ChannelGroup = Resource<ChannelGroup>("AWS.MediaPackageV2.ChannelGroup");
export const ChannelGroupProvider = () =>
Provider.effect(
ChannelGroup,
Effect.gen(function* () {
const createName = Effect.fn(function* (id: string, props: ChannelGroupProps) {
return props.channelGroupName ?? (yield* createPhysicalName({ id, maxLength: 256 }));
});
const toAttrs = (group: { ChannelGroupName: string; Arn: string; EgressDomain: string }) => ({
channelGroupName: group.ChannelGroupName,
channelGroupArn: group.Arn,
egressDomain: group.EgressDomain,
});
/** Get a channel group by name; typed not-found → undefined. */
const getGroup = Effect.fn(function* (channelGroupName: string) {
return yield* mediapackagev2
.getChannelGroup({ ChannelGroupName: channelGroupName })
.pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
});
return {
stables: ["channelGroupName", "channelGroupArn", "egressDomain"],
diff: Effect.fn(function* ({ id, olds, news }) {
if (!isResolved(news)) return undefined;
const oldName = yield* createName(id, olds);
const newName = yield* createName(id, news);
// The name is the group's identity; a change means a new group.
if (oldName !== newName) return { action: "replace" } as const;
}),
read: Effect.fn(function* ({ id, olds, output }) {
const name = output?.channelGroupName ?? (yield* createName(id, olds ?? {}));
const group = yield* getGroup(name);
if (group === undefined) return undefined;
const attrs = toAttrs(group);
return (yield* hasAlchemyTags(id, toMpTagRecord(group.Tags))) ? attrs : Unowned(attrs);
}),
reconcile: Effect.fn(function* ({ id, news, output, session }) {
const internalTags = yield* createInternalTags(id);
const desiredTags = { ...internalTags, ...news.tags };
const name = output?.channelGroupName ?? (yield* createName(id, news));
// 1. Observe — cloud state is authoritative; output is an id cache.
let group = yield* getGroup(name);
// 2. Ensure — create if missing; a Conflict means a peer created it
// concurrently, so fall through to observing the winner.
if (group === undefined) {
group = yield* mediapackagev2
.createChannelGroup({
ChannelGroupName: name,
Description: news.description,
Tags: desiredTags,
})
.pipe(
Effect.catchTag("ConflictException", () =>
mediapackagev2.getChannelGroup({ ChannelGroupName: name }),
),
);
} else if ((news.description ?? "") !== (group.Description ?? "")) {
// 3. Sync — the description is the only mutable field.
group = yield* mediapackagev2.updateChannelGroup({
ChannelGroupName: name,
Description: news.description,
});
}
// 3b. Sync tags — diff against OBSERVED cloud tags.
yield* syncMpTags(group.Arn, toMpTagRecord(group.Tags), desiredTags);
yield* session.note(name);
return toAttrs(group);
}),
delete: Effect.fn(function* ({ output }) {
const channelGroupName = output.channelGroupName;
// Reap child channels (and their origin endpoints) first. A normal
// stack destroy deletes children before the group, so this observes
// nothing — but an orphan sweep can target a group whose children
// it never enumerated, and deleting the group would then Conflict
// until the retry budget ran out and leak the group.
const channels = yield* listGroupChannels(channelGroupName);
yield* Effect.forEach(
channels,
(channel) => deleteChannelWithEndpoints(channelGroupName, channel.ChannelName),
{ concurrency: 5, discard: true },
);
// MediaPackage v2 deletes are idempotent (deleting a missing group
// succeeds), but the group transiently rejects deletion with a
// Conflict while its just-deleted channels are cleaned up.
yield* mediapackagev2
.deleteChannelGroup({ ChannelGroupName: channelGroupName })
.pipe(retryWhileMpConflict);
}),
list: () =>
Effect.gen(function* () {
const items = yield* mediapackagev2.listChannelGroups.pages({}).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Items ?? [])),
);
// The list shape omits the egress domain, so hydrate each item
// via get; a group can vanish between enumeration and hydration.
const groups = yield* Effect.forEach(
items,
(item) =>
mediapackagev2.getChannelGroup({ ChannelGroupName: item.ChannelGroupName }).pipe(
Effect.map(toAttrs),
Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
),
{ concurrency: 5 },
);
return groups.filter((g): g is ChannelGroup["Attributes"] => g !== undefined);
}),
};
}),
);
.repos/alchemy-effect/packages/aws/src/MediaPackageV2/ChannelGroup.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated.\n * Changing the name replaces the channel group.\n */\n channelGroupName?: string;\n /**\n * Optional description of the channel group (up to 1024 characters).\n */\n description?: string;\n /**\n * User-defined tags for the channel group. Merged with internal Alchemy\n * tags.\n */\n tags?: Record\u003cstring, string>;\n}\n\nexport interface ChannelGroup extends Resource\u003c\n \"AWS.MediaPackageV2.ChannelGroup\",\n ChannelGroupProps,\n {\n /** Name of the channel group. */\n channelGroupName: string;\n /** ARN of the channel group. */\n channelGroupArn: string;\n /** Shared egress domain that serves all origin endpoints in the group. */\n egressDomain: string;\n },\n never,\n Providers\n> {}\n\n/**\n * An AWS Elemental MediaPackage v2 channel group — the top-level container\n * for channels and origin endpoints. All channels and origin endpoints in a\n * group share one egress domain, giving downstream players and CDNs\n * predictable URLs for stream delivery.\n *\n * ### Creating a Channel Group\n * **Example:** Basic Channel Group\n * ```typescript\n * import * as MediaPackageV2 from \"@alchemy.run/aws/MediaPackageV2\";\n *\n * const group = yield* MediaPackageV2.ChannelGroup(\"Live\");\n * ```\n *\n * **Example:** Channel Group with Description and Tags\n * ```typescript\n * const group = yield* MediaPackageV2.ChannelGroup(\"Live\", {\n * description: \"Live sports streams\",\n * tags: { team: \"media\" },\n * });\n * ```\n *\n * ### Egress Domain\n * **Example:** Use the shared egress domain\n * ```typescript\n * const group = yield* MediaPackageV2.ChannelGroup(\"Live\");\n * // e.g. abcde.egress.xyz.mediapackagev2.us-east-1.amazonaws.com\n * const domain = group.egressDomain;\n * ```\n *\n * @resource\n */\nexport const ChannelGroup = Resource\u003cChannelGroup>(\"AWS.MediaPackageV2.ChannelGroup\");\n\nexport const ChannelGroupProvider = () =>\n Provider.effect(\n ChannelGroup,\n Effect.gen(function* () {\n const createName = Effect.fn(function* (id: string, props: ChannelGroupProps) {\n return props.channelGroupName ?? (yield* createPhysicalName({ id, maxLength: 256 }));\n });\n\n const toAttrs = (group: { ChannelGroupName: string; Arn: string; EgressDomain: string }) => ({\n channelGroupName: group.ChannelGroupName,\n channelGroupArn: group.Arn,\n egressDomain: group.EgressDomain,\n });\n\n /** Get a channel group by name; typed not-found → undefined. */\n const getGroup = Effect.fn(function* (channelGroupName: string) {\n return yield* mediapackagev2\n .getChannelGroup({ ChannelGroupName: channelGroupName })\n .pipe(Effect.catchTag(\"ResourceNotFoundException\", () => Effect.undefined));\n });\n\n return {\n stables: [\"channelGroupName\", \"channelGroupArn\", \"egressDomain\"],\n\n diff: Effect.fn(function* ({ id, olds, news }) {\n if (!isResolved(news)) return undefined;\n const oldName = yield* createName(id, olds);\n const newName = yield* createName(id, news);\n // The name is the group's identity; a change means a new group.\n if (oldName !== newName) return { action: \"replace\" } as const;\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const name = output?.channelGroupName ?? (yield* createName(id, olds ?? {}));\n const group = yield* getGroup(name);\n if (group === undefined) return undefined;\n const attrs = toAttrs(group);\n return (yield* hasAlchemyTags(id, toMpTagRecord(group.Tags))) ? attrs : Unowned(attrs);\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output, session }) {\n const internalTags = yield* createInternalTags(id);\n const desiredTags = { ...internalTags, ...news.tags };\n const name = output?.channelGroupName ?? (yield* createName(id, news));\n\n // 1. Observe — cloud state is authoritative; output is an id cache.\n let group = yield* getGroup(name);\n\n // 2. Ensure — create if missing; a Conflict means a peer created it\n // concurrently, so fall through to observing the winner.\n if (group === undefined) {\n group = yield* mediapackagev2\n .createChannelGroup({\n ChannelGroupName: name,\n Description: news.description,\n Tags: desiredTags,\n })\n .pipe(\n Effect.catchTag(\"ConflictException\", () =>\n mediapackagev2.getChannelGroup({ ChannelGroupName: name }),\n ),\n );\n } else if ((news.description ?? \"\") !== (group.Description ?? \"\")) {\n // 3. Sync — the description is the only mutable field.\n group = yield* mediapackagev2.updateChannelGroup({\n ChannelGroupName: name,\n Description: news.description,\n });\n }\n\n // 3b. Sync tags — diff against OBSERVED cloud tags.\n yield* syncMpTags(group.Arn, toMpTagRecord(group.Tags), desiredTags);\n\n yield* session.note(name);\n return toAttrs(group);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n const channelGroupName = output.channelGroupName;\n // Reap child channels (and their origin endpoints) first. A normal\n // stack destroy deletes children before the group, so this observes\n // nothing — but an orphan sweep can target a group whose children\n // it never enumerated, and deleting the group would then Conflict\n // until the retry budget ran out and leak the group.\n const channels = yield* listGroupChannels(channelGroupName);\n yield* Effect.forEach(\n channels,\n (channel) => deleteChannelWithEndpoints(channelGroupName, channel.ChannelName),\n { concurrency: 5, discard: true },\n );\n // MediaPackage v2 deletes are idempotent (deleting a missing group\n // succeeds), but the group transiently rejects deletion with a\n // Conflict while its just-deleted channels are cleaned up.\n yield* mediapackagev2\n .deleteChannelGroup({ ChannelGroupName: channelGroupName })\n .pipe(retryWhileMpConflict);\n }),\n\n list: () =>\n Effect.gen(function* () {\n const items = yield* mediapackagev2.listChannelGroups.pages({}).pipe(\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Items ?? [])),\n );\n // The list shape omits the egress domain, so hydrate each item\n // via get; a group can vanish between enumeration and hydration.\n const groups = yield* Effect.forEach(\n items,\n (item) =>\n mediapackagev2.getChannelGroup({ ChannelGroupName: item.ChannelGroupName }).pipe(\n Effect.map(toAttrs),\n Effect.catchTag(\"ResourceNotFoundException\", () => Effect.undefined),\n ),\n { concurrency: 5 },\n );\n return groups.filter((g): g is ChannelGroup[\"Attributes\"] => g !== undefined);\n }),\n };\n }),\n );\n","preview":null,"history":null,"version":"0d948dd4b88b25f5db249b587193f491564b1467"},"/_session?shell=1":null}