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";
// src/utils/list.ts
import {
List$Empty,
List$isNonEmpty,
List$NonEmpty,
List$NonEmpty$first,
List$NonEmpty$rest
} from "../../prelude.mjs";
function toArray(list) {
const array = [];
let current = list;
while (List$isNonEmpty(current)) {
array.push(List$NonEmpty$first(current));
current = List$NonEmpty$rest(current);
}
return array;
}
// src/utils/result.ts
import { Result$Error, Result$Ok } from "../../prelude.mjs";
function toResult(value) {
return value === null || value === void 0 ? Result$Error(void 0) : Result$Ok(value);
}
toResult.fromThrows = function(throws) {
try {
return Result$Ok(throws());
} catch (error) {
return Result$Error(
error instanceof Error ? error.message : String(error)
);
}
};
toResult.fromPromise = function(promise) {
return promise.then(
(value) => Result$Ok(value),
(error) => Result$Error(error instanceof Error ? error.message : String(error))
);
};
// src/gossamer/readable_stream.ffi.ts
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 new ReadableStream(toUnderlyingSource(toArray(source)));
};
var from_iterator = (iterator) => {
return ReadableStream.from(iterator);
};
var from_async_iterator = (iterator) => {
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,
get_byob_reader,
get_reader,
is_locked,
new_,
pipe_through,
pipe_to,
tee
};