1import type * as runtime from "@cloudflare/workers-types";
2import * as Predicate from "effect/Predicate";
3import * as Stream from "effect/Stream";
4
5type ReplaceEffectStream<T> =
6 T extends Stream.Stream<any> ? runtime.ReadableStream<any> : T;
7
8/**
9 * If the value is an Effect stream, converts it to a ReadableStream.
10 * Otherwise, returns the value unchanged.
11 */
12export function replaceEffectStream<T>(value: T): ReplaceEffectStream<T> {
13 if (isEffectStream(value)) {
14 return Stream.toReadableStream(value) as ReplaceEffectStream<T>;
15 }
16 return value as ReplaceEffectStream<T>;
17}
18
19const isEffectStream = (value: unknown): value is Stream.Stream<any> =>
20 Predicate.hasProperty(value, "~effect/Stream");