"use strict";
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.readerFromStreamReader = void 0;
const buffer_js_1 = require("./buffer.js");
const write_all_js_1 = require("./write_all.js");
/**
 * Create a {@linkcode Reader} from a {@linkcode ReadableStreamDefaultReader}.
 *
 * @example
 * ```ts
 * import { copy } from "@std/io/copy";
 * import { readerFromStreamReader } from "@std/io/reader_from_stream_reader";
 *
 * const res = await fetch("https://deno.land");
 * using file = await Deno.open("./deno.land.html", { create: true, write: true });
 *
 * const reader = readerFromStreamReader(res.body!.getReader());
 * await copy(reader, file);
 * ```
 */
function readerFromStreamReader(streamReader) {
    const buffer = new buffer_js_1.Buffer();
    return {
        async read(p) {
            if (buffer.empty()) {
                const res = await streamReader.read();
                if (res.done) {
                    return null; // EOF
                }
                await (0, write_all_js_1.writeAll)(buffer, res.value);
            }
            return buffer.read(p);
        },
    };
}
exports.readerFromStreamReader = readerFromStreamReader;
