This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
/
app.js
426 lines (387 loc) · 12.4 KB
/
app.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const { EventEmitter } = require('events');
const { autoref } = require('./helpers');
const api = require('./api');
const lib = require('./native/lib');
const { parse: parseUrl } = require('url');
const consts = require('./consts');
const makeFfiError = require('./native/_error.js');
const errConst = require('./error_const');
/**
* Holds one sessions with the network and is the primary interface to interact
* with the network. As such it also provides all API-Providers connected through
* this session.
*/
class SAFEApp extends EventEmitter {
/**
* @private
* Initiate a new App instace. Wire up all the API's and set up the
* authentication URI-handler with the system.
*
* @param {AppInfo} appInfo
* @param {Function} [networkStateCallBack=null] optional callback function
* to receive network state updates
* @param {InitOptions} initilalisation options
*/
constructor(appInfo, networkStateCallBack, options) {
super();
this.options = Object.assign({
log: true,
registerScheme: true,
configPath: null
}, options);
lib.init(this.options);
this._appInfo = appInfo;
this.validateAppInfo();
this.networkState = consts.NET_STATE_INIT;
if (networkStateCallBack) {
this._networkStateCallBack = networkStateCallBack;
}
this.connection = null;
Object.getOwnPropertyNames(api).forEach((key) => {
this[`_${key}`] = new api[key](this);
});
// Init logging on the underlying library only if it wasn't done already
if (this.options.log && !SAFEApp.logFilePath) {
let filename = `${appInfo.name}.${appInfo.vendor}`.replace(/[^\w\d_\-.]/g, '_');
filename = `${filename}.log`;
lib.app_init_logging(filename)
.then(() => lib.app_output_log_path(filename))
.then((logPath) => { SAFEApp.logFilePath = logPath; })
.catch((err) => { console.error('Logger initilalisation failed', err); });
}
// Set additional search path for the config files if it was requested in
// the options. E.g. log.toml and crust.config files will be search
// in this additional search path.
if (this.options.configPath) {
lib.app_set_additional_search_path(this.options.configPath)
.catch((err) => { console.error('Faled to set additional config search path', err); });
}
}
/**
* get the AuthInterface instance connected to this session
* @returns {AuthInterface}
*/
get auth() {
return this._auth;
}
/**
* get the Crypto instance connected to this session
* @returns {CryptoInterface}
*/
get crypto() {
return this._crypto;
}
/**
* get the CipherOptInterface instance connected to this session
* @returns {CipherOptInterface}
*/
get cipherOpt() {
return this._cipherOpt;
}
/**
* get the ImmutableDataInterface instance connected to this session
* @returns {ImmutableDataInterface}
*/
get immutableData() {
return this._immutableData;
}
/**
* get the MutableDataInterface instance connected to this session
* @returns {MutableDataInterface}
*/
get mutableData() {
return this._mutableData;
}
/*
* Validates appInfo and properly handles error
*/
validateAppInfo() {
const appInfo = this._appInfo;
const appInfoMustHaveProperties = ['id', 'name', 'vendor'];
let bool = false;
const hasCorrectProperties = appInfoMustHaveProperties.every((prop) => {
if (appInfo[prop]) {
appInfo[prop] = appInfo[prop].trim();
}
bool = Object.prototype.hasOwnProperty.call(appInfo, prop) && appInfo[prop];
return bool;
});
if (!hasCorrectProperties) {
throw makeFfiError(errConst.MALFORMED_APP_INFO.code, errConst.MALFORMED_APP_INFO.msg);
}
}
/**
* Helper to lookup a given `safe://`-url in accordance with the
* convention and find the requested object.
*
* @arg {String} url the url you want to fetch
* @returns {Promise<File>} the file object found for that URL
*/
async webFetch(url) {
if (!url) return Promise.reject(new Error('No URL provided.'));
const parsedUrl = parseUrl(url);
if (!parsedUrl) return Promise.reject(new Error('Not a proper URL!'));
const hostname = parsedUrl.hostname;
let path = parsedUrl.pathname ? decodeURI(parsedUrl.pathname) : '';
const tokens = path.split('/');
if (!tokens[tokens.length - 1] && tokens.length > 1) {
tokens.pop();
}
path = tokens.join('/') || `/${consts.INDEX_HTML}`;
const PUBLIC_NAME_NOT_FOUND = -103;
const SERVICE_NOT_FOUND = -106;
// const NFS_NOT_FOUND = -301;
// lets' unpack
const hostParts = hostname.split('.');
const lookupName = hostParts.pop(); // last one is 'domain'
const serviceName = hostParts.join('.') || 'www'; // all others are 'service'
return new Promise(async (resolve, reject) => {
const getServiceInfo = async (pubName, servName) => {
try {
const address = await this.crypto.sha3Hash(pubName);
const servicesContainer = await this.mutableData.newPublic(address, consts.TAG_TYPE_DNS);
return await servicesContainer.get(servName);
} catch (err) {
if (err.code === PUBLIC_NAME_NOT_FOUND || err.code === SERVICE_NOT_FOUND) {
const error = new Error();
error.code = err.code;
error.message = `Requested ${err.code === PUBLIC_NAME_NOT_FOUND ? 'public name' : 'service'} is not found`;
throw error;
}
throw err;
}
};
// const getFile = async (emulation, path, shouldThrow) => {
// try {
// console.log('Fetching', path);
// let file = await emulation.fetch(path);
// return;
// } catch (e) {
// console.log(e.code, -301)
// if (e.code !== -301) {
// throw e;
// }
// if (shouldThrow) {
// throw e;
// }
// }
// };
const handleNfsFetchException = (error) => {
if (error.code !== -301) {
throw error;
}
};
try {
const serviceInfo = await getServiceInfo(lookupName, serviceName);
if (serviceInfo.buf.length === 0) {
const error = new Error();
error.code = SERVICE_NOT_FOUND;
error.message = 'Service not found';
return reject(error);
}
let serviceMd;
try {
serviceMd = await this.mutableData.fromSerial(serviceInfo.buf);
} catch (e) {
serviceMd = await this.mutableData.newPublic(serviceInfo.buf, consts.TAG_TYPE_WWW);
}
const emulation = await serviceMd.emulateAs('NFS');
let file;
try {
file = await emulation.fetch(path);
} catch (e) {
handleNfsFetchException(e);
}
if (!file && path.startsWith('/')) {
try {
file = await emulation.fetch(path.replace('/', ''));
} catch (e) {
handleNfsFetchException(e);
}
}
if (!file && path.split('/').length > 1) {
try {
file = await emulation.fetch(`${path}/${consts.INDEX_HTML}`);
} catch (e) {
handleNfsFetchException(e);
}
}
if (!file) {
file = await emulation.fetch(`${path}/${consts.INDEX_HTML}`.replace('/', ''));
}
const openedFile = await emulation.open(file, consts.pubConsts.NFS_FILE_MODE_READ);
const data = await openedFile.read(
consts.pubConsts.NFS_FILE_START, consts.pubConsts.NFS_FILE_END);
resolve(data);
} catch (e) {
reject(e);
}
});
}
/**
* @private
* Replace the connection to the native layer. When there is already one
* set up for the current app, free it on the native layer. Should only be
* used at startup/beginning as it will devaluate all handlers that might
* still be around after switching.
*
* @param {Pointer} con - the pointer to the native object
*/
set connection(con) {
if (this._connection) {
lib.app_free(this._connection);
}
this._connection = con;
}
/**
* The current connection object hold on the Rust-Side
* @returns {Pointer}
*/
get connection() {
if (!this._connection) throw Error('Setup Incomplete. Connection not available yet.');
return this._connection;
}
/**
* @private
* Set the new network state based on the state code provided.
*
* @param {Number} state
*/
set networkState(state) {
this._networkState = state;
}
/**
* Textual representation of the current network connection state.
*
* @returns {String} current network connection state
*/
get networkState() {
// Although it should never happen, if the state code is invalid
// we return the current network conn state as 'Unknown'.
let currentState = 'Unknown';
switch (this._networkState) {
case consts.NET_STATE_INIT:
currentState = 'Init';
break;
case consts.NET_STATE_DISCONNECTED:
currentState = 'Disconnected';
break;
case consts.NET_STATE_CONNECTED:
currentState = 'Connected';
break;
default:
break;
}
return currentState;
}
/**
* Returns true if current network connection state is INIT.
* This is state means the library has been initialised but there is no
* connection made with the network yet.
*
* @returns {Boolean}
*/
isNetStateInit() {
return this._networkState === consts.NET_STATE_INIT;
}
/**
* Returns true if current network connection state is CONNECTED.
*
* @returns {Boolean}
*/
isNetStateConnected() {
return this._networkState === consts.NET_STATE_CONNECTED;
}
/**
* Returns true if current network connection state is DISCONNECTED.
*
* @returns {Boolean}
*/
isNetStateDisconnected() {
return this._networkState === consts.NET_STATE_DISCONNECTED;
}
/**
* The current appInfo
*/
get appInfo() {
return this._appInfo;
}
/**
* Generate the log path for the provided filename.
* If the filename provided is null, it then returns
* the path of where the safe_core log file is located.
* @param {String} [logFilename=null] optional log filename to generate the path
*
* @returns {Promise<String>}
*/
/* eslint-disable class-methods-use-this */
logPath(logFilename) {
const filename = logFilename;
if (!logFilename) {
return Promise.resolve(SAFEApp.logFilePath);
}
return lib.app_output_log_path(filename);
}
getAccountInfo() {
return lib.app_account_info(this.connection);
}
/**
* Create a SAFEApp and try to login it through the `authUri`
* @param {AppInfo} appInfo - the AppInfo
* @param {String} authUri - URI containing the authentication info
* @param {Function} [networkStateCallBack=null] optional callback function
* to receive network state updates
* @param {InitOptions} initialisation options
* @returns {Promise<SAFEApp>} authenticated and connected SAFEApp
*/
static fromAuthUri(appInfo, authUri, networkStateCallBack, options) {
const app = autoref(new SAFEApp(appInfo, networkStateCallBack, options));
return app.auth.loginFromURI(authUri);
}
/**
* @private
* Called from the native library whenever the network state
* changes.
*/
_networkStateUpdated(userData, newState) {
const prevState = this.networkState;
this.networkState = newState;
this.emit('network-state-updated', this.networkState, prevState);
this.emit(`network-state-${this.networkState}`, prevState);
if (this._networkStateCallBack) {
this._networkStateCallBack.apply(this._networkStateCallBack, [this.networkState]);
}
}
/**
* Reconnect to the metwork
* Must be invoked when the client decides to connect back after the connection was lost.
*/
reconnect() {
return lib.app_reconnect(this.connection)
.then(() => this._networkStateUpdated(null, consts.NET_STATE_CONNECTED));
}
/**
* @private
* free the app. used by the autoref feature
* @param {SAFEApp} app - the app to free
*/
static free(app) {
// we are freed last, anything you do after this
// will probably fail.
lib.app_free(app.connection);
}
/**
* Resets the object cache kept by the underlyging library.
*/
clearObjectCache() {
return lib.app_reset_object_cache(this.connection);
}
/**
* Retuns true if the underlyging library was compiled against mock-routing.
*/
isMockBuild() {
return lib.is_mock_build();
}
}
SAFEApp.logFilename = null;
module.exports = SAFEApp;