Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

file-disk

balena-io-modules37kApache-2.08.0.1TypeScript support: included

Handles reads / writes on disk image files.

readme

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 by fs.open
  • readOnly a boolean (default false)
  • recordWrites, a boolean (default false); if you use readOnly without recordWrites, all write requests will be lost.
  • recordReads, a boolean (default false): cache reads in memory
  • discardIsZero, a boolean (default true): 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 }>

FileDisk.write(buffer, bufferOffset, length, fileOffset): Promise<{ bytesWritten: Number, buffer: Buffer }>

FileDisk.flush(): Promise<void>

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 Ranges: { 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 = require('file-disk');

await filedisk.withOpenFile('/path/to/some/file', 'r+', async (handle) => {
    const disk = new filedisk.FileDisk(handle)

    // get file size
    const size = await disk.getCapacity();
    console.log("size:", size);
    const buf = Buffer.alloc(1024);
    // read `buf.length` bytes starting at 0 from the file into `buf`
    const { bytesRead, buffer } = await disk.read(buf, 0, buf.length, 0);
    // write `buffer` into file starting at `buffer.length` (in the file)
    await disk.write(buf, 0, buf.length, buf.length);
    // flush
    await disk.flush();
});

Open a file readOnly, use the recordWrites mode, then stream the contents somewhere.


const filedisk = require('file-disk');

const BUF = Buffer.alloc(1024);

await filedisk.withOpenFile('/path/to/some/file', 'r', async (handle) => {
    const disk = new filedisk.FileDisk(handle, true, true);
    let bytesRead, bytesWritten, buffer;

    // read `BUF.length` bytes starting at 0 from the file into `BUF`
    { bytesRead, buffer } = await disk.read(BUF, 0, BUF.length, 0);
    // write `buffer` into file starting at `buffer.length` (in the file)
    { bytesWritten, buffer } = await disk.write(buffer, 0, buffer.length, buffer.length);
    const buf2 = Buffer.alloc(1024);
    // read what we've just written
    { bytesRead, buffer } = await disk.read(buf2, 0, buffer.length, 0);
    // writes are stored in memory
    assert(BUF.equals(buffer));
    const stream = await disk.getStream();
    // pipe the stream somewhere
    await new Promise((resolve, reject) => {
        stream.pipe(someWritableStream)
        .on('close', resolve)
        .on('error', reject);
    });
});

changelog

Change Log

All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to Semantic Versioning.

v8.0.1

(2020-11-26)

  • Add versionbot changelog [Alexis Svinartchouk]

v8.0.0

(2020-07-27)

  • Stop using bluebird [Alexis Svinartchouk]

v7.0.1

(2020-07-24)

  • Fix built files [Alexis Svinartchouk]

v7.0.0

(2020-07-24)

  • Set tsc target to es2018 [Alexis Svinartchouk]

v6.0.4

(2020-07-24)

  • Revert "Set typescript target to es2018" [Alexis Svinartchouk]

v6.0.3

(2020-07-24)

  • Set typescript target to es2018 [Alexis Svinartchouk]
  • Check that chunks are in the disk [Alexis Svinartchouk]

v6.0.2

(2020-07-14)

  • Add .resinci.yml file [Alexis Svinartchouk]
  • Update dependencies [Alexis Svinartchouk]
  • Update resin-lint -> balena-lint [Alexis Svinartchouk]
  • Use tslib [Alexis Svinartchouk]

6.0.1 - 2019-04-04

  • Add an overlapping discard test [Alexis Svinartchouk]
  • Fix readme: remove getBlockMap, add GetRanges [Alexis Svinartchouk]

6.0.0 - 2019-04-04

  • Remove no longer used .eslintrc.json file [Alexis Svinartchouk]
  • Remove travis [Alexis Svinartchouk]
  • Update ts-node and mocha [Alexis Svinartchouk]
  • Remove blockmap dependency [Alexis Svinartchouk]

5.1.1 - 2019-03-21

  • Don't use bluebird for promisifying fs read and write [Alexis Svinartchouk]
  • Update resin-lint to ^3.0.1 [Alexis Svinartchouk]
  • Add noImplicitAny, noUnusedLocals, noUnusedParameters and strictNullChecks [Alexis Svinartchouk]

5.1.0 - 2018-12-21

  • Add BufferDisk: a Disk backed by a Buffer [Alexis Svinartchouk]

5.0.0 - 2018-11-28

v4.1.3 - 2018-06-01

  • Fix resin-lint command, aws-sdk is a dependency, not a dev dependency #17 [Alexis Svinartchouk]

v4.1.2 - 2018-04-02

  • Create .map.js files and publish them #16 [Alexis Svinartchouk]
  • Add types to package.json #16 [Alexis Svinartchouk]

v4.1.1 - 2018-03-26

  • Fix DiskStream._read() method. #14 [Alexis Svinartchouk]

v4.1.0 - 2018-03-06

  • Add a Disk.getTransformStream method. #11 [Alexis Svinartchouk]

v4.0.0 - 2018-03-02

  • Update blockmap to 3.0.0 #9 [Alexis Svinartchouk]

v3.0.0 - 2018-03-02

  • Simplify DiskChunk constructor #6 [Alexis Svinartchouk]
  • Use async / await intead of .then as much as possible. #6 [Alexis Svinartchouk]
  • Use Bluebird instead of Promise for naming Bluebird #6 [Alexis Svinartchouk]
  • Convert to TypeScript #6 [Alexis Svinartchouk]
  • Use promises everywhere, no more callbacks. #6 [Alexis Svinartchouk]
  • Use fat arrow functions everywhere #6 [Alexis Svinartchouk]

v2.0.0 - 2018-02-28

  • Record discards, generate blockmaps #3 [Alexis Svinartchouk]