ya-webadb/libraries/struct/src/readable.ts
2024-10-31 17:26:37 +08:00

34 lines
1.1 KiB
TypeScript

// TODO: allow over reading (returning a `Uint8Array`, an `offset` and a `length`) to avoid copying
import type { MaybePromiseLike } from "./utils.js";
export class ExactReadableEndedError extends Error {
constructor() {
super("ExactReadable ended");
Object.setPrototypeOf(this, new.target.prototype);
}
}
export interface ExactReadable {
readonly position: number;
/**
* Read data from the underlying data source.
*
* The stream must return exactly `length` bytes or data. If that's not possible
* (due to end of file or other error condition), it must throw an {@link ExactReadableEndedError}.
*/
readExactly(length: number): Uint8Array;
}
export interface AsyncExactReadable {
readonly position: number;
/**
* Read data from the underlying data source.
*
* The stream must return exactly `length` bytes or data. If that's not possible
* (due to end of file or other error condition), it must throw an {@link ExactReadableEndedError}.
*/
readExactly(length: number): MaybePromiseLike<Uint8Array>;
}