@foxglove/ulog
Version:
PX4 ULog file reader
53 lines (44 loc) • 1.29 kB
text/typescript
import * as fs from "fs/promises";
import { Filelike } from "../file";
/**
* A FileReader implemented using the node.js filesystem API.
*/
export class FileReader implements Filelike {
constructor(filename: string) {
this.
}
async open(): Promise<number> {
await this.
return this.
}
async
if (this.
return this.
}
this.
const size = (await this.
if (size > Number.MAX_SAFE_INTEGER) {
throw new Error(`File size ${size} exceeds the maximum size`);
}
this.
return this.
}
async close(): Promise<void> {
await this.
}
/**
* Read up to `length` bytes starting from `offset` bytes.
*/
async read(offset: number, length: number): Promise<Uint8Array> {
const file = await this.
const data = new Uint8Array(length);
const res = await file.read(data, 0, length, offset);
return data.byteLength === res.bytesRead ? data : data.slice(0, res.bytesRead);
}
size(): number {
return this.
}
}