sailfork / t3code-github / .repos/ alchemy-effect/ packages/ aws/ src/ MediaPackageV2/ Channel.ts

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 Predicate from "effect/Predicate";
import type { Providers } from "../Providers.ts";
import {
  deleteChannelWithEndpoints,
  listAllChannelGroups,
  listGroupChannels,
  matchesDesired,
  policiesEqual,
  syncMpTags,
  toMpTagRecord,
} from "./internal.ts";

export interface ChannelProps {
  /**
   * Name of the channel group the channel belongs to. Changing it replaces
   * the channel.
   */
  channelGroupName: string;
  /**
   * Name of the channel. Must be unique within the channel group and match
   * `^[a-zA-Z0-9_-]+

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/aws/src/MediaPackageV2/Channel.ts at main · sailfork/t3code-github · Sailfork
	
		
. If omitted, a unique name is generated. Changing the * name replaces the channel. */ channelName?: string; /** * The input type of content the channel receives: `HLS` or `CMAF`. * Immutable — changing it replaces the channel. * @default "HLS" */ inputType?: mediapackagev2.InputType; /** * Optional description of the channel (up to 1024 characters). */ description?: string; /** * Input-switching behavior between the channel's redundant ingest inputs * (e.g. switch based on the media quality confidence score). */ inputSwitchConfiguration?: mediapackagev2.InputSwitchConfiguration; /** * Settings for what common media server data (CMSD) headers AWS Elemental * MediaPackage includes in responses to the CDN. */ outputHeaderConfiguration?: mediapackagev2.OutputHeaderConfiguration; /** * IAM resource policy (JSON) attached to the channel, controlling which * principals may push content to it (`mediapackagev2:PutObject`). * Omitting it removes any existing policy. */ policy?: string; /** * User-defined tags for the channel. Merged with internal Alchemy tags. */ tags?: Record<string, string>; } export interface Channel extends Resource< "AWS.MediaPackageV2.Channel", ChannelProps, { /** Name of the channel group the channel belongs to. */ channelGroupName: string; /** Name of the channel. */ channelName: string; /** ARN of the channel. */ channelArn: string; /** Ingest container type (`HLS` or `CMAF`). */ inputType: string | undefined; /** Redundant ingest endpoints the encoder pushes content to. */ ingestEndpoints: { /** Ingest endpoint id (e.g. `"1"`, `"2"`). */ id: string | undefined; /** Ingest URL the encoder pushes to. */ url: string | undefined; }[]; }, never, Providers > {} /** * An AWS Elemental MediaPackage v2 channel — the entry point for live * content into MediaPackage. An encoder (such as AWS Elemental MediaLive) * pushes an HLS or CMAF stream to the channel's ingest endpoints; origin * endpoints then package and serve that content downstream. * * ### Creating a Channel * **Example:** Basic Channel in a Group * ```typescript * import * as MediaPackageV2 from "@alchemy.run/aws/MediaPackageV2"; * * const group = yield* MediaPackageV2.ChannelGroup("Live"); * const channel = yield* MediaPackageV2.Channel("Feed", { * channelGroupName: group.channelGroupName, * }); * ``` * * **Example:** CMAF Ingest Channel * ```typescript * const channel = yield* MediaPackageV2.Channel("Feed", { * channelGroupName: group.channelGroupName, * inputType: "CMAF", * description: "CMAF contribution feed", * }); * ``` * * ### Resource Policy * **Example:** Allow a Principal to Push Content * ```typescript * const channel = yield* MediaPackageV2.Channel("Feed", { * channelGroupName: group.channelGroupName, * policy: JSON.stringify({ * Version: "2012-10-17", * Statement: [{ * Effect: "Allow", * Principal: { AWS: "arn:aws:iam::111122223333:root" }, * Action: "mediapackagev2:PutObject", * Resource: "arn:aws:mediapackagev2:us-east-1:111122223333:channelGroup/live/channel/feed", * }], * }), * }); * ``` * * ### Ingest Endpoints * **Example:** Point the encoder at the ingest URLs * ```typescript * const channel = yield* MediaPackageV2.Channel("Feed", { * channelGroupName: group.channelGroupName, * }); * // Two redundant ingest endpoints for the encoder to push to. * const urls = channel.ingestEndpoints; * ``` * * @resource */ export const Channel = Resource<Channel>("AWS.MediaPackageV2.Channel"); export const ChannelProvider = () => Provider.effect( Channel, Effect.gen(function* () { const createName = Effect.fn(function* (id: string, props: { channelName?: string }) { return props.channelName ?? (yield* createPhysicalName({ id, maxLength: 256 })); }); const toAttrs = (channel: { Arn: string; ChannelName: string; ChannelGroupName: string; IngestEndpoints?: mediapackagev2.IngestEndpoint[]; InputType?: mediapackagev2.InputType; }) => ({ channelGroupName: channel.ChannelGroupName, channelName: channel.ChannelName, channelArn: channel.Arn, inputType: channel.InputType, ingestEndpoints: (channel.IngestEndpoints ?? []).map((endpoint) => ({ id: endpoint.Id, url: endpoint.Url, })), }); /** Get a channel by group + name; typed not-found → undefined. */ const getChannel = Effect.fn(function* (channelGroupName: string, channelName: string) { return yield* mediapackagev2 .getChannel({ ChannelGroupName: channelGroupName, ChannelName: channelName, }) .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); }); return { stables: ["channelGroupName", "channelName", "channelArn"], diff: Effect.fn(function* ({ id, olds, news }) { if (!isResolved(news)) return undefined; // Group, name, and input type are the channel's identity. if (olds.channelGroupName !== news.channelGroupName) { return { action: "replace" } as const; } const oldName = yield* createName(id, olds); const newName = yield* createName(id, news); if (oldName !== newName) return { action: "replace" } as const; if ((olds.inputType ?? "HLS") !== (news.inputType ?? "HLS")) { return { action: "replace" } as const; } }), read: Effect.fn(function* ({ id, olds, output }) { const channelGroupName = output?.channelGroupName ?? olds?.channelGroupName; if (channelGroupName === undefined) return undefined; const channelName = output?.channelName ?? (yield* createName(id, olds ?? {})); const channel = yield* getChannel(channelGroupName, channelName); if (channel === undefined) return undefined; const attrs = toAttrs(channel); return (yield* hasAlchemyTags(id, toMpTagRecord(channel.Tags))) ? attrs : Unowned(attrs); }), reconcile: Effect.fn(function* ({ id, news, output, session }) { const internalTags = yield* createInternalTags(id); const desiredTags = { ...internalTags, ...news.tags }; const channelGroupName = news.channelGroupName; const channelName = output?.channelName ?? (yield* createName(id, news)); // 1. Observe — cloud state is authoritative; output is an id cache. let channel = yield* getChannel(channelGroupName, channelName); // 2. Ensure — create if missing; a Conflict means a peer created it // concurrently, so fall through to observing the winner. if (channel === undefined) { channel = yield* mediapackagev2 .createChannel({ ChannelGroupName: channelGroupName, ChannelName: channelName, InputType: news.inputType, Description: news.description, InputSwitchConfiguration: news.inputSwitchConfiguration, OutputHeaderConfiguration: news.outputHeaderConfiguration, Tags: desiredTags, }) .pipe( Effect.catchTag("ConflictException", () => mediapackagev2.getChannel({ ChannelGroupName: channelGroupName, ChannelName: channelName, }), ), ); } else { // 3. Sync — apply the mutable fields only when the observed // state has drifted from the desired state. const desired = { Description: news.description ?? "", InputSwitchConfiguration: news.inputSwitchConfiguration, OutputHeaderConfiguration: news.outputHeaderConfiguration, }; const observed = { Description: channel.Description ?? "", InputSwitchConfiguration: channel.InputSwitchConfiguration, OutputHeaderConfiguration: channel.OutputHeaderConfiguration, }; if (!matchesDesired(desired, observed)) { channel = yield* mediapackagev2.updateChannel({ ChannelGroupName: channelGroupName, ChannelName: channelName, Description: news.description, InputSwitchConfiguration: news.inputSwitchConfiguration, OutputHeaderConfiguration: news.outputHeaderConfiguration, }); } } // 3b. Sync tags — diff against OBSERVED cloud tags. yield* syncMpTags(channel.Arn, toMpTagRecord(channel.Tags), desiredTags); // 3c. Sync the resource policy — observe the live policy (absent // policy is the typed not-found) and apply only the delta. const observedPolicy = yield* mediapackagev2 .getChannelPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, }) .pipe( Effect.map((response) => response.Policy as string | undefined), Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), ); if (news.policy !== undefined) { if (!policiesEqual(observedPolicy, news.policy)) { yield* mediapackagev2.putChannelPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, Policy: news.policy, }); } } else if (observedPolicy !== undefined) { yield* mediapackagev2.deleteChannelPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, }); } yield* session.note(channelName); return toAttrs(channel); }), delete: Effect.fn(function* ({ output }) { // Reap the channel's origin endpoints first (a normal stack destroy // already deleted them; an orphan sweep may not have), then delete // the channel. Every step is idempotent and the transient Conflict // while just-deleted endpoints are cleaned up is retried. yield* deleteChannelWithEndpoints(output.channelGroupName, output.channelName); }), // Channels are keyed by their parent channel group, so enumerate // the groups first and fan out. list: () => Effect.gen(function* () { const groups = yield* listAllChannelGroups(); const items = yield* Effect.forEach( groups, (group) => listGroupChannels(group.ChannelGroupName), { concurrency: 5 }, ).pipe(Effect.map((nested) => nested.flat())); // The list shape omits ingest endpoints, so hydrate each item // via get; a channel can vanish between enumeration and // hydration. const channels = yield* Effect.forEach( items, (item) => getChannel(item.ChannelGroupName, item.ChannelName).pipe( Effect.map((channel) => (channel === undefined ? undefined : toAttrs(channel))), ), { concurrency: 5 }, ); return channels.filter(Predicate.isNotUndefined); }), }; }), );
.repos/alchemy-effect/packages/aws/src/MediaPackageV2/Channel.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated. Changing the\n * name replaces the channel.\n */\n channelName?: string;\n /**\n * The input type of content the channel receives: `HLS` or `CMAF`.\n * Immutable — changing it replaces the channel.\n * @default \"HLS\"\n */\n inputType?: mediapackagev2.InputType;\n /**\n * Optional description of the channel (up to 1024 characters).\n */\n description?: string;\n /**\n * Input-switching behavior between the channel's redundant ingest inputs\n * (e.g. switch based on the media quality confidence score).\n */\n inputSwitchConfiguration?: mediapackagev2.InputSwitchConfiguration;\n /**\n * Settings for what common media server data (CMSD) headers AWS Elemental\n * MediaPackage includes in responses to the CDN.\n */\n outputHeaderConfiguration?: mediapackagev2.OutputHeaderConfiguration;\n /**\n * IAM resource policy (JSON) attached to the channel, controlling which\n * principals may push content to it (`mediapackagev2:PutObject`).\n * Omitting it removes any existing policy.\n */\n policy?: string;\n /**\n * User-defined tags for the channel. Merged with internal Alchemy tags.\n */\n tags?: Record\u003cstring, string>;\n}\n\nexport interface Channel extends Resource\u003c\n \"AWS.MediaPackageV2.Channel\",\n ChannelProps,\n {\n /** Name of the channel group the channel belongs to. */\n channelGroupName: string;\n /** Name of the channel. */\n channelName: string;\n /** ARN of the channel. */\n channelArn: string;\n /** Ingest container type (`HLS` or `CMAF`). */\n inputType: string | undefined;\n /** Redundant ingest endpoints the encoder pushes content to. */\n ingestEndpoints: {\n /** Ingest endpoint id (e.g. `\"1\"`, `\"2\"`). */\n id: string | undefined;\n /** Ingest URL the encoder pushes to. */\n url: string | undefined;\n }[];\n },\n never,\n Providers\n> {}\n\n/**\n * An AWS Elemental MediaPackage v2 channel — the entry point for live\n * content into MediaPackage. An encoder (such as AWS Elemental MediaLive)\n * pushes an HLS or CMAF stream to the channel's ingest endpoints; origin\n * endpoints then package and serve that content downstream.\n *\n * ### Creating a Channel\n * **Example:** Basic Channel in a Group\n * ```typescript\n * import * as MediaPackageV2 from \"@alchemy.run/aws/MediaPackageV2\";\n *\n * const group = yield* MediaPackageV2.ChannelGroup(\"Live\");\n * const channel = yield* MediaPackageV2.Channel(\"Feed\", {\n * channelGroupName: group.channelGroupName,\n * });\n * ```\n *\n * **Example:** CMAF Ingest Channel\n * ```typescript\n * const channel = yield* MediaPackageV2.Channel(\"Feed\", {\n * channelGroupName: group.channelGroupName,\n * inputType: \"CMAF\",\n * description: \"CMAF contribution feed\",\n * });\n * ```\n *\n * ### Resource Policy\n * **Example:** Allow a Principal to Push Content\n * ```typescript\n * const channel = yield* MediaPackageV2.Channel(\"Feed\", {\n * channelGroupName: group.channelGroupName,\n * policy: JSON.stringify({\n * Version: \"2012-10-17\",\n * Statement: [{\n * Effect: \"Allow\",\n * Principal: { AWS: \"arn:aws:iam::111122223333:root\" },\n * Action: \"mediapackagev2:PutObject\",\n * Resource: \"arn:aws:mediapackagev2:us-east-1:111122223333:channelGroup/live/channel/feed\",\n * }],\n * }),\n * });\n * ```\n *\n * ### Ingest Endpoints\n * **Example:** Point the encoder at the ingest URLs\n * ```typescript\n * const channel = yield* MediaPackageV2.Channel(\"Feed\", {\n * channelGroupName: group.channelGroupName,\n * });\n * // Two redundant ingest endpoints for the encoder to push to.\n * const urls = channel.ingestEndpoints;\n * ```\n *\n * @resource\n */\nexport const Channel = Resource\u003cChannel>(\"AWS.MediaPackageV2.Channel\");\n\nexport const ChannelProvider = () =>\n Provider.effect(\n Channel,\n Effect.gen(function* () {\n const createName = Effect.fn(function* (id: string, props: { channelName?: string }) {\n return props.channelName ?? (yield* createPhysicalName({ id, maxLength: 256 }));\n });\n\n const toAttrs = (channel: {\n Arn: string;\n ChannelName: string;\n ChannelGroupName: string;\n IngestEndpoints?: mediapackagev2.IngestEndpoint[];\n InputType?: mediapackagev2.InputType;\n }) => ({\n channelGroupName: channel.ChannelGroupName,\n channelName: channel.ChannelName,\n channelArn: channel.Arn,\n inputType: channel.InputType,\n ingestEndpoints: (channel.IngestEndpoints ?? []).map((endpoint) => ({\n id: endpoint.Id,\n url: endpoint.Url,\n })),\n });\n\n /** Get a channel by group + name; typed not-found → undefined. */\n const getChannel = Effect.fn(function* (channelGroupName: string, channelName: string) {\n return yield* mediapackagev2\n .getChannel({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n })\n .pipe(Effect.catchTag(\"ResourceNotFoundException\", () => Effect.undefined));\n });\n\n return {\n stables: [\"channelGroupName\", \"channelName\", \"channelArn\"],\n\n diff: Effect.fn(function* ({ id, olds, news }) {\n if (!isResolved(news)) return undefined;\n // Group, name, and input type are the channel's identity.\n if (olds.channelGroupName !== news.channelGroupName) {\n return { action: \"replace\" } as const;\n }\n const oldName = yield* createName(id, olds);\n const newName = yield* createName(id, news);\n if (oldName !== newName) return { action: \"replace\" } as const;\n if ((olds.inputType ?? \"HLS\") !== (news.inputType ?? \"HLS\")) {\n return { action: \"replace\" } as const;\n }\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const channelGroupName = output?.channelGroupName ?? olds?.channelGroupName;\n if (channelGroupName === undefined) return undefined;\n const channelName = output?.channelName ?? (yield* createName(id, olds ?? {}));\n const channel = yield* getChannel(channelGroupName, channelName);\n if (channel === undefined) return undefined;\n const attrs = toAttrs(channel);\n return (yield* hasAlchemyTags(id, toMpTagRecord(channel.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 channelGroupName = news.channelGroupName;\n const channelName = output?.channelName ?? (yield* createName(id, news));\n\n // 1. Observe — cloud state is authoritative; output is an id cache.\n let channel = yield* getChannel(channelGroupName, channelName);\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 (channel === undefined) {\n channel = yield* mediapackagev2\n .createChannel({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n InputType: news.inputType,\n Description: news.description,\n InputSwitchConfiguration: news.inputSwitchConfiguration,\n OutputHeaderConfiguration: news.outputHeaderConfiguration,\n Tags: desiredTags,\n })\n .pipe(\n Effect.catchTag(\"ConflictException\", () =>\n mediapackagev2.getChannel({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n }),\n ),\n );\n } else {\n // 3. Sync — apply the mutable fields only when the observed\n // state has drifted from the desired state.\n const desired = {\n Description: news.description ?? \"\",\n InputSwitchConfiguration: news.inputSwitchConfiguration,\n OutputHeaderConfiguration: news.outputHeaderConfiguration,\n };\n const observed = {\n Description: channel.Description ?? \"\",\n InputSwitchConfiguration: channel.InputSwitchConfiguration,\n OutputHeaderConfiguration: channel.OutputHeaderConfiguration,\n };\n if (!matchesDesired(desired, observed)) {\n channel = yield* mediapackagev2.updateChannel({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n Description: news.description,\n InputSwitchConfiguration: news.inputSwitchConfiguration,\n OutputHeaderConfiguration: news.outputHeaderConfiguration,\n });\n }\n }\n\n // 3b. Sync tags — diff against OBSERVED cloud tags.\n yield* syncMpTags(channel.Arn, toMpTagRecord(channel.Tags), desiredTags);\n\n // 3c. Sync the resource policy — observe the live policy (absent\n // policy is the typed not-found) and apply only the delta.\n const observedPolicy = yield* mediapackagev2\n .getChannelPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n })\n .pipe(\n Effect.map((response) => response.Policy as string | undefined),\n Effect.catchTag(\"ResourceNotFoundException\", () => Effect.undefined),\n );\n if (news.policy !== undefined) {\n if (!policiesEqual(observedPolicy, news.policy)) {\n yield* mediapackagev2.putChannelPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n Policy: news.policy,\n });\n }\n } else if (observedPolicy !== undefined) {\n yield* mediapackagev2.deleteChannelPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n });\n }\n\n yield* session.note(channelName);\n return toAttrs(channel);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n // Reap the channel's origin endpoints first (a normal stack destroy\n // already deleted them; an orphan sweep may not have), then delete\n // the channel. Every step is idempotent and the transient Conflict\n // while just-deleted endpoints are cleaned up is retried.\n yield* deleteChannelWithEndpoints(output.channelGroupName, output.channelName);\n }),\n\n // Channels are keyed by their parent channel group, so enumerate\n // the groups first and fan out.\n list: () =>\n Effect.gen(function* () {\n const groups = yield* listAllChannelGroups();\n const items = yield* Effect.forEach(\n groups,\n (group) => listGroupChannels(group.ChannelGroupName),\n { concurrency: 5 },\n ).pipe(Effect.map((nested) => nested.flat()));\n // The list shape omits ingest endpoints, so hydrate each item\n // via get; a channel can vanish between enumeration and\n // hydration.\n const channels = yield* Effect.forEach(\n items,\n (item) =>\n getChannel(item.ChannelGroupName, item.ChannelName).pipe(\n Effect.map((channel) => (channel === undefined ? undefined : toAttrs(channel))),\n ),\n { concurrency: 5 },\n );\n return channels.filter(Predicate.isNotUndefined);\n }),\n };\n }),\n );\n","preview":null,"history":null,"version":"2f0e3841c05f3f59c0b548b4d05f753765403603"},"/_session?shell=1":null}