mirror of
https://github.com/signalapp/libsignal.git
synced 2026-04-25 17:25:18 +02:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
//
|
|
// Copyright 2024 Signal Messenger, LLC.
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
//
|
|
|
|
import { InputStream } from '../io.js';
|
|
|
|
export class ErrorInputStream extends InputStream {
|
|
public static Error = class extends Error {};
|
|
|
|
read(_amount: number): Promise<Uint8Array<ArrayBuffer>> {
|
|
throw new ErrorInputStream.Error();
|
|
}
|
|
skip(_amount: number): Promise<void> {
|
|
throw new ErrorInputStream.Error();
|
|
}
|
|
}
|
|
|
|
export class Uint8ArrayInputStream extends InputStream {
|
|
data: Uint8Array<ArrayBuffer>;
|
|
|
|
constructor(data: Uint8Array<ArrayBuffer>) {
|
|
super();
|
|
this.data = data;
|
|
}
|
|
|
|
read(amount: number): Promise<Uint8Array<ArrayBuffer>> {
|
|
const read_amount = Math.min(amount, this.data.length);
|
|
const read_data = this.data.subarray(0, read_amount);
|
|
this.data = this.data.subarray(read_amount);
|
|
return Promise.resolve(read_data);
|
|
}
|
|
|
|
skip(amount: number): Promise<void> {
|
|
if (amount > this.data.length) {
|
|
throw Error('skipped past end of data');
|
|
}
|
|
this.data = this.data.subarray(amount);
|
|
return Promise.resolve();
|
|
}
|
|
}
|