-
Notifications
You must be signed in to change notification settings - Fork 13
/
local-api.js
41 lines (34 loc) · 1012 Bytes
/
local-api.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
'use strict'
const Permissions = require('./permissions')
const u = require('./util')
module.exports = function createLocalCall (api, manifest, perms) {
perms = Permissions(perms)
function has (type, name) {
return type === u.get(manifest, name)
}
function localCall (type, name, args) {
if (name === 'emit') throw new Error('emit has been removed')
// is there a way to know whether it's sync or async?
if (type === 'async') {
if (has('sync', name)) {
const cb = args.pop()
let value
try {
value = u.get(api, name).apply(this, args)
} catch (err) {
return cb(err)
}
return cb(null, value)
}
}
if (!has(type, name)) {
throw new Error(`no ${type}:${name}`)
}
return u.get(api, name).apply(this, args)
}
return function localCallWithPerms (type, name, args) {
const err = perms.pre(name, args)
if (err) throw err
return localCall.call(this, type, name, args)
}
}