-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (79 loc) · 1.95 KB
/
index.js
File metadata and controls
100 lines (79 loc) · 1.95 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var RandomAccess = require('random-access-storage')
var unordered = require('unordered-set')
var inherits = require('inherits')
module.exports = RAF
function RAF (file) {
if (!(this instanceof RAF)) return new RAF(file)
RandomAccess.call(this)
this.file = file
this._free = []
this._used = []
}
inherits(RAF, RandomAccess)
RAF.prototype._alloc = function () {
var self = this
var reader = new Reader(this.file)
reader.onfree = onfree
unordered.add(this._free, reader)
function onfree () {
unordered.remove(self._used, reader)
unordered.add(self._free, reader)
}
}
RAF.prototype._next = function () {
if (!this._free.length) this._alloc()
var free = this._free[0]
unordered.remove(this._free, free)
unordered.add(this._used, free)
return free
}
RAF.prototype._stat = function (req) {
req.callback(null, {
size: this.file.size,
mtime: this.file.lastModifiedDate,
type: this.file.type
})
}
RAF.prototype._write = function (req) {
req.callback(new Error('Readonly'))
}
RAF.prototype._read = function (req) {
this._next().read(req)
}
RAF.prototype._close = function (req) {
for (var i = 0; i < this._used.length; i++) {
this._used[i].destroy()
}
req.callback()
}
function Reader (file) {
var self = this
this.onfree = null
this.file = file
this._index = 0
this._req = null
this._reader = new window.FileReader()
this._reader.onload = onload
this._reader.onerror = onerror
function onerror () {
call(new Error('Read failed'), null)
}
function onload (e) {
call(null, Buffer.from(e.target.result))
}
function call (err, val) {
var req = self._req
if (!req) return
self._req = null
self.onfree()
req.callback(err, val)
}
}
Reader.prototype.read = function (req) {
var slice = this.file.slice(req.offset, req.offset + req.size)
this._req = req
this._reader.readAsArrayBuffer(slice)
}
Reader.prototype.destroy = function () {
this._reader.abort()
}