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
/
_base.js
221 lines (203 loc) · 6.04 KB
/
_base.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
// 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 makeError = require('./_error.js');
const errConst = require('../error_const');
const ffi = require('ffi');
const ref = require('ref');
const Enum = require('enum');
const Struct = require('ref-struct');
const ArrayType = require('ref-array');
const i32 = ref.types.int32;
const u32 = ref.types.uint32;
const u8 = ref.types.uint8;
const i64 = ref.types.int64;
const u64 = ref.types.uint64;
const u8Pointer = ref.refType(u8);
const Void = ref.types.void;
const VoidPtr = ref.refType(Void);
const usize = ref.types.size_t;
const bool = ref.types.bool;
const NULL = ref.types.NULL;
const CString = ref.types.CString;
const u8Array = ArrayType(u8);
const XOR_NAME = ArrayType(u8, 32);
const SYM_KEYBYTES = ArrayType(u8, 32);
const SYM_NONCEBYTES = ArrayType(u8, 24);
const SIGN_PUBLICKEYBYTES = ArrayType(u8, 32);
const SIGN_SECRETKEYBYTES = ArrayType(u8, 64);
const ASYM_PUBLICKEYBYTES = ArrayType(u8, 32);
const ASYM_SECRETKEYBYTES = ArrayType(u8, 32);
const ASYM_NONCEBYTES = ArrayType(u8, 24);
const ObjectHandle = u64;
const App = Struct({});
const AppPtr = ref.refType(App);
const validPerms = new Enum({
Read: 0,
Insert: 1,
Update: 2,
Delete: 3,
ManagePermissions: 4
});
const validatePermission = (perm) => {
if (!validPerms.get(perm)) throw makeError(errConst.INVALID_PERM.code, errConst.INVALID_PERM.msg(perm));
}
const FfiResult = Struct({
error_code: i32,
error_description: 'string'
});
const FfiResultPtr = ref.refType(FfiResult);
const PermissionSet = Struct({
Read: bool,
Insert: bool,
Update: bool,
Delete: bool,
ManagePermissions: bool
});
const PermissionSetPtr = ref.refType(PermissionSet);
const makeFfiResult = (resultPtr) => {
const ffiResult = resultPtr.deref();
return {
error_code: ffiResult.error_code,
error_description: ffiResult.error_description
};
}
// Internal helper functions
const normaliseTypes = (rTypes) => {
let types = ['pointer', FfiResultPtr]; // we always have: user_data and FfiResultPtr
if (Array.isArray(rTypes)) {
types = [...types, ...rTypes];
} else if (rTypes) {
types.push(rTypes);
}
return types;
}
const callLibFn = (fn, args, types, postProcess) => {
return new Promise((resolve, reject) => {
// append the callback to receive the result to the args
args.push(ffi.Callback("void", types,
(uctx, resultPtr, ...restArgs) => {
const result = makeFfiResult(resultPtr);
if (result.error_code !== 0) {
// error found, errback with translated error
return reject(makeError(result.error_code, result.error_description));
}
let res;
if (postProcess) {
// we are post-processing the entry
res = postProcess(restArgs);
} else if (types.length === 3){
// no post-processing but given only one
// item given, use instead of array.
res = restArgs[0];
} else {
res = [...restArgs];
}
resolve(res);
}));
// and call the function
fn(...args);
});
}
module.exports = {
types: {
App,
AppPtr,
FfiResult,
FfiResultPtr,
PermissionSet,
PermissionSetPtr,
ObjectHandle,
XOR_NAME,
SYM_KEYBYTES,
SYM_NONCEBYTES,
SIGN_PUBLICKEYBYTES,
SIGN_SECRETKEYBYTES,
ASYM_PUBLICKEYBYTES,
ASYM_SECRETKEYBYTES,
ASYM_NONCEBYTES,
VoidPtr,
i32,
u32,
bool,
i64,
u64,
u8,
u8Array,
u8Pointer,
Void,
usize,
NULL,
CString
},
helpers: {
fromCString: (cstr) => cstr.readCString(),
asBuffer: (res) => {
let b = ref.isNull(res[0]) ? Buffer.alloc(0) : Buffer.from(ref.reinterpret(res[0], res[1]));
return b;
},
toSafeLibTime: (now) => {
const now_msec = now.getTime();
const now_msec_part = (now_msec % 1000);
const now_sec_part = (now_msec - now_msec_part) / 1000;
const now_nsec_part = 1000000 * now_msec_part;
return {now_sec_part, now_nsec_part};
},
fromSafeLibTime: (sec, nsec_part) => {
let d = new Date();
d.setTime((sec * 1000) + (nsec_part / 1000000));
return d;
},
makePermissionSet: (perms) => {
const permsObj = new PermissionSet({
Read: false,
Insert: false,
Update: false,
Delete: false,
ManagePermissions: false
});
perms.map((perm) => {
validatePermission(perm);
permsObj[perm] = true;
});
return permsObj;
},
makeFfiResult,
Promisified: (formatter, rTypes, after) => {
// create internal function that will be
// invoked on top of the direct binding
// mixing a callback into the arguments
// and returning a promise
return (lib, fn) => ((...varArgs) => {
// the internal function that wraps the actual function call
// compile the callback-types-definiton
let args;
let types = normaliseTypes(rTypes);
return new Promise((resolve, reject) => {
// if there is a formatter, we are reformatting
// the incoming arguments first
try {
args = formatter ? formatter(...varArgs): [...varArgs];
} catch(err) {
// reject promise if error is thrown by the formatter
return reject(err);
}
// append just user-context to the argument since the
// result callback is appended and handled by callLibFn
args.push(ref.NULL);
return callLibFn(fn, args, types, after)
.then(resolve)
.catch(reject);
});
});
}
}
}