// 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";
import { toWritableStream } from "jsr:/@std/io@^0.224.1/to-writable-stream";

/**
 * Options for {@linkcode writableStreamFromWriter}.
 *
 * @deprecated This will be removed in 1.0.0. Use {@linkcode https://jsr.io/@std/io/doc/~/toWritableStream | toWritableStream} instead.
 */
export interface WritableStreamFromWriterOptions {
  /**
   * If the `writer` is also a `Closer`, automatically close the `writer`
   * when the stream is closed, aborted, or a write error occurs.
   *
   * @default {true}
   */
  autoClose?: boolean;
}

/**
 * Create a {@linkcode WritableStream} from a {@linkcode https://jsr.io/@std/io/doc/types/~/Writer | Writer}.
 *
 * @param writer A `Writer` to convert into a `WritableStream`.
 * @param options Options for the `writableStreamFromWriter` function.
 * @returns A `WritableStream` of `Uint8Array`s.
 *
 * @example Convert `Deno.stdout` into a writable stream
 * ```ts no-eval no-assert
 * // Note that you can directly get the writer from `Deno.stdout` by
 * // `Deno.stdout.writable`. This example is just for demonstration purposes;
 * // definitely not a recommended way.
 *
 * import { writableStreamFromWriter } from "@std/streams/writable-stream-from-writer";
 *
 * const stdoutStream = writableStreamFromWriter(Deno.stdout);
 * ```
 *
 * @deprecated This will be removed in 1.0.0. Use {@linkcode https://jsr.io/@std/io/doc/~/toWritableStream | toWritableStream} instead.
 */
export function writableStreamFromWriter(
  writer: Writer,
  options: WritableStreamFromWriterOptions = {},
): WritableStream<Uint8Array> {
  return toWritableStream(writer, options);
}

// denoCacheMetadata={"headers":{"cf-ray":"a223e657eaf67db6-LAX","age":"112265","content-length":"1777","access-control-allow-origin":"*","cache-control":"public, max-age=31536000, immutable","etag":"\"92b7650ef033f83d2a91c7147fb608d1\"","access-control-expose-headers":"*","server":"cloudflare","vary":"Origin","cross-origin-resource-policy":"cross-origin","cf-cache-status":"HIT","set-cookie":"__cf_bm=MX.0G4PSOFPKq4l.EBkRGBUVy5_lWOLbUFU9eXtx8bo-1785241875.1918752-1.0.1.1-77JvfeFI2JIfI0F16ODzmcpsI7zNaIYhPPrr9l6pdzKvU6Tgd4R8onSk8oTSfhpCF.52XNFHAGEzsem7K9L5XE5AMYCEX0O3pyMjHqkfEyWDWDj0EoqpJkqKgs8cOSXQ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=LhFvcOkCXGzQ2f48XN737AoEBFf8rpqvFbxfQZiZR0R5Lgh1smt9IARb5kKnu6VGfkI%2BGmRnqf2SjDg13OYN22vgjiMSuCfqqP29zr00EnIgcD0t0OTx7UE%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-type":"text/typescript","accept-ranges":"bytes","date":"Tue, 28 Jul 2026 12:31:15 GMT","alt-svc":"h3=\":443\"; ma=86400","x-jsr-backend":"modules","x-robots-tag":"noindex","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';"},"url":"https://jsr.io/@std/streams/0.224.5/writable_stream_from_writer.ts","time":1785241875}