-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
162 lines (122 loc) · 2.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var ipfsAPI = require('ipfs-api')
var nconf = require('nconf');
// see if ipfs connection settings were configured in environment variables
nconf.env(
'IPFS_DAEMON_HOST'
'IPFS_DAEMON_PORT',
'IPFS_DAEMON_PROTOCOL'
);
var ipfsDaemonHost;
var ipfsDaemonPort;
var ipfsDaemonProtocol;
var options;
var ipfs;
ipfsDaemonHost = nconf.get('IPFS_DAEMON_HOST');
ipfsDaemonPort = nconf.get('IPFS_DAEMON_PORT');
ipfsDaemonProtocol = nconf.get('IPFS_DAEMON_PROTOCOL');
if (typeof (ipfsDaemonHost) !== 'undefined') {
// connect to ipfs daemon API server with configured values
options = {
host: ipfsDaemonHost,
port: ipfsDaemonPort,
protocol: ipfsDaemonProtocol
};
} else {
// connect to ipfs daemon API server with defaults
options = {
host: 'localhost',
port: '5001',
protocol: 'http'
}
}
ipfs = ipfsAPI(options);
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/**
* save
*
* saves game data to IPFS
*
* @param {JSON} data - game data to save
* @pram {onSavedCallback}
*/
var save = function save(data, cb) {
// make sure data we got was JSON
if (!isJson(data)) {
console.log(data);
var e = new Error('save data was not JSON');
console.error(e);
return cb(e);
}
ipfs.add(new Buffer(data), function (err, res) {
if (err) {
console.error(err);
console.error(res);
return cb(err);
}
//console.log('added to ipfs without issue');
console.log(res);
// @todo return to cb
return cb(null, res[0].Hash);
});
}
/**
* onSavedCallback
*
* @callback {onSavedCallback}
* @param {error} err
* @param {string} hash - the IPFS hash address to the saved data
*/
/**
* load
*
* loads game save data from ipfs
*
* @param {string} key - the key to the game data (an ipfs hash)
* @param {onLoadedCallback} cb
*/
var load = function load(key, cb) {
//console.log('loading');
//console.log(key);
// @todo make sure we're just getting one hash and not a list of hashes
ipfs.cat(key, function (err, res) {
if (err) {
var e = new Error('could not load save data from ipfs');
console.error(e);
return cb(e);
}
if (res.readable) {
console.log('piping');
var string = ''
res.on('readable', function () {
var part = res.read(); //.toString();
string += part;
//console.log('stream data ' + part);
});
res.on('end', function () {
//console.log('final output ' + string);
return cb(null, string);
});
} else {
//console.log('logging');
return cb(null, res);
}
});
}
/**
* onLoadedCallback
*
* @callback {onLoadedCallback}
* @param {error} err
* @param {JSON} data - game save data
*/
module.exports = {
save: save,
load: load
}