UNPKG

generic-filehandle2

Version:

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

50 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * 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. */ class BlobFile { constructor(blob) { this.blob = blob; } 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; const slice = this.blob.slice(start, end); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition return slice.bytes ? slice.bytes() : new Uint8Array(await slice.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 { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition return this.blob.bytes ? this.blob.bytes() : new Uint8Array(await this.blob.arrayBuffer()); } } async stat() { return { size: this.blob.size }; } async close() { return; } } exports.default = BlobFile; //# sourceMappingURL=blobFile.js.map