UNPKG

generic-filehandle2

Version:

uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data

41 lines 1.3 kB
/** * Blob of binary data fetched from a local file (with FileReader). * * Adapted by Robert Buels and Garrett Stevens from the BlobFetchable object in * the Dalliance Genome Explorer, which is copyright Thomas Down 2006-2011. */ export default class BlobFile { constructor(blob) { this.blob = blob; this.size = blob.size; } async read(length, position = 0) { // short-circuit a read of 0 bytes here, because browsers actually sometimes // crash if you try to read 0 bytes from a local file! if (!length) { return new Uint8Array(0); } const start = position; const end = start + length; return new Uint8Array(await this.blob.slice(start, end).arrayBuffer()); } async readFile(options) { const encoding = typeof options === 'string' ? options : options?.encoding; if (encoding === 'utf8') { return this.blob.text(); } else if (encoding) { throw new Error(`unsupported encoding: ${encoding}`); } else { return new Uint8Array(await this.blob.arrayBuffer()); } } async stat() { return { size: this.size }; } async close() { return; } } //# sourceMappingURL=blobFile.js.map