-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuplink.ts
301 lines (194 loc) · 6.74 KB
/
uplink.ts
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
/* eslint-disable */
import {AccessResultStruct} from "./access.js";
import bindings = require("bindings");
const uplink = bindings("uplink"),
defaultValue = 0;
const errorhandle = require("./error.js");
class ListBucketsOptions {
cursor: string;
constructor (cursor = "") {
this.cursor = cursor;
}
}
class Permission {
allow_download: boolean;
allow_upload: boolean;
allow_list: boolean;
allow_delete: boolean;
not_before: number;
not_after: number;
constructor (allow_download = false, allow_upload = false, allow_list = false, allow_delete = false, not_before = defaultValue, not_after = defaultValue) {
this.allow_download = allow_download;
this.allow_upload = allow_upload;
this.allow_list = allow_list;
this.allow_delete = allow_delete;
this.not_before = not_before;
this.not_after = not_after;
}
}
class UploadOptions {
expires: number;
constructor (expires = defaultValue) {
this.expires = expires;
}
}
class DownloadOptions {
offset: number;
length: number;
constructor (offset = defaultValue, length = defaultValue) {
this.offset = offset;
this.length = length;
}
}
class ListObjectsOptions {
prefix: string;
cursor: string;
recursive: boolean;
system: boolean;
custom: boolean;
constructor (prefix = "", cursor = "", recursive = false, system = false, custom = false) {
this.prefix = prefix;
this.cursor = cursor;
this.recursive = recursive;
this.system = system;
this.custom = custom;
}
}
class SharePrefix {
bucket: string;
prefix: string;
constructor (bucket = "", prefix = "") {
this.bucket = bucket;
this.prefix = prefix;
}
}
class CustomMetadataEntry {
key: string;
key_length: number;
value: string;
value_length: number;
constructor (key = "", key_length = defaultValue, value = "", value_length = defaultValue) {
this.key = key;
this.key_length = key_length;
this.value = value;
this.value_length = value_length;
}
}
class CustomMetadata {
entries: CustomMetadataEntry[];
count: number;
constructor (entries = [], count = defaultValue) {
this.entries = entries;
this.count = count;
}
}
class Config {
user_agent: string;
dial_timeout_milliseconds: number;
temp_directory: string;
constructor (user_agent = "", dial_timeout_milliseconds = defaultValue, temp_directory = "") {
this.user_agent = user_agent;
this.dial_timeout_milliseconds = dial_timeout_milliseconds;
this.temp_directory = temp_directory;
}
}
class Uplink {
/*
* Request_access_with_passphrase generates a new access grant using a passhprase.
* It must talk to the Satellite provided to get a project-based salt for deterministic
* key derivation.
* Note: this is a CPU-heavy function that uses a password-based key derivation
* function (Argon2). This should be a setup-only step.
* Most common interactions with the library should be using a serialized access grant
* through ParseAccess directly.
* Input : Satellite Address (String) , API key (String) , Encryption phassphrase(String)
* Output : Access (Object)
*/
async requestAccessWithPassphrase (satelliteURL: string, apiKey: string, encryptionPassphrase: string): Promise<AccessResultStruct> {
const access = await uplink.request_access_with_passphrase(
satelliteURL,
apiKey,
encryptionPassphrase
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
}),
accessResultReturn = new AccessResultStruct(access.access);
return accessResultReturn;
}
/*
* ParseAccess parses a serialized access grant string.
* This should be the main way to instantiate an access grant for opening a project.
* See the note on RequestAccessWithPassphrase
* Input : Shared string
* Output : Access (Object)
*/
async parseAccess (stringResult: string): Promise<AccessResultStruct> {
const parsedSharedAccess = await uplink.parse_access(stringResult).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
}),
accessResultReturn = new AccessResultStruct(parsedSharedAccess.access);
return accessResultReturn;
}
/*
* RequestAccessWithPassphrase generates a new access grant using a passhprase and custom configuration.
* It must talk to the Satellite provided to get a project-based salt for deterministic key derivation.
* Note: this is a CPU-heavy function that uses a password-based key derivation function (Argon2). This should be a setup-only step.
* Most common interactions with the library should be using a serialized access grant
* hrough ParseAccess directly.
* Input : Config (Object) , Satellite Address (String) , API key (String) , Encryption phassphrase(String)
* Output : Access (Object)
*/
async configRequestAccessWithPassphrase (config: Config, satelliteURL: string, apiKey: string, encryptionPassphrase: string): Promise<AccessResultStruct> {
const access = await uplink.config_request_access_with_passphrase(
config,
satelliteURL,
apiKey,
encryptionPassphrase
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
}),
accessResultReturn = new AccessResultStruct(access.access);
return accessResultReturn;
}
/*
* Input : Encryption phassphrase(String) , Array
* Output : Output : Encryptio_Key (Object)
*/
async uplinkDeriveEncryptionKey (phassphrase: string, salt: any): Promise<any> {
const encryption = await uplink.derive_encryption_key(
phassphrase,
salt,
salt.length
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return encryption;
}
}
// Exporting function and object
export {
Uplink,
DownloadOptions,
ListBucketsOptions,
Permission,
UploadOptions,
ListObjectsOptions,
CustomMetadataEntry,
CustomMetadata,
SharePrefix,
Config,
errorhandle
};
/* eslint-enable */