-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathread.js
84 lines (67 loc) · 1.91 KB
/
read.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* global FileReader */
const { Readable } = require('readable-stream')
const toBuffer = require('typedarray-to-buffer')
class FileReadStream extends Readable {
constructor (file, opts = {}) {
super(opts)
// save the read offset
this._offset = 0
this._ready = false
this._file = file
this._size = file.size
this._chunkSize = opts.chunkSize || Math.max(this._size / 1000, 200 * 1024)
// create the reader
const reader = new FileReader()
reader.onload = () => {
// get the data chunk
this.push(toBuffer(reader.result))
}
reader.onerror = () => {
this.emit('error', reader.error)
}
this.reader = reader
// generate the header blocks that we will send as part of the initial payload
this._generateHeaderBlocks(file, opts, (err, blocks) => {
// if we encountered an error, emit it
if (err) {
return this.emit('error', err)
}
// push the header blocks out to the stream
if (Array.isArray(blocks)) {
blocks.forEach(block => this.push(block))
}
this._ready = true
this.emit('_ready')
})
}
_generateHeaderBlocks (file, opts, callback) {
callback(null, [])
}
_read () {
if (!this._ready) {
this.once('_ready', this._read.bind(this))
return
}
const startOffset = this._offset
let endOffset = this._offset + this._chunkSize
if (endOffset > this._size) endOffset = this._size
if (startOffset === this._size) {
this.destroy()
this.push(null)
return
}
this.reader.readAsArrayBuffer(this._file.slice(startOffset, endOffset))
// update the stream offset
this._offset = endOffset
}
destroy () {
this._file = null
if (this.reader) {
this.reader.onload = null
this.reader.onerror = null
try { this.reader.abort() } catch (e) {};
}
this.reader = null
}
}
module.exports = FileReadStream