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

bufferfromfile

Package Overview
Dependencies
Maintainers
1
Versions
354
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bufferfromfile

Module in JavaScript providing convenient means for using files as standard ArrayBuffer making mmap behind the scene.

  • 0.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18
increased by28.57%
Maintainers
1
Weekly downloads
 
Created
Source

BufferFromFile

Module in JavaScript providing convenient means for using files as buffer making mmap behind the scene. BufferFromFile use mmap to map file from hard drive to memory returning ArrayBuffer or TypedBuffer which can be manipulated just like ordinary buffer. mmap() creates a new mapping in the virtual address space of the calling process. During mmap operation all performance issues addressed operation system, so it's nearly fastest way to get data from / store on hard drive with zero copy. Just like native version of the routine BufferFromFile can accept ( protection ), ( flags ), ( offset ), ( size ) and even ( advise ) parameters. BufferFromFIle works on Windows, OsX, Lynux other Unix-like systems. Does not depend of NaN and does not support deprecated versions of Nodejs.

Usage:

Sample simply mmap a file

var buffer = BufferFromFile( filePath ).Uint8Array();

// edit single byte

buffer[ 0 ] = 48 + Math.round( Math.random()*9 );

// print the buffer

console.log( 'buffer.length :',buffer.length );
console.log( 'buffer.toString :',buffer.toString() );

// unmap file

BufferFromFile.unmap( buffer );

Sample make read only buffer from a file

// mmap file

var filePath = __dirname + '/TestFile.txt';
var buffer = BufferFromFile({ filePath : filePath, protection : BufferFromFile.Protection.read }).Uint8Array();

// attempt to write data would give error

/* buffer[ 0 ] = 48 + Math.round( Math.random()*9 ); */

// read data and status

console.log( 'buffer.toString :',buffer.toString() );
console.log( 'status',BufferFromFile.status( buffer ) );

// unmap file

BufferFromFile.unmap( buffer );

Sample make read only buffer from a file

// mmap file

var buffer = BufferFromFile( filePath ).Uint8Array();

// write data

buffer[ 0 ] = 48 + Math.round( Math.random()*9 );

// flush written data to make sure data on will get on hard drive shortly

BufferFromFile.flush( buffer );

// rewrite data

buffer[ 0 ] = 48 + Math.round( Math.random()*9 );

// unmap file

BufferFromFile.unmap( buffer );

Keywords

FAQs

Package last updated on 10 Feb 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