file-disk
Handles reads / writes on disk image files.
API
Warning: The API exposed by this library is still forming and can change at any time!
FileDisk
new FileDisk(fd, readOnly, recordWrites, recordReads, discardIsZero=true)
fd
is a file descriptor returned byfs.open
readOnly
a boolean (defaultfalse
)recordWrites
, a boolean (defaultfalse
); if you usereadOnly
withoutrecordWrites
, all write requests will be lost.recordReads
, a boolean (defaultfalse
): cache reads in memorydiscardIsZero
, a boolean (defaulttrue
): don't read discarded regions, return zero filled buffers instead.
FileDisk.getCapacity()
: Promise<Number>
FileDisk.read(buffer, bufferOffset, length, fileOffset)
: Promise<{ bytesRead: Number, buffer: Buffer }>
- behaves like fs.read
FileDisk.write(buffer, bufferOffset, length, fileOffset)
: Promise<{ bytesWritten: Number, buffer: Buffer }>
- behaves like fs.write
FileDisk.flush()
: Promise<void>
- behaves like fs.fdatasync
FileDisk.discard(offset, length)
: Promise<void>
FileDisk.getStream([position, [length, [highWaterMark]]])
: Promise<stream.Readable>
position
start reading from this offset (defaults to 0)length
read that amount of bytes (defaults to (disk capacity - position))highWaterMark
(defaults to 16384, minimum 16) is the size of chunks that will be read
FileDisk.getDiscardedChunks()
returns the list of discarded chunks. Each chunk
has a start
and end
properties. end
position is inclusive.
FileDisk.getRanges(blockSize)
: Promise<Range[]>
- using the disk's discarded chunks and the given blockSize, it returns a Promise
of an array of
Range
s:{ offset: number, length: number }
.
S3Disk
S3Disk
has been moved to a separate repository.
Examples
Read 1024 first bytes, write them starting at position 1024 then flush.
const filedisk = ; await filedisk;
Open a file readOnly, use the recordWrites mode, then stream the contents somewhere.
const filedisk = ; const BUF = Buffer; await filedisk;