UNPKG

macoolka-stream

Version:

`macoolka-stream` is a library contains common stream functions.

70 lines (69 loc) 2.19 kB
/// <reference types="node" /> /// <reference types="node" /> /** * Common function for Stream * @desczh * Stream 常用函数 * @file */ import { Readable, Stream } from 'stream'; import { Task } from 'fp-ts/Task'; /** * Returns a Buffer with data read from the stream. * @desczh * 读取流到Buffer * @since 0.2.0 */ export declare function streamToBuffer(stream: Stream): Task<Buffer>; /** * Determines whether the passed value is a `Stream`. * @desczh * 是否输入为`Stream` * @since 0.2.0 */ export declare const isStream: (a: unknown) => a is Stream; /** * Returns stream's size * @desczh * 获得流的长度 * @since 0.2.0 */ export declare function streamSize(stream: Stream): Task<number>; /** * Returns a string with data read from the stream. * @desczh * 把流转换为文本 * @since 0.2.0 */ export declare const streamToString: ({ encoding }: { encoding?: BufferEncoding | undefined; }) => (stream: Stream) => Task<string>; /** * Returns a stream with text. * @desczh * 把文本转换为流 * @since 0.2.2 */ export declare const stringToStream: ({ encoding }: { encoding?: BufferEncoding | undefined; }) => (a: string) => Stream; /** * Reads a certain amount of bytes from the beginning of a Stream, returning a Buffer. * The amount of data read might be smaller if the stream ends before it could return the amount of data requested. * * If the `peek` argument is true, the data is put back into the beginning of the stream, so it can be consumed by another function * * Note that this function will pause the stream, so you might need to call the `resume` method on it to make it flow again. * * If passing a stream that has already ended, the function could enter into an infinite loop and return a Promise that never resolves. It's your responsibility to ensure that streams passed to this function still have data to return. * * @desczh * 从流的开始位置读取给定的长度的内容块 * 如果peek为真,数据将放回到流中。 * * @since 0.2.0 */ export declare const readChunkFromStream: ({ size, peek }: { size: number; peek?: boolean | undefined; }) => (stream: Readable) => Task<Buffer>;