Current section
Files
Jump to
Current section
Files
src/gossamer/readable_stream.ffi.mjs
// src/gossamer/readable_stream.ffi.ts
import * as $readableStream from "./readable_stream.mjs";
import { toArray } from "../utils/list.ffi.mjs";
import { toResult } from "../utils/result.ffi.mjs";
function toUnderlyingSource(options) {
const result = {};
for (const option of options) {
if ($readableStream.UnderlyingSource$isStart(option)) {
result.start = $readableStream.UnderlyingSource$Start$0(option);
} else if ($readableStream.UnderlyingSource$isPull(option)) {
result.pull = $readableStream.UnderlyingSource$Pull$0(option);
} else if ($readableStream.UnderlyingSource$isCancel(option)) {
result.cancel = $readableStream.UnderlyingSource$Cancel$0(option);
}
}
return result;
}
function toStreamPipeOptions(options) {
const result = {};
for (const option of toArray(options)) {
if ($readableStream.StreamPipeOption$isPreventAbort(option)) {
result.preventAbort = true;
} else if ($readableStream.StreamPipeOption$isPreventCancel(option)) {
result.preventCancel = true;
} else if ($readableStream.StreamPipeOption$isPreventClose(option)) {
result.preventClose = true;
} else if ($readableStream.StreamPipeOption$isSignal(option)) {
result.signal = $readableStream.StreamPipeOption$Signal$0(option);
}
}
return result;
}
var new_ = (source) => {
return toResult.fromThrows(
() => new ReadableStream(toUnderlyingSource(toArray(source)))
);
};
var from_pull = (pull) => {
return new ReadableStream({ pull });
};
var from_iterator = (iterator) => {
if (typeof ReadableStream.from !== "function") {
throw new Error(
"readable_stream.from_iterator is unavailable on Bun - see https://github.com/oven-sh/bun/issues/3700"
);
}
return ReadableStream.from(iterator);
};
var from_async_iterator = (iterator) => {
if (typeof ReadableStream.from !== "function") {
throw new Error(
"readable_stream.from_async_iterator is unavailable on Bun - see https://github.com/oven-sh/bun/issues/3700"
);
}
return ReadableStream.from(iterator);
};
var is_locked = (stream) => {
return stream.locked;
};
var cancel = (stream, reason) => {
return toResult.fromPromise(stream.cancel(reason).then(() => void 0));
};
var get_reader = (stream) => {
return toResult.fromThrows(() => stream.getReader());
};
var get_byob_reader = (stream) => {
return toResult.fromThrows(() => stream.getReader({ mode: "byob" }));
};
var pipe_through = (stream, [readable, writable], options) => {
return toResult.fromThrows(
() => stream.pipeThrough(
{ readable, writable },
toStreamPipeOptions(options)
)
);
};
var pipe_to = (stream, destination, options) => {
return toResult.fromPromise(
stream.pipeTo(
destination,
toStreamPipeOptions(options)
).then(() => void 0)
);
};
var tee = (stream) => {
return toResult.fromThrows(() => stream.tee());
};
var async_iterator = (stream) => {
return stream.values();
};
export {
async_iterator,
cancel,
from_async_iterator,
from_iterator,
from_pull,
get_byob_reader,
get_reader,
is_locked,
new_,
pipe_through,
pipe_to,
tee
};