netcdf4-wasm
Version:
NetCDF4 library compiled to WebAssembly with TypeScript bindings
40 lines • 1.48 kB
JavaScript
;
// Lazy file reading - browser Blob/File backend
//
// Reads byte ranges out of a Blob/File with FileReaderSync. Because the read must
// be synchronous (libc read() is), this only works inside a Web Worker, where
// FileReaderSync exists. On the main thread there is no synchronous way to read a
// Blob, so construction throws with a clear message.
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlobReader = void 0;
function getFileReaderSync() {
return globalThis.FileReaderSync;
}
class BlobReader {
constructor(blob) {
this.blob = blob;
const ctor = getFileReaderSync();
if (!ctor) {
throw new Error("BlobReader requires FileReaderSync, which is only available inside a Web Worker. " +
"Run lazy Blob reading in a worker, or load the file with the default (eager) path.");
}
this.readerCtor = ctor;
}
get size() {
return this.blob.size;
}
read(offset, length) {
const end = Math.min(offset + length, this.blob.size);
if (offset < 0 || offset >= this.blob.size || end <= offset) {
return new Uint8Array(0);
}
const slice = this.blob.slice(offset, end);
const buffer = new this.readerCtor().readAsArrayBuffer(slice);
return new Uint8Array(buffer);
}
close() {
// Nothing to release.
}
}
exports.BlobReader = BlobReader;
//# sourceMappingURL=blob-reader.js.map