@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
45 lines (44 loc) • 1.25 kB
JavaScript
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { writeAll } from "./write_all.js";
import { isCloser } from "./_common.js";
/**
* Create a {@linkcode WritableStream} from a {@linkcode Writer}.
*
* @example Usage
* ```ts no-assert
* import { toWritableStream } from "@std/io/to-writable-stream";
*
* const a = toWritableStream(Deno.stdout); // Same as `Deno.stdout.writable`
* ```
*
* @param writer The writer to write to
* @param options The options
* @returns The writable stream
*/
export function toWritableStream(writer, options) {
const { autoClose = true } = options ?? {};
return new WritableStream({
async write(chunk, controller) {
try {
await writeAll(writer, chunk);
}
catch (e) {
controller.error(e);
if (isCloser(writer) && autoClose) {
writer.close();
}
}
},
close() {
if (isCloser(writer) && autoClose) {
writer.close();
}
},
abort() {
if (isCloser(writer) && autoClose) {
writer.close();
}
},
});
}