-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
171 lines (133 loc) · 3.88 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
const RandomAccess = require('random-access-storage')
const isOptions = require('is-options')
const b4a = require('b4a')
const DEFAULT_PAGE_SIZE = 1024 * 1024
module.exports = class RAM extends RandomAccess {
constructor (opts) {
super()
if (typeof opts === 'number') opts = { length: opts }
if (!opts) opts = {}
if (b4a.isBuffer(opts)) {
opts = { length: opts.length, buffer: opts }
}
if (!isOptions(opts)) opts = {}
this.length = opts.length || 0
this.pageSize = opts.length || opts.pageSize || DEFAULT_PAGE_SIZE
this.buffers = []
if (opts.buffer) this.buffers.push(opts.buffer)
}
static reusable () {
const all = new Map()
const RAM = this
return function createStorage (name) {
const existing = all.get(name)
const ram = existing ? existing.clone() : new RAM()
if (!existing || existing.closed) {
// only overwrite storage if its closed
// this techically also wrong but better than storing a clone instead...
all.set(name, ram)
}
ram.on('unlink', function () {
if (all.get(name) === ram) all.delete(name)
})
return ram
}
}
_stat (req) {
const st = {
size: this.length,
blksize: this.pageSize,
blocks: 0
}
for (let i = 0; i < this.buffers.length; i++) {
if (this.buffers[i]) st.blocks += this.buffers[i].byteLength / 512
}
req.callback(null, st)
}
_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.length) this.length = len
while (start < req.size) {
const page = this._page(i++, true)
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)
}
_read (req) {
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0
if (req.offset + req.size > this.length) {
return req.callback(new Error('Could not satisfy length'), null)
}
const data = b4a.alloc(req.size)
while (start < req.size) {
const page = this._page(i++, false)
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)
}
_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.length) {
const buf = this.buffers[i]
if (buf) buf.fill(0, rel)
}
if (req.offset + req.size > this.length) {
req.size = Math.max(0, this.length - req.offset)
}
while (start < req.size) {
if (rel === 0 && req.size - start >= this.pageSize) {
this.buffers[i] = undefined
}
rel = 0
i += 1
start += this.pageSize - rel
}
if (req.offset + req.size >= this.length) {
this.length = req.offset
}
req.callback(null, null)
}
_unlink (req) {
this._buffers = []
this.length = 0
req.callback(null, null)
}
_page (i, upsert) {
let page = this.buffers[i]
if (page || !upsert) return page
page = this.buffers[i] = b4a.alloc(this.pageSize)
return page
}
toBuffer () {
const buf = b4a.alloc(this.length)
for (let i = 0; i < this.buffers.length; i++) {
if (this.buffers[i]) b4a.copy(this.buffers[i], buf, i * this.pageSize)
}
return buf
}
clone () {
const ram = new RAM()
ram.length = this.length
ram.pageSize = this.pageSize
ram.buffers = this.buffers.map((buffer) => b4a.from(buffer))
return ram
}
}