Skip to main content
Home
This release is 47 versions behind 1.0.16 — the latest version of @std/streams. Jump to latest

Utilities for working with the Web Streams API

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 years ago (0.203.0)
Package root>read_all.ts
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. import { Buffer } from "jsr:/@std/io@^0.203.0/buffer"; import type { Reader, ReaderSync } from "jsr:@std/internal@^0.203.0"; /** * @deprecated (will be removed after 1.0.0) Use ReadableStream and toArrayBuffer instead. * * Read Reader `r` until EOF (`null`) and resolve to the content as * Uint8Array`. * * ```ts * import { Buffer } from "@std/io/buffer"; * import { readAll } from "@std/streams/read-all"; * * // Example from stdin * const stdinContent = await readAll(Deno.stdin); * * // Example from file * const file = await Deno.open("my_file.txt", {read: true}); * const myFileContent = await readAll(file); * file.close(); * * // Example from buffer * const myData = new Uint8Array(100); * // ... fill myData array with data * const reader = new Buffer(myData.buffer); * const bufferContent = await readAll(reader); * ``` */ export async function readAll(r: Reader): Promise<Uint8Array> { const buf = new Buffer(); await buf.readFrom(r); return buf.bytes(); } /** * @deprecated (will be removed after 1.0.0) Use ReadableStream and toArrayBuffer instead. * * Synchronously reads Reader `r` until EOF (`null`) and returns the content * as `Uint8Array`. * * ```ts * import { Buffer } from "@std/io/buffer"; * import { readAllSync } from "@std/streams/read-all"; * * // Example from stdin * const stdinContent = readAllSync(Deno.stdin); * * // Example from file * const file = Deno.openSync("my_file.txt", {read: true}); * const myFileContent = readAllSync(file); * file.close(); * * // Example from buffer * const myData = new Uint8Array(100); * // ... fill myData array with data * const reader = new Buffer(myData.buffer); * const bufferContent = readAllSync(reader); * ``` */ export function readAllSync(r: ReaderSync): Uint8Array { const buf = new Buffer(); buf.readFromSync(r); return buf.bytes(); }