This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
_app.js
116 lines (107 loc) · 4.87 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
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under
// the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> or
// the Modified BSD license <LICENSE-BSD or https://opensource.org/licenses/BSD-3-Clause>,
// at your option.
//
// This file may not be copied, modified, or distributed except according to those terms.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
const ffi = require('ffi');
const ref = require("ref");
const Struct = require('ref-struct');
const makeError = require('./_error.js');
const { types: t, helpers } = require('./_base');
const { helpersForNative } = require('./_auth.js');
const { types } = require('./_auth');
const AuthGranted = types.AuthGranted;
const consts = require('../consts');
const errConst = require('../error_const');
const AccountInfo = Struct({
mutations_done: t.u64,
mutations_available: t.u64
});
module.exports = {
functions: {
app_account_info: [t.Void, [t.AppPtr, t.VoidPtr, 'pointer']],
app_unregistered: [t.Void ,[t.u8Pointer, t.usize, t.VoidPtr, 'pointer', 'pointer']],
app_registered: [t.Void , ['string', ref.refType(AuthGranted), t.VoidPtr, 'pointer', 'pointer']],
app_reconnect: [t.Void, [t.AppPtr, t.VoidPtr, 'pointer']],
app_free: [t.Void, [t.AppPtr]],
app_reset_object_cache: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
app_is_mock: [t.bool, []],
app_set_additional_search_path: [t.Void, ['string', 'pointer', 'pointer']],
app_container_name: [t.Void, ['string', t.VoidPtr, 'pointer']]
},
api: {
app_account_info: helpers.Promisified(null, ref.refType(AccountInfo), (accInfoPtr) => {
const accInfo = accInfoPtr[0].deref();
return { mutations_done: accInfo.mutations_done, mutations_available: accInfo.mutations_available }
}),
app_unregistered: (lib, fn) => {
return ((app, uri) => {
const disconnect_notifier_cb = ffi.Callback("void", [t.VoidPtr], (user_data) => app._networkStateUpdated(user_data, consts.NET_STATE_DISCONNECTED));
return new Promise((resolve, reject) => {
if (!uri) reject(makeError(errConst.MISSING_AUTH_URI.code, errConst.MISSING_AUTH_URI.msg));
const uriBuf = Buffer.isBuffer(uri) ? uri : (uri.buffer || Buffer.from(uri));
const result_cb = ffi.Callback("void", [t.VoidPtr, t.FfiResultPtr, t.AppPtr], (user_data, resultPtr, appCon) => {
const result = helpers.makeFfiResult(resultPtr);
if (result.error_code !== 0) {
reject(makeError(result.error_code, result.error_description));
return;
}
app.connection = appCon;
app._networkStateUpdated(null, consts.NET_STATE_CONNECTED);
resolve(app);
});
fn.apply(fn, [uriBuf, uriBuf.length, ref.NULL, disconnect_notifier_cb, result_cb]);
});
})
},
app_registered: (lib, fn) => {
return ((app, authGrantedObj) => {
const disconnect_notifier_cb = ffi.Callback("void", [t.VoidPtr], (user_data) => app._networkStateUpdated(user_data, consts.NET_STATE_DISCONNECTED));
return new Promise((resolve, reject) => {
const result_cb = ffi.Callback("void", [t.VoidPtr, t.FfiResultPtr, t.AppPtr], (user_data, resultPtr, appCon) => {
const result = helpers.makeFfiResult(resultPtr);
if (result.error_code !== 0) {
return reject(makeError(result.error_code, result.error_description));
}
app.connection = appCon;
app._networkStateUpdated(null, consts.NET_STATE_CONNECTED);
resolve(app);
});
const authGranted = helpersForNative.makeAuthGrantedFfiStruct(authGrantedObj);
fn.apply(fn, [app.appInfo.id, authGranted.authGranted.ref(), ref.NULL, disconnect_notifier_cb, result_cb]);
});
});
},
app_reconnect: (lib, fn) => {
return ((app) => {
return new Promise((resolve, reject) => {
const result_cb = ffi.Callback("void", [t.VoidPtr, t.FfiResultPtr], (user_data, resultPtr) => {
const result = helpers.makeFfiResult(resultPtr);
if (result.error_code !== 0) {
return reject(makeError(result.error_code, result.error_description));
}
app._networkStateUpdated(null, consts.NET_STATE_CONNECTED);
resolve(result);
});
fn.apply(fn, [app.connection, ref.NULL, result_cb]);
});
});
},
app_free: (lib, fn) => {
return ((app) => {
fn(app);
return Promise.resolve();
});
},
app_reset_object_cache: helpers.Promisified(null, []),
app_is_mock: (lib, fn) => (() => { return fn(); }),
app_set_additional_search_path: helpers.Promisified(null, []),
app_container_name: helpers.Promisified(null, 'string')
}
};