forked from internetarchive/dweb-transports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransportFLUENCE.js
308 lines (250 loc) · 11.3 KB
/
TransportFLUENCE.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const Url = require('url');
const Transport = require('./Transport'); // Base class for TransportXyz
const Transports = require('./Transports'); // Manage all Transports that are loaded
const { TransportError, CodingError } = require('./Errors'); // Standard Dweb Errors
const canonicaljson = require('@stratumn/canonicaljson');
//const fluence = require('fluence');
const debug = require('debug')('dweb-transports:fluence');
const defaultOptions = {
nodeUrl: 'https://ia-redis.fluence.one',
nodePort: 443,
appId: '4' // Redis
};
class TransportFLUENCE extends Transport {
constructor(options) {
super(options);
this.options = options; // Dictionary of options
this.session = undefined;
this.name = 'FLUENCE'; // For console log etc
this.supportURLs = ['fluence'];
this.supportFunctions = [
// General data functions
'fetch', // p_rawfetch(url, {timeoutMS, start, end, relay}) – Fetch some bytes based on a url
// Lists functions
'list', // p_rawlist(url) – Fetch all the objects in a list .. identified by the url of the .. 'signedby' parameter of the p_rawadd call
'add', // p_rawadd(url, sig) – Store a new list item, it should be stored so that it can be retrieved either by "signedby" (using p_rawlist) or by "url" (with p_rawreverse).
'newlisturls', // p_newlisturls(cl) – Obtain a pair of URLs for a new list
// KeyValueTable functions
'newdatabase', // p_newdatabase(pubkey) – Create a new database based on some existing object
'newtable', // p_newtable(pubkey, table) – Create a new table
'get', // p_get(url, keys) – Get one or more keys from a table
'set', // p_set(url, keyvalues, value) – Set one or more keys in a table.
'getall',// p_getall(url) – Return a dictionary representing the table
'keys', // p_keys(url) – Return a list of keys in a table (suitable for iterating through)
];
this.supportFeatures = [];
this.setStatus(Transport.STATUS_LOADED);
}
static setup0(options) {
const combinedOptions = Transport.mergeoptions(defaultOptions, options.fluence);
console.assert(combinedOptions.nodeUrl, 'Fluence Node url should be specified');
console.assert(combinedOptions.nodePort, 'Fluence Node port should be specified');
console.assert(combinedOptions.appId, 'Fluence AppId should be specified');
let t = new TransportFLUENCE(combinedOptions);
Transports.addtransport(t);
return t;
}
async p_setup1() {
try {
this.setStatus(Transport.STATUS_STARTING);
debug('connecting...');
const rndString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
this.session = fluence.directConnect(this.options.nodeUrl, this.options.nodePort, this.options.appId, rndString);
debug('connected.');
this.setStatus(Transport.STATUS_CONNECTED);
} catch (err) {
console.error(this.name, 'failed to start', err);
this.setStatus(Transport.STATUS_FAILED);
}
return this;
}
async p_status() {
if (this.session !== null && this.session !== undefined) {
this.setStatus(Transport.STATUS_CONNECTED);
}
return super.p_status();
}
parseUrl(url) {
const parsedUrl = Url.parse(url);
if (parsedUrl.protocol !== 'fluence:') {
throw new TransportError(`TransportFLUENCE Error encountered retrieving val: url (${parsedUrl.href}) is not a valid FLUENCE url | protocol = ${parsedUrl.protocol}`);
}
debug('retrieve url', parsedUrl.href);
return parsedUrl;
}
newKey(pubkey) {
if (pubkey.hasOwnProperty("keypair")) {
pubkey = pubkey.keypair.signingexport();
}
// By this point pubkey should be an export of a public key of form xyz:abc where xyz
// specifies the type of public key (NACL VERIFY being the only kind we expect currently)
return `fluence:/fluence/${encodeURIComponent(pubkey)}`;
}
parseRedisResponse(result) {
if (result.startsWith('$-1')) {
return undefined;
}
function parseResponsePart(result) {
const [, type, countStr] = /^([\+\-\:\$\*]{1})([0-9]+)/.exec(result);
const count = Number(countStr);
switch (type) {
case '+': { // Simple string
const offset = 1;
const [, data] = /([^\n\r]+)/.exec(result.substring(offset));
return {
data: data,
offset: offset + data.length + 2
};
}
case ':': { // Integer
const offset = 1;
const [, data] = /([0-9]+)/.exec(result.substring(offset));
return {
data: Number(data),
offset: offset + data.length + 2
};
}
case '$': { // Bulk string
const offset = 1 + String(count).length + 2;
return {
data: result.substring(offset, offset + count),
offset: offset + count + 2
};
}
case '*': { // Array
let offset = 1 + String(count).length + 2;
const list = [];
for(let i = 0;i < count;i++) {
const parsedListItem = parseResponsePart(result.substring(offset));
list.push(parsedListItem.data);
offset += parsedListItem.offset;
}
return {
data: list,
offset: null
};
}
default: {
throw new TransportError(`TransportFLUENCE Error unsupprted Redis response type: ${type}, response: ${result}`);
}
}
}
return parseResponsePart(result).data;
}
// General data functions (uses Redis basic GET\SET)
async p_rawfetch(url) {
const parsedUrl = this.parseUrl(url);
const key = parsedUrl.path;
const result = await this.session.request(`GET ${key}`);
const data = this.parseRedisResponse(result.asString());
if (!data) {
throw new TransportError(`TransportFLUENCE unable to retrieve: ${url.href}`);
}
return typeof data === 'string' ? JSON.parse(data) : data;
}
// List functions (uses Redis list)
async p_rawlist(url) {
const parsedUrl = this.parseUrl(url);
const key = parsedUrl.path;
const result = await this.session.request(`LRANGE ${key} 0 -1`);
const data = this.parseRedisResponse(result.asString());
if (!data) {
throw new TransportError(`TransportFLUENCE unable to retrieve list: ${url.href}`);
}
return data.map(listItem => typeof listItem === 'string' ? JSON.parse(listItem) : listItem);
}
async p_rawadd(url, sig) {
const parsedUrl = this.parseUrl(url);
const key = parsedUrl.path;
const data = canonicaljson.stringify( sig.preflight( Object.assign({}, sig)));
await this.session.request(`RPUSH ${key} ${data}`);
}
async p_newlisturls(cl) {
const key = this.newKey(cl);
return [key, key];
}
// KeyValueTable functions (uses Redis hashes)
async p_newdatabase(pubkey) {
/*
Request a new database
returns: { publicurl: "fluence:/fluence/<publickey>", privateurl: "fluence:/fluence/<publickey>"> }
*/
let key = await this.newKey(pubkey);
return {
publicurl: key,
privateurl: key
};
}
async p_newtable(pubkey, table) {
/*
Request a new table
returns: {publicurl: "fluence:/fluence/<publickey>/<table>", privateurl: "fluence:/fluence/<publickey>/<table>">
*/
if (!pubkey) {
throw new CodingError("p_newtable currently requires a pubkey");
}
const { publicurl, privateurl } = await this.p_newdatabase(pubkey);
return {
privateurl: `${privateurl}/${table}`,
publicurl: `${publicurl}/${table}`
};
}
async p_set(url, keyvalues, value) { // url = fluence:/fluence/<publickey>/<table>
/*
Set key values
keyvalues: string (key) in which case value should be set there OR
object in which case value is ignored
*/
if (typeof keyvalues === 'string') {
await this.session.request(`HSET ${url} ${keyvalues} ${canonicaljson.stringify(value)}`);
} else {
// Store all key-value pairs without destroying any other key/value pairs previously set
console.assert(!Array.isArray(keyvalues), 'TransportFLUENCE - shouldnt pass an array as the keyvalues');
await Promise.all(
Object.keys(keyvalues).map(hKey => this.session.request(`HSET ${url} ${hKey} ${canonicaljson.stringify(keyvalues[hKey])}`))
);
}
}
async p_get(url, keys) {
if (Array.isArray(keys)) {
const result = await this.session.request(`HMGET ${url} ${keys.join(' ')}`);
const data = this.parseRedisResponse(result.asString());
return keys.reduce((store, key, index) => {
const keyValue = data[index];
store[key] = typeof keyValue === "string" ? JSON.parse(keyValue) : keyValue;
return store;
}, {});
} else {
const result = await this.session.request(`HGET ${url} ${keys}`);
let data = this.parseRedisResponse(result.asString());
return typeof data === 'string' ? JSON.parse(data) : data;
}
}
async p_keys(url) {
const result = await this.session.request(`HKEYS ${url}`);
return this.parseRedisResponse(result.asString());
}
async p_getall(url) {
const result = await this.session.request(`HGETALL ${url}`);
const dataArray = this.parseRedisResponse(result.asString());
return dataArray.reduce((store, key, index, dataArray) => {
if (index % 2 !== 0) {
return store;
}
const keyValue = dataArray[index + 1];
store[key] = typeof keyValue === "string" ? JSON.parse(keyValue) : keyValue;
return store;
}, {});
}
async p_delete(url, keys) {
if (typeof keys === "string") {
await this.session.request(`HDEL ${url} ${keys}`);
} else {
await this.session.request(`HDEL ${url} ${keys.join(' ')}`);
}
}
}
Transports._transportclasses['FLUENCE'] = TransportFLUENCE;
TransportFLUENCE.scripts = ["fluence@0.3.14-no-webj/bundle/bundle.js"];
TransportFLUENCE.requires = {"fluence": "fluence"};
exports = module.exports = TransportFLUENCE;