UNPKG

netcdf4-wasm

Version:

NetCDF4 library compiled to WebAssembly with TypeScript bindings

49 lines 1.83 kB
"use strict"; // Lazy file reading - Node.js backend // // Uses synchronous positional reads (`fs.readSync` with a position argument) so // only the requested bytes are pulled off disk - the file is never slurped into // memory. This is the simplest genuinely-lazy path and needs no Web Worker, since // `readSync` is synchronous on every platform. Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeFileReader = void 0; class NodeFileReader { constructor(path, fsImpl) { this.closed = false; // Lazily require 'fs' only when no implementation is injected, so bundlers // building for the browser never need to resolve the Node builtin. this.fs = fsImpl ?? require("fs"); this.fd = this.fs.openSync(path, "r"); this._size = this.fs.fstatSync(this.fd).size; } get size() { return this._size; } read(offset, length) { if (this.closed) throw new Error("NodeFileReader is closed"); const end = Math.min(offset + length, this._size); const want = end - offset; if (offset < 0 || offset >= this._size || want <= 0) { return new Uint8Array(0); } const buffer = new Uint8Array(want); let read = 0; // readSync may return short; loop until the range is filled or EOF. while (read < want) { const n = this.fs.readSync(this.fd, buffer, read, want - read, offset + read); if (n <= 0) break; read += n; } return read === want ? buffer : buffer.subarray(0, read); } close() { if (this.closed) return; this.closed = true; this.fs.closeSync(this.fd); } } exports.NodeFileReader = NodeFileReader; //# sourceMappingURL=node-reader.js.map