-
Notifications
You must be signed in to change notification settings - Fork 5
/
mutable-file-wrapper.js
69 lines (62 loc) · 1.57 KB
/
mutable-file-wrapper.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
module.exports = function mutableStorage (options) {
const randomAccess = require('random-access-storage')
const mutableAccess = require('random-access-idb-mutable-file')
let mounted = null
let loading = null
function doMount () {
return mutableAccess.mount(options).then((requestFile) => {
mounted = requestFile
loading = null
})
}
return (name) => {
let file = null
return randomAccess({
open: function (req) {
if (!mounted && !loading) {
loading = doMount()
}
if(loading) {
loading.then(() => {
this._open(req)
}, (err) => {
req.callback(err)
})
return
}
file = mounted(name)
req.callback()
},
write: function (req) {
file.write(req.offset, req.data, function(err, data) {
req.callback(err, data)
})
},
read: function (req) {
file.read(req.offset, req.size, function(err, data) {
req.callback(err, data)
})
},
del: function (req) {
file.del(req.offset, req.size, function(err, data) {
req.callback(err, data)
})
},
stat: function (req) {
file.stat( function(err, data) {
req.callback(err, data)
})
},
close: function (req) {
file.close( function(err, data) {
req.callback(err, data)
})
},
destroy: function (req) {
file.destroy( function(err, data) {
req.callback(err, data)
})
}
})
}
}