Continuous reading or writing to a file using random offsets and lengths
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore 0.2.0 restored Jul 10, 2013
.travis.yml add docs for .end() Jul 27, 2016
example.js update src Apr 15, 2016
index.js fix re-read bug Sep 2, 2016
package.json 1.3.1 Sep 2, 2016
test.js add .end([opts], cb) (#5) Jul 25, 2016


Continuous reading or writing to a file using random offsets and lengths

npm install random-access-file

build status


If you are receiving a file in multiple pieces in a distributed system it can be useful to write these pieces to disk one by one in various places throughout the file without having to open and close a file descriptor all the time.

random-access-file allows you to do just this.


var randomAccessFile = require('random-access-file')

var file = randomAccessFile('my-file.txt')

file.write(10, new Buffer('hello'), function(err) {
  // write a buffer to offset 10, 5, function(err, buffer) {
    console.log(buffer) // read 5 bytes from offset 10
    file.close(function() {
      console.log('file is closed')

file will use an open file descriptor. When you are done with the file you should call file.close().


var file = randomAccessFile(filename, [options])

Create a new file. Options include:

  truncate: false, // truncate the file before reading / writing
  length: someLength, // truncate the file to this size first
  readable: true, // should the file be opened as readable?
  writable: true  // should the file be opened as writable?

file.write(offset, buffer, [callback])

Write a buffer at a specific offset., length, callback)

Read a buffer at a specific offset. Callback is called with the buffer read.

file.end([options], callback)

Call this method when the entire file has been written. Options include:

  mtime: mtime, // set the file's mtime
  atime: atime // set the file's atime


Close the underlying file descriptor.


Unlink the underlying file.


Emitted when the file descriptor has been opened. You can access the fd using file.fd. You do not need to wait for this event before doing any reads/writes.


Emitted when the file has been closed.