// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { toReadableStream } from "jsr:/@std/io@^0.224.1/to-readable-stream";
import type { Closer, Reader } from "jsr:/@std/io@^0.224.1/types";
export type { Closer };

/**
 * Options for {@linkcode readableStreamFromReader}.
 *
 * @deprecated This will be removed in 1.0.0. Use {@linkcode https://jsr.io/@std/io/doc/~/toReadableStream | toReadableStream} instead.
 */
export interface ReadableStreamFromReaderOptions {
  /** If the `reader` is also a `Closer`, automatically close the `reader`
   * when `EOF` is encountered, or a read error occurs.
   *
   * @default {true}
   */
  autoClose?: boolean;

  /** The size of chunks to allocate to read, the default is ~16KiB, which is
   * the maximum size that Deno operations can currently support. */
  chunkSize?: number;

  /** The queuing strategy to create the `ReadableStream` with. */
  strategy?: { highWaterMark?: number | undefined; size?: undefined };
}

/**
 * Create a {@linkcode ReadableStream} of {@linkcode Uint8Array}s from a
 * {@linkcode https://jsr.io/@std/io/doc/types/~/Reader | Reader}.
 *
 * When the pull algorithm is called on the stream, a chunk from the reader
 * will be read.  When `null` is returned from the reader, the stream will be
 * closed along with the reader (if it is also a {@linkcode https://jsr.io/@std/io/doc/types/~/Closer | Closer}).
 *
 * @param reader A reader to convert into a `ReadableStream`.
 * @param options Options for the `readableStreamFromReader` function.
 * @returns A `ReadableStream` of `Uint8Array`s.
 *
 * @example Convert a `Deno.FsFile` into a readable stream:
 * ```ts no-eval no-assert
 * import { readableStreamFromReader } from "@std/streams/readable-stream-from-reader";
 *
 * using file = await Deno.open("./README.md", { read: true });
 * const fileStream = readableStreamFromReader(file);
 * ```
 *
 * @deprecated This will be removed in 1.0.0. Use {@linkcode https://jsr.io/@std/io/doc/~/toReadableStream | toReadableStream} instead.
 */
export function readableStreamFromReader(
  reader: Reader | (Reader & Closer),
  options: ReadableStreamFromReaderOptions = {},
): ReadableStream<Uint8Array> {
  return toReadableStream(reader, options);
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","x-jsr-backend":"modules","server":"cloudflare","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=EUkm7pwfHEVBxpECOhCLG7Dpn7CytBxgvd%2B5kdtydknx%2FL9mKn1UX8PwulLHBFjOzC7AnTY1o7FUYxhqXT63b0AnAoE5zpjyUJTJs%2BY1dZ%2FAFEJ4sv6Oft8%3D\"}]}","date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-allow-origin":"*","content-length":"2288","set-cookie":"__cf_bm=QEGwKLjLEFCF9zJd8P6AOrTpm7a4.aDJd9CxyTrOnr8-1785241875.1809871-1.0.1.1-L1T0V5RBQ1pRE13cFM4LP5pjcPPbatyA0IEd.fe0C8gZCxsK3VBZni2Wl2Ek3ira1_YD_ya8TH9VQQoOfDz2BRRILwtsPPxycgvBuVjNt67KGDpTgQqCuZxOHwv28v1Q; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-type":"text/typescript","cache-control":"public, max-age=31536000, immutable","alt-svc":"h3=\":443\"; ma=86400","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-content-type-options":"nosniff","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","etag":"\"e693daaaf3aa905efe5f190a42cd002a\"","access-control-expose-headers":"*","cf-ray":"a223e657dabb7db6-LAX","cross-origin-resource-policy":"cross-origin"},"url":"https://jsr.io/@std/streams/0.224.5/readable_stream_from_reader.ts","time":1785241875}