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 = require('pieces')
var size = Math.pow(2, 14)
var input = document.createElement('input')
input.type = 'file'
input.onchange = function(e) {
var file = e.target.files[0]
pieces(file, size, function(error, array) {
var data = new Blob([array])
window.open(
URL.createObjectURL(data)
)
})
}
document.body.appendChild(input)
node
var pieces = require('pieces')
var path = '/path/to/file'
var size = Math.pow(2, 18)
pieces(path, size, function(error, buffer) {
console.log(buffer.toString())
})
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 = require('pieces')
var file|path = …
var size = …
pieces(file|path, size)
.on('progress', function(percentage) {
})
.on('data', function(bytes) {
})
.on('end', function() {
})
License
MIT