Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

file-disk

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

file-disk

Handles reads / writes on disk image files.

  • 0.0.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.3K
increased by13.45%
Maintainers
1
Weekly downloads
 
Created
Source

file-disk

Handles reads / writes on disk image files.

Build Status

API

Warning: The API exposed by this library is still forming and can change at any time!

FileDisk

new FileDisk(fd, readOnly, recordWrites)

  • 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.

FileDisk.getCapacity(callback(err, size))

  • callback(err, size) will be called with the size of the disk image in bytes

FileDisk.read(buffer, bufferOffset, length, fileOffset, callback(err, bytesRead, buffer))

FileDisk.write(buffer, bufferOffset, length, fileOffset, callback(err, bytesWritten))

FileDisk.flush(callback(err))

FileDisk.discard(offset, length, callback(err))

  • not implemented

FileDisk.getStream(highWaterMark, callback(err, stream))

  • highWaterMark [optional, defaults to 16384, minimum 16] is the size of chunks that will be read
  • callback(err, stream) will be called with a readable stream of the disk content

S3Disk

S3Disk acts like FileDisk except it reads the image file from S3 instead of the filesystem. S3Disk has readOnly and recordWrites enabled. This can not be changed.

new S3Disk(s3, bucket, key)

  • s3 is an s3 connection.
  • bucket is the S3 bucket to use.
  • key is the key (file name) to use in the bucket.

For more information about S3Disk parameters see the aws documentation

Examples

Read 1024 first bytes, write them starting at position 1024 then flush.


const Promise = require('bluebird');
const filedisk = Promise.promisifyAll(require('file-disk'), { multiArgs: true });

Promise.using(filedisk.openFile('/path/to/some/file', 'r+'), function(fd) {
	const disk = new filedisk.FileDisk(fd)

	// get file size
	return disk.getCapacityAsync()
	.spread(function(size) {
		console.log("size:", size);
		const buf = Buffer.alloc(1024);
		// read `buf.length` bytes starting at 0 from the file into `buf`
		return disk.readAsync(buf, 0, buf.length, 0);
	})
	.spread(function(bytesRead, buf) {
		// write `buf` into file starting at `buf.length` (in the file)
		return disk.writeAsync(buf, 0, buf.length, buf.length);
	})
	.spread(function(bytesWritten) {
		// flush
		return disk.flushAsync();
	});
});


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


const Promise = require('bluebird');
const filedisk = Promise.promisifyAll(require('file-disk'), { multiArgs: true });

const buf = Buffer.alloc(1024);

Promise.using(filedisk.openFile('/path/to/some/file', 'r'), function(fd) {
	const disk = new filedisk.FileDisk(fd, true, true);

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

FAQs

Package last updated on 01 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc