-
Notifications
You must be signed in to change notification settings - Fork 2
/
bridge.js
59 lines (50 loc) · 1.43 KB
/
bridge.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
const raf = require('random-access-file')
const {Request, Callback, Action} = require('./proto')
const rafMap = new Map()
function RasBridge (getRas) {
return function (data, cb) {
data = Request.decode(data)
if (!rafMap.has(data.name)) {
rafMap.set(data.name, getRas(data.name))
}
const current = rafMap.get(data.name)
data.callback = function (error, arg) {
const response = {id: data.id, error: error, name: data.name, stat: null, data: null}
if (arg !== undefined) {
if (Buffer.isBuffer(arg)) response.data = arg
else response.stat = arg
}
cb(Callback.encode(response))
}
propagate(data, current)
}
}
function propagate (request, ras) {
switch (request.action) {
case Action.OPEN:
ras.open(request.callback)
return
case Action.OPENREADONLY:
ras.open(request.callback)
return
case Action.READ:
ras.read(request.offset, request.size, request.callback)
return
case Action.WRITE:
ras.write(request.offset, request.data, request.callback)
return
case Action.DEL:
ras.del(request.offset, request.size, request.callback)
return
case Action.STAT:
ras.stat(request.callback)
return
case Action.CLOSE:
ras.close(request.callback)
return
case Action.DESTROY:
ras.destroy(request.callback)
}
}
module.exports = RasBridge
module.exports.propagate = propagate