nufatfs
Version:
A new async-friendly library for accessing FAT16 and FAT32 filesystems
35 lines (34 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createChunkingDriver = void 0;
function createChunkingDriver(underlying, maxSectors, sectorSize) {
return {
...underlying,
readSectors: async (sectorIndex, sectorCount) => {
let outputBuffers = [];
while (sectorCount > 0) {
let toRead = Math.min(sectorCount, maxSectors);
outputBuffers.push(await underlying.readSectors(sectorIndex, toRead));
sectorIndex += toRead;
sectorCount -= toRead;
}
const newBuffer = new Uint8Array(outputBuffers.reduce((a, b) => a + b.length, 0));
let bufferIndex = 0;
for (const bfr of outputBuffers) {
newBuffer.set(bfr, bufferIndex);
bufferIndex += bfr.length;
}
return newBuffer;
},
writeSectors: underlying.writeSectors !== null ? async (sectorIndex, data) => {
let offset = 0;
while (offset < data.length) {
let toWrite = data.subarray(offset, offset + Math.min(data.length - offset, sectorSize * maxSectors));
await underlying.writeSectors(sectorIndex, toWrite);
offset += toWrite.length;
sectorIndex += toWrite.length / sectorSize;
}
} : null,
};
}
exports.createChunkingDriver = createChunkingDriver;