node:stream/consumers
Node-compatible stream consumption utilities (node:stream/consumers).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed.
import ... from "node:stream/consumers"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import { Readable } from "node:stream";import { text, json } from "node:stream/consumers";
const out = await text(Readable.from(["hello ", "world"]));console.log(out); // "hello world"const parsed = await json(Readable.from(['{"ok":true}']));console.log(parsed);arrayBufferv0.0.0-alpha.2stream completely and resolve with an ArrayBuffer of its bytes.blobv0.0.0-alpha.2stream completely and resolve with a Blob of its bytes.bufferv0.0.0-alpha.2stream completely and resolve with a Buffer (Uint8Array subclass).bytesv0.0.0-alpha.2stream completely and resolve with a Uint8Array of its bytes.jsonv0.0.0-alpha.2stream completely, decode as UTF-8, and parse with JSON.parse.textv0.0.0-alpha.2stream completely and decode as a UTF-8 string.Functions
Section titled “Functions”arrayBuffer
Section titled “arrayBuffer”export function arrayBuffer(stream: StreamConsumerInput): Promise<ArrayBuffer>;
Consume stream completely and resolve with an ArrayBuffer of its bytes.
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
Promise<ArrayBuffer> — Promise resolving to the collected ArrayBuffer.export function blob(stream: StreamConsumerInput): Promise<BlobLike>;
Consume stream completely and resolve with a Blob of its bytes.
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
buffer
Section titled “buffer”export function buffer(stream: StreamConsumerInput): Promise<Uint8Array>;
Consume stream completely and resolve with a Buffer (Uint8Array subclass).
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
Promise<Uint8Array> — Promise resolving to the collected Buffer bytes.export function bytes(stream: StreamConsumerInput): Promise<Uint8Array>;
Consume stream completely and resolve with a Uint8Array of its bytes.
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
Promise<Uint8Array> — Promise resolving to the collected Uint8Array.export function json(stream: StreamConsumerInput): Promise<unknown>;
Consume stream completely, decode as UTF-8, and parse with JSON.parse.
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
Promise<unknown> — Promise resolving to the parsed JSON value.export function text(stream: StreamConsumerInput): Promise<string>;
Consume stream completely and decode as a UTF-8 string.
| Name | Type | Description |
|---|---|---|
stream | StreamConsumerInput | Readable Node stream, Web stream, or async iterable. |
Promise<string> — Promise resolving to the decoded UTF-8 string.Interfaces
Section titled “Interfaces”BlobLike
Section titled “BlobLike”export interface BlobLike {
readonly size: number;
readonly type: string;
arrayBuffer(): Promise<ArrayBuffer>;
bytes(): Promise<Uint8Array>;
text(): Promise<string>;
slice(start?: number, end?: number, contentType?: string): BlobLike;
}
Minimal Blob structural interface returned by blob().
| Property | Type | Description | Since |
|---|---|---|---|
size readonly | number | Total size of the blob in bytes. | v0.0.0-alpha.2 |
type readonly | string | MIME media type string (empty string if unspecified). | v0.0.0-alpha.2 |
BlobLike.arrayBuffer
Section titled “BlobLike.arrayBuffer”arrayBuffer(): Promise<ArrayBuffer>;
Read blob contents into a new ArrayBuffer.
BlobLike.bytes
Section titled “BlobLike.bytes”bytes(): Promise<Uint8Array>;
Read blob contents into a Uint8Array.
BlobLike.text
Section titled “BlobLike.text”text(): Promise<string>;
Decode blob contents as UTF-8 text.
Promise<string>BlobLike.slice
Section titled “BlobLike.slice”slice(start?: number, end?: number, contentType?: string): BlobLike;
Create a sliced Blob sharing the underlying data range.
| Name | Type | Description |
|---|---|---|
start optional | number | — |
end optional | number | — |
contentType optional | string | — |
Type Aliases
Section titled “Type Aliases”StreamConsumerInput
Section titled “StreamConsumerInput”export type StreamConsumerInput = AsyncIterable<unknown> | { getReader(): unknown };
Stream-like input accepted by node:stream/consumers helpers (Node Readable,
Web ReadableStream, or any AsyncIterable).
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** Consume stream into an ArrayBuffer. @since 0.0.0-alpha.2 */
arrayBuffer: typeof arrayBuffer;
/** Consume stream into a Blob. @since 0.0.0-alpha.2 */
blob: typeof blob;
/** Consume stream into a Buffer. @since 0.0.0-alpha.2 */
buffer: typeof buffer;
/** Consume stream into a Uint8Array. @since 0.0.0-alpha.2 */
bytes: typeof bytes;
/** Consume stream and parse JSON. @since 0.0.0-alpha.2 */
json: typeof json;
/** Consume stream and decode UTF-8 text. @since 0.0.0-alpha.2 */
text: typeof text;
};
export default _default;
Default export object providing { arrayBuffer, blob, buffer, bytes, json, text }.
