forked from holochain/holochain-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 1.52 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
const binary = require('node-pre-gyp');
const fs = require('fs');
const path = require('path');
// deals with ensuring the correct version for the machine/node version
const binding_path = binary.find(path.resolve(path.join(__dirname, './package.json')));
const { makeConfig, Container } = require(binding_path);
Container.prototype.callRaw = Container.prototype.call
Container.prototype.call = function (id, zome, trait, fn, params) {
const stringInput = JSON.stringify(params);
const rawResult = this.callRaw(id, zome, trait, fn, stringInput);
let result;
try {
result = JSON.parse(rawResult);
} catch (e) {
console.log("JSON.parse failed to parse the result. The raw value is: ", rawResult);
result = { error: "JSON.parse failed to parse the result", rawResult };
}
return result;
}
// Convenience function for making an object that can call into the container
// in the context of a particular instance. This may be temporary.
Container.prototype.makeCaller = function (agentId, dnaPath) {
const instanceId = agentId + '::' + dnaPath
return {
call: (zome, cap, fn, params) => this.call(instanceId, zome, cap, fn, params),
agentId: this.agent_id(instanceId)
}
}
const Config = {
agent: name => ({ name }),
dna: (path) => ({ path }),
instance: (agent, dna, name) => {
if (!name) {
name = agent.name
}
return { agent, dna, name }
},
container: (instances) => makeConfig(instances),
}
module.exports = { Config, Container };