Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { writeAll } from "./write_all.ts";
|
||||
import type { Closer, Writer } from "../types.d.ts";
|
||||
|
||||
function isCloser(value: unknown): value is Closer {
|
||||
return typeof value === "object" && value != null && "close" in value &&
|
||||
// deno-lint-ignore no-explicit-any
|
||||
typeof (value as Record<string, any>)["close"] === "function";
|
||||
}
|
||||
|
||||
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 `WritableStream` from a `Writer`. */
|
||||
export function writableStreamFromWriter(
|
||||
writer: Writer,
|
||||
options: WritableStreamFromWriterOptions = {},
|
||||
): WritableStream<Uint8Array> {
|
||||
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();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user