Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 40a60ca1 authored by Simon Chan's avatar Simon Chan
Browse files

feat(stream): add `Consumable.WrapByteReadableStream` and `MaybeConsumable.WrapWritableStream`

parent a835eb81
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
---
"@yume-chan/stream-extra": minor
---

Add `Consumable.WrapByteReadableStream` and `MaybeConsumable.WrapWritableStream`
+5 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import type {
} from "./consumable/index.js";
import {
    ConsumableReadableStream,
    ConsumableWrapByteReadableStream,
    ConsumableWrapWritableStream,
    ConsumableWritableStream,
} from "./consumable/index.js";
@@ -17,6 +18,7 @@ export class Consumable<T> {
    static readonly WritableStream = ConsumableWritableStream;
    static readonly WrapWritableStream = ConsumableWrapWritableStream;
    static readonly ReadableStream = ConsumableReadableStream;
    static readonly WrapByteReadableStream = ConsumableWrapByteReadableStream;

    readonly #task: Task;
    readonly #resolver: PromiseResolver<void>;
@@ -75,4 +77,7 @@ export namespace Consumable {
        ConsumableReadableStreamController<T>;
    export type ReadableStreamSource<T> = ConsumableReadableStreamSource<T>;
    export type ReadableStream<T> = typeof ConsumableReadableStream<T>;

    export type WrapByteReadableStream =
        typeof ConsumableWrapByteReadableStream;
}
+1 −0
Original line number Diff line number Diff line
export * from "./readable.js";
export * from "./wrap-byte-readable.js";
export * from "./wrap-writable.js";
export * from "./writable.js";
+33 −0
Original line number Diff line number Diff line
import type { Consumable } from "../consumable.js";
import { ReadableStream } from "../stream.js";

import { ConsumableReadableStream } from "./readable.js";

export class ConsumableWrapByteReadableStream extends ReadableStream<
    Consumable<Uint8Array>
> {
    constructor(
        stream: ReadableStream<Uint8Array>,
        chunkSize: number,
        min?: number,
    ) {
        const reader = stream.getReader({ mode: "byob" });
        let array = new Uint8Array(chunkSize);
        super({
            async pull(controller) {
                const { done, value } = await reader.read(array, { min });
                if (done) {
                    controller.close();
                    return;
                }

                await ConsumableReadableStream.enqueue(controller, value);

                array = new Uint8Array(value.buffer);
            },
            cancel(reason) {
                return reader.cancel(reason);
            },
        });
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -302,7 +302,7 @@ export declare class ReadableStreamBYOBReader {
 * @public
 */
export declare interface ReadableStreamBYOBReaderReadOptions {
    min?: number;
    min?: number | undefined;
}

/**