pieces
pieces is a utility that takes a file (browser) or file path (node) and a piece length, and generates a collection of SHA1 hashes that can be used when creating BitTorrent files.
Install
npm install pieces
Usage
browser(ify)
var pieces =var size = Math // 16kb// Typically you’ll get the File object from// an input[type=file], but you you could also// just create your own Blob.var input = documentinputtype = 'file'input {var file = etargetfiles0}documentbody
node
var pieces =var path = '/path/to/file'var size = Math // 256kb// You should probably make sure the path// exists before running pieces
Events
When you start processing larger files, you’ll notice that it takes a while for the callback to be fired (particularly in browsers). To make this more managable, the pieces object emits progress
, data
, and end
events.
Example
Where the implementation differences are denoted with a pipe (ie. browser|server).
var pieces =var file|path = …var size = …