-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (27 loc) · 898 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* global FileReader */
var toBuffer = require('typedarray-to-buffer')
module.exports = function (file, opts) {
opts = opts || {}
var offset = opts.offset || 0
var chunkSize = opts.chunkSize || 1024 * 1024 // default 1MB chunk has tolerable perf on large files
return function (end, cb) {
if (end) return cb(end)
// If finished reading then stop
if (offset >= file.size) return cb(true)
var fileReader = new FileReader(file)
fileReader.onloadend = function loaded (event) {
var data = event.target.result
if (data instanceof ArrayBuffer) {
data = toBuffer(new Uint8Array(event.target.result))
}
cb(null, data)
}
fileReader.onerror = function (err) {
cb(err)
}
var endIndex = offset + chunkSize
var slice = file.slice(offset, endIndex)
fileReader.readAsArrayBuffer(slice)
offset = endIndex
}
}