-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
324 lines (261 loc) · 7.05 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const ras = require('random-access-storage')
const TYPE = { type: 'octet/stream' }
const requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem
const persistentStorage = navigator.persistentStorage || navigator.webkitPersistentStorage
const FileReader = window.FileReader
const Blob = window.Blob
createFile.DEFAULT_MAX_SIZE = Number.MAX_SAFE_INTEGER
createFile.requestQuota = requestQuota
module.exports = createFile
function requestQuota (n, force, cb) {
if (typeof force === 'function') return requestQuota(n, true, force)
persistentStorage.queryUsageAndQuota(function (used, quota) {
if (quota && !force) return cb(null, quota)
persistentStorage.requestQuota(n, function (quota) {
cb(null, quota)
}, cb)
}, cb)
}
function createFile (name, opts) {
if (!opts) opts = {}
const maxSize = opts.maxSize || createFile.DEFAULT_MAX_SIZE
const mutex = new Mutex()
let fs = null
let entry = null
let toDestroy = null
let readers = []
let writers = []
let deleters = []
return ras({ read, write, del, open, stat, close, destroy })
function read (req) {
const r = readers.pop() || new ReadRequest(readers, entry, mutex)
r.run(req)
}
function write (req) {
const w = writers.pop() || new WriteRequest(writers, entry, mutex)
w.run(req)
}
function del (req) {
const d = deleters.pop() || new DeleteRequest(deleters, entry, mutex)
d.run(req)
}
function close (req) {
readers = writers = deleters = entry = fs = null
req.callback(null)
}
function stat (req) {
entry.file(file => {
req.callback(null, file)
}, err => req.callback(err))
}
function destroy (req) {
toDestroy.remove(ondone, onerror)
function ondone () {
toDestroy = null
req.callback(null, null)
}
function onerror (err) {
toDestroy = null
req.callback(err, null)
}
}
function open (req) {
requestQuota(maxSize, false, function (err, granted) {
if (err) return onerror(err)
requestFileSystem(window.PERSISTENT, granted, function (res) {
fs = res
mkdirp(parentFolder(name), function () {
fs.root.getFile(name, { create: true }, function (e) {
entry = toDestroy = e
req.callback(null)
}, onerror)
})
}, onerror)
})
function mkdirp (name, ondone) {
if (!name) return ondone()
fs.root.getDirectory(name, { create: true }, ondone, function () {
mkdirp(parentFolder(name), function () {
fs.root.getDirectory(name, { create: true }, ondone, ondone)
})
})
}
function onerror (err) {
fs = entry = null
req.callback(err)
}
}
}
function parentFolder (path) {
const i = path.lastIndexOf('/')
const j = path.lastIndexOf('\\')
const p = path.slice(0, Math.max(0, i, j))
return /^\w:$/.test(p) ? '' : p
}
function WriteRequest (pool, entry, mutex) {
this.pool = pool
this.entry = entry
this.mutex = mutex
this.writer = null
this.req = null
this.locked = false
this.truncating = false
}
WriteRequest.prototype.makeWriter = function () {
const self = this
this.entry.createWriter(function (writer) {
self.writer = writer
writer.onwriteend = function () {
self.onwrite(null)
}
writer.onerror = function (err) {
self.onwrite(err)
}
self.run(self.req)
})
}
WriteRequest.prototype.onwrite = function (err) {
const req = this.req
this.req = null
if (this.locked) {
this.locked = false
this.mutex.release()
}
if (this.truncating) {
this.truncating = false
if (!err) return this.run(req)
}
this.pool.push(this)
req.callback(err, null)
}
WriteRequest.prototype.truncate = function () {
this.truncating = true
this.writer.truncate(this.req.offset)
}
WriteRequest.prototype.lock = function () {
if (this.locked) return true
this.locked = this.mutex.lock(this)
return this.locked
}
WriteRequest.prototype.run = function (req) {
this.entry.file(file => {
this.req = req
if (!this.writer || this.writer.length !== file.size) return this.makeWriter()
if (req.offset + req.size > file.size && !this.lock()) return
if (req.offset > this.writer.length) {
if (req.offset > file.size) return this.truncate()
return this.makeWriter()
}
this.writer.seek(req.offset)
this.writer.write(new Blob([req.data], TYPE))
}, err => req.callback(err))
}
function Mutex () {
this.queued = null
}
Mutex.prototype.release = function () {
const queued = this.queued
this.queued = null
for (let i = 0; i < queued.length; i++) {
queued[i].run(queued[i].req)
}
}
Mutex.prototype.lock = function (req) {
if (this.queued) {
this.queued.push(req)
return false
}
this.queued = []
return true
}
function ReadRequest (pool, entry, mutex) {
this.pool = pool
this.entry = entry
this.mutex = mutex
this.reader = new FileReader()
this.req = null
this.retry = true
this.locked = false
const self = this
this.reader.onerror = function () {
self.onread(this.error, null)
}
this.reader.onload = function () {
const buf = Buffer.from(this.result)
self.onread(null, buf)
}
}
ReadRequest.prototype.lock = function () {
if (this.locked) return true
this.locked = this.mutex.lock(this)
return this.locked
}
ReadRequest.prototype.onread = function (err, buf) {
const req = this.req
if (err && this.retry) {
this.retry = false
if (this.lock(this)) this.run(req)
return
}
this.req = null
this.pool.push(this)
this.retry = true
if (this.locked) {
this.locked = false
this.mutex.release()
}
req.callback(err, buf)
}
ReadRequest.prototype.run = function (req) {
this.entry.file(file => {
const end = req.offset + req.size
this.req = req
if (end > file.size) return this.onread(new Error('Could not satisfy length'), null)
this.reader.readAsArrayBuffer(file.slice(req.offset, end))
}, err => req.callback(err))
}
function DeleteRequest (pool, entry, mutex) {
this.pool = pool
this.entry = entry
this.mutex = mutex
this.writer = null
this.req = null
this.locked = false
}
DeleteRequest.prototype.makeWriter = function () {
const self = this
this.entry.createWriter(function (writer) {
self.writer = writer
writer.onwriteend = function () {
self.onwrite(null)
}
writer.onerror = function (err) {
self.onwrite(err)
}
self.run(self.req)
})
}
DeleteRequest.prototype.onwrite = function (err) {
const req = this.req
this.req = null
if (this.locked) {
this.locked = false
this.mutex.release()
}
this.pool.push(this)
req.callback(err, null)
}
DeleteRequest.prototype.lock = function () {
if (this.locked) return true
this.locked = this.mutex.lock(this)
return this.locked
}
DeleteRequest.prototype.run = function (req) {
this.entry.file(file => {
this.req = req
if (req.offset + req.size < file.size) return req.callback(null)
if (!this.writer) return this.makeWriter()
if (!this.lock()) return
this.writer.truncate(req.offset)
}, err => req.callback(err))
}