Current section

Files

Jump to
gossamer src utils bit_array.ffi.mjs
Raw

src/utils/bit_array.ffi.mjs

// src/utils/bit_array.ffi.ts
import {
BitArray$BitArray,
BitArray$BitArray$data,
BitArray$isBitArray
} from "../../prelude.mjs";
import { pad_to_bytes } from "../../gleam_stdlib/gleam/bit_array.mjs";
import { toResult } from "./result.ffi.mjs";
function toBufferSource(bitArray) {
return BitArray$BitArray$data(pad_to_bytes(bitArray));
}
function toUint8Array(bitArray) {
const view = BitArray$BitArray$data(pad_to_bytes(bitArray));
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
}
function wrapArrayBuffer(value) {
if (value instanceof ArrayBuffer) {
return BitArray$BitArray(new Uint8Array(value));
}
return value;
}
function unwrapBitArrayForClone(value) {
if (BitArray$isBitArray(value)) {
return toUint8Array(value).slice().buffer;
}
return value;
}
function toBitArrayResult(fn) {
return toResult.fromAsync(
async () => BitArray$BitArray(new Uint8Array(await fn()))
);
}
function toBitArrayStream(source) {
return source.pipeThrough(
new TransformStream({
transform(chunk, controller) {
controller.enqueue(BitArray$BitArray(chunk));
}
})
);
}
function fromBitArrayStream(target) {
const transform = new TransformStream({
transform(chunk, controller) {
controller.enqueue(toBufferSource(chunk));
}
});
transform.readable.pipeTo(target).catch(() => {
});
return transform.writable;
}
function fromBitArrayReadable(source) {
return source.pipeThrough(
new TransformStream({
transform(chunk, controller) {
controller.enqueue(toUint8Array(chunk));
}
})
);
}
export {
fromBitArrayReadable,
fromBitArrayStream,
toBitArrayResult,
toBitArrayStream,
toBufferSource,
toUint8Array,
unwrapBitArrayForClone,
wrapArrayBuffer
};