sailfork / t3code-github / .repos/ alchemy-effect/ packages/ aws/ src/ MediaPackageV2/ OriginEndpoint.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 { toWireSeconds } from "alchemy/Util/Duration";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Predicate from "effect/Predicate";
import type { Providers } from "../Providers.ts";
import {
  listAllChannelGroups,
  listChannelEndpoints,
  listGroupChannels,
  matchesDesired,
  policiesEqual,
  retryWhileMpConflict,
  syncMpTags,
  toMpTagRecord,
} from "./internal.ts";

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

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/aws/src/MediaPackageV2/OriginEndpoint.ts at main · sailfork/t3code-github · Sailfork
	
		
. If omitted, a unique name is generated. Changing the * name replaces the endpoint. */ originEndpointName?: string; /** * The container format packaged for the endpoint's manifests: `TS`, * `CMAF`, or `ISM`. Immutable — changing it replaces the endpoint. */ containerType: mediapackagev2.ContainerType; /** * Segment settings: duration, name, SCTE-35 ad-marker filtering, and DRM * encryption (SPEKE key provider). */ segment?: mediapackagev2.Segment; /** * Optional description of the origin endpoint (up to 1024 characters). */ description?: string; /** * The size of the window (1 minute - 14 days, e.g. `"1 hour"` or * `Duration.hours(1)`) from which viewers can start over or catch up on * previously streamed content. Sent to the API in whole seconds. */ startoverWindow?: Duration.Input; /** * HLS manifest configurations served by the endpoint. */ hlsManifests?: mediapackagev2.CreateHlsManifestConfiguration[]; /** * Low-latency HLS manifest configurations served by the endpoint. */ lowLatencyHlsManifests?: mediapackagev2.CreateLowLatencyHlsManifestConfiguration[]; /** * DASH manifest configurations served by the endpoint. */ dashManifests?: mediapackagev2.CreateDashManifestConfiguration[]; /** * Microsoft Smooth Streaming (MSS) manifest configurations served by the * endpoint. Requires the `ISM` container type. */ mssManifests?: mediapackagev2.CreateMssManifestConfiguration[]; /** * Conditions (stale manifest, missing DRM key, ...) under which the * endpoint deliberately serves errors, for testing player behavior. */ forceEndpointErrorConfiguration?: mediapackagev2.ForceEndpointErrorConfiguration; /** * The separator (`UNDERSCORE` or `HYPHEN`) inserted into manifest and * segment URIs. */ uriSeparator?: mediapackagev2.UriSeparator; /** * IAM resource policy (JSON) attached to the origin endpoint, controlling * which principals may retrieve content from it * (`mediapackagev2:GetObject` / `mediapackagev2:GetHeadObject`). Omitting * it removes any existing policy. */ policy?: string; /** * CDN authorization for the endpoint policy: the Secrets Manager secrets * holding the CDN identifier and the role MediaPackage assumes to read * them. Only applied together with {@link OriginEndpointProps.policy}. */ cdnAuthConfiguration?: mediapackagev2.CdnAuthConfiguration; /** * User-defined tags for the origin endpoint. Merged with internal Alchemy * tags. */ tags?: Record<string, string>; } interface ManifestRef { /** Name of the manifest. */ manifestName: string; /** Playback URL of the manifest on the group's egress domain. */ url: string; } export interface OriginEndpoint extends Resource< "AWS.MediaPackageV2.OriginEndpoint", OriginEndpointProps, { /** Name of the channel group the endpoint belongs to. */ channelGroupName: string; /** Name of the channel the endpoint packages content from. */ channelName: string; /** Name of the origin endpoint. */ originEndpointName: string; /** ARN of the origin endpoint. */ originEndpointArn: string; /** Output container type (`TS`, `CMAF`, or `ISM`). */ containerType: string; /** HLS manifests served by the endpoint. */ hlsManifests: ManifestRef[]; /** Low-latency HLS manifests served by the endpoint. */ lowLatencyHlsManifests: ManifestRef[]; /** DASH manifests served by the endpoint. */ dashManifests: ManifestRef[]; /** Microsoft Smooth Streaming manifests served by the endpoint. */ mssManifests: ManifestRef[]; }, never, Providers > {} /** * An AWS Elemental MediaPackage v2 origin endpoint — the output side of a * channel. The endpoint packages the channel's ingested content into HLS, * low-latency HLS, DASH, and/or MSS manifests and serves them to downstream * devices (players or CDNs) on the channel group's egress domain. * * ### Creating an Origin Endpoint * **Example:** HLS Endpoint on a Channel * ```typescript * import * as MediaPackageV2 from "@alchemy.run/aws/MediaPackageV2"; * * const group = yield* MediaPackageV2.ChannelGroup("Live"); * const channel = yield* MediaPackageV2.Channel("Feed", { * channelGroupName: group.channelGroupName, * }); * const endpoint = yield* MediaPackageV2.OriginEndpoint("Playback", { * channelGroupName: group.channelGroupName, * channelName: channel.channelName, * containerType: "TS", * hlsManifests: [{ ManifestName: "index" }], * }); * ``` * * **Example:** CMAF Endpoint with DASH and Low-Latency HLS * ```typescript * const endpoint = yield* MediaPackageV2.OriginEndpoint("Playback", { * channelGroupName: group.channelGroupName, * channelName: channel.channelName, * containerType: "CMAF", * segment: { SegmentDurationSeconds: 4 }, * dashManifests: [{ ManifestName: "dash" }], * lowLatencyHlsManifests: [{ ManifestName: "ll-hls" }], * }); * ``` * * ### Startover Window * **Example:** Allow viewers to catch up on the last hour * ```typescript * const endpoint = yield* MediaPackageV2.OriginEndpoint("Playback", { * channelGroupName: group.channelGroupName, * channelName: channel.channelName, * containerType: "TS", * startoverWindow: "1 hour", * hlsManifests: [{ ManifestName: "index" }], * }); * ``` * * ### Resource Policy * **Example:** Restrict playback to a CDN principal * ```typescript * const endpoint = yield* MediaPackageV2.OriginEndpoint("Playback", { * channelGroupName: group.channelGroupName, * channelName: channel.channelName, * containerType: "TS", * hlsManifests: [{ ManifestName: "index" }], * policy: JSON.stringify({ * Version: "2012-10-17", * Statement: [{ * Effect: "Allow", * Principal: { AWS: "arn:aws:iam::111122223333:root" }, * Action: ["mediapackagev2:GetObject", "mediapackagev2:GetHeadObject"], * Resource: "arn:aws:mediapackagev2:us-east-1:111122223333:channelGroup/live/channel/feed/originEndpoint/playback", * }], * }), * }); * ``` * * ### Playback URLs * **Example:** Read the served manifest URLs * ```typescript * const playbackUrl = endpoint.hlsManifests.map((m) => m.url); * ``` * * @resource */ export const OriginEndpoint = Resource<OriginEndpoint>("AWS.MediaPackageV2.OriginEndpoint"); export const OriginEndpointProvider = () => Provider.effect( OriginEndpoint, Effect.gen(function* () { const createName = Effect.fn(function* (id: string, props: { originEndpointName?: string }) { return props.originEndpointName ?? (yield* createPhysicalName({ id, maxLength: 256 })); }); const manifestRefs = ( manifests: { ManifestName: string; Url: string }[] | undefined, ): ManifestRef[] => (manifests ?? []).map((m) => ({ manifestName: m.ManifestName, url: m.Url, })); const toAttrs = (endpoint: { Arn: string; ChannelGroupName: string; ChannelName: string; OriginEndpointName: string; ContainerType: mediapackagev2.ContainerType; HlsManifests?: mediapackagev2.GetHlsManifestConfiguration[]; LowLatencyHlsManifests?: mediapackagev2.GetLowLatencyHlsManifestConfiguration[]; DashManifests?: mediapackagev2.GetDashManifestConfiguration[]; MssManifests?: mediapackagev2.GetMssManifestConfiguration[]; }) => ({ channelGroupName: endpoint.ChannelGroupName, channelName: endpoint.ChannelName, originEndpointName: endpoint.OriginEndpointName, originEndpointArn: endpoint.Arn, containerType: endpoint.ContainerType, hlsManifests: manifestRefs(endpoint.HlsManifests), lowLatencyHlsManifests: manifestRefs(endpoint.LowLatencyHlsManifests), dashManifests: manifestRefs(endpoint.DashManifests), mssManifests: manifestRefs(endpoint.MssManifests), }); /** Get an origin endpoint; typed not-found → undefined. */ const getEndpoint = Effect.fn(function* ( channelGroupName: string, channelName: string, originEndpointName: string, ) { return yield* mediapackagev2 .getOriginEndpoint({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: originEndpointName, }) .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); }); return { stables: [ "channelGroupName", "channelName", "originEndpointName", "originEndpointArn", "containerType", ], diff: Effect.fn(function* ({ id, olds, news }) { if (!isResolved(news)) return undefined; // Group, channel, name, and container type are the endpoint's // identity. if (olds.channelGroupName !== news.channelGroupName) { return { action: "replace" } as const; } if (olds.channelName !== news.channelName) { 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.containerType !== news.containerType) { return { action: "replace" } as const; } }), read: Effect.fn(function* ({ id, olds, output }) { const channelGroupName = output?.channelGroupName ?? olds?.channelGroupName; const channelName = output?.channelName ?? olds?.channelName; if (channelGroupName === undefined || channelName === undefined) { return undefined; } const name = output?.originEndpointName ?? (yield* createName(id, olds ?? {})); const endpoint = yield* getEndpoint(channelGroupName, channelName, name); if (endpoint === undefined) return undefined; const attrs = toAttrs(endpoint); return (yield* hasAlchemyTags(id, toMpTagRecord(endpoint.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 = news.channelName; // The wire field is whole seconds. const startoverWindowSeconds = toWireSeconds(news.startoverWindow); const name = output?.originEndpointName ?? (yield* createName(id, news)); // 1. Observe — cloud state is authoritative; output is an id cache. let endpoint = yield* getEndpoint(channelGroupName, channelName, name); // 2. Ensure — create if missing; a Conflict means a peer created it // concurrently, so fall through to observing the winner. if (endpoint === undefined) { endpoint = yield* mediapackagev2 .createOriginEndpoint({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, ContainerType: news.containerType, Segment: news.segment, Description: news.description, StartoverWindowSeconds: startoverWindowSeconds, HlsManifests: news.hlsManifests, LowLatencyHlsManifests: news.lowLatencyHlsManifests, DashManifests: news.dashManifests, MssManifests: news.mssManifests, ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration, UriSeparator: news.uriSeparator, Tags: desiredTags, }) .pipe( Effect.catchTag("ConflictException", () => mediapackagev2.getOriginEndpoint({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, }), ), ); } else { // 3. Sync — the update is a full PUT of the mutable aspects, so // apply it only when the observed state has drifted from the // desired subset. Manifest arrays are compared by length and // by the fields the desired config specifies, so server-side // defaults never register as drift. const desired = { Segment: news.segment, Description: news.description ?? "", StartoverWindowSeconds: startoverWindowSeconds, HlsManifests: news.hlsManifests ?? [], LowLatencyHlsManifests: news.lowLatencyHlsManifests ?? [], DashManifests: news.dashManifests ?? [], MssManifests: news.mssManifests ?? [], ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration, UriSeparator: news.uriSeparator, }; const observed = { Segment: endpoint.Segment, Description: endpoint.Description ?? "", StartoverWindowSeconds: endpoint.StartoverWindowSeconds, HlsManifests: endpoint.HlsManifests ?? [], LowLatencyHlsManifests: endpoint.LowLatencyHlsManifests ?? [], DashManifests: endpoint.DashManifests ?? [], MssManifests: endpoint.MssManifests ?? [], ForceEndpointErrorConfiguration: endpoint.ForceEndpointErrorConfiguration, UriSeparator: endpoint.UriSeparator, }; if (!matchesDesired(desired, observed)) { endpoint = yield* mediapackagev2.updateOriginEndpoint({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, ContainerType: news.containerType, Segment: news.segment, Description: news.description, StartoverWindowSeconds: startoverWindowSeconds, HlsManifests: news.hlsManifests, LowLatencyHlsManifests: news.lowLatencyHlsManifests, DashManifests: news.dashManifests, MssManifests: news.mssManifests, ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration, UriSeparator: news.uriSeparator, }); } } // 3b. Sync tags — diff against OBSERVED cloud tags. yield* syncMpTags(endpoint.Arn, toMpTagRecord(endpoint.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 .getOriginEndpointPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, }) .pipe( Effect.map((response) => ({ policy: response.Policy as string | undefined, cdnAuth: response.CdnAuthConfiguration, })), Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({ policy: undefined as string | undefined, cdnAuth: undefined as mediapackagev2.CdnAuthConfiguration | undefined, }), ), ); if (news.policy !== undefined) { const cdnDrift = news.cdnAuthConfiguration === undefined ? observedPolicy.cdnAuth !== undefined : !matchesDesired(news.cdnAuthConfiguration, observedPolicy.cdnAuth); if (!policiesEqual(observedPolicy.policy, news.policy) || cdnDrift) { yield* mediapackagev2.putOriginEndpointPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, Policy: news.policy, CdnAuthConfiguration: news.cdnAuthConfiguration, }); } } else if (observedPolicy.policy !== undefined) { yield* mediapackagev2.deleteOriginEndpointPolicy({ ChannelGroupName: channelGroupName, ChannelName: channelName, OriginEndpointName: name, }); } yield* session.note(name); return toAttrs(endpoint); }), delete: Effect.fn(function* ({ output }) { // MediaPackage v2 deletes are idempotent (deleting a missing // endpoint succeeds); a Conflict from a concurrent mutation is // transient. yield* mediapackagev2 .deleteOriginEndpoint({ ChannelGroupName: output.channelGroupName, ChannelName: output.channelName, OriginEndpointName: output.originEndpointName, }) .pipe(retryWhileMpConflict); }), // Origin endpoints are keyed by their parent channel, so enumerate // groups → channels → endpoints. list: () => Effect.gen(function* () { const groups = yield* listAllChannelGroups(); const channels = yield* Effect.forEach( groups, (group) => listGroupChannels(group.ChannelGroupName), { concurrency: 5 }, ).pipe(Effect.map((nested) => nested.flat())); const items = yield* Effect.forEach( channels, (channel) => listChannelEndpoints(channel.ChannelGroupName, channel.ChannelName), { concurrency: 5 }, ).pipe(Effect.map((nested) => nested.flat())); // Hydrate each item via get so the attributes carry the manifest // URLs; an endpoint can vanish between enumeration and hydration. const endpoints = yield* Effect.forEach( items, (item) => getEndpoint(item.ChannelGroupName, item.ChannelName, item.OriginEndpointName).pipe( Effect.map((endpoint) => endpoint === undefined ? undefined : toAttrs(endpoint), ), ), { concurrency: 5 }, ); return endpoints.filter(Predicate.isNotUndefined); }), }; }), );
.repos/alchemy-effect/packages/aws/src/MediaPackageV2/OriginEndpoint.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated. Changing the\n * name replaces the endpoint.\n */\n originEndpointName?: string;\n /**\n * The container format packaged for the endpoint's manifests: `TS`,\n * `CMAF`, or `ISM`. Immutable — changing it replaces the endpoint.\n */\n containerType: mediapackagev2.ContainerType;\n /**\n * Segment settings: duration, name, SCTE-35 ad-marker filtering, and DRM\n * encryption (SPEKE key provider).\n */\n segment?: mediapackagev2.Segment;\n /**\n * Optional description of the origin endpoint (up to 1024 characters).\n */\n description?: string;\n /**\n * The size of the window (1 minute - 14 days, e.g. `\"1 hour\"` or\n * `Duration.hours(1)`) from which viewers can start over or catch up on\n * previously streamed content. Sent to the API in whole seconds.\n */\n startoverWindow?: Duration.Input;\n /**\n * HLS manifest configurations served by the endpoint.\n */\n hlsManifests?: mediapackagev2.CreateHlsManifestConfiguration[];\n /**\n * Low-latency HLS manifest configurations served by the endpoint.\n */\n lowLatencyHlsManifests?: mediapackagev2.CreateLowLatencyHlsManifestConfiguration[];\n /**\n * DASH manifest configurations served by the endpoint.\n */\n dashManifests?: mediapackagev2.CreateDashManifestConfiguration[];\n /**\n * Microsoft Smooth Streaming (MSS) manifest configurations served by the\n * endpoint. Requires the `ISM` container type.\n */\n mssManifests?: mediapackagev2.CreateMssManifestConfiguration[];\n /**\n * Conditions (stale manifest, missing DRM key, ...) under which the\n * endpoint deliberately serves errors, for testing player behavior.\n */\n forceEndpointErrorConfiguration?: mediapackagev2.ForceEndpointErrorConfiguration;\n /**\n * The separator (`UNDERSCORE` or `HYPHEN`) inserted into manifest and\n * segment URIs.\n */\n uriSeparator?: mediapackagev2.UriSeparator;\n /**\n * IAM resource policy (JSON) attached to the origin endpoint, controlling\n * which principals may retrieve content from it\n * (`mediapackagev2:GetObject` / `mediapackagev2:GetHeadObject`). Omitting\n * it removes any existing policy.\n */\n policy?: string;\n /**\n * CDN authorization for the endpoint policy: the Secrets Manager secrets\n * holding the CDN identifier and the role MediaPackage assumes to read\n * them. Only applied together with {@link OriginEndpointProps.policy}.\n */\n cdnAuthConfiguration?: mediapackagev2.CdnAuthConfiguration;\n /**\n * User-defined tags for the origin endpoint. Merged with internal Alchemy\n * tags.\n */\n tags?: Record\u003cstring, string>;\n}\n\ninterface ManifestRef {\n /** Name of the manifest. */\n manifestName: string;\n /** Playback URL of the manifest on the group's egress domain. */\n url: string;\n}\n\nexport interface OriginEndpoint extends Resource\u003c\n \"AWS.MediaPackageV2.OriginEndpoint\",\n OriginEndpointProps,\n {\n /** Name of the channel group the endpoint belongs to. */\n channelGroupName: string;\n /** Name of the channel the endpoint packages content from. */\n channelName: string;\n /** Name of the origin endpoint. */\n originEndpointName: string;\n /** ARN of the origin endpoint. */\n originEndpointArn: string;\n /** Output container type (`TS`, `CMAF`, or `ISM`). */\n containerType: string;\n /** HLS manifests served by the endpoint. */\n hlsManifests: ManifestRef[];\n /** Low-latency HLS manifests served by the endpoint. */\n lowLatencyHlsManifests: ManifestRef[];\n /** DASH manifests served by the endpoint. */\n dashManifests: ManifestRef[];\n /** Microsoft Smooth Streaming manifests served by the endpoint. */\n mssManifests: ManifestRef[];\n },\n never,\n Providers\n> {}\n\n/**\n * An AWS Elemental MediaPackage v2 origin endpoint — the output side of a\n * channel. The endpoint packages the channel's ingested content into HLS,\n * low-latency HLS, DASH, and/or MSS manifests and serves them to downstream\n * devices (players or CDNs) on the channel group's egress domain.\n *\n * ### Creating an Origin Endpoint\n * **Example:** HLS Endpoint on a Channel\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 * const endpoint = yield* MediaPackageV2.OriginEndpoint(\"Playback\", {\n * channelGroupName: group.channelGroupName,\n * channelName: channel.channelName,\n * containerType: \"TS\",\n * hlsManifests: [{ ManifestName: \"index\" }],\n * });\n * ```\n *\n * **Example:** CMAF Endpoint with DASH and Low-Latency HLS\n * ```typescript\n * const endpoint = yield* MediaPackageV2.OriginEndpoint(\"Playback\", {\n * channelGroupName: group.channelGroupName,\n * channelName: channel.channelName,\n * containerType: \"CMAF\",\n * segment: { SegmentDurationSeconds: 4 },\n * dashManifests: [{ ManifestName: \"dash\" }],\n * lowLatencyHlsManifests: [{ ManifestName: \"ll-hls\" }],\n * });\n * ```\n *\n * ### Startover Window\n * **Example:** Allow viewers to catch up on the last hour\n * ```typescript\n * const endpoint = yield* MediaPackageV2.OriginEndpoint(\"Playback\", {\n * channelGroupName: group.channelGroupName,\n * channelName: channel.channelName,\n * containerType: \"TS\",\n * startoverWindow: \"1 hour\",\n * hlsManifests: [{ ManifestName: \"index\" }],\n * });\n * ```\n *\n * ### Resource Policy\n * **Example:** Restrict playback to a CDN principal\n * ```typescript\n * const endpoint = yield* MediaPackageV2.OriginEndpoint(\"Playback\", {\n * channelGroupName: group.channelGroupName,\n * channelName: channel.channelName,\n * containerType: \"TS\",\n * hlsManifests: [{ ManifestName: \"index\" }],\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:GetObject\", \"mediapackagev2:GetHeadObject\"],\n * Resource: \"arn:aws:mediapackagev2:us-east-1:111122223333:channelGroup/live/channel/feed/originEndpoint/playback\",\n * }],\n * }),\n * });\n * ```\n *\n * ### Playback URLs\n * **Example:** Read the served manifest URLs\n * ```typescript\n * const playbackUrl = endpoint.hlsManifests.map((m) => m.url);\n * ```\n *\n * @resource\n */\nexport const OriginEndpoint = Resource\u003cOriginEndpoint>(\"AWS.MediaPackageV2.OriginEndpoint\");\n\nexport const OriginEndpointProvider = () =>\n Provider.effect(\n OriginEndpoint,\n Effect.gen(function* () {\n const createName = Effect.fn(function* (id: string, props: { originEndpointName?: string }) {\n return props.originEndpointName ?? (yield* createPhysicalName({ id, maxLength: 256 }));\n });\n\n const manifestRefs = (\n manifests: { ManifestName: string; Url: string }[] | undefined,\n ): ManifestRef[] =>\n (manifests ?? []).map((m) => ({\n manifestName: m.ManifestName,\n url: m.Url,\n }));\n\n const toAttrs = (endpoint: {\n Arn: string;\n ChannelGroupName: string;\n ChannelName: string;\n OriginEndpointName: string;\n ContainerType: mediapackagev2.ContainerType;\n HlsManifests?: mediapackagev2.GetHlsManifestConfiguration[];\n LowLatencyHlsManifests?: mediapackagev2.GetLowLatencyHlsManifestConfiguration[];\n DashManifests?: mediapackagev2.GetDashManifestConfiguration[];\n MssManifests?: mediapackagev2.GetMssManifestConfiguration[];\n }) => ({\n channelGroupName: endpoint.ChannelGroupName,\n channelName: endpoint.ChannelName,\n originEndpointName: endpoint.OriginEndpointName,\n originEndpointArn: endpoint.Arn,\n containerType: endpoint.ContainerType,\n hlsManifests: manifestRefs(endpoint.HlsManifests),\n lowLatencyHlsManifests: manifestRefs(endpoint.LowLatencyHlsManifests),\n dashManifests: manifestRefs(endpoint.DashManifests),\n mssManifests: manifestRefs(endpoint.MssManifests),\n });\n\n /** Get an origin endpoint; typed not-found → undefined. */\n const getEndpoint = Effect.fn(function* (\n channelGroupName: string,\n channelName: string,\n originEndpointName: string,\n ) {\n return yield* mediapackagev2\n .getOriginEndpoint({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: originEndpointName,\n })\n .pipe(Effect.catchTag(\"ResourceNotFoundException\", () => Effect.undefined));\n });\n\n return {\n stables: [\n \"channelGroupName\",\n \"channelName\",\n \"originEndpointName\",\n \"originEndpointArn\",\n \"containerType\",\n ],\n\n diff: Effect.fn(function* ({ id, olds, news }) {\n if (!isResolved(news)) return undefined;\n // Group, channel, name, and container type are the endpoint's\n // identity.\n if (olds.channelGroupName !== news.channelGroupName) {\n return { action: \"replace\" } as const;\n }\n if (olds.channelName !== news.channelName) {\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.containerType !== news.containerType) {\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 const channelName = output?.channelName ?? olds?.channelName;\n if (channelGroupName === undefined || channelName === undefined) {\n return undefined;\n }\n const name = output?.originEndpointName ?? (yield* createName(id, olds ?? {}));\n const endpoint = yield* getEndpoint(channelGroupName, channelName, name);\n if (endpoint === undefined) return undefined;\n const attrs = toAttrs(endpoint);\n return (yield* hasAlchemyTags(id, toMpTagRecord(endpoint.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 = news.channelName;\n // The wire field is whole seconds.\n const startoverWindowSeconds = toWireSeconds(news.startoverWindow);\n const name = output?.originEndpointName ?? (yield* createName(id, news));\n\n // 1. Observe — cloud state is authoritative; output is an id cache.\n let endpoint = yield* getEndpoint(channelGroupName, channelName, 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 (endpoint === undefined) {\n endpoint = yield* mediapackagev2\n .createOriginEndpoint({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n ContainerType: news.containerType,\n Segment: news.segment,\n Description: news.description,\n StartoverWindowSeconds: startoverWindowSeconds,\n HlsManifests: news.hlsManifests,\n LowLatencyHlsManifests: news.lowLatencyHlsManifests,\n DashManifests: news.dashManifests,\n MssManifests: news.mssManifests,\n ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration,\n UriSeparator: news.uriSeparator,\n Tags: desiredTags,\n })\n .pipe(\n Effect.catchTag(\"ConflictException\", () =>\n mediapackagev2.getOriginEndpoint({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n }),\n ),\n );\n } else {\n // 3. Sync — the update is a full PUT of the mutable aspects, so\n // apply it only when the observed state has drifted from the\n // desired subset. Manifest arrays are compared by length and\n // by the fields the desired config specifies, so server-side\n // defaults never register as drift.\n const desired = {\n Segment: news.segment,\n Description: news.description ?? \"\",\n StartoverWindowSeconds: startoverWindowSeconds,\n HlsManifests: news.hlsManifests ?? [],\n LowLatencyHlsManifests: news.lowLatencyHlsManifests ?? [],\n DashManifests: news.dashManifests ?? [],\n MssManifests: news.mssManifests ?? [],\n ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration,\n UriSeparator: news.uriSeparator,\n };\n const observed = {\n Segment: endpoint.Segment,\n Description: endpoint.Description ?? \"\",\n StartoverWindowSeconds: endpoint.StartoverWindowSeconds,\n HlsManifests: endpoint.HlsManifests ?? [],\n LowLatencyHlsManifests: endpoint.LowLatencyHlsManifests ?? [],\n DashManifests: endpoint.DashManifests ?? [],\n MssManifests: endpoint.MssManifests ?? [],\n ForceEndpointErrorConfiguration: endpoint.ForceEndpointErrorConfiguration,\n UriSeparator: endpoint.UriSeparator,\n };\n if (!matchesDesired(desired, observed)) {\n endpoint = yield* mediapackagev2.updateOriginEndpoint({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n ContainerType: news.containerType,\n Segment: news.segment,\n Description: news.description,\n StartoverWindowSeconds: startoverWindowSeconds,\n HlsManifests: news.hlsManifests,\n LowLatencyHlsManifests: news.lowLatencyHlsManifests,\n DashManifests: news.dashManifests,\n MssManifests: news.mssManifests,\n ForceEndpointErrorConfiguration: news.forceEndpointErrorConfiguration,\n UriSeparator: news.uriSeparator,\n });\n }\n }\n\n // 3b. Sync tags — diff against OBSERVED cloud tags.\n yield* syncMpTags(endpoint.Arn, toMpTagRecord(endpoint.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 .getOriginEndpointPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n })\n .pipe(\n Effect.map((response) => ({\n policy: response.Policy as string | undefined,\n cdnAuth: response.CdnAuthConfiguration,\n })),\n Effect.catchTag(\"ResourceNotFoundException\", () =>\n Effect.succeed({\n policy: undefined as string | undefined,\n cdnAuth: undefined as mediapackagev2.CdnAuthConfiguration | undefined,\n }),\n ),\n );\n if (news.policy !== undefined) {\n const cdnDrift =\n news.cdnAuthConfiguration === undefined\n ? observedPolicy.cdnAuth !== undefined\n : !matchesDesired(news.cdnAuthConfiguration, observedPolicy.cdnAuth);\n if (!policiesEqual(observedPolicy.policy, news.policy) || cdnDrift) {\n yield* mediapackagev2.putOriginEndpointPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n Policy: news.policy,\n CdnAuthConfiguration: news.cdnAuthConfiguration,\n });\n }\n } else if (observedPolicy.policy !== undefined) {\n yield* mediapackagev2.deleteOriginEndpointPolicy({\n ChannelGroupName: channelGroupName,\n ChannelName: channelName,\n OriginEndpointName: name,\n });\n }\n\n yield* session.note(name);\n return toAttrs(endpoint);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n // MediaPackage v2 deletes are idempotent (deleting a missing\n // endpoint succeeds); a Conflict from a concurrent mutation is\n // transient.\n yield* mediapackagev2\n .deleteOriginEndpoint({\n ChannelGroupName: output.channelGroupName,\n ChannelName: output.channelName,\n OriginEndpointName: output.originEndpointName,\n })\n .pipe(retryWhileMpConflict);\n }),\n\n // Origin endpoints are keyed by their parent channel, so enumerate\n // groups → channels → endpoints.\n list: () =>\n Effect.gen(function* () {\n const groups = yield* listAllChannelGroups();\n const channels = yield* Effect.forEach(\n groups,\n (group) => listGroupChannels(group.ChannelGroupName),\n { concurrency: 5 },\n ).pipe(Effect.map((nested) => nested.flat()));\n const items = yield* Effect.forEach(\n channels,\n (channel) => listChannelEndpoints(channel.ChannelGroupName, channel.ChannelName),\n { concurrency: 5 },\n ).pipe(Effect.map((nested) => nested.flat()));\n // Hydrate each item via get so the attributes carry the manifest\n // URLs; an endpoint can vanish between enumeration and hydration.\n const endpoints = yield* Effect.forEach(\n items,\n (item) =>\n getEndpoint(item.ChannelGroupName, item.ChannelName, item.OriginEndpointName).pipe(\n Effect.map((endpoint) =>\n endpoint === undefined ? undefined : toAttrs(endpoint),\n ),\n ),\n { concurrency: 5 },\n );\n return endpoints.filter(Predicate.isNotUndefined);\n }),\n };\n }),\n );\n","preview":null,"history":null,"version":"0fa54f27725a343c5219e8c0785281c57113b675"},"/_session?shell=1":null}