MCPcopy Create free account
hub / github.com/denoland/std / toByteStream

Function toByteStream

streams/unstable_to_byte_stream.ts:32–78  ·  view source on GitHub ↗
(
  readable: ReadableStream<Uint8Array>,
)

Source from the content-addressed store, hash-verified

30 * @returns A BYOB ReadableStream.
31 */
32export function toByteStream(
33 readable: ReadableStream<Uint8Array>,
34): ReadableStream<Uint8Array> {
35 try {
36 const reader = readable.getReader({ mode: "byob" });
37 reader.releaseLock();
38 return readable;
39 } catch {
40 const reader = readable.getReader();
41 return new ReadableStream({
42 type: "bytes",
43 autoAllocateChunkSize: 1024,
44 async pull(controller) {
45 const value = await async function () {
46 while (true) {
47 const { done, value } = await reader.read();
48 if (done) return undefined;
49 if (value.length) return value;
50 }
51 }();
52
53 if (value == undefined) {
54 controller.close();
55 return controller.byobRequest!.respond(0);
56 }
57
58 const buffer = new Uint8Array(
59 controller.byobRequest!.view!.buffer,
60 controller.byobRequest!.view!.byteOffset,
61 controller.byobRequest!.view!.byteLength,
62 );
63 const size = buffer.length;
64 if (value.length > size) {
65 buffer.set(value.subarray(0, size));
66 controller.byobRequest!.respond(size);
67 controller.enqueue(value.subarray(size) as Uint8Array_);
68 } else {
69 buffer.set(value);
70 controller.byobRequest!.respond(value.length);
71 }
72 },
73 async cancel(reason) {
74 await reader.cancel(reason);
75 },
76 });
77 }
78}

Callers 10

#handleMethod · 0.90
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
#handleMethod · 0.85
#tarMethod · 0.85

Calls 1

releaseLockMethod · 0.45

Tested by

no test coverage detected