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

import type { Writer } from "jsr:/@std/io@^0.224.1/types";

export type { Writer };

/**
 * Create a {@linkcode https://jsr.io/@std/io/doc/types/~/Writer | Writer} from a {@linkcode WritableStreamDefaultWriter}.
 *
 * @param streamWriter A `WritableStreamDefaultWriter` to convert into a `Writer`.
 * @returns A `Writer` that writes to the `WritableStreamDefaultWriter`.
 *
 * @example Read from a file and write to stdout using a writable stream
 * ```ts no-eval no-assert
 * import { copy } from "@std/io/copy";
 * import { writerFromStreamWriter } from "@std/streams/writer-from-stream-writer";
 *
 * using file = await Deno.open("./README.md", { read: true });
 *
 * const writableStream = new WritableStream({
 *   write(chunk): void {
 *     console.log(chunk);
 *   },
 * });
 * const writer = writerFromStreamWriter(writableStream.getWriter());
 * await copy(file, writer);
 * ```
 *
 * @deprecated This will be removed in 1.0.0. Use {@linkcode WritableStreamDefaultWriter} directly.
 */
export function writerFromStreamWriter(
  streamWriter: WritableStreamDefaultWriter<Uint8Array>,
): Writer {
  return {
    async write(p: Uint8Array): Promise<number> {
      await streamWriter.ready;
      await streamWriter.write(p);
      return p.length;
    },
  };
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","etag":"\"495694507c00cc2672309ee70fe49ae1\"","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=SUZ6qyr2TXh_LmLn_g1ImT0shn5Rnbw3euPNFBHQupA-1785241875.1923792-1.0.1.1-wbtHkhDKTEtYsxcXxGEgvld6OqKo6.Wudqir.wWVTwWqf7SHWGYjJV6TAnvs64FmOQUZ6_0CafFKt6lUPbJWc.S9BVl61mZOiqJAmEhP3GqAk3fwSnz1Oc6vjj40D4wu; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","vary":"Origin","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=1OO1SMvAc%2F%2B22NI46yXBEGHGjqWiCOummigB1KBYaSUfx0PavKTsJrOCkqMH0%2FTX7K7VnN4RPoVpY5vDItqxwmXR%2B2FtcKmfJbqdHVDY3EdynJ6S7LjCBKo%3D\"}]}","content-type":"text/typescript","cf-ray":"a223e657eaf87db6-LAX","cache-control":"public, max-age=31536000, immutable","content-length":"1384","access-control-allow-origin":"*","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';","date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-expose-headers":"*","x-jsr-backend":"modules","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/streams/0.224.5/writer_from_stream_writer.ts","time":1785241875}