-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (137 loc) · 4.08 KB
/
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const RAS = require('random-access-storage')
const b4a = require('b4a')
const DEFAULT_PAGE_SIZE = 1024 * 1024
module.exports = class MemoryOverlayStorage extends RAS {
constructor (a, opts = {}) {
super()
this._original = a
this._changes = []
this._originalSize = 0
this._size = 0
this.pageSize = opts.pageSize || DEFAULT_PAGE_SIZE
}
_open (req) {
this._original.open((err) => {
if (err) return req.callback(err)
this._original.stat((_, st) => {
this._size = st && st.size
this._originalSize = this._size
req.callback(null)
})
})
}
_copyInChanges (req, data) {
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0
while (start < req.size) {
const p = i++
const page = p < this._changes.length ? this._changes[p] : null
const avail = this.pageSize - rel
const wanted = req.size - start
const len = avail < wanted ? avail : wanted
if (page) b4a.copy(page, data, start, rel, rel + len)
start += len
rel = 0
}
req.callback(null, data)
}
_read (req) {
const end = req.offset + req.size
if (end > this._size) return req.callback(new Error('Could not satisfy length'))
const buf = b4a.alloc(req.size)
if (req.offset >= this._originalSize) {
this._copyInChanges(req, buf)
return
}
const oSize = end > this._originalSize ? req.size - (end - this._originalSize) : req.size
this._original.read(req.offset, oSize, (err, oBuf) => {
if (err) return req.callback(err, null)
buf.set(oBuf)
this._copyInChanges(req, buf)
})
}
async _loadPage (p) {
if (p < this._changes.length && this._changes[p] !== null) {
return this._changes[p]
}
const offset = p * this.pageSize
const buf = b4a.alloc(this.pageSize)
if (offset < this._originalSize) {
const end = offset + this.pageSize
const oSize = end > this._originalSize ? this.pageSize - (end - this._originalSize) : this.pageSize
const oBuf = await new Promise((resolve, reject) => {
this._original.read(offset, oSize, (err, oBuf) => {
if (err) return reject(err)
resolve(oBuf)
})
})
buf.set(oBuf)
}
while (this._changes.length <= p) this._changes.push(null)
if (this._changes[p] === null) this._changes[p] = buf
return this._changes[p]
}
async _write (req) {
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0
const len = req.offset + req.size
if (len > this._size) this._size = len
while (start < req.size) {
const p = i++
let page = null
try {
page = await this._loadPage(p)
} catch (err) {
return req.callback(err, null)
}
const free = this.pageSize - rel
const end = free < (req.size - start)
? start + free
: req.size
b4a.copy(req.data, page, rel, start, end)
start = end
rel = 0
}
req.callback(null, null)
}
async _del (req) {
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0
if (rel && req.offset + req.size >= this._size) {
try {
const buf = await this._loadPage(i)
if (buf) buf.fill(0, rel)
} catch (err) {
return req.callback(err, null)
}
}
if (req.offset + req.size > this._size) {
req.size = Math.max(0, this._size - req.offset)
}
while (start < req.size) {
if (rel === 0 && req.size - start >= this.pageSize) {
try {
const buf = await this._loadPage(i++)
buf.fill(0)
} catch (err) {
return req.callback(err, null)
}
}
rel = 0
start += this.pageSize - rel
}
if (req.offset + req.size >= this._size) {
this._size = req.offset
}
req.callback(null, null)
}
_stat (req) {
this.open((err) => {
if (err) return req.callback(err, null)
req.callback(null, { size: this._size })
})
}
}