///
import {Duplex, DuplexOptions, Readable, Transform, Writable} from 'node:stream';
import {TypedDuplex, TypedReadable, TypedTransform, TypedWritable} from './typed-streams';
import {
none,
stop,
Stop,
finalSymbol,
finalValue,
final,
isFinalValue,
getFinalValue,
manySymbol,
many,
isMany,
getManyValues,
getFunctionList,
flushSymbol,
flushable,
isFlushable,
fListSymbol,
isFunctionList,
setFunctionList,
clearFunctionList,
toMany,
normalizeMany,
combineMany,
combineManyMut,
type AsFlatList,
type Fn,
type OutputType
} from './defs';
import gen from './gen';
import asStream from './asStream';
export = chain;
/**
* Creates a stream object out of a list of functions and streams.
* @param fns array of functions, streams, or other arrays
* @returns a duplex stream with additional properties
* @remarks This is the main function of this library.
*/
declare function chain(
fns: chain.ChainList, L>,
options?: chain.ChainOptions
): chain.ChainOutput, chain.Ret>;
declare function chainUnchecked(
fns: readonly any[],
options?: chain.ChainOptions
): chain.ChainOutput;
/**
* Takes a function or an iterable and returns the underlying function.
* @param fn function or iterable
* @returns the underlying function
* @remarks In the case of a function, it returns the argument. For iterables it returns the function associated with `Symbol.iterator` or `Symbol.asyncIterator`.
*/
declare function dataSource(
fn: F
): F extends AsyncIterable
? () => AsyncIterator
: F extends Iterable
? () => Iterator
: F extends Fn
? F
: never;
declare namespace chain {
/**
* Represents a typed duplex stream as a pair of readable and writable streams.
*/
export type DuplexStream = {
readable: ReadableStream;
writable: WritableStream;
};
/**
* Options for the chain function, which is based on `DuplexOptions`.
*/
export interface ChainOptions extends DuplexOptions {
/** If `true`, no groupings will be done. Each function will be a separate stream object. */
noGrouping?: boolean;
/** If `true`, event bindings to the chain stream object will be skipped. */
skipEvents?: boolean;
}
/**
* The tuple type for a chain function with one item.
*/
type ChainStreams1 = [Readable | Writable | Duplex | Transform];
/**
* The tuple type for a chain function with multiple items.
*/
type ChainStreams = [
Readable | Duplex | Transform,
...(Duplex | Transform)[],
Writable | Duplex | Transform
];
/**
* Represents the output of the chain function. It is based on `Duplex` with extra properties.
*/
export interface ChainOutput extends Duplex {
/** Internal list of streams. */
streams: ChainStreams1 | ChainStreams;
/** The first stream, which can be used to feed the chain and to attach event handlers. */
input: Readable | Writable | Duplex | Transform;
/** The last stream, which can be used to consume results and to attach event handlers. */
output: Readable | Writable | Duplex | Transform;
}
/**
* Returns the first argument of a chain, a stream, or a function.
*/
export type Arg0 =
F extends TypedTransform
? W
: F extends TypedDuplex
? W
: F extends TypedReadable
? never
: F extends TypedWritable
? W
: F extends Writable | Transform | Duplex
? any
: F extends Readable
? never
: F extends TransformStream
? W
: F extends DuplexStream
? W
: F extends WritableStream
? W
: F extends ReadableStream
? never
: F extends readonly unknown[]
? AsFlatList extends readonly [infer F1, ...(readonly unknown[])]
? Arg0
: AsFlatList extends readonly []
? unknown
: AsFlatList extends readonly (infer F1)[]
? Arg0
: never
: F extends (...args: readonly any[]) => unknown
? Parameters[0]
: never;
/**
* Returns the return type of a chain, a stream, or a function.
*/
export type Ret =
F extends TypedTransform
? R
: F extends TypedDuplex
? R
: F extends TypedReadable
? R
: F extends TypedWritable
? never
: F extends Readable | Transform | Duplex
? any
: F extends Writable
? never
: F extends TransformStream
? R
: F extends DuplexStream
? R
: F extends ReadableStream
? R
: F extends WritableStream
? never
: F extends readonly unknown[]
? AsFlatList extends readonly [...unknown[], infer F1]
? Ret
: AsFlatList extends readonly []
? Default
: AsFlatList extends readonly (infer F1)[]
? Ret
: never
: F extends Fn
? OutputType
: never;
/**
* Represents an item in the chain function.
* It is used to highlight mismatches between argument types and return types in a list.
*/
export type ChainItem =
F extends TypedTransform
? I extends W
? F
: TypedTransform
: F extends TypedDuplex
? I extends W
? F
: TypedDuplex
: F extends TypedReadable
? [I] extends [never]
? F
: never
: F extends TypedWritable
? I extends W
? F
: TypedWritable
: F extends Writable | Transform | Duplex
? F
: F extends Readable
? [I] extends [never]
? F
: never
: F extends TransformStream
? I extends W
? F
: TransformStream
: F extends DuplexStream
? I extends W
? F
: DuplexStream
: F extends ReadableStream
? [I] extends [never]
? F
: never
: F extends WritableStream
? I extends W
? F
: WritableStream
: F extends readonly [infer F1, ...infer R]
? F1 extends null | undefined
? readonly [F1, ...ChainList]
: readonly [ChainItem, ...ChainList, R>]
: F extends readonly unknown[]
? readonly [ChainItem]
: F extends Fn
? I extends Arg0
? F
: (arg: I, ...rest: readonly unknown[]) => ReturnType
: never;
/**
* Replicates a tuple verifying the types of the list items so arguments match returns.
* The replicated tuple is used to highlight mismatches between list items.
*/
export type ChainList = L extends readonly [infer F1, ...infer R]
? F1 extends null | undefined
? readonly [F1, ...ChainList]
: readonly [ChainItem, ...ChainList, R>]
: L;
export {
none,
stop,
Stop,
finalSymbol,
finalValue,
final,
isFinalValue,
getFinalValue,
manySymbol,
many,
isMany,
getManyValues,
getFunctionList,
flushSymbol,
flushable,
isFlushable,
fListSymbol,
isFunctionList,
setFunctionList,
clearFunctionList,
toMany,
normalizeMany,
combineMany,
combineManyMut,
chain,
chainUnchecked,
gen,
asStream,
dataSource
};
}