-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
74 lines (68 loc) · 2.34 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
class LotusRPC {
constructor (provider, { schema }) {
this.provider = provider
this.schema = schema
return new Proxy(this, {
get: (obj, prop) => {
if (prop in obj) {
return obj[prop]
} else if (prop === Symbol.iterator) {
return undefined
} else if (prop === Symbol.toStringTag) {
return undefined
} else if (prop === '$$typeof') {
return undefined
} else if (prop === 'then') {
return undefined
} else {
let method = prop.charAt(0).toUpperCase() + prop.slice(1)
const schemaMethod = schema.methods[method]
if (schemaMethod) {
if (schemaMethod.namespace && method.startsWith(schemaMethod.namespace)) {
// Namespaces other than "Filecoin" are permitted such that name of the
// `schemaMethod` provided in the schema is `NamespaceMethod` for the respective
// `Namespace.Method` method. Further, the `schemaMethod` needs to provide
// the `namespace` string set to `Namespace`. This allows calling
// provider.namespaceMethod() or provider.NamespaceMethod().
method = `${schemaMethod.namespace}.${method.slice(schemaMethod.namespace.length)}`
} else {
method = `Filecoin.${method}`
}
if (schemaMethod.subscription) {
return this.callSchemaMethodSub.bind(this, method, schemaMethod)
} else {
return this.callSchemaMethod.bind(this, method, schemaMethod)
}
} else {
// FIXME: throw?
console.warn(`Unknown method ${method}`)
}
}
}
})
}
async callSchemaMethod (method, schemaMethod, ...args) {
await this.provider.connect()
const request = {
method
}
request.params = args
return this.provider.send(request, schemaMethod)
}
callSchemaMethodSub (method, schemaMethod, ...args) {
// await this.provider.connect()
const request = {
method
}
const cb = args[0]
request.params = args.slice(1)
return this.provider.sendSubscription(request, schemaMethod, cb)
}
async importFile (body) {
return this.provider.importFile(body)
}
async destroy () {
await this.provider.destroy()
}
}
module.exports = { LotusRPC }