@apr144/generic-filehandle
Version:
uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data
114 lines • 4.35 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const buffer_1 = require("buffer");
// Using this you can "await" the file like a normal promise
// https://blog.shovonhasan.com/using-promises-with-filereader/
function readBlobAsArrayBuffer(blob) {
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.onerror = () => {
fileReader.abort();
reject(new Error('problem reading blob'));
};
fileReader.onabort = () => {
reject(new Error('blob reading was aborted'));
};
fileReader.onload = () => {
if (fileReader.result && typeof fileReader.result !== 'string') {
resolve(fileReader.result);
}
else {
reject(new Error('unknown error reading blob'));
}
};
fileReader.readAsArrayBuffer(blob);
});
}
function readBlobAsText(blob) {
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.onerror = () => {
fileReader.abort();
reject(new Error('problem reading blob'));
};
fileReader.onabort = () => {
reject(new Error('blob reading was aborted'));
};
fileReader.onload = () => {
if (fileReader.result && typeof fileReader.result === 'string') {
resolve(fileReader.result);
}
else {
reject(new Error('unknown error reading blob'));
}
};
fileReader.readAsText(blob);
});
}
/**
* 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;
this.size = blob.size;
}
read(buffer, offset = 0, length, position = 0) {
return __awaiter(this, void 0, void 0, function* () {
// 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 { bytesRead: 0, buffer };
}
const start = position;
const end = start + length;
const result = yield readBlobAsArrayBuffer(this.blob.slice(start, end));
const resultBuffer = buffer_1.Buffer.from(result);
const bytesCopied = resultBuffer.copy(buffer, offset);
return { bytesRead: bytesCopied, buffer: resultBuffer };
});
}
readFile(options) {
return __awaiter(this, void 0, void 0, function* () {
let encoding;
if (typeof options === 'string') {
encoding = options;
}
else {
encoding = options && options.encoding;
}
if (encoding === 'utf8') {
return readBlobAsText(this.blob);
}
if (encoding) {
throw new Error(`unsupported encoding: ${encoding}`);
}
const result = yield readBlobAsArrayBuffer(this.blob);
return buffer_1.Buffer.from(result);
});
}
stat() {
return __awaiter(this, void 0, void 0, function* () {
return { size: this.size };
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
return;
});
}
}
exports.default = BlobFile;
//# sourceMappingURL=blobFile.js.map